-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_agents.sh
More file actions
executable file
·119 lines (103 loc) · 2.69 KB
/
migrate_agents.sh
File metadata and controls
executable file
·119 lines (103 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
set -e
BASE_DIR=$(dirname $(readlink -e $0))
. $BASE_DIR/common.sh
function cleanup {
rm -rf $SCRIPT_PATH $TMP_DIR $DELETE_AUTH_CONFIG_PATH
}
trap cleanup EXIT
function usage_exit {
error "Usage: $SCRIPT_NAME [-d deployment_id] [-n attempts_limit] (install|uninstall) (3.1|3.2|3.2.1|auto) cli_venv cli_dir [auth_config]" 1
}
#$1 - manager virtualenv path - it depends on version
#$2 - operation - either migration_install or migration_uninstall
#$3 - path to custom authentication configuration
#$4 - result script path
#$5 - number of failed tasks
#$6 - optional - deployment id
function prepare_agents_script {
TMP_DIR=$(mktemp -d)
cp $BASE_DIR/common_agents/* $TMP_DIR
cd $TMP_DIR
mv run.sh.template run.sh
sed -i s@__MANAGER_ENV__@$1@ run.sh
sed -i s@__OPERATION__@$2@ run.sh
sed -i s@__MAX_ATTEMPTS__@$5@ run.sh
cp $3 auth_config.yaml
if [ -n "$6" ]; then
echo -n $6 > deployment_id
fi
tar -cf $4 *
cd -
}
DEPLOYMENT_ID=""
MAX_ATTEMPTS=-1
while getopts d:n: opt; do
case $opt in
d)
DEPLOYMENT_ID=$OPTARG
;;
n)
MAX_ATTEMPTS=$OPTARG
;;
\?)
usage_exit
;;
esac
done
shift $((OPTIND - 1))
if [[ $# -lt 4 ]]; then
usage_exit
fi
case $1 in
install)
OPERATION=install
;;
uninstall)
OPERATION=uninstall
;;
*)
usage_exit
;;
esac
VENV_PATH=$(absolute_path $3)
CLOUDIFY_PATH=$(absolute_path $4)
if [[ $# -gt 4 ]]; then
AUTH_CONFIG_PATH=$(absolute_path $5)
else
# creating empty dummy config file for simplicity:
AUTH_CONFIG_PATH=$(mktemp)
DELETE_AUTH_CONFIG_PATH=$AUTH_CONFIG_PATH
fi
activate_cli $CLOUDIFY_PATH $VENV_PATH
VERSION=$2
DETECTED_VERSION=$(get_manager_version)
if [[ "$VERSION" == "auto" ]]; then
VERSION=$DETECTED_VERSION
fi
if [ "$VERSION" != "$DETECTED_VERSION" ]; then
DECL_MSG="Declared manager version: ${VERSION}."
REAL_MSG="Real manager version: ${DETECTED_VERSION}."
error "Wrong manager version supplied. $DECL_MSG $REAL_MSG" 1
fi
case $VERSION in
3.1)
RUNNER=run_on_manager.sh
MANAGER_VENV=/opt/manager
;;
3.2 | 3.2.1)
RUNNER=run_on_docker.sh
MANAGER_VENV=/opt/manager/env
;;
*)
echo "Unsupported version: $VERSION"
usage_exit
;;
esac
echo "Preparing operation script"
SCRIPT_PATH=$(mktemp)
prepare_agents_script $MANAGER_VENV $OPERATION $AUTH_CONFIG_PATH $SCRIPT_PATH $MAX_ATTEMPTS $DEPLOYMENT_ID
echo "Operation script prepared, running operation $OPERATION"
supplement_credentials $2
run_operation $SCRIPT_PATH $RUNNER
echo "Operation $OPERATION completed"