Ejemplo n.º 1
0
def _find_dockyard_with_image(vagrant_dev, is_restart, dockyard_alias,
                              microservice_name):
    image = ArmadaImageFactory(microservice_name, dockyard_alias)

    if vagrant_dev and is_restart:
        local_image = ArmadaImageFactory(image.image_name_with_tag, '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 = ArmadaImageFactory(image.image_name_with_tag, dockyard_alias)

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

    return dockyard_alias, image
Ejemplo n.º 2
0
def _find_dockyard_with_image(vagrant_dev, is_restart, dockyard_alias, microservice_name):
    image = ArmadaImageFactory(microservice_name, dockyard_alias)

    if vagrant_dev and is_restart:
        local_image = ArmadaImageFactory(image.image_name_with_tag, '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 = ArmadaImageFactory(image.image_name_with_tag, dockyard_alias)

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

    return dockyard_alias, image
Ejemplo n.º 3
0
def command_dockyard_default(args):
    if args.name:
        alias.set_default(args.name)
    else:
        default_alias = alias.get_default()
        if default_alias:
            print('Default alias for dockyard is: {default_alias}'.format(
                **locals()))
        else:
            print('No default alias set')
Ejemplo n.º 4
0
def get_default_alias():
    default_alias = alias.get_default()
    if default_alias:
        dockyard = alias.get_alias(default_alias)
        if not dockyard:
            raise DockyardException('Default dockyard does not exist.')
        return default_alias
    alias_list = alias.get_list()
    if not alias_list:
        raise DockyardException('There are no running dockyards.')
    if len(alias_list) > 1:
        raise CriticalDockyardException('There are multiple running dockyards and no default set.'
                                        'Please specify dockyard by -d/--dockyard or set the default.')
    return alias_list[0]['name']
Ejemplo n.º 5
0
def get_default_alias():
    default_alias = alias.get_default()
    if default_alias:
        dockyard = alias.get_alias(default_alias)
        if not dockyard:
            raise DockyardException('Default dockyard does not exist.')
        return default_alias
    alias_list = alias.get_list()
    if not alias_list:
        raise DockyardException('There are no running dockyards.')
    if len(alias_list) > 1:
        raise CriticalDockyardException('There are multiple running dockyards and no default set.'
                                        'Please specify dockyard by -d/--dockyard or set the default.')
    return alias_list[0]['name']
Ejemplo n.º 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)