-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
131 lines (108 loc) · 3.81 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
#!/usr/bin/env bash
#? Description:
#? Deploy shadowsocks-manager with Docker containers.
#? Any existing network and containers with the same name will be removed.
#? Assume the Docker is installed.
#?
#? Usage:
#? install.sh [OPTIONS]
#?
#? Options:
#? [OPTIONS]
#?
#? Options are the same as the docker-entrypoint.sh script.
#? If any option is provided, the options will pass along with the default options to the `docker run` command.
#? The default options are:
#?
#? -e SSM_SECRET_KEY=$(guid)
#? -e SSM_DEBUG=False
#? -e SSM_MEMCACHED_HOST=ssm-{SSM_VERSION}-memcached
#? -e SSM_RABBITMQ_HOST=ssm-{SSM_VERSION}-rabbitmq
#?
#? Environment:
#? The following environment variables are used by this script:
#?
#? - SSM_VERSION
#?
#? Optional.
#? Set the version of the shadowsocks-manager Docker image.
#? The default value is `latest`.
#?
#? Example:
#? # quick start with default options
#? $ bash install.sh
#?
#? # install a specific version of shadowsocks-manager
#? $ SSM_VERSION=0.1.5 bash install.sh
#?
# exit on any error
set -e -o pipefail
function usage () {
awk '/^#\?/ {sub("^[ ]*#\\?[ ]?", ""); print}' "$0" \
| awk '{gsub(/^[^ ]+.*/, "\033[1m&\033[0m"); print}'
}
function check-os () {
if [[ $(uname) != 'Linux' && $(uname) != 'Darwin' ]]; then
printf "fatal: this script support Linux and MacOS only.\n" >&2
return 255
fi
}
function check-docker () {
if ! command -v docker &>/dev/null; then
printf "fatal: docker is not installed.\n" >&2
return 255
fi
}
function guid () {
# generate a random guid
od -vN 25 -An -tx1 /dev/urandom | tr -d ' \n'
}
function main () {
if [[ $1 == '-h' || $1 == '--help' ]]; then
usage
return
fi
check-os
check-docker
declare SSM_VERSION=${SSM_VERSION:-latest}
declare ssm_image="alexzhangs/shadowsocks-manager:$SSM_VERSION"
declare ssm_container_name="ssm-$SSM_VERSION"
declare ssm_network_name="${ssm_container_name}-network"
declare memcached_container_name="${ssm_container_name}-memcached"
declare rabbitmq_container_name="${ssm_container_name}-rabbitmq"
declare ssm_volume_path=~/"${ssm_container_name}-volume"
declare -a default_options=(
-e "SSM_SECRET_KEY=$(guid)"
-e "SSM_DEBUG=False"
-e "SSM_MEMCACHED_HOST=$memcached_container_name"
-e "SSM_RABBITMQ_HOST=$rabbitmq_container_name"
)
# create volume path on host
mkdir -p "$ssm_volume_path"
echo "Removing the existing container and network if any ..."
if docker ps -a --format '{{.Names}}' | grep -q "^${memcached_container_name}$"; then
docker rm -f "$memcached_container_name"
fi
if docker ps -a --format '{{.Names}}' | grep -q "^${rabbitmq_container_name}$"; then
docker rm -f "$rabbitmq_container_name"
fi
if docker ps -a --format '{{.Names}}' | grep -q "^${ssm_container_name}$"; then
docker rm -f "$ssm_container_name"
fi
if docker network inspect "$ssm_network_name" &>/dev/null; then
docker network rm "$ssm_network_name"
fi
echo "Creating $ssm_network_name ..."
docker network create "$ssm_network_name"
echo "Running $memcached_container_name ..."
docker run --restart=always -d --network "$ssm_network_name" --name "$memcached_container_name" memcached
echo "Running $rabbitmq_container_name ..."
# run rabbitmq, used by celery
docker run --restart=always -d --network "$ssm_network_name" --name "$rabbitmq_container_name" rabbitmq
# run shadowsocks-manager
echo "Running $ssm_container_name ..."
docker run --restart=always -d -p 80:80 -p 443:443 --network "$ssm_network_name" -v "$ssm_volume_path:/var/local/ssm" \
--name "$ssm_container_name" "$ssm_image" "${default_options[@]}" "$@"
}
main "$@"
exit