Skip to content

Commit

Permalink
Merge pull request #10 from freedomofpress/RPMBuilder
Browse files Browse the repository at this point in the history
Adds rpm buider container.. swaps out build scripts
  • Loading branch information
msheiny authored Nov 6, 2018
2 parents 805eb0d + d3eb34b commit 36b01f6
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 55 deletions.
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[dev-packages]

[packages]
PyYAML = "*"

[requires]
python_version = "3"
38 changes: 38 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ For example in `circleci-docker/meta.yml`:
repo: "quay.io/freedomofpress/circleci-docker"
tag: "v2"
```
## Enacting a build
* Make sure you install the repo requirements via pipenv. Need to install pipenv?
Follow [upstream documentation](ttps://pipenv.readthedocs.io/en/latest/install/#installing-pipenv).
* Either jump into a shell `pipenv shell` or you can run the following command
with `pipenv run _____`

* Run `./build.py $<name_of_local_container_dir>` .. I like to take advantage of
the tab completion in terminal here when selecting a container to build.
5 changes: 0 additions & 5 deletions ansible.cfg

This file was deleted.

13 changes: 0 additions & 13 deletions build-image.yml

This file was deleted.

101 changes: 101 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
"""
Builds docker images using specs from folders and metadata config files.
"""

import argparse
import os
import shlex
import subprocess
import yaml


CUR_DIRECTORY = os.path.dirname(os.path.realpath(__file__))


def get_container_names(dir_name=CUR_DIRECTORY):
"""
Get me a list of sub-directories that contain a meta.yml.
Assume these are container names we are interested in
"""
containers = []

for dir in os.listdir(dir_name):
try:
if ('meta.yml' in os.listdir(dir) and
'Dockerfile' in os.listdir(dir)):
containers.append(dir)
except NotADirectoryError:
continue

return containers


def extract_metadata(rootdir):
""" Provide root directory of container files, reads that folders meta.yml, and returns the yaml object """
with open(os.path.join(rootdir, 'meta.yml'), 'r') as f:
return yaml.load(f.read())


def run_command(command):
"""
Takes in a string command, runs said command as a subprocess, print to stdout live
Taken from https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python
"""
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)

while True:
output = process.stdout.readline().decode()
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
rc = process.poll()
return rc


def build(image_name, dir=CUR_DIRECTORY):
"""
Build docker image.
Assumes there is a dir structure of ./<container_name_dir>/
With at least `meta.yml` and `Dockerfile` within that folder.
"""
container_dir = os.path.join(dir, image_name)

meta_data = extract_metadata(container_dir)

# If build args exist in metadata. lets pull em out!
try:
opt_args = ""
for build_arg in meta_data['args']:
opt_args += "--build-arg={}={} ".format(
build_arg,
meta_data['args'][build_arg])
except KeyError:
pass

# Build docker command
docker_cmd = ("docker build {opt} -t {tag}"
" -f {dockerfile} {context}").format(
opt=opt_args,
tag=':'.join([meta_data['repo'], meta_data['tag']]),
dockerfile=os.path.join(container_dir, 'Dockerfile'),
context=container_dir)

print("Running '{}'".format(docker_cmd))
run_command(docker_cmd)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
# Basically a folder that has a meta.yml and a Dockerfile
parser.add_argument('container',
type=str,
help='container to build',
choices=get_container_names())

args = parser.parse_args()

build(args.container)
31 changes: 0 additions & 31 deletions playbook.yml

This file was deleted.

2 changes: 0 additions & 2 deletions requirements.txt

This file was deleted.

30 changes: 30 additions & 0 deletions rpmbuilder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM fedora:25
LABEL maintainer="Freedom of the Press Foundation"
LABEL description="RedHat tooling for building RPMs"
ARG FEDORA_PKGR_VER


RUN echo "${FEDORA_PKGR_VER}"

RUN dnf update -y && \
dnf install -y \
fedora-packager-${FEDORA_PKGR_VER}.noarch \
make \
python3-cryptography \
python3-devel \
python3-requests \
python3-setuptools \
vim && \
yum clean all

ENV HOME /home/user
RUN useradd --create-home --home-dir $HOME user \
&& chown -R user:user $HOME && \
su -c rpmdev-setuptree user

WORKDIR $HOME/rpmbuild
VOLUME $HOME/rpmbuild

USER user

CMD ["/usr/bin/bash"]
5 changes: 5 additions & 0 deletions rpmbuilder/meta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
repo: "quay.io/freedomofpress/rpmbuilder"
tag: "0.6.0.1-1.fc25"
args:
FEDORA_PKGR_VER: 0.6.0.1-1.fc25

0 comments on commit 36b01f6

Please sign in to comment.