Theo Chatzimichos 1f0a34
#!/usr/bin/python3
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
# For description and usage, see the argparse options at the end of the file
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
import argparse
Theo Chatzimichos 1f0a34
import os
Theo Chatzimichos 4af2b2
import sys
Theo Chatzimichos 1f0a34
import yaml
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
Theo Chatzimichos 4af2b2
def clone_or_pull(DEST, SYMLINK=False):
Theo Chatzimichos 1f0a34
    def use_git_to_clone_or_pull_repo():
Theo Chatzimichos 1f0a34
        # pygit2 is not available for python3 in Leap, use plain git instead
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
        import subprocess
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
        if not os.path.exists(DEST):
Theo Chatzimichos 1f0a34
            os.mkdir(DEST)
Theo Chatzimichos 1f0a34
        if os.path.isdir(FULL_PATH):
Theo Chatzimichos 1f0a34
            subprocess.Popen(['git', 'pull'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Theo Chatzimichos 1f0a34
        else:
Theo Chatzimichos 1f0a34
            subprocess.Popen(['git', 'clone', url, FULL_PATH], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Theo Chatzimichos 968f21
        subprocess.Popen(['git', 'remote', 'add', 'opensuse', opensuse_fork_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=FULL_PATH)
Theo Chatzimichos 968f21
        subprocess.Popen(['git', 'fetch', 'opensuse'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=FULL_PATH)
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
    def use_pygit2_to_clone_or_pull_repo():
Theo Chatzimichos 1f0a34
        import pygit2
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
        if os.path.isdir(FULL_PATH):
Theo Chatzimichos 1f0a34
            repo = pygit2.Repository(FULL_PATH)
Theo Chatzimichos 1f0a34
            repo.checkout('HEAD')
Theo Chatzimichos 1f0a34
        else:
Theo Chatzimichos 1f0a34
            pygit2.clone_repository(url, FULL_PATH, bare=False)
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
    for formula, data in FORMULAS.items():
Theo Chatzimichos 1f0a34
        namespace = data.get('namespace', 'saltstack-formulas')
Theo Chatzimichos 1f0a34
        prefix = data.get('prefix', '')
Theo Chatzimichos 1f0a34
        url = 'https://github.com/%s/%s%s-formula' % (namespace, prefix, formula)
Theo Chatzimichos 968f21
        opensuse_fork_url = 'https://gitlab.infra.opensuse.org/saltstack-formulas/%s-formula' % formula
Theo Chatzimichos 1f0a34
        FULL_PATH = '%s/%s-formula' % (DEST, formula)
Theo Chatzimichos 1f0a34
        use_git_to_clone_or_pull_repo()
Theo Chatzimichos 1f0a34
        if SYMLINK:
Theo Chatzimichos 1f0a34
            os.symlink('%s/%s' % (FULL_PATH, formula), '/srv/salt/%s' % formula)
Theo Chatzimichos 1f0a34
Theo Chatzimichos 1f0a34
Theo Chatzimichos 968f21
def enable_remote(REMOTE, DEST):
Theo Chatzimichos 968f21
    def use_git_to_enable_remote():
Theo Chatzimichos 968f21
        # pygit2 is not available for python3 in Leap, use plain git instead
Theo Chatzimichos 968f21
Theo Chatzimichos 968f21
        import subprocess
Theo Chatzimichos 968f21
Theo Chatzimichos 968f21
        for formula in FORMULAS.keys():
Theo Chatzimichos 968f21
            FULL_PATH = '%s/%s-formula' % (DEST, formula)
Theo Chatzimichos 968f21
            subprocess.Popen(['git', 'checkout', '-B', 'master', '%s/master' % REMOTE], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=FULL_PATH)
Theo Chatzimichos 968f21
Theo Chatzimichos 968f21
Theo Chatzimichos 1f0a34
with open('FORMULAS.yaml', 'r') as f:
Theo Chatzimichos 1f0a34
    FORMULAS = yaml.load(f)
Theo Chatzimichos 1f0a34
Theo Chatzimichos 968f21
parser = argparse.ArgumentParser(description='Loads the formulas from FORMULAS.yaml and optionally clones them in a specified destination. Optionally it can also create a symlink from the cloned path to /srv/salt, useful for the CI worker. The internal gitlab fork will also be added as secondary remote.')
Theo Chatzimichos 968f21
parser.add_argument('-c', '--clone', action='store_true', help='Clone the formulas to the destination specified with "--destination". The gitlab fork will also be added as remote. If the repository is already cloned, then both remotes will be pulled/fetched.')
Theo Chatzimichos 1f0a34
parser.add_argument('-s', '--symlink', action='store_true', help='Creates symlink from the specified destination to /srv/salt.')
Theo Chatzimichos 968f21
parser.add_argument('-r', '--remote', nargs=1, help='Enable the specified remote. Available remotes: origin, opensuse. Default: origin')
Theo Chatzimichos 4af2b2
requiredArgs = parser.add_argument_group('required arguments')
Theo Chatzimichos 4af2b2
requiredArgs.add_argument('-d', '--destination', nargs=1, required=True, help='Destination absolute path of the cloned (or to-be-cloned) repositories of the formulas.')
Theo Chatzimichos 1f0a34
args = parser.parse_args()
Theo Chatzimichos 1f0a34
Theo Chatzimichos 4af2b2
if not os.path.isabs(args.destination[0]):
Theo Chatzimichos 4af2b2
    parser.print_help()
Theo Chatzimichos 4af2b2
    sys.exit(1)
Theo Chatzimichos 4af2b2
Theo Chatzimichos 968f21
if args.remote and args.remote[0] not in ['origin', 'opensuse']:
Theo Chatzimichos 968f21
    parser.print_help()
Theo Chatzimichos 968f21
    sys.exit(1)
Theo Chatzimichos 968f21
Theo Chatzimichos 1f0a34
if args.clone:
Theo Chatzimichos 4af2b2
    clone_or_pull(args.destination[0], args.symlink)
Theo Chatzimichos 968f21
Theo Chatzimichos 968f21
if args.remote:
Theo Chatzimichos 968f21
    enable_remote(args.remote[0], args.destination[0])