-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfpctl
More file actions
executable file
·246 lines (212 loc) · 8.48 KB
/
fpctl
File metadata and controls
executable file
·246 lines (212 loc) · 8.48 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env bash
FPCTL_CONFIG_DIR="$HOME/.config/fpctl"
if [[ -z ${FPCTL_ROOT_DIR} ]]; then
FPCTL_ROOT_DIR="$HOME/.local"
fi
FPCTL_DATA_DIR="$FPCTL_ROOT_DIR/share/fpctl"
C_WARN="\033[1;33m==> WARNING: \033[0m"
C_YELL="\033[1;33m==> \033[0m"
C_QUEST=$C_YELL
C_ERR="\033[1;31m==> ERROR: \033[0m"
C_RED="\033[1;31m==> \033[0m"
C_MSG="\033[1;32m==> \033[0m"
C_ITEM="\033[1;34m -> \033[0m"
bail()
{
echo -e "${C_ERR}$1"
echo -e "${C_RED}fpctl will now quit."
}
message()
{
echo -e "${C_MSG}$1"
}
check_dependencies()
{
message "Checking dependencies..."
which pip3 &>/dev/null
HAS_PYTHON3="$?"
which git &>/dev/null
HAS_GIT="$?"
which ansible &>/dev/null
HAS_ANSIBLE="$?"
which ssh &>/dev/null
HAS_OPENSSH="$?"
python3 -c "import colorama" &>/dev/null
HAS_COLORAMA="$?"
python3 -c "import terminaltables" &>/dev/null
HAS_TERMINALTABLES="$?"
if [[ "$HAS_PYTHON3" != "0" ||
"$HAS_GIT" != "0" ||
"$HAS_ANSIBLE" != "0" ||
"$HAS_OPENSSH" != "0" ||
"$HAS_COLORAMA" != "0" ||
"$HAS_TERMINALTABLES" != "0" ]]; then
message "Installing dependencies..."
sudo yum -y -q install python34 python34-pip ansible git openssh-clients
if [[ "$?" != 0 ]]; then
bail $'could not install dependencies. To install the required packages manually, try running the following command \033[1mas root\033[0m:\nyum install python34 ansible git openssh-clients'
exit 5
fi
sudo su -c "yes|pip3 install colorama terminaltables"
if [[ "$?" != 0 ]]; then
bail $'could not install dependencies. To install the required packages manually, try running the following command \033[1mas root\033[0m:\npip3 install colorama terminaltables'
exit 5
fi
fi
}
configure_ansible()
{
/usr/bin/cp -f /etc/ansible/ansible.cfg $FPCTL_CONFIG_DIR
sed -i "/^# control_path_dir =/s/.*/control_path_dir = \/tmp\/.ansible\/cp/" $FPCTL_CONFIG_DIR/ansible.cfg
sed -i "/^#host_key_checking =/s/.*/host_key_checking = False/" $FPCTL_CONFIG_DIR/ansible.cfg
# NOTE: the following line enables SSH pipelining to improve performance. This is ok
# on CC7 since its sudoers configuration does not contain "requiretty", but
# might fail miserably on other target distros.
sed -i "/^#pipelining =/s/.*/pipelining = True/" $FPCTL_CONFIG_DIR/ansible.cfg
# NOTE: another potential performance gain. With this line we ask Ansible to keep
# idle SSH connections active for 3 minutes before closing them.
sed -i "/^connect_timeout =/s/.*/connect_timeout = 180/" $FPCTL_CONFIG_DIR/ansible.cfg
sed -i "\|^#fact_caching =|s|.*|fact_caching = jsonfile\nfact_caching_connection = $FPCTL_DATA_DIR/ansible_cache|" $FPCTL_CONFIG_DIR/ansible.cfg
}
setup()
{
# (1) SUPPORTED OS
message "Checking for supported operating system..."
uname -r | grep -q el7
IS_KERNEL_EL7="$?"
lsb_release -d | grep -q "CentOS Linux release 7"
IS_LSB_CENTOS7="$?"
if [[ ! ($IS_KERNEL_EL7 && $IS_LSB_CENTOS7) ]]; then
echo -e "${C_WARN}unsupported operating system detected."
echo -e "${C_YELL}The reference platform for fpctl is CERN CentOS 7. fpctl will continue, but if issues arise you're on your own."
fi
# (2) DEPENDENCIES
check_dependencies
# (3) DIRECTORIES
message "Setting up directories..."
E_BAD_PATH=3
if [[ ! -d $FPCTL_CONFIG_DIR ]]; then
message "Creating configuration directory at $FPCTL_CONFIG_DIR..."
mkdir -p $FPCTL_CONFIG_DIR
if [[ "$?" != 0 ]]; then
bail "cannot create fpctl configuration directory: $FPCTL_CONFIG_DIR."
exit $E_BAD_PATH
fi
fi
if [[ ! -w $FPCTL_CONFIG_DIR ]]; then
bail "configuration directory $FPCTL_CONFIG_DIR is not writable."
exit $E_BAD_PATH
fi
if [[ ! -d $FPCTL_DATA_DIR ]]; then
message "Creating data directory at $FPCTL_DATA_DIR..."
mkdir -p $FPCTL_DATA_DIR
if [[ "$?" != 0 ]]; then
bail "cannot create fpctl data directory: $FPCTL_DATA_DIR."
exit $E_BAD_PATH
fi
fi
if [[ ! -w $FPCTL_DATA_DIR ]]; then
bail "data directory $FPCTL_DATA_DIR is not writable."
exit $E_BAD_PATH
fi
configure_ansible
# (4) GIT REPOSITORIES
message "Fetching fpctl data and system configuration..."
FPCTL_CONTROL_REPOSITORY_SSH="git@github.com:AliceO2Group/Control.git"
FPCTL_CONTROL_REPOSITORY_HTTPS="https://github.com/AliceO2Group/Control.git"
FPCTL_CONTROL_REPOSITORY_NAME="GitHub"
FPCTL_ANSIBLE_REPOSITORY_SSH="ssh://git@gitlab.cern.ch:7999/AliceO2Group/system-configuration.git"
FPCTL_ANSIBLE_REPOSITORY_HTTPS="https://gitlab.cern.ch/AliceO2Group/system-configuration.git"
FPCTL_ANSIBLE_REPOSITORY_NAME="CERN GitLab"
git_clone()
{
SSH_URL="$1"
HTTPS_URL="$2"
KEYS_URL="$3"
DEST_PATH="$4"
REPO_PROVIDER="$5"
if [[ -d "$DEST_PATH" ]]; then
while true; do
echo -ne "${C_QUEST}${DEST_PATH} already exists. Overwrite [y/n]?\n${C_QUEST}------------------------------------\n${C_QUEST}"
read yn
case $yn in
[Yy]* ) rm -rf "$DEST_PATH" &>/dev/null; break;;
[Nn]* ) message "Skipping repository setup..."; return 0;;
* ) echo -e "${C_QUEST}Please answer yes or no [y/n].";;
esac
done
fi
git clone "$SSH_URL" "$DEST_PATH"
if [[ "$?" != 0 ]]; then
echo -e "${C_WARN}cannot fetch system-configuration repository via SSH. Your SSH key must be listed on $KEYS_URL for this to work.\n${C_YELL}Falling back to HTTPS, you will be prompted for your $REPO_PROVIDER credentials."
git clone "$HTTPS_URL" "$DEST_PATH"
if [[ "$?" != 0 ]]; then
exit 4
fi
fi
}
git_clone $FPCTL_ANSIBLE_REPOSITORY_SSH $FPCTL_ANSIBLE_REPOSITORY_HTTPS "https://gitlab.cern.ch/profile/keys" "$FPCTL_DATA_DIR/system-configuration" "${FPCTL_ANSIBLE_REPOSITORY_NAME}"
if [[ "$?" != 0 ]]; then
bail "could not fetch git repository system-configuration. Please make sure that your CERN credentials are correct, and that you are a member of the alice-o2-detector-teams e-group (https://e-groups.cern.ch/e-groups/EgroupsSearch.do?searchValue=alice-o2-detector-teams)."
exit $?
fi
git_clone $FPCTL_CONTROL_REPOSITORY_SSH $FPCTL_CONTROL_REPOSITORY_HTTPS "https://github.com/settings/keys" "$FPCTL_DATA_DIR/Control" "${FPCTL_CONTROL_REPOSITORY_NAME}"
if [[ "$?" != 0 ]]; then
bail "could not fetch git repository Control. Please make sure that your GitHub credentials are correct."
exit $?
fi
}
update()
{
message "Updating fpctl..."
cd $FPCTL_DATA_DIR/Control && git pull --rebase
if [[ "$?" != 0 ]]; then
message "Cannot update git repository Control. Try running fpctl setup instead."
return 5
fi
message "Updating system configuration..."
cd $FPCTL_DATA_DIR/system-configuration && git pull --rebase
if [[ "$?" != 0 ]]; then
message "Cannot update git repository system-configuration. Try running fpctl setup instead."
return 5
fi
check_dependencies
configure_ansible
message "Finishing up..."
MYPATH=`readlink -f $0`
\cp -f $FPCTL_DATA_DIR/Control/fpctl/src/fpctl $MYPATH || true
}
if [[ "$1" == "setup" ]]; then
setup
STATUS="$?"
if [[ "$STATUS" == "0" ]]; then
echo -e "$FPCTL_ROOT_DIR" > "$FPCTL_CONFIG_DIR/.installed"
message "All done."
fi
exit $STATUS
elif [[ "$1" == "update" || "$1" == "up" ]]; then
FPCTL_ROOT_DIR=$(head -n 1 "$FPCTL_CONFIG_DIR/.installed")
if [[ -z ${FPCTL_ROOT_DIR} ]]; then
FPCTL_ROOT_DIR="$HOME/.local"
fi
FPCTL_DATA_DIR="$FPCTL_ROOT_DIR/share/fpctl"
update
STATUS="$?"
if [[ "$STATUS" == "0" ]]; then
echo -e "$FPCTL_ROOT_DIR" > "$FPCTL_CONFIG_DIR/.installed"
message "All done."
fi
exit $STATUS
elif [[ ! -e "$FPCTL_CONFIG_DIR/.installed" ]]; then
message "fpctl configuration not found. Running first-run setup."
setup
STATUS="$?"
if [[ "$STATUS" == "0" ]]; then
echo -e "$FPCTL_ROOT_DIR" > "$FPCTL_CONFIG_DIR/.installed"
message "Setup done."
exec "$FPCTL_DATA_DIR/Control/fpctl/src/fpctl.py" "$@"
fi
exit $STATUS
else
exec "$FPCTL_DATA_DIR/Control/fpctl/src/fpctl.py" "$@"
fi