-
Notifications
You must be signed in to change notification settings - Fork 11
/
website_deployment.sh
129 lines (107 loc) · 4.82 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
#!/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}/.mtt-site"
hostname=""
tag="latest"
auto_update=0
profile=""
allow_push="false"
clean_images_cron="0 3 * * * docker rmi \$(docker images | grep -E 'mintter/mintter-site|mintter/sitegw' | awk '{print \$3}') # mintter site cleanup"
usage()
{
echo "group_deployment script. It links a group [options] hostname"
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, storing all data. False by default."
echo "-a --auto-update :Updates containers whenever a new image is available. Disabled by default"
echo "-m --monitoring :Sets up monitoring system"
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 ) allow_push="true"
;;
-t | --tag ) shift
tag="$1"
;;
* ) 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}/mttsite.yml https://raw.githubusercontent.com/MintterHypermedia/mintter/main/docker-compose.yml
install_docker
if [ -n "$profile" ]; then
mkdir -p ${workspace}/monitoring/grafana/dashboards/libp2p
mkdir -p ${workspace}/monitoring/grafana/dashboards/mintter
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 nextjs minttersite proxy grafana prometheus 2> ${workspace}/deployment.log 1> ${workspace}/deployment.log || true
docker rm nextjs minttersite proxy grafana prometheus 2> ${workspace}/deployment.log 1> ${workspace}/deployment.log || true
dns=$(echo "MTT_SITE_HOSTNAME=${hostname}" | sed -n 's/.*MTT_SITE_HOSTNAME=http[s]*:\/\/\([^/]*\).*/\1/p')
mkdir -p ${workspace}/proxy
cat << BLOCK > ${workspace}/proxy/CaddyFile
{\$MTT_SITE_HOSTNAME}
@ipfsget {
method GET HEAD OPTIONS
path /ipfs/*
}
@wellknown {
method GET HEAD OPTIONS
path /.well-known/hypermedia-site
}
@version {
method GET HEAD OPTIONS
path /.well-known/hypermedia-site/version
}
reverse_proxy @wellknown minttersite:{\$MTT_SITE_BACKEND_GRPCWEB_PORT:56001}
reverse_proxy /.metrics* grafana:{\$MTT_SITE_MONITORING_PORT:3001}
route @version {
rewrite /.well-known/hypermedia-site/version /debug/version
reverse_proxy minttersite:{\$MTT_SITE_BACKEND_GRPCWEB_PORT:56001}
}
reverse_proxy @ipfsget minttersite:{\$MTT_SITE_BACKEND_GRPCWEB_PORT:56001}
reverse_proxy * nextjs:{\$MTT_SITE_LOCAL_PORT:3000}
BLOCK
if [ $auto_update -eq 1 ]; then
docker rm -f autoupdater >/dev/null 2>&1
if ! (crontab -l 2>/dev/null || true) | grep -q "mintter 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 nextjs minttersite >/dev/null 2>&1
fi
MTT_SITE_DNS="$dns" MTT_SITE_TAG="$tag" MTT_SITE_ALLOW_PUSH="$allow_push" MTT_SITE_HOSTNAME="$hostname" MTT_SITE_PROXY_CONTAINER_NAME="proxy" MTT_SITE_NEXTJS_CONTAINER_NAME="nextjs" MTT_SITE_DAEMON_CONTAINER_NAME="minttersite" MTT_SITE_MONITORING_WORKDIR="${workspace}/monitoring" MTT_SITE_MONITORING_PORT="$MTT_SITE_MONITORING_PORT" docker compose -f ${workspace}/mttsite.yml --profile "$profile" up -d --pull always --quiet-pull 2> ${workspace}/deployment.log || true
# MTT_SITE_DNS="$dns" MTT_SITE_HOSTNAME="$hostname" MTT_SITE_PROXY_CONTAINER_NAME="proxy" MTT_SITE_NEXTJS_CONTAINER_NAME="nextjs" MTT_SITE_DAEMON_CONTAINER_NAME="minttersite" docker compose -f ${workspace}/mttsite.yml up -d --pull always --quiet-pull 2> ${workspace}/deployment.log || true
timeout 15 docker logs -f minttersite 2> /dev/null | sed '/Site Invitation secret token: / q' | awk -F ': ' '{print $2}'
rm -f ${workspace}/mttsite.yml
exit 0