2 # -*- coding: utf-8 -*-
4 # Copyright (C) 2015 - Michael Jeanson <mjeanson@efficios.com>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """ This script is used to upgrade the base snapshot of standalone ci slaves """
23 JENKINS_URL
= 'https://ci.lttng.org'
25 DISTRO_LIST
= ['el', 'sles', 'ubuntu']
26 DEFAULT_DISTRO
= 'ubuntu'
28 'el': 'yum update -y && package-cleanup -y --oldkernels --count=2 && yum clean all',
29 'sles': 'zypper --non-interactive refresh && zypper --non-interactive patch --auto-agree-with-licenses --with-interactive',
30 'ubuntu': 'apt-get update && apt-get dist-upgrade -V -y && apt-get clean && apt-get --purge autoremove -y',
33 BASESNAP
= 'base-configuration'
38 <description>Snapshot of OS install and updates</description>
39 <memory snapshot='no'/>
46 from jenkinsapi
.jenkins
import Jenkins
47 from time
import sleep
55 parser
= argparse
.ArgumentParser(description
='Update base snapshot.')
56 parser
.add_argument('instance_name', metavar
='INSTANCE', type=str,
57 help='the shortname of the instance to update')
58 parser
.add_argument('vmhost_name', metavar
='VMHOST', type=str,
59 help='the hostname of the VM host')
60 parser
.add_argument('--distro', choices
=DISTRO_LIST
,
61 default
=DEFAULT_DISTRO
, type=str,
62 help='the distro of the target instance')
64 args
= parser
.parse_args()
66 instance_name
= args
.instance_name
67 vmhost_name
= args
.vmhost_name
71 # Get jenkibs connexion
72 jenkins
= Jenkins(JENKINS_URL
, username
=USERNAME
, password
=APIKEY
)
75 print("Getting node %s from Jenkins..." % instance_name
)
76 node
= jenkins
.get_node(instance_name
)
79 print("Could not get node %s on %s" % (instance_name
, JENKINS_URL
))
82 # Check if node is idle
84 print("Node %s is not idle" % instance_name
)
88 # Set node temporarily offline
89 if not node
.is_temporarily_offline():
90 node
.toggle_temporarily_offline('Down for upgrade to base snapshot')
92 # Get libvirt connexion
93 print("Opening libvirt connexion to %s..." % vmhost_name
)
94 vmhost
= libvirt
.open("qemu+ssh://root@%s/system" % vmhost_name
)
97 print("Could not connect to libvirt on %s" % vmhost_name
)
101 print("Getting instance %s from libvirt..." % instance_name
)
102 vminstance
= vmhost
.lookupByName(instance_name
)
105 print("Could not get instance %s on %s" % (instance_name
, vmhost_name
))
108 # If instance is running, shutdown
109 print("Checking if instance %s is running..." % instance_name
)
110 if vminstance
.isActive():
112 print("Shutting down instance %s" % instance_name
)
115 print("Failed to shutdown %s", instance_name
)
119 # Revert to base snapshot
120 print("Getting base snapshot...")
121 basesnap
= vminstance
.snapshotLookupByName(BASESNAP
)
123 print("Could not find base snapshot %s" % BASESNAP
)
126 #if not basesnap.isCurrent():
127 # print("Not current snapshot")
129 print("Reverting to base snapshot...")
131 vminstance
.revertToSnapshot(basesnap
)
133 print("Failed to revert to base snapshot %s" % basesnap
.getName())
138 print("Starting instance %s.." % instance_name
)
141 print("Failed to start instance %s" % instance_name
)
145 # Wait for instance to boot
146 print("Waiting for instance to boot...")
150 print("Running upgrade command...")
151 client
= paramiko
.SSHClient()
152 client
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
153 client
.load_system_host_keys()
154 client
.connect(instance_name
, username
="root")
155 stdin
, stdout
, stderr
= client
.exec_command(DISTRO_COMMAND
[distro
])
156 while not stdout
.channel
.exit_status_ready():
157 if stdout
.channel
.recv_ready():
158 rl
, wl
, xl
= select
.select([stdout
.channel
], [], [], 0.0)
160 print(stdout
.channel
.recv(1024)),
162 if stdout
.channel
.recv_exit_status() != 0:
163 print("Update command failed!")
166 # Close ssh connexion
170 print("Shutting down instance...")
172 vminstance
.shutdown()
174 print("Failed to shutdown instance %s" % instance_name
)
177 while vminstance
.isActive():
179 print("Waiting for instance to shutdown...")
181 # Delete original base snapshot
182 print("Deleting current base snapshot...")
186 print("Failed to delete base snapshot %s" % basesnap
.getName())
189 # Create new base snapshot
190 print("Creating new base snapshot...")
192 vminstance
.snapshotCreateXML(SNAPSHOTXML
)
194 print("Failed to create new snapshot.")
197 # Set node online in jenkins
198 if node
.is_temporarily_offline():
199 node
.toggle_temporarily_offline()
205 if __name__
== "__main__":
This page took 0.03605 seconds and 4 git commands to generate.