7 import xml
.etree
.ElementTree
11 def get_argument_parser():
12 parser
= argparse
.ArgumentParser(
13 prog
='update_jenkins_node.py',
14 description
='Create, update, or delete Jenkins nodes'
17 '-u', '--url', default
=None,
18 help='Jenkins server URL including protocol'
21 '--user', default
=None,
22 help='Jenkins username'
25 '--password', default
=None,
26 help='Jenkins password'
29 '-n', '--node', default
=None, required
=True,
30 help='The name of the node to manage in Jenkins'
33 '-c', '--node-config', default
=[], action
='append',
34 help='An equals-separated set path=value[=attrib]. When attrib is not set, text is assumed'
37 '-f', '--config-file', default
=None, type=argparse
.FileType('r'),
38 help='An INI config file as used by jenkins_jobs'
41 '-s', '--state', default
='online',
42 choices
=['online', 'offline', 'absent'],
43 help='The state of the Jenkins node'
46 '-m', '--message', default
='',
47 help='A message to set for the offline reason of a node'
50 '-j', '--json', default
='',
51 help='Additional config in a json dictionary which will be appended after -c items',
56 def manage_node(url
, user
, password
, node
, state
, offline_message
='', config
={}):
57 server
= jenkins
.Jenkins(url
, username
=user
, password
=password
)
58 exists
= server
.node_exists(node
)
61 if exists
and state
== 'absent':
62 server
.delete_node(node
)
64 if not exists
and state
!= 'absent':
65 server
.create_node(node
, numExecutors
=1, remoteFS
='/home/jenkins',
66 launcher
=jenkins
.LAUNCHER_SSH
)
71 node_config
= xml
.etree
.ElementTree
.fromstring(server
.get_node_config(node
))
72 for key
, value
in config
.items():
73 element
= node_config
.find(key
)
76 while element
is None:
77 head
= key
.rsplit('/', 1)[0] if '/' in current_key
else None
78 tail
= key
.rsplit('/', 1)[1] if '/' in current_key
else current_key
79 e
= xml
.etree
.ElementTree
.Element(tail
)
80 if new_element
is not None:
85 element
= node_config
.find(key
)
87 parent
= node_config
.find(head
)
90 element
= node_config
.find(key
)
96 if value
['attrib'] is None:
97 if element
.text
!= value
['value']:
99 element
.text
= value
['value']
102 if element
.attrib
[value
['attrib']] != value
['value']:
104 element
.attrib
[value
['attrib']] = value
['value']
106 element
.attrib
[value
['attrib']] = value
['value']
109 xml_string
= xml
.etree
.ElementTree
.tostring(
111 xml_declaration
=True,
114 server
.reconfig_node(node
, xml_string
)
117 node_info
= server
.get_node_info(node
)
118 if node_info
['offline'] and state
== 'online':
119 server
.enable_node(node
)
121 if not node_info
['offline'] and state
== 'offline':
122 server
.disable_node(node
, offline_message
)
127 if __name__
== '__main__':
128 parser
= get_argument_parser()
129 args
= parser
.parse_args()
130 if args
.config_file
is not None:
131 config
= configparser
.ConfigParser()
132 config
.read_file(args
.config_file
)
133 if 'jenkins' not in config
.sections():
134 print("[jenkins] section not found")
137 args
.url
= config
.get('jenkins', 'url')
138 if args
.user
is None:
139 args
.user
= config
['jenkins']['user']
140 if args
.password
is None:
141 args
.password
= config
['jenkins']['password']
142 assert(args
.user
is not None)
143 assert(args
.url
is not None)
144 assert(args
.password
is not None)
146 for entry
in args
.node_config
:
147 key
, value
= entry
.split('=', 1)
149 'attrib': value
.split('=', 1)[1] if '=' in value
else None,
150 'value': value
.split('=', 1)[0] if '=' in value
else value
,
153 for key
, value
in json
.loads(args
.json
).items():
155 'attrib': value
.split('=', 1)[1] if '=' in value
else None,
156 'value': value
.split('=', 1)[0] if '=' in value
else value
,
159 args
.url
, args
.user
, args
.password
, args
.node
, args
.state
,
160 args
.message
, node_config
This page took 0.03227 seconds and 4 git commands to generate.