Пример #1
0
    def execute(self):
        args = self.arguments
        settings = self.context.settings
        context = self.context

        MIN_FORCE_CREDENTIALS_CHECK_VERSION = ('00000003', '00000001',
                                               '00000000', '00000004')

        key_file = self.xNoneType(settings.get('ovirt-shell:key_file'))
        cert_file = self.xNoneType(settings.get('ovirt-shell:cert_file'))
        ca_file = self.xNoneType(settings.get('ovirt-shell:ca_file'))
        port = settings.get('ovirt-shell:port')
        timeout = settings.get('ovirt-shell:timeout')
        session_timeout = settings.get('ovirt-shell:session_timeout')
        renew_session = settings.get('ovirt-shell:renew_session')
        debug = settings.get('cli:debug')
        insecure = settings.get('ovirt-shell:insecure')
        dont_validate_cert_chain = settings.get(
            'ovirt-shell:dont_validate_cert_chain')
        filter_ = settings.get('ovirt-shell:filter')
        kerberos = settings.get('ovirt-shell:kerberos')

        if self.context.connection is not None and \
           self.context.status != self.context.COMMUNICATION_ERROR and \
           self.context.status != self.context.AUTHENTICATION_ERROR and \
           self.__test_connectivity():
            self.write(Messages.Warning.ALREADY_CONNECTED)
            return
        if len(args) == 3:
            url, username, password = args
        else:
            url = settings.get('ovirt-shell:url')
            if not url:
                self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE %
                           'url')
            if kerberos:
                username = None
                password = None
            else:
                username = settings.get('ovirt-shell:username')
                if not username:
                    self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE %
                               'username')
                password = settings.get('ovirt-shell:password')
                if not password:
                    self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE %
                               'password')

        if not self.is_valid_url(url):
            self.error(Messages.Error.INVALID_URL_SEGMENT % url)

        try:
            StateMachine.connecting()  # @UndefinedVariable

            self.context.set_connection(API(
                url=url,
                username=username,
                password=password,
                key_file=key_file,
                cert_file=cert_file,
                ca_file=ca_file,
                insecure=insecure,
                validate_cert_chain=not dont_validate_cert_chain,
                filter=filter_,
                port=port if port != -1 else None,
                timeout=timeout if timeout != -1 else None,
                session_timeout=session_timeout
                if session_timeout != -1 else None,
                renew_session=renew_session,
                debug=debug,
                kerberos=kerberos),
                                        url=url)

            if context.sdk_version < MIN_FORCE_CREDENTIALS_CHECK_VERSION:
                self.__test_connectivity()

            StateMachine.connected()  # @UndefinedVariable

        except RequestError, e:
            StateMachine.rollback()
            self.__cleanContext()
            if debug:
                self.error("[" + str(e.status) + '] - ' + str(e.reason) +
                           ', ' + str(e.detail))
            else:
                self.error("[" + str(e.status) + '] - ' + str(e.reason))
Пример #2
0
class ConnectCommand(OvirtCommand):

    name = 'connect'
    description = 'connect to a oVirt manager'
    args_check = (0, len(ConnectCmdShell.OPTIONS))
    valid_options = [('--' + item, str) for item in ConnectCmdShell.OPTIONS]

    helptext = """\
        == Usage ==

        connect [command options]

        == Description ==

        Connect to a oVirt manager. This command has two forms. In the first
        form, no arguments are provided, and the connection details are read
        from their respective configuration variables. In the second form,
        the connection details are provided as arguments.

        == Arguments ==

         * url               - The URL to connect to (http[s]://server[:port]/ovirt-engine/api).
         * [username]        - The user to connect as. (user@domain).
         * [password]        - The password to use.
         * [key-file]        - The client PEM key file to use.
         * [cert-file]       - The client PEM certificate file to use.
         * [ca-file]         - The server CA certificate file to use.
         * [filter]          - Enables user permission based filtering.
         * [insecure]        - Allow connecting to SSL sites without certificates.
         * [port]            - The port to use (if not specified in url).
         * [timeout]         - The request timeout.
         * [session-timeout] - The authentication session timeout in minutes (positive number).
         * [kerberos]        - Use Kerberos authentication.
        """

    def execute(self):
        args = self.arguments
        settings = self.context.settings
        context = self.context

        MIN_FORCE_CREDENTIALS_CHECK_VERSION = ('00000003', '00000001',
                                               '00000000', '00000004')

        key_file = self.xNoneType(settings.get('ovirt-shell:key_file'))
        cert_file = self.xNoneType(settings.get('ovirt-shell:cert_file'))
        ca_file = self.xNoneType(settings.get('ovirt-shell:ca_file'))
        port = settings.get('ovirt-shell:port')
        timeout = settings.get('ovirt-shell:timeout')
        session_timeout = settings.get('ovirt-shell:session_timeout')
        renew_session = settings.get('ovirt-shell:renew_session')
        debug = settings.get('cli:debug')
        insecure = settings.get('ovirt-shell:insecure')
        dont_validate_cert_chain = settings.get(
            'ovirt-shell:dont_validate_cert_chain')
        filter_ = settings.get('ovirt-shell:filter')
        kerberos = settings.get('ovirt-shell:kerberos')

        if self.context.connection is not None and \
           self.context.status != self.context.COMMUNICATION_ERROR and \
           self.context.status != self.context.AUTHENTICATION_ERROR and \
           self.__test_connectivity():
            self.write(Messages.Warning.ALREADY_CONNECTED)
            return
        if len(args) == 3:
            url, username, password = args
        else:
            url = settings.get('ovirt-shell:url')
            if not url:
                self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE %
                           'url')
            if kerberos:
                username = None
                password = None
            else:
                username = settings.get('ovirt-shell:username')
                if not username:
                    self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE %
                               'username')
                password = settings.get('ovirt-shell:password')
                if not password:
                    self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE %
                               'password')

        if not self.is_valid_url(url):
            self.error(Messages.Error.INVALID_URL_SEGMENT % url)

        try:
            StateMachine.connecting()  # @UndefinedVariable

            self.context.set_connection(API(
                url=url,
                username=username,
                password=password,
                key_file=key_file,
                cert_file=cert_file,
                ca_file=ca_file,
                insecure=insecure,
                validate_cert_chain=not dont_validate_cert_chain,
                filter=filter_,
                port=port if port != -1 else None,
                timeout=timeout if timeout != -1 else None,
                session_timeout=session_timeout
                if session_timeout != -1 else None,
                renew_session=renew_session,
                debug=debug,
                kerberos=kerberos),
                                        url=url)

            if context.sdk_version < MIN_FORCE_CREDENTIALS_CHECK_VERSION:
                self.__test_connectivity()

            StateMachine.connected()  # @UndefinedVariable

        except RequestError, e:
            StateMachine.rollback()
            self.__cleanContext()
            if debug:
                self.error("[" + str(e.status) + '] - ' + str(e.reason) +
                           ', ' + str(e.detail))
            else:
                self.error("[" + str(e.status) + '] - ' + str(e.reason))
        except NoCertificatesError:
            StateMachine.rollback()
            self.__cleanContext()
            self.error(Messages.Error.NO_CERTIFICATES)
Пример #3
0
    def execute(self):
        key_file = self.__option_or_setting('ke-file', 'ovirt-shell:key_file')
        cert_file = self.__option_or_setting('cert-file', 'ovirt-shell:cert_file')
        ca_file = self.__option_or_setting('ca-file', 'ovirt-shell:ca_file')
        port = self.__option_or_setting('port', 'ovirt-shell:port')
        timeout = self.__option_or_setting('timeout', 'ovirt-shell:timeout')
        session_timeout = self.__option_or_setting('session-timeout', 'ovirt-shell:session_timeout')
        renew_session = self.__option_or_setting(None, 'ovirt-shell:renew_session')
        debug = self.__option_or_setting(None, 'cli:debug')
        insecure = self.__option_or_setting('insecure', 'ovirt-shell:insecure')
        dont_validate_cert_chain = self.__option_or_setting(None, 'ovirt-shell:dont_validate_cert_chain')
        filter_ = self.__option_or_setting('filter', 'ovirt-shell:filter')
        kerberos = self.__option_or_setting('kerberos', 'ovirt-shell:kerberos')

        if self.context.connection is not None and \
           self.context.status != self.context.COMMUNICATION_ERROR and \
           self.context.status != self.context.AUTHENTICATION_ERROR and \
           self.__test_connectivity():
            self.write(
                   Messages.Warning.ALREADY_CONNECTED
            )
            return
        if len(self.arguments) == 3:
            url, username, password = self.arguments
        else:
            url = self.__option_or_setting('url', 'ovirt-shell:url')
            if url is None:
                self.error(
                       Messages.Error.MISSING_CONFIGURATION_VARIABLE % 'url'
                )
            if kerberos:
                username = None
                password = None
            else:
                username = self.__option_or_setting('user', 'ovirt-shell:username')
                if username is None:
                    self.error(
                        Messages.Error.MISSING_CONFIGURATION_VARIABLE % 'username'
                    )
                password = self.__option_or_setting('password', 'ovirt-shell:password')
                if password is None:
                    self.error(
                        Messages.Error.MISSING_CONFIGURATION_VARIABLE % 'password'
                    )

        if not self.is_valid_url(url):
            self.error(
               Messages.Error.INVALID_URL_SEGMENT % url
            )

        try:
            StateMachine.connecting()  # @UndefinedVariable

            self.context.set_connection (
                     API(
                         url=url,
                         username=username,
                         password=password,
                         key_file=key_file,
                         cert_file=cert_file,
                         ca_file=ca_file,
                         insecure=insecure,
                         validate_cert_chain=not dont_validate_cert_chain,
                         filter=filter_,
                         port=port if port != -1 else None,
                         timeout=timeout if timeout != -1 else None,
                         session_timeout=session_timeout if session_timeout != -1 else None,
                         renew_session=renew_session,
                         debug=debug,
                         kerberos=kerberos
                     ),
                     url=url
             )

            StateMachine.connected()  # @UndefinedVariable

        except RequestError, e:
            StateMachine.rollback()
            self.__cleanContext()
            if debug:
                self.error("[" + str(e.status) + '] - ' + str(e.reason) + ', ' + str(e.detail))
            else:
                self.error("[" + str(e.status) + '] - ' + str(e.reason))
Пример #4
0
            StateMachine.connected()  # @UndefinedVariable

        except RequestError, e:
            StateMachine.rollback()
            self.__cleanContext()
            if debug:
                self.error("[" + str(e.status) + '] - ' + str(e.reason) +
                           ', ' + str(e.detail))
            else:
                self.error("[" + str(e.status) + '] - ' + str(e.reason))
        except NoCertificatesError:
            StateMachine.rollback()
            self.__cleanContext()
            self.error(Messages.Error.NO_CERTIFICATES)
        except ConnectionError, e:
            StateMachine.rollback()
            self.__cleanContext()
            self.context._clean_settings()
            self.error(str(e))
        except TypeError, e:
            StateMachine.rollback()
            self.__cleanContext()
            option, value, expected = self.__normalize_typeerror(e)
            self.error(Messages.Error.INVALID_ARGUMENT_TYPE %
                       (option, value, expected))
        except Exception, e:
            StateMachine.rollback()
            self.__cleanContext()
            self.error(str(e))
        finally:
            # do not log connect command details as it may be
Пример #5
0
            StateMachine.connected()  # @UndefinedVariable

        except RequestError, e:
            StateMachine.rollback()
            self.__cleanContext()
            if debug:
                self.error("[" + str(e.status) + '] - ' + str(e.reason) + ', ' + str(e.detail))
            else:
                self.error("[" + str(e.status) + '] - ' + str(e.reason))
        except NoCertificatesError:
            StateMachine.rollback()
            self.__cleanContext()
            self.error(Messages.Error.NO_CERTIFICATES)
        except ConnectionError, e:
            StateMachine.rollback()
            self.__cleanContext()
            self.context._clean_settings()
            self.error(str(e))
        except TypeError, e:
            StateMachine.rollback()
            self.__cleanContext()
            option, value, expected = self.__normalize_typeerror(e)
            self.error(
                   Messages.Error.INVALID_ARGUMENT_TYPE % (
                               option,
                               value,
                               expected)
            )
        except Exception, e:
            StateMachine.rollback()