-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.sh
executable file
·157 lines (141 loc) · 3.87 KB
/
setup.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
#!/usr/bin/env bash
# shellcheck shell=bash
set -euf -o pipefail
RESET=$(tput sgr0)
RED=$(tput setaf 9)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 6)
readonly RESET RED GREEN BLUE
# For some commands we must invoke a Windows executable if in the context of
# WSL.
IS_WSL=$(grep -q WSL /proc/version 2>/dev/null && echo "true" || echo "false")
readonly IS_WSL
if [[ "${IS_WSL}" == "true" ]]; then
MKCERT=mkcert.exe
else
MKCERT=mkcert
fi
# Used to include host-platform specific docker compose files.
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
readonly OS
function executable_exists {
local executable="${1}"
if ! command -v "${executable}" >/dev/null; then
return 1
fi
return 0
}
function has_prerequisites {
local executables=(
"docker"
"${MKCERT}"
)
for executable in "${executables[@]}"; do
if ! executable_exists "${executable}"; then
cat <<-EOT >&2
${RED}Could not find executable: ${executable}${RESET}
${BLUE}Consult the README.md for how to install requirements.${RESET}
EOT
return 1
fi
done
echo "Prerequisites found..."
return 0
}
function valid_repository_name {
local name="${1}"
echo "${name}" | awk '/^[a-zA-Z0-9_-]+$/' | grep . &>/dev/null
return $?
}
function get_repository_name {
read -r -p "Please enter a name for your new git repository: " name
if ! valid_repository_name "${name}"; then
echo "Invalid repository name ${name} (Only alpha-numeric, underscores, and hyphens allowed)"
exit 1
fi
echo "${name}"
}
function create_repository {
local repository
repository=$(get_repository_name)
echo "Creating new repository: ${repository}..."
mkdir -p "${repository}"
pushd "${repository}" 2>/dev/null
git init
}
function get_refs {
local repository="${1}"
echo "refs/heads/main" # Only interested in the main branch.
git ls-remote --sort=-version:refname "${repository}" 'refs/tags/*' | cut -f2
}
function choose_ref {
local repository="${1}"
local refs=()
local display=()
local display_ref
while IFS='' read -r line; do refs+=("$line"); done < <(get_refs "${repository}")
# Only show the user the interesting part of the ref.
for ref in "${refs[@]}"; do
display_ref=$(echo "${ref}" | sed -e 's/^.*\/\([^\/]*\)$/\1/g')
display+=("${display_ref}")
done
PS3="Choose a branch/tag from ${repository##*/}: "
select ref in "${display[@]}"; do
echo "${refs[$REPLY - 1]}"
return
done
}
function initialize_from_site_template {
local repo="https://github.com/Islandora-Devops/isle-site-template"
local ref
echo "Initializing from site template..."
ref=$(choose_ref "${repo}")
curl -L "${repo}/archive/${ref}.tar.gz" | tar -xz --strip-components=1
rm -fr .github setup.sh
git add .
git commit -am "First commit, added isle-site-template."
}
function initialize_from_starter_site {
local repo="https://github.com/Islandora-Devops/islandora-starter-site"
local ref
echo "Initializing from starter site..."
ref=$(choose_ref "${repo}")
wget \
-c "${repo}/archive/${ref}.tar.gz" \
-O - | tar --strip-components=1 -C drupal/rootfs/var/www/drupal -xz
rm -fr drupal/rootfs/var/www/drupal/.github
git checkout drupal/rootfs/var/www/drupal/assets/patches/default_settings.txt
git add .
git commit -am "Second commit, added isle-starter-site."
}
function create_docker_compose_override {
if [[ "${IS_WSL}" == "false" ]]; then
cp "docker-compose.${OS}.yml" docker-compose.override.yml
fi
}
function generate_certs {
./generate-certs.sh
}
function generate_secrets {
./generate-secrets.sh
}
function main {
cat <<-EOT
${GREEN}
#################################
# Setting up isle-site-template #
#################################
${RESET}
EOT
if has_prerequisites; then
create_repository
initialize_from_site_template
initialize_from_starter_site
create_docker_compose_override
generate_certs
generate_secrets
else
exit 1
fi
}
main