forked from jjrom/resto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
executable file
·179 lines (164 loc) · 5.48 KB
/
deploy
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
#!/bin/bash
#
# Copyright 2018 Jérôme Gasperi
#
# Licensed under the Apache License, version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
####### DO NOT TOUCH THIS ######
ENV_FILE=./config.env
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
CLEAN=0
FORCE_DATABASE_INSTALLATION=0
################################
#
# Force script to exit on error
#
set -e
err_report() {
echo -e "${RED}[ERROR] Error on line $1 - see errors.log file ${NC}"
}
trap 'err_report $LINENO' ERR
#
# Help function
#
function showUsage {
echo -e ""
echo -e "Deploy a resto docker instance "
echo -e ""
echo -e " Usage $0 [options]"
echo -e ""
echo -e " Options:"
echo -e ""
echo -e " -e | --envfile Environnement file (default is ${GREEN}config.env${NC})"
echo -e " -F | --force Force database model installation"
echo -e " -C | --clean Clean all containers and volumes before deploying. ${YELLOW}USE WITH CAUTION !${NC}"
echo -e " -h | --help show this help"
echo -e ""
echo -e " !!! This script requires docker !!!"
echo -e ""
}
while (( "$#" ));
do
case "$1" in
-e|--envfile)
if [[ "$2" == "" || ${2:0:1} == "-" ]]; then
showUsage
echo -e "${RED}[ERROR] Missing config file name${NC}"
echo -e ""
exit 1
fi
ENV_FILE="$2"
shift 2 # past argument
;;
-C|--clean)
CLEAN=1
shift # past argument
;;
-F|--force)
FORCE_DATABASE_INSTALLATION=1
shift # past argument
;;
-h|--help)
showUsage
shift # past argument
exit 0
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
showUsage
echo -e "${RED}[ERROR] Unsupported flag $1${NC}"
echo -e ""
exit 1
;;
esac
done
#
# The environement file is mandatory
# It contains all configuration to build and run resto images
#
if [[ ! -f ${ENV_FILE} ]]; then
showUsage
echo -e "${RED}[ERROR]${NC} The \"${ENV_FILE}\" file does not exist!${NC}"
echo ""
exit 1
fi
#
# Check mandatory tools
#
if ! command -v docker &> /dev/null
then
echo -e "${RED}[ERROR]${NC} The required \"docker\" command was not found. See https://docs.docker.com/get-docker/"
echo ""
exit 1
fi
PUBLIC_ENDPOINT=$(grep ^PUBLIC_ENDPOINT= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
RESTO_EXPOSED_PORT=$(grep ^RESTO_EXPOSED_PORT= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
DATABASE_PORT=$(grep ^DATABASE_PORT= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
DATABASE_HOST=$(grep ^DATABASE_HOST= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
DATABASE_NAME=$(grep ^DATABASE_NAME= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
DATABASE_USER_NAME=$(grep ^DATABASE_USER_NAME= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
DATABASE_USER_PASSWORD=$(grep ^DATABASE_USER_PASSWORD= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
COMPOSE_FILE=$(grep ^COMPOSE_FILE= ${ENV_FILE} | awk -F= '{for (i=2; i<=NF; i++) print $i}'| xargs echo -n)
# Clean errors.log file
rm -f errors.log
echo -e "[INFO] Public endpoint is ${GREEN}${PUBLIC_ENDPOINT}${NC}"
#
# Containers are part of the external network "rnet" (resto network)
#
RNET_EXIST=$(docker network ls | grep rnet | wc | awk '{print $1}')
if [[ "${RNET_EXIST}" == "0" ]]; then
echo -e "[INFO] Creating external network ${GREEN}rnet${NC}"
docker network create rnet
else
echo -e "[INFO] Using existing network ${GREEN}rnet${NC}"
fi
# Set default compose file
if [[ "${COMPOSE_FILE}" == "" ]]; then
COMPOSE_FILE=docker-compose.yml
echo -e "[WARNING] COMPOSE_FILE is not set in ${ENV_FILE} - using default ${GREEN}${COMPOSE_FILE}${NC}"
fi
echo -e "[INFO] Using compose files: ${GREEN}${COMPOSE_FILE}${NC}"
# Clean - ask !
if [[ ${CLEAN} -eq 1 ]]; then
read -p "You're about to remove existing resto container and volumes. Are you sure? [yN] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo -e "[INFO] Cancel deployment"
exit 1
else
echo -e "[INFO] Cleaning existing resto container and volumes"
COMPOSE_FILE=${COMPOSE_FILE} \
RESTO_EXPOSED_PORT=${RESTO_EXPOSED_PORT} \
DATABASE_PORT=${DATABASE_PORT} \
ENV_FILE=${ENV_FILE} \
docker compose down -v
fi
fi
COMPOSE_FILE=${COMPOSE_FILE} \
RESTO_EXPOSED_PORT=${RESTO_EXPOSED_PORT} \
DATABASE_PORT=${DATABASE_PORT} \
DATABASE_HOST=${DATABASE_HOST} \
DATABASE_NAME=${DATABASE_NAME} \
DATABASE_USER_NAME=${DATABASE_USER_NAME} \
DATABASE_USER_PASSWORD=${DATABASE_USER_PASSWORD} \
ENV_FILE=${ENV_FILE} \
docker compose up -d
echo -e ""
echo -e "[INFO] The resto service is up and running locally at ${GREEN}${PUBLIC_ENDPOINT}${NC}"
echo ""