-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare_machine.sh
executable file
·117 lines (106 loc) · 2.65 KB
/
prepare_machine.sh
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
#! /usr/bin/env bash
test -z "${ANSIBLE_DIR}" && ANSIBLE_DIR="ansible"
PLAYBOOK="${ANSIBLE_DIR}/dev_machine.yaml"
BECOME="root"
USER=`whoami`
TMP_DIR="/tmp/ansible-${USER}/tmp"
SSH_CONTROL_PATH_DIR="/tmp/ansible-${USER}/cp"
VENV_DIR="venv"
report()
{
echo "$@" >&2
}
print_help()
{
report "Usage: ./prepare_machine.sh [-p<playbook>] [-b<user>] [-v] [-h]"
report "Options:"
report " -p<playbook> - specify path to ansible playbook (default: ${PLAYBOOK})"
report " -b<user> - specify user to become (default: ${BECOME})"
report " -r<dir> - specify tmp dir to be used by ansible (default: ${TMP_DIR})"
report " -c<dir> - specify dir to be used for ssh control path on the local machine (default: ${SSH_CONTROL_PATH_DIR})"
report " -s - skip python virtual environment (pipenv) creation"
report " -e<dir> - change python virtualenv dirname (default: ${VENV_DIR})"
report " -x<vars> - extra ansible vars to be used (key=value format, e.g. nfb_card=/dev/nfb1)"
report " -v - increase verbosity of ansible"
report " -h - print help"
}
playbook="${PLAYBOOK}"
become="${BECOME}"
tmp_dir=${TMP_DIR}
ssh_control_path_dir=${SSH_CONTROL_PATH_DIR}
venv_dir=${VENV_DIR}
ansible_extra_vars=""
verbosity=""
while getopts 'hp:b:r:c:se:x:v' c; do
case $c in
h)
print_help
exit 0
;;
p)
playbook="$OPTARG"
;;
b)
become="$OPTARG"
;;
r)
tmp_dir="$OPTARG"
;;
c)
ssh_control_path_dir="$OPTARG"
;;
s)
skip_venv=1
;;
e)
venv_dir="$OPTARG"
;;
x)
ansible_extra_vars="$OPTARG"
;;
v)
test -z "${verbosity}" && verbosity="-v" || verbosity="${verbosity}v"
;;
*)
report "Error: Unknown option!"
exit 1
;;
esac
done
if ! command -v ansible-playbook &> /dev/null
then
report "Error: Ansible must be installed!"
exit 1
fi
ask_become_pass=""
if ! sudo -vn &> /dev/null
then
ask_become_pass="--ask-become-pass"
fi
# installation of python3.9 and dev libraries like libpcap
ANSIBLE_SSH_CONTROL_PATH_DIR="${ssh_control_path_dir}" \
ansible-playbook \
${verbosity} \
-b --become-user "${become}" ${ask_become_pass} \
-e ansible_remote_tmp="${tmp_dir}" \
-i localhost, \
--connection=local \
--extra-vars "$ansible_extra_vars" \
"${playbook}"
ansible_rc=$?
if [ $ansible_rc -ne 0 ]
then
report "Error: An error occurred during the execution of the ansible!"
exit $ansible_rc
fi
# setup virtualenv and install packages
if [ -z "$skip_venv" ]
then
rm -rf ${venv_dir}
python3 -m venv ${verbosity} --prompt flowtest ${venv_dir}
. ${venv_dir}/bin/activate
python3 -m pip install pipenv
pipenv install ${verbosity} --dev
echo "To activate new python virtualenv run:"
echo ". ${venv_dir}/bin/activate"
fi