示例#1
0
    def list(self, output_type="JSON"):
        """ Dump world data in a few different formats."""

        # initialize our final dict
        d = copy(utils.api_meta)

        d["meta"]["object"]["panel_revision"] = utils.settings.get(
            "application", "panel_revision")

        d["world"] = collections.OrderedDict()

        # get the raw world info (we'll sort it later)
        raw_world = {}
        for asset in utils.mdb.world.find():
            raw_world[asset["handle"]] = asset
            raw_world_age = datetime.now() - asset['created_on']
            raw_world[
                asset["handle"]]["age_in_seconds"] = raw_world_age.seconds

        #
        # This is where we filter data key/value pairs from being returned
        #   by calls to the /world route
        #

        def recursive_key_del(chk_d, f_key):
            if f_key in chk_d.keys():
                del chk_d[f_key]

            for k in chk_d.keys():
                if type(chk_d[k]) == dict:
                    recursive_key_del(chk_d[k], f_key)

        for banned_key in ["max_age", "email", "admins"]:
            recursive_key_del(raw_world, banned_key)

        # sort the world dict
        key_order = sorted(raw_world, key=lambda x: raw_world[x]["name"])
        for k in key_order:
            d["world"][k] = raw_world[k]

        # output options from here down:

        spacer = 45
        if output_type == "CLI":
            spacer = 25
            print("\n\tWarehouse data:\n")
            for k, v in d["world"].iteritems():
                utils.cli_dump(k, spacer, v)
                print("")
        elif output_type == "keys":
            return d["world"].keys()
        elif output_type == "keys_cli":
            output = ""
            for k in sorted(d["world"].keys()):
                utils.cli_dump(k, spacer, d["world"][k]["_id"])
            return output
        elif output_type == dict:
            return d
        elif output_type == "JSON":
            return json.dumps(d, default=json_util.default)
示例#2
0
    def dump(self, asset_handle):
        """ Prints a single asset to STDOUT. CLI admin functionality. """

        asset = utils.mdb.world.find_one({"handle": asset_handle})
        print("\n\t%s\n" % asset_handle)
        spacer = 20
        for k, v in asset.iteritems():
            utils.cli_dump(k, spacer, v)
        print("\n")
示例#3
0
    def dump_status(self, output_type="CLI"):
        """ Prints daemon status to stdout. """

        active = False
        d = {"active": active}
        if self.pid is not None and os.path.isfile(self.pid_file_path):
            active = True

        if active:
            owner_uid = os.stat(self.pid_file_path).st_uid
            owner_name = getpwuid(owner_uid).pw_name

            try:
                utils.mdb.world.find()
            except Exception as e:
                self.logger.error(
                    "Daemon is active, but MDB cannot be reached!")
                self.logger.exception(e)
                raise

            d = {}
            d["active"] = active
            d["up_since"] = self.get_uptime("date")
            d["uptime_hms"] = self.get_uptime()
            d["owner_uid"] = owner_uid
            d["owner_name"] = owner_name
            d["pid"] = self.pid
            d["pid_file"] = self.pid_file_path
            d["assets"] = utils.mdb.world.find().count()

        if output_type == dict:
            return d
        elif output_type == "CLI":
            spacer = 15
            print("\n\tWorld Daemon stats:\n")
            for k, v in sorted(d.iteritems()):
                utils.cli_dump(k, spacer, v)
            print("\n")