Skip to content

Commit

Permalink
add inventory-generator under new sub pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Aug 9, 2017
1 parent 6528031 commit c3c7e45
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 0 deletions.
26 changes: 26 additions & 0 deletions images/inventory-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM centos:7

RUN INSTALL_PKGS="openssh-clients wget git" \
&& yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS \
&& EPEL_PKGS="PyYAML ansible python2-boto" \
&& yum install -y epel-release \
&& yum install -y --setopt=tsflags=nodocs $EPEL_PKGS \
&& rpm -q $INSTALL_PKGS $EPEL_PKGS \
&& yum clean all

LABEL name="dynamic-inventory-generator" \
summary="Generates inventory file from host information" \
description="A containerized dynamic inventory generator based on OpenShift host information"

COPY root /

ENV APP_ROOT=/opt/app-root \
HOME=/opt/app-root/src \
WORK_DIR=/opt/app-root \
USER_UID=1001

WORKDIR ${HOME}
RUN /usr/local/bin/user_setup

ENTRYPOINT [ "/usr/local/bin/entrypoint" ]
CMD [ "/usr/local/bin/run" ]
78 changes: 78 additions & 0 deletions images/inventory-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Dynamic Inventory Generation
============================

Image that creates a container capable of
connecting to an OpenShift master and dynamically
creating an inventory file from its environment.

### Build

`docker build --rm -t openshift/inventory-gen .`

### Run

Given a master node's `master-config.yaml` file and its `admin.kubeconfig` file, the command below will:

1. Connect to the host using the bind-mounted `libra.pem` file (replace with `id_rsa` or anything else used to access your remote machine via ssh)
2. Generate an inventory file based on the current configuration and environment of the existing OpenShift deployment on the remote host
3. run the specified [openshift-ansible](https://github.com/openshift/openshift-ansible) `health.yml` playbook using the generated inventory file from the previous step

```
docker run -u `id -u` \
-v $HOME/.ssh/libra.pem:/opt/app-root/src/.ssh/id_rsa:Z,ro \
-v /tmp/ssh/config:/opt/app-root/src/.ssh/config:Z,ro \
-v /tmp/origin/master/admin.kubeconfig:/opt/app-root/src/.kube/config:Z \
-v /tmp/aws/master-config.yaml:/opt/app-root/src/master-config.yaml:Z \
-e PLAYBOOK=playbooks/byo/openshift-checks/health.yml \
openshift/inventory-gen
```

If a `PLAYBOOK` environment variable is not supplied, the container will simply perform steps `1` and `2` from above, and output the contents of the generated inventory file to standard output.

```
$ docker run -u `id -u` \
-v $HOME/.ssh/libra.pem:/opt/app-root/src/.ssh/id_rsa:Z,ro \
-v /tmp/ssh/config:/opt/app-root/src/.ssh/config:Z,ro \
-v /tmp/origin/master/admin.kubeconfig:/opt/app-root/src/.kube/config:Z \
-v /tmp/aws/master-config.yaml:/opt/app-root/src/master-config.yaml:Z \
openshift/inventory-gen > myinventory
$ cat myinventory
localhost ansible_python_interpreter=/usr/bin/python
[OSEv3:children]
masters
nodes
etcd
[OSEv3:vars]
...
```

### Configure

To include additional inventory variables in the final generated inventory file,
create or edit the `root/etc/config.yaml` file.

### Debug

To debug the `generate` script, run the above script interactively
and manually execute `/usr/local/bin/generate`:

```
...
docker run -u `id -u` \
-v ...
...
-it openshift/inventory-gen /bin/bash
---
bash-4.2$ ls
master-config.yaml
bash-4.2$ /usr/local/bin/generate
bash-4.2$ ls
generated_hosts master-config.yaml
bash-4.2$ less generated_hosts
...
```
1 change: 1 addition & 0 deletions images/inventory-generator/config.yaml
13 changes: 13 additions & 0 deletions images/inventory-generator/root/etc/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
# default user configuration
common_host_alias: openshiftdevel # host alias as defined in ~/.ssh/config
ansible_ssh_user: ec2-user

# openshift-ansible inventory vars
openshift_uninstall_images: false
openshift_install_examples: true
openshift_deployment_type: origin

# openshift cluster-admin credentials
openshift_cluster_user: developer
openshift_cluster_pass: fakepass
17 changes: 17 additions & 0 deletions images/inventory-generator/root/usr/local/bin/entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -e
#
# This file serves as the main entrypoint to the inventory-generator image.
#
# For more information see the documentation:
# https://github.com/juanvallejo/dynamic-inventory/blob/master/README.md


# Patch /etc/passwd file with the current user info.
# The current user's entry must be correctly defined in this file in order for
# the `ssh` command to work within the created container.

if ! whoami &>/dev/null; then
echo "${USER:-default}:x:$(id -u):$(id -g):Default User:$HOME:/sbin/nologin" >> /etc/passwd
fi

exec "$@"
Loading

0 comments on commit c3c7e45

Please sign in to comment.