def analyse(tui): x = xapi.connect() x.login_with_password("root", "") try: pool = x.xenapi.pool.get_all()[0] pool_r = x.xenapi.pool.get_record(pool) default_sr = pool_r["default_SR"] hosts = x.xenapi.host.get_all() # Set iSCSI IQN if not x.xenapi.host.get_other_config(hosts[0]).has_key("iscsi_iqn"): x.xenapi.host.add_to_other_config( hosts[0], "iscsi_iqn", "iqn.2013-12.com.xendomain.xenserver") try: sr_r = x.xenapi.SR.get_record(default_sr) print >> sys.stderr, "OK: default SR is set" except: if len(hosts) <> 1: print >> sys.stderr, "ERROR: host is already in a pool" return sr = create_default_sr(tui, x, hosts[0]) if sr is None: return x.xenapi.pool.set_default_SR(pool, sr) x.xenapi.pool.set_suspend_image_SR(pool, sr) x.xenapi.pool.set_crash_dump_SR(pool, sr) print >> sys.stderr, "OK: created default SR" finally: x.logout()
def analyse(tui): x = xapi.connect() x.login_with_password("root", "") try: srs = x.xenapi.SR.get_all_records() for sr in srs.keys(): if "created_by_install_wizard_for_openstack" in srs[sr]["other_config"].keys(): return if not(tui.yesno("Would you like to configure some local storage for openstack?", True)): return uuid = generate_uuid () hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >>sys.stderr, "ERROR: host is already in a pool" return path = "/var/lib/xapi/sr-mount/%s" % uuid mkdir(path) sr = x.xenapi.SR.introduce(uuid, path, "Files stored in %s" % path, "file", "default", False, {}) x.xenapi.SR.add_to_other_config(sr, "created_by_install_wizard_for_openstack", "") pbd = x.xenapi.PBD.create({ "host": hosts[0], "SR": sr, "device_config": {"path": path, "location": path}}) x.xenapi.PBD.plug(pbd) pool = x.xenapi.pool.get_all()[0] x.xenapi.pool.set_default_SR(pool, sr) x.xenapi.pool.set_suspend_image_SR(pool, sr) x.xenapi.pool.set_crash_dump_SR(pool, sr) print >>sys.stderr, "OK: created local file SR for openstack" finally: x.logout()
def analyse(tui): x = xapi.connect() x.login_with_password("root", "") try: pool = x.xenapi.pool.get_all()[0] pool_r = x.xenapi.pool.get_record(pool) default_sr = pool_r["default_SR"] hosts = x.xenapi.host.get_all() # Set iSCSI IQN if not x.xenapi.host.get_other_config(hosts[0]).has_key("iscsi_iqn"): x.xenapi.host.add_to_other_config(hosts[0], "iscsi_iqn", "iqn.2013-12.com.xendomain.xenserver") try: sr_r = x.xenapi.SR.get_record(default_sr) print >>sys.stderr, "OK: default SR is set" except: if len(hosts) <> 1: print >>sys.stderr, "ERROR: host is already in a pool" return sr = create_default_sr(tui, x, hosts[0]) if sr is None: return x.xenapi.pool.set_default_SR(pool, sr) x.xenapi.pool.set_suspend_image_SR(pool, sr) x.xenapi.pool.set_crash_dump_SR(pool, sr) print >>sys.stderr, "OK: created default SR" finally: x.logout()
def analyse(tui, config): devices = config["devices"] x = xapi.connect() x.login_with_password("root", "") try: file_changes = [] interfaces_to_reconfigure = {} for d in devices: sysconfig = load_sysconfig(d) if not sysconfig: print >> sys.stderr, "There is no ifcfg- file for NIC %s: skipping" % d continue new_sysconfig = {} for key in sysconfig: if key == "NM_CONTROLLED" and sysconfig[ "NM_CONTROLLED"] == "yes": new_sysconfig["NM_CONTROLLED"] = "no" print >> sys.stderr, "Setting ifcfg-%s:NM_CONTROLLED to no" % d elif key == "ONBOOT" and sysconfig["ONBOOT"] == "yes": new_sysconfig["ONBOOT"] = "no" print >> sys.stderr, "Setting ifcfg-%s:ON_BOOT to no" % d else: new_sysconfig[key] = sysconfig[key] if sysconfig <> new_sysconfig: file_changes.append(( sysconfig_file(d), save_sysconfig(new_sysconfig), )) if "BOOTPROTO" in sysconfig and sysconfig["BOOTPROTO"] == "dhcp": interfaces_to_reconfigure[d] = ("DHCP", "", "", "", "") if "BOOTPROTO" in sysconfig and (sysconfig["BOOTPROTO"] == "none" or sysconfig["BOOTPROTO"] == "static"): if "IPADDR" in sysconfig and "NETMASK" in sysconfig: ipaddr = sysconfig["IPADDR"] if "GATEWAY" in sysconfig: gateway = sysconfig["GATEWAY"] elif "NETWORK" in sysconfig: gateway = sysconfig["NETWORK"] else: gateway = "" netmask = sysconfig["NETMASK"] interfaces_to_reconfigure[d] = ("Static", ipaddr, netmask, gateway, "") return (file_changes, interfaces_to_reconfigure) finally: x.logout()
def analyse(tui): x = xapi.connect() x.login_with_password("root", "") try: hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >> sys.stderr, "FAILED: to find localhost (is this host already pooled?)" return current = x.xenapi.host.get_name_label(hosts[0]) h = hostname() if current <> h and h <> "": x.xenapi.host.set_name_label(hosts[0], h) finally: x.logout()
def analyse(tui): x = xapi.connect() x.login_with_password("root", "") try: hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >>sys.stderr, "FAILED: to find localhost (is this host already pooled?)" return current = x.xenapi.host.get_name_label(hosts[0]) h = hostname () if current <> h and h <> "": x.xenapi.host.set_name_label(hosts[0], h) finally: x.logout()
def configure(config, new_interfaces): """Configure [new_interfaces] through the XenAPI""" x = xapi.connect() x.login_with_password("root", "") try: for device in new_interfaces: mode, address, netmask, gateway, dns = new_interfaces[device] if mode == "DHCP": print >> sys.stderr, "Configuring %s with DHCP" % device else: print >> sys.stderr, "Configuring %s with static IP %s netmask %s gateway %s DNS %s" % (device, address, netmask, gateway, dns) x.xenapi.PIF.reconfigure_ip(config["device_to_pif"][device], mode, address, netmask, gateway, dns) if "management" in config: print >> sys.stderr, "Configuring %s as the management interface" % config["management"] x.xenapi.host.management_reconfigure(config["device_to_pif"][config["management"]]) finally: x.logout()
def analyse(tui, config): devices = config["devices"] x = xapi.connect() x.login_with_password("root", "") try: file_changes = [] interfaces_to_reconfigure = {} for d in devices: sysconfig = load_sysconfig(d) if not sysconfig: print >>sys.stderr, "There is no ifcfg- file for NIC %s: skipping" % d continue new_sysconfig = {} for key in sysconfig: if key == "NM_CONTROLLED" and sysconfig["NM_CONTROLLED"] == "yes": new_sysconfig["NM_CONTROLLED"] = "no" print >>sys.stderr, "Setting ifcfg-%s:NM_CONTROLLED to no" % d elif key == "ONBOOT" and sysconfig["ONBOOT"] == "yes": new_sysconfig["ONBOOT"] = "no" print >>sys.stderr, "Setting ifcfg-%s:ON_BOOT to no" % d else: new_sysconfig[key] = sysconfig[key] if sysconfig <> new_sysconfig: file_changes.append((sysconfig_file(d), save_sysconfig(new_sysconfig),)) if "BOOTPROTO" in sysconfig and sysconfig["BOOTPROTO"] == "dhcp": interfaces_to_reconfigure[d] = ("DHCP", "", "", "", "") if "BOOTPROTO" in sysconfig and (sysconfig["BOOTPROTO"] == "none" or sysconfig["BOOTPROTO"] == "static"): if "IPADDR" in sysconfig and "NETMASK" in sysconfig: ipaddr = sysconfig["IPADDR"] if "GATEWAY" in sysconfig: gateway = sysconfig["GATEWAY"] elif "NETWORK" in sysconfig: gateway = sysconfig["NETWORK"] else: gateway = "" netmask = sysconfig["NETMASK"] interfaces_to_reconfigure[d] = ("Static", ipaddr, netmask, gateway, "") return (file_changes, interfaces_to_reconfigure) finally: x.logout()
def configure(config, new_interfaces): """Configure [new_interfaces] through the XenAPI""" x = xapi.connect() x.login_with_password("root", "") try: for device in new_interfaces: mode, address, netmask, gateway, dns = new_interfaces[device] if mode == "DHCP": print >> sys.stderr, "Configuring %s with DHCP" % device else: print >> sys.stderr, "Configuring %s with static IP %s netmask %s gateway %s DNS %s" % ( device, address, netmask, gateway, dns) x.xenapi.PIF.reconfigure_ip(config["device_to_pif"][device], mode, address, netmask, gateway, dns) if "management" in config: print >> sys.stderr, "Configuring %s as the management interface" % config[ "management"] x.xenapi.host.management_reconfigure( config["device_to_pif"][config["management"]]) finally: x.logout()
def list_devices(tui): """Use xapi to query the PIFs on the local host""" x = xapi.connect() x.login_with_password("root", "") no_configuration = { "devices": [], # none will be managed by xapi "device_to_pif": {} } try: hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >>sys.stderr, "WARNING: cannot configuring networking if already pooled" return no_configuration pifs = x.xenapi.PIF.get_all_records() for pif in pifs: if pifs[pif]["management"]: print >>sys.stderr, "OK: found a configured management interface" return no_configuration if not(tui.yesno("Would you like me to set up host networking for XenServer?", True)): print >>sys.stderr, "WARNING: host networking is not set up" return no_configuration print "PIF scan %s" % hosts[0] x.xenapi.PIF.scan(hosts[0]) print "PIF.get_all_records" pifs = x.xenapi.PIF.get_all_records() device_to_pif = {} devices = [] for pif in pifs: pif_r = pifs[pif] devices.append(pif_r["device"]) device_to_pif[pif_r["device"]] = pif devices.sort() return { "devices": devices, "device_to_pif": device_to_pif } finally: x.logout()
def list_devices(tui): """Use xapi to query the PIFs on the local host""" x = xapi.connect() x.login_with_password("root", "") no_configuration = { "devices": [], # none will be managed by xapi "device_to_pif": {} } try: hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >> sys.stderr, "WARNING: cannot configuring networking if already pooled" return no_configuration pifs = x.xenapi.PIF.get_all_records() for pif in pifs: if pifs[pif]["management"]: print >> sys.stderr, "OK: found a configured management interface" return no_configuration if not (tui.yesno( "Would you like me to set up host networking for XenServer?", True)): print >> sys.stderr, "WARNING: host networking is not set up" return no_configuration print "PIF scan %s" % hosts[0] x.xenapi.PIF.scan(hosts[0]) print "PIF.get_all_records" pifs = x.xenapi.PIF.get_all_records() device_to_pif = {} devices = [] for pif in pifs: pif_r = pifs[pif] devices.append(pif_r["device"]) device_to_pif[pif_r["device"]] = pif devices.sort() return {"devices": devices, "device_to_pif": device_to_pif} finally: x.logout()
def test_vm_create(): server, session = connect() vm_uuid = None local_vdi_uuid = None local_vbd_uuid = None vif_uuid = None # List all VMs vm_list = execute(server, 'VM.get_all', (session, )) vm_names = [] for vm_uuid in vm_list: vm_record = execute(server, 'VM.get_record', (session, vm_uuid)) vm_names.append(vm_record['name_label']) # Get default SR local_sr_list = execute(server, 'SR.get_by_name_label', (session, local_vdi_cfg['SR_name'])) local_sr_uuid = local_sr_list[0] # Get default network net_list = execute(server, 'network.get_all', (session, )) net_uuid = net_list[0] try: # Create a new VM print 'Create VM' vm_uuid = execute(server, 'VM.create', (session, vm_cfg)) print 'Create VDI' # Create a new VDI (Local) local_vdi_cfg['SR'] = local_sr_uuid local_vdi_uuid = execute(server, 'VDI.create', (session, local_vdi_cfg)) print 'Create VBD' # Create a new VBD (Local) local_vbd_cfg['VM'] = vm_uuid local_vbd_cfg['VDI'] = local_vdi_uuid local_vbd_uuid = execute(server, 'VBD.create', (session, local_vbd_cfg)) print 'Craete VIF' # Create a new VIF vif_cfg['network'] = net_uuid vif_cfg['VM'] = vm_uuid vif_uuid = execute(server, 'VIF.create', (session, vif_cfg)) # Create a console console_cfg['VM'] = vm_uuid console_uuid = execute(server, 'console.create', (session, console_cfg)) print console_uuid # Start the VM execute(server, 'VM.start', (session, vm_uuid, False)) time.sleep(30) test_suspend = False if test_suspend: print 'Suspending VM..' execute(server, 'VM.suspend', (session, vm_uuid)) print 'Suspended VM.' time.sleep(5) print 'Resuming VM ...' execute(server, 'VM.resume', (session, vm_uuid, False)) print 'Resumed VM.' # Wait for user to say we're good to shut it down while True: destroy = raw_input('destroy VM? ') if destroy[0] in ('y', 'Y'): break finally: # Clean up if vif_uuid: execute(server, 'VIF.destroy', (session, vif_uuid)) if local_vbd_uuid: execute(server, 'VBD.destroy', (session, local_vbd_uuid)) if local_vdi_uuid: execute(server, 'VDI.destroy', (session, local_vdi_uuid)) if vm_uuid: try: execute(server, 'VM.hard_shutdown', (session, vm_uuid)) time.sleep(2) except: pass try: execute(server, 'VM.destroy', (session, vm_uuid)) except: pass
def test_vm_create(): server, session = connect() vm_uuid = None vdi_uuid = None local_vdi_uuid = None local_vbd_uuid = None vbd_uuid = None vif_uuid = None # List all VMs vm_list = execute(server, 'VM.get_all', (session,)) vm_names = [] for vm_uuid in vm_list: vm_record = execute(server, 'VM.get_record', (session, vm_uuid)) vm_names.append(vm_record['name_label']) # Get default SR sr_list = execute(server, 'SR.get_by_name_label', (session, vdi_cfg['SR_name'])) sr_uuid = sr_list[0] local_sr_list = execute(server, 'SR.get_by_name_label', (session, local_vdi_cfg['SR_name'])) local_sr_uuid = local_sr_list[0] # Get default network net_list = execute(server, 'network.get_all', (session,)) net_uuid = net_list[0] try: # Create a new VM vm_uuid = execute(server, 'VM.create', (session, vm_cfg)) # Create a new VDI vdi_cfg['SR'] = sr_uuid vdi_uuid = execute(server, 'VDI.create', (session, vdi_cfg)) # Create a VDI backed VBD vbd_cfg['VM'] = vm_uuid vbd_cfg['VDI'] = vdi_uuid vbd_uuid = execute(server, 'VBD.create', (session, vbd_cfg)) # Create a new VDI (Local) local_vdi_cfg['SR'] = local_sr_uuid local_vdi_uuid = execute(server, 'VDI.create', (session, local_vdi_cfg)) # Create a new VBD (Local) local_vbd_cfg['VM'] = vm_uuid local_vbd_cfg['VDI'] = local_vdi_uuid local_vbd_uuid = execute(server, 'VBD.create', (session, local_vbd_cfg)) # Create a new VIF vif_cfg['network'] = net_uuid vif_cfg['VM'] = vm_uuid vif_uuid = execute(server, 'VIF.create', (session, vif_cfg)) # Create a console console_cfg['VM'] = vm_uuid console_uuid = execute(server, 'console.create', (session, console_cfg)) print console_uuid # Start the VM execute(server, 'VM.start', (session, vm_uuid, False)) time.sleep(30) test_suspend = False if test_suspend: print 'Suspending VM..' execute(server, 'VM.suspend', (session, vm_uuid)) print 'Suspended VM.' time.sleep(5) print 'Resuming VM ...' execute(server, 'VM.resume', (session, vm_uuid, False)) print 'Resumed VM.' finally: # Wait for user to say we're good to shut it down while True: destroy = raw_input('destroy VM? ') if destroy[0] in ('y', 'Y'): break # Clean up if vif_uuid: execute(server, 'VIF.destroy', (session, vif_uuid)) if local_vbd_uuid: execute(server, 'VBD.destroy', (session, local_vbd_uuid)) if local_vdi_uuid: execute(server, 'VDI.destroy', (session, local_vdi_uuid)) if vbd_uuid: execute(server, 'VBD.destroy', (session, vbd_uuid)) if vdi_uuid: execute(server, 'VDI.destroy', (session, vdi_uuid)) if vm_uuid: try: execute(server, 'VM.hard_shutdown', (session, vm_uuid)) time.sleep(2) except: pass execute(server, 'VM.destroy', (session, vm_uuid))