def create_config_file(resource, filename, force=False): """Create a new config file in user directory. Args: resource (str): Resource where to find the config file. filename (str): Name of the config file. force (bool): If True, config file in user directory is overwritten if it existed already. Otherwise, prompt the user. """ with path(resource, filename) as file: # get the file origin = Path(file) destination = directories.user_config_dir / filename # create directory destination.dirname().mkdir_p() # check destination does not exist if not force and destination.exists(): try: result = strtobool( input("{} already exists, overwrite? [y/N] ".format( destination))) except ValueError: result = False if not result: return # copy file origin.copyfile(destination) logger.info("Config created in '{}'".format(destination))
def run(self, options={}): if "id" in options: self.cp = self._client_requestor.Cp(options["id"]) with self._opv_directory_manager.Open( self.cp.pto_dir) as (_, pto_dirpath): proj_pto = Path(pto_dirpath) / Const.CP_PTO_FILENAME with self._opv_directory_manager.Open( self.cp.lot.pictures_path) as (_, pictures_dir): local_tmp_pto = Path(pictures_dir) / self.TMP_PTONAME logging.debug("Copy pto file " + proj_pto + " -> " + local_tmp_pto) proj_pto.copyfile(local_tmp_pto) self.stitch(local_tmp_pto) return json.dumps({"id": self.panorama.id})
def reinstall_repo_packages(self, remove_pkgs=None): """ Trigger a puppet apply to reinstall packages from a bigtop repository. Call this after a new bigtop version has been configured by the user. Typically called from a charm action, this method will: - optionally remove packages before reconfiguring the system - ensure the repo for the current bigtop version has priority - render a site-wide hiera.yaml pointing to the current bigtop source - trigger puppet apply :param: str remove_pkgs: 'apt-get remove' this prior to any reinstall """ # To support both upgrades and downgrades with this method, we may need # a package list that we can use to remove all charm-related packages # and ensure reinstalled versions are accurate. This can be any string # that would be valid for 'apt-get remove'. if remove_pkgs: cmd = ['apt-get', 'remove', '-qqy', remove_pkgs] try: subprocess.check_call(cmd) except subprocess.CalledProcessError as e: # NB: at this point, we have not altered our apt repo or # system-wide puppet config. Simply trigger puppet to put the # system back to the way it was. hookenv.log( 'Package removal failed; triggering puppet apply to return ' 'the system to the previous working state.', hookenv.ERROR) self.trigger_puppet() return e.output # Pin the new repo # NB: When configuring a new bigtop version, the repo priority is set # low. Set it higher so new repo packages will take precedent over the # currently installed versions. self.pin_bigtop_packages(priority=900) # Backup hiera.yaml; render a new one so it points to the new source hiera_src = Path('/etc/puppet/hiera.yaml') hiera_bak = Path('/etc/puppet/hiera.yaml.juju') hiera_src.copyfile(hiera_bak) self.render_hiera_yaml() # call puppet apply; roll back on failure try: self.trigger_puppet() except subprocess.CalledProcessError: hookenv.log( 'Puppet apply failed. Restoring hiera.yaml and repo to the ' 'previous working state.', hookenv.ERROR) hiera_bak.copyfile(hiera_src) self.pin_bigtop_packages(priority=10) return "failed" else: # We added a new repo when the bigtop version changed. Remove it # now that our reinstallation is complete. self.update_bigtop_repo(remove=True) # We've processed the version change; remove our changed state remove_state('bigtop.version.changed') return "success" finally: hiera_bak.remove()