Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Java Agent launching script for Jenkins connections #2762

Merged
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9be62c3
hardened lauch_java_agent
TerryMcGuinness-NOAA Jul 12, 2024
303275f
put curl api call into a function
Jul 12, 2024
4035b70
added feature to wait five minutes after it has been determined that …
TerryMcGuinness-NOAA Jul 12, 2024
eec3369
added headher to launch script
Jul 12, 2024
a740da1
added shell norm suggestions
Jul 12, 2024
ea85144
added shell norm for set e
Jul 12, 2024
240ed9c
added shell suggestion for shopt -s inherit_errexit
Jul 12, 2024
f548c30
fixed syntax error in rm arg and replaced -p with -f
Jul 12, 2024
3e6b671
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 15, 2024
773b07b
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 15, 2024
b36510b
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 15, 2024
d9e165f
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 15, 2024
bd4ca78
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 15, 2024
41b5af9
updated comprehensive documentation
TerryMcGuinness-NOAA Jul 15, 2024
68b67de
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 15, 2024
fc9826b
nitpicks for PR review
TerryMcGuinness-NOAA Jul 15, 2024
b243b0d
added two optional swithces to agument the now intention with force
TerryMcGuinness-NOAA Jul 15, 2024
e4651ce
addeing get plugins test
TerryMcGuinness-NOAA Jul 16, 2024
26792fe
remove get_plugins (do not have admin permissions on controler)
TerryMcGuinness-NOAA Jul 16, 2024
0d3f659
simplified force/now logic in launch script
TerryMcGuinness-NOAA Jul 16, 2024
f82ccf7
Merge branch 'harden-launch_java_agent' of github.com:TerrenceMcGuinn…
TerryMcGuinness-NOAA Jul 16, 2024
6e412f3
simplified now/force logic and add usage checks
TerryMcGuinness-NOAA Jul 16, 2024
a8d6b29
added -d on command line for checking on line function for shellnorms
TerryMcGuinness-NOAA Jul 16, 2024
ce117b5
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 17, 2024
74940b1
Merge branch 'NOAA-EMC:develop' into harden-launch_java_agent
TerrenceMcGuinness-NOAA Jul 17, 2024
308a738
use getopts
TerryMcGuinness-NOAA Jul 17, 2024
ee06c4b
reverted to getops and single char switches
TerryMcGuinness-NOAA Jul 17, 2024
377cfad
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 17, 2024
d2e7efe
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Jul 18, 2024
a242f2c
Update ci/scripts/utils/launch_java_agent.sh
TerrenceMcGuinness-NOAA Aug 2, 2024
677245b
added url to gh 2.251
TerryMcGuinness-NOAA Aug 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 91 additions & 12 deletions ci/scripts/utils/launch_java_agent.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
#!/bin/env bash

# ==============================================================================
# Script Name: launch_java_agent.sh
#
# Description:
# This script automates the process of launching a Jenkins agent
# on a specified machine. It ensures that the necessary
# prerequisites are met, such as the availability of JAVA_HOME,
# the Jenkins agent launch directory, and proper authentication
# with GitHub.
#
# It then proceeds to check if the Jenkins node is online and
# decides whether to launch the Jenkins agent based on the node's
# status. The agent is launched in the background,
# and its PID is logged for reference.
#
# Prerequisites:
# JAVA_HOME must be set to a valid JDK installation.
# Jenkins agent launch directory must exist and be specified.
# GitHub CLI (gh) must be installed and authenticated for messeging
# from the Jenkins controller to GitHub PR via shell commands.
# Jenkins agent launch directory must exist and be specified.
# TODO: Must use GitHub CLI v2.25.1 (newer versoins have issues)
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
# Jenkins controller URL and authentication token must be provided.
# jenkins-secret-file:
# Must be present in the Jenkins agent launch directory.
# This file contains the secret key for the Jenkins agent
# established by the Jenkins administrator for each Node.
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
# jenkins_token:
# Must be present in the Jenkins agent launch directory.
# This file contains the user authentication token for the Jenkins controller
# to use the Remote API. This token can be generated by the user
# on the Jenkins controller.
# controller_user:
# Must be set to the Jenkins controller username corresponing to the jenkins_token.
#
# Usage: ./launch_java_agent.sh [-f]
# The optional '-f' argument forces the script to launch the Jenkins
# agent without waiting and trying again.
#
# ==============================================================================

set -e
shopt -s inherit_errexit

controller_url="https://jenkins.epic.oarcloud.noaa.gov"
controller_user="terry.mcguinness"
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
controller_user_auth_token="jenkins_token"

HOMEgfs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." >/dev/null 2>&1 && pwd )"
host=$(hostname)

Expand Down Expand Up @@ -46,6 +91,13 @@ export GH="${HOME}/bin/gh"
command -v "${GH}"
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
${GH} --version

check_mark=$(gh auth status -t 2>&1 | grep "Token:" | awk '{print $1}') || true
if [[ "${check_mark}" != "✓" ]]; then
echo "gh not authenticating with emcbot token"
exit 1
fi
echo "gh authenticating with emcbot TOKEN ok"

if [[ -d "${JENKINS_AGENT_LANUCH_DIR}" ]]; then
echo "Jenkins Agent Lanuch Directory: ${JENKINS_AGENT_LANUCH_DIR}"
else
Expand All @@ -56,22 +108,49 @@ cd "${JENKINS_AGENT_LANUCH_DIR}"

if ! [[ -f agent.jar ]]; then
curl -sO "${controller_url}/jnlpJars/agent.jar"
echo "Updated agent.jar downloaded"
fi

JENKINS_TOKEN=$(cat jenkins_token)
if [[ ! -f "${controller_user_auth_token}" ]]; then
echo "User Jenkins authetication TOKEN to the controller for using the Remote API does not exist"
exit 1
fi

JENKINS_TOKEN=$(cat "${controller_user_auth_token}")
echo -e "#!/usr/bin/env python
import json,sys
with open(sys.argv[1], 'r') as file:
data = json.load(file)
print(data.get('offline','True'))
" > parse.py
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
chmod u+x parse.py

check_node_online() {
rm -f curl_response
curl_response=$(curl --silent -u "${controller_user}:${JENKINS_TOKEN}" "${controller_url}/computer/${MACHINE_ID^}-EMC/api/json?pretty=true") || true
echo -n "${curl_response}" > curl_response
./parse.py curl_response
}

#
offline=$(curl --silent -u "${controller_user}:${JENKINS_TOKEN}" "${controller_url}/computer/${MACHINE_ID^}-EMC/api/json?pretty=true" | grep '\"offline\"' | awk '{gsub(/,/,"");print $3}') || true
echo "Jenkins Agent offline setting: ${offline}"
offline=$(set -e; check_node_online)

if [[ "${offline}" == "true" ]]; then
echo "Jenkins Agent is offline. Lanuching Jenkins Agent on ${host}"
command="nohup ${JAVA} -jar agent.jar -jnlpUrl ${controller_url}/computer/${MACHINE_ID^}-EMC/jenkins-agent.jnlp -secret @jenkins-secret-file -workDir ${JENKINS_WORK_DIR}"
echo -e "Lanuching Jenkins Agent on ${host} with the command:\n${command}" >& "${LOG}"
${command} >> "${LOG}" 2>&1 &
nohup_PID=$!
echo "Java agent running on PID: ${nohup_PID}" >> "${LOG}" 2>&1
echo "Java agent running on PID: ${nohup_PID}"
if [[ "${offline}" != "False" ]]; then
if [[ "${1:-}" != "-f" ]]; then
echo "Jenkins Agent is offline. Waiting 5 more minutes to check again in the event it is a temp network issue"
sleep 300
fi
TerrenceMcGuinness-NOAA marked this conversation as resolved.
Show resolved Hide resolved
offline=$(check_node_online)
if [[ "${offline}" != "False" ]]; then
echo "Jenkins Agent is offline. Launching Jenkins Agent on ${host}"
command="nohup ${JAVA} -jar agent.jar -jnlpUrl ${controller_url}/computer/${MACHINE_ID^}-EMC/jenkins-agent.jnlp -secret @jenkins-secret-file -workDir ${JENKINS_WORK_DIR}"
echo -e "Launching Jenkins Agent on ${host} with the command:\n${command}" >& "${LOG}"
${command} >> "${LOG}" 2>&1 &
nohup_PID=$!
echo "Java agent running on PID: ${nohup_PID}" >> "${LOG}" 2>&1
echo "Java agent running on PID: ${nohup_PID}"
else
echo "Jenkins Agent is online (nothing done)"
fi
else
echo "Jenkins Agent is online (nothing done)"
fi
Loading