Skip to content

Commit 6b7abcb

Browse files
committed
Integrate progress bars and uninstaller
1 parent c923d58 commit 6b7abcb

File tree

8 files changed

+442
-261
lines changed

8 files changed

+442
-261
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pyyaml = "*"
1313
ansible = "*"
1414
pyhcl = "*"
1515
gnupg = "*"
16+
tqdm = "*"
1617

1718
[requires]
1819
python_version = "3"

Pipfile.lock

Lines changed: 114 additions & 141 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ansible.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import sys
3+
from tqdm import tqdm
34

45
import utils as utl
56
from client_config import CLI_CONFIG
@@ -18,14 +19,14 @@ def write_ansible_hosts_file(pub_ip, inst_user, ssh_priv_key_path, node_name):
1819
param4: node name
1920
'''
2021
logger.info('Writing ansible\'s hosts file...')
21-
print('>>> writing ansible\'s hosts file...')
22+
tqdm.write('>>> writing ansible\'s hosts file...')
2223
try:
2324
hosts_path = f'{CLI_CONFIG["ans_hosts_path"]}-{node_name}'
2425
cont = (f'[nodes]\n{pub_ip} ansible_ssh_user={inst_user}'
2526
f' ansible_ssh_private_key_file={ssh_priv_key_path}')
2627
utl.write_file(cont, hosts_path)
2728
logger.info('Ansible hosts file created')
28-
print('>>> ansible hosts file created')
29+
tqdm.write('>>> ansible hosts file created')
2930
except Exception as exc:
3031
logger.error(f'Error writing hosts file for ansible\n{exc}')
3132
print(utl.print_err_str('>>> error writing hosts file for ansible'))
@@ -47,7 +48,7 @@ def run_playbook(ans_playbooks_path,
4748
'''
4849
if extra_vars is None:
4950
logger.info('Running playbooks')
50-
print('>>> running ansible playbooks')
51+
tqdm.write('>>> running ansible playbooks')
5152
try:
5253
utl.run_cmd('ANSIBLE_HOST_KEY_CHECKING=False '
5354
f'ansible-playbook '
@@ -59,7 +60,7 @@ def run_playbook(ans_playbooks_path,
5960
sys.exit(1)
6061
else:
6162
logger.info('Running playbooks')
62-
print('>>> running ansible playbooks')
63+
tqdm.write('>>> running ansible playbooks')
6364
try:
6465
utl.run_cmd('ANSIBLE_HOST_KEY_CHECKING=False '
6566
f'ansible-playbook '
@@ -80,7 +81,7 @@ def sys_config(node_name):
8081
param1: node name
8182
'''
8283
logger.info('Deploying sys_config ansible playbook')
83-
print('>>> deploying sys_config ansible playbook')
84+
tqdm.write('>>> deploying sys_config ansible playbook')
8485
hosts_path = f'{CLI_CONFIG["ans_hosts_path"]}-{node_name}'
8586
run_playbook(CLI_CONFIG['ans_playbooks_path'],
8687
'sys_config.yml',
@@ -96,7 +97,7 @@ def deploy_pnode_package_playbook(node_name, pnode_rel_url):
9697
param2: release-server url (prod or dev - based on `--dev` arg)
9798
'''
9899
logger.info('Deploying pnode_package ansible playbook')
99-
print('>>> deploying pnode_package ansible playbook')
100+
tqdm.write('>>> deploying pnode_package ansible playbook')
100101
hosts_path = f'{CLI_CONFIG["ans_hosts_path"]}-{node_name}'
101102
run_playbook(CLI_CONFIG['ans_playbooks_path'],
102103
'pnode_package.yml',

install.sh

Lines changed: 187 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,208 @@ user=$(whoami)
55
BASE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
66

77
function check_inst_tools() {
8+
pip3 &> /dev/null
9+
if [ ! $? == 0 ]; then
10+
echo '>>> please install pip3 (python3-pip)'
11+
return 1
12+
fi
813
python3 -c 'import distutils' &> /dev/null
914
if [ $? == 1 ]; then
1015
echo '>>> please install python distutils module'
16+
echo '>>> ie: pip3 install distutils'
17+
return 1
18+
fi
19+
python3 -c 'import distutils.util' &> /dev/null
20+
if [ $? == 1 ]; then
21+
echo '>>> please install python distutils.util module'
22+
echo '>>> ie: pip3 install distutils.util'
23+
return 1
1124
fi
1225
curl &> /dev/null
1326
if [ ! $? == 2 ]; then
1427
echo '>>> please install curl'
28+
return 1
1529
fi
1630
unzip &> /dev/null
1731
if [ ! $? == 0 ]; then
1832
echo '>>> please install unzip'
33+
return 1
1934
fi
2035
sudo &> /dev/null
2136
if [ ! $? == 1 ]; then
2237
echo '>>> please install sudo'
38+
return 1
39+
fi
40+
}
41+
42+
function spinner(){
43+
spinner="/|\\-/|\\-"
44+
while :
45+
do
46+
for i in `seq 0 7`
47+
do
48+
echo -n "${spinner:$i:1}"
49+
echo -en "\010"
50+
sleep 0.2
51+
done
52+
done
53+
}
54+
55+
function setup_folders(){
56+
mkdir -p ~/.ssh/pcli
57+
sudo mkdir -p /etc/pcli/ansible/playbooks
58+
sudo mkdir -p /etc/pcli/terraform
59+
sudo mkdir -p /var/log/pcli
60+
sudo chown -R "$user:$user" /etc/pcli
61+
sudo chown -R "$user:$user" /var/log/pcli
62+
FOLDERS_ARRAY=( ~/.ssh/pcli/ /etc/pcli/ansible/playbooks/ /etc/pcli/terraform/ /var/log/pcli/ )
63+
for folder in "${FOLDERS_ARRAY[@]}"; do
64+
if [ -d "$folder" ]; then
65+
echo ">>> $folder created"
66+
else
67+
echo ">>> $folder missing"
68+
return 1
69+
fi
70+
done
71+
}
72+
73+
function inst_pipenv_ansible(){
74+
printf '>>> installing ansible and pipenv '
75+
spinner &
76+
SPINNER_PID=$!
77+
trap "kill -9 $SPINNER_PID" `seq 0 15`
78+
python3 -m pip install --user --no-warn-script-location ansible pipenv passlib >> /var/log/pcli/pcli.log
79+
kill -9 $SPINNER_PID
80+
wait $SPINNER_PID 2> /dev/null
81+
PYTHON_BIN_PATH="$(python3 -m site --user-base)/bin"
82+
PATH="$PATH:$PYTHON_BIN_PATH"
83+
pipenv &> /dev/null
84+
if [ $? == 127 ]; then
85+
echo '>>> something wrong during pipenv installation - please check /var/pcli/pcli.log'
86+
return 1
87+
fi
88+
echo; echo '>>> pipenv installed'
89+
ansible &> /dev/null
90+
if [ $? == 127 ]; then
91+
echo '>>> something wrong during ansible installation - please check /var/pcli/pcli.log'
92+
return 1
93+
fi
94+
echo '>>> ansible installed'
95+
}
96+
97+
function inst_terraform(){
98+
if [ ! -f '/usr/bin/terraform' ]; then
99+
printf '>>> downloading and installing terraform '
100+
spinner &
101+
SPINNER_PID=$!
102+
trap "kill -9 $SPINNER_PID" `seq 0 15`
103+
install -d -m 775 -o "$user" /tmp/pcli
104+
mkdir -p /tmp/pcli && cd "$_"
105+
curl -s https://releases.hashicorp.com/terraform/0.13.3/terraform_0.13.3_linux_amd64.zip --output terraform_0.13.3_linux_amd64.zip >> /var/log/pcli/pcli.log
106+
unzip terraform_0.13.3_linux_amd64.zip -d /tmp/pcli/ >> /var/log/pcli/pcli.log
107+
chmod +x terraform
108+
sudo mv terraform /usr/bin/terraform
109+
cd - > /dev/null
110+
rm -rf /tmp/pcli
111+
kill -9 $SPINNER_PID
112+
wait $SPINNER_PID 2> /dev/null
113+
fi
114+
if [ ! -f '/usr/bin/terraform' ]; then
115+
echo '>>>'; echo '>>> something wrong during terraform installation - please check /var/pcli/pcli.log'
116+
return 1
117+
fi
118+
echo; echo '>>> terraform installed'
119+
}
120+
121+
function config_setup(){
122+
cp "$BASE_PATH/config/ansible/ansible.cfg" /etc/pcli/ansible/
123+
cp "$BASE_PATH/config/ansible/playbooks/edit_user_pwd.yml" /etc/pcli/ansible/playbooks/
124+
cp "$BASE_PATH/config/ansible/playbooks/pnode_package.yml" /etc/pcli/ansible/playbooks/
125+
cp "$BASE_PATH/config/ansible/playbooks/sys_config.yml" /etc/pcli/ansible/playbooks/
126+
cp "$BASE_PATH/config/terraform/inst_config.json" /etc/pcli/terraform/
127+
cp "$BASE_PATH/config/terraform/variables.tf.json.orig" /etc/pcli/terraform/
128+
cp "$BASE_PATH/config/terraform/main.tf.orig" /etc/pcli/terraform/
129+
cp "$BASE_PATH/config/terraform/output.tf.orig" /etc/pcli/terraform/
130+
FILES_ARRAY=("/etc/pcli/ansible/ansible.cfg" "/etc/pcli/ansible/playbooks/edit_user_pwd.yml"
131+
"/etc/pcli/ansible/playbooks/pnode_package.yml" "/etc/pcli/ansible/playbooks/sys_config.yml"
132+
"/etc/pcli/terraform/inst_config.json" "/etc/pcli/terraform/variables.tf.json.orig"
133+
"/etc/pcli/terraform/main.tf.orig" "/etc/pcli/terraform/output.tf.orig")
134+
for file in "${FILES_ARRAY[@]}"; do
135+
if [ -f "$file" ]; then
136+
echo ">>> $file moved in path"
137+
else
138+
echo ">>> $file not moved in path"
139+
return 1
140+
fi
141+
done
142+
echo '>>> all config files moved in path'
143+
}
144+
145+
function build_pipenv_env(){
146+
printf '>>> building pipenv environment '
147+
spinner &
148+
SPINNER_PID=$!
149+
trap "kill -9 $SPINNER_PID" `seq 0 15`
150+
cd "$BASE_PATH"
151+
pipenv install >> /var/log/pcli/pcli.log 2>&1
152+
kill -9 $SPINNER_PID
153+
wait $SPINNER_PID 2> /dev/null
154+
echo; echo '>>> pipenv environment built'
155+
}
156+
157+
function setup_bashrc(){
158+
if ! grep -q "if [ -r ~/.bashrc ]; then" ~/.bash_profile > /dev/null 2>&1 /dev/null ; then
159+
if [ ! -f ~/.bash_profile ]; then
160+
echo '>>> ~/.bash_profile not found'
161+
touch ~/.bash_profile
162+
fi
163+
echo "if [ -r ~/.bashrc ]; then" >> ~/.bash_profile
164+
echo " source ~/.bashrc" >> ~/.bash_profile
165+
echo "fi" >> ~/.bash_profile
166+
echo '>>> ~/.bash_profile created and edited'
167+
fi
168+
if ! grep -q "pcli" ~/.bashrc > /dev/null 2>&1 /dev/null; then
169+
if [ ! -f ~/.bashrc ]; then
170+
touch ~/.bashrc
171+
fi
172+
echo "alias pcli='$(cd $BASE_PATH; pipenv --venv)/bin/python $BASE_PATH/cli.py'" >> ~/.bashrc
173+
source ~/.bashrc
174+
echo '>>> bashrc edited'
175+
echo '>>> pcli moved in path'
176+
fi
177+
}
178+
179+
function start_checks(){
180+
if check_inst_tools; then
181+
main
182+
else return 1
23183
fi
24184
}
25185

26-
check_inst_tools
27-
28-
mkdir -p ~/.ssh/pcli
29-
sudo mkdir -p /etc/pcli/ansible/playbooks
30-
sudo mkdir -p /etc/pcli/terraform
31-
sudo mkdir -p /var/log/pcli
32-
sudo chown -R "$user:$user" /etc/pcli
33-
sudo chown -R "$user:$user" /var/log/pcli
34-
echo '>>> folder /etc/pcli/ created'
35-
echo '>>> folder /etc/pcli/ansible created'
36-
echo '>>> folder /etc/pcli/ansible/playbooks created'
37-
echo '>>> folder /etc/pcli/terraform created'
38-
echo '>>> folder /var/log/pcli/ created'
39-
echo '>>> log file /var/log/pcli/pcli.log created'
40-
41-
pip3 > /dev/null 2>&1 /dev/null
42-
if [ ! $? == 0 ]; then
43-
echo '>>> installing pip3'
44-
curl -sS https://bootstrap.pypa.io/get-pip.py | python3 >> /var/log/pcli/pcli.log 2>&1
45-
export PATH="$HOME/.local/bin:$PATH"
46-
echo '>>> pip3 installed'
47-
else
48-
echo '>>> pip3 already installed'
49-
fi
50-
51-
echo '>>> installing ansible and pipenv'
52-
pip3 install --user --no-warn-script-location ansible pipenv passlib >> /var/log/pcli/pcli.log
53-
echo '>>> ansible and pipenv installed'
54-
55-
echo '>>> downloading and installing terraform'
56-
install -d -m 775 -o "$user" /tmp/pcli
57-
mkdir -p /tmp/pcli && cd "$_"
58-
curl -s https://releases.hashicorp.com/terraform/0.13.3/terraform_0.13.3_linux_amd64.zip --output terraform_0.13.3_linux_amd64.zip >> /var/log/pcli/pcli.log
59-
unzip terraform_0.13.3_linux_amd64.zip -d /tmp/pcli/ >> /var/log/pcli/pcli.log
60-
chmod +x terraform
61-
sudo mv terraform /usr/bin/terraform
62-
cd - > /dev/null
63-
rm -rf /tmp/pcli
64-
echo '>>> terraform installed'
65-
66-
cp "$BASE_PATH/config/ansible/ansible.cfg" /etc/pcli/ansible/
67-
cp "$BASE_PATH/config/ansible/playbooks/edit_user_pwd.yml" /etc/pcli/ansible/playbooks/
68-
cp "$BASE_PATH/config/ansible/playbooks/pnode_package.yml" /etc/pcli/ansible/playbooks/
69-
cp "$BASE_PATH/config/ansible/playbooks/sys_config.yml" /etc/pcli/ansible/playbooks/
70-
cp "$BASE_PATH/config/terraform/inst_config.json" /etc/pcli/terraform/
71-
cp "$BASE_PATH/config/terraform/variables.tf.json.orig" /etc/pcli/terraform/
72-
cp "$BASE_PATH/config/terraform/main.tf.orig" /etc/pcli/terraform/
73-
cp "$BASE_PATH/config/terraform/output.tf.orig" /etc/pcli/terraform/
74-
echo '>>> all config files moved in path'
75-
76-
echo '>>> building pipenv environment'
77-
cd "$BASE_PATH"
78-
pipenv install >> /var/log/pcli/pcli.log 2>&1
79-
echo '>>> pipenv environment built'
80-
cd > /dev/null
81-
if ! grep -q "if [ -r ~/.bashrc ]; then" ~/.bash_profile > /dev/null 2>&1 /dev/null ; then
82-
if [ ! -f ~/.bash_profile ]; then
83-
echo '>>> ~/.bash_profile not found'
84-
touch ~/.bash_profile
85-
fi
86-
echo "if [ -r ~/.bashrc ]; then" >> ~/.bash_profile
87-
echo " source ~/.bashrc" >> ~/.bash_profile
88-
echo "fi" >> ~/.bash_profile
89-
echo '>>> ~/.bash_profile created and edited'
90-
fi
91-
if ! grep -q "pcli" ~/.bashrc > /dev/null 2>&1 /dev/null; then
92-
if [ ! -f ~/.bashrc ]; then
93-
touch ~/.bashrc
94-
fi
95-
echo "alias pcli='$(cd $BASE_PATH; pipenv --venv)/bin/python $BASE_PATH/cli.py'" >> ~/.bashrc
96-
source ~/.bashrc
97-
echo '>>> bashrc edited'
98-
echo '>>> pcli moved in path'
99-
fi
100-
echo; echo;
101-
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
102-
echo '>>> installation finished!'
103-
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
104-
echo; echo;
186+
function main(){
187+
if ! setup_folders; then
188+
return 1
189+
fi
190+
if ! inst_pipenv_ansible; then
191+
return 1
192+
fi
193+
if ! inst_terraform; then
194+
return 1
195+
fi
196+
if ! config_setup; then
197+
return 1
198+
fi
199+
build_pipenv_env
200+
201+
cd > /dev/null
202+
203+
setup_bashrc
204+
205+
echo; echo;
206+
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
207+
echo '>>> installation finished!'
208+
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
209+
echo; echo;
210+
}
211+
212+
start_checks

node.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
import sys
44
import requests
5+
from tqdm import tqdm
56

67
import terraform as trf
78
import utils as utl
@@ -117,7 +118,7 @@ def pnode_setup_and_start_cmds(node_name, new_rnd_pwd):
117118
utl.run_remote_cmd(f'pnode_logs_viewer start >> {CLI_CONFIG["pcli_log_path"]} 2>&1',
118119
node_name,
119120
output=True)
120-
print('>>> starting all pnode_nitro components')
121+
tqdm.write('>>> starting all pnode_nitro components')
121122
except Exception as exc:
122123
logger.error(f'Error running pnode_logs_viewer_start:\n{exc}')
123124
print('>>> error running pnode_logs_viewer_start')
@@ -153,7 +154,7 @@ def pnode_setup_and_start_cmds(node_name, new_rnd_pwd):
153154
time.sleep(5)
154155
nitro_ret_code = 0
155156
logger.error('nitro api ping not ready - retrying in 5s')
156-
print('>>> nitro enclave status: ready')
157+
tqdm.write('>>> nitro enclave status: ready')
157158

158159
def node_mng(args):
159160
'''

0 commit comments

Comments
 (0)