def add_npm_product(product_name, product_key, environment, repository_name, debug): """Creates a new NPM product in the shared configuration file. More information: https://mojo.redhat.com/docs/DOC-1199638 PRODUCT_NAME is the name of the product to be stored in the configuration file for easier reading PRODUCT_KEY is the key under which it will be created ENVIRONMENT - working environment (prod by default) REPOSITORY_NAME - name of the repository in Nexus """ nexus_config = config.load(environment) try: with config.cloned_repo(nexus_config) as cloned_repo: print("Creating npm product in Nexus...") try: config.add_npm_product(cloned_repo, product_key, product_name, repository_name) except Exception as exc: print("Failed to update configuration npm repo: %s" % exc, file=sys.stderr) sys.exit(1) except RuntimeError as exc: if debug: raise print(str(exc), file=sys.stderr) sys.exit(1)
def rollback(args, config, session, delete_log=None, debug=False): """Remove the given staging repository from all release groups More Information: https://mojo.redhat.com/docs/DOC-1132234 """ nexus_config = config.load(environment, debug) groups = [ RELEASE_GROUP_NAME, TECHPREVIEW_GROUP_NAME, PRERELEASE_GROUP_NAME ] session = Session(config.base_url, user, debug=debug, disable_ssl_validation=config.permissive_ssl, preemptive_auth=config.preemptive_auth) try: print "Removing content of: %s" % staging_repo_name for group_name in groups: group = groups.load(session, group_name, True) if group is not None: print "Removing %s from group %s" % (staging_repo_name, group_name) group.remove_member(session, staging_repo_name).save(session) finally: if session is not None: session.close()
def test_init(self): config_file = config.init_config() conf = config.load('prod') self.assertEqual( conf.get_profile_id('MYPRODUCT', is_ga=True) is None, False) self.assertEqual( conf.get_profile_id('MYPRODUCT', is_ga=False) is None, False)
def list_products(environment, npm, java=False, maven=False): """ Lists all configured products. It is possible to filter the results to npm/java products using the switches. """ # Workaround for ancient version of click available on RHEL 6. It uses the # last option name for the keyword argument. No matter which alias is used, # old click uses maven kwargs, new click uses java kwargs. java = java or maven nexus_config = config.load(environment) fmt = None if sys.stdout.isatty(): if npm == java: # print all by default fmt = "%-4s %-20s%-30s%-20s%-20s%-20s" print("\033[1m" + (fmt % ("Type", "Key", "Product Name", "EA", "GA", "NPM Repository")) + "\033[0m") elif java: fmt = "%-20s%-30s%-20s%-20s" print("\033[1m" + (fmt % ("Key", "Product Name", "EA", "GA")) + "\033[0m") else: fmt = "%-20s%-30s%-20s" print("\033[1m" + (fmt % ("Key", "Product Name", "NPM Repository")) + "\033[0m") for product in sorted(nexus_config.profile_map.keys()): product_type, product_name, ga_id, ea_id, npm_repository = nexus_config.get_profile_data( product) if npm == java and product_type: # print everything but UNKNOWN product type print(fmt % (config.ProfileType(product_type).name, product, product_name, ga_id, ea_id, npm_repository)) elif java and product_type == config.ProfileType.JAVA: print(fmt % (product, product_name, ga_id, ea_id)) elif npm and product_type == config.ProfileType.NPM: print(fmt % (product, product_name, npm_repository))
def test_ssl_verify(self): url = 'http://nowhere.com/nexus' data = { 'test': { config.URL: url, config.SSL_VERIFY: True, } } rc = self.write_config(data) nxconfig = config.load('test') self.assertEqual(nxconfig.url, url) self.assertEqual(nxconfig.ssl_verify, True)
def test_interactive(self): url = 'http://nowhere.com/nexus' data = { 'test': { config.URL: url, config.INTERACTIVE: True, } } rc = self.write_config(data) nxconfig = config.load('test') self.assertEqual(nxconfig.url, url) self.assertEqual(nxconfig.interactive, True)
def test_preemptive_auth(self): url = 'http://nowhere.com/nexus' data = { 'test': { config.URL: url, config.PREEMPTIVE_AUTH: True, } } rc = self.write_config(data) nxconfig = config.load('test') self.assertEqual(nxconfig.url, url) self.assertEqual(nxconfig.preemptive_auth, True)
def test_with_username_and_oracle_password(self): username = '******' password = '******' url = 'http://nowhere.com/nexus' data = { 'test': { config.URL: url, config.USERNAME: username, config.PASSWORD: "******" % password } } rc = self.write_config(data) nxconfig = config.load('test') self.assertEqual(nxconfig.username, username) self.assertEqual(nxconfig.get_password(), password)
def check(repo, environment, debug=False): nexus_config = config.load(environment) if not nexus_config.web_url: print("Missing option %s in config file" % config.WEB_URL, file=sys.stderr) sys.exit(1) session = Session(nexus_config, debug=debug) if debug: print("Checking file", repo) try: if not checker.check_zip_file(session, nexus_config.web_url, repo): sys.exit(1) except requests.exceptions.HTTPError as exc: if debug: raise print("Network error: %s" % exc, file=sys.stderr) sys.exit(1)
def test_get_profile_id_ea(self): url = 'http://nowhere.com/nexus' ea_profile = '0123456789' data = { 'test': { config.URL: url, } } profile_map = { 'eap': { config.GA_STAGING_PROFILE: '9876543210', config.EA_STAGING_PROFILE: str(ea_profile) } } rc = self.write_config(data, profile_map) nxconfig = config.load('test') profile_id = nxconfig.get_profile_id('eap', is_ga=False) self.assertEqual(profile_id, ea_profile)
def rollback(staging_repo_name, environment, debug=False): """Drop given staging repository. More information: https://mojo.redhat.com/docs/DOC-1199638 """ nexus_config = config.load(environment) session = Session(nexus_config, debug=debug) try: print("Dropping repository %s" % staging_repo_name) if not staging.drop_staging_repo(session, staging_repo_name): sys.exit(1) except requests.exceptions.HTTPError as exc: if debug: raise print("Network error: %s" % exc, file=sys.stderr) sys.exit(1) finally: if session is not None: session.close()
def test_get_profile_id_ga(self): url = 'http://nowhere.com/nexus' ga_profile = '0123456789' data = { 'test': { config.URL: url, } } profile_map = { 'test': { 'eap': { config.GA_PROFILE: str(ga_profile), config.EA_PROFILE: '9876543210' } } } rc = self.write_config(data, profile_map) nxconfig = config.load('test', debug=True) profile_id = nxconfig.get_profile_id('eap', is_ga=True) self.assertEqual(profile_id, ga_profile)
def test_minimal_from_default(self): url = 'http://nowhere.com/nexus' data = {'test': {config.URL: url}} rc = self.write_config(data) nxconfig = config.load('test') self.assertEqual(nxconfig.url, url)
def push(repo, environment, product, version, ga=False, debug=False): """Push Apache Maven repository content to a Nexus staging repository, then add the staging repository to appropriate content groups. More Information: https://mojo.redhat.com/docs/DOC-1132234 """ nexus_config = config.load(environment, debug=debug) if ga: groups = [RELEASE_GROUP_NAME, TECHPREVIEW_GROUP_NAME] else: groups = [PRERELEASE_GROUP_NAME] session = Session(nexus_config, debug=debug) try: print "Pushing: %s content to: %s" % (repo, environment) # produce a set of clean repository zips for PUT upload. zips_dir = tempfile.mkdtemp() print "Creating ZIP archives in: %s" % zips_dir if os.path.isdir(repo): print "Processing repository directory: %s" % repo # Walk the directory tree, and create a zip. zip_paths = archive.create_partitioned_zips_from_dir( repo, zips_dir) else: print "Processing repository zip archive: %s" % repo # Open the zip, walk the entries and normalize the structure to clean zip (if necessary) zip_paths = archive.create_partitioned_zips_from_zip( repo, zips_dir) # Open new staging repository with description staging_repo_id = staging.start_staging_repo(session, nexus_config, product, version, ga) # HTTP PUT clean repository zips to Nexus. delete_first = True for zipfile in zip_paths: repo.push_zip(session, staging_repo_id, zipfile, delete_first) delete_first = False # Close staging repository staging.finish_staging_repo(session, nexus_config, staging_repo_id, product, version, ga) for group_id in groups: group = groups.load(session, group_id, ignore_missing=True) if group is not None: print "Adding %s to group: %s" % (staging_repo_id, group_id) group.append_member(session, staging_repo_id).save(session) else: print "No such group: %s" % group_id raise Exception("No such group: %s" % group_id) finally: if session is not None: session.close()
def push(repo, environment, product, version, ga=False, debug=False): """Push Apache Maven repository content to a Nexus staging repository, then add the staging repository to appropriate content groups. More information: https://mojo.redhat.com/docs/DOC-1199638 """ nexus_config = config.load(environment) npm_archive_type = npm.detect_npm_archive(repo) if npm_archive_type != npm.NpmArchiveType.NOT_NPM: # if any npm archive types.... npm.push(nexus_config, repo, npm_archive_type, product, debug=debug) else: if nexus_config.get_profile_type(product) != config.ProfileType.JAVA: print(product + " is not a java product!", file=sys.stderr) exit(1) session = Session(nexus_config, debug=debug) print("Pushing: %s content to: %s" % (repo, environment)) zips_dir = None try: # produce a set of clean repository zips for PUT upload. zips_dir = tempfile.mkdtemp() print("Creating ZIP archives in: %s" % zips_dir) if os.path.isdir(repo): print("Processing repository directory: %s" % repo) # Walk the directory tree, and create a zip. zip_paths = archive.create_partitioned_zips_from_dir( repo, zips_dir) else: print("Processing repository zip archive: %s" % repo) # Open the zip, walk the entries and normalize the structure to # clean zip zip_paths = archive.create_partitioned_zips_from_zip( repo, zips_dir, debug=debug) # Open new staging repository with description staging_repo_id = staging.start_staging_repo( session, nexus_config, product, version, ga) # HTTP PUT clean repository zips to Nexus. delete_first = True for idx, zipfile in enumerate(zip_paths, start=1): print("Uploading zip %s out of %s" % (idx, len(zip_paths))) repos.push_zip(session, staging_repo_id, zipfile, delete_first) delete_first = False # Close staging repository staging.finish_staging_repo(session, nexus_config, staging_repo_id, product, version, ga) if staging.verify_action(session, staging_repo_id, "close"): sys.exit(1) print("Promoting repo") profiles = nexus_config.get_promote_profile_ids(product, ga) promote_entity = None for promote_profile in profiles: if not promote_entity: # First iteration, promote staging repo directly. promote_entity = staging_repo_id else: # On every other iteration promote the group created by # previous promotion. promote_entity = staging.get_next_promote_entity( session, promote_entity) staging.promote(session, promote_profile, promote_entity, product, version, ga) if staging.verify_action(session, promote_entity, "promote"): sys.exit(1) except RuntimeError as exc: if debug: raise print("Error: %s" % exc, file=sys.stderr) sys.exit(1) except requests.exceptions.HTTPError as exc: if debug: raise print("Network error: %s" % exc, file=sys.stderr) sys.exit(1) finally: if session is not None: session.close() if zips_dir: shutil.rmtree(zips_dir) print("Deployment successfully finished.")
def add_java_product( product_name, product_key, environment, target_group, promote_ruleset, promotion_target, debug, ): """Creates a new product in Nexus and updates configuration so it can be used. PRODUCT_NAME is the name used by the Nexus service PRODUCT_KEY is the shorthand used to reference the product by rcm-nexus tools More information: https://mojo.redhat.com/docs/DOC-1199638 """ ids = {} nexus_config = config.load(environment) session = Session(nexus_config, debug=debug) try: with config.cloned_repo(nexus_config) as cloned_repo: print("Creating product in Nexus...") try: ids = { config.IS_GA: create_product( session, product_name, target_group or nexus_config.target_groups[config.IS_GA], promote_ruleset or nexus_config.promote_ruleset[config.IS_GA], promotion_target or nexus_config.promote_target[config.IS_GA], ), config.IS_EA: create_product( session, product_name + " Early Access", target_group or nexus_config.target_groups[config.IS_EA], promote_ruleset or nexus_config.promote_ruleset[config.IS_EA], promotion_target or nexus_config.promote_target[config.IS_EA], ), } except requests.exceptions.HTTPError: if debug: raise sys.exit(1) print("Updating permissions") for product_id in ids.values(): modify_permissions(session, product_id, nexus_config.deployer_role) try: config.add_product(cloned_repo, product_key, product_name, ids) except Exception as exc: print("Failed to update configuration repo: %s" % exc, file=sys.stderr) print("Add the following manually:", file=sys.stderr) print("\n[%s]\nga = %s\nea = %s\n" % (product_key, ids[config.IS_GA], ids[config.IS_EA])) sys.exit(1) except RuntimeError as exc: if debug: raise print(str(exc), file=sys.stderr) sys.exit(1)