Skip to content

Commit

Permalink
Make enable-ssh work for both linux and macos
Browse files Browse the repository at this point in the history
  • Loading branch information
Wasapl committed Feb 28, 2020
1 parent eb0e977 commit 576d275
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
40 changes: 29 additions & 11 deletions scripts/enable-ssh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand All @@ -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:"
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions scripts/enable-vnc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 576d275

Please sign in to comment.