def _create_connection(): url = self.get_virt_url() if not url: raise error.ConnError("[io] No connection url supplied") if url.startswith('qemu+ssh'): return Ssh.new_from_url(url) if url.startswith('qemu'): return Local() raise error.ConnError("Unsupported connection type. Currently " "qemu and qemu+ssh is supported")
def _create_connection(): libvirt.registerErrorHandler(_error_handler, ctx=None) url = self.get_virt_url() if not url: raise error.ConnError("[libvirt] No connection url supplied") virt = libvirt.open(url) if not virt: raise error.ConnError("[libvirt] Could not connect " "to {}".format(url)) return virt
def yaml_read(path): try: with open(path, 'r') as stream: return yaml.load(stream) except IOError: raise error.ConnError("Could not open definition: " "No such file or directory") except yaml.YAMLError as err: raise error.ValidatorError("Error parsing definition: {}".format(err))
def user_name(self, cmpnt): if self.args().user is not None: return self.args().user #FIXME: Add default user after ansible branch is merged attr = cmpnt.get_attribute("user") if attr is None: raise error.ConnError("Could not find default ssh user") return attr.default_user()
def _run_ssh_cmd(self, name, user, ip, retry, options="", step=0): if step == retry: raise error.ConnError("Could not connect to {}".format(name)) self.counted(step, "connecting to {}...".format(name)) cmd = "ssh {} {}@{}".format(options, user, ip) #FIXME: Is there a better way to find out if a ssh connection was successfully # established? status = subprocess.call(cmd, shell=True) if status == 255: self.counted(step, "connection refused! Retrying...") time.sleep(self.get("global/wait", 3)) self._run_ssh_cmd(name, user, ip, options, retry, step + 1)
def default_ssh(self): """returns the default ssh connection Returns a _Ssh object by using the default logins given from the definition file. (The default user defined or 'xii') Returns: A initialized and connected _Ssh object """ (user, host, password, key) = self.default_ssh_connection() if not user or not host: raise error.ConnError("Could not connect to ssh. " "Invalid configuration") return self.ssh(user, host, password, key)
def parse_url(cls, url): matcher = re.compile(r"qemu\+ssh:\/\/((.+)@)?(((.+)\.(.+))|(.+))\/system") matched = matcher.match(url) user = None if not matched: raise error.ConnError("Invalid connection URL specified. `{}` is " "invalid!".format(url)) host = matched.group(3) if matched.group(2): user = matched.group(2) return (user, host)
def jinja_read(tpl, store): try: path, filename = os.path.split(tpl) from_file = jinja2.FileSystemLoader(path) env = jinja2.Environment(loader=from_file, undefined=jinja2.StrictUndefined) yml = env.get_template(filename).render(store.values()) return yaml.load(yml) except IOError: raise error.ConnError("Could not open definition: " "No such file or directory") except yaml.YAMLError as err: raise error.ValidatorError("error parsing definition, {}".format(err)) except jinja2.exceptions.UndefinedError as err: raise error.ValidatorError("error parsing definition, {}".format(err))
def domain_get_ip(self, domain_name, retry=20, wait=3, quiet=False): """fetch ipv4 for a given domain name Args: domain_name: Domain name retry: n times until exception wait: n seconds between each cycle Returns: Returns the bound ipv4 address """ domain = self.get_domain(domain_name) nets = [] if not domain.isActive(): return None for i in range(retry): if not quiet: self.counted( i, "fetching ip address from {}...".format(domain_name)) nets = domain.interfaceAddresses( libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE) if len(nets): break sleep(wait) if not len(nets): raise error.ConnError( "Could not fetch ip address for {}. Giving up!".format( domain_name)) net = nets.itervalues().next() if not len(net['addrs']): return False return net['addrs'][0]['addr']
def ssh(self): if self._ssh: return self._ssh for i in range(self._retry): self.counted(i, "connecting to host {}...".format(self._host)) try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(self._host, username=self._user) self._ssh = client sleep(self._wait) self.say("connected!") return self._ssh except (paramiko.BadHostKeyException, paramiko.AuthenticationException, paramiko.SSHException, socket.error) as e: sleep(self._wait) raise error.ConnError("Could not connect to {}@{}. Giving up!" .format(self._user, self._host))
def _error_handler(_, err): if err[3] != libvirt.VIR_ERR_ERROR: self.warn("Non-error from libvirt: '{}'".format(err[2])) else: raise error.ConnError("[libvirt] {}".format(err[2]))