def take_action(self, parsed_args): model, model_id = self.parse_model(parsed_args) params = { "owner_type": model, "owner_id": model_id, "role_name": parsed_args.name } file_path = self.get_file_path(parsed_args.directory, model, model_id, parsed_args.name, parsed_args.format) try: with open(file_path, 'r') as stream: data = data_utils.safe_load(parsed_args.format, stream) self.uploader(data, **params) except (OSError, IOError): msg = "Could not read description for role '{}' at {}".format( parsed_args.name, file_path) raise error.InvalidFileException(msg) msg = ("Description of role '{role}' for {owner} with id {id} was " "{action}d from {file_path}\n".format(role=parsed_args.name, owner=model, id=model_id, action=self.action, file_path=file_path)) self.app.stdout.write(msg)
def read_from_full_path(self, full_path): try: with open(full_path, "r") as file_to_read: return self.serializer["r"](file_to_read.read()) except IOError as e: raise error.InvalidFileException( "Can't open file '{0}': {1}.".format(full_path, e.strerror))
def read_file(cls, path): if not os.path.exists(path): raise error.InvalidFileException( "File '{0}' doesn't exist.".format(path)) serializer = Serializer() return serializer.read_from_full_path(path)
def write_to_full_path(self, path, data): try: with open(path, "w") as file_to_write: self.write_to_file(file_to_write, data) except IOError as e: raise error.InvalidFileException( "Can't write to file '{0}': {1}.".format(path, e.strerror)) return path
def take_action(self, parsed_args): file_path = self.get_config_path(parsed_args.directory, parsed_args.format) config = self.client.get_default_config() try: fileutils.ensure_tree(os.path.dirname(file_path)) fileutils.delete_if_exists(file_path) with open(file_path, 'w') as stream: data_utils.safe_dump(parsed_args.format, stream, config) except (OSError, IOError): msg = 'Could not store configuration at {}.' raise error.InvalidFileException(msg.format(file_path)) msg = "Configuration was stored in {path}\n" self.app.stdout.write(msg.format(path=file_path))
def take_action(self, parsed_args): file_path = parsed_args.config config = dict() if file_path: file_format = os.path.splitext(file_path)[1].lstrip('.') try: with open(file_path, 'r') as stream: config = data_utils.safe_load(file_format, stream) except (OSError, IOError): msg = 'Could not read configuration at {}.' raise error.InvalidFileException(msg.format(file_path)) result = self.client.create_snapshot(config) msg = "Diagnostic snapshot generation task with id {id} was started\n" self.app.stdout.write(msg.format(id=result.id))
def take_action(self, parsed_args): directory = parsed_args.directory file_path = self.get_attributes_path(self.attribute, parsed_args.format, parsed_args.id, directory) try: with open(file_path, 'r') as stream: attribute = data_utils.safe_load(parsed_args.format, stream) except (IOError, OSError): msg = 'Could not read configuration of {} at {}.' raise error.InvalidFileException( msg.format(self.attribute, file_path)) self.uploader(parsed_args.id, attribute) msg = ('Configuration of {t} for the environment with id ' '{env} was loaded from {path}\n') self.app.stdout.write( msg.format(t=self.attribute, env=parsed_args.id, path=file_path))
def take_action(self, parsed_args): model, model_id = self.parse_model(parsed_args) file_path = self.get_file_path(parsed_args.directory, model, model_id, parsed_args.name, parsed_args.format) data = self.client.get_one(model, model_id, parsed_args.name) try: fileutils.ensure_tree(os.path.dirname(file_path)) fileutils.delete_if_exists(file_path) with open(file_path, 'w') as stream: data_utils.safe_dump(parsed_args.format, stream, data) except (OSError, IOError): msg = ("Could not store description data " "for role {} at {}".format(parsed_args.name, file_path)) raise error.InvalidFileException(msg) msg = ("Description data of role '{}' within {} id {} " "was stored in {}\n".format(parsed_args.name, model, model_id, file_path)) self.app.stdout.write(msg)
def take_action(self, parsed_args): directory = parsed_args.directory attributes = self.downloader(parsed_args.id) file_path = self.get_attributes_path(self.attribute, parsed_args.format, parsed_args.id, directory) try: fileutils.ensure_tree(os.path.dirname(file_path)) fileutils.delete_if_exists(file_path) with open(file_path, 'w') as stream: data_utils.safe_dump(parsed_args.format, stream, attributes) except (OSError, IOError): msg = 'Could not store configuration of {} at {}.' raise error.InvalidFileException( msg.format(self.attribute, file_path)) msg = ('Configuration of {t} for node with id ' '{node} was stored in {path}\n') self.app.stdout.write( msg.format(t=self.attribute, node=parsed_args.id, path=file_path))
def check_file_path(cls, file_path): if not os.path.exists(file_path): raise error.InvalidFileException( "File '{0}' doesn't exist.".format(file_path))