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']['executor'] = 4
j-rivero marked this conversation as resolved.
Show resolved Hide resolved
# TODO tags
default['osrf_buildfarm']['agent']['labels'] = %w(docker)

75 changes: 63 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
wget
Expand All @@ -39,9 +44,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 All @@ -63,28 +68,74 @@
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
not_if { node.chef_environment == "test" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier versions of the swarm client would start and not exit on a connection failure.
By running the agent with a bogus or unreachable jenkins server your tests will see errors in the config that lead to startup failure. So you may want to reconsider this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you here. What you see wrong in the code about avoiding the jenkins-agent service in the case of running test environment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The not_if here won't try to start the jenkins-agent service in test environments. But I think it should start it in all cases eve if you can't connect to the server.

Suggested change
not_if { node.chef_environment == "test" }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try, I think that it is going to make CI to fail since the agent connection will return an error code from provisioning making the provision to stop there. Let's see if that is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if that is correct.

Failing now after applying the change.


  * service[jenkins-agent] action restart  * service[jenkins-agent] action restart
    
    ================================================================================
    Error executing action `restart` on resource 'service[jenkins-agent]'
    ================================================================================
    
    Mixlib::ShellOut::ShellCommandFailed
    ------------------------------------
    Expected process to exit with [0], but received '1'
    ---- Begin output of /usr/bin/systemctl --system restart jenkins-agent ----
    STDOUT: 
    STDERR: Job for jenkins-agent.service failed because the control process exited with error code.
    See "systemctl status jenkins-agent.service" and "journalctl -xe" for details.
    ---- End output of /usr/bin/systemctl --system restart jenkins-agent ----
    Ran /usr/bin/systemctl --system restart jenkins-agent returned 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to revert the change to make CI happy. Ticketed in #7 in the case we find time to debug and solve it.

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