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')
def test_vbox_manage_increase_cpu(self): vm_name = 'default' cpu_count = '4' mock_output = 'test output' check_output_mock = MagicMock(return_value='{}\n'.format(mock_output)) with patch('subprocess.check_output', check_output_mock): result = terminal.vbox_manage_increase_cpu(vm_name, cpu_count) self.assertEqual(result, mock_output) check_output_mock.assert_called_with(['VBoxManage', 'modifyvm', vm_name, '--cpus', cpu_count], universal_newlines=True)
def test_vbox_manage_increase_cpu(self): vm_name = 'default' cpu_count = '4' mock_output = 'test output' check_output_mock = MagicMock(return_value='{}\n'.format(mock_output)) with patch('subprocess.check_output', check_output_mock): result = terminal.vbox_manage_increase_cpu(vm_name, cpu_count) self.assertEqual(result, mock_output) check_output_mock.assert_called_with( ['VBoxManage', 'modifyvm', vm_name, '--cpus', cpu_count], universal_newlines=True)
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')
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)
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')