Exemple #1
0
 def resolve():
     try:
         terminal.docker_info()
         return '127.0.0.1'
     except (CalledProcessError, FileNotFoundError):
         try:
             return with_docker_machine()
         except validation.NOT_FOUND_ERROR:
             try:
                 return with_boot2docker()
             except validation.NOT_FOUND_ERROR:
                 try:
                     return with_hostname()
                 except validation.NOT_FOUND_ERROR:
                     return '127.0.0.1'
Exemple #2
0
    def test_docker_info(self):
        output = "test"
        check_output_mock = MagicMock(return_value='{}\n'.format(output))

        with patch('subprocess.check_output', check_output_mock):
            result = terminal.docker_info()

        self.assertEqual(result, output)
        check_output_mock.assert_called_with(['docker', 'info'], stderr=subprocess.DEVNULL)
Exemple #3
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')
Exemple #4
0
    def test_docker_info(self):
        output = "test"
        check_output_mock = MagicMock(return_value='{}\n'.format(output))

        with patch('subprocess.check_output', check_output_mock):
            result = terminal.docker_info()

        self.assertEqual(result, output)
        check_output_mock.assert_called_with(['docker', 'info'],
                                             stderr=subprocess.DEVNULL)
Exemple #5
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 = ram_check(info)
            existing_cpu, has_sufficient_cpu = 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, 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, DEFAULT_DOCKER_CPU_COUNT))
        except (AttributeError, CalledProcessError, NOT_FOUND_ERROR):
            raise DockerValidationError([
                'Docker is installed but not running.',
                'Please start Docker with one of the Docker flavors based on your OS:',
                '  Linux:   Docker service', '  MacOS:   Docker for Mac',
                'A successful Docker startup can be verified with: docker info'
            ])

    elif vm_type is DockerVmType.DOCKER_MACHINE:
        raise DockerValidationError([
            'Docker machine envs are set but Docker machine is not supported by the conductr-cli.',
            'We recommend to use one of following the Docker distributions depending on your OS:',
            '  Linux:                                         Docker Engine',
            '  MacOS:                                         Docker for Mac',
            'For more information checkout: https://www.docker.com/products/overview'
        ])

    elif vm_type is DockerVmType.NONE:
        raise DockerValidationError([
            'Docker is not installed.',
            'We recommend to use one of following the Docker distributions depending on your OS:',
            '  Linux:                                         Docker Engine',
            '  MacOS:                                         Docker for Mac',
            'For more information checkout: https://www.docker.com/products/overview'
        ])
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')
Exemple #7
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)
Exemple #8
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')