|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set +e |
| 4 | + |
| 5 | +# use VSCode script to start Docker |
| 6 | +/usr/local/share/docker-init.sh |
| 7 | + |
| 8 | +# for D-in-D case container engine will be always Docker |
| 9 | +# however we do not want to hardcode it for future cases |
| 10 | +if [ "$(command -v podman)" ]; then |
| 11 | + CONTAINER_ENGINE="podman" |
| 12 | +elif [ "$(command -v docker)" ]; then |
| 13 | + CONTAINER_ENGINE="docker" |
| 14 | +else |
| 15 | + echo "ERROR: Failed to find container engine. Please install docker or podman." >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +# always prune old containers to clean the lab on laptops |
| 20 | +${CONTAINER_ENGINE} container prune -f |
| 21 | + |
| 22 | +if [ -z "${CEOS_IMAGE_FULLPATH}" ]; then |
| 23 | + CEOS_IMAGE_FULLPATH="${CONTAINERWSF}/cEOS*tar.xz" |
| 24 | +fi |
| 25 | +if [ -z "${CEOS_MD5_FULLPATH}" ]; then |
| 26 | + CEOS_MD5_FULLPATH="${CONTAINERWSF}/cEOS*tar.xz.sha512sum" |
| 27 | +fi |
| 28 | +# check if ceos-lab image already present |
| 29 | +if [ -z "$(${CONTAINER_ENGINE} image ls | grep 'arista/ceos')" ]; then |
| 30 | + if [ "${ARISTA_TOKEN}" ]; then |
| 31 | + # `uname -m` is used to find platform architecture |
| 32 | + # currently we check for arm64 and aarch64 and expect everything else to be an x86 machine |
| 33 | + echo "$(uname -m)" |
| 34 | + if [ "$(uname -m)" = "aarch64" ] || [ "$(uname -m)" = "arm64" ]; then |
| 35 | + ardl get eos --format cEOSarm --version ${CEOS_LAB_VERSION} --import-docker |
| 36 | + ${CONTAINER_ENGINE} tag arista/ceos:${CEOS_LAB_VERSION} arista/ceos:latest |
| 37 | + else |
| 38 | + ardl get eos --format cEOS --version ${CEOS_LAB_VERSION} --import-docker |
| 39 | + ${CONTAINER_ENGINE} tag arista/ceos:${CEOS_LAB_VERSION} arista/ceos:latest |
| 40 | + fi |
| 41 | + # confirm that cEOS image was deleted just in case ardl failed to do that |
| 42 | + rm -rf ${CEOS_IMAGE_FULLPATH} >/dev/null 2>&1 |
| 43 | + rm -rf ${CEOS_MD5_FULLPATH} >/dev/null 2>&1 |
| 44 | + else |
| 45 | + # if ARISTA_TOKEN is not defined - we'll try find image in the workspace cEOS*.tar.xz |
| 46 | + if [ -e ${CEOS_IMAGE_FULLPATH} ]; then |
| 47 | + ${CONTAINER_ENGINE} import ${CEOS_IMAGE_FULLPATH} arista/ceos:latest |
| 48 | + rm ${CEOS_IMAGE_FULLPATH} |
| 49 | + # also delete SHA if it was copied, currently we do not check if archive was broken |
| 50 | + rm -rf ${CEOS_MD5_FULLPATH} >/dev/null 2>&1 |
| 51 | + echo "WARNING: cEOS-lab image was successfully imported from the workspace." |
| 52 | + else |
| 53 | + echo "WARNING: arista.com token was not defined and cEOS-lab image is not present in container workspace!" |
| 54 | + fi 2>/dev/null |
| 55 | + fi |
| 56 | +else |
| 57 | + echo "WARNING: cEOS-lab image already present. Skipping the image pull/download." |
| 58 | +fi |
| 59 | + |
| 60 | +# add aliases if any were defined |
| 61 | +if [ -f "${CONTAINERWSF}/addAliases.sh" ]; then |
| 62 | + chmod +x ${CONTAINERWSF}/addAliases.sh |
| 63 | + ${CONTAINERWSF}/addAliases.sh |
| 64 | +fi |
| 65 | + |
| 66 | +# when running on Codespaces, replace any Github variables in lab files |
| 67 | +if ${CODESPACES}; then |
| 68 | + # replace all markdown vars in demo directory |
| 69 | + if [ "${GITHUB_REPOSITORY}" ]; then |
| 70 | + grep -rl '{{gh.repo_name}}' . --exclude-dir .git | xargs sed -i 's/{{gh.repo_name}}/'"${GITHUB_REPOSITORY##*/}"'/g' |
| 71 | + grep -rl '{{gh.org_name}}' . --exclude-dir .git | xargs sed -i 's/{{gh.org_name}}/'"${GITHUB_REPOSITORY%%/*}"'/g' |
| 72 | + grep -rl '{{gh.repository}}' . --exclude-dir .git | xargs sed -i 's@{{gh.repository}}@'"${GITHUB_REPOSITORY}"'@g' |
| 73 | + else |
| 74 | + # if not running on Codespaces and GITHUB_REPOSITORY is not set - set aristanetworks/acLabs by default |
| 75 | + grep -rl '{{gh.repo_name}}' . --exclude-dir .git | xargs sed -i 's/{{gh.repo_name}}/'"acLabs"'/g' |
| 76 | + grep -rl '{{gh.org_name}}' . --exclude-dir .git | xargs sed -i 's/{{gh.org_name}}/'"aristanetworks"'/g' |
| 77 | + grep -rl '{{gh.repository}}' . --exclude-dir .git | xargs sed -i 's@{{gh.repository}}@'"aristanetworks/acLabs"'@g' |
| 78 | + fi |
| 79 | +fi |
| 80 | + |
| 81 | +# update URL in clab init configs if set |
| 82 | +if [ "${CVURL}" ]; then |
| 83 | + grep -rl '{{cv_url}}' . --exclude-dir .git | xargs sed -i 's@{{cv_url}}@'"${CVURL}"'@g' |
| 84 | + # check for legacy labs using hardcoded URL |
| 85 | + grep -rl 'cv-staging.corp.arista.io' . --exclude-dir .git | xargs sed -i 's@cv-staging.corp.arista.io@'"${CVURL}"'@g' |
| 86 | +else |
| 87 | + # set defaul url to staging if nothing is defined |
| 88 | + grep -rl '{{cv_url}}' . --exclude-dir .git | xargs sed -i 's@{{cv_url}}@'"cv-staging.corp.arista.io"'@g' |
| 89 | +fi |
| 90 | + |
| 91 | +if [ "${CV_API_TOKEN}" ]; then |
| 92 | + if [ "${CVURL}" ]; then |
| 93 | + CVTOKEN=$(curl -H "Authorization: Bearer ${CV_API_TOKEN}" "https://www.${CVURL}/api/v3/services/admin.Enrollment/AddEnrollmentToken" -d '{"enrollmentToken":{"reenrollDevices":["*"],"validFor":"24h"}}' | sed -n 's|.*"token":"\([^"]*\)".*|\1|p') |
| 94 | + else |
| 95 | + CVTOKEN=$(curl -H "Authorization: Bearer ${CV_API_TOKEN}" "https://www.cv-staging.corp.arista.io/api/v3/services/admin.Enrollment/AddEnrollmentToken" -d '{"enrollmentToken":{"reenrollDevices":["*"],"validFor":"24h"}}' | sed -n 's|.*"token":"\([^"]*\)".*|\1|p') |
| 96 | + fi |
| 97 | + if [ "${CVTOKEN}" ]; then |
| 98 | + echo "$CVTOKEN" > ${CONTAINERWSF}/clab/cv-onboarding-token |
| 99 | + else |
| 100 | + echo "ERROR: failed to generate onboarding token!" |
| 101 | + fi |
| 102 | +else |
| 103 | + # add default to avoid clab failing due to missing file |
| 104 | + echo "CAFECAFE" > ${CONTAINERWSF}/clab/cv-onboarding-token |
| 105 | +fi |
| 106 | + |
| 107 | +if [ -f "${CONTAINERWSF}/postCreate.sh" ]; then |
| 108 | + chmod +x ${CONTAINERWSF}/postCreate.sh |
| 109 | + ${CONTAINERWSF}/postCreate.sh |
| 110 | + # delete postCreate.sh after use to avoid confusing lab user |
| 111 | + rm ${CONTAINERWSF}/postCreate.sh |
| 112 | +fi |
| 113 | + |
| 114 | +# init demo dir as Git repo if requested for this demo env |
| 115 | +if ${GIT_INIT:-false}; then |
| 116 | + cd ${CONTAINERWSF} |
| 117 | + # if not a git repo already - init |
| 118 | + if [ -z "$(git status 2>/dev/null)" ]; then |
| 119 | + git init |
| 120 | + git config --global --add safe.directory ${PWD} |
| 121 | + if [ -z "$(git config user.name)" ]; then git config user.name "Lab User"; fi |
| 122 | + if [ -z "$(git config user.email)" ]; then git config user.email user@one-click.lab; fi |
| 123 | + git add . |
| 124 | + git commit -m "git init" |
| 125 | + fi |
| 126 | +fi |
| 127 | + |
| 128 | +if [ -z "${CODE_SERVER_BIND_ADDR}" ]; then |
| 129 | + CODE_SERVER_BIND_ADDR="0.0.0.0:8080" |
| 130 | +fi |
| 131 | +code-server --bind-addr ${CODE_SERVER_BIND_ADDR} --auth password --disable-telemetry --disable-update-check --disable-workspace-trust "${CONTAINERWSF}" & |
| 132 | + |
| 133 | +# check if image is still missing and print a warning |
| 134 | +if [ -z "$(${CONTAINER_ENGINE} image ls | grep 'arista/ceos')" ]; then |
| 135 | + echo "WARNING: cEOS-lab image download failed. Try to upload and import it manually." |
| 136 | + echo " Please make sure that image is imported with a correct name - arista/ceos:latest" |
| 137 | + echo " The lab will not auto start! Run 'make start' when image is ready." |
| 138 | +else |
| 139 | + cd ${CONTAINERWSF} |
| 140 | + make start |
| 141 | +fi |
| 142 | + |
| 143 | +# on Codespaces this will not work correctly |
| 144 | +if ! ${CODESPACES:-false}; then |
| 145 | + # Execute command from docker cli if any. |
| 146 | + if [ ${@+True} ]; then |
| 147 | + exec "$@" |
| 148 | + # Otherwise just enter sh or zsh. |
| 149 | + else |
| 150 | + if [ -f "/bin/zsh" ]; then |
| 151 | + exec zsh |
| 152 | + else |
| 153 | + exec sh |
| 154 | + fi |
| 155 | + fi |
| 156 | +fi |
0 commit comments