Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement local source fetching #295

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions obal/data/modules/srpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 17 additions & 1 deletion obal/data/playbooks/srpm/metadata.obal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,3 +25,7 @@ variables:
build_srpm_output_dir:
parameter: --dir
help: Absolute path to output dir for SRPM. Defaults to <inventory_dir>/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
1 change: 0 additions & 1 deletion obal/data/roles/build_srpm/meta/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
dependencies:
- role: setup_workspace
- role: ensure_package
- role: setup_sources