def stageFile(pathname, context): """ Prepare a configuration file for modification. It involves making a copy of the previous version, then opening a temporary file for edition. """ stage_user = context.value('admin') stage_group = context.value('admin') new_path = context.modEtcDir + pathname org_path = context.tplEtcDir + pathname log_info('stage %s\n to %s\n original at %s' % (pathname, new_path, org_path)) if not os.path.exists(org_path): # We copy the original configuration file into the local build # directory before modifying it. # Note that we only do that the first time through so unless # the original (cache) directory is deleted, we donot overwrite # the original original files when the script is run a second time. # try: shell_command([ 'install', '-D', '-p', '-o', stage_user, '-g', stage_group, pathname, org_path ], admin=True) except Error as err: # We sometimes need sudo access to make backup copies of config # files (even ones with no credentials). This is just a convoluted # way to achieve the first copy before modification. pass if (not os.path.exists(os.path.dirname(new_path)) and len(os.path.dirname(new_path)) > 0): os.makedirs(os.path.dirname(new_path)) return org_path, new_path
def modify_config(pathname, settings={}, sep=' = ', enter_block_sep='{', exit_block_sep='}', one_per_line=False, context=None): # In the directory where the script is executed, the original configuration # file is saved into a "org" subdirectory while the updated configuration # is temporarly created into a "new" subdirectory before being copied # over the actual configuration. # The temporary files are created in the local directory and not in /tmp # because it does not seem a good idea to have important files such # as system configuration leaked outside a potentially encrypted drive. # The philosophy being the user is thus fully aware of what gets created # where and can thus make appropriate decisions about the commands he/she # runs. unchanged = {} log_info('configure ' + pathname + "...") org_config_path, new_config_path = stageFile(pathname, context) if os.path.exists(org_config_path): with open(org_config_path) as orgConfig: with open(new_config_path, 'w') as newConfig: unchanged = modify_config_file( newConfig, orgConfig, settings, sep=sep, enter_block_sep=enter_block_sep, exit_block_sep=exit_block_sep, one_per_line=one_per_line) else: logging.warning('%s does not exists.', org_config_path) # Add lines that did not previously appear in the configuration file. with open(new_config_path, 'w') as newConfig: writeSettings(newConfig, settings, [], sep=sep, one_per_line=one_per_line) return unchanged
def stageFile(pathname, context): """ Prepare a configuration file for modification. It involves making a copy of the previous version, then opening a temporary file for edition. """ stage_user = context.value('admin') stage_group = context.value('admin') new_path = context.MOD_SYSCONFDIR + pathname org_path = context.TPL_SYSCONFDIR + pathname log_info('stage %s\n to %s\n original at %s' % (pathname, new_path, org_path)) if not os.path.exists(org_path): # We copy the original configuration file into the local build # directory before modifying it. # Note that we only do that the first time through so unless # the original (cache) directory is deleted, we donot overwrite # the original original files when the script is run a second time. # try: shell_command([ 'install', '-D', '-p', '-o', stage_user, '-g', stage_group, pathname, org_path], admin=True) except Error as err: # We sometimes need sudo access to make backup copies of config # files (even ones with no credentials). This is just a convoluted # way to achieve the first copy before modification. pass if (not os.path.exists(os.path.dirname(new_path)) and len(os.path.dirname(new_path)) > 0): os.makedirs(os.path.dirname(new_path)) return org_path, new_path
def find_meminfo(dist_host): """ List information about memory usage """ tero.log_info("cat /proc/meminfo") with open('/proc/meminfo') as meminfo_file: for line in meminfo_file.readlines(): sys.stdout.write(line)
def modify_config(pathname, settings={}, sep=' = ', enter_block_sep='{', exit_block_sep='}', one_per_line=False, context=None): # In the directory where the script is executed, the original configuration # file is saved into a "org" subdirectory while the updated configuration # is temporarly created into a "new" subdirectory before being copied # over the actual configuration. # The temporary files are created in the local directory and not in /tmp # because it does not seem a good idea to have important files such # as system configuration leaked outside a potentially encrypted drive. # The philosophy being the user is thus fully aware of what gets created # where and can thus make appropriate decisions about the commands he/she # runs. unchanged = {} log_info('configure ' + pathname + "...") org_config_path, new_config_path = stageFile(pathname, context) if os.path.exists(org_config_path): with open(org_config_path) as orgConfig: with open(new_config_path, 'w') as newConfig: unchanged = modify_config_file(newConfig, orgConfig, settings, sep=sep, enter_block_sep=enter_block_sep, exit_block_sep=exit_block_sep, one_per_line=one_per_line) else: logging.warning('%s does not exists.', org_config_path) # Add lines that did not previously appear in the configuration file. with open(new_config_path, 'w') as newConfig: writeSettings(newConfig, settings, [], sep=sep, one_per_line=one_per_line) return unchanged