|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set -o pipefail |
| 5 | + |
| 6 | +check_required_param() { |
| 7 | + PARAM_NAME="$1" |
| 8 | + PARAM_VAL="$2" |
| 9 | + if [[ -z "${PARAM_VAL}" ]]; then |
| 10 | + echo "missing parameter: '${PARAM_NAME}'" |
| 11 | + exit 1 |
| 12 | + fi |
| 13 | +} |
| 14 | + |
| 15 | +# Constants: |
| 16 | +CODEFRESH_SECRET_NAME="codefresh-token" |
| 17 | +REPO_CREDS_SECRET_NAME="autopilot-secret" |
| 18 | +ARGOCD_TOKEN_SECRET_NAME="argocd-token" |
| 19 | +ARGOCD_INITIAL_TOKEN_SECRET_NAME="argocd-initial-admin-secret" |
| 20 | +BOOTSTRAP_APP_NAME="autopilot-bootstrap" |
| 21 | +ADDITIONAL_COMPONENTS="\nevents-reporter\nrollout-reporter\nworkflow-reporter" |
| 22 | +RUNTIME_DEF_URL="https://github.com/codefresh-io/cli-v2/releases/VERSION/download/runtime.yaml" |
| 23 | + |
| 24 | +# Params: |
| 25 | +check_required_param "namespace" "${NAMESPACE}" |
| 26 | +check_required_param "csdp token" "${CSDP_TOKEN}" |
| 27 | +check_required_param "runtime repo" "${CSDP_RUNTIME_REPO}" |
| 28 | +check_required_param "git token" "${CSDP_RUNTIME_GIT_TOKEN}" |
| 29 | +check_required_param "runtime cluster" "${CSDP_RUNTIME_CLUSTER}" |
| 30 | +check_required_param "runtime ingress url" "${CSDP_RUNTIME_INGRESS_URL}" |
| 31 | +check_required_param "runtime name" "${CSDP_RUNTIME_NAME}" |
| 32 | + |
| 33 | +# Defaults: |
| 34 | +CSDP_URL="${CSDP_URL:-https://g.codefresh.io}" |
| 35 | +CSDP_RUNTIME_VERSION="${CSDP_RUNTIME_VERSION:-latest}" |
| 36 | +CSDP_GIT_INTEGRATION_PROVIDER="${CSDP_GIT_INTEGRATION_PROVIDER:-GITHUB}" |
| 37 | +CSDP_GIT_INTEGRATION_API_URL="${CSDP_GIT_INTEGRATION_API_URL:-https://api.github.com}" |
| 38 | +CSDP_GIT_INTEGRATION_TOKEN="${CSDP_GIT_INTEGRATION_TOKEN:-${CSDP_RUNTIME_GIT_TOKEN}}" |
| 39 | +CSDP_RUNTIME_REPO_CREDS_PATTERN=`echo ${CSDP_RUNTIME_REPO} | sed "s/\/[a-zA-Z0-9\?\._\-]*$//g"` |
| 40 | + |
| 41 | +create_codefresh_secret() { |
| 42 | + # Download runtime definition |
| 43 | + RUNTIME_DEF_URL=`echo "${RUNTIME_DEF_URL}" | sed s/VERSION/${CSDP_RUNTIME_VERSION}/g` |
| 44 | + |
| 45 | + echo " --> Downloading runtime definition..." |
| 46 | + echo " --> curl -f -L ${RUNTIME_DEF_URL}" |
| 47 | + RUNTIME_DEF=$(curl -SsfL "$RUNTIME_DEF_URL") |
| 48 | + RESOLVED_RUNTIME_VERSION=`echo "$RUNTIME_DEF" | yq e '.spec.version' -` |
| 49 | + echo " --> Resolved runtime version: ${RESOLVED_RUNTIME_VERSION}" |
| 50 | + echo "" |
| 51 | + |
| 52 | + # Prepare components for request |
| 53 | + COMPONENT_NAMES=`echo "$RUNTIME_DEF" | yq e '.spec.components.[].name' -` |
| 54 | + COMPONENT_NAMES=`printf "${COMPONENT_NAMES}${ADDITIONAL_COMPONENTS}" | tr '\n' ' '` |
| 55 | + COMPONENTS="[\"argo-cd\"" |
| 56 | + for COMPONENT in $COMPONENT_NAMES |
| 57 | + do |
| 58 | + CUR_COMPONENT=`echo -n "\"${CSDP_RUNTIME_NAME}-${COMPONENT}\""` |
| 59 | + COMPONENTS="${COMPONENTS},${CUR_COMPONENT}" |
| 60 | + done |
| 61 | + COMPONENTS="${COMPONENTS}]" |
| 62 | + |
| 63 | + RUNTIME_CREATE_ARGS="{ |
| 64 | + \"repo\": \"${CSDP_RUNTIME_REPO}\", |
| 65 | + \"runtimeName\":\"${CSDP_RUNTIME_NAME}\", |
| 66 | + \"cluster\":\"${CSDP_RUNTIME_CLUSTER}\", |
| 67 | + \"ingressHost\":\"${CSDP_RUNTIME_INGRESS_URL}\", |
| 68 | + \"componentNames\":${COMPONENTS}, |
| 69 | + \"runtimeVersion\":\"${RESOLVED_RUNTIME_VERSION}\" |
| 70 | + }" |
| 71 | + |
| 72 | + RUNTIME_CREATE_DATA="{\"operationName\":\"CreateRuntime\",\"variables\":{\"args\":$RUNTIME_CREATE_ARGS}" |
| 73 | + RUNTIME_CREATE_DATA+=$',"query":"mutation CreateRuntime($args: RuntimeInstallationArgs\u0021) {\\n createRuntime(installationArgs: $args) {\\n name\\n newAccessToken\\n }\\n}\\n"}' |
| 74 | + echo " --> Creating runtime with args:" |
| 75 | + echo "$RUNTIME_CREATE_ARGS" |
| 76 | + |
| 77 | + RUNTIME_CREATE_RESPONSE=`curl "${CSDP_URL}/2.0/api/graphql" \ |
| 78 | + -SsfL \ |
| 79 | + -H "Authorization: ${CSDP_TOKEN}" \ |
| 80 | + -H 'content-type: application/json' \ |
| 81 | + --compressed \ |
| 82 | + --insecure \ |
| 83 | + --data-raw "$RUNTIME_CREATE_DATA"` |
| 84 | + RUNTIME_ACCESS_TOKEN=`echo $RUNTIME_CREATE_RESPONSE | jq '.data.createRuntime.newAccessToken'` |
| 85 | + RUNTIME_ENCRYPTION_IV=`hexdump -n 16 -e '4/4 "%08x" 1 "\n"' /dev/urandom` |
| 86 | + echo " --> Runtime created!" |
| 87 | + echo "" |
| 88 | + |
| 89 | + echo " --> Creating $CODEFRESH_SECRET_NAME secret..." |
| 90 | + echo " |
| 91 | + apiVersion: v1 |
| 92 | + kind: Secret |
| 93 | + metadata: |
| 94 | + name: $CODEFRESH_SECRET_NAME |
| 95 | + namespace: $NAMESPACE |
| 96 | + stringData: |
| 97 | + token: $RUNTIME_ACCESS_TOKEN |
| 98 | + encryptionIV: $RUNTIME_ENCRYPTION_IV |
| 99 | + " | kubectl apply -f - |
| 100 | + |
| 101 | + if kubectl -n "$NAMESPACE" get secret -l io.codefresh.integration-type=git -l io.codefresh.integration-name=default 2>&1 | grep "No resources found"; then |
| 102 | + echo "" |
| 103 | + else |
| 104 | + echo " --> Found old git integration, deleteing because the data inside cannot be decrypted anymore..." |
| 105 | + kubectl -n "$NAMESPACE" delete secret -l io.codefresh.integration-type=git -l io.codefresh.integration-name=default |
| 106 | + fi |
| 107 | +} |
| 108 | + |
| 109 | +create_bootstrap_application() { |
| 110 | + echo " --> Creating $BOOTSTRAP_APP_NAME application..." |
| 111 | + echo " |
| 112 | + apiVersion: argoproj.io/v1alpha1 |
| 113 | + kind: Application |
| 114 | + metadata: |
| 115 | + labels: |
| 116 | + app.kubernetes.io/managed-by: argocd-autopilot |
| 117 | + app.kubernetes.io/name: ${BOOTSTRAP_APP_NAME} |
| 118 | + codefresh.io/internal: \"true\" |
| 119 | + name: ${BOOTSTRAP_APP_NAME} |
| 120 | + namespace: ${NAMESPACE} |
| 121 | + finalizers: |
| 122 | + - 'resources-finalizer.argocd.argoproj.io' |
| 123 | + spec: |
| 124 | + destination: |
| 125 | + namespace: ${NAMESPACE} |
| 126 | + server: https://kubernetes.default.svc |
| 127 | + ignoreDifferences: |
| 128 | + - group: argoproj.io |
| 129 | + kind: Application |
| 130 | + jsonPointers: |
| 131 | + - /status |
| 132 | + project: default |
| 133 | + source: |
| 134 | + path: bootstrap |
| 135 | + repoURL: ${CSDP_RUNTIME_REPO} |
| 136 | + syncPolicy: |
| 137 | + automated: |
| 138 | + allowEmpty: true |
| 139 | + prune: true |
| 140 | + selfHeal: true |
| 141 | + syncOptions: |
| 142 | + - allowEmpty=true |
| 143 | + " | kubectl apply -f - |
| 144 | +} |
| 145 | + |
| 146 | +create_repo_creds_secret() { |
| 147 | + echo " --> Creating $REPO_CREDS_SECRET_NAME secret..." |
| 148 | + echo " |
| 149 | + apiVersion: v1 |
| 150 | + kind: Secret |
| 151 | + metadata: |
| 152 | + labels: |
| 153 | + argocd.argoproj.io/secret-type: repo-creds |
| 154 | + name: $REPO_CREDS_SECRET_NAME |
| 155 | + namespace: $NAMESPACE |
| 156 | + stringData: |
| 157 | + type: git |
| 158 | + url: $CSDP_RUNTIME_REPO_CREDS_PATTERN |
| 159 | + password: $CSDP_RUNTIME_GIT_TOKEN |
| 160 | + username: username |
| 161 | + " | kubectl apply -f - |
| 162 | +} |
| 163 | + |
| 164 | +create_argocd_token_secret() { |
| 165 | + echo " --> Reading ArgoCD intial admin token..." |
| 166 | + INITIAL_PASSWORD=`kubectl -n ${NAMESPACE} get secret ${ARGOCD_INITIAL_TOKEN_SECRET_NAME} -o=jsonpath="{.data.password}" | base64 -d` |
| 167 | + echo "" |
| 168 | + |
| 169 | + echo " --> Running ArgoCD login..." |
| 170 | + argocd login argocd-server --plaintext --username admin --password $INITIAL_PASSWORD |
| 171 | + echo "" |
| 172 | + |
| 173 | + echo " --> Generating ArgoCD API Key..." |
| 174 | + ARGOCD_API_KEY=`argocd account generate-token -a admin --server argocd-server --plaintext` |
| 175 | + echo "" |
| 176 | + |
| 177 | + echo " --> Creating $REPO_CREDS_SECRET_NAME secret..." |
| 178 | + echo " |
| 179 | + apiVersion: v1 |
| 180 | + kind: Secret |
| 181 | + metadata: |
| 182 | + name: $ARGOCD_TOKEN_SECRET_NAME |
| 183 | + namespace: $NAMESPACE |
| 184 | + stringData: |
| 185 | + token: $ARGOCD_API_KEY |
| 186 | + " | kubectl apply -f - |
| 187 | + echo "" |
| 188 | +} |
| 189 | + |
| 190 | +create_git_integration() { |
| 191 | + GIT_INTEGRATION_CREATE_ARGS="{ |
| 192 | + \"name\": \"default\", |
| 193 | + \"provider\":\"${CSDP_GIT_INTEGRATION_PROVIDER}\", |
| 194 | + \"apiUrl\":\"${CSDP_GIT_INTEGRATION_API_URL}\", |
| 195 | + \"sharingPolicy\":\"ALL_USERS_IN_ACCOUNT\" |
| 196 | + }" |
| 197 | + |
| 198 | + GIT_INTEGRATION_CREATE_DATA="{\"operationName\":\"AddGitIntegration\",\"variables\":{\"args\":$GIT_INTEGRATION_CREATE_ARGS}" |
| 199 | + GIT_INTEGRATION_CREATE_DATA+=$',"query":"mutation AddGitIntegration($args: AddGitIntegrationArgs\u0021) {\\n addGitIntegration(args: $args) {\\n name\\n }\\n}\\n"}' |
| 200 | + |
| 201 | + echo " --> Creating default git integration with args:" |
| 202 | + echo "$GIT_INTEGRATION_CREATE_ARGS" |
| 203 | + |
| 204 | + GIT_INTEGRATION_CREATE_RESPONSE=`curl "${CSDP_RUNTIME_INGRESS_URL}/app-proxy/api/graphql" \ |
| 205 | + -SsfL \ |
| 206 | + -H "Authorization: ${CSDP_TOKEN}" \ |
| 207 | + -H 'content-type: application/json' \ |
| 208 | + --compressed \ |
| 209 | + --insecure \ |
| 210 | + --data-raw "$GIT_INTEGRATION_CREATE_DATA"` |
| 211 | + |
| 212 | + echo " --> Created git integration:" |
| 213 | + echo "${GIT_INTEGRATION_CREATE_RESPONSE}" |
| 214 | + echo "" |
| 215 | + |
| 216 | + echo " --> Registering user to default git integration" |
| 217 | + |
| 218 | + GIT_INTEGRATION_REGISTER_ARGS="{ |
| 219 | + \"name\": \"default\", |
| 220 | + \"token\":\"${CSDP_GIT_INTEGRATION_TOKEN}\" |
| 221 | + }" |
| 222 | + |
| 223 | + GIT_INTEGRATION_REGISTER_DATA="{\"operationName\":\"RegisterToGitIntegration\",\"variables\":{\"args\":$GIT_INTEGRATION_REGISTER_ARGS}" |
| 224 | + GIT_INTEGRATION_REGISTER_DATA+=$',"query":"mutation RegisterToGitIntegration($args: RegisterToGitIntegrationArgs\u0021) {\\n registerToGitIntegration(args: $args) {\\n name\\n }\\n}\\n"}' |
| 225 | + |
| 226 | + GIT_INTEGRATION_REGISTER_RESPONSE=`curl "${CSDP_RUNTIME_INGRESS_URL}/app-proxy/api/graphql" \ |
| 227 | + -SsfL \ |
| 228 | + -H "Authorization: ${CSDP_TOKEN}" \ |
| 229 | + -H 'content-type: application/json' \ |
| 230 | + --compressed \ |
| 231 | + --insecure \ |
| 232 | + --data-raw "$GIT_INTEGRATION_REGISTER_DATA"` |
| 233 | + |
| 234 | + echo " --> Register to default git integration:" |
| 235 | + echo "${GIT_INTEGRATION_REGISTER_RESPONSE}" |
| 236 | + echo "" |
| 237 | +} |
| 238 | + |
| 239 | +# |
| 240 | +# Start here: |
| 241 | +# |
| 242 | + |
| 243 | +# Print param values |
| 244 | +echo "#######################################" |
| 245 | +echo "# Starting with options: #" |
| 246 | +echo "#######################################" |
| 247 | +echo " namespace: ${NAMESPACE}" |
| 248 | +echo " csdp url: ${CSDP_URL}" |
| 249 | +echo " csdp token: ****" |
| 250 | +echo " runtime repo: ${CSDP_RUNTIME_REPO}" |
| 251 | +echo " runtime repo creds pattern: ${CSDP_RUNTIME_REPO_CREDS_PATTERN}" |
| 252 | +echo " runtime git-token: ****" |
| 253 | +echo " runtime cluster: ${CSDP_RUNTIME_CLUSTER}" |
| 254 | +echo " runtime ingress: ${CSDP_RUNTIME_INGRESS_URL}" |
| 255 | +echo " runtime name: ${CSDP_RUNTIME_NAME}" |
| 256 | +echo " runtime version: ${CSDP_RUNTIME_VERSION}" |
| 257 | +echo "#######################################" |
| 258 | +echo "" |
| 259 | + |
| 260 | +# 1. Check codefresh secret |
| 261 | +echo "Checking secret $CODEFRESH_SECRET_NAME..." |
| 262 | +if kubectl -n "$NAMESPACE" get secret "$CODEFRESH_SECRET_NAME"; then |
| 263 | + echo " --> Secret $CODEFRESH_SECRET_NAME exists" |
| 264 | +else |
| 265 | + echo " --> Secret $CODEFRESH_SECRET_NAME doesn't exists." |
| 266 | + echo "" |
| 267 | + create_codefresh_secret |
| 268 | +fi |
| 269 | +echo "" |
| 270 | +echo "" |
| 271 | + |
| 272 | +# 2. Check repo creds secret |
| 273 | +echo "Checking secret $REPO_CREDS_SECRET_NAME..." |
| 274 | +if kubectl -n "$NAMESPACE" get secret "$REPO_CREDS_SECRET_NAME"; then |
| 275 | + echo " --> Secret $REPO_CREDS_SECRET_NAME exists" |
| 276 | +else |
| 277 | + echo " --> Secret $REPO_CREDS_SECRET_NAME doesn't exists." |
| 278 | + echo "" |
| 279 | + create_repo_creds_secret |
| 280 | +fi |
| 281 | +echo "" |
| 282 | +echo "" |
| 283 | + |
| 284 | +create_argocd_token_secret |
| 285 | +echo "" |
| 286 | +echo "" |
| 287 | + |
| 288 | +# 4. Check bootstrap application |
| 289 | +echo "Checking application $BOOTSTRAP_APP_NAME..." |
| 290 | +if kubectl -n "$NAMESPACE" get application "$BOOTSTRAP_APP_NAME"; then |
| 291 | + echo " --> Application $BOOTSTRAP_APP_NAME exists" |
| 292 | +else |
| 293 | + echo " --> Application $BOOTSTRAP_APP_NAME doesn't exists." |
| 294 | + echo "" |
| 295 | + create_bootstrap_application |
| 296 | +fi |
| 297 | +echo "" |
| 298 | + |
| 299 | +# 5. Check git integration |
| 300 | +echo "Checking default git integration..." |
| 301 | +echo "Checking application $BOOTSTRAP_APP_NAME..." |
| 302 | +if kubectl -n "$NAMESPACE" get secret -l io.codefresh.integration-type=git -l io.codefresh.integration-name=default 2>&1 | grep "No resources found"; then |
| 303 | + echo " --> Default git integration doesn't exists." |
| 304 | + echo "" |
| 305 | + create_git_integration |
| 306 | +else |
| 307 | + echo " --> Default git integration exists" |
| 308 | +fi |
| 309 | +echo "" |
| 310 | + |
| 311 | +echo "Done!" |
0 commit comments