def run(self, context, args, kwargs, opargs): if len(args) != 1 and 'name' not in kwargs: raise CommandException("Please specify fragment of the image name") name = kwargs.get('name') or args[0] return Table(context.call_sync('docker.image.search', name), [ Table.Column('Name', 'name', width=30), Table.Column('Description', 'description') ])
def set_aux_groups(self, entity, value): groups = self.context.entity_subscribers['group'].query( ('name', 'in', list(value))) diff_groups = set.difference(set(value), set(x['name'] for x in groups)) if len(diff_groups): raise CommandException( _('Groups {0} do not exist.'.format(', '.join(diff_groups)))) entity['groups'] = [group['id'] for group in groups]
def run(self, context, args, kwargs, opargs): vol = kwargs.get('volume', None) if not vol: raise CommandException(_('Please specify a volume name')) tid = context.submit_task( 'system_dataset.import', vol, callback=lambda s, t: post_save(self.parent, s, t)) return TaskPromise(context, tid)
def run(self, context, args, kwargs, opargs): try: new_be_name = args.pop(0) except IndexError: raise CommandException('Please provide a target name for the renaming') entity = self.parent.entity name_property = self.parent.get_mapping('name') name_property.do_set(entity, new_be_name) self.parent.modified = True self.parent.save()
def get_show(obj): if isinstance(obj, ConfigNamespace): obj.load() commands = obj.commands() if 'show' in commands: instance = commands['show'] return instance.run(context, '', '', '') else: raise CommandException( _("Namespace {0} does not have 'show' command".format( obj.name)))
def run(self, context, args, kwargs, opargs): if len(args) != 1 and 'name' not in kwargs: raise CommandException("Please specify the image name") name = kwargs.get('name') or args[0] readme = context.call_sync('docker.image.readme', name) if readme: return Sequence(readme) else: return Sequence("Image {0} readme does not exist".format(args[0]))
def run(self, context, args, kwargs, opargs): if len(args) < 1 or not isinstance(args[0], Quote): raise CommandException("Provide code fragment to evaluate") start = datetime.now() result = context.eval(args[0].body) end = datetime.now() msg = "Execution time: {0} seconds".format( (end - start).total_seconds()) return Sequence(*(result + [msg]))
def run(self, context, args, kwargs, opargs): desired_range = None if args: if len(args) != 1: raise CommandException( _("Invalid Syntax for history command. For help see 'help <command>'" )) try: desired_range = int(args[0]) except ValueError: raise CommandException( _("Please specify an integer for the history range")) histroy_range = readline.get_current_history_length() if desired_range is not None and desired_range < histroy_range: histroy_range = desired_range + 1 return Table( [{ 'cmd': readline.get_history_item(i) } for i in range(1, histroy_range)], [Table.Column('Command History', 'cmd', ValueType.STRING)])
def set_type(o, v): elem = self.context.entity_subscribers[ self.entity_subscriber_name].query( ('type', '=', v), select='name', single=True, ) if elem: raise CommandException( _("Only one instance of type: {0} allowed".format(v))) o['type'] = v
def run(self, context, args, kwargs, opargs): if not kwargs.get('username') or not kwargs.get('password'): raise CommandException( 'You have to provide a bug tracking system password and username in order to submit a ticket' ) if not kwargs.get('subject'): raise CommandException( _('You have to provide a subject for a ticket')) if not kwargs.get('description'): raise CommandException( _('You have to provide a description for a ticket')) if not kwargs.get('type'): raise CommandException( _('You have to provide a type of the ticket: bug/feature')) if not kwargs.get('category'): raise CommandException( _('You have to provide a category for the ticket')) if not kwargs.get('attach_debug_data'): kwargs['debug'] = True else: kwargs['debug'] = True if kwargs.pop( 'attach_debug_data') == 'yes' else False if kwargs.get('attachments') and isinstance(kwargs['attachments'], str): kwargs['attachments'] = [kwargs['attachments']] if not self.ticket_categories: self.ticket_categories.update( context.call_sync('support.categories', kwargs['username'], kwargs['password'])) kwargs['category'] = self.ticket_categories[kwargs['category']] kwargs['password'] = Password(kwargs['password']) tid = context.submit_task('support.submit', kwargs) return TaskPromise(context, tid)
def run(self, context, args, kwargs, opargs, input=None): ns = context.pipe_cwd available_props = [x.name for x in ns.property_mappings if x.list] if len(args) == 0: raise CommandException( _("Please specify a property field. Available properties are: {0}" .format(','.join(available_props)))) field = args[0] if ns.has_property(field): field = ns.get_mapping(field).get_name else: raise CommandException( _("Please specify a property field. Available properties are: {0}" .format(','.join(available_props)))) if isinstance(input, Table): input.data = [{'result': x.get(field)} for x in input.data] input.columns = [Table.Column('Result', 'result')] return input
def run(self, context, args, kwargs, opargs): if (args or len(kwargs) > 1 or ('reboot' not in kwargs and len(kwargs) == 1)): raise CommandException(_( "Incorrect syntax {0} {1}\n{2}".format(args, kwargs, inspect.getdoc(self)) )) self.context = context self.reboot = read_value(kwargs.get('reboot', self.reboot), tv=ValueType.BOOLEAN) self.task_id = context.submit_task( 'update.updatenow', self.reboot, callback=self.task_callback )
def run(self, context, args, kwargs, opargs): if not kwargs: raise CommandException( _("Export requires more arguments. For help see 'help export'") ) if 'path' not in kwargs: raise CommandException( _("Please specify path where the certificate should be exported. " "For help see 'help import'")) if self.parent.entity['certificate']: p = Path( PurePath(kwargs['path']).joinpath( self.parent.entity['name']).with_suffix('.crt')) with p.open('w') as f: f.writelines(self.parent.entity['certificate']) if self.parent.entity['privatekey']: p = Path( PurePath(kwargs['path']).joinpath( self.parent.entity['name']).with_suffix('.key')) with p.open('w') as f: f.writelines(self.parent.entity['privatekey'])
def run(self, context, args, kwargs, opargs): if len(args) == 0: context.variables.save() return "Environment Variables Saved to file: {0}".format( context.variables.save_to_file) if len(args) == 1: context.variables.save(args[0]) return "Environment Variables Saved to file: {0}".format(args[0]) if len(args) > 1: raise CommandException( _("Incorrect syntax: {0}\n{1}".format(args, inspect.getdoc(self))))
def set_disks(self, obj, disk_names): all_disks = self.context.entity_subscribers['disk'].query( select='name') for d in disk_names: if d not in all_disks: raise CommandException( _("Invalid disk: {0}, see '/ disk show' for a list of disks" .format(d))) self.set_task_args( obj, self.context.entity_subscribers['disk'].query( ('name', 'in', list(disk_names)), select='id'), 'disks')
def run(self, context, args, kwargs, opargs): if len(args) == 0: self.variables.save() return "Environment Variables Saved to file: {0}".format( context.variables.save_to_file) if len(args) == 1: self.variables.save(args[0]) return "Environment Variables Saved to file: {0}".format(args[0]) if len(args) > 1: raise CommandException( _("Incorrect syntax: {0}. For help see 'help <command>'". format(args)))
def check_disks(context, disks, cache_disks=None, log_disks=None): all_disks = [disk["path"] for disk in context.call_sync("disk.query")] available_disks = context.call_sync('volume.get_available_disks') if cache_disks is not None: for disk in cache_disks: disk = correct_disk_path(disk) if disk not in all_disks: raise CommandException(_("Disk {0} does not exist.".format(disk))) if disk in available_disks: available_disks.remove(disk) else: raise CommandException(_("Disk {0} is not available.".format(disk))) if log_disks is not None: for disk in log_disks: disk = correct_disk_path(disk) if disk not in all_disks: raise CommandException(_("Disk {0} does not exist.".format(disk))) if disk in available_disks: available_disks.remove(disk) else: raise CommandException(_("Disk {0} is not available.".format(disk))) if 'auto' in disks: return 'auto', cache_disks, log_disks else: for disk in disks: disk = correct_disk_path(disk) if disk not in all_disks: raise CommandException(_("Disk {0} does not exist.".format(disk))) if disk not in available_disks: raise CommandException(_("Disk {0} is not available.".format(disk))) return disks, cache_disks, log_disks
def set_disks(self, entity, args): all_disks = [ disk["path"] for disk in self.context.call_sync("disk.query") ] disks = [] for disk in args: disk = correct_disk_path(disk) if disk not in all_disks: raise CommandException( _("Invalid disk: {0}, see '/ disk show' for a list of disks" .format(disk))) disks.append(disk) self.set_args(entity, disks, 'disks')
def run(self, context, args, kwargs, opargs): host = kwargs.get('host', None) name = q.get(self.parent.entity, 'names.0') if host: host = context.call_sync('docker.host.query', [('name', '=', host)], {'single': True, 'select': 'id'}) if not host: raise CommandException(_('Docker host {0} not found'.format(kwargs.get('host')))) if host not in self.parent.entity['hosts']: raise CommandException(_('Image {0} does not exist on {1}'.format( name, kwargs.get('host') ))) tid = context.submit_task( 'docker.image.delete', name, host, callback=lambda s, t: post_save(self.parent, s, t) ) return TaskPromise(context, tid)
def run(self, context, args, kwargs, opargs): ns = self.exec_path[-1] if len(args) > 1: raise CommandException(_('Invalid syntax: {0}. For help see "help <command>"'.format(args))) result = [] if getattr(ns, 'serialize'): try: for i in ns.serialize(): result.append(unparse(i)) except NotImplementedError: return contents = '\n'.join(result) if len(args) == 1: filename = args[0] try: with open(filename, 'w') as f: f.write(contents) except IOError: raise CommandException(_('Error writing to file {0}'.format(filename))) return _('Configuration successfully dumped to file {0}'.format(filename)) else: return contents
def run(self, context, args, kwargs, opargs): if len(args) == 0: raise CommandException(_("Please specify a disk")) disk = args[0] volume = self.parent.entity disk = correct_disk_path(disk) vdevs = list(iterate_vdevs(volume['topology'])) guid = None for vdev in vdevs: if vdev['path'] == disk: guid = vdev['guid'] break if guid is None: raise CommandException(_("Disk {0} is not part of the volume.".format(disk))) context.submit_task( 'volume.vdev.online', self.parent.entity['id'], guid, callback=lambda s, t: post_save(self.parent, s, t) )
def run(self, context, args, kwargs, opargs): name = self.parent.entity['name'] compress = kwargs.pop('compress', None) encrypt = kwargs.pop('encrypt', None) throttle = kwargs.pop('throttle', None) transport_plugins = [] if compress: if compress not in ['fast', 'default', 'best']: raise CommandException('Compression level must be selected as one of: fast, default, best') transport_plugins.append({ 'name': 'compress', 'level': compress.upper() }) if throttle: if not isinstance(throttle, int): raise CommandException('Throttle must be a number representing maximum transfer per second') transport_plugins.append({ 'name': 'throttle', 'buffer_size': throttle }) if encrypt: if encrypt not in ['AES128', 'AES192', 'AES256']: raise CommandException('Encryption type must be selected as one of: AES128, AES192, AES256') transport_plugins.append({ 'name': 'encrypt', 'type': encrypt }) context.submit_task( 'replication.sync', name, transport_plugins, callback=lambda s, t: post_save(self.parent, s, t) )
def run(self, context, args, kwargs, opargs): if len(args) < 1: raise CommandException('Not enough arguments passed') id = args[0] oldname = args[0] if 'key' in kwargs: if 'disks' not in kwargs: raise CommandException('You have to provide list of disks when importing an encrypted volume') disks = kwargs['disks'] if isinstance(disks, str): disks = [disks] correct_disks = [] for dname in disks: correct_disks.append(correct_disk_path(dname)) encryption = {'key': kwargs['key'], 'disks': correct_disks} password = kwargs.get('password', None) else: encryption = {} password = None if not args[0].isdigit(): vols = context.call_sync('volume.find') vol = first_or_default(lambda v: v['name'] == args[0], vols) if not vol: raise CommandException('Importable volume {0} not found'.format(args[0])) id = vol['id'] oldname = vol['name'] context.submit_task('volume.import', id, kwargs.get('newname', oldname), {}, encryption, password)
def map_opargs(opargs, context): ns = context.pipe_cwd mapped_opargs = [] for k, o, v in opargs: if ns.has_property(k): mapping = ns.get_mapping(k) mapped_opargs.append((mapping.name, o, read_value(v, mapping.type))) else: raise CommandException( _('Property {0} not found, valid properties are: {1}'.format( k, ','.join([x.name for x in ns.property_mappings if x.list])))) return mapped_opargs
def run(self, context, args, kwargs, opargs): if not args and not kwargs: raise CommandException(_( "create requires more arguments, see 'help create' for more information" )) if len(args) > 1: raise CommandException(_( "Wrong syntax for create, see 'help create' for more information" )) if len(args) == 1: if 'name' in kwargs: raise CommandException(_( "Both implicit and explicit 'name' parameters are specified." )) else: kwargs[self.parent.primary_key.name] = args.pop(0) if 'name' not in kwargs: raise CommandException(_('Please specify a name for your snapshot')) else: name = kwargs.pop('name') descr = kwargs.pop('description', '') ns = get_item_stub(context, self.parent, name) tid = context.submit_task( self.parent.create_task, self.parent.parent.entity['id'], name, descr, callback=lambda s, t: post_save(ns, s, t) ) return EntityPromise(context, tid, ns)
def run(self, context, args, kwargs, opargs): if not args: raise CommandException( _("'mkdir' requires 1 argument. For help see 'help mkdir'")) self.parent.curr_obj.mkdir(args[0]) output_msg(_(">{0}".format(str(self.parent.curr_obj)))) contents = [{ 'name': o.name, 'type': o.type.name } for o in self.parent.curr_obj.readdir()] return Table(contents, [ Table.Column('Name', 'name'), Table.Column('Type', 'type'), ])
def run(self, context, args, kwargs, opargs): if args: try: tid = int(args[0]) except ValueError: raise CommandException('Task id argument must be an integer') else: tid = None try: tid = context.global_env.find('_last_task_id').value except KeyError: pass if tid is None: return 'No recently submitted tasks (which are still active) found' return context.wait_for_task_with_progress(tid)
def run(self, context, args, kwargs, opargs): if not args: raise CommandException( _("Open requires 1 argument. For help see 'help open'")) self.parent.curr_obj = FileProvider.open( args[0], remote_logpass=self.parent.remote_logpass) output_msg( _("Connection opened: {0}".format(str(self.parent.curr_obj)))) contents = [{ 'name': o.name, 'type': o.type.name } for o in self.parent.curr_obj.readdir()] return Table(contents, [ Table.Column('Name', 'name'), Table.Column('Type', 'type'), ])
def run(self, context, args, kwargs, opargs): if not self.parent.entity.get('bidirectional'): raise CommandException('This replication link is not bi-directional') name = self.parent.entity['name'] partners = self.parent.entity['partners'] master = self.parent.entity['master'] for partner in partners: if partner != master: master = partner break context.submit_task( 'replication.update', name, {'master': master}, callback=lambda s, t: post_save(self.parent, s, t) )
def run(self, context, args, kwargs, opargs): result = context.call_task_sync('backup.query', self.parent.entity['id']) if result['state'] != 'FINISHED': raise CommandException('Failed to query backup: {0}'.format( q.get(result, 'error.message'))) manifest = result['result'] return Sequence( Object( Object.Item('Hostname', 'hostname', manifest['hostname']), Object.Item('Dataset', 'dataset', manifest['dataset']), ), Table(manifest['snapshots'], [ Table.Column('Snapshot name', 'name', ValueType.STRING), Table.Column('Incremental', 'incremental', ValueType.BOOLEAN), Table.Column('Created at', 'created_at', ValueType.TIME) ]))