class OpenStack(Cal): def __init__(self): self.__config = Config('config.ini') server = self.__config.get_server() domain = self.__config.get_domain() username = self.__config.get_username() password = self.__config.get_password() self.__network_name = self.__config.get_internal_network() auth = Auth(server, 5000, username, password, domain) auth_token = auth.get_auth_token() tenant_id = auth.get_tenant_id() self.__nova = Nova(server, 8774, auth_token, tenant_id) self.__glance = Glance(server, 9292, auth_token) self.__network = Network(server, 9696, auth_token) self.__key_pair = self.__config.get_key_pair() self.__user_vm = self.__config.get_user_vm() def start(self, name='test'): image_name = self.__config.get_image_name() key_name = self.__config.get_key_name() flavor = self.__config.get_flavor() image_ref = self.__glance.get_image_ref(image_name) network_id = self.__network.get_network_id(self.__network_name) server_id = self.__nova.create(image_ref, name, flavor, network_id=network_id, key_name=key_name) return server_id def stop(self): ret = self.__nova.delete() return ret def status(self): return self.__nova.show_details() def associate_floating_ip(self): return self.__nova.add_floating_ip() def execute(self, command): ip = self.__nova.get_floating_ip() cmd = 'ssh -i ' + self.__key_pair + ' ' + self.__user_vm + '@' + ip + ' ' + command cmd_run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) return cmd_run.communicate() def put_data(self, source, destination): ip = self.__nova.get_floating_ip() cmd = 'ssh-keygen -R ' + ip subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = 'scp -i ' + self.__key_pair + ' -r ' + source + ' ' + self.__user_vm + '@' + ip + ':' + destination proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) proc.communicate() def get_data(self, source, destination): ip = self.__nova.get_floating_ip() cmd = 'ssh-keygen -R ' + ip subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = 'scp -i ' + self.__key_pair + ' -r ' + self.__user_vm + '@' + ip + ':' + destination + ' ' + source proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) proc.communicate() def backup(self, name='backup'): return self.__nova.backup(name) def restore(self): return self.__nova.restore() def get_system_info(self): absolute = self.__nova.get_system_info() print print 'maxTotalInstances: %d' % absolute['maxTotalInstances'] print 'totalInstancesUsed: %d' % absolute['maxTotalCores'] print print 'maxTotalRAMSize: %d' % absolute['maxTotalRAMSize'] print 'totalRAMUsed: %d' % absolute['totalRAMUsed'] print print 'maxTotalCores: %d' % absolute['maxTotalCores'] print 'totalCoresUsed: %d' % absolute['totalCoresUsed'] print print 'maxTotalFloatingIps: %d' % absolute['maxTotalFloatingIps'] print 'totalFloatingIpsUsed: %d' % absolute['totalFloatingIpsUsed'] print
def main(argv): usage = "backup.py -c <config.cfg> -l <vm_list>" try: opts, args = getopt(argv, "hc:l:d") debug = False if not opts: print usage sys.exit(1) for opt, arg in opts: if (opt == "-h") or (opt == "--help"): print usage sys.exit(0) elif opt in ("-c"): config_file = arg elif opt in ("-l"): list_file = arg elif opt in ("-d"): debug = True except GetoptError: print usage sys.exit(1) config = Config(config_file, debug) # Load vm list with open(list_file) as f: vm_list = [line.rstrip('\n') for line in f] time_start = int(time.time()) # Connect to server api = ovirtsdk.api.API( url=config.get_server(), username=config.get_username(), password=config.get_password(), insecure=True, debug=False ) # Queue for VMs to be backed up queue = Queue.Queue() for vm in vm_list: # Skip comment lines if vm[0] == "#": continue if api.vms.get(vm): # Check VM exists queue.put(vm) else: log(vm, "Skipping - does not exist") # Create workers worker_count = 2 for i in range(worker_count): t = BackupWorker(queue, api, config) t.setDaemon(True) t.start() # Wait for queue to be emptied while threading.active_count() > 1: time.sleep(1) print "All backups done" # Disconnect from the server api.disconnect()
def main(argv): usage = "backup.py -c <config.cfg>" try: opts, args = getopt(argv, "hc:d") debug = False if not opts: print usage sys.exit(1) for opt, arg in opts: if (opt == "-h") or (opt == "--help"): print usage sys.exit(0) elif opt in ("-c"): config_file = arg elif opt in ("-d"): debug = True except GetoptError: print usage sys.exit(1) config = Config(config_file, debug) time_start = int(time.time()) # Connect to server api = ovirtsdk.api.API( url=config.get_server(), username=config.get_username(), password=config.get_password(), insecure=True, debug=False, ) # Test if all VM names are valid for vm_from_list in config.get_vm_names(): if not api.vms.get(vm_from_list): print "!!! There are no VM with the following name in your cluster: " + vm_from_list sys.exit(1) for vm_from_list in config.get_vm_names(): print "Start backup for: " + vm_from_list # Get the VM vm = api.vms.get(vm_from_list) # Cleanup: Delete the cloned VM VMTools.delete_vm(api, config, vm_from_list) # Delete old backup snapshots VMTools.delete_snapshots(vm, config, vm_from_list) # Create a VM snapshot: try: print "Snapshot creation started ..." if not config.get_dry_run(): vm.snapshots.add(params.Snapshot(description=config.get_snapshot_description(), vm=vm)) VMTools.wait_for_snapshot_operation(vm, config, "creation") print "Snapshot created" except Exception as e: print "Can't create snapshot for VM: " + vm_from_list print "DEBUG: " + str(e) sys.exit(1) # Clone the snapshot into a VM snapshots = vm.snapshots.list(description=config.get_snapshot_description()) if not snapshots: print "!!! No snapshot found" sys.exit(1) snapshot_param = params.Snapshot(id=snapshots[0].id) snapshots_param = params.Snapshots(snapshot=[snapshot_param]) print "Clone into VM started ..." if not config.get_dry_run(): api.vms.add( params.VM( name=vm_from_list + config.get_vm_middle() + config.get_vm_suffix(), memory=vm.get_memory(), cluster=api.clusters.get(config.get_cluster_name()), snapshots=snapshots_param, ) ) VMTools.wait_for_vm_operation(api, config, "Cloning", vm_from_list) print "Cloning finished" # Delete backup snapshots VMTools.delete_snapshots(vm, config, vm_from_list) # Delete old backups VMTools.delete_old_backups(api, config, vm_from_list) # Export the VM try: vm_clone = api.vms.get(vm_from_list + config.get_vm_middle() + config.get_vm_suffix()) print "Export started ..." if not config.get_dry_run(): vm_clone.export(params.Action(storage_domain=api.storagedomains.get(config.get_export_domain()))) VMTools.wait_for_vm_operation(api, config, "Exporting", vm_from_list) print "Exporting finished" except Exception as e: print "Can't export cloned VM (" + vm_from_list + config.get_vm_middle() + config.get_vm_suffix() + ") to domain: " + config.get_export_domain() print "DEBUG: " + str(e) sys.exit(1) # Delete the VM VMTools.delete_vm(api, config, vm_from_list) time_end = int(time.time()) time_diff = time_end - time_start time_minutes = int(time_diff / 60) time_seconds = time_diff % 60 print "Duration: " + str(time_minutes) + ":" + str(time_seconds) + " minutes" print "VM exported as " + vm_from_list + config.get_vm_middle() + config.get_vm_suffix() print "Backup done for: " + vm_from_list print "All backups done" # Disconnect from the server api.disconnect()