forked from shellhub-io/shellhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
167 lines (141 loc) · 5.39 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
#!/bin/sh
docker_install() {
KEEPALIVE_INTERVAL_ARG="-e SHELLHUB_KEEPALIVE_INTERVAL=$KEEPALIVE_INTERVAL"
PREFERRED_HOSTNAME_ARG="-e SHELLHUB_PREFERRED_HOSTNAME=$PREFERRED_HOSTNAME"
PREFERRED_IDENTITY_ARG="-e SHELLHUB_PREFERRED_IDENTITY=$PREFERRED_IDENTITY"
docker run -d \
--name=$CONTAINER_NAME \
--restart=on-failure \
--privileged \
--net=host \
--pid=host \
-v /:/host \
-v /dev:/dev \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/passwd:/etc/passwd \
-v /etc/group:/etc/group \
-v /etc/resolv.conf:/etc/resolv.conf \
-v /var/run:/var/run \
-v /var/log:/var/log \
-e SHELLHUB_SERVER_ADDRESS=$SERVER_ADDRESS \
-e SHELLHUB_PRIVATE_KEY=/host/etc/shellhub.key \
-e SHELLHUB_TENANT_ID=$TENANT_ID \
$KEEPALIVE_INTERVAL_ARG \
$PREFERRED_HOSTNAME_ARG \
$PREFERRED_IDENTITY_ARG \
shellhubio/agent:$AGENT_VERSION
}
bundle_install() {
INSTALL_DIR="${INSTALL_DIR:-/opt/shellhub}"
if [ "$(id -u)" -ne 0 ]; then
printf "NOTE: This install method requires root privileges.\n"
SUDO="sudo"
fi
if ! systemctl show-environment > /dev/null 2>&1 ; then
printf "ERROR: This is not a systemd-based operation system. Unable to proceed with the requested action.\n"
exit 1
fi
echo "Downloading runc static binary..."
{
download "https://github.com/opencontainers/runc/releases/download/${RUNC_VERSION}/runc.${RUNC_ARCH}" $TMP_DIR/runc && chmod 755 $TMP_DIR/runc
} || { rm -rf $TMP_DIR && echo "Failed to download runc binary." && exit 1; }
echo "Downloading OCI runtime spec file..."
{
download https://raw.githubusercontent.com/shellhub-io/shellhub/${AGENT_VERSION}/agent/packaging/config.json $TMP_DIR/config.json
} || { rm -rf $TMP_DIR && echo "Failed to download OCI runtime spec." && exit 1; }
echo "Downloading systemd service file..."
{
download https://raw.githubusercontent.com/shellhub-io/shellhub/${AGENT_VERSION}/agent/packaging/shellhub-agent.service $TMP_DIR/shellhub-agent.service
} || { rm -rf $TMP_DIR && echo "Failed to download systemd service file." && exit 1; }
echo "Downloading rootfs tarball..."
{
download https://github.com/shellhub-io/shellhub/releases/download/$AGENT_VERSION/rootfs-$AGENT_ARCH.tar.gz $TMP_DIR/rootfs.tar.gz
} || { rm -rf $TMP_DIR && echo "Failed to download rootfs." && exit 1; }
echo "Extracting rootfs..."
{
mkdir -p $TMP_DIR/rootfs && tar -C $TMP_DIR/rootfs -xzf $TMP_DIR/rootfs.tar.gz && rm -f $TMP_DIR/rootfs.tar.gz
} || { rm -rf $TMP_DIR && echo "Failed to extract rootfs." && exit 1; }
rm -f $TMP_DIR/rootfs/.dockerenv
sed -i "s,__SERVER_ADDRESS__,$SERVER_ADDRESS,g" $TMP_DIR/config.json
sed -i "s,__TENANT_ID__,$TENANT_ID,g" $TMP_DIR/config.json
sed -i "s,__ROOT_PATH__,$INSTALL_DIR/rootfs,g" $TMP_DIR/config.json
sed -i "s,__INSTALL_DIR__,$INSTALL_DIR,g" $TMP_DIR/shellhub-agent.service
echo "Creating systemd service and starting it..."
$SUDO cp $TMP_DIR/shellhub-agent.service /etc/systemd/system/shellhub-agent.service
$SUDO systemctl enable --now shellhub-agent || { rm -rf $TMP_DIR && echo "Failed to enable systemd service."; exit 1; }
$SUDO rm -rf $INSTALL_DIR
$SUDO mv $TMP_DIR $INSTALL_DIR
$SUDO rm -rf $TMP_DIR
}
download() {
_DOWNLOAD_URL=$1
_DOWNLOAD_OUTPUT=$2
if type curl > /dev/null 2>&1; then
curl -fsSL $_DOWNLOAD_URL --output $_DOWNLOAD_OUTPUT
elif type wget > /dev/null 2>&1; then
wget -q -O $_DOWNLOAD_OUTPUT $_DOWNLOAD_URL
fi
}
http_get() {
_HTTP_GET_URL=$1
if type curl > /dev/null 2>&1; then
curl -sk $_HTTP_GET_URL
elif type wget > /dev/null 2>&1; then
wget -q -O - $_HTTP_GET_URL
fi
}
[ -z "$TENANT_ID" ] && { echo "ERROR: TENANT_ID is missing."; exit 1; }
SERVER_ADDRESS="${SERVER_ADDRESS:-https://cloud.shellhub.io}"
TENANT_ID="${TENANT_ID}"
INSTALL_METHOD="$INSTALL_METHOD"
AGENT_VERSION="${AGENT_VERSION:-$(http_get $SERVER_ADDRESS/info | sed -E 's/.*"version":\s?"?([^,"]*)"?.*/\1/')}"
AGENT_ARCH="$AGENT_ARCH"
CONTAINER_NAME="${CONTAINER_NAME:-shellhub}"
RUNC_VERSION=${RUNC_VERSION:-v1.1.3}
RUNC_ARCH=$RUNC_ARCH
INSTALL_DIR="${INSTALL_DIR:-/opt/shellhub}"
TMP_DIR="${TMP_DIR:-`mktemp -d -t shellhub-installer-XXXXXX`}"
if type docker > /dev/null 2>&1; then
while :; do
if $SUDO docker info > /dev/null 2>&1; then
INSTALL_METHOD="${INSTALL_METHOD:-docker}"
break
elif [ "$(id -u)" -ne 0 ]; then
[ -z "$SUDO" ] && SUDO="sudo" || { SUDO="" && break; }
fi
done
fi
INSTALL_METHOD="${INSTALL_METHOD:-bundle}"
# Auto detect arch if it has not already been set
if [ -z "$AGENT_ARCH" ]; then
case `uname -m` in
x86_64)
AGENT_ARCH=amd64
RUNC_ARCH=amd64
;;
armv6l)
AGENT_ARCH=arm32v6
RUNC_ARCH=armel
;;
armv7l)
AGENT_ARCH=arm32v7
RUNC_ARCH=armhf
;;
aarch64)
AGENT_ARCH=arm64v8
RUNC_ARCH=arm64
esac
fi
echo "Install method: $INSTALL_METHOD"
echo "Agent version: $AGENT_VERSION"
case "$INSTALL_METHOD" in
bundle)
bundle_install
;;
docker)
docker_install
;;
*)
echo "Install method not supported."
exit 1
esac