def __init__(self, debug=False): """Constructor. Args: debug: bool, True if debug output should write to the console. """ self.debug = debug facility = logging.handlers.SysLogHandler.LOG_DAEMON self.logger = logger.Logger( name='instance-setup', debug=self.debug, facility=facility) self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger) self.metadata_dict = None self.instance_config = instance_config.InstanceConfig(logger=self.logger) if self.instance_config.GetOptionBool('InstanceSetup', 'network_enabled'): self.metadata_dict = self.watcher.GetMetadata() instance_config_metadata = self._GetInstanceConfig() self.instance_config = instance_config.InstanceConfig( logger=self.logger, instance_config_metadata=instance_config_metadata) if self.instance_config.GetOptionBool('InstanceSetup', 'set_host_keys'): host_key_types = self.instance_config.GetOptionString( 'InstanceSetup', 'host_key_types') self._SetSshHostKeys(host_key_types=host_key_types) if self.instance_config.GetOptionBool('InstanceSetup', 'set_boto_config'): self._SetupBotoConfig() if self.instance_config.GetOptionBool( 'InstanceSetup', 'optimize_local_ssd'): self._RunScript('google_optimize_local_ssd') if self.instance_config.GetOptionBool('InstanceSetup', 'set_multiqueue'): self._RunScript('google_set_multiqueue') try: self.instance_config.WriteConfig() except (IOError, OSError) as e: self.logger.warning(str(e))
def __init__(self, debug=False): """Constructor. Args: debug: bool, True if debug output should write to the console. """ self.debug = debug facility = logging.handlers.SysLogHandler.LOG_DAEMON self.logger = logger.Logger(name='instance-setup', debug=self.debug, facility=facility) self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger) self.metadata_dict = None self.instance_config = instance_config.InstanceConfig( logger=self.logger) if self.instance_config.GetOptionBool('InstanceSetup', 'network_enabled'): self.metadata_dict = self.watcher.GetMetadata() instance_config_metadata = self._GetInstanceConfig() self.instance_config = instance_config.InstanceConfig( logger=self.logger, instance_config_metadata=instance_config_metadata) if self.instance_config.GetOptionBool('InstanceSetup', 'set_host_keys'): host_key_types = self.instance_config.GetOptionString( 'InstanceSetup', 'host_key_types') self._SetSshHostKeys(host_key_types=host_key_types) if self.instance_config.GetOptionBool('InstanceSetup', 'set_boto_config'): self._SetupBotoConfig() # machineType is e.g. u'projects/00000000000000/machineTypes/n1-standard-1' machineType = self.metadata_dict['instance']['machineType'].split( '/')[-1] if machineType.startswith( "e2-" ) and 'bsd' not in distro_name: # Not yet supported on BSD. subprocess.call(["sysctl", "vm.overcommit_memory=1"]) if self.instance_config.GetOptionBool('InstanceSetup', 'optimize_local_ssd'): self._RunScript('google_optimize_local_ssd') if self.instance_config.GetOptionBool('InstanceSetup', 'set_multiqueue'): self._RunScript('google_set_multiqueue') try: self.instance_config.WriteConfig() except (IOError, OSError) as e: self.logger.warning(str(e))
def testInstanceConfigError(self, mock_init, mock_set, mock_exists, mock_read): mock_read.side_effect = instance_config.parser.ParsingError('Error') mocks = mock.Mock() mocks.attach_mock(mock_init, 'init') mocks.attach_mock(mock_set, 'set') mocks.attach_mock(mock_read, 'read') mocks.attach_mock(mock_exists, 'exists') mocks.attach_mock(self.mock_logger, 'logger') mock_exists.return_value = True instance_config.InstanceConfig(logger=self.mock_logger, instance_config_metadata='Error') expected_calls = [ mock.call.init(config_file='template', config_header='/tmp/test.py template'), mock.call.logger.error(mock.ANY, mock.ANY), mock.call.exists('config'), mock.call.read('config'), mock.call.logger.error(mock.ANY, mock.ANY), mock.call.exists('distro'), mock.call.read('distro'), mock.call.logger.error(mock.ANY, mock.ANY), mock.call.set('first', 'a', 'false', overwrite=False), mock.call.set('second', 'b', 'true', overwrite=False), mock.call.set('third', 'c', '1', overwrite=False), mock.call.set('third', 'd', '2', overwrite=False), mock.call.set('third', 'e', '3', overwrite=False) ] self.assertEqual(mocks.mock_calls, expected_calls)
def testInstanceConfigExists(self, mock_init, mock_set, mock_parser, mock_exists): mock_config = mock.create_autospec( instance_config.parser.SafeConfigParser) mock_config.read = mock.Mock() mock_config.sections = mock.Mock() mock_config.sections.return_value = ['a', 'b'] mock_config.items = lambda key: {'key: %s' % key: 'value: %s' % key} mock_parser.SafeConfigParser.return_value = mock_config mocks = mock.Mock() mocks.attach_mock(mock_init, 'init') mocks.attach_mock(mock_set, 'set') mocks.attach_mock(mock_parser, 'parser') mocks.attach_mock(mock_exists, 'exists') mock_exists.return_value = True instance_config.InstanceConfig() expected_calls = [ mock.call.init(config_file='template', config_header='/tmp/test.py template'), mock.call.exists('config'), mock.call.parser.SafeConfigParser(), mock.call.parser.SafeConfigParser().read('config'), mock.call.parser.SafeConfigParser().sections(), mock.call.set('a', 'key: a', 'value: a', overwrite=False), mock.call.set('b', 'key: b', 'value: b', overwrite=False), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testInstanceConfigExists(self, mock_init, mock_set, mock_exists): config_parser = instance_config.parser.SafeConfigParser() config_metadata = '[first]\na = true' mock_config = mock.create_autospec( instance_config.parser.SafeConfigParser) with mock.patch('google_compute_engine.instance_setup.instance_config' '.parser') as mock_parser: mock_config.read = mock.Mock() mock_config.sections = mock.Mock() mock_config.sections.return_value = ['a', 'b'] mock_config.items = lambda key: { 'key: %s' % key: 'value: %s' % key } mock_parser.SafeConfigParser.side_effect = [ config_parser, mock_config, mock_config ] mocks = mock.Mock() mocks.attach_mock(mock_init, 'init') mocks.attach_mock(mock_set, 'set') mocks.attach_mock(mock_parser, 'parser') mocks.attach_mock(mock_config, 'config') mocks.attach_mock(mock_exists, 'exists') mocks.attach_mock(self.mock_logger, 'logger') mock_exists.return_value = True instance_config.InstanceConfig( logger=self.mock_logger, instance_config_metadata=config_metadata) expected_calls = [ mock.call.init(config_file='template', config_header='/tmp/test.py template'), mock.call.parser.SafeConfigParser(), mock.call.exists('config'), mock.call.parser.SafeConfigParser(), mock.call.config.read('config'), mock.call.config.sections(), mock.call.exists('distro'), mock.call.parser.SafeConfigParser(), mock.call.config.read('distro'), mock.call.config.sections(), mock.call.set('first', 'a', 'true', overwrite=False), mock.call.set('a', 'key: a', 'value: a', overwrite=False), mock.call.set('b', 'key: b', 'value: b', overwrite=False), mock.call.set('a', 'key: a', 'value: a', overwrite=False), mock.call.set('b', 'key: b', 'value: b', overwrite=False), mock.call.set('first', 'a', 'false', overwrite=False), mock.call.set('second', 'b', 'true', overwrite=False), mock.call.set('third', 'c', '1', overwrite=False), mock.call.set('third', 'd', '2', overwrite=False), mock.call.set('third', 'e', '3', overwrite=False) ] self.assertEqual(mocks.mock_calls, expected_calls)
def testInstanceConfig(self, mock_init, mock_set, mock_exists): mocks = mock.Mock() mocks.attach_mock(mock_init, 'init') mocks.attach_mock(mock_set, 'set') mocks.attach_mock(mock_exists, 'exists') mock_exists.return_value = False instance_config.InstanceConfig() expected_calls = [ mock.call.init(config_file='template', config_header='/tmp/test.py template'), mock.call.exists('config'), mock.call.set('first', 'a', 'false', overwrite=False), mock.call.set('second', 'b', 'true', overwrite=False), mock.call.set('third', 'c', '1', overwrite=False), mock.call.set('third', 'd', '2', overwrite=False), mock.call.set('third', 'e', '3', overwrite=False), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testWriteConfig(self, mock_write): mock_config = instance_config.InstanceConfig() instance_config.InstanceConfig.WriteConfig(mock_config) mock_write.assert_called_once_with(config_file='config')