def execute(self, args, remaining_args): self.apply_overrides(DaemonThread.settings_schema(), args.overrides) subcmd = getattr(args, self.ARG_SUBCOMMAND) config = DaemonThread.settings_value() # Assure the caller may actually call us res = IDParser().parse(login_name()) if config.authentication.privileged_group not in [g[1] for g in res.groups]: self.log().error("Your are not authorized to run this program - user '%s' is not in group '%s'" % (login_name(), config.authentication.privileged_group)) return 255 #end check authentication session = PackageSession.new(url=config.db.url) if subcmd == self.MODE_LIST: return self._handle_list(args, session) elif subcmd == self.MODE_TRANSACTION: return self._handle_transactions(args, session) else: raise NotImplemented("Subcommand '%s' unknown" % subcmd)
def __init__(self, name): """make sure platform instances for the current OS are available""" super(OSContext, self).__init__(name) # instantiate platform singleton try: inst = self.platform_service_type() value = self.settings_value(self._kvstore, resolve=False) if not value.platform.id: value.platform.id = inst.id(inst.ID_FULL) # Enforce the actual system user value.user.login = login_name() value.user.home = Path('~').expanduser() value.host.fqname = socket.gethostname() value.host.name = value.host.fqname.split('.')[0] self.settings().set_value_by_schema(self.settings_schema(), value) except KeyError: raise EnvironmentError("Unknown platform %s" % sys.platform)
def _cache_dir(self): """@return an existing directory into which caches can be written safely. Doesn't necessarily exist""" return Path(tempfile.gettempdir()) / ('bkvstore-py%i%i-%s' % (sys.version_info[:2] + (login_name(),)))
def _handle_transactions(self, args, session): """Implement transaction subcommand""" if args.action in self.query_actions: if args.reason: self.log().warn("--reason has no effect in query actions like %s", ', '.join(self.query_actions)) # end handle reason else: if not args.reason: self.log().error("Please specify a reason for performing the '%s' action, use the --reason argument", args.action) return self.ERROR # end need reason # end assure reason is set try: for tid in getattr(args, self.ARG_TRANSACTION_IDS): trans = session.query(SQLPackageTransaction).filter(SQLPackageTransaction.id == tid)[:] if not trans: raise ValueError("No transaction found with id %i" % tid) # end fail on missing transactions trans = trans[0] if args.action == self.ACTION_APPROVE: if trans.finished_at is not None or trans.started_at is not None: raise ValueError("Transaction %i is already done and cannot be approved after the fact" % tid) # end handle finished transactions trans.approved_by_login = login_name() self.log().info("Approved %i" % tid) elif args.action == self.ACTION_REJECT: trans.reject_approval() self.log().info("Rejected %i" % tid) elif args.action == self.ACTION_CANCEL: trans.cancel() self.log().info("Canceled %i" % tid) elif args.action == self.ACTION_LIST_FILES: print "Files for transaction %i" % tid report = Report(columns=self.report_schema_files) record = report.records.append for f in trans.files: try: uid = getpwuid(f.uid).pw_name except KeyError: uid = f.uid # end remap uid if possible record((f.path, f.size, uid, f.gid, f.mode)) # end for each file report.serialize(Report.SERIALIZE_TTY, sys.stdout.write) else: raise NotImplemented("unknown action: %s" % args.action) # end handle action # Always keep the reason around if args.action not in self.query_actions: assert args.reason trans.reason = args.reason # end assure reason is set only in edit mode # end for each tid session.commit() except Exception, err: self.log().error(str(err)) if args.action not in self.query_actions: session.rollback() self.log().error("Failed to set transaction - all progress rolled back") # end don't warn if we are read-only return self.ERROR