Example #1
0
def ors_standalone(ctx, api_key, debug, host, port):
    from circuits import Manager, Debugger
    from circuits.web import Server

    set_logfile('/tmp', 'mini')
    set_verbosity(5)

    app = Manager()

    if debug:
        debugger = Debugger().register(app)

    ors_controller = RESTORSService(api_key=api_key, no_db=True).register(app)

    try:
        server = Server(
            (host, port),
            display_banner=False,
            secure=False,
        ).register(app)
    except PermissionError as e:
        if port <= 1024:
            log(
                "Could not open privileged port (%i), check permissions!" %
                port,
                e,
                lvl=critical,
            )
        else:
            log("Could not open port (%i):" % port, e, lvl=critical)
    except OSError as e:
        if e.errno == 98:
            log("Port (%i) is already opened!" % port, lvl=critical)
        else:
            log("Could not open port (%i):" % port, e, lvl=critical)

    app.run()
Example #2
0
    def _set_logger():
        if log_path is not None or log_file is not None:
            set_logfile(log_path, instance, log_file)

        if no_colors is False:
            set_color()
Example #3
0
def cli(
    ctx,
    instance,
    env,
    quiet,
    no_colors,
    console_level,
    file_level,
    no_log,
    log_path,
    log_file,
    dbhost,
    dbname,
    prefix_path,
    config_path,
    fat_logo,
):
    """Isomer Management Tool

    This tool supports various operations to manage Isomer instances.

    Most of the commands are grouped. To obtain more information about the
    groups' available sub commands/groups, try

    iso [group]

    To display details of a command or its subgroups, try

    iso [group] [subgroup] [..] [command] --help

    To get a map of all available commands, try

    iso cmdmap
    """

    ctx.obj["quiet"] = quiet

    def _set_verbosity():
        if quiet:
            console_setting = 100
        else:
            console_setting = int(
                console_level if console_level is not None else 20)

        if no_log:
            file_setting = 100
        else:
            file_setting = int(file_level if file_level is not None else 20)

        global_setting = min(console_setting, file_setting)
        set_verbosity(global_setting, console_setting, file_setting)

    def _set_logger():
        if log_path is not None or log_file is not None:
            set_logfile(log_path, instance, log_file)

        if no_colors is False:
            set_color()

    _set_verbosity()
    _set_logger()

    ctx.obj["instance"] = instance

    log("Running with Python",
        sys.version.replace("\n", ""),
        sys.platform,
        lvl=verbose)
    log("Interpreter executable:", sys.executable, lvl=verbose)

    set_etc_path(config_path)
    configuration = load_configuration()

    if configuration is not None:
        ctx.obj["config"] = configuration
    else:
        if ctx.invoked_subcommand not in ("version", "cmdmap"):
            log(
                "No configuration found. Most commands won't work. "
                "Use 'iso system configure' to generate a configuration.",
                lvl=warn)
        return

    set_prefix_path(configuration['meta']['prefix'])

    instances = load_instances()

    ctx.obj["instances"] = instances

    if instance not in instances:
        log(
            "No instance configuration called %s found! Using fresh defaults."
            % instance,
            lvl=warn,
        )
        instance_configuration = instance_template
    else:
        instance_configuration = instances[instance]

    if file_level is None and console_level is None:
        instance_log_level = int(instance_configuration["loglevel"])

        set_verbosity(instance_log_level, file_level=instance_log_level)
        log("Instance log level set to", instance_log_level, lvl=verbose)

    ctx.obj["instance_configuration"] = instance_configuration

    instance_environment = instance_configuration["environment"]

    if env is not None:
        if env == "current":
            ctx.obj["acting_environment"] = instance_environment
        elif env == "other":
            ctx.obj["acting_environment"] = ("blue" if instance_environment
                                             == "green" else "blue")
        else:
            ctx.obj["acting_environment"] = env
        env = ctx.obj["acting_environment"]
    else:
        env = instance_configuration["environment"]
        ctx.obj["acting_environment"] = None

    def get_environment_toggle(platform, toggles):
        """Checks well known methods to determine if the other environment should be
        booted instead of the default environment."""
        def temp_file_toggle():
            """Check by looking for a state file in /tmp"""

            state_filename = "/tmp/isomer_toggle_%s" % instance_configuration[
                "name"]
            log("Checking for override state file ", state_filename, lvl=debug)

            if os.path.exists(state_filename):
                log("Environment override state file found!", lvl=warn)
                return True
            else:
                log("Environment override state file not found", lvl=debug)
                return False

        def gpio_switch_toggle():
            """Check by inspection of a GPIO pin for a closed switch"""

            log("Checking for override GPIO switch on channel ",
                RPI_GPIO_CHANNEL,
                lvl=debug)

            if platform != "rpi":
                log(
                    "Environment toggle: "
                    "GPIO switch can only be handled on Raspberry Pi!",
                    lvl=critical)
                return False
            else:
                try:
                    import RPi.GPIO as GPIO
                except ImportError:
                    log(
                        "RPi Python module not found. "
                        "This only works on a Raspberry Pi!",
                        lvl=critical)
                    return False

                GPIO.setmode(GPIO.BOARD)
                GPIO.setup(RPI_GPIO_CHANNEL, GPIO.IN)

                state = GPIO.input(RPI_GPIO_CHANNEL) is True

                if state:
                    log("Environment override switch active!", lvl=warn)
                else:
                    log("Environment override switch not active", lvl=debug)

                return state

        toggle = False
        if "temp_file" in toggles:
            toggle = toggle or temp_file_toggle()
        if "gpio_switch" in toggles:
            toggle = toggle or gpio_switch_toggle()

        if toggle:
            log("Booting other Environment per user request.")
        else:
            log("Booting active environment", lvl=debug)

        return toggle

    #log(configuration['meta'], pretty=True)
    #log(instance_configuration, pretty=True)

    if get_environment_toggle(configuration["meta"]["platform"],
                              instance_configuration['environment_toggles']):
        if env == 'blue':
            env = 'green'
        else:
            env = 'blue'

    ctx.obj["environment"] = env

    if not fat_logo:
        log("<> Isomer", version_info, " [%s|%s]" % (instance, env), lvl=99)
    else:
        from isomer.misc import logo

        pad = len(logo.split("\n", maxsplit=1)[0])
        log(("Isomer %s" % version_info).center(pad), lvl=99)
        for line in logo.split("\n"):
            log(line, lvl=99)

    if dbname is None:
        dbname = instance_configuration["environments"][env]["database"]
        if dbname in ("", None) and ctx.invoked_subcommand in (
                "config",
                "db",
                "environment",
                "plugin",
        ):
            log(
                "Database for this instance environment is unset, "
                "you probably have to install the environment first.",
                lvl=warn,
            )

    if dbhost is None:
        dbhost = "%s:%i" % (
            instance_configuration["database_host"],
            instance_configuration["database_port"],
        )

    ctx.obj["dbhost"] = dbhost
    ctx.obj["dbname"] = dbname

    set_instance(instance, env, prefix_path)

    if log_path is None and log_file is None:
        log_path = get_log_path()

        set_logfile(log_path, instance, log_file)