Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Automate migration of local agents to remote agents

Starting from Bamboo 9.6, Local agents are deprecated, and you have to migrate your local agents, as mentioned in the following article.

 

Script Explanation

I used here a Python script that would do the following:

  1. Request some information for accessing the server, like username, password, and Bamboo version.

  2. Get the number of the Local agent on Bamboo server

  3. After getting the number of the Local agents, the script will ask the user for the IPs of the remote agents to automate the installation

  4. The script will ask about the authentication method used for the remote IPs that will be the remote agent. You can use username/password or SSH keys

  5. After gathering all this information, the script will start automating the agent installation.

 

import paramiko
import getpass
import requests
import time

def fetch_local_agents_count(baseurl, username, password):
headers = {'Accept': 'application/json'}
auth = (username, password)
BASEURL = f'{baseurl}/rest/api/latest/agent'

response = requests.get(BASEURL, headers=headers, auth=auth)
if response.status_code == 200:
agents = response.json()
local_agents_count = sum(1 for agent in agents if agent['type'] == 'LOCAL')
return local_agents_count
else:
print(f"Failed to fetch agents. Status code: {response.status_code}, Reason: {response.reason}")
return 0

def check_log_for_completion(ssh_client, log_file_path, completion_text, timeout=300):
start_time = time.time()
while True:
if time.time() - start_time > timeout:
print("Timeout reached while waiting for the agent to be ready.")
return False

try:
cmd = f"cat {log_file_path}"
stdin, stdout, stderr = ssh_client.exec_command(cmd)
log_content = stdout.read().decode("utf-8")

if completion_text in log_content:
print("Found the completion text in the log file. Agent installation is completed.")
return True
else:
time.sleep(10)
except Exception as e:
print(f"An error occurred while checking the log file: {e}")
return False

def get_ssh_connection(ip, username, password=None, ssh_key_filepath=None):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
if ssh_key_filepath:
private_key = paramiko.RSAKey.from_private_key_file(ssh_key_filepath)
ssh_client.connect(hostname=ip, username=username, pkey=private_key)
else:
ssh_client.connect(hostname=ip, username=username, password=password)
except Exception as e:
print(f"Failed to connect: {e}")
return None
return ssh_client

def run_java_command(ssh_client, jar_dest, bamboo_server, log_file_path, agent_home_path):
try:
run_cmd = f"nohup java -Dbamboo.home={agent_home_path} -jar {jar_dest} https://{bamboo_server}/agentServer/ > {log_file_path} 2>&1 &"
stdin, stdout, stderr = ssh_client.exec_command(run_cmd)
print("Java process started, monitoring the log file for completion status...")
completion_text = "ready to receive builds"
return check_log_for_completion(ssh_client, log_file_path, completion_text)
except Exception as e:
print(f"An error occurred: {e}")
return False

def main():
baseurl = input("Enter the Bamboo base URL: ")
bamboo_version = input("Enter your Bamboo version (e.g., 9.2.4): ")
admin_username = input("Enter your Bamboo admin username: ")
admin_password = getpass.getpass("Enter your Bamboo admin password: ")
server_username = input("Enter your remote agent server username: ")
num_local_agents = fetch_local_agents_count(baseurl, admin_username, admin_password)
print(f"You have {num_local_agents} local agents. Please enter their IPs.")

if not bamboo_version:
print("No Bamboo version provided, exiting.")
return

jar_url = f"{baseurl}/agentServer/agentInstaller/atlassian-bamboo-agent-installer-{bamboo_version}.jar"
jar_dest = f"/tmp/atlassian-bamboo-agent-installer-{bamboo_version}.jar"
bamboo_server = baseurl.replace("http://", "").replace("https://", "")
log_file_path = "/tmp/bamboo_agent_install.log"

auth_method = input("Enter authentication method (password/ssh-key): ").strip().lower()
if auth_method == 'password':
password = getpass.getpass("Enter the SSH password for the remote server: ")
elif auth_method == 'ssh-key':
ssh_key_filepath = input("Enter the SSH key filepath: ")
else:
print("Invalid authentication method selected.")
return

for i in range(num_local_agents):
ip = input(f"Enter IP for agent {i+1}: ")
agent_home_path = input(f"Enter agent_home_path: ")
print(f"Connecting to {ip} to install the Bamboo agent...")
ssh_client = get_ssh_connection(ip, server_username, password, ssh_key_filepath)
if ssh_client:
if run_java_command(ssh_client, jar_dest, bamboo_server, log_file_path, agent_home_path):
print("Agent installation command executed successfully. Check the agent server for status.")
else:
print(f"Failed to confirm agent installation on {ip}.")
ssh_client.close()
else:
print(f"Failed to establish SSH connection to {ip}.")

if __name__ == "__main__":
main()

 

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events