Skip to content

Commit 36b01f6

Browse files
authored
Merge pull request #10 from freedomofpress/RPMBuilder
Adds rpm buider container.. swaps out build scripts
2 parents 805eb0d + d3eb34b commit 36b01f6

File tree

11 files changed

+197
-55
lines changed

11 files changed

+197
-55
lines changed

Makefile

Lines changed: 0 additions & 4 deletions
This file was deleted.

Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
url = "https://pypi.python.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[dev-packages]
7+
8+
[packages]
9+
PyYAML = "*"
10+
11+
[requires]
12+
python_version = "3"

Pipfile.lock

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ For example in `circleci-docker/meta.yml`:
1818
repo: "quay.io/freedomofpress/circleci-docker"
1919
tag: "v2"
2020
```
21+
22+
## Enacting a build
23+
24+
* Make sure you install the repo requirements via pipenv. Need to install pipenv?
25+
Follow [upstream documentation](ttps://pipenv.readthedocs.io/en/latest/install/#installing-pipenv).
26+
27+
* Either jump into a shell `pipenv shell` or you can run the following command
28+
with `pipenv run _____`
29+
30+
* Run `./build.py $<name_of_local_container_dir>` .. I like to take advantage of
31+
the tab completion in terminal here when selecting a container to build.

ansible.cfg

Lines changed: 0 additions & 5 deletions
This file was deleted.

build-image.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

build.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Builds docker images using specs from folders and metadata config files.
4+
"""
5+
6+
import argparse
7+
import os
8+
import shlex
9+
import subprocess
10+
import yaml
11+
12+
13+
CUR_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
14+
15+
16+
def get_container_names(dir_name=CUR_DIRECTORY):
17+
"""
18+
Get me a list of sub-directories that contain a meta.yml.
19+
Assume these are container names we are interested in
20+
"""
21+
containers = []
22+
23+
for dir in os.listdir(dir_name):
24+
try:
25+
if ('meta.yml' in os.listdir(dir) and
26+
'Dockerfile' in os.listdir(dir)):
27+
containers.append(dir)
28+
except NotADirectoryError:
29+
continue
30+
31+
return containers
32+
33+
34+
def extract_metadata(rootdir):
35+
""" Provide root directory of container files, reads that folders meta.yml, and returns the yaml object """
36+
with open(os.path.join(rootdir, 'meta.yml'), 'r') as f:
37+
return yaml.load(f.read())
38+
39+
40+
def run_command(command):
41+
"""
42+
Takes in a string command, runs said command as a subprocess, print to stdout live
43+
44+
Taken from https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python
45+
"""
46+
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
47+
48+
while True:
49+
output = process.stdout.readline().decode()
50+
if output == '' and process.poll() is not None:
51+
break
52+
if output:
53+
print(output.strip())
54+
rc = process.poll()
55+
return rc
56+
57+
58+
def build(image_name, dir=CUR_DIRECTORY):
59+
"""
60+
Build docker image.
61+
62+
Assumes there is a dir structure of ./<container_name_dir>/
63+
With at least `meta.yml` and `Dockerfile` within that folder.
64+
"""
65+
container_dir = os.path.join(dir, image_name)
66+
67+
meta_data = extract_metadata(container_dir)
68+
69+
# If build args exist in metadata. lets pull em out!
70+
try:
71+
opt_args = ""
72+
for build_arg in meta_data['args']:
73+
opt_args += "--build-arg={}={} ".format(
74+
build_arg,
75+
meta_data['args'][build_arg])
76+
except KeyError:
77+
pass
78+
79+
# Build docker command
80+
docker_cmd = ("docker build {opt} -t {tag}"
81+
" -f {dockerfile} {context}").format(
82+
opt=opt_args,
83+
tag=':'.join([meta_data['repo'], meta_data['tag']]),
84+
dockerfile=os.path.join(container_dir, 'Dockerfile'),
85+
context=container_dir)
86+
87+
print("Running '{}'".format(docker_cmd))
88+
run_command(docker_cmd)
89+
90+
91+
if __name__ == '__main__':
92+
parser = argparse.ArgumentParser(description=__doc__)
93+
# Basically a folder that has a meta.yml and a Dockerfile
94+
parser.add_argument('container',
95+
type=str,
96+
help='container to build',
97+
choices=get_container_names())
98+
99+
args = parser.parse_args()
100+
101+
build(args.container)

playbook.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

rpmbuilder/Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM fedora:25
2+
LABEL maintainer="Freedom of the Press Foundation"
3+
LABEL description="RedHat tooling for building RPMs"
4+
ARG FEDORA_PKGR_VER
5+
6+
7+
RUN echo "${FEDORA_PKGR_VER}"
8+
9+
RUN dnf update -y && \
10+
dnf install -y \
11+
fedora-packager-${FEDORA_PKGR_VER}.noarch \
12+
make \
13+
python3-cryptography \
14+
python3-devel \
15+
python3-requests \
16+
python3-setuptools \
17+
vim && \
18+
yum clean all
19+
20+
ENV HOME /home/user
21+
RUN useradd --create-home --home-dir $HOME user \
22+
&& chown -R user:user $HOME && \
23+
su -c rpmdev-setuptree user
24+
25+
WORKDIR $HOME/rpmbuild
26+
VOLUME $HOME/rpmbuild
27+
28+
USER user
29+
30+
CMD ["/usr/bin/bash"]

0 commit comments

Comments
 (0)