forked from openshift-pipelines/pipeline-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev_setup.sh
executable file
·274 lines (239 loc) · 8.36 KB
/
dev_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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env bash
#quit if exit status of any cmd is a non-zero value
set -euo pipefail
DEV_DIR="$(
cd "$(dirname "$0")" >/dev/null
pwd
)"
PROJECT_DIR="$(
cd "$DEV_DIR/../.." >/dev/null || exit 1
pwd
)"
# shellcheck source=operator/images/cluster-setup/content/bin/utils.sh
source "$PROJECT_DIR/operator/images/cluster-setup/content/bin/utils.sh"
GITOPS_DIR="$PROJECT_DIR/operator/gitops"
CONFIG="$DEV_DIR/../config.yaml"
KUBECONFIG=${KUBECONFIG:-$HOME/.kube/config}
usage() {
TMPDIR=$(dirname "$(mktemp -u)")
echo "
Usage:
${0##*/} [options]
Setup Pipeline Service on a single cluster.
Optional arguments:
--force
No question asked.
--use-current-branch
Use the current branch to deploy the application. In the case of a detached
head, the revision is used instead.
-w, --work-dir
Directory in which to create the gitops file structure.
If the directory already exists, all content will be removed.
By default a temporary directory will be created in $TMPDIR.
-d, --debug
Activate tracing/debug mode.
-h, --help
Display this message.
Example:
${0##*/}
" >&2
}
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--force)
FORCE=1
;;
--use-current-branch)
USE_CURRENT_BRANCH="1"
;;
-w | --work-dir)
shift
WORK_DIR="$1"
mkdir -p "$WORK_DIR"
WORK_DIR="$(
cd "$1" >/dev/null
pwd
)"
;;
-d | --debug)
set -x
DEBUG="--debug"
;;
-h | --help)
usage
exit 0
;;
--)
# End of arguments
break
;;
*)
echo "Unknown argument: $1"
usage
exit 1
;;
esac
shift
done
git fetch >/dev/null
if [ -z "${FORCE:-}" ] && [ -n "${USE_CURRENT_BRANCH:-}" ] && ! git diff --quiet "@{upstream}"; then
while true; do
read -r -p "You have uncommitted/unpushed changes, do you want to continue? [y/N]: " answer
case "$answer" in
y | Y)
break
;;
n | N | "")
exit 0
;;
esac
done
fi
}
# Checks if a binary is present on the local system
precheck_binary() {
for binary in "$@"; do
command -v "$binary" >/dev/null 2>&1 || {
echo "[ERROR] This script requires '$binary' to be installed on your local machine." >&2
exit 1
}
done
}
init() {
# get the list of APPS to be installed
read -ra APP_LIST <<< "$(yq eval '.apps // [] | join(" ")' "$CONFIG")"
GIT_URL=$(yq '.git_url // "https://github.com/openshift-pipelines/pipeline-service.git"' "$CONFIG")
GIT_REF=$(yq '.git_ref // "main"' "$CONFIG")
# Create SRE repository folder
WORK_DIR="${WORK_DIR:-}"
if [[ -z "$WORK_DIR" ]]; then
WORK_DIR=$(mktemp -d)
echo "Working directory: $WORK_DIR"
fi
rsync --archive --delete --exclude .gitignore --exclude README.md "$GITOPS_DIR/sre/" "$WORK_DIR"
mkdir -p "$WORK_DIR/credentials/kubeconfig/compute"
cp "$KUBECONFIG" "$WORK_DIR/credentials/kubeconfig/compute/compute.kubeconfig.base"
KUBECONFIG="$WORK_DIR/credentials/kubeconfig/compute/compute.kubeconfig.base"
export KUBECONFIG
}
check_cluster_role() {
if [ "$(kubectl auth can-i '*' '*' --all-namespaces)" != "yes" ]; then
echo
echo "[ERROR] User '$(oc whoami)' does not have the required 'cluster-admin' role." >&2
echo "Log into the cluster with a user with the required privileges (e.g. kubeadmin) and retry."
exit 1
fi
}
install_openshift_gitops() {
APP="openshift-gitops"
local ns="$APP"
#############################################################################
# Install the gitops operator
#############################################################################
echo -n "- OpenShift-GitOps: "
kubectl apply -k "$DEV_DIR/operators/$APP" >/dev/null
echo "OK"
# Subscription information for potential debug
mkdir -p "$WORK_DIR/logs/$APP"
kubectl get subscriptions $APP-operator -n openshift-operators -o yaml >"$WORK_DIR/logs/$APP/subscription.yaml"
#############################################################################
# Wait for the URL to be available
#############################################################################
echo -n "- Argo CD dashboard: "
test_cmd="kubectl get route/openshift-gitops-server --ignore-not-found -n $ns -o jsonpath={.spec.host}"
ARGOCD_HOSTNAME="$(${test_cmd})"
until curl --fail --insecure --output /dev/null --silent "https://$ARGOCD_HOSTNAME"; do
echo -n "."
sleep 2
ARGOCD_HOSTNAME="$(${test_cmd})"
done
echo "OK"
echo "- Argo CD URL: https://$ARGOCD_HOSTNAME"
#############################################################################
# Post install
#############################################################################
# Log into Argo CD
echo -n "- Argo CD Login: "
local argocd_password
argocd_password="$(kubectl get secret openshift-gitops-cluster -n $ns -o jsonpath="{.data.admin\.password}" | base64 --decode)"
argocd login "$ARGOCD_HOSTNAME" --grpc-web --insecure --username admin --password "$argocd_password" >/dev/null
echo "OK"
# Register the host cluster as pipeline-cluster
local cluster_name="plnsvc"
if ! argocd cluster get "$cluster_name" >/dev/null 2>&1; then
echo "- Register host cluster to ArgoCD as '$cluster_name': "
argocd cluster add "$(yq e ".current-context" <"$KUBECONFIG")" --name="$cluster_name" --upsert --yes >/dev/null
echo " OK"
else
echo "- Register host cluster to ArgoCD as '$cluster_name': OK"
fi
}
setup_compute_access() {
if [ -n "${USE_CURRENT_BRANCH:-}" ]; then
kustomization_dir="$PROJECT_DIR/operator/gitops/compute/pipeline-service-manager"
else
kustomization_dir="$GIT_URL/operator/gitops/compute/pipeline-service-manager?ref=$GIT_REF"
fi
"$PROJECT_DIR/operator/images/access-setup/content/bin/setup_compute.sh" \
${DEBUG:+"$DEBUG"} \
--kubeconfig "$KUBECONFIG" \
--work-dir "$WORK_DIR" \
--kustomization "$kustomization_dir" |
indent 2
}
install_pipeline_service() {
#############################################################################
# Setup working directory
#############################################################################
TEKTON_RESULTS_DATABASE_USER="$(yq '.tekton_results_db.user' "$CONFIG")"
TEKTON_RESULTS_DATABASE_PASSWORD="$(yq '.tekton_results_db.password' "$CONFIG")"
export TEKTON_RESULTS_DATABASE_USER
export TEKTON_RESULTS_DATABASE_PASSWORD
TEKTON_RESULTS_S3_USER="$(yq '.tekton_results_s3.user // "minio"' "$CONFIG")"
TEKTON_RESULTS_S3_PASSWORD="$(yq ".tekton_results_s3.password // \"$(openssl rand -base64 20)\"" "$CONFIG")"
export TEKTON_RESULTS_S3_USER
export TEKTON_RESULTS_S3_PASSWORD
echo "- Setup working directory:"
"$PROJECT_DIR/operator/images/access-setup/content/bin/setup_work_dir.sh" \
${DEBUG:+"$DEBUG"} \
--work-dir "$WORK_DIR" \
--kustomization "git::$GIT_URL/developer/openshift/gitops/argocd?ref=$GIT_REF" |
indent 2
#############################################################################
# Deploy Applications
#############################################################################
echo "- Deploy applications:"
if [ -n "${USE_CURRENT_BRANCH:-}" ]; then
echo -n " - Source: "
manifest_dir="$(find "$WORK_DIR/environment/compute" -mindepth 1 -maxdepth 1 -type d)"
repo_url="$(git remote get-url origin | sed "s|[email protected]:|https://github.com/|")"
branch="$(git branch --show-current)"
# In the case of a PR, there's no branch, so use the revision instead
branch="${branch:-$(git rev-parse HEAD)}"
kubectl create -k "$manifest_dir" --dry-run=client -o yaml >"$manifest_dir/pipeline-service.yaml"
yq -i ".spec.source.repoURL=\"$repo_url\" | .spec.source.targetRevision=\"$branch\"" "$manifest_dir/pipeline-service.yaml"
yq -i '.resources[0]="pipeline-service.yaml"' "$manifest_dir/kustomization.yaml"
echo "$(echo "$repo_url" | sed "s:\.git$::")/tree/$branch"
fi
"$PROJECT_DIR/operator/images/cluster-setup/content/bin/install.sh" \
${DEBUG:+"$DEBUG"} \
--workspace-dir "$WORK_DIR" | indent 2
}
main() {
parse_args "$@"
precheck_binary "curl" "argocd" "kubectl" "yq"
init
check_cluster_role
echo "[compute-access]"
setup_compute_access
echo
for APP in "${APP_LIST[@]}"; do
echo "[$APP]"
install_"$(echo "$APP" | tr '-' '_')" | indent 2
echo
done
}
if [ "${BASH_SOURCE[0]}" == "$0" ]; then
main "$@"
fi