-
Notifications
You must be signed in to change notification settings - Fork 2
/
sonm-auto-deploy-consumer.sh
145 lines (122 loc) · 4.15 KB
/
sonm-auto-deploy-consumer.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
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
#!/bin/bash
# Exit script as soon as a command fails.
set -o errexit
# Executes cleanup function at script exit.
trap cleanup EXIT
download_url='https://packagecloud.io/install/repositories/SONM/core/script.deb.sh'
github_url='https://raw.githubusercontent.com/sonm-io/autodeploy'
node_config="node-default.yaml"
cli_config="cli.yaml"
branch='master'
download_url='https://packagecloud.io/install/repositories/SONM/core/script.deb.sh'
if [ ${SUDO_USER} ]; then actual_user=${SUDO_USER}; else actual_user=$(whoami); fi
actual_user_home=$(eval echo ~${actual_user})
echo Installing SONM packages
rm -f /etc/apt/sources.list.d/SONM_core-dev.list
rm -f /etc/apt/sources.list.d/SONM_core.list
cleanup() {
rm -f *_template.yaml
rm -f variables.txt
}
install_dependencies() {
apt-get update
apt-get install -y software-properties-common
if ! [ -z "$(lsb_release -a | grep Ubuntu)" ]; then
echo "Ubuntu"
add-apt-repository universe
apt-get update
else
echo "Not Ubuntu"
fi
apt-get install -y gnupg apt-transport-https gawk
declare -a deps=("jq" "curl" "wget")
for dep in "${deps[@]}"
do
if ! [ $(which $dep) ]; then
to_install="$to_install $dep"
fi
done
if [ -n "$to_install" ]; then
apt-get install -y ${to_install}
fi
}
install_sonm() {
gpg_key_url="https://packagecloud.io/SONM/core/gpgkey"
apt_config_url="https://packagecloud.io/install/repositories/SONM/core/config_file.list?os=ubuntu&dist=xenial&source=script"
apt_source_path="/etc/apt/sources.list.d/SONM_core.list"
curl -sSf "${apt_config_url}" > ${apt_source_path}
echo -n "Importing packagecloud gpg key... "
# import the gpg key
curl -L "${gpg_key_url}" 2> /dev/null | apt-key add - &>/dev/null
echo "done."
echo -n "Running apt-get update... "
apt-get update &> /dev/null
echo "done."
apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install -y sonm-cli sonm-node
echo "Sonm packages installed"
}
download_templates() {
echo "Downloading templates..."
wget -q ${github_url}/${branch}/node_template.yaml -O node_template.yaml
wget -q ${github_url}/${branch}/cli_template.yaml -O cli_template.yaml
wget -q ${github_url}/${branch}/variables.txt -O variables.txt
echo "Templates downloaded"
}
load_variables() {
echo "Loading variables..."
source ./variables.txt
export $(cut -d= -f1 variables.txt)
echo "Variables loaded"
}
var_value() {
eval echo \$$1
}
modify_config() {
template="${1}"
vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//')
replaces=""
vars=$(echo $vars | sort | uniq)
for var in ${vars}; do
value=$(var_value ${var} | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g")
value=$(echo "$value" | sed 's/\//\\\//g');
replaces="-e \"s|{{$var}}|${value}|g\" $replaces"
done
escaped_template_path=$(echo ${template} | sed 's/ /\\ /g')
eval sed ${replaces} "${escaped_template_path}" > $2
}
get_password() {
if [ -f "$actual_user_home/.sonm/$cli_config" ]
then
PASSWORD=$(cat $actual_user_home/.sonm/$cli_config | grep pass_phrase | cut -c16- | awk '{gsub("\x22","\x5C\x5C\x5C\x22");gsub("\x27","\x5C\x5C\x5C\x27"); print}')
fi
}
set_up_cli() {
echo setting up cli...
get_password
modify_config "cli_template.yaml" ${cli_config}
mkdir -p ${KEYSTORE}
mkdir -p ${actual_user_home}/.sonm/
mv ${cli_config} ${actual_user_home}/.sonm/${cli_config}
chown -R ${actual_user}:${actual_user} ${KEYSTORE}
chown -R ${actual_user}:${actual_user} ${actual_user_home}/.sonm
su ${actual_user} -c "sonmcli login"
sleep 1
ADMIN_ADDRESS=$(su ${actual_user} -c "sonmcli login | grep 'Default key:' | cut -c14-56" | tr -d '\r')
chmod -R 755 ${KEYSTORE}/*
get_password
}
set_up_node() {
echo setting up node...
modify_config "node_template.yaml" ${node_config}
mv ${node_config} /etc/sonm/${node_config}
}
install_dependencies
install_sonm
download_templates
load_variables
#cli
set_up_cli
#node
set_up_node
echo starting node...
systemctl start sonm-node