Skip to content

Commit

Permalink
Packaging: Introducing RPM packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Spredzy committed May 27, 2020
1 parent ee70a20 commit 320849d
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 103 deletions.
104 changes: 104 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
PYTHON ?= python
ifeq ($(origin VIRTUAL_ENV), undefined)
DIST_PYTHON ?= poetry run $(PYTHON)
else
DIST_PYTHON ?= $(PYTHON)
endif

NAME = ansible-builder
IMAGE_NAME ?= $(NAME)
PIP_NAME = ansible_builder
LONG_VERSION := $(shell poetry version)
VERSION := $(filter-out $(NAME), $(LONG_VERSION))
ifeq ($(OFFICIAL),yes)
RELEASE ?= 1
else
ifeq ($(origin RELEASE), undefined)
RELEASE := 0.git$(shell date -u +%Y%m%d%H).$(shell git rev-parse --short HEAD)
endif
endif

# RPM build variables
MOCK_BIN ?= mock
MOCK_CONFIG ?= epel-7-x86_64

RPM_NVR = $(NAME)-$(VERSION)-$(RELEASE)$(RPM_DIST)
RPM_DIST ?= $(shell rpm --eval '%{?dist}' 2>/dev/null)
RPM_ARCH ?= $(shell rpm --eval '%{_arch}' 2>/dev/null)

# Provide a fallback value for RPM_ARCH
ifeq ($(RPM_ARCH),)
RPM_ARCH = $(shell uname -m)
endif

.PHONY: clean dist sdist dev shell rpm srpm docs

clean:
rm -rf dist
rm -rf build
rm -rf ansible-builder.egg-info
rm -rf rpm-build
find . -type f -regex ".*\py[co]$$" -delete

dist:
poetry build

sdist: dist/$(NAME)-$(VERSION).tar.gz

# Generate setup.py transiently for the sdist so we don't have to deal with
# packaging poetry as a RPM for rpm build time dependencies.
dist/$(NAME)-$(VERSION).tar.gz:
dephell deps convert --from=pyproject.toml --to=setup.py
$(DIST_PYTHON) setup.py sdist

dev:
poetry install

shell:
poetry shell

test:
tox

docs:
cd docs && make html

rpm:
MOCK_CONFIG=$(MOCK_CONFIG) docker-compose -f packaging/rpm/docker-compose.yml build
MOCK_CONFIG=$(MOCK_CONFIG) docker-compose -f packaging/rpm/docker-compose.yml \
run --rm -e RELEASE=$(RELEASE) rpm-builder "make mock-rpm"

srpm:
MOCK_CONFIG=$(MOCK_CONFIG) docker-compose -f packaging/rpm/docker-compose.yml build
MOCK_CONFIG=$(MOCK_CONFIG) docker-compose -f packaging/rpm/docker-compose.yml \
run --rm -e RELEASE=$(RELEASE) rpm-builder "make mock-srpm"

mock-rpm: rpm-build/$(RPM_NVR).$(RPM_ARCH).rpm

rpm-build/$(RPM_NVR).$(RPM_ARCH).rpm: rpm-build/$(RPM_NVR).src.rpm
$(MOCK_BIN) -r $(MOCK_CONFIG) --arch=noarch \
--resultdir=rpm-build \
--rebuild rpm-build/$(RPM_NVR).src.rpm

mock-srpm: rpm-build/$(RPM_NVR).src.rpm

rpm-build/$(RPM_NVR).src.rpm: dist/$(NAME)-$(VERSION).tar.gz rpm-build rpm-build/$(NAME).spec
$(MOCK_BIN) -r $(MOCK_CONFIG) --arch=noarch \
--resultdir=rpm-build \
--spec=rpm-build/$(NAME).spec \
--sources=rpm-build \
--buildsrpm

rpm-build/$(NAME).spec:
ansible -c local -i localhost, all \
-m template \
-a "src=packaging/rpm/$(NAME).spec.j2 dest=rpm-build/$(NAME).spec" \
-e version=$(VERSION) \
-e release=$(RELEASE)

rpm-build: sdist
mkdir -p $@
cp dist/$(NAME)-$(VERSION).tar.gz rpm-build/$(NAME)-$(VERSION)-$(RELEASE).tar.gz

print-%:
@echo $($*)
2 changes: 1 addition & 1 deletion ansible_builder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, *args, filename):
self.filename = filename

with open(filename, 'r') as f:
self.raw = yaml.load(f, Loader=yaml.FullLoader)
self.raw = yaml.load(f)

@property
def version(self):
Expand Down
9 changes: 9 additions & 0 deletions packaging/rpm/Dockerfile.epel-7-x86_64
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM centos:7

RUN yum install -y epel-release
RUN yum install -y make mock python3 which git gcc python3-devel

# Fix output of rpm --eval '%{?dist}'
RUN sed -i "s/.el7.centos/.el7/g" /etc/rpm/macros.dist

RUN pip3 install -IU poetry ansible dephell[full]
6 changes: 6 additions & 0 deletions packaging/rpm/Dockerfile.epel-8-x86_64
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM centos:8

RUN dnf install -y epel-release
RUN yum install -y make mock python3-pip which git gcc python3-devel

RUN pip3 install -IU poetry ansible dephell[full]
43 changes: 43 additions & 0 deletions packaging/rpm/ansible-builder.spec.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%global pypi_name ansible-builder

Name: %{pypi_name}
Version: {{ version }}
Release: {{ release }}%{?dist}
Summary: An Ansible execution environment builder

License: ASL 2.0
URL: https://github.com/ansible/ansible-builder
Source0: https://github.com/ansible/%{name}/archive/%{version}.tar.gz?/%{name}-%{version}-{{ release }}.tar.gz
BuildArch: noarch

BuildRequires: python3
BuildRequires: python3-setuptools
BuildRequires: python3-rpm-macros


%if 0%{?fedora} || 0%{?rhel} > 7
Requires: python3-pyyaml
%else
Requires: python36-PyYAML
%endif

%description
An Ansible execution environment builder

%prep
%autosetup -n %{pypi_name}-%{version}

%build
%py3_build

%install
%py3_install

%files
%defattr(-,root,root)

%files
%{python3_sitelib}/*
%{_bindir}/ansible-builder

%changelog
21 changes: 21 additions & 0 deletions packaging/rpm/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
version: '3'
services:
rpm-builder:
build:
dockerfile: Dockerfile.${MOCK_CONFIG}
context: .
image: builder-rpm-builder:${MOCK_CONFIG}
environment:
MOCK_BIN: "mock --old-chroot"
MOCK_CONFIG:
RELEASE:
OFFICIAL:
volumes:
- ../../:/ansible-builder
- mock-cache:/var/cache/mock
entrypoint: ["/bin/bash", "-c"]
working_dir: /ansible-builder
privileged: true
volumes:
mock-cache:
Loading

0 comments on commit 320849d

Please sign in to comment.