@@ -17,6 +17,8 @@ if [[ $# -lt 1 ]]; then
1717 usage
1818fi
1919
20+ source /home/core/metadata-utils.sh
21+
2022# To accommodate the use of SSH URLs for public Git repositories, set the following Git configuration:
2123# Note: This script is to be run as root on Flatcar Linux. We need to set system config instead of global config because
2224# the latter requires $HOME to be set and root is $HOME-less.
@@ -28,21 +30,69 @@ readonly LOCAL_REPO=/home/core/devcontainer
2830# will fail.
2931if [[ -d " ${LOCAL_REPO} /.git" ]]; then
3032 echo " Git repo already exists, skip cloning..."
33+ exit 0
34+ fi
35+
36+ PRIVATE_DEVCONTAINER_ENABLED=" $( get_metadata_value " private-devcontainer-enabled" " " ) "
37+ # Check if repo is private by attempting to list files
38+ if [[ " ${PRIVATE_DEVCONTAINER_ENABLED} " = " TRUE" ]] && ! git ls-remote " ${REPO_SRC} " & > /dev/null; then
39+ # disable logs to not expose access token
40+ set +o xtrace
41+
42+ # Retrieve GitHub access token
43+ response=$( curl https://workbench-dev.verily.com/api/ecm/api/oauth/v1/github/access-token \
44+ -w " \n%{http_code}" \
45+ -H " Authorization: Bearer $( /home/core/wb.sh auth print-access-token) " )
46+ http_code=$( echo " ${response} " | tail -n1)
47+ body=$( echo " ${response} " | head -n -1)
48+ if [[ ${http_code} -eq 404 ]]; then
49+ set_metadata " startup_script/status" " ERROR"
50+ set_metadata " startup_script/message" " Failed to clone the devcontainer GitHub repo. Please verify your GitHub account is linked and try recreating the VM."
51+ exit 1
52+ elif [[ ${http_code} -ne 200 ]]; then
53+ set_metadata " startup_script/status" " ERROR"
54+ set_metadata " startup_script/message" " Failed to clone the devcontainer GitHub repo. ERROR: ${body} ."
55+ exit 1
56+ fi
57+
58+ token=$( echo " ${response} " | head -n1)
59+ # Insert token into url
60+ repo_auth_url=$( echo " ${REPO_SRC} " | sed " s/:\/\//:\/\/${token} @/" )
61+
62+ # Clone the private repo
63+ response=$( git clone " ${repo_auth_url} " " ${LOCAL_REPO} " 2>&1 )
64+ git_status=$?
65+ if [[ ${git_status} -ne 0 ]]; then
66+ set_metadata " startup_script/status" " ERROR"
67+ set_metadata " startup_script/message" " Failed to clone the devcontainer GitHub repo. ERROR: ${response} "
68+ exit 1
69+ fi
70+
71+ # re-enable logs
72+ set -o xtrace
3173else
32- git clone " ${REPO_SRC} " " ${LOCAL_REPO} "
33- if [[ $# -eq 2 ]]; then
34- readonly GIT_REF=" $2 "
35- pushd " ${LOCAL_REPO} "
36- if git show-ref --verify --quiet " refs/heads/${GIT_REF} " ; then
37- # this is a local branch
38- git switch --detach " ${GIT_REF} "
39- elif git show-ref --verify --quiet " refs/remotes/origin/${GIT_REF} " ; then
40- # this is a remote branch
41- git switch --detach " origin/${GIT_REF} "
42- else
43- # this is a commit hash
44- git switch --detach " ${GIT_REF} "
45- fi
46- popd
47- fi
74+ # GitHub repo is public
75+ response=$( git clone " ${REPO_SRC} " " ${LOCAL_REPO} " 2>&1 )
76+ git_status=$?
77+ if [[ ${git_status} -ne 0 ]]; then
78+ set_metadata " startup_script/status" " ERROR"
79+ set_metadata " startup_script/message" " Failed to clone the devcontainer GitHub repo. ERROR: ${response} "
80+ exit 1
81+ fi
4882fi
83+
84+ if [[ $# -eq 2 ]]; then
85+ readonly GIT_REF=" $2 "
86+ pushd " ${LOCAL_REPO} "
87+ if git show-ref --verify --quiet " refs/heads/${GIT_REF} " ; then
88+ # this is a local branch
89+ git switch --detach " ${GIT_REF} "
90+ elif git show-ref --verify --quiet " refs/remotes/origin/${GIT_REF} " ; then
91+ # this is a remote branch
92+ git switch --detach " origin/${GIT_REF} "
93+ else
94+ # this is a commit hash
95+ git switch --detach " ${GIT_REF} "
96+ fi
97+ popd
98+ fi
0 commit comments