def __init__(
      self, proto_id=None, ip_aliases=True, target_instance_ips=True,
      debug=False):
    """Constructor.

    Args:
      proto_id: string, the routing protocol identifier for Google IP changes.
      ip_aliases: bool, True if the guest should configure IP alias routes.
      target_instance_ips: bool, True supports internal IP load balancing.
      debug: bool, True if debug output should write to the console.
    """
    facility = logging.handlers.SysLogHandler.LOG_DAEMON
    self.logger = logger.Logger(
        name='google-ip-forwarding', debug=debug, facility=facility)
    self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
    self.network_utils = network_utils.NetworkUtils(logger=self.logger)
    self.ip_forwarding_utils = ip_forwarding_utils.IpForwardingUtils(
        logger=self.logger, proto_id=proto_id)
    self.ip_aliases = ip_aliases
    self.target_instance_ips = target_instance_ips
    try:
      with file_utils.LockFile(LOCKFILE):
        self.logger.info('Starting Google IP Forwarding daemon.')
        timeout = 60 + random.randint(0, 30)
        self.watcher.WatchMetadata(
            self.HandleNetworkInterfaces, metadata_key=self.network_interfaces,
            recursive=True, timeout=timeout)
    except (IOError, OSError) as e:
      self.logger.warning(str(e))
Esempio n. 2
0
    def __init__(self, groups=None, remove=False, debug=False):
        """Constructor.

    Args:
      groups: string, a comma separated list of groups.
      remove: bool, True if deprovisioning a user should be destructive.
      debug: bool, True if debug output should write to the console.
    """
        facility = logging.handlers.SysLogHandler.LOG_DAEMON
        self.logger = logger.Logger(name='google-accounts',
                                    debug=debug,
                                    facility=facility)
        self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
        self.utils = accounts_utils.AccountsUtils(logger=self.logger,
                                                  groups=groups,
                                                  remove=remove)
        try:
            with file_utils.LockFile(LOCKFILE):
                self.logger.info('Starting Google Accounts daemon.')
                timeout = 60 + random.randint(0, 30)
                self.watcher.WatchMetadata(self.HandleAccounts,
                                           recursive=True,
                                           timeout=timeout)
        except (IOError, OSError) as e:
            self.logger.warning(str(e))
Esempio n. 3
0
    def __init__(self, debug=False):
        """Constructor.

    Args:
      debug: bool, True if debug output should write to the console.
    """
        facility = logging.handlers.SysLogHandler.LOG_DAEMON
        self.logger = logger.Logger(name='google-diagnostics',
                                    debug=debug,
                                    facility=facility)
        self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
        try:
            with file_utils.LockFile(LOCKFILE):
                self.logger.info('Starting Google Diagnostics daemon.')
                self.watcher.WatchMetadata(self.HandleDiagnostics,
                                           metadata_key=self.diagnose_token,
                                           recursive=False)
                self.watcher.WatchMetadata(
                    self.SetInstanceDiagnosticsEnabled,
                    metadata_key=self.instance_diagnostics_token,
                    recursive=False)
                self.watcher.WatchMetadata(
                    self.SetProjectDiagnosticsEnabled,
                    metadata_key=self.project_diagnostics_token,
                    recursive=False)
        except (IOError, OSError) as e:
            self.logger.warning(str(e))
Esempio n. 4
0
    def __init__(self,
                 ip_forwarding_enabled,
                 proto_id,
                 ip_aliases,
                 target_instance_ips,
                 dhclient_script,
                 dhcp_command,
                 network_setup_enabled,
                 debug=False):
        """Constructor.

    Args:
      ip_forwarding_enabled: bool, True if ip forwarding is enabled.
      proto_id: string, the routing protocol identifier for Google IP changes.
      ip_aliases: bool, True if the guest should configure IP alias routes.
      target_instance_ips: bool, True supports internal IP load balancing.
      dhclient_script: string, the path to a dhclient script used by dhclient.
      dhcp_command: string, a command to enable Ethernet interfaces.
      network_setup_enabled: bool, True if network setup is enabled.
      debug: bool, True if debug output should write to the console.
    """
        facility = logging.handlers.SysLogHandler.LOG_DAEMON
        self.logger = logger.Logger(name='google-networking',
                                    debug=debug,
                                    facility=facility)
        self.ip_aliases = ip_aliases
        self.ip_forwarding_enabled = ip_forwarding_enabled
        self.network_setup_enabled = network_setup_enabled
        self.target_instance_ips = target_instance_ips
        self.dhclient_script = dhclient_script

        self.ip_forwarding = ip_forwarding.IpForwarding(proto_id=proto_id,
                                                        debug=debug)
        self.network_setup = network_setup.NetworkSetup(
            dhclient_script=dhclient_script,
            dhcp_command=dhcp_command,
            debug=debug)
        self.network_utils = network_utils.NetworkUtils(logger=self.logger)
        self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
        self.distro_utils = distro_utils.Utils(debug=debug)

        try:
            with file_utils.LockFile(LOCKFILE):
                self.logger.info('Starting Google Networking daemon.')
                timeout = 60 + random.randint(0, 30)
                self.watcher.WatchMetadata(
                    self.HandleNetworkInterfaces,
                    metadata_key=self.instance_metadata_key,
                    recursive=True,
                    timeout=timeout)
        except (IOError, OSError) as e:
            self.logger.warning(str(e))
Esempio n. 5
0
    def WriteConfig(self, config_file=None):
        """Write the config values to a given file.

    Args:
      config_file: string, the file location of the config file to write.
    """
        config_file = config_file or self.config_file
        config_name = os.path.splitext(os.path.basename(config_file))[0]
        config_lock = '/var/lock/google_%s.lock' % config_name
        with file_utils.LockFile(config_lock):
            with open(config_file, 'w') as config_fp:
                if self.config_header:
                    self._AddHeader(config_fp)
                self.config.write(config_fp)
Esempio n. 6
0
    def __init__(self,
                 groups=None,
                 remove=False,
                 gpasswd_add_cmd=None,
                 gpasswd_remove_cmd=None,
                 groupadd_cmd=None,
                 useradd_cmd=None,
                 userdel_cmd=None,
                 usermod_cmd=None,
                 debug=False):
        """Constructor.

    Args:
      groups: string, a comma separated list of groups.
      remove: bool, True if deprovisioning a user should be destructive.
      useradd_cmd: string, command to create a new user.
      userdel_cmd: string, command to delete a user.
      usermod_cmd: string, command to modify user's groups.
      groupadd_cmd: string, command to add a new group.
      gpasswd_add_cmd: string, command to add an user to a group.
      gpasswd_remove_cmd: string, command to remove an user from a group.
      debug: bool, True if debug output should write to the console.
    """
        facility = logging.handlers.SysLogHandler.LOG_DAEMON
        self.logger = logger.Logger(name='google-accounts',
                                    debug=debug,
                                    facility=facility)
        self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
        self.utils = accounts_utils.AccountsUtils(
            logger=self.logger,
            groups=groups,
            remove=remove,
            gpasswd_add_cmd=gpasswd_add_cmd,
            gpasswd_remove_cmd=gpasswd_remove_cmd,
            groupadd_cmd=groupadd_cmd,
            useradd_cmd=useradd_cmd,
            userdel_cmd=userdel_cmd,
            usermod_cmd=usermod_cmd)
        self.oslogin = oslogin_utils.OsLoginUtils(logger=self.logger)

        try:
            with file_utils.LockFile(LOCKFILE):
                self.logger.info('Starting Google Accounts daemon.')
                timeout = 60 + random.randint(0, 30)
                self.watcher.WatchMetadata(self.HandleAccounts,
                                           recursive=True,
                                           timeout=timeout)
        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.
    """
    facility = logging.handlers.SysLogHandler.LOG_DAEMON
    self.logger = logger.Logger(
        name='google-clock-skew', debug=debug, facility=facility)
    self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
    try:
      with file_utils.LockFile(LOCKFILE):
        self.logger.info('Starting Google Clock Skew daemon.')
        self.watcher.WatchMetadata(
            self.HandleClockSync, metadata_key=self.drift_token,
            recursive=False)
    except (IOError, OSError) as e:
      self.logger.warning(str(e))
    def __init__(self, proto_id=None, debug=False):
        """Constructor.

    Args:
      proto_id: string, the routing protocol identifier for Google IP changes.
      debug: bool, True if debug output should write to the console.
    """
        facility = logging.handlers.SysLogHandler.LOG_DAEMON
        self.logger = logger.Logger(name='google-ip-forwarding',
                                    debug=debug,
                                    facility=facility)
        self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
        self.utils = ip_forwarding_utils.IpForwardingUtils(logger=self.logger,
                                                           proto_id=proto_id)
        try:
            with file_utils.LockFile(LOCKFILE):
                self.logger.info('Starting Google IP Forwarding daemon.')
                self.watcher.WatchMetadata(self.HandleForwardedIps,
                                           metadata_key=self.forwarded_ips,
                                           recursive=True)
        except (IOError, OSError) as e:
            self.logger.warning(str(e))
    def testLockFile(self, mock_os, mock_lock, mock_unlock):
        mock_callable = mock.Mock()
        mock_os.open.return_value = self.fd
        mock_os.O_CREAT = 1
        mocks = mock.Mock()
        mocks.attach_mock(mock_callable, 'callable')
        mocks.attach_mock(mock_lock, 'lock')
        mocks.attach_mock(mock_unlock, 'unlock')
        mocks.attach_mock(mock_os.open, 'open')
        mocks.attach_mock(mock_os.close, 'close')

        with file_utils.LockFile(self.path, blocking=True):
            mock_callable('test')

        expected_calls = [
            mock.call.open(self.path, 1),
            mock.call.lock(self.fd, self.path, True),
            mock.call.callable('test'),
            mock.call.unlock(self.fd, self.path),
            mock.call.close(1),
        ]
        self.assertEqual(mocks.mock_calls, expected_calls)
Esempio n. 10
0
This patch won't be necessary anymore in further versions.
See https://github.com/GoogleCloudPlatform/compute-image-packages/pull/440

--- google_compute_engine/config_manager.py.orig	2017-07-23 07:12:16 UTC
+++ google_compute_engine/config_manager.py
@@ -21,7 +21,7 @@ import textwrap
 from google_compute_engine import file_utils
 from google_compute_engine.compat import parser
 
-CONFIG = '/etc/default/instance_configs.cfg'
+CONFIG = '%%PREFIX%%/etc/instance_configs.cfg'
 
 
 class ConfigManager(object):
@@ -101,7 +101,7 @@ class ConfigManager(object):
     """
     config_file = config_file or self.config_file
     config_name = os.path.splitext(os.path.basename(config_file))[0]
-    config_lock = '/var/lock/google_%s.lock' % config_name
+    config_lock = '/var/spool/lock/google_%s.lock' % config_name
     with file_utils.LockFile(config_lock):
       with open(config_file, 'w') as config_fp:
         if self.config_header: