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 && 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=PASSWORD)
75 #node = jenkins.get_node(instance_name)
77 # Set node temporarily offline
78 #node.toggle_temporarily_offline('Down for upgrade to base snapshot')
80 # Get libvirt connexion
81 print("Opening libvirt connexion to %s..." % vmhost_name
)
82 vmhost
= libvirt
.open("qemu+ssh://root@%s/system" % vmhost_name
)
85 print("Could not connect to libvirt on %s" % vmhost_name
)
89 print("Getting instance %s from libvirt..." % instance_name
)
90 vminstance
= vmhost
.lookupByName(instance_name
)
93 print("Could not get instance %s on %s" % (instance_name
, vmhost_name
))
96 # If instance is running, shutdown
97 print("Checking if instance %s is running..." % instance_name
)
98 if vminstance
.isActive():
100 print("Shutting down instance %s" % instance_name
)
103 print("Failed to shutdown %s", instance_name
)
107 # Revert to base snapshot
108 print("Getting base snapshot...")
109 basesnap
= vminstance
.snapshotLookupByName(BASESNAP
)
111 print("Could not find base snapshot %s" % BASESNAP
)
114 #if not basesnap.isCurrent():
115 # print("Not current snapshot")
117 print("Reverting to base snapshot...")
119 vminstance
.revertToSnapshot(basesnap
)
121 print("Failed to revert to base snapshot %s" % basesnap
.getName())
126 print("Starting instance %s.." % instance_name
)
129 print("Failed to start instance %s" % instance_name
)
133 # Wait for instance to boot
134 print("Waiting for instance to boot...")
138 print("Running upgrade command...")
139 client
= paramiko
.SSHClient()
140 client
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
141 client
.load_system_host_keys()
142 client
.connect(instance_name
, username
="root")
143 stdin
, stdout
, stderr
= client
.exec_command(DISTRO_COMMAND
[distro
])
144 while not stdout
.channel
.exit_status_ready():
145 if stdout
.channel
.recv_ready():
146 rl
, wl
, xl
= select
.select([stdout
.channel
], [], [], 0.0)
148 print(stdout
.channel
.recv(1024)),
150 if stdout
.channel
.recv_exit_status() != 0:
151 print("Update command failed!")
154 # Close ssh connexion
158 print("Shutting down instance...")
160 vminstance
.shutdown()
162 print("Failed to shutdown instance %s" % instance_name
)
165 while vminstance
.isActive():
167 print("Waiting for instance to shutdown...")
169 # Delete original base snapshot
170 print("Deleting current base snapshot...")
174 print("Failed to delete base snapshot %s" % basesnap
.getName())
177 # Create new base snapshot
178 print("Creating new base snapshot...")
180 vminstance
.snapshotCreateXML(SNAPSHOTXML
)
182 print("Failed to create new snapshot.")
189 if __name__
== "__main__":
This page took 0.049061 seconds and 4 git commands to generate.