diff --git a/obal/data/modules/srpm.py b/obal/data/modules/srpm.py index ed649c24..8d4cf939 100644 --- a/obal/data/modules/srpm.py +++ b/obal/data/modules/srpm.py @@ -15,6 +15,11 @@ from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.obal import run_command, get_specfile_sources # pylint:disable=import-error,no-name-in-module +SOURCE_SYSTEM_URLS = { + 'jenkins': '{}/lastSuccessfulBuild/artifact/*zip*/archive.zip', +} +VALID_SOURCE_SYSTEMS = list(SOURCE_SYSTEM_URLS.keys()) + @contextmanager def chdir(directory): @@ -59,15 +64,28 @@ def copy_sources(spec_file, package_dir, sources_dir): run_command(["git-annex", "lock", "--force"]) +def fetch_local_sources(source_location, source_system, sources_dir): + if source_system == 'local-bundle-rake': + command = ['bundle', 'exec', 'rake', 'pkg:generate_source'] + elif source_system == 'local-rake': + command = ['rake', 'pkg:generate_source'] + else: + raise ValueError("Unknown source system %s" % source_system) + + with chdir(source_location): + output = run_command(command) + + for line in output.splitlines(): + if os.path.isfile(line): + shutil.copy(line, sources_dir) + + def fetch_remote_sources(source_location, source_system, sources_dir): """ Copy RPM sources from a remote source like Jenkins to rpmbuild environment """ - source_system_urls = { - 'jenkins': '{}/lastSuccessfulBuild/artifact/*zip*/archive.zip', - } - url = source_system_urls[source_system].format(source_location) + url = SOURCE_SYSTEM_URLS[source_system].format(source_location) request = urlopen(url) with TemporaryFile() as archive: @@ -113,14 +131,28 @@ def main(): os.mkdir(build_dir) if source_location: + if source_system.startswith('local-'): + try: + fetch_local_sources(source_location, source_system, sources_dir) + except ValueError: + module.fail_json(msg="Unknown source_system specified.", + source_system=source_system, valid_choices=VALID_SOURCE_SYSTEMS) + except subprocess.CalledProcessError as error: + module.fail_json(msg='Failed fetch local sources', output=error.output) + else: + try: + fetch_remote_sources(source_location, source_system, sources_dir) + except HTTPError as error: + module.fail_json(msg="HTTP %s: %s. Check %s exists." % (error.code, error.reason, source_location)) + except KeyError as error: + module.fail_json(msg="Unknown source_system specified.", + source_system=source_system, valid_choices=VALID_SOURCE_SYSTEMS) + else: try: - fetch_remote_sources(source_location, source_system, sources_dir) - except HTTPError as error: - module.fail_json(msg="HTTP %s: %s. Check %s exists." % (error.code, error.reason, source_location)) - except KeyError as error: - module.fail_json(msg="Unknown source_system specified.", output=error) + copy_sources(spec_file, package, sources_dir) + except subprocess.CalledProcessError as error: + module.fail_json(msg='Failed to build srpm', output=error.output) - copy_sources(spec_file, package, sources_dir) shutil.copy(spec_file, base_dir) command = ['rpmbuild', '-bs'] @@ -150,8 +182,6 @@ def main(): path = os.path.join(output, os.path.basename(result)) module.exit_json(changed=True, path=path) - except subprocess.CalledProcessError as error: - module.fail_json(msg='Failed to build srpm', output=error.output) finally: shutil.rmtree(base_dir) diff --git a/obal/data/playbooks/srpm/metadata.obal.yaml b/obal/data/playbooks/srpm/metadata.obal.yaml index 3d7815d4..1de46adb 100644 --- a/obal/data/playbooks/srpm/metadata.obal.yaml +++ b/obal/data/playbooks/srpm/metadata.obal.yaml @@ -2,7 +2,19 @@ help: | Build a SRPM for packages - Building source RPMs can be useful when locally building packages. + Building source RPMs can be useful when locally building packages. For + example, when you've made local changes. + + --source-system local-bundle-rake --source-location $HOME/foreman-installer + + Not all projects need bundler so it's possible to run without. + + --source-system local-bundle-rake --source-location $HOME/smart-proxy + + It is also possible to retrieve the source from a Jenkins job. + + --source-system jenkins --source-location https://ci.theforeman.org/job/foreman-installer-develop-source-release + variables: build_srpm_dist: parameter: --dist @@ -13,3 +25,7 @@ variables: build_srpm_output_dir: parameter: --dir help: Absolute path to output dir for SRPM. Defaults to /SRPMs + source_system: + help: The system to be used when passing source_location. Either jenkins, local-bundle-rake or local-rake. + source_location: + help: Source location. Format is dependent on the source system diff --git a/obal/data/roles/build_srpm/meta/main.yaml b/obal/data/roles/build_srpm/meta/main.yaml index 7b3819bd..1e1c5b9d 100644 --- a/obal/data/roles/build_srpm/meta/main.yaml +++ b/obal/data/roles/build_srpm/meta/main.yaml @@ -2,4 +2,3 @@ dependencies: - role: setup_workspace - role: ensure_package - - role: setup_sources