Skip to content

Commit

Permalink
Support conversion with sr3d
Browse files Browse the repository at this point in the history
  • Loading branch information
reidsunderland committed Dec 1, 2023
1 parent bce19ae commit db3cfe5
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
112 changes: 112 additions & 0 deletions bin/sr3_action_convert
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/bash

#
# sr3 convert
#
# Runs a convert on one node and pulls the config(s) back to your workstation.
# - Runs sr3 convert
# - Runs the plugins conversion tool, if it exists (internal script)
# - Copies the converted configs back to your workstation
# - Runs sr3_pull to reset the repo on the nodes back to the original state
#
# Note: after conversion, you need to commit the converted configs manually
# and do sr3_pull
#

# shellcheck source=./bin/sr3_utils
source "${BASH_SOURCE%/*}/sr3_utils"

MAIN_BRANCH="main"

# sets git_tld and pump_name
sr3_get_git_path_pump_name

pump_path="${git_tld}/${pump_name}/"

# Get the paths to the files to be converted
things_to_convert=()
remote_v2_files_to_convert=()
remote_sr3_files=()
local_sr3_files=()
for argu in "$@"; do
if [[ "$argu" == "convert" ]] ; then
continue
fi
if [[ "$argu" == "--dangerWillRobinson" ]] ; then
continue
fi
if [[ "$argu" == *".conf"* ]] ; then
things_to_convert+=("${argu/.conf/}")
remote_v2_files_to_convert+=("~/.config/sarra/${argu}")
remote_sr3_files+=("~/.config/sr3/${argu}")
local_sr3_files+=("${pump_path}${argu}")
else
things_to_convert+=("${argu}")
remote_v2_files_to_convert+=("~/.config/sarra/${argu}.conf")
remote_sr3_files+=("~/.config/sr3/${argu}.conf")
local_sr3_files+=("${pump_path}${argu}.conf")
fi
done

if [ ${#things_to_convert[@]} -eq 0 ]; then
echo '[ERROR] no configs to convert'
exit 2
fi

# What node?
machine_list="${git_tld}/_dsh_config/${pump_name}.list"
first_node=$(head -n1 "${machine_list}")
echo "Running on $first_node"

# Check that the v2 configs exist
for remote_v2_file in "${remote_v2_files_to_convert[@]}"; do
result=""
result=$(sr3_ssh "${first_node}" "[[ -f ${remote_v2_file} ]] && echo EXISTS")
if [[ "${result}" != *"EXISTS" ]]; then
echo "${remote_v2_file} does not exist on the remote node. Aborting conversion!"
echo "No configs have been converted. Exiting."
exit 3
fi
done

# Check that the sr3 configs do not exist
for i in "${!remote_sr3_files[@]}"; do
remote_sr3_file="${remote_sr3_files[i]}"
comp_config="${things_to_convert[i]}"
result=""
result=$(sr3_ssh "${first_node}" "[[ -f ${remote_sr3_file} ]] && echo EXISTS")
if [[ "${result}" == *"EXISTS" ]]; then
echo "${remote_sr3_file} already exists on the remote node. Aborting conversion!"
echo "If you really want to convert ${comp_config} from v2, remove the sr3 config first."
echo "No configs have been converted. Exiting."
exit 3
fi
done

# Run the convert
sr3_ssh "${first_node}" "sr3 convert ${things_to_convert[@]}" | sed 's/Data Pump Name:.*//g' | awk NF
# Run the plugins conversion script, if it exists. "looking for..." lines are filtered out
sr3_ssh "${first_node}" "[[ -f ~/.config/sr3/plugins/tools/convert_configs.sh ]] && cd ~/.config/sr3/ \
&& ~/.config/sr3/plugins/tools/convert_configs.sh ${remote_sr3_files[@]} | sed 's/looking for.*//g' \
|| echo 'WARNING: ~/.config/sr3/plugins/tools/convert_configs.sh failed or ' \
'does not exist. References to plugins in converted configs need to ' \
'be updated manually.'" | sed 's/Data Pump Name:.*//g' | awk NF

# Copy converted files back and delete from node
for i in "${!remote_sr3_files[@]}"; do
remote_sr3_file="${remote_sr3_files[i]}"
local_sr3_file="${local_sr3_files[i]}"
sr3_scp "${first_node}:${remote_sr3_file}" "${local_sr3_file}" | sed 's/Data Pump Name:.*//g' | awk NF
sr3_ssh "${first_node}" "rm ${remote_sr3_file}" | sed 's/Data Pump Name:.*//g' | awk NF
done

# Reset nodes to match the remote Git repo
sr3_pull > /dev/null

echo
echo "Converted ${things_to_convert[@]}"
echo
echo "You should commit the converted files to Git and run sr3_pull to update the nodes."
echo " Run: sr3_commit ${local_sr3_files[@]}"
echo
echo "NOTE: v2 configs need to be disabled and removed."
12 changes: 9 additions & 3 deletions bin/sr3d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Need to figure out where the action is
SR3_ACTIONS="cleanup declare disable enable list remove restart run sanity setup show start status stop"
SR3_ACTIONS="cleanup convert declare disable enable list remove restart run sanity setup show start status stop"
for argu in "$@"; do
if [[ "${SR3_ACTIONS}" == *"${argu}"* ]]; then
action="${argu}"
Expand All @@ -15,13 +15,19 @@ if [ -z "${action}" ]; then
fi

# Special cases for certain actions BEFORE running on nodes
if [[ "$action" == "add" ]] || [[ "$action" == "convert" ]] || [[ "$action" == "foreground" ]] || \
[[ "$action" == "convert" ]] || [[ "$action" == "run" ]] ; then
if [[ "$action" == "add" ]] || [[ "$action" == "foreground" ]] || \
[[ "$action" == "run" ]] ; then
echo "Action ${action} is not supported with sr3d."
echo "https://github.com/MetPX/sr3_tools/issues/2"i
exit 2
fi

if [[ "$action" == "convert" ]] ; then
sr3_action_convert "$@"
# don't run sr3r, conversion is already done
exit "$?"
fi

sr3r "sr3 $*"

# Special cases for certain actions AFTER running on nodes
Expand Down
2 changes: 1 addition & 1 deletion completion/sr3_tools_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SARRA_COMPONENTS="cpost flow poll post watch sarra sender shovel subscribe winnow"
SARRA_COMPONENTS_RE="^(${SARRA_COMPONENTS// /|})\$"
SR3_ACTIONS="cleanup declare disable enable list remove restart run sanity setup show start status stop"
SR3_ACTIONS="cleanup convert declare disable enable list remove restart run sanity setup show start status stop"

###
# component_config
Expand Down

0 comments on commit db3cfe5

Please sign in to comment.