azure-vm-agents-plugin icon indicating copy to clipboard operation
azure-vm-agents-plugin copied to clipboard

Unable to use the new inbound agent mode with Linux

Open dduportal opened this issue 2 years ago • 4 comments

Hello đź‘‹ I've failed to use this new mode instead of SSH.

The goal is to run ci.jenkins.io agents as inbound agent so the agents does not require to be reachable by the controller.

What did I do?

  • I've set up the launch method to "connect agent to controller" (https://github.com/jenkinsci/azure-vm-agents-plugin/pull/406 and https://github.com/jenkinsci/azure-vm-agents-plugin/pull/418)
  • Jenkins creates the agent and wait for connections
  • Started a build: the VM was created in Azure (and I can SSH to it as non privileged jenkins user with the default keys we have)
  • But the agent never connects to Jenkins.

I see that there are two files in the documentation that could be related to the feature:

  • https://github.com/jenkinsci/azure-vm-agents-plugin/blob/master/docs/init-scripts/linux-inbound-agent.sh
  • https://github.com/jenkinsci/azure-vm-agents-plugin/blob/master/docs/init-scripts/systemd-unit.service

But it does not seem these files are used by the plugin? Are they "examples" to be set in the "init script" field ?

If yes, then I have 2 questions:

  • How is Jenkins able to execute these scripts on the VM: is it using cloud-init?
  • The script seems to expect parameters (https://github.com/jenkinsci/azure-vm-agents-plugin/blob/8d35f0960a4358a23e304964ad1378f2e4bd3ded/docs/init-scripts/linux-inbound-agent.sh#L3-L5) : what is the syntax to pass these parameters in the Jenkins UI configuration?

dduportal avatar May 11 '23 16:05 dduportal

A minimal init script assuming Java is pre-installed to the VM would be (a trimmed down version of the example as the example installs Java, git and maven):

#!/usr/bin/env bash

JENKINS_URL=$1
AGENT_NAME=$2
SECRET=$3

# Update if your user is called something different
export USER=jenkins

mkdir -p /home/$USER/inbound-agent
chown $USER:$USER /home/$USER/inbound-agent

(
  cd /home/$USER/inbound-agent || exit
  curl -O "$JENKINS_URL/jnlpJars/agent.jar"
  echo "${SECRET}" > agent-secret

  curl -O https://raw.githubusercontent.com/jenkinsci/azure-vm-agents-plugin/HEAD/docs/init-scripts/systemd-unit.service
  export AGENT_URL="$JENKINS_URL/computer/$AGENT_NAME/jenkins-agent.jnlp"
  envsubst < systemd-unit.service > /etc/systemd/system/jenkins-agent.service
  rm -f systemd-unit.service

  sudo systemctl daemon-reload
  sudo systemctl enable jenkins-agent
  sudo systemctl start jenkins-agent || sudo systemctl status jenkins-agent
) |& tee /home/$USER/inbound-agent/init-script.log

But it does not seem these files are used by the plugin? Are they "examples" to be set in the "init script" field ?

Yes, should be explained in the help field for the launcher.

How is Jenkins able to execute these scripts on the VM: is it using cloud-init?

No it uses a VM extension called custom script which can execute a script that has been uploaded to blob storage (done by the plugin automatically).

https://github.com/jenkinsci/azure-vm-agents-plugin/blob/8d35f0960a4358a23e304964ad1378f2e4bd3ded/src/main/resources/customImageTemplateWithScriptAndManagedDisk.json#L171-L172

what is the syntax to pass these parameters in the Jenkins UI configuration?

$1, $2, $3 ?

The plugin will pass those variables to your script, see: https://github.com/jenkinsci/azure-vm-agents-plugin/blob/8d35f0960a4358a23e304964ad1378f2e4bd3ded/src/main/resources/customImageTemplateWithScriptAndManagedDisk.json#L180

timja avatar May 11 '23 21:05 timja

Thanks! I was able to spin up inbound agents on ci.jenkins.io with a test template.

I'm pretty impressed by the speed at which the agent comes online (compared to SSH launcher).

Next steps:

  • Switch ci.jenkins.io whole to inbound launcher (to allows changing network area)
  • Open a documentation PR in this repo to help other users

dduportal avatar May 13 '23 12:05 dduportal

Btw you can use SSH keys now to authenticate if you want to switch to that at the same time

Wouldn’t be used by the controller but if you ever need to login to them…

timja avatar May 13 '23 13:05 timja

Btw you can use SSH keys now to authenticate if you want to switch to that at the same time

Wouldn’t be used by the controller but if you ever need to login to them…

Yep, thanks for the reminder. I tested this one yesterday and it works very well!

Nice job !

dduportal avatar May 13 '23 14:05 dduportal