def __register_dfsm_callbacks(self): """ registers StateMachine events callbacks """ StateMachine.add_callback(DFSAState.UNAUTHORIZED, self.__on_unauthorized_callback) StateMachine.add_callback(DFSAState.COMMUNICATION_ERROR, self.__on_communication_error_callback) StateMachine.add_callback(DFSAState.DISCONNECTED, self.__on_disconnected_callback) StateMachine.add_callback(DFSAState.CONNECTED, self.__on_connected_callback) StateMachine.add_callback(DFSAState.EXITING, self.__on_exiting_callback)
def execute(self): connection = self.context.connection if connection is None: self.error(Messages.Error.NOT_CONNECTED) return try: StateMachine.disconnecting() # @UndefinedVariable connection.disconnect() self.context.status = ExecutionContext.OK except Exception: # TODO: consider rollback on disconnect failure (StateMachine.rollback()) self.context.status = ExecutionContext.COMMAND_ERROR finally: self.context._clean_settings() self.context.connection = None StateMachine.disconnected() # @UndefinedVariable
def __on_exiting_callback(self, **kwargs): """ triggered when StateMachine.EXITING state is acquired """ if StateMachine.get_origin_state() != DFSAState.DISCONNECTED and self.context.connection: self.do_disconnect("") else: self.do_echo("\n") # break shell prompt in to bash shell
def do_exit(self, args): """\ == Usage == exit == Description == Exists shell by /exit/ command. == Examples == exit """ self.onExit.fire() StateMachine.exiting() # @UndefinedVariable sys.exit(0)
def do_EOF(self, line): """\ == Usage == Ctrl+D == Description == Exists shell by /Ctrl+D/ sequence. == Examples == Ctrl+D """ self.onExit.fire() StateMachine.exiting() # @UndefinedVariable return True
def __on_exiting_callback(self, **kwargs): """ triggered when StateMachine.EXITING state is acquired """ if StateMachine.get_origin_state() != DFSAState.DISCONNECTED \ and \ self.context.connection: self.do_disconnect('') else: self.do_echo("\n") # break shell prompt in to bash shell
def onecmd(self, s): if not s.startswith('#'): command = s.split(' ')[0] if command == '' and not self.__input_buffer: pass elif self.context.connection == None and \ command.strip() not in EngineShell.OFF_LINE_CONTENT: self._error(Messages.Error.INVALID_COMMAND % command) else: try: if s.endswith('\\') and s != 'EOF': self.__set_prompt(mode=PromptMode.Multiline) self.__input_buffer += ' ' + s.replace('\\', '') \ if not s.startswith(' ') and self.__input_buffer != ''\ else s.replace('\\', '') return elif self.__input_buffer != '' and s != 'EOF': self.__input_buffer += ' ' + s \ if not s.startswith(' ') else s s = self.__input_buffer self.__input_buffer = '' self.__set_prompt(mode=PromptMode.Original) return cmd.Cmd.onecmd(self, s) finally: if self.context.status == \ self.context.SYNTAX_ERROR \ or \ self.context.status == \ self.context.COMMAND_ERROR \ or \ self.context.status == \ self.context.UNKNOWN_ERROR: self.onError.fire() elif self.context.status == \ self.context.COMMUNICATION_ERROR: self.__last_status = self.context.status self.onError.fire() StateMachine.communication_error( ) # @UndefinedVariable elif self.context.status == \ self.context.AUTHENTICATION_ERROR: self.__last_status = self.context.status self.onError.fire() StateMachine.unauthorized() # @UndefinedVariable elif self.__last_status <> -1 and \ ( ( self.__last_status == self.context.COMMUNICATION_ERROR and StateMachine.get_current_state() != DFSAState.COMMUNICATION_ERROR ) or \ ( self.__last_status == self.context.AUTHENTICATION_ERROR and StateMachine.get_current_state() != DFSAState.UNAUTHORIZED ) ): self.__set_prompt(mode=PromptMode.Original) self.__last_status = -1
def onecmd(self, s): if not s.startswith("#"): command = s.split(" ")[0] if command == "" and not self.__input_buffer: pass elif self.context.connection == None and command.strip() not in EngineShell.OFF_LINE_CONTENT: self._error(Messages.Error.INVALID_COMMAND % command) else: try: if s.endswith("\\") and s != "EOF": self.__set_prompt(mode=PromptMode.Multiline) self.__input_buffer += ( " " + s.replace("\\", "") if not s.startswith(" ") and self.__input_buffer != "" else s.replace("\\", "") ) return elif self.__input_buffer != "" and s != "EOF": self.__input_buffer += " " + s if not s.startswith(" ") else s s = self.__input_buffer self.__input_buffer = "" self.__set_prompt(mode=PromptMode.Original) return cmd.Cmd.onecmd(self, s) finally: if ( self.context.status == self.context.SYNTAX_ERROR or self.context.status == self.context.COMMAND_ERROR or self.context.status == self.context.UNKNOWN_ERROR ): self.onError.fire() elif self.context.status == self.context.COMMUNICATION_ERROR: self.__last_status = self.context.status self.onError.fire() StateMachine.communication_error() # @UndefinedVariable elif self.context.status == self.context.AUTHENTICATION_ERROR: self.__last_status = self.context.status self.onError.fire() StateMachine.unauthorized() # @UndefinedVariable elif self.__last_status <> -1 and ( ( self.__last_status == self.context.COMMUNICATION_ERROR and StateMachine.get_current_state() != DFSAState.COMMUNICATION_ERROR ) or ( self.__last_status == self.context.AUTHENTICATION_ERROR and StateMachine.get_current_state() != DFSAState.UNAUTHORIZED ) ): self.__set_prompt(mode=PromptMode.Original) self.__last_status = -1
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))
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)
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))
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
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()