示例#1
0
 def __init__(self, params):
     self.virsh_exec = virsh.VIRSH_EXEC
     self.sasl_user = params.get('sasl_user')
     self.sasl_pwd = params.get('sasl_pwd')
     self.remote_ip = params.get('remote_ip')
     self.remote_user = params.get('remote_user')
     self.remote_pwd = params.get('remote_pwd')
     self.remote_auth = False
     if self.remote_ip:
         self.remote_auth = True
     super(VirshSessionSASL,
           self).__init__(virsh_exec=self.virsh_exec,
                          remote_ip=self.remote_ip,
                          remote_user=self.remote_user,
                          remote_pwd=self.remote_pwd,
                          ssh_remote_auth=self.remote_auth,
                          auto_close=True,
                          check_libvirtd=False)
     self.sendline('connect')
     self.sendline(self.sasl_user)
     self.sendline(self.sasl_pwd)
     # make sure session is connected successfully
     if self.cmd_status('list', timeout=60) != 0:
         logging.debug("Persistent virsh session is not responding, "
                       "libvirtd may be dead.")
         raise aexpect.ShellStatusError(virsh.VIRSH_EXEC, 'list')
示例#2
0
    def __init__(self,
                 virsh_exec=None,
                 uri=None,
                 a_id=None,
                 prompt=r"virsh\s*\#\s*"):
        """
        Initialize virsh session server, or client if id set.

        @param: virsh_exec: path to virsh executable
        @param: uri: uri of libvirt instance to connect to
        @param: id: ID of an already running server, if accessing a running
                server, or None if starting a new one.
        @param prompt: Regular expression describing the shell's prompt line.
        """

        self.uri = uri

        if self.uri:
            virsh_exec += " -c '%s'" % self.uri

        # aexpect tries to auto close session because no clients connected yet
        aexpect.ShellSession.__init__(self,
                                      virsh_exec,
                                      a_id,
                                      prompt=prompt,
                                      auto_close=False)
        # fail if libvirtd is not running
        if self.cmd_status('list', timeout=10) != 0:
            raise aexpect.ShellStatusError(virsh_exec, 'list')
示例#3
0
    def __init__(self,
                 disk_img=None,
                 ro_mode=False,
                 libvirt_domain=None,
                 inspector=False,
                 uri=None):
        super(GuestfishPersistent,
              self).__init__(disk_img, ro_mode, libvirt_domain, inspector, uri)
        if self.get('session_id') is None:
            # set_uri does not call when INITIALIZED = False
            # and no session_id passed to super __init__
            self.new_session()

        # Check whether guestfish session is prepared.
        guestfs_session = self.open_session()
        if guestfs_session.cmd_status('is-ready', timeout=60) != 0:
            logging.debug("Persistent guestfish session is not responding.")
            raise aexpect.ShellStatusError(self.lgf_exec, 'is-ready')
示例#4
0
    def __init__(self,
                 virtadmin_exec=None,
                 uri=None,
                 a_id=None,
                 prompt=r"virt-admin\s*[\#\>]\s*",
                 remote_ip=None,
                 remote_user=None,
                 remote_pwd=None,
                 ssh_remote_auth=False,
                 readonly=False,
                 unprivileged_user=None,
                 auto_close=False,
                 check_libvirtd=True):
        """
        Initialize virtadmin session server, or client if id set.

        :param virtadmin_exec: path to virtadmin executable
        :param uri: uri of libvirt instance to connect to
        :param id: ID of an already running server, if accessing a running
                server, or None if starting a new one.
        :param prompt: Regular expression describing the shell's prompt line.
        :param remote_ip: Hostname/IP of remote system to ssh into (if any)
        :param remote_user: Username to ssh in as (if any)
        :param remote_pwd: Password to use, or None for host/pubkey
        :param auto_close: Param to init ShellSession.
        :param ssh_remote_auth: ssh to remote first.(VirtadminConnectBack).
                                Then execute virtadmin commands.

        Because the VirtadminSession is designed for class VirtadminPersistent, so
        the default value of auto_close is False, and we manage the reference
        to VirtadminSession in VirtadminPersistent manually with counter_increase and
        counter_decrease. If you really want to use it directly over VirtadminPe-
        rsistent, please init it with auto_close=True, then the session will
        be closed in __del__.

            * session = VirtadminSession(virtadmin.VIRSH_EXEC, auto_close=True)
        """

        self.uri = uri
        self.remote_ip = remote_ip
        self.remote_user = remote_user
        self.remote_pwd = remote_pwd

        # Special handling if setting up a remote session
        if ssh_remote_auth:  # remote to remote
            logging.error("remote session is not supported by virt-admin yet.")
            if remote_pwd:
                pref_auth = "-o PreferredAuthentications=password"
            else:
                pref_auth = "-o PreferredAuthentications=hostbased,publickey"
            # ssh_cmd is not None flags this as remote session
            ssh_cmd = ("ssh -o UserKnownHostsFile=/dev/null %s -p %s %s@%s" %
                       (pref_auth, 22, self.remote_user, self.remote_ip))
            if uri:
                self.virtadmin_exec = ("%s \"%s -c '%s'\"" %
                                       (ssh_cmd, virtadmin_exec, self.uri))
            else:
                self.virtadmin_exec = ("%s \"%s\"" % (ssh_cmd, virtadmin_exec))
        else:  # setting up a local session or re-using a session
            self.virtadmin_exec = virtadmin_exec
            if self.uri:
                self.virtadmin_exec += " -c '%s'" % self.uri
            ssh_cmd = None  # flags not-remote session

        if unprivileged_user:
            self.virtadmin_exec = "su - %s -c '%s'" % (unprivileged_user,
                                                       self.virtadmin_exec)

        # aexpect tries to auto close session because no clients connected yet
        aexpect.ShellSession.__init__(self,
                                      self.virtadmin_exec,
                                      a_id,
                                      prompt=prompt,
                                      auto_close=auto_close)

        # Handle remote session prompts:
        # 1.remote to remote with ssh
        # 2.local to remote with "virtadmin -c uri"
        if ssh_remote_auth or self.uri:
            # Handle ssh / password prompts
            remote.handle_prompts(self,
                                  self.remote_user,
                                  self.remote_pwd,
                                  prompt,
                                  debug=True)

        # fail if libvirtd is not running
        if check_libvirtd:
            if self.cmd_status('uri', timeout=60) != 0:
                logging.debug(
                    "Persistent virt-admin session is not responding, "
                    "libvirtd may be dead.")
                self.auto_close = True
                raise aexpect.ShellStatusError(virtadmin_exec, 'uri')