forked from weaveworks/wks-quickstart-firekube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·172 lines (144 loc) · 4.3 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
unset CDPATH
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "${SCRIPT_DIR}" || exit 1
. lib/functions.sh
. lib/footloose.sh
. lib/ignite.sh
. lib/jk.sh
. lib/wksctl.sh
# user-overrideable via ENV
if command -v sudo >/dev/null 2>&1; then
sudo="${sudo:-"sudo"}"
else
sudo="${sudo}"
fi
set -euo pipefail
JK_VERSION=0.3.0
FOOTLOOSE_VERSION=0.6.2
IGNITE_VERSION=0.7.0
WKSCTL_VERSION=0.8.1
config_backend() {
sed -n -e 's/^backend: *\(.*\)/\1/p' config.yaml
}
set_config_backend() {
local tmp=.config.yaml.tmp
sed -e "s/^backend: .*$/backend: ${1}/" config.yaml > "${tmp}" && \
mv "${tmp}" config.yaml && \
rm -f "${tmp}"
}
do_footloose() {
if [ "$(config_backend)" == "ignite" ]; then
$sudo env "PATH=${PATH}" footloose "${@}"
else
footloose "${@}"
fi
}
if git_current_branch > /dev/null 2>&1; then
log "Using git branch: $(git_current_branch)"
else
error "Please checkout a git branch."
fi
git_remote="$(git config --get "branch.$(git_current_branch).remote" || true)" # fallback to "", user may override
git_deploy_key=""
download="yes"
download_force="no"
setup_help() {
echo "
setup.sh
- ensure dependent binaries are available
- generate a cluster config
- bootstrap the gitops cluster
- push the changes to the remote for the cluster to pick up
optional flags:
--no-download Do not download dependent binaries
--force-download Force downloading version-specific dependent binaries
--git-remote string Override the remote used for pushing changes and configuring the cluster
--git-deploy-key filepath Provide a deploy key for private/authenticated repo access
-h, -help Print this help text
"
}
while test $# -gt 0; do
case "${1}" in
--no-download)
download="no"
;;
--force-download)
download_force="yes"
;;
--git-remote)
shift
git_remote="${1}"
;;
--git-deploy-key)
shift
git_deploy_key="--git-deploy-key=${1}"
log "Using git deploy key: ${1}"
;;
-h|--help)
setup_help
exit 0
;;
*)
setup_help
error "unknown argument '${1}'"
;;
esac
shift
done
if [ "${git_remote}" ]; then
log "Using git remote: ${git_remote}"
else
error "
Please configure a remote for your current branch:
git branch --set-upstream-to <remote_name>/$(git_current_branch)
Or use the --git-remote flag:
./setup.sh --git-remote <remote_name>
Your repo has the following remotes:
$(git remote -v)"
fi
echo
if [ "${download}" == "yes" ]; then
mkdir -p "${HOME}/.wks/bin"
export PATH="${HOME}/.wks/bin:${PATH}"
fi
# On macOS, we only support the docker backend.
if [ "$(goos)" == "darwin" ]; then
set_config_backend docker
fi
check_command docker
check_version jk "${JK_VERSION}"
check_version footloose "${FOOTLOOSE_VERSION}"
if [ "$(config_backend)" == "ignite" ]; then
check_version ignite "${IGNITE_VERSION}"
fi
check_version wksctl "${WKSCTL_VERSION}"
log "Creating footloose manifest"
jk generate -f config.yaml setup.js
cluster_key="cluster-key"
if [ ! -f "${cluster_key}" ]; then
# Create the cluster ssh key with the user credentials.
log "Creating SSH key"
ssh-keygen -q -t rsa -b 4096 -C [email protected] -f ${cluster_key} -N ""
fi
log "Creating virtual machines"
do_footloose create
log "Creating Cluster API manifests"
status="footloose-status.yaml"
do_footloose status -o json > "${status}"
jk generate -f config.yaml -f "${status}" setup.js
rm -f "${status}"
log "Updating container images and git parameters"
wksctl init --git-url="$(git_http_url "$(git_remote_fetchurl "${git_remote}")")" --git-branch="$(git_current_branch)"
log "Pushing initial cluster configuration"
git add config.yaml footloose.yaml machines.yaml flux.yaml wks-controller.yaml
git diff-index --quiet HEAD || git commit -m "Initial cluster configuration"
git push "${git_remote}" HEAD
log "Installing Kubernetes cluster"
apply_args=(
"--git-url=$(git_http_url "$(git_remote_fetchurl "${git_remote}")")"
"--git-branch=$(git_current_branch)"
)
[ "${git_deploy_key}" ] && apply_args+=("${git_deploy_key}")
wksctl apply "${apply_args[@]}"
wksctl kubeconfig