Skip to content

Commit 3f7c72f

Browse files
authored
Gitlab runner container pr 1.x (#30)
* Adding a build for a prebuilt CI image. * Adding the docker SSH entrypoint script. * Updating container image name. * Making apt handling more robust. * Moving the CI container code to just be documentation. * Updating docs for using ce-dev to build CI containers.
1 parent 5012d48 commit 3f7c72f

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

docker-images/controller-ci/Dockerfile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,32 @@ RUN \
1919
pip3 install ansible boto3 && \
2020
git lfs install --skip-repo && \
2121
update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \
22+
useradd -m controller && \
23+
usermod -a -G controller ce-dev && \
24+
mkdir -p /home/controller/.ssh && \
25+
chmod -R 700 /home/controller/.ssh && \
26+
echo "Host remotehost\n\tStrictHostKeyChecking no\n" >> /home/controller/.ssh/config && \
27+
mkdir -p /home/ce-dev/.ssh && \
28+
chmod -R 700 /home/ce-dev/.ssh && \
29+
echo "Host remotehost\n\tStrictHostKeyChecking no\n" >> /home/ce-dev/.ssh/config && \
2230
rm -rf \
2331
/var/lib/apt/lists/* \
2432
/var/log/* \
2533
/tmp/*
2634

35+
# The keys gets copied in place by before_script in .gitlab-ci.yml
36+
COPY id_rsa* /home/ce-dev/.ssh/
37+
COPY id_rsa* /home/controller/.ssh/
38+
39+
RUN \
40+
set -x && \
41+
export DEBIAN_FRONTEND=noninteractive && \
42+
chown -R ce-dev:ce-dev /home/ce-dev/.ssh && \
43+
chmod 600 /home/ce-dev/.ssh/id_rsa && \
44+
chmod 644 /home/ce-dev/.ssh/id_rsa.pub && \
45+
chown -R controller:controller /home/controller/.ssh && \
46+
chmod 600 /home/controller/.ssh/id_rsa && \
47+
chmod 644 /home/controller/.ssh/id_rsa.pub
2748

2849
RUN su - ce-dev -c "git clone --branch 1.x https://github.com/codeenigma/ce-provision.git /home/ce-dev/ce-provision"
2950

@@ -33,7 +54,7 @@ RUN \
3354
set -x && \
3455
export DEBIAN_FRONTEND=noninteractive && \
3556
apt-get update && \
36-
su - ce-dev -c "/usr/local/bin/ansible-playbook /home/ce-dev/ce-provision/provision.yml" && \
57+
su - ce-dev -c "/usr/local/bin/ansible-playbook --extra-vars=\"{ansible_common_remote_group: controller}\" /home/ce-dev/ce-provision/provision.yml" && \
3758
rm /home/ce-dev/ce-provision/provision.yml && \
3859
apt-get clean && \
3960
rm -rf \
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
controller-ci
22
=============
3+
Although `ce-dev` is a cli tool first and foremost, it can be used to pack containers to use with `ce-provision` and CI. This is simply an EXAMPLE for provisioning a controller for running `ce-provision` in a container with GitLab CI.
34

4-
This is simply an EXAMPLE for provisioning a controller for running ce-provision in a container with GitLab CI.
5-
6-
Every organisation wanting to run ce-provision in a container must necessarily make their own container image which incorporates their own version of ce-provision-config and installs the dependencies for their choice of CI. There can be no such thing as a "generic" CI container because it needs to contain secrets and it needs to be tailored to the CI product.
5+
Every organisation wanting to run `ce-provision` in a container must necessarily make their own container image which incorporates their own version of `ce-provision-config` and installs the dependencies for their choice of CI. There can be no such thing as a "generic" CI container because it needs to contain secrets and it needs to be tailored to the CI product.
76

87
As such any generated CI container *must* be private in the container registry, never make them public.
98

10-
The Dockerfile within is a mix of ce-dev's controller image:
9+
The Dockerfile within is a mix of `ce-dev`'s controller image:
1110
* https://github.com/codeenigma/ce-dev/tree/1.x/docker-images/controller
1211

1312
And a GitLab Runner controller based on this project:
1413
* https://gitlab.com/tmaczukin-test-projects/fargate-driver-debian/-/blob/master/Dockerfile
1514

16-
You can build this container to see how it works, but it will not work with your infra because it currently incorporates an example ce-provision-config repo.
15+
You can build this container to see how it works, but it will not work with your infra because it currently incorporates an example `ce-provision-config` repo.
16+
17+
# Key handling and user management
18+
The main complications with using ce-dev to build a CI container are:
19+
* The SSH private key to access your config repo will need baking in
20+
* The username for provisioning infrastructure is typically `controller`, whereas the `ce-dev` containers are packed with `ce-dev` as the user
21+
22+
To get around this you can copy an SSH key on to the container, either via a CI variable or from the machine orchestrating the CI. In our example `Dockerfile` we are handling copying a key from the host. Note in particular we create the `controller` user in advance and we add `ce-dev` to the `controller` group. Also, importantly, we this to the `ansible-playbook` command:
23+
* ` --extra-vars=\"{ansible_common_remote_group: controller}\"`
24+
25+
This is important, so the ownership of tmp files created by `ce-dev` can be changed to use the `controller` group, thus the `controller` user can access them. Otherwise `become: controller` in Ansible will fail.
26+
27+
Steps will vary with CI flavour, but with GitLab CI we use `before_script` and `after_script` to ensure the private and public keys are where `docker` needs them to be, *and* to clean up the private key when we're finished:
28+
29+
```yaml
30+
before_script:
31+
- cp -r /PATH/TO/KEYS/id_rsa* $CI_PROJECT_DIR/docker-images/controller-ci/
32+
33+
after_script:
34+
- rm $CI_PROJECT_DIR/docker-images/controller-ci/id_rsa
35+
```
36+
37+
With these extra changes in place you should be able to pack a `docker` image that uses the `controller` user for executing `ce-provision` and has all the secrets and permissions it needs to build infrastructure.

docker-images/controller-ci/provision.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
config_repository: https://github.com/codeenigma/ce-dev-ce-provision-config.git
1515
config_repository_branch: 1.x
1616
config_repository_skip_checkout: false
17-
username: ce-dev
18-
local_dir: /home/ce-dev/ce-provision
17+
username: controller
18+
local_dir: /home/controller/ce-provision
1919
groups: []
2020
galaxy_custom_requirements_file: ""
2121
roles:

0 commit comments

Comments
 (0)