def analyse(tui): x = xapi.open() 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"] try: sr_r = x.xenapi.SR.get_record(default_sr) print >>sys.stderr, "OK: default SR is set" except: path = tui.text("Where would you like to store disk images?", "/usr/share/xapi/images") hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >>sys.stderr, "ERROR: host is already in a pool" return mkdir(path) sr = x.xenapi.SR.create(hosts[0], { "path": path }, "0", path, "Files stored in %s" % path, "ffs", "default", False) 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.open() x.login_with_password("root", "") try: srs = x.xenapi.SR.get_all_records() for sr in srs.keys(): if srs[sr]["type"] == "ext": 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/run/sr-mount/%s" % uuid mkdir(path) sr = x.xenapi.SR.introduce(uuid, path, "Files stored in %s" % path, "ext", "default", False, {}) pbd = x.xenapi.PBD.create({ "host": hosts[0], "SR": sr, "device_config": {"path": path}}) x.xenapi.PBD.plug(pbd) pool = x.xenapi.pool.get_all()[0] pool_r = x.xenapi.pool.get_record(pool) default_sr = pool_r["default_SR"] x.xenapi.pool.set_default_SR(pool, sr) print >>sys.stderr, "OK: created local ext SR for openstack" finally: x.logout()
def analyse(tui): x = xapi.open() 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.open() 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, mode, 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.open() 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.open() 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?", False)): 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 analyse(tui, config): devices = config["devices"] x = xapi.open() 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": if "IPADDR" in sysconfig and "NETWORK" in sysconfig and "NETMASK" in sysconfig: ipaddr = sysconfig["IPADDR"] network = sysconfig["NETWORK"] netmask = sysconfig["NETMASK"] interfaces_to_reconfigure[d] = ("Static", ipaddr, netmask, network, "") return (file_changes, interfaces_to_reconfigure) finally: x.logout()
def analyse(tui): x = xapi.open() x.login_with_password("root", "") try: hosts = x.xenapi.host.get_all() if len(hosts) <> 1: print >>sys.stderr, "WARNING: cannot configuring networking if already pooled" return 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 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 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() options = [] for d in devices: options.append((d, "<insert description>",)) mgmt = tui.choose("Please select a management interface", options) file_changes = [] for d in devices: sysconfig = load_sysconfig(d) 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": print >>sys.stderr, "Setting PIF to DHCP" x.xenapi.PIF.reconfigure_ip(device_to_pif[d], "DHCP", "", "", "", "") if "BOOTPROTO" in sysconfig and sysconfig["BOOTPROTO"] == "none": if "IPADDR" in sysconfig and "NETWORK" in sysconfig and "NETMASK" in sysconfig: ipaddr = sysconfig["IPADDR"] network = sysconfig["NETWORK"] netmask = sysconfig["NETMASK"] print >>sys.stderr, "Setting PIF to static" x.xenapi.PIF.reconfigure_ip(device_to_pif[d], "Static", ipaddr, netmask, network, "") if d == mgmt: print >>sys.stderr, "Setting PIF as management" x.xenapi.host.management_reconfigure(device_to_pif[d]) return file_changes finally: x.logout()