def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')

        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.remove_custom_info("distributor",
                                                      distributor['id'],
                                                      keyname)

        ident = dist_uuid if dist_uuid else dist_name

        if len(response) == 0:
            print _(
                "Successfully removed Custom Information from Distributor [ %s ]"
            ) % ident
        else:
            print _(
                "Could not remove Custom Information from Distributor [ %s ]"
            ) % ident
示例#2
0
    def run(self):
        org_name = self.get_option("org")
        env_name = self.get_option("environment")
        sys_name = self.get_option("name")
        sys_uuid = self.get_option("uuid")
        keyname = self.get_option("keyname")
        new_value = self.get_option("value")

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("system", system["id"], keyname, new_value)

        ident = sys_uuid if sys_uuid else sys_name

        if response is not None:
            print _("Successfully updated Custom Information [ %(keyname)s ] for System [ %(ident)s ]") % {
                "keyname": keyname,
                "ident": ident,
            }
        else:
            print _("Could not update Custom Information [ %(keyname)s ] for System [ %(ident)s ]") % {
                "keyname": keyname,
                "ident": ident,
            }
示例#3
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')

        if dist_uuid:
            self.printer.set_header(_("Distributor Information [ %s ]") % dist_uuid)
        elif env_name is None:
            self.printer.set_header(_("Distributor Information For Org [ %s ]") % org_name)
        else:
            self.printer.set_header(_("Distributor Information For Environment [ %(env)s ] in Org [ %(org)s ]") \
                % {'env':env_name, 'org':org_name})

        # get distributor details
        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()
        custom_info = custom_info_api.get_custom_info("distributor", distributor['id'])
        distributor['custom_info'] = stringify_custom_info(custom_info)

        if 'environment' in distributor:
            distributor['environment'] = distributor['environment']['name']

        batch_add_columns(self.printer, {'name': _("Name")}, {'version': _("Version")},
            {'uuid': _("UUID")}, {'environment': _("Environment")})
        self.printer.add_column('created_at', _("Created"), formatter=format_date)
        self.printer.add_column('updated_at', _("Last Updated"), formatter=format_date)
        self.printer.add_column('description', _("Description"), multiline=True)
        self.printer.add_column('custom_info', _("Custom Info"), multiline=True, show_with=printer.VerboseStrategy)

        self.printer.print_item(distributor)

        return os.EX_OK
示例#4
0
文件: system.py 项目: lzap/katello
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')

        if sys_uuid:
            self.printer.set_header(_("System Information [ %s ]") % sys_uuid)
        elif env_name is None:
            self.printer.set_header(_("System Information For Org [ %s ]") % org_name)
        else:
            self.printer.set_header(_("System Information For Environment [ %(env_name)s ] in Org [ %(org_name)s ]") \
                % {'env_name':env_name, 'org_name':org_name})

        # get system details
        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()
        custom_info = custom_info_api.get_custom_info("system", system['id'])
        system['custom_info'] = stringify_custom_info(custom_info)

        system["activation_keys"] = "[ "+ ", ".join([ak["name"] for ak in system["activation_key"]]) +" ]"
        if 'host' in system:
            system['host'] = system['host']['name']
        if 'guests' in system:
            system["guests"] = "[ "+ ", ".join([guest["name"] for guest in system["guests"]]) +" ]"
        if 'environment' in system:
            system['environment'] = system['environment']['name']

        if 'content_view' in system:
            system['content_view'] = "[ %s ]" % system['content_view']['name']


        batch_add_columns(self.printer, {'name': _("Name")}, {'ipv4_address': _("IPv4 Address")}, \
            {'uuid': _("UUID")}, {'environment': _("Environment")}, {'location': _("Location")})
        self.printer.add_column('created_at', _("Registered"), formatter=format_date)
        self.printer.add_column('updated_at', _("Last Updated"), formatter=format_date)
        self.printer.add_column('description', _("Description"), multiline=True)
        if 'release' in system and system['release']:
            self.printer.add_column('release', _("OS Release"))
        self.printer.add_column('activation_keys', _("Activation Keys"), multiline=True, \
            show_with=printer.VerboseStrategy)
        self.printer.add_column('host', _("Host"), show_with=printer.VerboseStrategy)
        self.printer.add_column('sockets', _("Sockets"))
        self.printer.add_column('ram', _("RAM (MB)"))
        self.printer.add_column('serviceLevel', _("Service Level"))
        self.printer.add_column('guests', _("Guests"), show_with=printer.VerboseStrategy)
        self.printer.add_column('custom_info', _("Custom Info"), multiline=True, show_with=printer.VerboseStrategy)
        self.printer.add_column('content_view', _("Content View"))

        self.printer.print_item(system)

        return os.EX_OK
示例#5
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')

        if sys_uuid:
            self.printer.set_header(_("System Information [ %s ]") % sys_uuid)
        elif env_name is None:
            self.printer.set_header(_("System Information For Org [ %s ]") % org_name)
        else:
            self.printer.set_header(_("System Information For Environment [ %s ] in Org [ %s ]") % (env_name, org_name))

        # get system details
        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()
        custom_info = custom_info_api.get_custom_info("system", system['id'])
        system['custom_info'] = "[ %s ]" % ", ".join(["%s: [ %s ]" % (k, ", ".join(custom_info[k])) \
            for k in custom_info.keys()])

        system["activation_keys"] = "[ "+ ", ".join([ak["name"] for ak in system["activation_key"]]) +" ]"
        if 'host' in system:
            system['host'] = system['host']['name']
        if 'guests' in system:
            system["guests"] = "[ "+ ", ".join([guest["name"] for guest in system["guests"]]) +" ]"

        self.printer.add_column('name')
        self.printer.add_column('ipv4_address')
        self.printer.add_column('uuid')
        self.printer.add_column('location')
        self.printer.add_column('created_at', 'Registered', formatter=format_date)
        self.printer.add_column('updated_at', 'Last updated', formatter=format_date)
        self.printer.add_column('description', multiline=True)
        if 'releaseVer' in system and system['releaseVer']:
            self.printer.add_column('releaseVer', 'OS release')
        self.printer.add_column('activation_keys', multiline=True, show_with=printer.VerboseStrategy)
        self.printer.add_column('host', show_with=printer.VerboseStrategy)
        self.printer.add_column('serviceLevel', _('Service Level'))
        self.printer.add_column('guests', show_with=printer.VerboseStrategy)
        if "template" in system:
            self.printer.add_column('template', show_with=printer.VerboseStrategy, value=system["template"]["name"])
        self.printer.add_column('custom_info', multiline=True, show_with=printer.VerboseStrategy)

        self.printer.print_item(system)

        return os.EX_OK
示例#6
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')

        if dist_uuid:
            self.printer.set_header(
                _("Distributor Information [ %s ]") % dist_uuid)
        elif env_name is None:
            self.printer.set_header(
                _("Distributor Information For Org [ %s ]") % org_name)
        else:
            self.printer.set_header(_("Distributor Information For Environment [ %(env)s ] in Org [ %(org)s ]") \
                % {'env':env_name, 'org':org_name})

        # get distributor details
        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()
        custom_info = custom_info_api.get_custom_info("distributor",
                                                      distributor['id'])
        distributor['custom_info'] = stringify_custom_info(custom_info)

        if 'environment' in distributor:
            distributor['environment'] = distributor['environment']['name']

        batch_add_columns(self.printer, {'name': _("Name")}, \
            {'uuid': _("UUID")}, {'environment': _("Environment")})
        self.printer.add_column('created_at',
                                _("Created"),
                                formatter=format_date)
        self.printer.add_column('updated_at',
                                _("Last Updated"),
                                formatter=format_date)
        self.printer.add_column('description',
                                _("Description"),
                                multiline=True)
        self.printer.add_column('custom_info',
                                _("Custom Info"),
                                multiline=True,
                                show_with=printer.VerboseStrategy)

        self.printer.print_item(distributor)

        return os.EX_OK
示例#7
0
    def run(self):
        org_name = self.get_option("org")
        env_name = self.get_option("environment")
        sys_name = self.get_option("name")
        sys_uuid = self.get_option("uuid")

        if sys_uuid:
            self.printer.set_header(_("System Information [ %s ]") % sys_uuid)
        elif env_name is None:
            self.printer.set_header(_("System Information For Org [ %s ]") % org_name)
        else:
            self.printer.set_header(_("System Information For Environment [ %s ] in Org [ %s ]") % (env_name, org_name))

        # get system details
        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()
        custom_info = custom_info_api.get_custom_info("system", system["id"])
        system["custom_info"] = stringify_custom_info(custom_info)

        system["activation_keys"] = "[ " + ", ".join([ak["name"] for ak in system["activation_key"]]) + " ]"
        if "host" in system:
            system["host"] = system["host"]["name"]
        if "guests" in system:
            system["guests"] = "[ " + ", ".join([guest["name"] for guest in system["guests"]]) + " ]"

        batch_add_columns(self.printer, "name", "ipv4_address", "uuid", "location")
        self.printer.add_column("created_at", _("Registered"), formatter=format_date)
        self.printer.add_column("updated_at", _("Last updated"), formatter=format_date)
        self.printer.add_column("description", multiline=True)
        if "release" in system and system["release"]:
            self.printer.add_column("release", _("OS release"))
        self.printer.add_column("activation_keys", multiline=True, show_with=printer.VerboseStrategy)
        self.printer.add_column("host", show_with=printer.VerboseStrategy)
        self.printer.add_column("sockets")
        self.printer.add_column("ram", _("RAM (MB)"))
        self.printer.add_column("serviceLevel", _("Service Level"))
        self.printer.add_column("guests", show_with=printer.VerboseStrategy)
        if "template" in system:
            self.printer.add_column("template", show_with=printer.VerboseStrategy, value=system["template"]["name"])
        self.printer.add_column("custom_info", multiline=True, show_with=printer.VerboseStrategy)

        self.printer.print_item(system)

        return os.EX_OK
示例#8
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.remove_custom_info("system", system['id'], keyname)

        ident = sys_uuid if sys_uuid else sys_name

        if len(response) == 0:
            print _("Successfully removed Custom Information from System [ %s ]") % ident
        else:
            print _("Could not remove Custom Information from System [ %s ]") % ident
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')

        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.remove_custom_info("distributor", distributor['id'], keyname)

        ident = dist_uuid if dist_uuid else dist_name

        if len(response) == 0:
            print _("Successfully removed Custom Information from Distributor [ %s ]") % ident
        else:
            print _("Could not remove Custom Information from Distributor [ %s ]") % ident
示例#10
0
    def run(self):
        org_name = self.get_option("org")
        env_name = self.get_option("environment")
        sys_name = self.get_option("name")
        sys_uuid = self.get_option("uuid")
        keyname = self.get_option("keyname")

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.remove_custom_info("system", system["id"], keyname)

        ident = sys_uuid if sys_uuid else sys_name

        if response is not None:
            print _("Successfully removed Custom Information from System [ %s ]") % ident
        else:
            print _("Could not remove Custom Information from System [ %s ]") % ident
示例#11
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.remove_custom_info("system", system['id'], keyname)

        ident = sys_uuid if sys_uuid else sys_name

        if response is not None:
            print _("Successfully removed Custom Information from System [ %s ]") % ident
        else:
            print _("Could not remove Custom Information from System [ %s ]") % ident
示例#12
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        new_value = self.get_option('value')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("system", system['id'], keyname, new_value)

        ident = sys_uuid if sys_uuid else sys_name

        test_record(response,
            _("Successfully updated Custom Information [ %s ] for System [ %s ]") % (keyname, ident),
            _("Could not update Custom Information [ %s ] for System [ %s ]") % (keyname, ident)
        )
示例#13
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option("environment")
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        value = self.get_option('value')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.add_custom_info("system", system['id'], keyname, value)

        ident = sys_uuid if sys_uuid else sys_name

        test_record(response,
            _("Successfully added Custom Information [ %s : %s ] to System [ %s ]") % (keyname, value, ident),
            _("Could not add Custom Information [ %s : %s ] to System [ %s ]") % (keyname, value, ident)
        )
示例#14
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        current_value = self.get_option('current-value')
        new_value = self.get_option('new-value')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("system", system['id'], keyname, current_value, new_value)

        ident = sys_uuid if sys_uuid else sys_name

        if response[keyname][0] == new_value:
            print _("Successfully updated Custom Information for System [ %s ]") % ident
        else:
            print _("Could not update Custom Information for System [ %s ]") % ident
示例#15
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option("environment")
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        value = self.get_option('value')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.add_custom_info("system", system['id'], keyname, value)

        ident = sys_uuid if sys_uuid else sys_name

        test_record(response,
            _("Successfully added Custom Information [ %(keyname)s : %(value)s ] to System [ %(ident)s ]") \
                % {'keyname':keyname, 'value':value, 'ident':ident},
            _("Could not add Custom Information [ %(keyname)s : %(value)s ] to System [ %(ident)s ]") \
                % {'keyname':keyname, 'value':value, 'ident':ident}
        )
示例#16
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        new_value = self.get_option('value')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("system", system['id'], keyname, new_value)

        ident = sys_uuid if sys_uuid else sys_name

        if response is not None:
            print _("Successfully updated Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident}
        else:
            print _("Could not update Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident}
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option("environment")
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        value = self.get_option('value')

        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.add_custom_info("distributor", distributor['id'], keyname, value)

        ident = dist_uuid if dist_uuid else dist_name

        test_record(response,
            _("Successfully added Custom Information [ %(keyname)s : %(value)s ] to Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'value':value, 'ident':ident},
            _("Could not add Custom Information [ %(keyname)s : %(value)s ] to Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'value':value, 'ident':ident}
        )
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        new_value = self.get_option('value')

        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("distributor", distributor['id'], keyname, new_value)

        ident = dist_uuid if dist_uuid else dist_name

        test_record(response,
            _("Successfully updated Custom Information [ %(keyname)s ] for Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident},
            _("Could not update Custom Information [ %(keyname)s ] for Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident}
        )
示例#19
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        new_value = self.get_option('value')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("system", system['id'], keyname, new_value)

        ident = sys_uuid if sys_uuid else sys_name

        if response:
            print _("Successfully updated Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident}
        else:
            print _("Could not update Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident}
示例#20
0
    def run(self):
        org_name = self.get_option("org")
        env_name = self.get_option("environment")
        sys_name = self.get_option("name")
        sys_uuid = self.get_option("uuid")
        keyname = self.get_option("keyname")
        value = self.get_option("value")

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.add_custom_info("system", system["id"], keyname, value)

        ident = sys_uuid if sys_uuid else sys_name

        test_record(
            response,
            _("Successfully added Custom Information [ %(keyname)s : %(value)s ] to System [ %(ident)s ]")
            % {"keyname": keyname, "value": value, "ident": ident},
            _("Could not add Custom Information [ %(keyname)s : %(value)s ] to System [ %(ident)s ]")
            % {"keyname": keyname, "value": value, "ident": ident},
        )
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        new_value = self.get_option('value')

        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.update_custom_info("distributor",
                                                      distributor['id'],
                                                      keyname, new_value)

        ident = dist_uuid if dist_uuid else dist_name

        test_record(response,
            _("Successfully updated Custom Information [ %(keyname)s ] for Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident},
            _("Could not update Custom Information [ %(keyname)s ] for Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'ident':ident}
        )
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option("environment")
        dist_name = self.get_option('name')
        dist_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')
        value = self.get_option('value')

        distributor = get_distributor(org_name, dist_name, env_name, dist_uuid)

        custom_info_api = CustomInfoAPI()

        response = custom_info_api.add_custom_info("distributor",
                                                   distributor['id'], keyname,
                                                   value)

        ident = dist_uuid if dist_uuid else dist_name

        test_record(response,
            _("Successfully added Custom Information [ %(keyname)s : %(value)s ] to Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'value':value, 'ident':ident},
            _("Could not add Custom Information [ %(keyname)s : %(value)s ] to Distributor [ %(ident)s ]") \
                % {'keyname':keyname, 'value':value, 'ident':ident}
        )
示例#23
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')
        keyname = self.get_option('keyname')

        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()

        custom_info = custom_info_api.get_custom_info("system", system['id'], keyname)

        for k in sorted(custom_info.keys()):
            self.printer.add_column(k, k)

        if sys_uuid:
            self.printer.set_header(_("Custom Information For System [ %s ]") % sys_uuid)
        elif env_name is None:
            self.printer.set_header(_("Custom Information For System [ %s ] in Org [ %s ]") % (sys_name, org_name))
        else:
            self.printer.set_header(_("Custom Information For System [ %s ] in Environment [ %s ] in Org [ %s ]") % \
                (sys_name, env_name, org_name))
        self.printer.print_item(custom_info)
 def __init__(self):
     self.orgapi = OrganizationAPI()
     self.systemapi = SystemAPI()
     self.userapi = UserAPI()
     self.envapi = EnvironmentAPI()
     self.rolesapi = UserRoleAPI()
     self.permissionapi = PermissionAPI()
     self.distributorapi = DistributorAPI()
     self.provapi = ProviderAPI()
     self.infoapi = CustomInfoAPI()
     s = server.KatelloServer(CONFIG.get("katello", "hostname"),
                              CONFIG.get("katello", "port"),
                              CONFIG.get("katello", "proto"),
                              CONFIG.get("katello", "api_url"))
     s.set_auth_method(BasicAuthentication(CONFIG.get("katello", "admin_user"), CONFIG.get("katello", "admin_pass")))
     server.set_active_server(s)
示例#25
0
    def run(self):
        org_name = self.get_option('org')
        env_name = self.get_option('environment')
        sys_name = self.get_option('name')
        sys_uuid = self.get_option('uuid')

        if sys_uuid:
            self.printer.set_header(_("System Information [ %s ]") % sys_uuid)
        elif env_name is None:
            self.printer.set_header(
                _("System Information For Org [ %s ]") % org_name)
        else:
            self.printer.set_header(_("System Information For Environment [ %(env_name)s ] in Org [ %(org_name)s ]") \
                % {'env_name':env_name, 'org_name':org_name})

        # get system details
        system = get_system(org_name, sys_name, env_name, sys_uuid)

        custom_info_api = CustomInfoAPI()
        custom_info = custom_info_api.get_custom_info("system", system['id'])
        system['custom_info'] = stringify_custom_info(custom_info)

        system["activation_keys"] = "[ " + ", ".join(
            [ak["name"] for ak in system["activation_key"]]) + " ]"
        if 'host' in system:
            system['host'] = system['host']['name']
        if 'guests' in system:
            system["guests"] = "[ " + ", ".join(
                [guest["name"] for guest in system["guests"]]) + " ]"
        if 'environment' in system:
            system['environment'] = system['environment']['name']

        if 'content_view' in system:
            system['content_view'] = "[ %s ]" % system['content_view']['name']


        batch_add_columns(self.printer, {'name': _("Name")}, {'ipv4_address': _("IPv4 Address")}, \
            {'uuid': _("UUID")}, {'environment': _("Environment")}, {'location': _("Location")})
        self.printer.add_column('created_at',
                                _("Registered"),
                                formatter=format_date)
        self.printer.add_column('updated_at',
                                _("Last Updated"),
                                formatter=format_date)
        self.printer.add_column('description',
                                _("Description"),
                                multiline=True)
        if 'release' in system and system['release']:
            self.printer.add_column('release', _("OS Release"))
        self.printer.add_column('activation_keys', _("Activation Keys"), multiline=True, \
            show_with=printer.VerboseStrategy)
        self.printer.add_column('host',
                                _("Host"),
                                show_with=printer.VerboseStrategy)
        self.printer.add_column('sockets', _("Sockets"))
        self.printer.add_column('ram', _("RAM (MB)"))
        self.printer.add_column('serviceLevel', _("Service Level"))
        self.printer.add_column('guests',
                                _("Guests"),
                                show_with=printer.VerboseStrategy)
        self.printer.add_column('custom_info',
                                _("Custom Info"),
                                multiline=True,
                                show_with=printer.VerboseStrategy)
        self.printer.add_column('content_view', _("Content View"))

        self.printer.print_item(system)

        return os.EX_OK
class KatelloConnection():

    def __init__(self):
        self.orgapi = OrganizationAPI()
        self.systemapi = SystemAPI()
        self.userapi = UserAPI()
        self.envapi = EnvironmentAPI()
        self.rolesapi = UserRoleAPI()
        self.permissionapi = PermissionAPI()
        self.distributorapi = DistributorAPI()
        self.provapi = ProviderAPI()
        self.infoapi = CustomInfoAPI()
        s = server.KatelloServer(CONFIG.get("katello", "hostname"),
                                 CONFIG.get("katello", "port"),
                                 CONFIG.get("katello", "proto"),
                                 CONFIG.get("katello", "api_url"))
        s.set_auth_method(BasicAuthentication(CONFIG.get("katello", "admin_user"), CONFIG.get("katello", "admin_pass")))
        server.set_active_server(s)

    def get_owners(self):
        return self.orgapi.organizations()

    def create_distributor(self, name, root_org):
        return self.distributorapi.create(name=name, org=root_org, environment_id=None)

    def delete_distributor(self, name, root_org):
        dist_uuid = self.distributorapi.distributor_by_name(distName=name, orgName=root_org)['uuid']
        return self.distributorapi.delete(distributor_uuid=dist_uuid)

    def update_distributor(self, name, root_org, params):
        dist_uuid = self.distributorapi.distributor_by_name(
            distName=name, orgName=root_org)['uuid']
        return self.distributorapi.update(dist_uuid, params)

    def export_manifest(self, dist_uuid):
        return self.distributorapi.export_manifest(distributor_uuid=dist_uuid)

    def import_manifest(self, prov_id, file):
        return self.provapi.import_manifest(provId=prov_id, manifestFile=file)

    def get_redhat_provider(self, org):
        return self.provapi.provider_by_name(orgName=org, provName="Red Hat")

    def get_entitlements(self, system_id):
        return self.systemapi.subscriptions(system_id=system_id)['entitlements']

    def get_subscription_status(self, system_uuid):
        return self.systemapi.subscription_status(system_id=system_uuid)

    def create_owner(self, label, name):
        return self.orgapi.create(name, label, "no description")

    def delete_owner(self, name):
        # todo: error handling, not sure if orgapi will handle it
        self.orgapi.delete(name)

    def update_owner(self, name, params):
        return self.orgapi.update(name, params)

    def get_users(self):
        return self.userapi.users()

    def create_user(self, username, email):
        return self.userapi.create(name=username, pw="CHANGEME", email=email, disabled=False, default_environment=None)

    def delete_user(self, user_id):
        return self.userapi.delete(user_id=user_id)

    def get_spacewalk_id(self, object_id):
        # this wants an object ID
        info_list = self.infoapi.get_custom_info(informable_type='system', informable_id=object_id)
        for info in info_list:
            if info['keyname'] == 'spacewalk-id':
                return info['value']

    def find_by_spacewalk_id(self, org, spacewalk_id):
        result = self.systemapi.find_by_custom_info(org, 'spacewalk-id', spacewalk_id)
        if len(result) > 1:
            raise Exception("more than one record found for spacewalk ID %s in org %s!" % (spacewalk_id, org))

        # we're guaranteed at this point to have zero or one records
        if result:
            return result[0]
        return

    def create_consumer(self, name, facts, installed_products, last_checkin, sw_uuid=None, owner=None):
        # there are four calls here! we need to work with katello to send all this stuff up at once
        consumer = self.systemapi.register(name=name, org='satellite-' + owner, environment_id=None,
                                           facts=facts, activation_keys=None, cp_type='system',
                                           installed_products=installed_products)

        #TODO: get rid of this extra call!
        facts = consumer['facts']
        if 'virt.is_guest' in facts:
            facts['virt.uuid'] = consumer['uuid']
            self.updateConsumer(name=consumer['name'], cp_uuid=consumer['uuid'], facts=facts)

        self.systemapi.checkin(consumer['uuid'], self._convert_date(last_checkin))
        self.systemapi.refresh_subscriptions(consumer['uuid'])

        self.infoapi.add_custom_info(informable_type='system', informable_id=consumer['id'],
                                     keyname='spacewalk-id', value=sw_uuid)

        return consumer['uuid']

    # katello demands a name here
    def update_consumer(self, cp_uuid, name, facts=None, installed_products=None,
                       last_checkin=None, owner=None, guest_uuids=None,
                       release=None, service_level=None):
        params = {}
        params['name'] = name
        if installed_products is not None:
            params['installedProducts'] = installed_products
        if guest_uuids is not None:
            params['guestIds'] = guest_uuids
        if facts is not None:
            # this logic should be moved elsewhere
            if 'virt.is_guest' in facts:
                facts['virt.uuid'] = cp_uuid
            params['facts'] = facts
        if release is not None:
            params['releaseVer'] = release
        if service_level is not None:
            params['serviceLevel'] = service_level

        # three rest calls, just one would be better
        self.systemapi.update(cp_uuid, params)
        if last_checkin is not None:
            self.systemapi.checkin(cp_uuid, self._convert_date(last_checkin))
        self.systemapi.refresh_subscriptions(cp_uuid)

    def get_consumers(self, owner=None, with_details=True):
        # TODO: this has a lot of logic and could be refactored

        # the API wants "orgId" but they mean "label"
        org_ids = map(lambda x: x['label'], self.orgapi.organizations())
        consumer_list = []
        for org_id in org_ids:
            consumer_list.append(self.systemapi.systems_by_org(orgId=org_id))

        # flatten the list
        consumer_list = list(itertools.chain.from_iterable(consumer_list))
        # return what we have, if we don't need the detailed list
        if not with_details:
            return consumer_list

        full_consumers_list = []
        # unfortunately, we need to call again to get the "full" consumer with facts
        for consumer in consumer_list:
            full_consumer = self._get_consumer(consumer['uuid'])
            full_consumer['entitlement_status'] = self.get_subscription_status(consumer['uuid'])
            full_consumers_list.append(full_consumer)

        return full_consumers_list

    def _get_consumer(self, consumer_uuid):
        return self.systemapi.system(system_id=consumer_uuid)

    def delete_consumer(self, consumer_uuid):
        self.systemapi.unregister(consumer_uuid)
        # XXX: only for dev use
        self.systemapi.remove_consumer_deletion_record(consumer_uuid)

    def get_roles(self, user_id=None):
        if user_id:
            return self.userapi.roles(user_id=user_id)
        else:
            return self.rolesapi.roles()

    def update_role(self, name, new_name):
        role = self.rolesapi.role_by_name(name=name)
        return self.rolesapi.update(role['id'], new_name, role['description'])

    # TODO: this is using kt_org_label but is really kt_org_name
    def create_org_admin_role_permission(self, kt_org_label):
        role = self.rolesapi.create(name="Org Admin Role for %s" % kt_org_label, description="generated from spacewalk")
        self.permissionapi.create(roleId=role['id'], name="Org Admin Permission for %s" % kt_org_label,
                                  description="generated from spacewalk", type_in="organizations", verbs=None,
                                  tagIds=None, orgId=kt_org_label, all_tags=True, all_verbs=True)

    def grant_org_admin(self, kt_user, kt_org_label):
        oa_role = self.rolesapi.role_by_name(name="Org Admin Role for %s" % kt_org_label)
        if not oa_role:
            _LOG.error("could not obtain org admin role from katello for org %s!" % kt_org_label)
        self.userapi.assign_role(user_id=kt_user['id'], role_id=oa_role['id'])

    def ungrant_org_admin(self, kt_user, kt_org_label):
        oa_role = self.rolesapi.role_by_name(name="Org Admin Role for %s" % kt_org_label)
        self.userapi.unassign_role(user_id=kt_user['id'], role_id=oa_role['id'])

    def grant_full_admin(self, kt_user):
        admin_role = self.rolesapi.role_by_name(name="Administrator")
        self.userapi.assign_role(user_id=kt_user['id'], role_id=admin_role['id'])

    def ungrant_full_admin(self, kt_user, kt_org_label):
        admin_role = self.rolesapi.role_by_name(name="Administrator")
        self.userapi.unassign_role(user_id=kt_user['id'], role_id=admin_role['id'])

    def _convert_date(self, dt):
        retval = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
        return retval
class KatelloConnection():

    def __init__(self):
        self.orgapi = OrganizationAPI()
        self.systemapi = SystemAPI()
        self.userapi = UserAPI()
        self.envapi = EnvironmentAPI()
        self.rolesapi = UserRoleAPI()
        self.permissionapi = PermissionAPI()
        self.distributorapi = DistributorAPI()
        self.provapi = ProviderAPI()
        self.infoapi = CustomInfoAPI()
        s = server.KatelloServer(CONFIG.get("katello", "hostname"),
                                 CONFIG.get("katello", "port"),
                                 CONFIG.get("katello", "proto"),
                                 CONFIG.get("katello", "api_url"))
        s.set_auth_method(BasicAuthentication(CONFIG.get("katello", "admin_user"), CONFIG.get("katello", "admin_pass")))
        server.set_active_server(s)

    def get_owners(self):
        return self.orgapi.organizations()

    def refresh_subs(self, org_label):
        _LOG.debug("entering async task to refresh systems in %s" % org_label)
        return self.orgapi.attach_all_systems(org_label)

    def create_distributor(self, name, root_org):
        return self.distributorapi.create(name=name, org=root_org, environment_id=None)

    def delete_distributor(self, name, root_org):
        dist_uuid = self.distributorapi.distributor_by_name(distName=name, orgName=root_org)['uuid']
        return self.distributorapi.delete(distributor_uuid=dist_uuid)

    def update_distributor(self, name, root_org, params):
        dist_uuid = self.distributorapi.distributor_by_name(
            distName=name, orgName=root_org)['uuid']
        return self.distributorapi.update(dist_uuid, params)

    def export_manifest(self, dist_uuid):
        return self.distributorapi.export_manifest(distributor_uuid=dist_uuid)

    def import_manifest(self, prov_id, file):
        return self.provapi.import_manifest(provId=prov_id, manifestFile=file)

    def get_redhat_provider(self, org):
        return self.provapi.provider_by_name(orgName=org, provName="Red Hat")

    def get_entitlements(self, system_id):
        return self.systemapi.subscriptions(system_id=system_id)['entitlements']

    def get_subscription_status(self, system_uuid):
        return self.systemapi.subscription_status(system_id=system_uuid)

    def create_owner(self, label, name):
        return self.orgapi.create(name, label, "no description")

    def delete_owner(self, name):
        # todo: error handling, not sure if orgapi will handle it
        self.orgapi.delete(name)

    def update_owner(self, name, params):
        return self.orgapi.update(name, params)

    def get_users(self):
        return self.userapi.users()

    def create_user(self, username, email):
        return self.userapi.create(name=username, pw="CHANGEME", email=email, disabled=False, default_environment=None)

    def delete_user(self, user_id):
        return self.userapi.delete(user_id=user_id)

    def get_spacewalk_id(self, object_id):
        # this wants an object ID
        info_list = self.infoapi.get_custom_info(informable_type='system', informable_id=object_id)
        for info in info_list:
            if info['keyname'] == 'spacewalk-id':
                return info['value']

    def find_by_spacewalk_id(self, org, spacewalk_id):
        result = self.systemapi.find_by_custom_info(org, 'spacewalk-id', spacewalk_id)
        if len(result) > 1:
            raise Exception("more than one record found for spacewalk ID %s in org %s!" % (spacewalk_id, org))

        # we're guaranteed at this point to have zero or one records
        if result:
            return result[0]
        return

    def create_consumer(self, name, facts, installed_products, last_checkin, sw_uuid=None, owner=None):
        # there are four calls here! we need to work with katello to send all this stuff up at once
        consumer = self.systemapi.register(name=name, org='satellite-' + owner, environment_id=None,
                                           facts=facts, activation_keys=None, cp_type='system',
                                           installed_products=installed_products, last_checkin=self._convert_date(last_checkin).isoformat())

        # we want this to happen ASAP. Ideally, it would be in the same transaction as the above call.
        self.infoapi.add_custom_info(informable_type='system', informable_id=consumer['id'],
                                     keyname='spacewalk-id', value=sw_uuid)

        # TODO: get rid of this extra call!
        facts = consumer['facts']
        if 'virt.is_guest' in facts:
            facts['virt.uuid'] = consumer['uuid']
            self.update_consumer(name=consumer['name'], cp_uuid=consumer['uuid'], facts=facts)

        # there appears to be a candlepin bug where this isn't being set on initial consumer setup
        self.systemapi.checkin(consumer['uuid'], self._convert_date(last_checkin))

        return consumer['uuid']

    # katello demands a name here
    def update_consumer(self, cp_uuid, name, facts=None, installed_products=None,
                        last_checkin=None, owner=None, guest_uuids=None,
                        release=None, service_level=None):
        params = {}
        params['name'] = name
        if installed_products is not None:
            params['installedProducts'] = installed_products
        if guest_uuids is not None:
            params['guestIds'] = guest_uuids
        if facts is not None:
            # this logic should be moved elsewhere
            if 'virt.is_guest' in facts:
                facts['virt.uuid'] = cp_uuid
            params['facts'] = facts
        if release is not None:
            params['releaseVer'] = release
        if service_level is not None:
            params['serviceLevel'] = service_level
        if last_checkin is not None:
            params['lastCheckin'] = self._convert_date(last_checkin).isoformat()

        self.systemapi.update(cp_uuid, params)

    def get_consumers(self, owner=None, with_details=True):
        # TODO: this has a lot of logic and could be refactored into katello_sync
        # the API wants "orgId" but they mean "label"
        org_ids = map(lambda x: x['label'], self.orgapi.organizations())
        # some katello-specific query params for pagination
        query_params = {'paged': 'true',
                        'sort_by': 'name',
                        'offset': 0,
                        'sort_order': 'ASC'}
        consumer_list = []
        # TODO: this could be done in parallel
        for org_id in org_ids:
            # we need to grab the system list in sets of 25 so we don't time out on especially large queries
            query_params['offset'] = 0
            while True:
                _LOG.debug("retrieving systems for org %s, offset %s" % (org_id, query_params['offset']))
                system_subset = self.systemapi.systems_by_org(orgId=org_id, query=query_params)['systems']
                _LOG.debug("found %s systems" % len(system_subset))
                consumer_list.append(system_subset)
                # if we didn't get 25 results, stop here
                if len(system_subset) < 25:
                    break
                query_params['offset'] += 25

        # flatten the list
        consumer_list = list(itertools.chain.from_iterable(consumer_list))
        # return what we have, if we don't need the detailed list
        if not with_details:
            return consumer_list

        consumer_uuids = map(lambda x: x['uuid'], consumer_list)

        def _get_full_consumer_worker(uuid):
            full_consumer = self._get_consumer(uuid)
            full_consumer['entitlement_status'] = self.get_subscription_status(uuid)
            return full_consumer

        full_consumers_list = utils.queued_work(_get_full_consumer_worker,
                                                consumer_uuids,
                                                CONFIG.getint('main', 'num_threads'))

        if len(consumer_uuids) != len(full_consumers_list):
            raise Exception("unable to fetch all consumer records (expected %s, found %s), see log for more detail" %
                            (len(consumer_uuids), len(full_consumers_list)))

        return full_consumers_list

    def _get_consumer(self, consumer_uuid):
        return self.systemapi.system(system_id=consumer_uuid)

    def delete_consumer(self, consumer_uuid):
        self.systemapi.unregister(consumer_uuid)

    def get_roles(self, user_id=None):
        if user_id:
            return self.userapi.roles(user_id=user_id)
        else:
            return self.rolesapi.roles()

    def update_role(self, name, new_name):
        role = self.rolesapi.role_by_name(name=name)
        return self.rolesapi.update(role['id'], new_name, role['description'])

    # TODO: this is using kt_org_label but is really kt_org_name
    def create_org_admin_role_permission(self, kt_org_label):
        role = self.rolesapi.create(name="Org Admin Role for %s" % kt_org_label, description="generated from spacewalk")
        self.permissionapi.create(roleId=role['id'], name="Org Admin Permission for %s" % kt_org_label,
                                  description="generated from spacewalk", type_in="organizations", verbs=None,
                                  tagIds=None, orgId=kt_org_label, all_tags=True, all_verbs=True)

    def grant_org_admin(self, kt_user, kt_org_label):
        oa_role = self.rolesapi.role_by_name(name="Org Admin Role for %s" % kt_org_label)
        if not oa_role:
            _LOG.error("could not obtain org admin role from katello for org %s!" % kt_org_label)
        self.userapi.assign_role(user_id=kt_user['id'], role_id=oa_role['id'])

    def ungrant_org_admin(self, kt_user, kt_org_label):
        oa_role = self.rolesapi.role_by_name(name="Org Admin Role for %s" % kt_org_label)
        self.userapi.unassign_role(user_id=kt_user['id'], role_id=oa_role['id'])

    def grant_full_admin(self, kt_user):
        admin_role = self.rolesapi.role_by_name(name="Administrator")
        self.userapi.assign_role(user_id=kt_user['id'], role_id=admin_role['id'])

    def ungrant_full_admin(self, kt_user, kt_org_label):
        admin_role = self.rolesapi.role_by_name(name="Administrator")
        self.userapi.unassign_role(user_id=kt_user['id'], role_id=admin_role['id'])

    def get_deleted_systems(self):
        return self.systemapi.get_deleted_consumers()

    def _convert_date(self, dt):
        retval = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
        return retval