def info(self, flags="-auxh"): """ Returns a struct of hardware information. By default, this pulls down all of the devices. If you don't care about them, set with_devices to False. """ flags.replace(";", "") # prevent stupidity cmd = sub_process.Popen(["/bin/ps", flags], executable="/bin/ps", stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) data, error = cmd.communicate() # We can get warnings for odd formatting. warnings != errors. if error and error[:7] != "Warning": raise codes.FuncException(error.split('\n')[0]) results = [] for x in data.split("\n"): tokens = x.split() results.append(tokens) return results
def find_vm(self, vmid): """ Extra bonus feature: vmid = -1 returns a list of everything """ conn = self.conn vms = [] # this block of code borrowed from virt-manager: # get working domain's name ids = conn.listDomainsID() for id in ids: vm = conn.lookupByID(id) vms.append(vm) # get defined domain names = conn.listDefinedDomains() for name in names: vm = conn.lookupByName(name) vms.append(vm) if vmid == -1: return vms for vm in vms: if vm.name() == vmid: return vm raise codes.FuncException("virtual machine %s not found" % vmid)
def get_running(self): """ Get a list of which services are running, stopped, or disabled. """ results = [] # Get services services = self.get_enabled() init_return_codes = { 0: 'running', 1: 'dead', 2:'locked', 3:'stopped' } for service in services: filename = os.path.join("/etc/init.d/",service[0]) # Run status for service try: init_exec = sub_process.Popen([filename, "status"], stdout=sub_process.PIPE, close_fds=True, env={ "LANG": "C" }) except Exception, e: raise codes.FuncException("Service status error %s on initscript %s" % (e, filename)) # Get status output data = init_exec.communicate()[0] # Wait for command to complete init_exec.wait() # Append the result, service name, return status and status output results.append((service[0], init_return_codes[init_exec.returncode], data))
def install(self, server_name, target_name, system=False, virt_name=None, virt_path=None, graphics=False): """ Install a new virt system by way of a named cobbler profile. """ # Example: # install("bootserver.example.org", "fc7webserver", True) # install("bootserver.example.org", "client.example.org", True, "client-disk0", "HostVolGroup00") conn = self.__get_conn() if conn is None: raise codes.FuncException("no connection") if not os.path.exists("/usr/bin/koan"): raise codes.FuncException("no /usr/bin/koan") target = "profile" if system: target = "system" koan_args = [ "/usr/bin/koan", "--virt", "--%s=%s" % (target, target_name), "--server=%s" % server_name ] if virt_name: koan_args.append("--virt-name=%s" % virt_name) if virt_path: koan_args.append("--virt-path=%s" % virt_path) if not graphics: koan_args.append("--nogfx") rc = sub_process.call(koan_args, shell=False, close_fds=True) if rc == 0: return 0 else: raise codes.FuncException("koan returned %d" % rc)
def __command(self, service_name, command): service_name = service_name.strip() # remove useless spaces filename = os.path.join("/etc/init.d/",service_name) if os.path.exists(filename): return sub_process.call(["/sbin/service", service_name, command], close_fds=True, env={ 'LANG':'C' }) else: raise codes.FuncException("Service not installed: %s" % service_name)
def kill(self, pid, signal="TERM"): if pid == "0": raise codes.FuncException("Killing pid group 0 not permitted") if signal == "": # this is default /bin/kill behaviour, # it claims, but enfore it anyway signal = "-TERM" if signal[0] != "-": signal = "-%s" % signal rc = sub_process.call(["/bin/kill", signal, pid], executable="/bin/kill", shell=False) print rc return rc
def __init__(self): cmd = sub_process.Popen("uname -r", shell=True, stdout=sub_process.PIPE, close_fds=True) output = cmd.communicate()[0] if output.find("xen") != -1: conn = libvirt.open(None) else: conn = libvirt.open("qemu:///system") if not conn: raise codes.FuncException("hypervisor connection failure") self.conn = conn