Skip to content

Commit

Permalink
Merge 452090f into 40045d5
Browse files Browse the repository at this point in the history
  • Loading branch information
lubomir committed Dec 2, 2019
2 parents 40045d5 + 452090f commit 0784dd7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 56 deletions.
75 changes: 42 additions & 33 deletions rcm_nexus/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,40 +232,49 @@ def add_product(
ids = {}
nexus_config = config.load(environment, debug=debug)
session = Session(nexus_config, debug=debug)
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:
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, 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
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(nexus_config, environment, product_key, 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])
)
print(str(exc), file=sys.stderr)
sys.exit(1)
70 changes: 47 additions & 23 deletions rcm_nexus/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import print_function

import contextlib
import getpass
import subprocess
import os
import shutil
import sys
import tempfile
import textwrap

from six.moves import configparser

Expand Down Expand Up @@ -164,41 +166,63 @@ def _read_config(path):
return result


def add_product(config, environment, key, ids):
@contextlib.contextmanager
def cloned_repo(config):
tempdir = tempfile.mkdtemp(prefix="rcm-nexus-config-")
clone_dir = os.path.join(tempdir, "clone")
remote_url = config.write_remote_repo.format(user=config.username.split("@")[0])
try:
print("Cloning remote git configuration repository...")
_clone_config_repo(clone_dir, remote_url, limit_depth=False)
print("Adding new configuration entries...")
parser = configparser.RawConfigParser()
with open(os.path.join(clone_dir, "rcm-nexus.conf")) as f:
parser.readfp(f)
if not parser.has_section(key):
parser.add_section(key)
parser.set(key, "ga", ids[IS_GA])
parser.set(key, "ea", ids[IS_EA])
with open(os.path.join(clone_dir, "rcm-nexus.conf"), "w") as f:
parser.write(f)
print("Commiting the changes...")
subprocess.check_call(
["git", "commit", "-a", "-m", "%s added" % key],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=clone_dir,
yield clone_dir
except Exception:
xdg_config_home = (
os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config")
)
print("Pushing changes to remote repo...")
subprocess.check_call(
["git", "push", "origin", "master"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=clone_dir,
error = textwrap.dedent(
"""
Failed to clone configuration repository from writable URL:
{0}
Modify {1}/rcm-nexus.conf and add following snippet with correct
URL to which it is possible to push a new commit.
[{2}]
write_config_repo = FILL_ME
""".format(remote_url, xdg_config_home, SECTION)
)
raise RuntimeError(error)
finally:
shutil.rmtree(tempdir)


def add_product(clone_dir, key, ids):
print("Adding new configuration entries...")
parser = configparser.RawConfigParser()
with open(os.path.join(clone_dir, "rcm-nexus.conf")) as f:
parser.readfp(f)
if not parser.has_section(key):
parser.add_section(key)
parser.set(key, "ga", ids[IS_GA])
parser.set(key, "ea", ids[IS_EA])
with open(os.path.join(clone_dir, "rcm-nexus.conf"), "w") as f:
parser.write(f)
print("Commiting the changes...")
subprocess.check_call(
["git", "commit", "-a", "-m", "%s added" % key],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=clone_dir,
)
print("Pushing changes to remote repo...")
subprocess.check_call(
["git", "push", "origin", "master"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=clone_dir,
)


def init_config():
conf_path = get_config_path()[0]
if os.path.exists(conf_path):
Expand Down

0 comments on commit 0784dd7

Please sign in to comment.