|
| 1 | + |
| 2 | +show_credentials () { |
| 3 | + CYAN='\033[0;36m' |
| 4 | + GREEN='\033[1;32m' |
| 5 | + NC='\033[0m' # No Color |
| 6 | + password=$(get_password) |
| 7 | + echo -e "${GREEN}Credentials for ${CYAN}https://vendor.replicated.com" |
| 8 | + echo -e "${GREEN}Username: ${CYAN}${INSTRUQT_PARTICIPANT_ID}@replicated-labs.com" |
| 9 | + echo -e "${GREEN}Password: ${CYAN}${password}${NC}" |
| 10 | +} |
| 11 | + |
| 12 | +get_replicated_sdk_version () { |
| 13 | + set +eu pipefail |
| 14 | + replicated_sdk_version=$(agent variable get REPLICATED_SDK_VERSION) |
| 15 | + |
| 16 | + # if we don't already have a token, fetch one |
| 17 | + if [[ -z "$replicated_sdk_version" ]]; then |
| 18 | + set -eu pipefail |
| 19 | + token=$(curl --silent "https://registry.replicated.com/v2/token?scope=repository:library/replicated:pull&service=registry.replicated.com" | jq -r .token) |
| 20 | + replicated_sdk_version=$(curl --silent -H "Authorization: Bearer ${token}" https://registry.replicated.com/v2/library/replicated/tags/list | jq -r '.tags[]' | awk -F '[.-]' '{ |
| 21 | + # Extract version components |
| 22 | + major=$1; |
| 23 | + minor=$2; |
| 24 | + patch=$3; |
| 25 | + prerelease=$4; |
| 26 | + prerelease_number=$5; |
| 27 | +
|
| 28 | + # Assign priority to pre-release versions |
| 29 | + if (prerelease == "alpha") { |
| 30 | + prerelease_priority = 1; |
| 31 | + } else if (prerelease == "beta") { |
| 32 | + prerelease_priority = 2; |
| 33 | + } else { |
| 34 | + prerelease_priority = 3; |
| 35 | + } |
| 36 | +
|
| 37 | + # Handle missing pre-release number |
| 38 | + if (prerelease_number == "") { |
| 39 | + prerelease_number = 0; |
| 40 | + } |
| 41 | +
|
| 42 | + # Format output to aid sorting |
| 43 | + printf "%04d%04d%04d%02d%04d-%s\n", major, minor, patch, prerelease_priority, prerelease_number, $0 |
| 44 | + }' | sort -r | head -1 | sed 's/^[0-9]*-//') |
| 45 | + fi |
| 46 | + |
| 47 | + set -eu |
| 48 | + echo ${replicated_sdk_version} |
| 49 | +} |
| 50 | + |
| 51 | +get_embedded_cluster_version () { |
| 52 | + set +eu pipefail |
| 53 | + embedded_cluster_version=$(agent variable get EMBEDDED_CLUSTER_VERSION) |
| 54 | + |
| 55 | + # if we don't already have a token, fetch one |
| 56 | + if [[ -z "$empedded_cluster_version" ]]; then |
| 57 | + embedded_cluster_version=$(curl -s "https://api.github.com/repos/replicatedhq/embedded-cluster/releases/latest" | jq -r .tag_name) |
| 58 | + fi |
| 59 | + |
| 60 | + set -eu pipefail |
| 61 | + echo ${embedded_cluster_version} |
| 62 | +} |
| 63 | + |
| 64 | +get_username () { |
| 65 | + echo ${INSTRUQT_PARTICIPANT_ID}@replicated-labs.com |
| 66 | +} |
| 67 | + |
| 68 | +get_password () { |
| 69 | + password=$(echo -n "${INSTRUQT_PARTICIPANT_ID}" | sha256sum) |
| 70 | + echo ${password::20} |
| 71 | +} |
| 72 | + |
| 73 | +get_api_token () { |
| 74 | + set +eu |
| 75 | + access_token=$(agent variable get REPLICATED_API_TOKEN) |
| 76 | + |
| 77 | + # if we don't already have a token, fetch one |
| 78 | + if [[ -z "$access_token" ]]; then |
| 79 | + set -eu |
| 80 | + sleep 5 |
| 81 | + password=$(get_password) |
| 82 | + login=$( jq -n -c --arg email "${INSTRUQT_PARTICIPANT_ID}@replicated-labs.com" --arg password "${password}" '$ARGS.named' ) |
| 83 | + set +eu pipefail |
| 84 | + token=$(curl -s -H "Content-Type: application/json" --request POST -d "$login" https://api.replicated.com/vendor/v1/login | jq -r ".token") |
| 85 | + set -eu pipefail |
| 86 | + |
| 87 | + i=0 |
| 88 | + while [[ ( -z "$token" || "$token" == "null" ) && $i -lt 20 ]] |
| 89 | + do |
| 90 | + sleep $((i*5)) |
| 91 | + set +eu pipefail |
| 92 | + token=$(curl -s -H "Content-Type: application/json" --request POST -d "$login" https://api.replicated.com/vendor/v1/login | jq -r ".token") |
| 93 | + set -eu pipefail |
| 94 | + i=$((i+1)) |
| 95 | + done |
| 96 | + |
| 97 | + UUID=$(cat /proc/sys/kernel/random/uuid) |
| 98 | + apiToken=$( jq -n -c --arg name "instruqt-${UUID}" --argjson read_only false '$ARGS.named' ) |
| 99 | + access_token=$(curl -s -H "Content-Type: application/json" -H "Authorization: $token" --request POST -d "$apiToken" https://api.replicated.com/vendor/v1/user/token | jq -r ".access_token") |
| 100 | + |
| 101 | + agent variable set REPLICATED_API_TOKEN $access_token |
| 102 | + fi |
| 103 | + set +eu |
| 104 | + echo ${access_token} |
| 105 | +} |
| 106 | + |
| 107 | +get_app_slug () { |
| 108 | + application=${1:-"Slackernews"} |
| 109 | + access_token=$(get_api_token) |
| 110 | + app_slug=$(curl --header 'Accept: application/json' --header "Authorization: ${access_token}" https://api.replicated.com/vendor/v3/apps | jq -r --arg application ${application} '.apps[] | select( .name | startswith( $application )) | .slug') |
| 111 | + echo ${app_slug} |
| 112 | +} |
| 113 | + |
| 114 | +get_app_id () { |
| 115 | + application=${1:-"Slackernews"} |
| 116 | + access_token=$(get_api_token) |
| 117 | + app_id=$(curl --header 'Accept: application/json' --header "Authorization: ${access_token}" https://api.replicated.com/vendor/v3/apps | jq -r --arg application ${application} '.apps[] | select( .name | startswith( $application )) | .id') |
| 118 | + echo ${app_id} |
| 119 | +} |
| 120 | + |
| 121 | +get_customer_id () { |
| 122 | + customer=${1} |
| 123 | + access_token=$(get_api_token) |
| 124 | + app_id=$(get_app_id) |
| 125 | + customer_id=$(curl --header 'Accept: application/json' --header "Authorization: ${access_token}" https://api.replicated.com/vendor/v3/app/${app_id}/customers | jq -r --arg name $customer '.customers[] | select ( .name == $name ) | .id') |
| 126 | + echo ${customer_id} |
| 127 | +} |
| 128 | + |
| 129 | +get_license_id () { |
| 130 | + customer=${1} |
| 131 | + access_token=$(get_api_token) |
| 132 | + app_id=$(get_app_id) |
| 133 | + license_id=$(curl --header 'Accept: application/json' --header "Authorization: ${access_token}" https://api.replicated.com/vendor/v3/app/${app_id}/customers | jq -r --arg name $customer '.customers[] | select ( .name == $name ) | .installationId') |
| 134 | + echo ${license_id} |
| 135 | +} |
| 136 | + |
| 137 | +get_admin_console_password() { |
| 138 | + password=$(echo -n "${INSTRUQT_PARTICIPANT_ID}:${INSTUQT_CHALLENGE_ID}" | sha256sum) |
| 139 | + echo ${password::20} |
| 140 | +} |
| 141 | + |
| 142 | +get_slackernews_domain() { |
| 143 | + echo cluster-30443-${INSTRUQT_PARTICIPANT_ID}.env.play.instruqt.com |
| 144 | +} |
| 145 | + |
| 146 | +get_slackernews() { |
| 147 | + # get the app slug, since there's only one app created by the automation, just grab the first in the list |
| 148 | + app_slug=$(get_app_slug) |
| 149 | + |
| 150 | + # grab the sources for the Helm chart using a community license |
| 151 | + helm registry login chart.slackernews.io --username [email protected] --password 2ViYIi8SDFubA8XwQRhJtcrwn4C |
| 152 | + helm pull --untar oci://chart.slackernews.io/slackernews/slackernews |
| 153 | + |
| 154 | + # specify the nodeport for NGINX so we get a consistent and addressable endpoint |
| 155 | + # TODO: Update upstream to take this as a value |
| 156 | + sed -i '17 a\ nodePort: 30443' slackernews/templates/nginx-service.yaml |
| 157 | + |
| 158 | + # remove the Replicated SDK dependency, if we add more dependencies to |
| 159 | + # Slackernews this will need to be revised |
| 160 | + yq -i 'del(.dependencies)' slackernews/Chart.yaml |
| 161 | + |
| 162 | + # start version numbers over to simplify the lab text |
| 163 | + yq -i '.version = "0.1.0"' slackernews/Chart.yaml |
| 164 | + |
| 165 | + # get rid of troubleshoot files since leaners will create their own |
| 166 | + rm -rf slackernews/troubleshoot slackernews/templates/preflights.yaml slackernews/templates/support-bundle.yaml |
| 167 | + |
| 168 | + # set the values file ot use the right proxy image URI |
| 169 | + web_image=$(yq .images.slackernews.repository slackernews/values.yaml) |
| 170 | + rewritten_web_image=${web_image//images.slackernews.io/proxy.replicated.com} |
| 171 | + rewritten_web_image=${rewritten_web_image//proxy\/slackernews/proxy\/${app_slug}} |
| 172 | + yq -i ".images.slackernews.repository = \"${rewritten_web_image}\"" slackernews/values.yaml |
| 173 | + |
| 174 | + nginx_image=$(yq .images.nginx.repository slackernews/values.yaml) |
| 175 | + rewritten_nginx_image=${nginx_image//images.slackernews.io/proxy.replicated.com} |
| 176 | + rewritten_nginx_image=${rewritten_nginx_image//proxy\/slackernews/proxy\/${app_slug}} |
| 177 | + yq -i ".images.nginx.repository = \"${rewritten_nginx_image}\"" slackernews/values.yaml |
| 178 | + |
| 179 | + # add some optional components to make the application a bit more representative |
| 180 | + yq -i '.nginx.enabled = true' slackernews/values.yaml |
| 181 | + yq -i '.postgres.deploy_postgres = true' slackernews/values.yaml |
| 182 | + yq -i '.postgres.enabled = true' slackernews/values.yaml |
| 183 | + yq -i '.postgres.password = "thisisasecret"' slackernews/values.yaml |
| 184 | + |
| 185 | + # address awkward scenario where a TLS cert is required even if TLS isn't enabled |
| 186 | + # TODO: Fix upstream to not require TLS certs uneless TLS is enabled |
| 187 | + openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -keyout server.key -out server.crt -subj "/CN=Slackernews" -addext "subjectAltName = DNS:$(get_slackernews_domain)" \ |
| 188 | + && yq -i ".service.tls.key = \"$(cat server.key)\"" slackernews/values.yaml \ |
| 189 | + && rm server.key \ |
| 190 | + && yq -i ".service.tls.cert = \"$(cat server.crt)\"" slackernews/values.yaml \ |
| 191 | + && rm server.crt |
| 192 | + |
| 193 | + # since we have the certs anyway, let's enable TLS |
| 194 | + yq -i '.service.tls.enabled = true' slackernews/values.yaml |
| 195 | + |
| 196 | + # let's also deelte the values injected by Replicated so users can release |
| 197 | + # the chart without any sort of double injection |
| 198 | + yq -i 'del(.replicated)' slackernews/values.yaml |
| 199 | + yq -i 'del(.global.replicated)' slackernews/values.yaml |
| 200 | +} |
0 commit comments