예제 #1
0
def _find_dockyard_with_image(vagrant_dev, is_restart, dockyard_alias,
                              microservice_name):
    image = ArmadaImage(microservice_name, dockyard_alias)

    if vagrant_dev and is_restart:
        local_image = ArmadaImage(image.image_name, 'local')
        image = select_latest_image(image, local_image)
        if image == local_image:
            dockyard_alias = 'local'

    if vagrant_dev and not image.exists():
        print(
            'Image {image} not found. Searching in default dockyard...'.format(
                **locals()))
        dockyard_alias = get_default()
        image = ArmadaImage(microservice_name, dockyard_alias)

    if not image.exists():
        if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
            was_fallback_dockyard = True
        else:
            print(
                'Image {image} not found. Searching in official Armada dockyard...'
                .format(**locals()))
            dockyard_alias = DOCKYARD_FALLBACK_ALIAS
            image = ArmadaImage(microservice_name, dockyard_alias)
            was_fallback_dockyard = False
        if was_fallback_dockyard or not image.exists():
            raise ArmadaCommandException(
                'Image {image} not found. Aborting.'.format(**locals()))

    return dockyard_alias, image
예제 #2
0
def command_push(args):
    if not args.image_path:
        raise ArmadaCommandException('ERROR: Please specify image_path argument'
                                     ' or set MICROSERVICE_NAME environment variable')
    dockyard_alias = args.dockyard
    image = ArmadaImage(args.image_path, dockyard_alias)

    if '/' not in args.image_path:
        if not ArmadaImage(image.image_name, 'local').exists():
            raise Exception('Image {} does not exist. Typo?'.format(image.image_name))
        dockyard_string = image.dockyard.url or ''
        dockyard_string += ' (alias: {})'.format(dockyard_alias) if dockyard_alias else ''
        print('Pushing image {} to dockyard: {}...'.format(image.image_name, dockyard_string))
        tag_command = 'docker tag -f {} {}'.format(image.image_name, image.image_path)
        assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0
    else:
        # If command was called with [docker_registry_address]/[image_name] and no -d/--dockyard, then simply
        # mimic 'docker push' behavior (without tagging).
        print('Pushing image {}...'.format(image))

    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    did_print = False
    d = dockyard_factory(dockyard_dict.get('address'), dockyard_dict.get('user'), dockyard_dict.get('password'))
    if d.is_http():
        did_print = print_http_dockyard_unavailability_warning(
            dockyard_dict['address'],
            dockyard_alias,
            "ERROR! Cannot push to dockyard!",
        )

    retries = 0 if did_print else 3
    login_to_dockyard(dockyard_alias)
    push_command = 'docker push {}'.format(image.image_path)
    assert execute_local_command(push_command, stream_output=True, retries=retries)[0] == 0
예제 #3
0
def command_build(args):
    microservice_name = args.microservice_name or os.environ.get('MICROSERVICE_NAME')
    if not microservice_name:
        raise ValueError('No microservice name supplied.')
    if not os.path.exists('Dockerfile'):
        print('ERROR: Dockerfile not found in current directory', file=sys.stderr)
        return
    base_image_name = _get_base_image_name()
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(base_image_name, is_run_locally=True)

    base_image = ArmadaImage(base_image_name, dockyard_alias)

    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print('Base image {base_image} not found. Searching in official Armada dockyard...'.format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImage(base_image_name, dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                print('Base image {base_image} not found. Aborting.'.format(**locals()))
                sys.exit(1)
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'), dockyard_dict.get('user'), dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path
        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command, stream_output=True, retries=retries)[0] == 0
        if base_image_path != base_image_name:
            tag_command = 'docker tag -f {base_image_path} {base_image_name}'.format(**locals())
            assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0

    build_command = 'docker build -t {microservice_name} .'.format(**locals())
    assert execute_local_command(build_command, stream_output=True)[0] == 0
예제 #4
0
def command_build(args):
    microservice_name = args.microservice_name or os.environ.get(
        'MICROSERVICE_NAME')
    if not microservice_name:
        raise ValueError('No microservice name supplied.')
    if not os.path.exists('Dockerfile'):
        print('ERROR: Dockerfile not found in current directory',
              file=sys.stderr)
        return
    base_image_name = _get_base_image_name()
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(
        base_image_name, is_run_locally=True)

    base_image = ArmadaImage(base_image_name, dockyard_alias)

    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print(
                    'Base image {base_image} not found. Searching in official Armada dockyard...'
                    .format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImage(base_image_name, dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                print('Base image {base_image} not found. Aborting.'.format(
                    **locals()))
                sys.exit(1)
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'),
                             dockyard_dict.get('user'),
                             dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path
        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command,
                                     stream_output=True,
                                     retries=retries)[0] == 0
        if base_image_path != base_image_name:
            tag_command = 'docker tag -f {base_image_path} {base_image_name}'.format(
                **locals())
            assert execute_local_command(tag_command,
                                         stream_output=True,
                                         retries=1)[0] == 0

    build_command = 'docker build -t {microservice_name} .'.format(**locals())
    assert execute_local_command(build_command, stream_output=True)[0] == 0
예제 #5
0
def _find_dockyard_with_image(vagrant_dev, is_restart, dockyard_alias, microservice_name):
    image = ArmadaImage(microservice_name, dockyard_alias)

    if vagrant_dev and is_restart:
        local_image = ArmadaImage(image.image_name, 'local')
        image = select_latest_image(image, local_image)
        if image == local_image:
            dockyard_alias = 'local'

    if vagrant_dev and not image.exists():
        print('Image {image} not found. Searching in default dockyard...'.format(**locals()))
        dockyard_alias = get_default()
        image = ArmadaImage(microservice_name, dockyard_alias)

    if not image.exists():
        if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
            was_fallback_dockyard = True
        else:
            print('Image {image} not found. Searching in official Armada dockyard...'.format(**locals()))
            dockyard_alias = DOCKYARD_FALLBACK_ALIAS
            image = ArmadaImage(microservice_name, dockyard_alias)
            was_fallback_dockyard = False
        if was_fallback_dockyard or not image.exists():
            raise ArmadaCommandException('Image {image} not found. Aborting.'.format(**locals()))

    return dockyard_alias, image
예제 #6
0
def command_run(args):
    if args.verbose:
        global verbose
        verbose = True
    microservice_name = args.microservice_name
    if not microservice_name:
        raise ArmadaCommandException('ERROR: Please specify microservice_name argument'
                                     ' or set MICROSERVICE_NAME environment variable')
    ship = args.ship
    is_run_locally = ship is None
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(microservice_name, is_run_locally)
    vagrant_dev = False
    if args.hidden_vagrant_dev or (are_we_in_vagrant() and not args.dockyard and dockyard_alias == 'local'):
        print('INFO: Detected development environment for microservice {microservice_name}. '
              'Using local docker registry.'.format(**locals()))
        vagrant_dev = True
    image = ArmadaImage(microservice_name, dockyard_alias)

    if args.hidden_is_restart:
        local_image = ArmadaImage(image.microservice_name, 'local')
        image = select_latest_image(image, local_image)

    if vagrant_dev and not image.exists():
        print('Image {image} not found. Searching in default dockyard...'.format(**locals()))
        dockyard_alias = get_default()
        image = ArmadaImage(microservice_name, dockyard_alias)

    if not image.exists():
        if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
            was_fallback_dockyard = True
        else:
            print('Image {image} not found. Searching in official Armada dockyard...'.format(**locals()))
            dockyard_alias = DOCKYARD_FALLBACK_ALIAS
            image = ArmadaImage(microservice_name, dockyard_alias)
            was_fallback_dockyard = False
        if was_fallback_dockyard or not image.exists():
            print('Image {image} not found. Aborting.'.format(**locals()))
            sys.exit(1)

    dockyard_string = image.dockyard_address or ''
    if dockyard_alias:
        dockyard_string += ' (alias: {dockyard_alias})'.format(**locals())
    ship_string = ' on remote ship: {ship}'.format(**locals()) if ship else ' locally'
    if args.rename:
        print('Running microservice {name} (renamed from {image.microservice_name}) from dockyard: {dockyard_string}{ship_string}...'.format(
            name=args.rename, **locals()))
    else:
        print('Running microservice {image.microservice_name} from dockyard: {dockyard_string}{ship_string}...'.format(
            **locals()))

    payload = {'image_path': image.image_path, 'environment': {}, 'ports': {}, 'volumes': {}}
    if dockyard_alias and dockyard_alias != 'local':
        dockyard_info = dockyard.alias.get_alias(dockyard_alias)
        if not dockyard_info:
            raise ArmadaCommandException("Couldn't read configuration for dockyard alias {0}.".format(dockyard_alias))
        payload['dockyard_user'] = dockyard_info.get('user')
        payload['dockyard_password'] = dockyard_info.get('password')

    if vagrant_dev:
        if not args.dynamic_ports:
            payload['ports']['4999'] = '80'
            payload['ports']['2999'] = '22'
        if not args.use_latest_image_code:
            microservice_path = '/opt/{microservice_name}'.format(**locals())
            payload['volumes'][microservice_path] = microservice_path
        payload['environment']['ARMADA_VAGRANT_DEV'] = '1'

    hermes_env, hermes_volumes = process_hermes(image.microservice_name, args.env, args.app_id,
                                                sum(args.configs or [], []))
    payload['environment'].update(hermes_env or {})
    payload['volumes'].update(hermes_volumes or {})

    # --- environment
    for env_var in sum(args.e or [], []):
        env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2]
        payload['environment'][env_key] = env_value

    # --- ports
    for port_mapping in sum(args.publish or [], []):
        try:
            port_host, port_container = map(int, (port_mapping.split(':', 1) + [None])[:2])
            payload['ports'][str(port_host)] = str(port_container)
        except (ValueError, TypeError):
            print('Invalid port mapping: {0}'.format(port_mapping), file=sys.stderr)
            return

    # --- volumes
    for volume_string in sum(args.volumes or [], []):
        volume = volume_string.split(':')
        if len(volume) == 1:
            volume *= 2
        payload['volumes'][volume[0]] = volume[1]

    # --- name
    payload['microservice_name'] = args.rename

    # --- run_arguments
    run_command = 'armada ' + ' '.join(sys.argv[1:])
    if vagrant_dev and '--hidden_vagrant_dev' not in run_command:
        run_command += ' --hidden_vagrant_dev'
    if '--hidden_is_restart' not in run_command:
        run_command += ' --hidden_is_restart'
    payload['run_command'] = run_command

    # ---
    if verbose:
        print('payload: {0}'.format(payload))

    warn_if_hit_crontab_environment_variable_length(payload['environment'])

    result = armada_api.post('run', payload, ship_name=ship)

    if result['status'] == 'ok':
        container_id = result['container_id']
        if args.hidden_is_restart:
            print('Service has been restarted and is running in container {container_id} '
                  'available at addresses:'.format(**locals()))
        else:
            print('Service is running in container {container_id} available at addresses:'.format(**locals()))
        for service_address, docker_port in result['endpoints'].iteritems():
            print('  {0} ({1})'.format(service_address, docker_port))
    else:
        print('ERROR: {0}'.format(result['error']))
        sys.exit(1)