-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebsite_deployment.sh
162 lines (136 loc) · 6.11 KB
/
website_deployment.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
#!/bin/sh
set -e
command_exists() {
command -v "$@" > /dev/null 2>&1
}
install_docker() {
if ! command_exists docker; then
curl -fsSL https://get.docker.com -o install-docker.sh
sh install-docker.sh
rm install-docker.sh
fi
}
userid=$(id -u)
workspace="${HOME}/.seed-site"
hostname=""
tag="latest"
log_level="info"
auto_update=0
profile=""
lightning_url="https://ln.seed.hyper.media"
is_gateway="false"
clean_images_cron="0 0,4,8,12,16,20 * * * docker image prune -a -f # prune all unused images"
testnet_name=""
registration_secret=$(LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c 10)
usage()
{
echo "site deployment script. It deploys a group [options] hostname. Ready to be registered"
echo " hostname :Protocol + domain this sice will be served in. Ex.: https://example.com"
echo "Options"
echo "-t --tag T :Image tag to pull. Latest by default"
echo "-g --gateway :Site behaves as a gateway, serves all public data. False by default."
echo "-a --auto-update :Updates containers whenever a new image is available. Disabled by default"
echo "-l --log-level :Chose between various containers log levels debug | info(default) | warn | error"
echo "-m --monitoring :Sets up monitoring system"
echo "-w --workspace :To change the localtion of the workspace. Default /home/<user>/.seed-site"
echo "-h --help :Shows help and exit"
}
while [ "$1" != "" ]; do
case $1 in
-h | --help ) usage
exit
;;
-a | --auto-update ) auto_update=1
;;
-m | --monitoring ) profile="metrics"
;;
-g | --gateway ) is_gateway="true"
;;
-t | --tag ) shift
tag="$1"
;;
-l | --log-level ) shift
log_level="$1"
;;
-w | --workspace ) shift
workspace="$1"
;;
--testnet ) testnet_name="dev"
lightning_url="https://ln.testnet.seed.hyper.media"
;;
* ) hostname="$1"
esac
shift
done
if [ -z "$hostname" ]; then
echo "Please enter the hostname"
exit 1
fi
hostname="${hostname%/}"
mkdir -p ${workspace}
rm -f ${workspace}/deployment.log
touch ${workspace}/deployment.log
curl -s -o ${workspace}/hmsite.yml https://raw.githubusercontent.com/seed-hypermedia/seed/main/docker-compose.yml
install_docker
if [ -n "$profile" ]; then
mkdir -p ${workspace}/monitoring/grafana/dashboards/libp2p
mkdir -p ${workspace}/monitoring/grafana/dashboards/seed
mkdir -p ${workspace}/monitoring/grafana/dashboards/system
mkdir -p ${workspace}/monitoring/grafana/provisioning/dashboards
mkdir -p ${workspace}/monitoring/grafana/provisioning/datasources
mkdir -p ${workspace}/monitoring/prometheus
fi
docker stop seed-site seed-daemon seed-proxy grafana prometheus 2> ${workspace}/deployment.log 1> ${workspace}/deployment.log || true
docker rm seed-site seed-daemon seed-proxy grafana prometheus 2> ${workspace}/deployment.log 1> ${workspace}/deployment.log || true
dns=$(echo "SEED_SITE_HOSTNAME=${hostname}" | sed -n 's/.*SEED_SITE_HOSTNAME=http[s]*:\/\/\([^/]*\).*/\1/p')
mkdir -p ${workspace}/proxy
cat << BLOCK > ${workspace}/proxy/CaddyFile
{\$SEED_SITE_HOSTNAME}
encode zstd gzip
@ipfsget {
method GET HEAD OPTIONS
path /ipfs/*
}
reverse_proxy /.metrics* grafana:{\$SEED_SITE_MONITORING_PORT:3001}
reverse_proxy @ipfsget seed-daemon:{\$HM_SITE_BACKEND_GRPCWEB_PORT:56001}
reverse_proxy * seed-web:{\$SEED_SITE_LOCAL_PORT:3000}
BLOCK
mkdir -p ${workspace}/web
site_config_file="${workspace}/web/config.json"
if [ "$auto_update" -eq "1" ]; then
docker rm -f autoupdater >/dev/null 2>&1
if ! (crontab -l 2>/dev/null || true) | grep -q "seed site cleanup"; then
# Remove any existing cron job for this task, add the new cron job, and install the new crontab
{ crontab -l 2>/dev/null || true; echo "$clean_images_cron"; } | crontab -
fi
docker run -d --restart unless-stopped --name autoupdater -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --include-restarting -i 300 seed-web seed-daemon >/dev/null 2>&1
fi
did_init_registration_secret="0"
if [ ! -e "$site_config_file" ]; then
did_init_registration_secret="1"
echo "{\"availableRegistrationSecret\": \"$registration_secret\"}" > "$site_config_file"
fi
# this user and group ID align with the ones in /frontend/apps/web/Dockerfile, so web app is allowed to write to the volume
sudo chown -R 1001:1001 "${workspace}/web"
SEED_P2P_TESTNET_NAME="$testnet_name" SEED_LIGHTNING_URL="$lightning_url" SEED_LOG_LEVEL="$log_level" SEED_SITE_DNS="$dns" SEED_SITE_TAG="$tag" SEED_SITE_WORKSPACE="${workspace}" SEED_IS_GATEWAY="$is_gateway" SEED_SITE_HOSTNAME="$hostname" SEED_SITE_MONITORING_WORKDIR="${workspace}/monitoring" SEED_SITE_MONITORING_PORT="$SEED_SITE_MONITORING_PORT" docker compose -f ${workspace}/hmsite.yml --profile "$profile" up -d --pull always --quiet-pull 2> ${workspace}/deployment.log || true
echo "===================="
echo "Deployment done."
echo "===================="
if [ "$did_init_registration_secret" -eq "1" ]; then
echo "Your secret registration URL is:"
echo "${hostname}/hm/register?secret=${registration_secret}"
echo "===================="
fi
# rm -f ${workspace}/hmsite.yml
exit 0
# Set up Test Site:
# sh <(curl -sL https://raw.githubusercontent.com/seed-hypermedia/seed/main/website_deployment.sh) https://my.example.domain --tag main --auto-update --testnet
## To Set up Test Gateway:
# sh <(curl -sL https://raw.githubusercontent.com/seed-hypermedia/seed/main/website_deployment.sh) https://dev.hyper.media --tag main --auto-update --testnet --gateway
### To clean the server for new testing:
## 1. stop and delete all running docker containers
# docker rm -f $(docker ps -a -q)
## 2. delete old images
# docker image prune -a -f
## 3. wipe the workspace:
# rm -rf ~/.seed-site