Beispiel #1
0
def all_nodes_by_role_and_id():
    by_id, by_roles = tee(all_nodes())
    all_by_id = {o['id']: o for o in by_id}
    all_by_role = {
        r: list(x)
        for r, x in groupby(sorted(by_roles, key=lambda o: o['role']),
                            key=lambda o: o['role'])
    }
    for loc in ['/', '/serfnode']:
        with atomic_write(os.path.join(loc, 'serfnodes_by_id.json')) as f:
            json.dump(all_by_id, f)
        with atomic_write(os.path.join(loc, 'serfnodes_by_role.json')) as f:
            json.dump(all_by_role, f)
Beispiel #2
0
def launch(name, args, pos=None):
    if pos is not None and pos > 0:
        # wait for file /pos_{pos}
        wait_for_files('/pos_{}'.format(pos))
    try:
        cid = open('/child_{}'.format(name)).read().strip()
    except IOError:
        cid = name
    try:
        os.unlink('/child_{}'.format(name))
    except OSError:
        pass
    try:
        # first try to start the container, if it exists
        docker_utils.client.start(cid)
        return cid
    except docker_utils.dockerpy.errors.APIError:
        pass
    # if start fails, just do a 'run'
    args.insert(0, '--cidfile=/child_{}'.format(name))
    cid = docker_utils.docker('run', '-d',
                              '--volumes-from={}'.format(socket.gethostname()),
                              *args)
    if pos is not None:
        # touch file /pos_{pos+1}
        with atomic_write('/pos_{}'.format(pos + 1)) as f:
            f.write(str(pos + 1))
    return cid
Beispiel #3
0
def launch(name, args, pos=None):
    if pos is not None and pos > 0:
        # wait for file /pos_{pos}
        wait_for_files('/pos_{}'.format(pos))
    try:
        cid = open('/child_{}'.format(name)).read().strip()
    except IOError:
        cid = name
    try:
        os.unlink('/child_{}'.format(name))
    except OSError:
        pass
    try:
        # first try to start the container, if it exists
        docker_utils.client.start(cid)
        return cid
    except docker_utils.dockerpy.errors.APIError:
        pass
    # if start fails, just do a 'run'
    args.insert(0, '--cidfile=/child_{}'.format(name))
    cid = docker_utils.docker(
        'run', '-d',
        '--volumes-from={}'.format(socket.gethostname()),
        *args)
    if pos is not None:
        # touch file /pos_{pos+1}
        with atomic_write('/pos_{}'.format(pos+1)) as f:
            f.write(str(pos+1))
    return cid
def handler(obj):
    try:
        old = json.load(open('/children_by_name.json'))
    except IOError:
        old = {}
    wait_for_files('/agent_up')
    new = get_info(obj)
    parent = docker_utils.client.inspect_container(socket.gethostname())
    new['inspect']['NetworkSettings'] = parent['NetworkSettings']
    name = new['inspect']['Name'][1:]
    old[name] = new
    with atomic_write('/children_by_name.json') as f:
        json.dump(old, f)
Beispiel #5
0
def supervisor_install(block, **kwargs):
    """Update supervisor with `block` config.

    - `block` is the name to a .conf template file (in directory
      `/programs`)
    - `kwargs` are the key/values to use in the template

    """
    conf_filename = '{}.conf'.format(kwargs['target'])
    template = env.get_template(block)
    kwargs.update({
        'DOCKER': docker_utils.DOCKER,
        'DOCKER_SOCKET': docker_utils.DOCKER_SOCKET,
        'DOCKER_RUN': docker_utils.DOCKER_RUN})
    conf = template.render(kwargs)
    with atomic_write(os.path.join(
            '/etc/supervisor/conf.d', conf_filename)) as f:
        f.write(conf)
Beispiel #6
0
def supervisor_install(block, **kwargs):
    """Update supervisor with `block` config.

    - `block` is the name to a .conf template file (in directory
      `/programs`)
    - `kwargs` are the key/values to use in the template

    """
    conf_filename = '{}.conf'.format(kwargs['target'])
    template = env.get_template(block)
    kwargs.update({
        'DOCKER': docker_utils.DOCKER,
        'DOCKER_SOCKET': docker_utils.DOCKER_SOCKET,
        'DOCKER_RUN': docker_utils.DOCKER_RUN
    })
    conf = template.render(kwargs)
    with atomic_write(os.path.join('/etc/supervisor/conf.d',
                                   conf_filename)) as f:
        f.write(conf)
Beispiel #7
0
def notify():
    with atomic_write('/agent_up') as f:
        f.write('serf up')
Beispiel #8
0
def save_me(loc):
    with atomic_write(loc) as f:
        json.dump(get_info(socket.gethostname()), f)
Beispiel #9
0
def notify():
    with atomic_write('/agent_up') as f:
        f.write('serf up')