Example #1
0
 def handler(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except DockerMachineNotRunningError:
         vm_name = docker_machine.vm_name()
         log = get_logger_for_func(func)
         log.error('Docker machine VM has not been started.')
         log.error('Use the following command to start the VM:')
         log.error('  docker-machine start {}'.format(vm_name))
Example #2
0
def with_docker_machine():
    try:
        vm_name = docker_machine.vm_name()
        output = terminal.docker_machine_ip(vm_name)
        if output:
            return output
        else:
            raise DockerMachineError('docker-machine host is not running.')
    except CalledProcessError:
        raise DockerMachineError('docker-machine host is not running.')
def init(args):
    """`sandbox init` command"""

    log = logging.getLogger(__name__)
    vm_name = docker_machine.vm_name()
    if is_docker_machine_installed():
        is_docker_vm_installed = docker_vm_install_check(vm_name)
        if not is_docker_vm_installed:
            log.info('Creating Docker VM {}'.format(vm_name))
            log.info('This will take a few minutes - please be patient...')
            terminal.docker_machine_create_vm(vm_name)
        else:
            log.info('Docker VM {} already installed'.format(vm_name))

        is_docker_vm_started = docker_vm_running_check(vm_name)
        if not is_docker_vm_started:
            log.info('Starting Docker VM {}'.format(vm_name))
            terminal.docker_machine_start_vm(vm_name)
        else:
            log.info('Docker VM {} already running'.format(vm_name))

        existing_ram, has_sufficient_ram = docker_vm_ram_check(vm_name)
        existing_cpu, has_sufficient_cpu = docker_vm_cpu_check(vm_name)

        if not has_sufficient_ram or not has_sufficient_cpu:
            if not has_sufficient_ram:
                log.warning('Docker VM {} has insufficient RAM of {}MB - increasing to the minimum of {}MB'
                            .format(vm_name, existing_ram, docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))

            if not has_sufficient_cpu:
                log.warning('Docker VM {} has insufficient no of CPU {} - increasing to the minimum of {} CPU'
                            .format(vm_name, existing_cpu, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))

            log.info('Stopping Docker VM {}'.format(vm_name))
            terminal.docker_machine_stop_vm(vm_name)

            if not has_sufficient_ram:
                log.info('Increasing Docker VM {} RAM to {}MB'.format(vm_name,
                                                                      docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))
                terminal.vbox_manage_increase_ram(vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE)

            if not has_sufficient_cpu:
                log.info('Increasing Docker VM {} no of CPU to {}'
                         .format(vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))
                terminal.vbox_manage_increase_cpu(vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT)

            log.info('Starting Docker VM {}'.format(vm_name))
            terminal.docker_machine_start_vm(vm_name)
        else:
            log.info('Docker VM {} already has sufficient RAM and CPU'.format(vm_name))

        log.info('Sandbox initialization complete')
    else:
        log.warning('Unable to initialize sandbox - docker-machine not installed')
Example #4
0
 def handler(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except DockerMachineCannotConnectToDockerError:
         log = get_logger_for_func(func)
         log.info('It looks like the Docker machine environment variables are not set correctly.')
         log.info('Let me try to reset the Docker machine environment variables..')
         docker_machine_vm_name = docker_machine.vm_name()
         [docker_machine.set_env(env[0], env[1]) for env in docker_machine.envs(docker_machine_vm_name)]
         try:
             terminal.docker_info()
             log.warning('To set the environment variables for each terminal session '
                         'follow the instructions of the command:')
             log.warning('  docker-machine env {}'.format(docker_machine_vm_name))
             return func(*args, **kwargs)
         except (AttributeError, CalledProcessError):
             log.error('Docker still cannot connect to the Docker machine VM.')
             log.error('Please set the docker environment variables.')
             log.error('Afterwards verify that docker is up and running with: docker info')
Example #5
0
def handle_docker_vm_error(func):
    vm_name = docker_machine.vm_name()

    def handler(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except DockerMachineError:
            log = get_logger_for_func(func)
            log.error('Docker VM has not been started.')
            log.error('Use the following command to start the VM:')
            log.error('  docker-machine start {}'.format(vm_name))
        except Boot2DockerError:
            log = get_logger_for_func(func)
            log.error('Docker VM has not been started.')
            log.error('Use the following command to start the VM:')
            log.error('  boot2docker up')

    # Do not change the wrapped function name,
    # so argparse configuration can be tested.
    handler.__name__ = func.__name__

    return handler
Example #6
0
def handle_docker_vm_error(func):
    vm_name = docker_machine.vm_name()

    def handler(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except DockerMachineError:
            log = get_logger_for_func(func)
            log.error('Docker VM has not been started.')
            log.error('Use the following command to start the VM:')
            log.error('  docker-machine start {}'.format(vm_name))
        except Boot2DockerError:
            log = get_logger_for_func(func)
            log.error('Docker VM has not been started.')
            log.error('Use the following command to start the VM:')
            log.error('  boot2docker up')

    # Do not change the wrapped function name,
    # so argparse configuration can be tested.
    handler.__name__ = func.__name__

    return handler
Example #7
0
def init(args):
    """`sandbox init` command"""

    log = logging.getLogger(__name__)

    try:
        info = terminal.docker_info()
        existing_ram, has_sufficient_ram = docker_ram_check(info)
        existing_cpu, has_sufficient_cpu = docker_cpu_check(info)

        if not has_sufficient_ram or not has_sufficient_cpu:
            if not has_sufficient_ram:
                log.warning('Docker has insufficient RAM of {}MiB - please increase to a minimum of {}MiB'
                            .format(existing_ram, docker.DEFAULT_DOCKER_RAM_SIZE))

            if not has_sufficient_cpu:
                log.warning('Docker has an insufficient no. of CPUs {} - please increase to a minimum of {} CPUs'
                            .format(existing_cpu, docker.DEFAULT_DOCKER_CPU_COUNT))
        else:
            log.info('Docker already has sufficient RAM and CPUs')

    except (AttributeError, CalledProcessError):
        pass

    docker_machine_vm_name = docker_machine.vm_name()
    if is_docker_machine_installed():
        is_docker_machine_vm_installed = docker_machine_install_check(docker_machine_vm_name)
        if not is_docker_machine_vm_installed:
            log.info('Creating Docker machine VM {}'.format(docker_machine_vm_name))
            log.info('This will take a few minutes - please be patient...')
            terminal.docker_machine_create_vm(docker_machine_vm_name)
        else:
            log.info('Docker machine VM {} already installed'.format(docker_machine_vm_name))

        is_docker_machine_started = docker_machine_running_check(docker_machine_vm_name)
        if not is_docker_machine_started:
            log.info('Starting Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_start_vm(docker_machine_vm_name)
        else:
            log.info('Docker machine VM {} already running'.format(docker_machine_vm_name))

        existing_ram, has_sufficient_ram = docker_machine_ram_check(docker_machine_vm_name)
        existing_cpu, has_sufficient_cpu = docker_machine_cpu_check(docker_machine_vm_name)

        if not has_sufficient_ram or not has_sufficient_cpu:
            if not has_sufficient_ram:
                log.warning('Docker machine VM {} has insufficient RAM of {}MiB - increasing to the minimum of {}MiB'
                            .format(docker_machine_vm_name,
                                    existing_ram,
                                    docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))

            if not has_sufficient_cpu:
                log.warning(
                    'Docker machine VM {} has an insufficient no. of CPUs {} - increasing to the minimum of {} CPU'
                    .format(docker_machine_vm_name, existing_cpu, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))

            log.info('Stopping Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_stop_vm(docker_machine_vm_name)

            if not has_sufficient_ram:
                log.info(
                    'Increasing Docker machine VM {} RAM to {}MB'
                    .format(docker_machine_vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))
                terminal.vbox_manage_increase_ram(docker_machine_vm_name,
                                                  docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE)

            if not has_sufficient_cpu:
                log.info(
                    'Increasing Docker machine VM {} no. of CPUs to {}'
                    .format(docker_machine_vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))
                terminal.vbox_manage_increase_cpu(docker_machine_vm_name,
                                                  docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT)

            log.info('Starting Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_start_vm(docker_machine_vm_name)
        else:
            log.info('Docker machine VM {} already has sufficient RAM and CPUs'.format(docker_machine_vm_name))

        log.info('Sandbox initialization complete')
Example #8
0
def validate_docker_vm(vm_type):
    log = logging.getLogger(__name__)
    if vm_type is DockerVmType.DOCKER_ENGINE:
        try:
            info = terminal.docker_info()
            existing_ram, has_sufficient_ram = docker.ram_check(info)
            existing_cpu, has_sufficient_cpu = docker.cpu_check(info)

            if not has_sufficient_ram or not has_sufficient_cpu:
                if not has_sufficient_ram:
                    log.warning('Docker has insufficient RAM of {} GiB - please increase to a minimum of {} GiB'
                                .format(existing_ram, docker.DEFAULT_DOCKER_RAM_SIZE))

                if not has_sufficient_cpu:
                    log.warning('Docker has an insufficient no. of CPUs {} - please increase to a minimum of {} CPUs'
                                .format(existing_cpu, docker.DEFAULT_DOCKER_CPU_COUNT))
        except (AttributeError, CalledProcessError):
            log.error('Docker native is installed but not running.')
            log.error('Please start Docker with one of the Docker flavors based on your OS:')
            log.error('  Linux:   Docker service')
            log.error('  MacOS:   Docker for Mac')
            log.error('  Windows: Docker for Windows')
            log.error('A successful Docker startup can be verified with: docker info')
            exit(1)

    elif vm_type is DockerVmType.DOCKER_MACHINE:
        docker_machine_vm_name = docker_machine.vm_name()

        is_docker_machine_vm_installed = docker_machine.vm_install_check(docker_machine_vm_name)
        if not is_docker_machine_vm_installed:
            log.info('Creating Docker machine VM {}'.format(docker_machine_vm_name))
            log.info('This will take a few minutes - please be patient..')
            terminal.docker_machine_create_vm(docker_machine_vm_name)

        is_docker_machine_started = docker_machine.running_check(docker_machine_vm_name)
        if not is_docker_machine_started:
            log.info('Starting Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_start_vm(docker_machine_vm_name)

        existing_ram, has_sufficient_ram = docker_machine.ram_check(docker_machine_vm_name)
        existing_cpu, has_sufficient_cpu = docker_machine.cpu_check(docker_machine_vm_name)

        if not has_sufficient_ram or not has_sufficient_cpu:
            if not has_sufficient_ram:
                log.warning('Docker machine VM {} has insufficient RAM of {} MB - '
                            'increasing to the minimum of {} MB'
                            .format(docker_machine_vm_name,
                                    existing_ram,
                                    docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))

            if not has_sufficient_cpu:
                log.warning('Docker machine VM {} has an insufficient no. of CPUs {} - increasing to the minimum of {} CPU'
                            .format(docker_machine_vm_name, existing_cpu, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))

            log.info('Stopping Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_stop_vm(docker_machine_vm_name)

            if not has_sufficient_ram:
                log.info('Increasing Docker machine VM {} RAM to {} MB'
                         .format(docker_machine_vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))
                terminal.vbox_manage_increase_ram(docker_machine_vm_name,
                                                  docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE)

            if not has_sufficient_cpu:
                log.info('Increasing Docker machine VM {} no. of CPUs to {}'
                         .format(docker_machine_vm_name, docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))
                terminal.vbox_manage_increase_cpu(docker_machine_vm_name,
                                                  docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT)

            log.info('Starting Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_start_vm(docker_machine_vm_name)

            try:
                terminal.docker_info()
            except (AttributeError, CalledProcessError):
                log.info('It looks like the Docker machine environment variables are not set correctly.')
                log.info('Let me try to reset the Docker machine environment variables..')
                [docker_machine.set_env(env[0], env[1]) for env in docker_machine.envs(docker_machine_vm_name)]
                try:
                    terminal.docker_info()
                    log.warning('To set the environment variables for each terminal session '
                                'follow the instructions of the command:')
                    log.warning('  docker-machine env {}'.format(docker_machine_vm_name))
                except (AttributeError, CalledProcessError):
                    log.error('Docker still cannot connect to the Docker machine VM.')
                    log.error('Please set the docker environment variables.')
                    log.error('Afterwards verify that docker is up and running with: docker info')
                    exit(1)

    elif vm_type is DockerVmType.NONE:
        log.error('Neither Docker native is installed nor the Docker machine environment variables are set.')
        log.error('We recommend to use one of following the Docker distributions depending on your OS:')
        log.error('  Linux:                                         Docker Engine')
        log.error('  MacOS:                                         Docker for Mac')
        log.error('  Windows 10+ Professional or Enterprise 64-bit: Docker for Windows')
        log.error('  Other Windows:                                 Docker machine via Docker Toolbox')
        log.error('For more information checkout: https://www.docker.com/products/overview')
        exit(1)
Example #9
0
def init(args):
    """`sandbox init` command"""

    log = logging.getLogger(__name__)

    try:
        info = terminal.docker_info()
        existing_ram, has_sufficient_ram = docker_ram_check(info)
        existing_cpu, has_sufficient_cpu = docker_cpu_check(info)

        if not has_sufficient_ram or not has_sufficient_cpu:
            if not has_sufficient_ram:
                log.warning(
                    'Docker has insufficient RAM of {}MiB - please increase to a minimum of {}MiB'
                    .format(existing_ram, docker.DEFAULT_DOCKER_RAM_SIZE))

            if not has_sufficient_cpu:
                log.warning(
                    'Docker has an insufficient no. of CPUs {} - please increase to a minimum of {} CPUs'
                    .format(existing_cpu, docker.DEFAULT_DOCKER_CPU_COUNT))
        else:
            log.info('Docker already has sufficient RAM and CPUs')

    except (AttributeError, CalledProcessError):
        pass

    docker_machine_vm_name = docker_machine.vm_name()
    if is_docker_machine_installed():
        is_docker_machine_vm_installed = docker_machine_install_check(
            docker_machine_vm_name)
        if not is_docker_machine_vm_installed:
            log.info(
                'Creating Docker machine VM {}'.format(docker_machine_vm_name))
            log.info('This will take a few minutes - please be patient...')
            terminal.docker_machine_create_vm(docker_machine_vm_name)
        else:
            log.info('Docker machine VM {} already installed'.format(
                docker_machine_vm_name))

        is_docker_machine_started = docker_machine_running_check(
            docker_machine_vm_name)
        if not is_docker_machine_started:
            log.info(
                'Starting Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_start_vm(docker_machine_vm_name)
        else:
            log.info('Docker machine VM {} already running'.format(
                docker_machine_vm_name))

        existing_ram, has_sufficient_ram = docker_machine_ram_check(
            docker_machine_vm_name)
        existing_cpu, has_sufficient_cpu = docker_machine_cpu_check(
            docker_machine_vm_name)

        if not has_sufficient_ram or not has_sufficient_cpu:
            if not has_sufficient_ram:
                log.warning(
                    'Docker machine VM {} has insufficient RAM of {}MiB - increasing to the minimum of {}MiB'
                    .format(docker_machine_vm_name, existing_ram,
                            docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))

            if not has_sufficient_cpu:
                log.warning(
                    'Docker machine VM {} has an insufficient no. of CPUs {} - increasing to the minimum of {} CPU'
                    .format(docker_machine_vm_name, existing_cpu,
                            docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))

            log.info(
                'Stopping Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_stop_vm(docker_machine_vm_name)

            if not has_sufficient_ram:
                log.info('Increasing Docker machine VM {} RAM to {}MB'.format(
                    docker_machine_vm_name,
                    docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE))
                terminal.vbox_manage_increase_ram(
                    docker_machine_vm_name,
                    docker_machine.DEFAULT_DOCKER_MACHINE_RAM_SIZE)

            if not has_sufficient_cpu:
                log.info(
                    'Increasing Docker machine VM {} no. of CPUs to {}'.format(
                        docker_machine_vm_name,
                        docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT))
                terminal.vbox_manage_increase_cpu(
                    docker_machine_vm_name,
                    docker_machine.DEFAULT_DOCKER_MACHINE_CPU_COUNT)

            log.info(
                'Starting Docker machine VM {}'.format(docker_machine_vm_name))
            terminal.docker_machine_start_vm(docker_machine_vm_name)
        else:
            log.info(
                'Docker machine VM {} already has sufficient RAM and CPUs'.
                format(docker_machine_vm_name))

        log.info('Sandbox initialization complete')
Example #10
0
def handle_docker_errors(func):
    log = get_logger_for_func(func)
    vm_name = docker_machine.vm_name()

    def handle_linux():
        log.error('The docker service has not been started.')
        log.error('To start the docker service run:')
        log.error('  sudo service docker start')

    def handle_non_linux(*args, **kwargs):
        log.info('Docker could not connect to the docker VM.')
        log.info('It looks like the docker environment variables are not set. Let me try to set them..')
        [set_env(env[0], env[1]) for env in resolve_envs()]
        try:
            terminal.docker_ps()
            log.info('The Docker environment variables have been set for this command.')
            log.info('Continue processing..')
            log.warning('To set the environment variables for each terminal session '
                        'follow the instructions of the command:')
            log.warning('  docker-machine env {}'.format(vm_name))
            log.info('')
            return func(*args, **kwargs)
        except CalledProcessError:
            log.error('Docker could not be configured automatically.')
            log.error('Please set the docker environment variables.')

    def resolve_envs():
        try:
            env_lines = terminal.docker_machine_env(vm_name)
            log.info('Retrieved docker environment variables with `docker-machine env {}`'.format(vm_name))
        except NOT_FOUND_ERROR:
            try:
                env_lines = terminal.boot2docker_shellinit()
                log.info('Retrieved docker environment variables with: boot2docker shellinit')
                log.warning('boot2docker is deprecated. Upgrade to docker-machine.')
            except NOT_FOUND_ERROR:
                return []
        return [resolve_env(line) for line in env_lines if line.startswith('export')]

    def resolve_env(line):
        key = line.partition(' ')[-1].partition('=')[0]
        value = line.partition(' ')[-1].partition('=')[2].strip('"')
        return key, value

    def set_env(key, value):
        log.info('Set environment variable: {}="{}"'.format(key, value))
        os.environ[key] = value

    def handler(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except CalledProcessError:
            if sys.platform == 'linux' or sys.platform == 'linux2':
                return handle_linux()
            else:
                return handle_non_linux(*args, **kwargs)

        except NOT_FOUND_ERROR:
            log.error('docker command has not been found.')
            log.error('The sandbox need Docker to run the ConductR nodes in virtual containers.')
            log.error('Please install Docker first: https://www.docker.com')

    # Do not change the wrapped function name,
    # so argparse configuration can be tested.
    handler.__name__ = func.__name__

    return handler
Example #11
0
def handle_docker_errors(func):
    log = get_logger_for_func(func)
    vm_name = docker_machine.vm_name()

    def handle_linux():
        log.error('The docker service has not been started.')
        log.error('To start the docker service run:')
        log.error('  sudo service docker start')

    def handle_non_linux(*args, **kwargs):
        log.info('Docker could not connect to the docker VM.')
        log.info(
            'It looks like the docker environment variables are not set. Let me try to set them..'
        )
        [set_env(env[0], env[1]) for env in resolve_envs()]
        try:
            terminal.docker_ps()
            log.info(
                'The Docker environment variables have been set for this command.'
            )
            log.info('Continue processing..')
            log.warning(
                'To set the environment variables for each terminal session '
                'follow the instructions of the command:')
            log.warning('  docker-machine env {}'.format(vm_name))
            log.info('')
            return func(*args, **kwargs)
        except CalledProcessError:
            log.error('Docker could not be configured automatically.')
            log.error('Please set the docker environment variables.')

    def resolve_envs():
        try:
            env_lines = terminal.docker_machine_env(vm_name)
            log.info(
                'Retrieved docker environment variables with `docker-machine env {}`'
                .format(vm_name))
        except NOT_FOUND_ERROR:
            try:
                env_lines = terminal.boot2docker_shellinit()
                log.info(
                    'Retrieved docker environment variables with: boot2docker shellinit'
                )
                log.warning(
                    'boot2docker is deprecated. Upgrade to docker-machine.')
            except NOT_FOUND_ERROR:
                return []
        return [
            resolve_env(line) for line in env_lines
            if line.startswith('export')
        ]

    def resolve_env(line):
        key = line.partition(' ')[-1].partition('=')[0]
        value = line.partition(' ')[-1].partition('=')[2].strip('"')
        return key, value

    def set_env(key, value):
        log.info('Set environment variable: {}="{}"'.format(key, value))
        os.environ[key] = value

    def handler(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except CalledProcessError:
            if sys.platform == 'linux' or sys.platform == 'linux2':
                return handle_linux()
            else:
                return handle_non_linux(*args, **kwargs)

        except NOT_FOUND_ERROR:
            log.error('docker command has not been found.')
            log.error(
                'The sandbox need Docker to run the ConductR nodes in virtual containers.'
            )
            log.error('Please install Docker first: https://www.docker.com')

    # Do not change the wrapped function name,
    # so argparse configuration can be tested.
    handler.__name__ = func.__name__

    return handler