-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·207 lines (162 loc) · 6.43 KB
/
deploy.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
# SPDX-FileCopyrightText: 2022 Wilfred Nicoll <xyzroller@rollyourown.xyz>
# SPDX-License-Identifier: GPL-3.0-or-later
# deploy.sh
# This script deploys the modules required for the project and deploys the project components
# Required modules (space-separated list in the form "module_1 module_2 module_3")
MODULES="ryo-ingress-proxy ryo-postgres ryo-coturn ryo-wellknown"
# Help and error messages
#########################
helpMessage()
{
echo "deploy.sh: Deploy a rollyourown project"
echo "Usage: ./deploy.sh -n hostname -v version"
echo "Flags:"
echo -e "-n hostname \t\t(Mandatory) Name of the host on which to deploy the project"
echo -e "-v version \t\t(Mandatory) Version stamp for images to deploy, e.g. 20210101-1"
echo -r "-r \t\t\tRestore after system failure (should only be used during a restore after system failure)"
echo -e "-s \t\t\tSkip module deployment (should only be used during a restore after system failure)"
echo -e "-h \t\t\tPrint this help message"
echo ""
exit 1
}
errorMessage()
{
echo "Invalid option or input variables are missing"
echo "Use \"./deploy.sh -h\" for help"
exit 1
}
# Command-line input handling
#############################
restore="false"
skip_modules="false"
while getopts n:v:rsh flag
do
case "${flag}" in
n) hostname=${OPTARG};;
v) version=${OPTARG};;
r) restore="true";;
s) skip_modules="true";;
h) helpMessage ;;
?) errorMessage ;;
esac
done
if [ -z "$hostname" ] || [ -z "$version" ]; then
errorMessage
fi
# Script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Get Project ID from configuration file
PROJECT_ID="$(yq eval '.project_id' "$SCRIPT_DIR"/configuration/configuration_"$hostname".yml)"
# Get remote_build variable from configuration file
REMOTE="$(yq eval '.remote_build' "$SCRIPT_DIR"/configuration/configuration_"$hostname".yml)"
# Get Project IdP mode from configuration file
PROJECT_IDP_MODE="$(yq eval '.project_idp_mode' "$SCRIPT_DIR"/configuration/configuration_"$hostname".yml)"
modeErrorMessage()
{
echo "Invalid IdP mode \""$PROJECT_IDP_MODE"\". Please check configuration."
exit 1
}
# Check IdP mode in configuration is supported
if [ ! "$PROJECT_IDP_MODE" == "standalone" ] && [ ! "$PROJECT_IDP_MODE" == "gitea" ]; then
modeErrorMessage
fi
# Info
echo "rollyourown deployment script for "$PROJECT_ID""
# Update project repository
###########################
if [ $restore == false ]; then
echo "Refreshing project repository with git pull to ensure the current version"
cd "$SCRIPT_DIR" && git pull
fi
# Deploy Modules
################
# If -s flag is not included, get user input for whether to deploy all modules (default yes)
if [ $skip_modules == false ]; then
echo ""
echo "Deploy all modules?"
echo -n "If this is the first project to be deployed on the host "$hostname", then answer 'y' (the default) "
read -e -p "[y/n]:" DEPLOY_MODULES
DEPLOY_MODULES="${DEPLOY_MODULES:-"y"}"
DEPLOY_MODULES="${DEPLOY_MODULES,,}"
if [ ! "$DEPLOY_MODULES" == "y" ] && [ ! "$DEPLOY_MODULES" == "n" ]; then
echo "Invalid option "${DEPLOY_MODULES}". Quitting"
exit 1
elif [ "$DEPLOY_MODULES" == "y" ]; then
echo "Deploying all modules."
for module in $MODULES
do
# Clone or update module
echo ""
if [ -d ""$SCRIPT_DIR"/../"$module"" ]
then
echo "Module "$module" already cloned to this control node, refreshing with git pull"
cd "$SCRIPT_DIR"/../"$module" && git pull
else
echo "Cloning "$module" repository. Executing 'git clone' for "$module" repository"
git clone https://github.com/rollyourown-xyz/"$module" "$SCRIPT_DIR"/../"$module"
fi
# Deploy module
echo ""
echo "Deploying "$module" module on "$hostname" with version "$version""
/bin/bash "$SCRIPT_DIR"/../"$module"/deploy.sh -n "$hostname" -v "$version" -b "$REMOTE"
done
else
echo "Checking for each module."
for module in $MODULES
do
# Get user input for whether to do module deployment (default yes)
echo ""
echo "Checking whether to deploy "$module" module."
echo "Check the documentation for already-deployed projects at https://rollyourown.xyz/rollyourown/projects/"
echo "If this module has already been deployed for another project on the host "$hostname", then answer 'n'."
echo "Default is 'y'."
echo -n "Deploy "$module" module? "
read -e -p "[y/n]:" DEPLOY_MODULE
DEPLOY_MODULE="${DEPLOY_MODULE:-"y"}"
DEPLOY_MODULE="${DEPLOY_MODULE,,}"
# Check input
while [ ! "$DEPLOY_MODULE" == "y" ] && [ ! "$DEPLOY_MODULE" == "n" ]
do
echo "Invalid option "${DEPLOY_MODULE}". Please try again."
echo -n "Deploy "$module" module (default is 'y')? "
read -e -p "[y/n]:" DEPLOY_MODULE
DEPLOY_MODULE="${DEPLOY_MODULE:-"y"}"
DEPLOY_MODULE="${DEPLOY_MODULE,,}"
done
if [ "$DEPLOY_MODULE" == "y" ]; then
# Clone or update module repository
echo ""
if [ -d ""$SCRIPT_DIR"/../"$module"" ]
then
echo "Module "$module" already cloned to this control node, refreshing with git pull"
cd "$SCRIPT_DIR"/../"$module" && git pull
else
echo "Cloning "$module" repository. Executing 'git clone' for "$module" repository"
git clone https://github.com/rollyourown-xyz/"$module" "$SCRIPT_DIR"/../"$module"
fi
# Deploy module
echo ""
echo "Deploying "$module" module on "$hostname" with version "$version""
/bin/bash "$SCRIPT_DIR"/../"$module"/deploy.sh -n "$hostname" -v "$version" -b "$REMOTE"
else
echo ""
echo "Skipping "$module" module deployment."
fi
done
fi
fi
# Deploy project components
###########################
# Run host setup playbooks for project
echo ""
echo "Running project-specific host setup for "$PROJECT_ID" on "$hostname""
/bin/bash "$SCRIPT_DIR"/scripts-project/host-setup-project.sh -n "$hostname"
# Build project images for the configured IdP mode
echo ""
echo "Running image build for "$PROJECT_ID" on "$hostname" in IdP mode "$PROJECT_IDP_MODE""
/bin/bash "$SCRIPT_DIR"/scripts-project/build-image-project.sh -n "$hostname" -v "$version" -r "$REMOTE"
# Deploy project containers for the configured IdP mode
echo ""
echo "Deploying "$PROJECT_ID" on "$hostname""
/bin/bash "$SCRIPT_DIR"/scripts-project/deploy-project.sh -n "$hostname" -v "$version"