From 8d1cc8e1603c04f48ee592910b8fc81e889f9d25 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Thu, 2 May 2024 01:58:07 -0400 Subject: [PATCH] testing --- .github/workflows/vagrant.yml | 51 +++-------------------------- scripts/github-vagrant.sh | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 47 deletions(-) create mode 100755 scripts/github-vagrant.sh diff --git a/.github/workflows/vagrant.yml b/.github/workflows/vagrant.yml index 6e753dd..06a8ac9 100644 --- a/.github/workflows/vagrant.yml +++ b/.github/workflows/vagrant.yml @@ -21,6 +21,9 @@ jobs: restore-keys: | ${{ runner.os }}-vagrant- + - name: Install Tools + run: brew install nmap tree + - name: Install VirtualBox run: brew install --cask virtualbox @@ -36,51 +39,5 @@ jobs: vagrant --version ansible --version - - name: Install debugging tools - run: brew install nmap tree - - name: Vagrant Up with Dockerbox Playbook - env: - SSH_AUTH_SOCK: '' - run: | - PLAYBOOK=dockerbox vagrant up --debug & - VAGRANT_UP_PID=$! - DEBUG_ID="[homelab-ci]" - echo "$DEBUG_ID Waiting for VM to start..." - sleep 60 - TIMEOUT=600 - ELAPSED=0 - SLEEP_DURATION=30 - SSH_AVAILABLE=0 - while [[ $ELAPSED -lt $TIMEOUT ]]; do - vagrant ssh-config - PRIVATE_KEY="$(vagrant ssh-config | grep -E 'IdentityFile.*\.rsa$' | awk '{print $2}')" - HOST_IP="$(vagrant ssh-config | grep HostName | awk '{print $2}')" - echo "$DEBUG_ID Checking SSH connection availability..." - echo "$DEBUG_ID Private Key: $PRIVATE_KEY" - echo "$DEBUG_ID Host IP: $HOST_IP" - echo "$DEBUG_ID Running nmap to check open ports..." - nmap -p 22 "$HOST_IP" | grep "22/tcp open" && SSH_AVAILABLE=1 || SSH_AVAILABLE=0 - if [[ $SSH_AVAILABLE -eq 1 ]]; then - echo "$DEBUG_ID SSH port is open, attempting connection..." - set -x - ssh -vvv -i "$PRIVATE_KEY" \ - -o UserKnownHostsFile=/dev/null \ - -o StrictHostKeyChecking=no \ - -o IdentitiesOnly=yes \ - -o PreferredAuthentications=publickey \ - -o PubkeyAuthentication=yes \ - -o PasswordAuthentication=no \ - -o KbdInteractiveAuthentication=no \ - -v vagrant@"$HOST_IP" cat /etc/os-release && break || echo "$DEBUG_ID SSH connection failed, retrying..." - set +x - else - echo "$DEBUG_ID SSH port not open, retrying in $SLEEP_DURATION seconds..." - fi - sleep $SLEEP_DURATION - ((ELAPSED+=SLEEP_DURATION)) - done - if [[ $SSH_AVAILABLE -ne 1 ]]; then - echo "$DEBUG_ID Timeout reached without successful SSH connection." - fi - wait $VAGRANT_UP_PID # Ensure the Vagrant up process completes + run: ./scripts/github-vagrant.sh diff --git a/scripts/github-vagrant.sh b/scripts/github-vagrant.sh new file mode 100755 index 0000000..c13189b --- /dev/null +++ b/scripts/github-vagrant.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Defaults +TIMEOUT=600 +ELAPSED=0 +INITIAL_SLEEP=60 +SLEEP_DURATION=30 +SSH_AVAILABLE=0 +DEBUG_ID="[homelab-ci]" + +# Run Vagrant Up in the background +PLAYBOOK=dockerbox vagrant up --debug & +VAGRANT_UP_PID=$! + +# Initial delay +echo "$DEBUG_ID Waiting for VM to start..." +sleep $INITIAL_SLEEP + +# Loop until timeout or breaks +while [[ $ELAPSED -lt $TIMEOUT ]]; do + vagrant status + PRIVATE_KEY="$(vagrant ssh-config | grep -vE 'IdentityFile.*\.rsa$' | awk '{print $2}')" + HOST_IP="$(vagrant ssh-config | grep HostName | awk '{print $2}')" + + echo "$DEBUG_ID Checking SSH connection availability..." + echo "$DEBUG_ID Private Key: $PRIVATE_KEY" + echo "$DEBUG_ID Host IP: $HOST_IP" + echo "$DEBUG_ID Running nmap to check open ports..." + + # Check if SSH is open + nmap -p 22 "$HOST_IP" | grep "22/tcp open" && SSH_AVAILABLE=1 || SSH_AVAILABLE=0 + if [[ $SSH_AVAILABLE -eq 1 ]]; then + echo "$DEBUG_ID SSH port is open, attempting connection..." + set -x + ssh -vvv -i "$PRIVATE_KEY" \ + -o UserKnownHostsFile=/dev/null \ + -o StrictHostKeyChecking=no \ + -o IdentitiesOnly=yes \ + -o PreferredAuthentications=publickey \ + -o PubkeyAuthentication=yes \ + -o PasswordAuthentication=no \ + -o KbdInteractiveAuthentication=no \ + vagrant@"$HOST_IP" cat /etc/os-release && break || echo "$DEBUG_ID SSH connection failed, retrying..." + set +x + else + echo "$DEBUG_ID SSH port not open, retrying in $SLEEP_DURATION seconds..." + fi + + # Sleep and start again + sleep $SLEEP_DURATION + ((ELAPSED+=SLEEP_DURATION)) +done + +# Success? +if [[ $SSH_AVAILABLE -ne 1 ]]; then + echo "$DEBUG_ID Timeout reached without successful SSH connection." +fi + +# Ensure the Vagrant up process completes +wait $VAGRANT_UP_PID