Ejemplo n.º 1
0
class Provider():
    api = ProviderAPI()

    def create_provider(self,
                        org,
                        name=None,
                        description='Built by API',
                        ptype='Custom',
                        url=None):

        if name is None:
            name = generate_name(8)

        return self.api.create(name, org['label'], description, ptype, url)

    def delete_provider(self, name):
        return self.api.delete(name)

    def provider(self, pId):
        return self.api.provider(pId)

    def providers_by_org(self, org):
        return self.api.providers_by_org(org['label'])

    def sync(self, pId):
        task = self.api.sync(pId)[0]

        while task['state'] != 'finished':
            print "Synchronizing..."
            task = self.api.last_sync_status(pId)

        return task
Ejemplo n.º 2
0
def get_provider(orgName, provName):
    provider_api = ProviderAPI()

    prov = provider_api.provider_by_name(orgName, provName)
    if prov == None:
        raise ApiDataError(
            _("Could not find provider [ %s ] within organization [ %s ]") %
            (provName, orgName))
    return prov
Ejemplo n.º 3
0
 def __init__(self):
     super(ProviderAction, self).__init__()
     self.api = ProviderAPI()
Ejemplo n.º 4
0
def create_data(numorgs, numsystems, numproviders, numproducts, numrepos,
                singleorg):
    # Setup connection to Katello
    admin = AdminCLI()
    admin.setup_parser()
    admin.opts, admin.args = admin.parser.parse_args([])
    admin.setup_server()
    admin._username = "******"
    admin._password = "******"
    org_names = []
    if (singleorg):
        # If we pass in a single org name
        # we just load all the data into that.
        org_names.append(singleorg)
    else:
        # Otherwise just create fake orgs
        orgapi = OrganizationAPI()
        print "Creating [%s] Orgs" % numorgs
        for i in range(numorgs):
            name = "Org-%s" % randomString()
            org_names.append(name)
            print "[%s] Creating org with name [%s]" % (i, name)
            orgapi.create(name, "description")

    # create envs
    envapi = EnvironmentAPI()

    for i in range(len(org_names)):
        print "[%s] Creating DEV/TEST/STAGE in org: [%s]" % (i, org_names[i])
        libraryId = get_environment(org_names[i], "Library")["id"]
        print "Library ID: %s" % libraryId
        envids = [libraryId]
        for x in range(len(ENVIRONMENTS)):
            existing_env = get_environment(org_names[i], ENVIRONMENTS[x])
            if not existing_env:
                e = envapi.create(org_names[i], ENVIRONMENTS[x], "Desc",
                                  envids[x])
                envids.append(e["id"])
            else:
                envids.append(existing_env["id"])

    ## create providers, products and repos
    print "Creating [%s] providers in each org" % numproviders
    for i in range(len(org_names)):
        for y in range(numproviders):
            provider_name = "Provider-%s" % randomString()
            print "[%s] Creating Provider with name: [%s] in org: [%s] and products + repos" % (
                y, provider_name, org_names[i])
            providerapi = ProviderAPI()
            provider = providerapi.create(provider_name, org_names[i], "Desc",
                                          "Custom", None)
            print "  Creating [%s] Products in each provider" % numproducts
            for z in range(numproducts):
                product_name = "P-%s" % randomString()
                print "  [%s] Creating product with name: [%s]" % (
                    z, product_name)
                productapi = ProductAPI()
                product = productapi.create(provider["id"], product_name,
                                            "Desc", None)
                print "    Creating [%s] Products in each product" % numproducts
                for x in range(numrepos):
                    repo_name = "Repo-%s" % randomString()
                    print "    [%s] Creating repo with name: [%s]" % (
                        x, repo_name)
                    repoapi = RepoAPI()
                    url = "http://repos.example.com/%s" % repo_name
                    repoapi.create(org_names[i], product["id"], repo_name, url,
                                   None, True)
    ## Create systems
    print "Creating [%s] Systems in each org and assigning to random envs" % numsystems
    for i in range(len(org_names)):
        systemapi = SystemAPI()
        for x in range(numsystems):
            system_name = "System-%s" % randomString()
            randenv = random.choice(ENVIRONMENTS)
            print "Registering system: [%s] in environment: [%s]" % (
                system_name, randenv)
            system = systemapi.register(system_name, org_names[i], randenv, [],
                                        'system')
            print "[%s] Created system: %s" % (x, system["name"])
Ejemplo n.º 5
0
class Discovery(RepoAction):  # pylint: disable=R0904
    #TODO: temporary pylint disable, we need to refactor the class later

    description = _('discovery repositories contained within a URL')
    provider_api = ProviderAPI()

    def setup_parser(self, parser):
        opt_parser_add_org(parser, required=1)
        parser.add_option('--provider', dest='provider',
            help=_("provider name (required)"))
        parser.add_option('--name', dest='name',
            help=_("repository name prefix to add to all the discovered repositories (required)"))
        parser.add_option('--label', dest='label',
                               help=_("repo label, ASCII identifier to add to " +
                                "all discovered repositories.  (will be generated if not specified)"))
        parser.add_option('--unprotected', dest='unprotected', action='store_true', default=False,
            help=_("Publish the created repositories using http (in addition to https)."))
        parser.add_option("--url", dest="url", type="url", schemes=ALLOWED_REPO_URL_SCHEMES,
            help=_("root url to perform discovery of repositories eg: http://katello.org/repos/ (required)"))
        parser.add_option("--assumeyes", action="store_true", dest="assumeyes",
            help=_("assume yes; automatically create candidate repositories for discovered urls (optional)"))
        opt_parser_add_product(parser, required=1)

    def check_options(self, validator):
        validator.require(('name', 'org', 'url', 'provider'))
        validator.require_at_least_one_of(('product', 'product_label', 'product_id'))
        validator.mutually_exclude('product', 'product_label', 'product_id')

    def run(self):
        name     = self.get_option('name')
        label    = self.get_option('label')
        url      = self.get_option('url')
        assumeyes = self.get_option('assumeyes')
        providerName   = self.get_option('provider')
        prodName = self.get_option('product')
        prodLabel = self.get_option('product_label')
        prodId   = self.get_option('product_id')
        orgName  = self.get_option('org')
        unprotected = self.get_option('unprotected')

        prov_id = get_provider(orgName, providerName)['id']
        repourls = self.discover_repositories(prov_id, url)
        self.printer.set_header(_("Repository Urls discovered @ [%s]" % url))
        selectedurls = self.select_repositories(repourls, assumeyes)

        product = get_product(orgName, prodName, prodLabel, prodId)
        self.create_repositories(orgName, product["id"], name, label, selectedurls, unprotected)

        return os.EX_OK

    def discover_repositories(self, provider_id, url):
        print(_("Discovering repository urls, this could take some time..."))
        task = self.provider_api.repo_discovery(provider_id, url)

        run_spinner_in_bg(wait_for_async_task, [task])
        repourls = self.provider_api.provider(provider_id)['discovered_repos']

        if not len(repourls):
            system_exit(os.EX_OK, "No repositories discovered @ url location [%s]" % url)

        return repourls


    def select_repositories(self, repourls, assumeyes, our_raw_input = raw_input):
        selection = Selection()
        if not assumeyes:
            proceed = ''
            num_selects = [u_str(i+1) for i in range(len(repourls))]
            select_range_str = constants.SELECTION_QUERY % len(repourls)
            while proceed.strip().lower() not in  ['q', 'y']:
                if not proceed.strip().lower() == 'h':
                    self.__print_urls(repourls, selection)
                proceed = our_raw_input(
                    _("\nSelect urls for which candidate repos should be created; use `y` to confirm (h for help):"))
                select_val = proceed.strip().lower()
                if select_val == 'h':
                    print select_range_str
                elif select_val == 'a':
                    selection.add_selection(repourls)
                elif select_val in num_selects:
                    selection.add_selection([repourls[int(proceed.strip().lower())-1]])
                elif select_val == 'q':
                    selection = Selection()
                    system_exit(os.EX_OK, _("Operation aborted upon user request."))
                elif set(select_val.split(":")).issubset(num_selects):
                    lower, upper = tuple(select_val.split(":"))
                    selection.add_selection(repourls[int(lower)-1:int(upper)])
                elif select_val == 'c':
                    selection = Selection()
                elif select_val == 'y':
                    if not len(selection):
                        proceed = ''
                        continue
                    else:
                        break
                else:
                    continue
        else:
            #select all
            selection.add_selection(repourls)
            self.__print_urls(repourls, selection)

        return selection

    def create_repositories(self, orgName, productid, name, label, selectedurls, unprotected):
        for repourl in selectedurls:
            parsedUrl = urlparse.urlparse(repourl)
            repoName = self.repository_name(name, parsedUrl.path) # pylint: disable=E1101
            repoLabel = None
            if label:
                repoLabel = self.repository_name(label, parsedUrl.path) # pylint: disable=E1101
            self.api.create(orgName, productid, repoName, repoLabel, repourl, unprotected, None, None)
            print _("Successfully created repository [ %s ]") % repoName

    @classmethod
    def repository_name(cls, name, parsedUrlPath):
        return "%s%s" % (name, parsedUrlPath.replace("/", "_"))

    @classmethod
    def __print_urls(cls, repourls, selectedurls):
        for index, url in enumerate(repourls):
            if url in selectedurls:
                print "(+)  [%s] %-5s" % (index+1, url)
            else:
                print "(-)  [%s] %-5s" % (index+1, url)