forked from nullpo-head/Out-of-the-Box-CodeServer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·185 lines (142 loc) · 6.09 KB
/
install.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
#!/bin/bash
set -e
prompt_yn () {
info -n "$1: "
read response
if [[ -z "$response" ]]; then
response="$2"
fi
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
true
elif [[ "$response" =~ ^([nN][oO]|[nN])+$ ]]; then
false
else
$(prompt_yn "$1" "$2")
fi
}
echo_stage () {
echo -e "\e[32m$*\e[m"
}
error () {
echo -e "\e[91m[Error] $*\e[m" >&2
}
info () {
local flags=-e
if [[ "$1" == -n ]]; then
flags=-ne
shift
fi
echo $flags "\e[2m=> \e[m$*" >&2
}
check_install() {
if ! which $1; then
error "$1 is not found. Please install $1"
exit 1
fi
info $1 is installed.
}
if [[ $(whoami) == root ]]; then
error "Please run this script by a regular user, not by sudo or root" >&2
exit 1
fi
cd "$(dirname "$(realpath "$0")")"
echo_stage "== Installing utilities and CLIs: git, awscli, curl, jq, unzip, software-properties-common (apt-add-repository) and sudo =="
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y git awscli curl jq unzip software-properties-common sudo apt-transport-https
echo_stage "== Installing docker.io =="
sudo DEBIAN_FRONTEND=noninteractive apt install -y docker.io
sudo apt-mark hold docker.io
sudo systemctl enable docker
echo_stage "== Installing docker-compose =="
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
echo_stage "== Checking if lxd is installed =="
check_install lxd
echo_stage "== Checking .env =="
if [[ ! -e helper_containers/.env ]]; then
error "Please fill in .env file following README.md"
exit 1
else
info ".env file is found"
fi
echo_stage "== Checking existing LXD containers for Code-Server =="
info "Querying existing LXD containers"
if [ -n "$(lxc ls ootb-code-server --format=csv)" ]; then
if prompt_yn "A LXC container named 'ootb-code-server' already exists. Do you want to delete it? [y/N]" n; then
info "Stopping ootb-code-server. This might cause an expected errorfail."
lxc stop ootb-code-server || true
info "Deleting ootb-code-server"
lxc delete ootb-code-server
info stopped and deleted the container.
else
error "Aborting"
exit 0
fi
fi
echo_stage "== Filling some variables in .env =="
sed -i "s;^HEARTBEATS_FOLDER=.*;HEARTBEATS_FOLDER=$(realpath ./heartbeats_files_placeholder);" ./helper_containers/.env
touch ./helper_containers/emails
sed -i "s;^ALLOWED_EMAILS_LIST=.*;ALLOWED_EMAILS_LIST=$(realpath ./helper_containers/emails);" ./helper_containers/.env
sed -i "s;^OAUTH2_PROXY_COOKIE_SECRET=.*;OAUTH2_PROXY_COOKIE_SECRET=$(head -c 32 /dev/urandom | sha512sum | cut -c1-32);" ./helper_containers/.env
info done.
echo_stage "== Initializing a LXC container for Code-Server =="
USERNAME="$(whoami)"
UID_="$(id -u)"
GID="$(id -g)"
if prompt_yn "Do you run 'lxd init'? Run it if this is your first time to use LXD. [Y/n]" y; then
info "Launch 'lxd init'... The default options are suitable for most cases."
lxd init
fi
info "Creating an Ubuntu:20.04 LXC container"
lxc init ubuntu:20.04 ootb-code-server -p default -c security.nesting=true
sed "s/%%user%%/$USERNAME/g" ./codeserver/cloud-init.yml | lxc config set ootb-code-server user.user-data -
lxc start ootb-code-server
while ! ( lxc exec ootb-code-server -- tail -n50 /var/log/cloud-init-output.log | grep "Cloud-init .* finished .* Up .* seconds" ) ; do
sleep 2
info "waiting for the container getting ready..."
done
sleep 2
info "Mounting heartbeats_files_placeholder directory"
info "To allow for $USERNAME to mount a directory to a LXD container, adding a subuid mapping..."
set -x
sudo usermod --add-subuids ${UID_}-${UID_} --add-subgids ${GID}-${GID} root
set +x
echo -e "uid $(id -u "$USERNAME") 1000\ngid $(id -g "$USERNAME") 1000" | lxc config set ootb-code-server raw.idmap -
lxc exec ootb-code-server -- sudo -u "${USERNAME}" sh -c "mkdir -p /home/$USERNAME/.local/share"
lxc config device add ootb-code-server heartbeats disk source=$(realpath heartbeats_files_placeholder) path="/home/$USERNAME/.local/share/code-server"
info "Enabling code-server in the container..."
lxc exec ootb-code-server -- sudo -u "${USERNAME}" sh -c "DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus' systemctl --user enable code-server"
lxc stop ootb-code-server
lxc file push -p ./codeserver/config.yaml ootb-code-server/home/$USERNAME/.config/code-server/
lxc start ootb-code-server
info "Querying the IP address of the container..."
sleep 3
while ! ( lxc ls ootb-code-server -c4 --format=csv | grep -oP '^[0-9.]+' > /dev/null ); do
sleep 1
done
info IP: $( lxc ls ootb-code-server -c4 --format=csv | grep -oP '^[0-9.]+' )
sed -i "s;^LXC_IP=.*;LXC_IP=$(lxc ls ootb-code-server -c4 --format=csv | grep -oP '^[0-9.]+');" ./helper_containers/.env
echo_stage "== Making Docker containers up =="
cd helper_containers
if prompt_yn "Would you like to build the heartbeat watcher to automatially deallocate your Azure VM? [y/N]: " n; then
sudo docker-compose up -d
else
sudo docker-compose up -d https-portal oauth2-proxy
fi
echo_stage "== Fix heartbeats permission error =="
sudo chown -R $USERNAME:$USERNAME ./heartbeats_files_placeholder
echo_stage "== Download devops_tools_install_v3.sh =="
lxc exec ootb-code-server -- sudo -u "${USERNAME}" sh -c "wget -O /home/$USERNAME/devops_tools_install_v3.sh https://raw.githubusercontent.com/bmpi-dev/how-tos/main/src/devops_tools_install_v3.sh"
echo_stage "== Finish =="
info "Done!"
info "* PLEASE cd to helper_containers and run 'docker-compose logs' to check whether containers are working fine. *"
info "If they have no errors, you should be able to access to https://$(grep -o 'CODER_HOST=.*' .env | cut -c12-)"
info
info "If you want to enter the container of code-server from your shell, run 'lxc exec ootb-coder-server -- /bin/bash -i'"
echo_stage "== Follow-up =="
if prompt_yn "Would you like to see docker-compose logs now? [Y/n]" y; then
info "Hit ctrl+c to exit from the log"
sleep 1
sudo docker-compose logs --tail=30 -f
fi