[Fuego] [PATCH v2] ftc: implement add-nodes and rm-nodes using python-jenkins

Daniel Sangorrin daniel.sangorrin at toshiba.co.jp
Fri Mar 24 08:04:06 UTC 2017


Note that you can now add/remove multiple nodes at the same
time, or remove all nodes at once. This was not available
in the fuego-create-node and fuego-delete-node scripts.

Signed-off-by: Daniel Sangorrin <daniel.sangorrin at toshiba.co.jp>
---
 engine/scripts/ftc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/engine/scripts/ftc b/engine/scripts/ftc
index e064016..6c4a500 100755
--- a/engine/scripts/ftc
+++ b/engine/scripts/ftc
@@ -55,6 +55,7 @@ import getopt
 import shutil
 import tempfile
 import yaml
+import jenkins
 
 # MAJOR, MINOR, REVISION
 VERSION = (1,0,9)
@@ -71,6 +72,7 @@ def pvar(name):
 quiet = 0
 verbose = 0
 use_statusouput = 1
+server = jenkins.Jenkins('http://localhost:8080/fuego')
 
 # keep configuration file in /fuego-ro/conf area
 config_dir = "/fuego-ro/conf"
@@ -82,6 +84,20 @@ TARGET_ENV_VAR="FTC_TARGET"
 
 # format for command_help mapping with: key=name, value=(summary, long description)
 command_help = {
+"rm-nodes":("Removes nodes from Jenkins.",
+    """Usage: ftc rm-nodes [<target1> <target2> ...]
+  Use list-nodes to see the existing nodes.
+
+  If no target is provided all existing nodes will be removed."""),
+
+"add-nodes":("Adds new nodes to Jenkins.",
+    """Usage: ftc add-nodes [-f] <target1> <target2> ...
+  By convention the name of the node and target board is the same. Also,
+  the corresponding board file (<target>.board) must exist. Use list-targets
+  to see available target boards.
+
+  Use -f for "force" mode. This tries to remove the node before adding it"""),
+
 "list-targets":("Show a list of available target boards.",
     """Usage: ftc list-targets [-q]
   Prints target board names and summary information, if any.
@@ -831,6 +847,62 @@ def get_includes(include_filename, conf):
 	inc_vars = parse_shell_file(inc_path, conf)
 	return inc_vars
 
+def do_add_nodes(conf, options):
+	global server
+
+	if '-f' in options:
+		force = True
+		options.remove('-f')
+	else:
+		force = False
+
+	target_list = get_fuego_targets(conf).keys()
+	for board in options:
+		if board not in target_list:
+			raise Exception('No %s.board found.' % board)
+
+	params = { 'command' : 'java -jar /fuego-core/engine/slave.jar'}
+	for board in options:
+		nodes = [node['name'] for node in server.get_nodes()]
+		if board in nodes:
+			if force:
+				server.delete_node(board)
+			else:
+				raise Exception('Node \'%s\' already exists' % board)
+
+		server.create_node(
+			board,
+			numExecutors = 1,
+			launcher = jenkins.LAUNCHER_COMMAND,
+			launcher_params = params
+		)
+		# bug?: it seems enable_node requires two calls
+		server.enable_node(board)
+		server.enable_node(board)
+	sys.exit(0)
+
+def do_rm_nodes(conf, options):
+	global server
+
+	nodes = [node['name'] for node in server.get_nodes()]
+	if not options:
+		options = nodes
+	else:
+		for board in options:
+			if board not in nodes:
+				raise Exception('Node \'%s\' not found.' % board)
+
+	for board in options:
+		nodes = [node['name'] for node in server.get_nodes()]
+		if board not in nodes:
+			# in case the same node is repeated in options
+			continue
+		if board == 'master':
+			# the master node is special in jenkins
+			continue
+		server.delete_node(board)
+	sys.exit(0)
+
 def do_list_nodes(conf):
 	global quiet, verbose
 
@@ -2525,6 +2597,7 @@ def main():
     global verbose
     global quiet
     global use_statusoutput
+    global server
 
     if len(sys.argv)<2:
         error_out('Missing command\nUse "ftc help" to get usage help.', 1)
@@ -2586,6 +2659,20 @@ def main():
     # read config
     conf = config_class(config_dir + os.sep + CONFIG_FILE)
 
+    if command=="add-nodes":
+        # adds a Jenkins node
+        try:
+            do_add_nodes(conf, options)
+        except Exception as e:
+            sys.exit(str(e) + '\n' + command_help['add-nodes'][1])
+
+    if command=="rm-nodes":
+        # removes a Jenkins node
+        try:
+            do_rm_nodes(conf, options)
+        except Exception as e:
+            sys.exit(str(e) + '\n' + command_help['rm-nodes'][1])
+
     if command=="list-targets":
         # shows fuego targets (boards)
         do_list_targets(conf)
-- 
2.7.4




More information about the Fuego mailing list