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

Implement agent installation #4

Merged
merged 9 commits into from
Jan 21, 2021
11 changes: 10 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
default['osrf_jenkins_agent']['jenkins_url'] = "https://build.osrfoundation.org"
default['osrf_buildfarm']['agent']['jenkins_url'] = "https://build.osrfoundation.org"
default['osrf_buildfarm']['agent']['agent_username'] = 'jenkins'
default['osrf_buildfarm']['agent']['java_args'] = ''
default['osrf_buildfarm']['agent']['username'] = 'admin'
default['osrf_buildfarm']['agent']['nodename'] = 'agent'
default['osrf_buildfarm']['agent']['description'] = 'build agent'
default['osrf_buildfarm']['agent']['executors'] = 1
# TODO tags
default['osrf_buildfarm']['agent']['labels'] = %w(docker)

74 changes: 62 additions & 12 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#
# Copyright:: 2020, Open Source Robotics Foundation.
#

agent_username = node['osrf_buildfarm']['agent']['agent_username']
agent_homedir = "/home/#{agent_username}"

apt_update "default" do
action :periodic
frequency 3600
Expand All @@ -18,6 +22,7 @@
libssl-dev
mercurial
ntp
openjdk-8-jdk-headless
qemu-user-static
sudo
x11-xserver-utils
Expand Down Expand Up @@ -53,9 +58,9 @@
block do
lightdm_conf = Chef::Util::FileEdit.new("/etc/lightdm/lightdm.conf")
lightdm_conf.search_file_replace_line %r{^display-setup-script=.*},
"display-setup-script=/etc/lightdm/xhost.conf"
"display-setup-script=/etc/lightdm/xhost.sh"
lightdm_conf.insert_line_if_no_match %r{^display-setup-script=.*},
"display-setup-script=/etc/lightdm/xhost.conf"
"display-setup-script=/etc/lightdm/xhost.sh"
lightdm_conf.write_file if lightdm_conf.unwritten_changes?
end
end
Expand Down Expand Up @@ -92,28 +97,73 @@
action [:start, :enable]
end

user "jenkins" do
user agent_username do
shell "/bin/bash"
manage_home true
end
sudo "jenkins" do
user "jenkins"
sudo agent_username do
user agent_username
nopasswd true
end

# Add agent user to the docker group to allow them to build and run docker
# containers.
group 'docker' do
append true
members 'jenkins'
members agent_username
action :manage # Group should be created by docker package.
end

directory "/home/jenkins/jenkins-agent"
agent_jar_url = node["osrf_jenkins_agent"]["agent_jar_url"]
if agent_jar_url.nil? || agent_jar_url.empty?
agent_jar_url = "#{node["osrf_jenkins_agent"]["jenkins_url"]}/jnlpJars/agent.jar"

# TODO: how to read attributes from chef-osrf plugins into this cookbook
# swarm_client_version = node['jenkins-plugins']['swarm']
nuclearsandwich marked this conversation as resolved.
Show resolved Hide resolved
swarm_client_version = "3.24"
swarm_client_url = "https://repo.jenkins-ci.org/releases/org/jenkins-ci/plugins/swarm-client/#{swarm_client_version}/swarm-client-#{swarm_client_version}.jar"
swarm_client_jarfile_path = "/home/#{agent_username}/swarm-client-#{swarm_client_version}.jar"

# Download swarm client program from url and install it to the jenkins-agent user's home directory.
remote_file swarm_client_jarfile_path do
source swarm_client_url
owner agent_username
group agent_username
mode '0444'
end

jenkins_username = node['osrf_buildfarm']['agent']['username']
agent_jenkins_user = search('osrf_buildfarm_jenkins_users', "username:#{jenkins_username}").first
template '/etc/default/jenkins-agent' do
source 'jenkins-agent.env.erb'
variables Hash[
java_args: node['osrf_buildfarm']['agent']['java_args'],
jarfile: swarm_client_jarfile_path,
jenkins_url: node['osrf_buildfarm']['jenkins_url'],
username: jenkins_username,
password: agent_jenkins_user['password'],
name: node['osrf_buildfarm']['agent']['nodename'],
description: node['osrf_buildfarm']['agent']['description'],
executors: node['osrf_buildfarm']['agent']['executors'],
user_home: agent_homedir,
labels: node['osrf_buildfarm']['agent']['labels'],
]
notifies :restart, 'service[jenkins-agent]'
end
remote_file "/home/jenkins/jenkins-agent/agent.jar" do
source agent_jar_url

template '/etc/systemd/system/jenkins-agent.service' do
source 'jenkins-agent.service.erb'
variables Hash[
service_name: 'jenkins-agent',
username: agent_username,
]
notifies :run, 'execute[systemctl-daemon-reload]', :immediately
notifies :restart, 'service[jenkins-agent]'
end

execute 'systemctl-daemon-reload' do
command 'systemctl daemon-reload'
action :nothing
end

service 'jenkins-agent' do
action [:start, :enable]
# can not connect to server while testing
end
17 changes: 17 additions & 0 deletions templates/jenkins-agent.env.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
JAVA_ARGS=<%= @java_args %>

SWARM_CLIENT_JAR='<%= @jarfile %>'

JENKINS_URL='<%= @jenkins_url %>'

USERNAME='<%= @username %>'
PASSWORD='<%= @password %>'

NAME='<%= @name %>'
DESCRIPTION='<%= @description %>'
MODE=exclusive
EXECUTORS=<%= @executors %>
FSROOT='<%= @user_home %>'
LABELS='<%= @labels.join ' '%>'

JENKINS_ARGS=''
17 changes: 17 additions & 0 deletions templates/jenkins-agent.service.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=ROS build farm jenkins agent
j-rivero marked this conversation as resolved.
Show resolved Hide resolved
After=network.target

[Service]
EnvironmentFile=/etc/default/<%= @service_name %>
Type=simple
ExecStart=/bin/sh -c "/usr/bin/java $JAVA_ARGS -jar $SWARM_CLIENT_JAR \
-master $JENKINS_URL -username $USERNAME -password \"$PASSWORD\" \
-name $NAME -description \"$DESCRIPTION\" -mode $MODE -executors $EXECUTORS \
-fsroot $FSROOT -labels \"$LABELS\" $JENKINS_ARGS"

User=<%= @username %>
Restart=always

[Install]
WantedBy=multi-user.target