Example #1
0
def delete_env(env_id):
    project_name = get_env_project_name(env_id)
    project_file_name = './docker-compose-{}.yml'.format(project_name)
    project = get_project('./', project_name, project_file_name)
    ps = ps_(project)
    if is_project_running(ps):
        project.down(1, True, remove_orphans=True)
    wait_time = 0
    while is_project_running(ps) and wait_time < 120:
        print('Waiting for environment {} deletion...'.format(env_id))
        wait_time = wait_time + 15
        time.sleep(15)
        ps = ps_(project)
    os.remove(project_file_name)
Example #2
0
def delete_provisioner(env_id):
    project_name = get_provisioner_project_name(env_id)
    project_file_name = './docker-compose-{}.yml'.format(project_name)
    project = get_project('./', project_name, project_file_name)
    ps = ps_(project)
    if is_project_running(ps):
        project.down(1, False, remove_orphans=True)
    os.remove(project_file_name)
Example #3
0
def is_env_deployed(env_id):
    project_name = get_env_project_name(env_id)
    project_file_name = './docker-compose-{}.yml'.format(project_name)
    if os.path.isfile(project_file_name):
        project = get_project('./', project_name, project_file_name)
        ps = ps_(project)
        return is_project_starting(ps) or is_project_running(ps)
    else:
        return False
Example #4
0
def deploy_env(up_request, environment):
    print('Deploying environment {}...'.format(environment['id']))
    minienv_dict = {}
    minienv_json = None
    try:
        response = urllib2.urlopen('{}/raw/master/minienv.json'.format(
            up_request['repo']))
        minienv_json = response.read()
    except:
        print('Error downloading minienv.json')
    if minienv_json is not None and len(minienv_json) > 0:
        minienv_dict = json.loads(minienv_json)
    # download docker-compose file (first try yml, then yaml)
    docker_compose_yaml = None
    try:
        response = urllib2.urlopen('{}/raw/master/docker-compose.yml'.format(
            up_request['repo']))
        docker_compose_yaml = response.read()
    except:
        print('Error downloading docker-compose.yml')
    if docker_compose_yaml is None or len(docker_compose_yaml) == 0:
        try:
            response = urllib2.urlopen(
                '{}/raw/master/docker-compose.yaml'.format(up_request['repo']))
            docker_compose_yaml = response.read()
        except:
            print('Error downloading docker-compose.yaml')
    if docker_compose_yaml is None or len(docker_compose_yaml) == 0:
        abort(400)
        return
    docker_compose_dict = yaml.safe_load(docker_compose_yaml)
    # check if environment already running
    if is_env_deployed(environment['id']):
        print('Deleting existing environment {}...'.format(environment['id']))
        delete_env(environment['id'])
    # run using docker-compose
    project_name = get_env_project_name(environment['id'])
    volume_name = get_volume_name(environment['id'])
    src_file_name = './docker-compose-env.yml.template'
    dest_file_name = './docker-compose-{}.yml'.format(project_name)
    src_file = open(src_file_name, 'r')
    dest_file = open(dest_file_name, 'w')
    external_log_port = str(EXTERNAL_LOG_PORT_START +
                            (environment['index'] * EXTERNAL_PORT_INCREMENT))
    external_editor_port = str(EXTERNAL_EDITOR_PORT_START +
                               (environment['index'] *
                                EXTERNAL_PORT_INCREMENT))
    external_proxy_port = str(EXTERNAL_PROXY_PORT_START +
                              (environment['index'] * EXTERNAL_PORT_INCREMENT))
    for line in src_file:
        line = line.replace(VAR_INTERNAL_LOG_PORT, DEFAULT_INTERNAL_LOG_PORT)
        line = line.replace(VAR_INTERNAL_EDITOR_PORT,
                            DEFAULT_INTERNAL_EDITOR_PORT)
        line = line.replace(VAR_INTERNAL_PROXY_PORT,
                            DEFAULT_INTERNAL_PROXY_PORT)
        line = line.replace(VAR_EXTERNAL_LOG_PORT, external_log_port)
        line = line.replace(VAR_EXTERNAL_EDITOR_PORT, external_editor_port)
        line = line.replace(VAR_EXTERNAL_PROXY_PORT, external_proxy_port)
        line = line.replace(VAR_GIT_REPO, up_request['repo'])
        line = line.replace(VAR_ALLOW_ORIGIN, allow_origin)
        line = line.replace(VAR_VOLUME_NAME, volume_name)
        dest_file.write(line)
    src_file.close()
    dest_file.close()
    project = get_project('./', project_name, dest_file_name)
    print('Running docker-compose up for environment {}...'.format(
        environment['id']))
    project.up(detached=True, strategy=2)  # strategy 2 = always re-create
    ps = ps_(project)
    return get_up_details(ps, docker_compose_dict, minienv_dict)