Exemplo n.º 1
0
def go(squadron_dir, squadron_state_dir = None, config_file = None, node_name = None, status_server = None,
        dont_rollback = False, force = False, dry_run = True):
    """
    Gets the config and applies it if it's not a dry run.

    Keyword arguments:
        squadron_dir -- where the Squadron description dir is
        squadron_state_dir -- where Squadron should store its state between runs
        config_file -- overall config file location
        node_name -- what this node is called
        status_server -- the hostname (and optionally port) of the HTTPS server to
            send status to
        dont_rollback -- if true, doesn't automatically rollback to the previous version
        force -- treat all files as created, always deploy
        dry_run -- whether or not to apply changes
    """
    send_status = False
    try:
        first_squadron_dir = get_squadron_dir(squadron_dir, None)

        config = parse_config(first_squadron_dir, log, config_file)
        log.debug("Got config {}".format(config))

        squadron_dir = get_squadron_dir(squadron_dir, config)

        if squadron_state_dir is None:
            squadron_state_dir = config['statedir']
        if node_name is None:
            node_name = config['nodename']

        if config['send_status'].lower() == 'true':
            send_status = True

            if status_server is None:
                status_server = config['status_host']

            status_apikey = config['status_apikey']
            status_secret = config['status_secret']
            log.info("Sending status to {} with {}/{}".format(status_server, status_apikey, status_secret))

        info = _run_squadron(squadron_dir, squadron_state_dir, node_name, dont_rollback, force, dry_run)
    except UserException as e:
        # This is a user error, don't print a stack trace
        log.error(e.message)
    except Exception as e:
        if send_status and not dry_run:
            status.report_status(requests.session(), status_server, status_apikey, status_secret, str(uuid.uuid4()),
                    True, status='ERROR', hostname=node_name, info={'info':True, 'message':str(e)})
        log.exception('Caught exception')
        raise e
    else: #executes on no exception
        if send_status and not dry_run and info:
            status.report_status(requests.session(), status_server, status_apikey, status_secret, str(uuid.uuid4()),
                True, status='OK', hostname=node_name, info=info)
Exemplo n.º 2
0
def daemonize(squadron_dir, config_file, polltime, repo, node_name, exit_loop=threading.Event()):
    """
    Runs squadron every polltime minutes.

    Keyword arguments:
        squadron_dir -- squadron directory
        config_file -- config file or None for defaults
        polltime -- minutes to sleep in between runs
        repo -- source code for the squadron_dir for updating
        node_name -- override for nodename
        loglevel -- how much to log
    """
    log.debug('entering daemon.daemonize %s',
            [squadron_dir, config_file, polltime, repo])

    first_squadron_dir = main.get_squadron_dir(squadron_dir, None)
    parsed_config = parse_config(first_squadron_dir, log, config_file)

    print "Daemon is using loglevel: " + str(log.getEffectiveLevel())
    if not polltime:
        polltime = float(parsed_config['polltime'])
    else:
        polltime = float(polltime)

    squadron_dir = main.get_squadron_dir(squadron_dir, parsed_config)
    log.info('Using Squadron dir: %s', squadron_dir)

    if not os.path.exists(squadron_dir):
        log.info('Cloning %s into %s', repo, squadron_dir)
        repo = Repo.clone_from(repo, squadron_dir)
    else:
        repo = Repo(squadron_dir)

    wakeup = threading.Event()
    webhook_thread = None
    if 'webhook' in parsed_config:
        run_webhook = parsed_config['webhook']
        if bool(run_webhook):
            user = parsed_config['webhook_username']
            password = parsed_config['webhook_password']

            app = notify.webhook.WebHookHandler(user, password,
                    lambda x: wakeup.set(), log)

            listen = parsed_config['webhook_listen']
            port = parsed_config['webhook_port']

            webhook_thread, webhook_server = notify.server.get_server(listen,
                    port, app.application)

            log.info('Starting webhook server on %s:%s', listen, port)
            webhook_thread.start()

    while not exit_loop.is_set():
        start_time = time.time()

        try:
            git = repo.git
            log.debug('Doing git pull --rebase')
            out = git.pull('--rebase')
            log.debug('Git pull returned: %s', out)

            ret = main.go(squadron_dir, node_name=node_name,
                    config_file=config_file, dry_run=False)
        except Exception as e:
            log.exception('Caught top level exception')

        end_time = time.time()

        log.debug('daemon is sleeping: %s', polltime)

        if end_time - start_time < 60:
            # Don't wake up too often
            wakeup.clear()

        if wakeup.wait(polltime):
            log.debug('Woken up early')
            wakeup.clear()

    if webhook_thread:
        log.info('Stopping webhook server')
        webhook_server.stop()
        log.debug('Joining webhook server thread')
        webhook_thread.join()
        log.info('Stopped webhook server')
Exemplo n.º 3
0
def daemonize(squadron_dir,
              config_file,
              polltime,
              repo,
              node_name,
              exit_loop=threading.Event()):
    """
    Runs squadron every polltime minutes.

    Keyword arguments:
        squadron_dir -- squadron directory
        config_file -- config file or None for defaults
        polltime -- seconds to sleep in between runs
        repo -- source code for the squadron_dir for updating
        node_name -- override for nodename
        loglevel -- how much to log
    """
    log.debug('entering daemon.daemonize %s',
              [squadron_dir, config_file, polltime, repo])

    first_squadron_dir = main.get_squadron_dir(squadron_dir, None)
    parsed_config = parse_config(first_squadron_dir, log, config_file)

    print "Daemon is using loglevel: " + str(log.getEffectiveLevel())
    if not polltime:
        polltime = float(parsed_config['polltime'])
    else:
        polltime = float(polltime)

    squadron_dir = main.get_squadron_dir(squadron_dir, parsed_config)
    log.info('Using Squadron dir: %s', squadron_dir)

    if not os.path.exists(squadron_dir):
        log.info('Cloning %s into %s', repo, squadron_dir)
        repo = Repo.clone_from(repo, squadron_dir)
    else:
        repo = Repo(squadron_dir)

    wakeup = threading.Event()
    webhook_thread = None
    if 'webhook' in parsed_config:
        run_webhook = parsed_config['webhook']
        if bool(run_webhook):
            user = parsed_config['webhook_username']
            password = parsed_config['webhook_password']

            app = notify.webhook.WebHookHandler(user, password,
                                                lambda x: wakeup.set(), log)

            listen = parsed_config['webhook_listen']
            port = parsed_config['webhook_port']

            webhook_thread, webhook_server = notify.server.get_server(
                listen, port, app.application)

            log.info('Starting webhook server on %s:%s', listen, port)
            webhook_thread.start()

    while not exit_loop.is_set():
        start_time = time.time()

        try:
            git = repo.git
            log.debug('Doing git pull --rebase')
            out = git.pull('--rebase')
            log.debug('Git pull returned: %s', out)

            ret = main.go(squadron_dir,
                          node_name=node_name,
                          config_file=config_file,
                          dry_run=False,
                          dont_rollback=True)
        except Exception as e:
            log.exception('Caught top level exception')

        end_time = time.time()

        log.debug('daemon is sleeping: %s', polltime)

        if end_time - start_time < 60:
            # Don't wake up too often
            wakeup.clear()

        if wakeup.wait(polltime):
            log.debug('Woken up early')
            wakeup.clear()

    if webhook_thread:
        log.info('Stopping webhook server')
        webhook_server.stop()
        log.debug('Joining webhook server thread')
        webhook_thread.join()
        log.info('Stopped webhook server')