Example #1
0
    def connect(self):
        """
        Connect to the PO Project API service
        """
        # Open client
        self.con = GestusClient(self.args.host,
                                (self.args.user, self.args.password))

        # Connect to the service
        try:
            self.con.connect()
        except (HTTPError, ConnectionError, InvalidSchema) as e:
            import traceback
            top = traceback.extract_stack()[-1]
            self.root_logger.error("%s: %s", type(e).__name__, e)
            raise CommandError('Error exit')
Example #2
0
    def connect(self):
        """
        Connect to the PO Project API service
        """
        # Open client
        self.con = GestusClient(self.args.host, (self.args.user, self.args.password))

        # Connect to the service
        try:
            self.con.connect()
        except (HTTPError, ConnectionError, InvalidSchema) as e:
            import traceback

            top = traceback.extract_stack()[-1]
            self.root_logger.error("%s: %s", type(e).__name__, e)
            raise CommandError("Error exit")
Example #3
0
class CliInterfaceBase(object):
    """
    The common base CLI interface to use within a command function
    
    It takes care of the logging, timer, config and service connection, also embed some common args validate
    """

    def __init__(self, args):
        self.args = args

        self.config = None
        self.con = None

        self.starttime = datetime.datetime.now()
        # Init, load and builds
        self.root_logger = logging_handler.init_logging(self.args.loglevel.upper(), logfile=self.args.logfile)

    def open_config(self):
        # Open config file if exists
        self.config = GestusConfig()
        self.config.open(self.args.config)
        configdatas = self.config.get_datas()
        # Merge config in arguments for empty argument only
        for item in GestusConfig.options:
            if item in configdatas:
                val = getattr(self.args, item, None) or configdatas[item]
                setattr(self.args, item, val)
                self.root_logger.debug("Set config value '%s' to: %s", item, val)

    def save_config(self):
        # Save config with current values
        if self.config and not self.args.passive:
            self.root_logger.debug("Saving config")
            values = {}
            for item in GestusConfig.options:
                if hasattr(self.args, item):
                    values[item] = getattr(self.args, item)

            self.config.set_datas(values)
            self.config.save()

    def connect(self):
        """
        Connect to the PO Project API service
        """
        # Open client
        self.con = GestusClient(self.args.host, (self.args.user, self.args.password))

        # Connect to the service
        try:
            self.con.connect()
        except (HTTPError, ConnectionError, InvalidSchema) as e:
            import traceback

            top = traceback.extract_stack()[-1]
            self.root_logger.error("%s: %s", type(e).__name__, e)
            raise CommandError("Error exit")

    def close(self):
        if self.args.timer:
            endtime = datetime.datetime.now()
            self.root_logger.info("Done in %s", str(endtime - self.starttime))

    def validate_authentication_args(self):
        # Validate required arguments to connect
        if not self.args.user or not self.args.password or not self.args.host:
            self.root_logger.error("'user', 'password' and 'hostname' are required arguments to connect to the service")
            raise CommandError("Error exit")

    def validate_website_args(self):
        # Validate required argument to register
        if not self.args.name or not self.args.url or not self.args.env:
            self.root_logger.error("'name', 'url' and 'env' are required arguments to register the current environment")
            raise CommandError("Error exit")

    def validate_eggs_args(self):
        # Validate eggs path argument
        if self.args.eggs and not os.path.exists(self.args.eggs):
            self.root_logger.error("The given eggs path does not exists")
            raise CommandError("Error exit")
        if self.args.eggs and not os.path.isdir(self.args.eggs):
            self.root_logger.error("The given eggs path is not a directory")
            raise CommandError("Error exit")

    def validate_env_args(self):
        # Validate environment kind
        if self.args.env not in ENVIRONMENT_KIND_KEYS:
            self.root_logger.error(
                "Invalid environment given '%s'. Valid choices are: %s", self.args.env, ", ".join(ENVIRONMENT_KIND_KEYS)
            )
            raise CommandError("Error exit")

    def validate_url_args(self):
        # Validate url
        if self.args.url.find(" ") > -1:
            self.root_logger.warning(
                "Seems you tried to define multiple url separated with spaces, Gestus only accepts one url for an environment, the value has been splitted to get the first one item"
            )
            self.args.url = self.args.url.split(" ")[0]
Example #4
0
class CliInterfaceBase(object):
    """
    The common base CLI interface to use within a command function
    
    It takes care of the logging, timer, config and service connection, also embed some common args validate
    """
    def __init__(self, args):
        self.args = args

        self.config = None
        self.con = None

        self.starttime = datetime.datetime.now()
        # Init, load and builds
        self.root_logger = logging_handler.init_logging(
            self.args.loglevel.upper(), logfile=self.args.logfile)

    def open_config(self):
        # Open config file if exists
        self.config = GestusConfig()
        self.config.open(self.args.config)
        configdatas = self.config.get_datas()
        # Merge config in arguments for empty argument only
        for item in GestusConfig.options:
            if item in configdatas:
                val = getattr(self.args, item, None) or configdatas[item]
                setattr(self.args, item, val)
                self.root_logger.debug("Set config value '%s' to: %s", item,
                                       val)

    def save_config(self):
        # Save config with current values
        if self.config and not self.args.passive:
            self.root_logger.debug("Saving config")
            values = {}
            for item in GestusConfig.options:
                if hasattr(self.args, item):
                    values[item] = getattr(self.args, item)

            self.config.set_datas(values)
            self.config.save()

    def connect(self):
        """
        Connect to the PO Project API service
        """
        # Open client
        self.con = GestusClient(self.args.host,
                                (self.args.user, self.args.password))

        # Connect to the service
        try:
            self.con.connect()
        except (HTTPError, ConnectionError, InvalidSchema) as e:
            import traceback
            top = traceback.extract_stack()[-1]
            self.root_logger.error("%s: %s", type(e).__name__, e)
            raise CommandError('Error exit')

    def close(self):
        if self.args.timer:
            endtime = datetime.datetime.now()
            self.root_logger.info('Done in %s', str(endtime - self.starttime))

    def validate_authentication_args(self):
        # Validate required arguments to connect
        if not self.args.user or not self.args.password or not self.args.host:
            self.root_logger.error(
                "'user', 'password' and 'hostname' are required arguments to connect to the service"
            )
            raise CommandError('Error exit')

    def validate_website_args(self):
        # Validate required argument to register
        if not self.args.name or not self.args.url or not self.args.env:
            self.root_logger.error(
                "'name', 'url' and 'env' are required arguments to register the current environment"
            )
            raise CommandError('Error exit')

    def validate_eggs_args(self):
        # Validate eggs path argument
        if self.args.eggs and not os.path.exists(self.args.eggs):
            self.root_logger.error("The given eggs path does not exists")
            raise CommandError('Error exit')
        if self.args.eggs and not os.path.isdir(self.args.eggs):
            self.root_logger.error("The given eggs path is not a directory")
            raise CommandError('Error exit')

    def validate_env_args(self):
        # Validate environment kind
        if self.args.env not in ENVIRONMENT_KIND_KEYS:
            self.root_logger.error(
                "Invalid environment given '%s'. Valid choices are: %s",
                self.args.env, ', '.join(ENVIRONMENT_KIND_KEYS))
            raise CommandError('Error exit')

    def validate_url_args(self):
        # Validate url
        if self.args.url.find(' ') > -1:
            self.root_logger.warning(
                "Seems you tried to define multiple url separated with spaces, Gestus only accepts one url for an environment, the value has been splitted to get the first one item"
            )
            self.args.url = self.args.url.split(' ')[0]