docstring
stringlengths
52
499
function
stringlengths
67
35.2k
__index_level_0__
int64
52.6k
1.16M
Keep listening for events forever. Args: timeout_ms (int): How long to poll the Home Server for before retrying. exception_handler (func(exception)): Optional exception handler function which can be used to handle exceptions in the caller thr...
def listen_forever(self, timeout_ms=30000, exception_handler=None, bad_sync_timeout=5): _bad_sync_timeout = bad_sync_timeout self.should_listen = True while (self.should_listen): try: self._sync(timeout_ms) _bad_sync_tim...
239,845
Start a listener thread to listen for events in the background. Args: timeout (int): How long to poll the Home Server for before retrying. exception_handler (func(exception)): Optional exception handler function which can be used to handle exceptions in the...
def start_listener_thread(self, timeout_ms=30000, exception_handler=None): try: thread = Thread(target=self.listen_forever, args=(timeout_ms, exception_handler)) thread.daemon = True self.sync_thread = thread self.should_listen...
239,846
Upload content to the home server and recieve a MXC url. Args: content (bytes): The data of the content. content_type (str): The mimetype of the content. filename (str): Optional. Filename of the content. Raises: MatrixUnexpectedResponse: If the homeserv...
def upload(self, content, content_type, filename=None): try: response = self.api.media_upload(content, content_type, filename) if "content_uri" in response: return response["content_uri"] else: raise MatrixUnexpectedResponse( ...
239,848
Remove mapping of an alias Args: room_alias(str): The alias to be removed. Returns: bool: True if the alias is removed, False otherwise.
def remove_room_alias(self, room_alias): try: self.api.remove_room_alias(room_alias) return True except MatrixRequestError: return False
239,851
Update data on one-time keys count and upload new ones if necessary. Args: counts (dict): Counts of keys currently on the HS for each key type.
def update_one_time_key_counts(self, counts): self.one_time_keys_manager.server_counts = counts if self.one_time_keys_manager.should_upload(): logger.info('Uploading new one-time keys.') self.upload_one_time_keys()
239,859
Signs a JSON object. NOTE: The object is modified in-place and the return value can be ignored. As specified, this is done by encoding the JSON object without ``signatures`` or keys grouped as ``unsigned``, using canonical encoding. Args: json (dict): The JSON object to si...
def sign_json(self, json): signatures = json.pop('signatures', {}) unsigned = json.pop('unsigned', None) signature_base64 = self.olm_account.sign(encode_canonical_json(json)) key_id = 'ed25519:{}'.format(self.device_id) signatures.setdefault(self.user_id, {})[key_id] =...
239,860
Get this user's display name. Args: room (Room): Optional. When specified, return the display name of the user in this room. Returns: The display name. Defaults to the user ID if not set.
def get_display_name(self, room=None): if room: try: return room.members_displaynames[self.user_id] except KeyError: return self.user_id if not self.displayname: self.displayname = self.api.get_display_name(self.user_id) ...
239,863
Set this users display name. Args: display_name (str): Display Name
def set_display_name(self, display_name): self.displayname = display_name return self.api.set_display_name(self.user_id, display_name)
239,864
Add a row to the table Arguments: row - row of data, should be a list with as many elements as the table has fields
def add_row(self, row): if self._field_names and len(row) != len(self._field_names): raise Exception("Row has incorrect number of values, (actual) %d!=%d (expected)" %(len(row),len(self._field_names))) if not self._field_names: self.field_names = [("Field %d" % (n+1)) ...
240,102
Delete a row to the table Arguments: row_index - The index of the row you want to delete. Indexing starts at 0.
def del_row(self, row_index): if row_index > len(self._rows)-1: raise Exception("Cant delete row at index %d, table only has %d rows!" % (row_index, len(self._rows))) del self._rows[row_index]
240,103
Add a column to the table. Arguments: fieldname - name of the field to contain the new column of data column - column of data, should be a list with as many elements as the table has rows align - desired alignment for this column - "l" for left, "c" for centre and "r" for right...
def add_column(self, fieldname, column, align="c", valign="t"): if len(self._rows) in (0, len(column)): self._validate_align(align) self._validate_valign(valign) self._field_names.append(fieldname) self._align[fieldname] = align self._valign...
240,104
validate checks a kubernetes resource definition Args: definition (dict): resource definition version (str): version of kubernetes to validate against strict (bool): whether unexpected additional properties should be considered errors Returns: warnings (...
def validate(self, definition, version=None, strict=False): if not HAS_KUBERNETES_VALIDATE: raise KubernetesValidateMissing() errors = list() warnings = list() try: if version is None: try: version = self.version['kube...
240,130
A safety net. Decorator for functions that are only allowed to return True or raise an exception. Args: f: A function whose only expected return value is True. Returns: A wrapped functions whose guaranteed only return value is True.
def returns_true_or_raises(f): @functools.wraps(f) def wrapped(*args, **kwargs): ret = f(*args, **kwargs) if ret is not True: raise RuntimeError("Unexpected return value %r" % ret) return True return wrapped
241,921
Performs a request, and checks that the status is OK, and that the content-type matches expectations. Args: url: URL to request method: either 'get' or 'post' expected_content_type: prefix to match response content-type against **kwargs: passed to the request met...
def request_and_check(self, url, method='get', expected_content_type=None, **kwargs): assert method in ['get', 'post'] result = self.driver.request(method, url, **kwargs) if result.status_code != requests.codes.ok: raise RuntimeError('Error requesti...
242,310
Enable the list of network interfaces. Args: interfaces: list of string, the output device names to enable. logger: logger object, used to write to SysLog and serial port. dhclient_script: string, the path to a dhclient script used by dhclient.
def EnableNetworkInterfaces( self, interfaces, logger, dhclient_script=None): interfaces_to_up = [i for i in interfaces if i != 'eth0'] if interfaces_to_up: logger.info('Enabling the Ethernet interfaces %s.', interfaces_to_up) self._Dhcpcd(interfaces_to_up, logger)
242,924
Use dhcpcd to activate the interfaces. Args: interfaces: list of string, the output device names to enable. logger: logger object, used to write to SysLog and serial port.
def _Dhcpcd(self, interfaces, logger): for interface in interfaces: dhcpcd = ['/sbin/dhcpcd'] try: subprocess.check_call(dhcpcd + ['-x', interface]) except subprocess.CalledProcessError: # Dhcpcd not yet running for this device. logger.info('Dhcpcd not yet running for ...
242,925
Constructor. Args: logger: logger object, used to write to SysLog and serial port.
def __init__(self, logger=logging): self.logger = logger self.interfaces = self._CreateInterfaceMap()
242,926
Context manager for creating a temporary directory. Args: prefix: string, the prefix for the temporary directory. run_dir: string, the base directory location of the temporary directory. Yields: string, the temporary directory created.
def _CreateTempDir(prefix, run_dir=None): temp_dir = tempfile.mkdtemp(prefix=prefix + '-', dir=run_dir) try: yield temp_dir finally: shutil.rmtree(temp_dir)
242,929
Constructor. Args: script_type: string, the metadata script type to run. default_shell: string, the default shell to execute the script. run_dir: string, the base directory location of the temporary directory. debug: bool, True if debug output should write to the console.
def __init__( self, script_type, default_shell=None, run_dir=None, debug=False): self.script_type = script_type self.default_shell = default_shell name = '%s-script' % self.script_type facility = logging.handlers.SysLogHandler.LOG_DAEMON self.logger = logger.Logger(name=name, debug=debug,...
242,931
Retrieve metadata scripts and execute them. Args: run_dir: string, the base directory location of the temporary directory.
def _RunScripts(self, run_dir=None): with _CreateTempDir(self.script_type, run_dir=run_dir) as dest_dir: try: self.logger.info('Starting %s scripts.', self.script_type) script_dict = self.retriever.GetScripts(dest_dir) self.executor.RunScripts(script_dict) finally: s...
242,932
Constructor. Args: debug: bool, True if debug output should write to the console.
def __init__(self, debug=False): 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...
242,934
Run a script and log the streamed script output. Args: script: string, the file location of an executable script.
def _RunScript(self, script): process = subprocess.Popen( script, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) while True: for line in iter(process.stdout.readline, b''): self.logger.info(line.decode('utf-8').rstrip('\n')) if process.poll() is not None: ...
242,936
Generate a new SSH key. Args: key_type: string, the type of the SSH key. key_dest: string, a file location to store the SSH key.
def _GenerateSshKey(self, key_type, key_dest): # Create a temporary file to save the created RSA keys. with tempfile.NamedTemporaryFile(prefix=key_type, delete=True) as temp: temp_key = temp.name command = ['ssh-keygen', '-t', key_type, '-f', temp_key, '-N', '', '-q'] try: self.logger....
242,937
Regenerates SSH host keys when the VM is restarted with a new IP address. Booting a VM from an image with a known SSH key allows a number of attacks. This function will regenerating the host key whenever the IP address changes. This applies the first time the instance is booted, and each time the disk ...
def _SetSshHostKeys(self, host_key_types=None): section = 'Instance' instance_id = self._GetInstanceId() if instance_id != self.instance_config.GetOptionString( section, 'instance_id'): self.logger.info('Generating SSH host keys for instance %s.', instance_id) file_regex = re.compil...
242,939
Constructor. Args: logger: logger object, used to write to SysLog and serial port. script_type: string, the metadata script type to run.
def __init__(self, logger, script_type): self.logger = logger self.script_type = script_type self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger)
242,941
Download a Google Storage URL using an authentication token. If the token cannot be fetched, fallback to unauthenticated download. Args: url: string, the URL to download. dest_dir: string, the path to a directory for storing metadata scripts. Returns: string, the path to the file storin...
def _DownloadAuthUrl(self, url, dest_dir): dest_file = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False) dest_file.close() dest = dest_file.name self.logger.info( 'Downloading url from %s to %s using authentication token.', url, dest) if not self.token: response = self.wat...
242,942
Download a script from a given URL. Args: url: string, the URL to download. dest_dir: string, the path to a directory for storing metadata scripts. Returns: string, the path to the file storing the metadata script.
def _DownloadUrl(self, url, dest_dir): dest_file = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False) dest_file.close() dest = dest_file.name self.logger.info('Downloading url from %s to %s.', url, dest) try: urlretrieve.urlretrieve(url, dest) return dest except (httpclien...
242,943
Download the contents of the URL to the destination. Args: url: string, the URL to download. dest_dir: string, the path to a directory for storing metadata scripts. Returns: string, the path to the file storing the metadata script.
def _DownloadScript(self, url, dest_dir): # Check for the preferred Google Storage URL format: # gs://<bucket>/<object> if url.startswith(r'gs://'): # Convert the string into a standard URL. url = re.sub('^gs://', 'https://storage.googleapis.com/', url) return self._DownloadAuthUrl(ur...
242,944
Retrieve the scripts from attribute metadata. Args: attribute_data: dict, the contents of the attributes metadata. dest_dir: string, the path to a directory for storing metadata scripts. Returns: dict, a dictionary mapping metadata keys to files storing scripts.
def _GetAttributeScripts(self, attribute_data, dest_dir): script_dict = {} attribute_data = attribute_data or {} metadata_key = '%s-script' % self.script_type metadata_value = attribute_data.get(metadata_key) if metadata_value: self.logger.info('Found %s in metadata.', metadata_key) ...
242,945
Retrieve the scripts to execute. Args: dest_dir: string, the path to a directory for storing metadata scripts. Returns: dict, a dictionary mapping set metadata keys with associated scripts.
def GetScripts(self, dest_dir): metadata_dict = self.watcher.GetMetadata() or {} try: instance_data = metadata_dict['instance']['attributes'] except KeyError: instance_data = None self.logger.warning('Instance attributes were not found.') try: project_data = metadata_dict[...
242,946
Constructor. Args: logger: logger object, used to write to SysLog and serial port. script_type: string, the type of the script we are running. default_shell: string, the default shell to execute the script.
def __init__(self, logger, script_type, default_shell=None): self.logger = logger self.script_type = script_type self.default_shell = default_shell or '/bin/bash'
242,947
Add executable permissions to a file. Args: metadata_script: string, the path to the executable file.
def _MakeExecutable(self, metadata_script): mode = os.stat(metadata_script).st_mode os.chmod(metadata_script, mode | stat.S_IEXEC)
242,948
Run a script and log the streamed script output. Args: metadata_key: string, the key specifing the metadata script. metadata_script: string, the file location of an executable script.
def _RunScript(self, metadata_key, metadata_script): process = subprocess.Popen( metadata_script, shell=True, executable=self.default_shell, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) while True: for line in iter(process.stdout.readline, b''): message = line.dec...
242,949
Run the metadata scripts; execute a URL script first if one is provided. Args: script_dict: a dictionary mapping metadata keys to script files.
def RunScripts(self, script_dict): metadata_types = ['%s-script-url', '%s-script'] metadata_keys = [key % self.script_type for key in metadata_types] metadata_keys = [key for key in metadata_keys if script_dict.get(key)] if not metadata_keys: self.logger.info('No %s scripts found in metadata....
242,950
Constructor. Args: config_file: string, the location of the config file. config_header: string, the message to write at the top of the config.
def __init__(self, config_file=None, config_header=None): self.config_file = config_file or CONFIG self.config_header = config_header self.config = parser.Parser() self.config.read(self.config_file)
242,951
Create a file header in the config. Args: fp: int, a file pointer for writing the header.
def _AddHeader(self, fp): text = textwrap.wrap( textwrap.dedent(self.config_header), break_on_hyphens=False) fp.write('\n'.join(['# ' + line for line in text])) fp.write('\n\n')
242,952
Get the value of an option in the config file. Args: section: string, the section of the config file to check. option: string, the option to retrieve the value of. Returns: string, the value of the option or None if the option doesn't exist.
def GetOptionString(self, section, option): if self.config.has_option(section, option): return self.config.get(section, option) else: return None
242,953
Get the value of an option in the config file. Args: section: string, the section of the config file to check. option: string, the option to retrieve the value of. Returns: bool, True if the option is enabled or not set.
def GetOptionBool(self, section, option): return (not self.config.has_option(section, option) or self.config.getboolean(section, option))
242,954
Set the value of an option in the config file. Args: section: string, the section of the config file to check. option: string, the option to set the value of. value: string, the value to set the option. overwrite: bool, True to overwrite an existing value in the config file.
def SetOption(self, section, option, value, overwrite=True): if not overwrite and self.config.has_option(section, option): return if not self.config.has_section(section): self.config.add_section(section) self.config.set(section, option, str(value))
242,955
Write the config values to a given file. Args: config_file: string, the file location of the config file to write.
def WriteConfig(self, config_file=None): config_file = config_file or self.config_file config_name = os.path.splitext(os.path.basename(config_file))[0] config_lock = ( '%s/lock/google_%s.lock' % (constants.LOCALSTATEDIR, config_name)) with file_utils.LockFile(config_lock): with open(c...
242,956
Get a logging object with handlers for sending logs to SysLog. Args: name: string, the name of the logger which will be added to log entries. debug: bool, True if debug output should write to the console. facility: int, an encoding of the SysLog handler's facility and priority. Returns: logging ob...
def Logger(name, debug=False, facility=None): logger = logging.getLogger(name) logger.handlers = [] logger.addHandler(logging.NullHandler()) logger.propagate = False logger.setLevel(logging.DEBUG) formatter = logging.Formatter(name + ': %(levelname)s %(message)s') if debug: # Create a handler for ...
242,957
Configure a Linux user account. Args: user: string, the name of the Linux user account to create. Returns: bool, True if user creation succeeded.
def _AddUser(self, user): self.logger.info('Creating a new user account for %s.', user) command = self.useradd_cmd.format(user=user) try: subprocess.check_call(command.split(' ')) except subprocess.CalledProcessError as e: self.logger.warning('Could not create user %s. %s.', user, str(...
242,960
Update group membership for a Linux user. Args: user: string, the name of the Linux user account. groups: list, the group names to add the user as a member. Returns: bool, True if user update succeeded.
def _UpdateUserGroups(self, user, groups): groups = ','.join(groups) self.logger.debug('Updating user %s with groups %s.', user, groups) command = self.usermod_cmd.format(user=user, groups=groups) try: subprocess.check_call(command.split(' ')) except subprocess.CalledProcessError as e: ...
242,961
Update the authorized keys file for a Linux user with a list of SSH keys. Args: user: string, the name of the Linux user account. ssh_keys: list, the SSH key strings associated with the user. Raises: IOError, raised when there is an exception updating a file. OSError, raised when setti...
def _UpdateAuthorizedKeys(self, user, ssh_keys): pw_entry = self._GetUser(user) if not pw_entry: return uid = pw_entry.pw_uid gid = pw_entry.pw_gid home_dir = pw_entry.pw_dir ssh_dir = os.path.join(home_dir, '.ssh') # Not all sshd's support multiple authorized_keys files so we h...
242,962
Update sudoer group membership for a Linux user account. Args: user: string, the name of the Linux user account. sudoer: bool, True if the user should be a sudoer. Returns: bool, True if user update succeeded.
def _UpdateSudoer(self, user, sudoer=False): if sudoer: self.logger.info('Adding user %s to the Google sudoers group.', user) command = self.gpasswd_add_cmd.format( user=user, group=self.google_sudoers_group) else: self.logger.info('Removing user %s from the Google sudoers group...
242,963
Remove a Linux user account's authorized keys file to prevent login. Args: user: string, the Linux user account to remove access.
def _RemoveAuthorizedKeys(self, user): pw_entry = self._GetUser(user) if not pw_entry: return home_dir = pw_entry.pw_dir authorized_keys_file = os.path.join(home_dir, '.ssh', 'authorized_keys') if os.path.exists(authorized_keys_file): try: os.remove(authorized_keys_file) ...
242,964
Set the list of configured Google user accounts. Args: users: list, the username strings of the Linux accounts.
def SetConfiguredUsers(self, users): prefix = self.logger.name + '-' with tempfile.NamedTemporaryFile( mode='w', prefix=prefix, delete=True) as updated_users: updated_users_file = updated_users.name for user in users: updated_users.write(user + '\n') updated_users.flush() ...
242,966
Update a Linux user with authorized SSH keys. Args: user: string, the name of the Linux user account. ssh_keys: list, the SSH key strings associated with the user. Returns: bool, True if the user account updated successfully.
def UpdateUser(self, user, ssh_keys): if not bool(USER_REGEX.match(user)): self.logger.warning('Invalid user account name %s.', user) return False if not self._GetUser(user): # User does not exist. Attempt to create the user and add them to the # appropriate user groups. if no...
242,967
Remove a Linux user account. Args: user: string, the Linux user account to remove.
def RemoveUser(self, user): self.logger.info('Removing user %s.', user) if self.remove: command = self.userdel_cmd.format(user=user) try: subprocess.check_call(command.split(' ')) except subprocess.CalledProcessError as e: self.logger.warning('Could not remove user %s. %s....
242,968
Constructor. Args: logger: logger object, used to write to SysLog and serial port.
def __init__(self, logger): self.logger = logger self.oslogin_installed = True self.update_time = 0
242,969
Run the OS Login control script. Args: params: list, the params to pass to the script Returns: int, the return code from the call, or None if the script is not found.
def _RunOsLoginControl(self, params): try: return subprocess.call([constants.OSLOGIN_CONTROL_SCRIPT] + params) except OSError as e: if e.errno == errno.ENOENT: return None else: raise
242,970
Check whether OS Login is installed. Args: two_factor: bool, True if two factor should be enabled. Returns: bool, True if OS Login is installed.
def _GetStatus(self, two_factor=False): params = ['status'] if two_factor: params += ['--twofactor'] retcode = self._RunOsLoginControl(params) if retcode is None: if self.oslogin_installed: self.logger.warning('OS Login not installed.') self.oslogin_installed = False ...
242,971
Update whether OS Login is enabled and update NSS cache if necessary. Args: oslogin_desired: bool, enable OS Login if True, disable if False. two_factor_desired: bool, enable two factor if True, disable if False. Returns: int, the return code from updating OS Login, or None if not present.
def UpdateOsLogin(self, oslogin_desired, two_factor_desired=False): oslogin_configured = self._GetStatus(two_factor=False) if oslogin_configured is None: return None two_factor_configured = self._GetStatus(two_factor=True) # Two factor can only be enabled when OS Login is enabled. two_fac...
242,974
Configure the network interfaces using dhclient. Args: interfaces: list of string, the output device names to enable. logger: logger object, used to write to SysLog and serial port. dhclient_script: string, the path to a dhclient script used by dhclient.
def CallDhclient( interfaces, logger, dhclient_script=None): logger.info('Enabling the Ethernet interfaces %s.', interfaces) dhclient_command = ['dhclient'] if dhclient_script and os.path.exists(dhclient_script): dhclient_command += ['-sf', dhclient_script] try: subprocess.check_call(dhclient_...
242,975
Sync clock using hwclock. Args: logger: logger object, used to write to SysLog and serial port.
def CallHwclock(logger): command = ['/sbin/hwclock', '--hctosys'] try: subprocess.check_call(command) except subprocess.CalledProcessError: logger.warning('Failed to sync system time with hardware clock.') else: logger.info('Synced system time with hardware clock.')
242,976
Sync clock using ntpdate. Args: logger: logger object, used to write to SysLog and serial port.
def CallNtpdate(logger): ntpd_inactive = subprocess.call(['service', 'ntpd', 'status']) try: if not ntpd_inactive: subprocess.check_call(['service', 'ntpd', 'stop']) subprocess.check_call( 'ntpdate `awk \'$1=="server" {print $2}\' /etc/ntp.conf`', shell=True) if not ntpd_inactive: ...
242,977
Constructor. Args: dhclient_script: string, the path to a dhclient script used by dhclient. dhcp_command: string, a command to enable Ethernet interfaces. debug: bool, True if debug output should write to the console.
def __init__(self, dhclient_script=None, dhcp_command=None, debug=False): self.dhclient_script = dhclient_script or '/sbin/google-dhclient-script' self.dhcp_command = dhcp_command facility = logging.handlers.SysLogHandler.LOG_DAEMON self.logger = logger.Logger( name='network-setup', debug=d...
242,978
Enable the list of network interfaces. Args: interfaces: list of string, the output device names to enable.
def EnableNetworkInterfaces(self, interfaces): # The default Ethernet interface is enabled by default. Do not attempt to # enable interfaces if only one interface is specified in metadata. if not interfaces or set(interfaces) == self.interfaces: return self.logger.info('Ethernet interfaces: ...
242,979
Enable the list of network interfaces. Args: interfaces: list of string, the output device names to enable. logger: logger object, used to write to SysLog and serial port. dhclient_script: string, the path to a dhclient script used by dhclient.
def EnableNetworkInterfaces( self, interfaces, logger, dhclient_script=None): helpers.CallDhclient(interfaces, logger, dhclient_script=dhclient_script)
242,980
Constructor. Args: project_id: string, the project ID to use in the config file. debug: bool, True if debug output should write to the console.
def __init__(self, project_id=None, debug=False): self.logger = logger.Logger(name='boto-setup', debug=debug) self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger) self._CreateConfig(project_id)
242,981
Create the boto config to support standalone GSUtil. Args: project_id: string, the project ID to use in the config file.
def _CreateConfig(self, project_id): project_id = project_id or self._GetNumericProjectId() # Our project doesn't support service accounts. if not project_id: return self.boto_config_header %= ( self.boto_config_script, self.boto_config_template) config = config_manager.ConfigMa...
242,983
Constructor. Inherit from the ConfigManager class. Read the template for instance defaults and write new sections and options. This prevents package updates from overriding user set defaults. Args: logger: logger object, used to write to SysLog and serial port. instance_config_metadata: st...
def __init__(self, logger=logging, instance_config_metadata=None): self.logger = logger self.instance_config_metadata = instance_config_metadata self.instance_config_header %= ( self.instance_config_script, self.instance_config_template) # User provided instance configs should always take p...
242,984
Constructor. Args: logger: logger object, used to write to SysLog and serial port. proto_id: string, the routing protocol identifier for Google IP changes.
def __init__(self, logger, proto_id=None): self.logger = logger self.proto_id = proto_id or '66'
242,985
Create a dictionary of parameters to append to the ip route command. Args: **kwargs: dict, the string parameters to update in the ip route command. Returns: dict, the string parameters to append to the ip route command.
def _CreateRouteOptions(self, **kwargs): options = { 'proto': self.proto_id, 'scope': 'host', } options.update(kwargs) return options
242,986
Run a command with ip route and return the response. Args: args: list, the string ip route command args to execute. options: dict, the string parameters to append to the ip route command. Returns: string, the standard output from the ip route command execution.
def _RunIpRoute(self, args=None, options=None): args = args or [] options = options or {} command = ['ip', 'route'] command.extend(args) for item in options.items(): command.extend(item) try: process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subproces...
242,987
Parse and validate forwarded IP addresses. Args: forwarded_ips: list, the IP address strings to parse. Returns: list, the valid IP address strings.
def ParseForwardedIps(self, forwarded_ips): addresses = [] forwarded_ips = forwarded_ips or [] for ip in forwarded_ips: if ip and (IP_REGEX.match(ip) or IP_ALIAS_REGEX.match(ip)): addresses.append(ip[:-3] if ip.endswith('/32') else ip) else: self.logger.warning('Could not pa...
242,988
Retrieve the list of configured forwarded IP addresses. Args: interface: string, the output device to query. interface_ip: string, current interface ip address. Returns: list, the IP address strings.
def GetForwardedIps(self, interface, interface_ip=None): args = ['ls', 'table', 'local', 'type', 'local'] options = self._CreateRouteOptions(dev=interface) result = self._RunIpRoute(args=args, options=options) result = re.sub(r'local\s', r'', result) return self.ParseForwardedIps(result.split()...
242,989
Configure a new IP address on the network interface. Args: address: string, the IP address to configure. interface: string, the output device to use.
def AddForwardedIp(self, address, interface): address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address args = ['add', 'to', 'local', address] options = self._CreateRouteOptions(dev=interface) self._RunIpRoute(args=args, options=options)
242,990
Parse and validate forwarded IP addresses. Args: forwarded_ips: list, the IP address strings to parse. Returns: list, the valid IP address strings.
def ParseForwardedIps(self, forwarded_ips): addresses = [] forwarded_ips = forwarded_ips or [] for ip in forwarded_ips: if ip and (IP_REGEX.match(ip) or IP_ALIAS_REGEX.match(ip)): addresses.extend([str(addr) for addr in list(netaddr.IPNetwork(ip))]) else: self.logger.warning...
242,991
Retrieve the list of configured forwarded IP addresses. Args: interface: string, the output device to query. interface_ip: string, current interface ip address. Returns: list, the IP address strings.
def GetForwardedIps(self, interface, interface_ip=None): try: ips = netifaces.ifaddresses(interface) ips = ips[netifaces.AF_INET] except (ValueError, IndexError): return [] forwarded_ips = [] for ip in ips: if ip['addr'] != interface_ip: full_addr = '%s/%d' % (ip['ad...
242,992
Configure a new IP address on the network interface. Args: address: string, the IP address to configure. interface: string, the output device to use.
def AddForwardedIp(self, address, interface): for ip in list(netaddr.IPNetwork(address)): self._RunIfconfig(args=[interface, 'alias', '%s/32' % str(ip)])
242,993
Delete an IP address on the network interface. Args: address: string, the IP address to configure. interface: string, the output device to use.
def RemoveForwardedIp(self, address, interface): ip = netaddr.IPNetwork(address) self._RunIfconfig(args=[interface, '-alias', str(ip.ip)])
242,994
Constructor. Args: debug: bool, True if debug output should write to the console.
def __init__(self, debug=False): facility = logging.handlers.SysLogHandler.LOG_DAEMON self.logger = logger.Logger( name='google-clock-skew', debug=debug, facility=facility) self.distro_utils = distro_utils.Utils(debug=debug) self.watcher = metadata_watcher.MetadataWatcher(logger=self.logger...
242,999
Called when clock drift token changes. Args: response: string, the metadata response with the new drift token value.
def HandleClockSync(self, response): self.logger.info('Clock drift token has changed: %s.', response) self.distro_utils.HandleClockSync(self.logger)
243,000
Enable the list of network interfaces. Args: interfaces: list of string, the output device names to enable. logger: logger object, used to write to SysLog and serial port. dhclient_script: string, the path to a dhclient script used by dhclient.
def EnableNetworkInterfaces( self, interfaces, logger, dhclient_script=None): # Should always exist in EL 7. if os.path.exists(self.network_path): self._DisableNetworkManager(interfaces, logger) helpers.CallDhclient(interfaces, logger)
243,001
Disable network manager management on a list of network interfaces. Args: interfaces: list of string, the output device names enable. logger: logger object, used to write to SysLog and serial port.
def _DisableNetworkManager(self, interfaces, logger): for interface in interfaces: interface_config = os.path.join( self.network_path, 'ifcfg-%s' % interface) if os.path.exists(interface_config): self._ModifyInterface( interface_config, 'DEVICE', interface, replace=Fal...
243,002
Write a value to a config file if not already present. Args: interface_config: string, the path to a config file. config_key: string, the configuration key to set. config_value: string, the value to set for the configuration key. replace: bool, replace the configuration option if already pr...
def _ModifyInterface( self, interface_config, config_key, config_value, replace=False): config_entry = '%s=%s' % (config_key, config_value) if not open(interface_config).read().count(config_key): with open(interface_config, 'a') as config: config.write('%s\n' % config_entry) elif re...
243,003
Parse the SSH key data into a user map. Args: account_data: string, the metadata server SSH key attributes data. Returns: dict, a mapping of the form: {'username': ['sshkey1, 'sshkey2', ...]}.
def _ParseAccountsData(self, account_data): if not account_data: return {} lines = [line for line in account_data.splitlines() if line] user_map = {} for line in lines: if not all(ord(c) < 128 for c in line): self.logger.info('SSH key contains non-ascii character: %s.', line) ...
243,007
Get dictionaries for instance and project attributes. Args: metadata_dict: json, the deserialized contents of the metadata server. Returns: tuple, two dictionaries for instance and project attributes.
def _GetInstanceAndProjectAttributes(self, metadata_dict): metadata_dict = metadata_dict or {} try: instance_data = metadata_dict['instance']['attributes'] except KeyError: instance_data = {} self.logger.warning('Instance attributes were not found.') try: project_data = me...
243,008
Get the user accounts specified in metadata server contents. Args: metadata_dict: json, the deserialized contents of the metadata server. Returns: dict, a mapping of the form: {'username': ['sshkey1, 'sshkey2', ...]}.
def _GetAccountsData(self, metadata_dict): instance_data, project_data = self._GetInstanceAndProjectAttributes( metadata_dict) valid_keys = [instance_data.get('sshKeys'), instance_data.get('ssh-keys')] block_project = instance_data.get('block-project-ssh-keys', '').lower() if block_project ...
243,009
Provision and update Linux user accounts based on account metadata. Args: update_users: dict, authorized users mapped to their public SSH keys.
def _UpdateUsers(self, update_users): for user, ssh_keys in update_users.items(): if not user or user in self.invalid_users: continue configured_keys = self.user_ssh_keys.get(user, []) if set(ssh_keys) != set(configured_keys): if not self.utils.UpdateUser(user, ssh_keys): ...
243,010
Deprovision Linux user accounts that do not appear in account metadata. Args: remove_users: list, the username strings of the Linux accounts to remove.
def _RemoveUsers(self, remove_users): for username in remove_users: self.utils.RemoveUser(username) self.user_ssh_keys.pop(username, None) self.invalid_users -= set(remove_users)
243,011
Get the value of the enable-oslogin metadata key. Args: metadata_dict: json, the deserialized contents of the metadata server. Returns: bool, True if OS Login is enabled for VM access.
def _GetEnableOsLoginValue(self, metadata_dict): instance_data, project_data = self._GetInstanceAndProjectAttributes( metadata_dict) instance_value = instance_data.get('enable-oslogin') project_value = project_data.get('enable-oslogin') value = instance_value or project_value or '' ret...
243,012
Called when there are changes to the contents of the metadata server. Args: result: json, the deserialized contents of the metadata server.
def HandleAccounts(self, result): self.logger.debug('Checking for changes to user accounts.') configured_users = self.utils.GetConfiguredUsers() enable_oslogin = self._GetEnableOsLoginValue(result) enable_two_factor = self._GetEnableTwoFactorValue(result) if enable_oslogin: desired_users ...
243,013
Set the appropriate SELinux context, if SELinux tools are installed. Calls /sbin/restorecon on the provided path to set the SELinux context as specified by policy. This call does not operate recursively. Only some OS configurations use SELinux. It is therefore acceptable for restorecon to be missing, in which...
def _SetSELinuxContext(path): restorecon = '/sbin/restorecon' if os.path.isfile(restorecon) and os.access(restorecon, os.X_OK): subprocess.call([restorecon, path])
243,014
Set the permissions and ownership of a path. Args: path: string, the path for which owner ID and group ID needs to be setup. mode: octal string, the permissions to set on the path. uid: int, the owner ID to be set for the path. gid: int, the group ID to be set for the path. mkdir: bool, True if t...
def SetPermissions(path, mode=None, uid=None, gid=None, mkdir=False): if mkdir and not os.path.exists(path): os.mkdir(path, mode or 0o777) elif mode: os.chmod(path, mode) if uid and gid: os.chown(path, uid, gid) _SetSELinuxContext(path)
243,015
Lock the provided file descriptor. Args: fd: int, the file descriptor of the file to lock. path: string, the name of the file to lock. blocking: bool, whether the function should return immediately. Raises: IOError, raised from flock while attempting to lock a file.
def Lock(fd, path, blocking): operation = fcntl.LOCK_EX if blocking else fcntl.LOCK_EX | fcntl.LOCK_NB try: fcntl.flock(fd, operation) except IOError as e: if e.errno == errno.EWOULDBLOCK: raise IOError('Exception locking %s. File already locked.' % path) else: raise IOError('Exception ...
243,016
Release the lock on the file. Args: fd: int, the file descriptor of the file to unlock. path: string, the name of the file to lock. Raises: IOError, raised from flock while attempting to release a file lock.
def Unlock(fd, path): try: fcntl.flock(fd, fcntl.LOCK_UN | fcntl.LOCK_NB) except IOError as e: if e.errno == errno.EWOULDBLOCK: raise IOError('Exception unlocking %s. Locked by another process.' % path) else: raise IOError('Exception unlocking %s. %s.' % (path, str(e)))
243,017
Interface to flock-based file locking to prevent concurrent executions. Args: path: string, the name of the file to lock. blocking: bool, whether the function should return immediately. Yields: None, yields when a lock on the file is obtained. Raises: IOError, raised from flock locking operatio...
def LockFile(path, blocking=False): fd = os.open(path, os.O_CREAT) try: Lock(fd, path, blocking) yield finally: try: Unlock(fd, path) finally: os.close(fd)
243,018
Constructor. Args: logger: logger object, used to write to SysLog and serial port. timeout: int, timeout in seconds for metadata requests.
def __init__(self, logger=None, timeout=60): self.etag = 0 self.logger = logger or logging self.timeout = timeout
243,021
Performs a GET request with the metadata headers. Args: metadata_url: string, the URL to perform a GET request on. params: dictionary, the query parameters in the GET request. timeout: int, timeout in seconds for metadata requests. Returns: HTTP response from the GET request. Rais...
def _GetMetadataRequest(self, metadata_url, params=None, timeout=None): headers = {'Metadata-Flavor': 'Google'} params = urlparse.urlencode(params or {}) url = '%s?%s' % (metadata_url, params) request = urlrequest.Request(url, headers=headers) request_opener = urlrequest.build_opener(urlrequest...
243,022
Update the etag from an API response. Args: response: HTTP response with a header field. Returns: bool, True if the etag in the response header updated.
def _UpdateEtag(self, response): etag = response.headers.get('etag', self.etag) etag_updated = self.etag != etag self.etag = etag return etag_updated
243,023
Request the contents of metadata server and deserialize the response. Args: metadata_key: string, the metadata key to watch for changes. recursive: bool, True if we should recursively watch for metadata changes. wait: bool, True if we should wait for a metadata change. timeout: int, timeout...
def _GetMetadataUpdate( self, metadata_key='', recursive=True, wait=True, timeout=None): metadata_key = os.path.join(metadata_key, '') if recursive else metadata_key metadata_url = os.path.join(METADATA_SERVER, metadata_key) params = { 'alt': 'json', 'last_etag': self.etag, ...
243,024
Wait for a successful metadata response. Args: metadata_key: string, the metadata key to watch for changes. recursive: bool, True if we should recursively watch for metadata changes. wait: bool, True if we should wait for a metadata change. timeout: int, timeout in seconds for returning met...
def _HandleMetadataUpdate( self, metadata_key='', recursive=True, wait=True, timeout=None, retry=True): exception = None while True: try: return self._GetMetadataUpdate( metadata_key=metadata_key, recursive=recursive, wait=wait, timeout=timeout) excep...
243,025
Watch for changes to the contents of the metadata server. Args: handler: callable, a function to call with the updated metadata contents. metadata_key: string, the metadata key to watch for changes. recursive: bool, True if we should recursively watch for metadata changes. timeout: int, tim...
def WatchMetadata( self, handler, metadata_key='', recursive=True, timeout=None): while True: response = self._HandleMetadataUpdate( metadata_key=metadata_key, recursive=recursive, wait=True, timeout=timeout) try: handler(response) except Exception as e: ...
243,026
Retrieve the contents of metadata server for a metadata key. Args: metadata_key: string, the metadata key to watch for changes. recursive: bool, True if we should recursively watch for metadata changes. timeout: int, timeout in seconds for returning metadata output. retry: bool, True if we ...
def GetMetadata( self, metadata_key='', recursive=True, timeout=None, retry=True): return self._HandleMetadataUpdate( metadata_key=metadata_key, recursive=recursive, wait=False, timeout=timeout, retry=retry)
243,027
Constructor. Args: proto_id: string, the routing protocol identifier for Google IP changes. debug: bool, True if debug output should write to the console.
def __init__(self, proto_id=None, debug=False): facility = logging.handlers.SysLogHandler.LOG_DAEMON self.logger = logger.Logger( name='google-ip-forwarding', debug=debug, facility=facility) self.ip_forwarding_utils = ip_forwarding_utils.IpForwardingUtils( logger=self.logger, proto_id=p...
243,028
Log the planned IP address changes. Args: configured: list, the IP address strings already configured. desired: list, the IP address strings that will be configured. to_add: list, the forwarded IP address strings to configure. to_remove: list, the forwarded IP address strings to delete. ...
def _LogForwardedIpChanges( self, configured, desired, to_add, to_remove, interface): if not to_add and not to_remove: return self.logger.info( 'Changing %s IPs from %s to %s by adding %s and removing %s.', interface, configured or None, desired or None, to_add or None, ...
243,029
Configure the forwarded IP address on the network interface. Args: forwarded_ips: list, the forwarded IP address strings to configure. interface: string, the output device to use.
def _AddForwardedIps(self, forwarded_ips, interface): for address in forwarded_ips: self.ip_forwarding_utils.AddForwardedIp(address, interface)
243,030
Remove the forwarded IP addresses from the network interface. Args: forwarded_ips: list, the forwarded IP address strings to delete. interface: string, the output device to use.
def _RemoveForwardedIps(self, forwarded_ips, interface): for address in forwarded_ips: self.ip_forwarding_utils.RemoveForwardedIp(address, interface)
243,031