summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorintrigeri <intrigeri@boum.org>2020-02-17 09:11:42 +0000
committerintrigeri <intrigeri@boum.org>2020-02-17 09:18:23 +0000
commit1e67ab8750502c723ee36c9a6fbc98af3843f19d (patch)
tree0ee700cba66e85c889e597a663fd935920b3b4ba
parent189b63bc87e0901dc423061340ae1f09c4db068d (diff)
jenkins-slave.service: Migrate unreadable Python one-liners to an external script (#10601)
This will make this code easier to review, understand, and maintain. It also makes it easier to test it without having to fiddle with 2 layers of quoting and escaping. In passing, avoid introducing more of the awful master/slave terminology; in this case I'm using orchestrator/node but I wonder if orchestrator/worker would be better.
-rwxr-xr-xfiles/jenkins/slaves/jenkins-enable-node31
-rw-r--r--manifests/jenkins/slave.pp10
-rw-r--r--templates/jenkins/jenkins-slave.service.erb5
3 files changed, 43 insertions, 3 deletions
diff --git a/files/jenkins/slaves/jenkins-enable-node b/files/jenkins/slaves/jenkins-enable-node
new file mode 100755
index 0000000..51ea096
--- /dev/null
+++ b/files/jenkins/slaves/jenkins-enable-node
@@ -0,0 +1,31 @@
+#!/usr/bin/python3
+
+import argparse
+import jenkins
+import time
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Ensure a Jenkins node is not marked as temporarily offline"
+ )
+ parser.add_argument("--orchestrator-url",
+ type=str,
+ action="store",
+ required=True)
+ parser.add_argument("--node-name", type=str, action="store", required=True)
+ args = parser.parse_args()
+
+ server = jenkins.Jenkins(args.orchestrator_url)
+ if server.get_node_info(args.node_name)["temporarilyOffline"]:
+ print("Node is marked as temporarily offline, enabling it.")
+ server.enable_node(args.node_name)
+ while server.get_node_info(args.node_name)["temporarilyOffline"]:
+ print("Node is still temporarily offline, waiting…")
+ time.sleep(1)
+ else:
+ print("Node is not marked as temporarily offline, skipping.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/manifests/jenkins/slave.pp b/manifests/jenkins/slave.pp
index 63d911c..270cbf6 100644
--- a/manifests/jenkins/slave.pp
+++ b/manifests/jenkins/slave.pp
@@ -41,6 +41,14 @@ class tails::jenkins::slave (
mode => '0755',
}
+ file { '/usr/local/share/jenkins-enable-node':
+ source => 'puppet:///modules/tails/jenkins/slaves/jenkins-enable-node',
+ owner => 'root',
+ group => 'root',
+ mode => '0755',
+ require => Package['python3-jenkins'],
+ }
+
file { '/etc/tmpfiles.d/tails-jenkins-slave.conf':
ensure => file,
owner => root,
@@ -73,7 +81,6 @@ class tails::jenkins::slave (
owner => 'root',
group => 'root',
mode => '0644',
- require => Package['python3-jenkins'],
}
service { 'jenkins-slave':
@@ -83,6 +90,7 @@ class tails::jenkins::slave (
require => [
File[$unit_file],
File['/usr/local/share/jenkins-slave-download'],
+ File['/usr/local/share/jenkins-enable-node'],
],
}
diff --git a/templates/jenkins/jenkins-slave.service.erb b/templates/jenkins/jenkins-slave.service.erb
index 9a231f6..dfd1201 100644
--- a/templates/jenkins/jenkins-slave.service.erb
+++ b/templates/jenkins/jenkins-slave.service.erb
@@ -4,8 +4,9 @@ Description=Jenkins slave node
[Service]
Type=simple
ExecStartPre=/usr/local/share/jenkins-slave-download <%= @master_url %>
-ExecStartPre=/usr/bin/python3 -c 'import jenkins; server = jenkins.Jenkins("<%= @master_url %>"); node = "<%= @domain == "lizard" ? @hostname : @fqdn %>"; server.enable_node(node) if server.get_node_info(node)["temporarilyOffline"] else True'
-ExecStartPre=/usr/bin/python3 -c 'while __import__("jenkins").Jenkins("<%= @master_url %>").get_node_info("<%= @domain == "lizard" ? @hostname : @fqdn %>")["temporarilyOffline"]: __import__("time").sleep(1)'
+ExecStartPre=/usr/local/share/jenkins-enable-node \
+ --orchestrator-url <%= @master_url %> \
+ --node-name <%= @domain == "lizard" ? @hostname : @fqdn %>
ExecStart=/usr/bin/java -jar /run/jenkins/slave.jar -jnlpUrl <%= @master_url %>/computer/<%= @node_name %>/slave-agent.jnlp
User=jenkins
LimitNOFILE=8192