From 576d27551d3edcab60e6e2b5902eb6a4da27459a Mon Sep 17 00:00:00 2001 From: Vasily Pleshakov Date: Fri, 28 Feb 2020 12:05:08 -0800 Subject: [PATCH] Make enable-ssh work for both linux and macos --- scripts/enable-ssh.sh | 40 +++++++++++++++++++++++++++++----------- scripts/enable-vnc.sh | 7 ++++--- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/scripts/enable-ssh.sh b/scripts/enable-ssh.sh index ee40712..a810999 100644 --- a/scripts/enable-ssh.sh +++ b/scripts/enable-ssh.sh @@ -3,7 +3,7 @@ USER_NAME=appveyor LOCK_FILE="${HOME}/build.lock" HOSTKEY=/etc/ssh/ssh_host_ecdsa_key.pub - +PLATFORM=$(uname -s) YELLOW='\033[0;33m' NC='\033[0m' @@ -18,18 +18,29 @@ if ! ssh-keygen -E md5 -lf /dev/stdin <<< "${APPVEYOR_SSH_KEY}" >/dev/null; then exit 2 fi -trap 'sudo ufw deny OpenSSH >/dev/null' EXIT SIGHUP SIGINT SIGQUIT SIGTERM ERR +if [ "$PLATFORM" = "Linux" ] && command -v ufw >/dev/null; then + trap 'sudo ufw deny OpenSSH >/dev/null' EXIT SIGHUP SIGINT SIGQUIT SIGTERM ERR -# open 22 port for management network interface -sudo ufw allow OpenSSH > /dev/null 2>&1 + # open 22 port for management network interface + sudo ufw allow OpenSSH > /dev/null 2>&1 +fi # get external IP address via https://www.appveyor.com/tools/my-ip.aspx EXT_IP=$(curl -sf https://www.appveyor.com/tools/my-ip.aspx) # get ip address of management network interface and figure out corresponding port on NAT -INT_IP=$(ip -o -4 addr show up primary scope global | ( read -r num dev fam addr rest; echo ${addr%/*}; )) -IFS='.' read -r -a INT_IP_ARR <<< "$INT_IP" -PORT=$(( 22000 + (${INT_IP_ARR[2]} - 0) * 256 + ${INT_IP_ARR[3]} )) +case "$PLATFORM" in + "Linux") + INT_IP=$(ip -o -4 addr show up primary scope global | ( read -r num dev fam addr rest; echo ${addr%/*}; )) + IFS='.' read -r -a INT_IP_ARR <<< "$INT_IP" + PORT=$(( 22000 + (${INT_IP_ARR[2]} - 0) * 256 + ${INT_IP_ARR[3]} )) + ;; + "Darwin") + INT_IP=$(ipconfig getifaddr en0) + IFS='.' read -r -a INT_IP_ARR <<< "$INT_IP" + PORT=$(( 22000 + INT_IP_ARR[3] )) + ;; +esac # add ssh key (if set) to authorized_keys mkdir -p ${HOME}/.ssh @@ -48,11 +59,18 @@ if [ -d /etc/update-motd.d ]; then echo "echo '" echo "Project: ${APPVEYOR_PROJECT_NAME}" echo "Build Version: ${APPVEYOR_BUILD_VERSION}" - echo "URL: ${APPVEYOR_URL}/project/${APPVEYOR_ACCOUNT_NAME}/${APPVEYOR_PROJECT_SLUG}/build/job/${APPVEYOR_JOB_ID}" + echo "URL: ${APPVEYOR_URL}/project/${APPVEYOR_ACCOUNT_NAME}/${APPVEYOR_PROJECT_SLUG}/build/job/${APPVEYOR_JOB_ID}" echo "'" ) | sudo tee /etc/update-motd.d/01-appveyor >/dev/null sudo chmod +x /etc/update-motd.d/01-appveyor fi +if [ "$PLATFORM" = "Darwin" ]; then + ( + echo "Project: ${APPVEYOR_PROJECT_NAME}" + echo "Build Version: ${APPVEYOR_BUILD_VERSION}" + echo "URL: ${APPVEYOR_URL}/project/${APPVEYOR_ACCOUNT_NAME}/${APPVEYOR_PROJECT_SLUG}/build/job/${APPVEYOR_JOB_ID}" + ) |sudo tee /etc/motd >/dev/null +fi # print out connection command echo "Connect to ${EXT_IP} port $PORT with ${USER_NAME} user:" @@ -73,11 +91,11 @@ fi if [[ -n "${APPVEYOR_SSH_BLOCK}" ]] && ${APPVEYOR_SSH_BLOCK}; then # create "lock" file. touch "${LOCK_FILE}" - echo -e "Build paused. To resume it, open a SSH session to run '${YELLOW}rm "${LOCK_FILE}"${NC}' command." + echo -e "Build paused. To resume it, open a SSH session to run '${YELLOW}rm \"${LOCK_FILE}\"${NC}' command." # export all APPVEYOR_* variables to .appveyorrc file so it could be available to ssh session - export -p|grep -E '^declare -x APPVEYOR_' > $HOME/.appveyorrc + export -p|grep -E '^declare -x APPVEYOR_' > "$HOME/.appveyorrc" # this might fail if there is multiline values - echo "source $HOME/.appveyorrc" >> $HOME/.profile + echo "source $HOME/.appveyorrc" >> "$HOME/.profile" # wait until "lock" file is deleted by user. while [ -f "${LOCK_FILE}" ]; do sleep 1 diff --git a/scripts/enable-vnc.sh b/scripts/enable-vnc.sh index 251fa4f..58027ca 100644 --- a/scripts/enable-vnc.sh +++ b/scripts/enable-vnc.sh @@ -2,6 +2,7 @@ USER_NAME=appveyor LOCK_FILE="${HOME}/Desktop/Delete me to continue build.txt" +CURRENT=$(cat /usr/local/var/appveyor/build-agent/psw) YELLOW='\033[0;33m' NC='\033[0m' @@ -10,12 +11,12 @@ if [[ -z "${APPVEYOR_VNC_PASSWORD}" ]]; then echo -e "${YELLOW}APPVEYOR_VNC_PASSWORD${NC} variable is not defined!" echo "Generating one..." USER_PASSWORD_LENGTH=20 - APPVEYOR_VNC_PASSWORD=$(LC_CTYPE=C < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${USER_PASSWORD_LENGTH};) + APPVEYOR_VNC_PASSWORD=$(head -c200 /dev/urandom | LC_CTYPE=C tr -dc _A-Z-a-z-0-9 | head -c${USER_PASSWORD_LENGTH};) echo -e "Password set to ${YELLOW}'${APPVEYOR_VNC_PASSWORD}'${NC}" fi -/usr/bin/dscl -u "$USER_NAME" -P "appveyor" . -passwd "/Users/$USER_NAME" "$APPVEYOR_VNC_PASSWORD" && -security set-keychain-password -o appveyor -p "$APPVEYOR_VNC_PASSWORD" "/Users/$USER_NAME/Library/Keychains/login.keychain" || +/usr/bin/dscl -u "$USER_NAME" -P "$CURRENT" . -passwd "/Users/$USER_NAME" "$APPVEYOR_VNC_PASSWORD" && +security set-keychain-password -o "$CURRENT" -p "$APPVEYOR_VNC_PASSWORD" "/Users/$USER_NAME/Library/Keychains/login.keychain" || { echo "Failed to change user's password! Aborting" ; exit 1; } # get external IP address via https://www.appveyor.com/tools/my-ip.aspx