From e844aec85bdf2675dcf62bf7768fb9d1b6f51f48 Mon Sep 17 00:00:00 2001 From: Conor Finn Date: Wed, 8 Feb 2023 16:21:20 +0000 Subject: [PATCH] Add a test run of the slave script to the tasks RE #62 --- .../roles/agent/tasks/check-connection.sh | 40 +++++++++++++++++++ .../ansible/roles/agent/tasks/main.yml | 38 ++++++++++-------- 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/check-connection.sh diff --git a/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/check-connection.sh b/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/check-connection.sh new file mode 100644 index 0000000..2b1e4ca --- /dev/null +++ b/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/check-connection.sh @@ -0,0 +1,40 @@ +#! /bin/sh + +AGENT_NAME=${1} +AGENT_SECRET=${2} + + +echo "Check if the secret or name has changed and kill the current java process if it has. " + +cron_entry=$(crontab -l | grep jenkins-slave.sh) +cron_name_and_secret=$(echo "$cron_entry" | grep -o "$AGENT_NAME .*") + +if [[ "$AGENT_NAME $AGENT_SECRET" != "$cron_name_and_secret" ]]; then + pgrep java | xargs kill -9 +fi + + +echo "Run the agent startup script in the background. " + +$HOME/jenkins-slave.sh $AGENT_NAME $AGENT_SECRET & + + +echo "Wait for the script to get to its hang point. " + +sleep 5 + + +echo "Check that the script has connected the agent to the controller. " + +jenkins_json=$(curl https://builds.mantidproject.org/manage/computer/$AGENT_NAME/api/json) +is_offline=$(echo "$jenkins_json" | grep \"icon\":\"symbol-computer-offline\") + +if [[ $is_offline ]]; then + echo "Agent failed to connect to Jenkins controller. " + exit 1 +fi + + +echo "Agent connected successfully. " + +exit 0 \ No newline at end of file diff --git a/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/main.yml b/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/main.yml index 5b85687..0a82476 100644 --- a/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/main.yml +++ b/jenkins-node/mantid-builder-macos/ansible/roles/agent/tasks/main.yml @@ -3,7 +3,7 @@ # Install Requirements -- name: Add user to sudoers on new macs +- name: Add user to sudoers on new macs. shell: /Applications/Privileges.app/Contents/Resources/PrivilegesCLI --add ignore_errors: true # Not all the macs have these, so don't panic if it fails. @@ -11,7 +11,7 @@ include_role: name: geerlingguy.mac.homebrew -- name: Make sure homebrew bin is in the path +- name: Make sure homebrew bin is in the path. ansible.builtin.lineinfile: path: /etc/paths state: present @@ -19,37 +19,43 @@ become: true become_user: root -- name: Install git +- name: Install git. community.general.homebrew: name: git state: latest -- name: Install Java 11 +- name: Install Java 11. community.general.homebrew: name: java11 state: present -# Configure macOS Settings +# Configure macOS Settings. -- name: Disable screensaver +- name: Disable screensaver. shell: defaults write com.apple.screensaver idleTime 0 -- name: Disable saved application states to avoid dialog +- name: Disable saved application states to avoid dialog. shell: defaults write org.python.python NSQuitAlwaysKeepsWindows -bool false - -# TODO: Disable autolock (this seems to change between versions, so might not be possible to script) - -- name: Download jenkins slave script - shell: curl -o ~/jenkins-slave.sh https://raw.githubusercontent.com/mantidproject/mantid/main/buildconfig/Jenkins/jenkins-slave.sh -- name: Start script as chrontab entry +# Test and start the agent. Note: Connection will only begin consistently every 5th minute if changes are made. + +- name: Download jenkins slave script. + shell: curl -o $HOME/jenkins-slave.sh https://raw.githubusercontent.com/mantidproject/mantid/main/buildconfig/Jenkins/jenkins-slave.sh + +- name: Make the slave script executable. + shell: chmod 777 $HOME/jenkins-slave.sh + +- name: Check the Jenkins agent connection script. + script: ./check-connection.sh {{ agent_name }} {{ agent_secret }} + +- name: Setup a chrontab entry to run the agent script every 5th minute. ansible.builtin.cron: name: "Run slave script" minute: "*/5" job: "$HOME/jenkins-slave.sh {{ agent_name }} {{ agent_secret }}" -- name: Remove user from sudoers on new macs +# Tidy up the evironment. + +- name: Remove user from sudoers on new macs. shell: /Applications/Privileges.app/Contents/Resources/PrivilegesCLI --remove ignore_errors: true # Not all the macs have these, so don't panic if it fails. - -