コード例 #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
ファイル: connect.py プロジェクト: minqf/ovirt-engine-cli
    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))