-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjenkins_check.py
executable file
·58 lines (53 loc) · 1.97 KB
/
jenkins_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import logging
import socket
import subprocess
import sys
import time
from xml.etree.ElementTree import parse, Element
log = logging.getLogger(__name__)
is_debug = True
if is_debug:
logging.basicConfig(level=logging.DEBUG,format="%(levelname)s:%(message)s")
else:
logging.basicConfig(level=logging.INFO,format="%(levelname)s:%(message)s")
def update_location():
'''
update Jenkins URL to currrent location according to ip address
'''
jenkins_file = "/etc/sysconfig/jenkins"
jenkins_home = None
with open(jenkins_file) as fh:
for line in fh.readlines():
if line.startswith('JENKINS_HOME'):
jenkins_home = line.split('=')[-1].strip('\n').strip('"')
log.info("Find jenkins home: %s", jenkins_home)
if jenkins_home is None:
log.info("jenkins home not found!")
sys.exit(1)
location_file = jenkins_home + '/jenkins.model.JenkinsLocationConfiguration.xml'
doc = parse(location_file)
for i in range(60):
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
if "127.0.0.1" not in local_ip:
break
log.info("try again to get public ip")
subprocess.check_output("systemctl restart NetworkManager", stderr=subprocess.STDOUT, shell=True)
time.sleep(1)
log.info("Local IP: %s", local_ip)
root = doc.getroot()
e = root.find('jenkinsUrl')
#e = Element('jenkinsUrl')
log.info(e.text)
if local_ip not in e.text:
e.text = 'http://%s:8080/' % local_ip
doc.write(location_file,xml_declaration=True)
log.info("Updated jenkins url to %s", e.text )
cmd = 'systemctl restart jenkins'
subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
else:
log.info("No need to update jenkins url." )
subprocess.check_output("generate_key_data.sh", stderr=subprocess.STDOUT, shell=True)
if '__main__' == __name__:
update_location()