-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.azure.sh
265 lines (210 loc) · 6.89 KB
/
boot.azure.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
# Check if a command exists
check_command_exists() {
command -v "$1" >/dev/null 2>&1
}
send_compute_unit_events() {
# Define the URL
url="http://diphyx.com/api/compute/units/${DXO_COMPUTE_UNIT_POINTER}/events/?secret-key=${DXO_COMPUTE_UNIT_SECRET_KEY_RW}"
# Send request
if check_command_exists curl; then
curl -X PUT -H "Content-Type: application/json" -d "{\"type\": \"$1\", \"message\": \"$2\"}" "${url}" --connect-timeout 3 || true
elif check_command_exists wget; then
wget -q --method=PUT --header="Content-Type: application/json" --body-data "{\"type\": \"$1\", \"message\": \"$2\"}" "${url}" --timeout=3 || true
fi
}
# Install packages using the appropriate package manager
install_packages() {
local package_manager
if check_command_exists apt; then
package_manager="apt"
elif check_command_exists dnf; then
package_manager="dnf"
elif check_command_exists yum; then
package_manager="yum"
elif check_command_exists pacman; then
package_manager="pacman"
elif check_command_exists zypper; then
package_manager="zypper"
else
send_compute_unit_events "ERROR" "unhandled package manager"
exit 1
fi
# Check if the package is installed
if dpkg -l | grep -q "$1"; then
return 0
fi
# Update and install packages
"$package_manager" update
"$package_manager" install -y "$@"
}
# Add authorized key
add_authorized_key() {
# Define the home directory
home_dir="/home/$1"
# Check if the home directory exists
if [ ! -d "$home_dir" ]; then
return 1
fi
# Define the .ssh directory
ssh_dir="$home_dir/.ssh"
# Create the .ssh directory if it doesn't exist
if [ ! -d "$ssh_dir" ]; then
mkdir -p "$ssh_dir"
chown "$1:$1" "$ssh_dir"
chmod 700 "$ssh_dir"
fi
# Define the authorized_keys file
authorized_keys_file="$ssh_dir/authorized_keys"
# Create the authorized_keys file if it doesn't exist
if [ ! -f "$authorized_keys_file" ]; then
touch "$authorized_keys_file"
chown "$1:$1" "$authorized_keys_file"
chmod 600 "$authorized_keys_file"
fi
# Add the key if it doesn't exist
if ! grep -q "$2" "$authorized_keys_file"; then
echo "$2" >> "$authorized_keys_file"
chown "$1:$1" "$authorized_keys_file"
fi
}
# Download nessessary file
download_necessary_file() {
local file_path
local file_url
if [ "$1" = "docker" ]; then
file_path="/dx/docker.sh"
file_url="https://get.docker.com"
elif [ "$1" = "startup" ]; then
file_path="/dx/startup.sh"
file_url="https://raw.githubusercontent.com/diphyx/dxflow/main/startup.sh"
elif [ "$1" = "compose" ]; then
file_path="/dx/docker-compose.yaml"
file_url="https://raw.githubusercontent.com/diphyx/dxflow/main/docker-compose.yaml"
else
return 1
fi
# Download the file
if check_command_exists curl; then
curl -fsSL -o "${file_path}" "${file_url}"
elif check_command_exists wget; then
wget -q -O "${file_path}" "${file_url}"
fi
}
# Download startup file
download_startup_file() {
local file_path
if [ "$1" = "variables" ]; then
file_path="/dx/variables.env"
elif [ "$1" = "packages" ]; then
file_path="/dx/packages.txt"
elif [ "$1" = "script" ]; then
file_path="/dx/script.sh"
elif [ "$1" = "flow" ]; then
file_path="/dx/flow.yaml"
else
return 1
fi
# Define the URL
file_url="http://diphyx.com/api/compute/units/${DXO_COMPUTE_UNIT_POINTER}/startup/$1/?secret-key=${DXO_COMPUTE_UNIT_SECRET_KEY_RO}"
# Download the file
if check_command_exists curl; then
curl -fsSL -o "${file_path}" "${file_url}"
elif check_command_exists wget; then
wget -q -O "${file_path}" "${file_url}"
fi
}
send_compute_unit_events "INFO" "booting"
# Set environment variables
{
echo "DXO_COMPUTE_UNIT_POINTER=\"$DXO_COMPUTE_UNIT_POINTER\""
echo "DXO_COMPUTE_UNIT_SECRET_KEY_RW=\"$DXO_COMPUTE_UNIT_SECRET_KEY_RW\""
echo "DXO_COMPUTE_UNIT_SECRET_KEY_RO=\"$DXO_COMPUTE_UNIT_SECRET_KEY_RO\""
echo "DXO_COMPUTE_UNIT_EXTENSIONS=\"$DXO_COMPUTE_UNIT_EXTENSIONS\""
} >> /etc/environment
# Initialize authorized keys
authorized_keys=$(echo "$DXO_AUTHORIZED_KEYS" | tr "," "\n")
for authorized_key in "$authorized_keys"; do
# List all users
users=$(awk -F: '/\/home/ {print $1}' /etc/passwd)
# Add the authorized key to users
for user in $users; do
add_authorized_key "$user" "$authorized_key"
done
done
# Create dx directory
mkdir /dx
send_compute_unit_events "INFO" "downloading necessary files"
# Download necessary files
download_necessary_file "docker"
download_necessary_file "startup"
download_necessary_file "compose"
send_compute_unit_events "INFO" "downloading startup files"
# Download startup files
download_startup_file "variables"
download_startup_file "packages"
download_startup_file "script"
download_startup_file "flow"
# Set startup variables
if [ -e /dx/variables.env ]; then
while IFS= read -r line; do
export "$line"
echo "$line" >> /etc/environment
done < /dx/variables.env
fi
# Check if docker.sh exists
if [ ! -e /dx/docker.sh ]; then
send_compute_unit_events "ERROR" "cannot find docker.sh"
exit 1
fi
# Set execute permissions
chmod +x /dx/docker.sh
send_compute_unit_events "INFO" "installing docker"
# Install Docker
if ! check_command_exists docker; then
/bin/bash /dx/docker.sh
fi
send_compute_unit_events "INFO" "installing dependencies"
# Install dependencies
install_packages xfsprogs sysstat
# Install startup packages
if [ -e /dx/packages.txt ]; then
send_compute_unit_events "INFO" "installing startup packages"
while IFS=, read -r package; do
install_packages "$package"
done < /dx/packages.txt
fi
send_compute_unit_events "INFO" "pulling dxflow core"
# Pull dxflow core
docker pull dxflow/redis
docker pull dxflow/alpine
docker pull dxflow/syslog
docker pull dxflow/api
send_compute_unit_events "INFO" "pulling dxflow extensions"
# Pull dxflow extensions
dxflow_extensions=$(echo "$DXO_COMPUTE_UNIT_EXTENSIONS" | tr "," "\n")
for dxflow_extension in "$dxflow_extensions"; do
docker pull "dxflow/ext-$dxflow_extension"
done
send_compute_unit_events "INFO" "preparing"
# Create volume
mkdir /volume
# Format and mount disk
mkfs -t xfs /dev/sdc
mount /dev/sdc /volume
echo "/dev/sdc /volume xfs defaults 0 2" >> /etc/fstab
# Check if startup.sh exists
if [ ! -e /dx/startup.sh ]; then
send_compute_unit_events "ERROR" "cannot find startup.sh"
exit 1
fi
# Set execute permissions
chmod +x /dx/startup.sh
# Initialize crontab
echo "@reboot /dx/startup.sh" | crontab -
# Initialize named pipe
mkdir -p /volume/.dx
mkfifo /volume/.dx/.pipe
send_compute_unit_events "INFO" "starting"
# Run startup script
/bin/bash /dx/startup.sh &