Skip to content
Merged
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
45 changes: 44 additions & 1 deletion obal/data/modules/srpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import shutil
import os
import subprocess
from zipfile import ZipFile
from contextlib import contextmanager
from tempfile import mkdtemp
from tempfile import mkdtemp, TemporaryFile

from ansible.module_utils.six.moves.urllib.request import urlopen # pylint:disable=import-error,no-name-in-module
from ansible.module_utils.six.moves.urllib.error import HTTPError # pylint:disable=import-error,no-name-in-module

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


@contextmanager
def chdir(directory):
"""
Expand Down Expand Up @@ -54,6 +59,32 @@ def copy_sources(spec_file, package_dir, sources_dir):
run_command(["git-annex", "lock", "--force"])


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 = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought more of a global, static var (SOURCE_SYSTEM_URLS), but wouldn't mind either way here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about scope creep, and I figured if I went this route and there was a reason to go with a global static variable you'd point it out to me :)

'jenkins': '{}/lastSuccessfulBuild/artifact/*zip*/archive.zip',
}

url = source_system_urls[source_system].format(source_location)
request = urlopen(url)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ansible has open_url, fetch_url and fetch_file in https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/urls.py which could be used instead, but I don't think we buy anything by using them here.


try:
archive = TemporaryFile()
archive.write(request.read())

with ZipFile(archive) as zip_file:
for zip_info in zip_file.infolist():
if zip_info.filename[-1] == '/':
continue

zip_info.filename = os.path.basename(zip_info.filename)
zip_file.extract(zip_info, sources_dir)
finally:
archive.close()


def main():
"""
Build a package using tito
Expand All @@ -63,12 +94,16 @@ def main():
package=dict(type='str', required=False),
scl=dict(type='str', required=False),
output=dict(type='path', required=False),
source_location=dict(type='str', required=False),
source_system=dict(type='str', required=False),
)
)

package = module.params['package']
output = module.params['output']
scl = module.params['scl']
source_location = module.params['source_location']
source_system = module.params['source_system']

spec_file = os.path.join(package, '%s.spec' % os.path.basename(package))

Expand All @@ -80,6 +115,14 @@ def main():
os.mkdir(sources_dir)
os.mkdir(build_dir)

if source_location:
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)
shutil.copy(spec_file, base_dir)

Expand Down
2 changes: 2 additions & 0 deletions obal/data/roles/build_srpm/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
output: "{{ build_srpm_output_dir }}"
scl: "{{ scl | default(omit) }}"
source_location: "{{ source_location | default(omit) }}"
source_system: "{{ source_system | default(omit) }}"
register: srpm_build

- name: 'Built srpm path'
Expand Down