Skip to content

Commit

Permalink
merge inventory-generator image with existing origin-ansible image
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Aug 15, 2017
1 parent 5cee9db commit 45c37d0
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 149 deletions.
2 changes: 1 addition & 1 deletion images/installer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ COPY images/installer/origin-extra-root /
# install ansible and deps
RUN INSTALL_PKGS="python-lxml pyOpenSSL python2-cryptography openssl java-1.8.0-openjdk-headless python2-passlib httpd-tools openssh-clients origin-clients" \
&& yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS \
&& EPEL_PKGS="ansible python2-boto" \
&& EPEL_PKGS="PyYAML ansible python2-boto" \
&& yum install -y epel-release \
&& yum install -y --setopt=tsflags=nodocs $EPEL_PKGS \
&& rpm -V $INSTALL_PKGS $EPEL_PKGS \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Dynamic Inventory Generation
============================

Image that creates a container capable of
connecting to an OpenShift master and dynamically
creating an inventory file from its environment.
Script within the origin-ansible 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 .`
`docker build --rm -t openshift/origin-ansible -f images/installer/Dockerfile .`

### Run

Expand All @@ -20,39 +21,21 @@ Given a master node's `master-config.yaml` file and its `admin.kubeconfig` file,
```
docker run -u `id -u` \
-v $HOME/.ssh/id_rsa:/opt/app-root/src/.ssh/id_rsa:Z,ro \
-v /tmp/ssh/config:/opt/app-root/src/.ssh/config:Z,ro \
-v /tmp/aws/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/id_rsa:/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
-e OPTS="-v --become --become-user root" \
-e PLAYBOOK_FILE=playbooks/byo/config.yml \
-e GENERATE_INVENTORY=true \
-e USER=jvallejo \
openshift/origin-ansible
[OSEv3:vars]
...
```

### Configure

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

### Debug

Expand All @@ -64,15 +47,21 @@ and manually execute `/usr/local/bin/generate`:
docker run -u `id -u` \
-v ...
...
-it openshift/inventory-gen /bin/bash
-it openshift/origin-ansible /bin/bash
---
bash-4.2$ cd $HOME
bash-4.2$ ls
master-config.yaml
bash-4.2$ /usr/local/bin/generate
bash-4.2$ /usr/local/bin/generate $HOME/generated_hosts
bash-4.2$ ls
generated_hosts master-config.yaml
bash-4.2$ less generated_hosts
...
```

### Notes

For now, the `/usr/local/bin/generate` script will look for the `master-config.yaml` file in the
home directory in the container (`/opt/app-root/src`).
1 change: 1 addition & 0 deletions images/installer/inventory-generator-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ openshift_install_examples: true
openshift_deployment_type: origin

# openshift cluster-admin credentials
openshift_cluster_user: cluster-node-viewer
openshift_cluster_user: developer
openshift_cluster_pass: ""
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import subprocess
import sys
import yaml

DEFAULT_USER_CONFIG_PATH = '/etc/config.yaml'
DEFAULT_USER_CONFIG_PATH = '/etc/inventory-generator-config.yaml'

try:
HOME = os.environ['HOME']
Expand Down Expand Up @@ -44,7 +44,7 @@ class InvalidHostGroup(Exception):
pass


class OpenShiftClient():
class OpenShiftClient:
oc = None

def __init__(self):
Expand Down Expand Up @@ -84,16 +84,16 @@ class OpenShiftClient():
raise OpenShiftClientError('[rc {}] {}\n{}'.format(err.returncode, ' '.join(err.cmd), err.output))
return out


def login(self, host, user, password):
"""Login using `oc` to the specified host"""
call_cmd = 'login {host} -u {u} -p {p}'
call_cmd = 'login {host} -u {u} -p {p} --insecure-skip-tls-verify'
return self.call(call_cmd.format(host=host, u=user, p=password))

def get_nodes(self):
"""Retrieve remote node information as a yaml object"""
return self.call('get nodes -o yaml')


class HostGroup():
groupname = ""
hosts = list()
Expand Down Expand Up @@ -217,7 +217,6 @@ def main():
print "Bind-mounted host master configuration file is not of 'kind' MasterConfig. Aborting..."
exit(1)


# finish reading config file and begin gathering
# cluster information for inventory file
file_obj.close()
Expand All @@ -230,21 +229,23 @@ def main():
print "Unable to find or read user configuration file '{}': {}".format(USER_CONFIG, err)
exit(1)


# set inventory values based on user configuration
common_host_alias = user_config.get('common_host_alias', 'openshiftdevel')
ansible_ssh_user = user_config.get('ansible_ssh_user', 'ec2-user')
common_host_alias = user_config.get('common_host_alias', 'openshiftdevel')
ansible_ssh_user = user_config.get('ansible_ssh_user', 'ec2-user')
openshift_uninstall_images = user_config.get('openshift_uninstall_images', False)
openshift_install_examples = user_config.get('openshift_install_examples', True)
openshift_deployment_type = user_config.get('openshift_deployment_type', 'origin')
openshift_cluster_user = user_config.get('openshift_cluster_user', 'developer')
openshift_cluster_pass = user_config.get('openshift_cluster_pass', 'fakepass')
openshift_deployment_type = user_config.get('openshift_deployment_type', 'origin')
openshift_cluster_user = user_config.get('openshift_cluster_user', 'developer')
openshift_cluster_pass = user_config.get('openshift_cluster_pass', 'fakepass')

# default value for cluster-viewere is blank in config. Handle this case to avoid an `oc login` flag error
if not len(openshift_cluster_pass):
openshift_cluster_pass = 'fakepass'

# extract host config info from parsed yaml file
asset_config = y.get("assetConfig")
asset_config = y.get("assetConfig")
master_config = y.get("kubernetesMasterConfig")
etcd_config = y.get("etcdClientInfo")
etcd_config = y.get("etcdClientInfo")

# if master_config is missing, error out; we expect to be running on a master to be able to
# gather enough information to generate the rest of the inventory file.
Expand All @@ -258,7 +259,7 @@ def main():
exit(1)

# connect to remote host using `oc login...` and extract all possible node information
oc = OpenShiftClient()
oc = OpenShiftClient()
oc.login(master_public_url, openshift_cluster_user, openshift_cluster_pass)
nodes_config = yaml.load(oc.get_nodes())

Expand All @@ -271,7 +272,6 @@ def main():
if asset_config and asset_config.get('loggingPublicURL'):
openshift_hosted_logging_deploy = True


m = Host("masters")
m.host_alias(common_host_alias)
m.address(master_config["masterIP"])
Expand Down Expand Up @@ -312,7 +312,7 @@ def main():

host_groups["etcd"] = HostGroup(etcd_hosts)

# open new inventory file for writing
# open new inventory file for writing
try:
inv_file_obj = open(HOME + '/' + INVY, 'w+')
except IOError as err:
Expand All @@ -324,7 +324,6 @@ def main():
inv_file_obj.write("{}\n".format(group))
inv_file_obj.write("\n")


inv_file_obj.write("[OSEv3:vars]\n")
inv_file_obj.write("ansible_ssh_user={}\n".format(ansible_ssh_user))
inv_file_obj.write("ansible_become={}\n".format(ansible_become))
Expand Down
5 changes: 4 additions & 1 deletion images/installer/root/usr/local/bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ elif [[ -v INVENTORY_URL ]]; then
elif [[ -v DYNAMIC_SCRIPT_URL ]]; then
curl -o ${INVENTORY} ${DYNAMIC_SCRIPT_URL}
chmod 755 ${INVENTORY}
elif [[ -v GENERATE_INVENTORY ]]; then
# dynamically generate inventory file using bind-mounted info
/usr/local/bin/generate ${INVENTORY}
else
echo
echo "One of INVENTORY_FILE, INVENTORY_URL or DYNAMIC_SCRIPT_URL must be provided."
echo "One of INVENTORY_FILE, INVENTORY_URL, GENERATE_INVENTORY, or DYNAMIC_SCRIPT_URL must be provided."
exec /usr/local/bin/usage
fi
INVENTORY_ARG="-i ${INVENTORY}"
Expand Down
6 changes: 6 additions & 0 deletions images/installer/root/usr/local/bin/user_setup
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ mkdir -p ${HOME}
chown ${USER_UID}:0 ${HOME}
chmod ug+rwx ${HOME}

chown ${USER_UID}:0 ${HOME}/.ssh/config

# runtime user will need to be able to self-insert in /etc/passwd
chmod g+rw /etc/passwd

# make required config dirs writable
chown ${USER_UID}:0 ${HOME}/.kube/config
chmod a+rw ${HOME}/.kube/config

# ensure that the ansible content is accessible
chmod -R g+r ${WORK_DIR}
find ${WORK_DIR} -type d -exec chmod g+x {} +
Expand Down
26 changes: 0 additions & 26 deletions images/inventory-generator/Dockerfile

This file was deleted.

1 change: 0 additions & 1 deletion images/inventory-generator/config.yaml

This file was deleted.

17 changes: 0 additions & 17 deletions images/inventory-generator/root/usr/local/bin/entrypoint

This file was deleted.

15 changes: 0 additions & 15 deletions images/inventory-generator/root/usr/local/bin/run

This file was deleted.

39 changes: 0 additions & 39 deletions images/inventory-generator/root/usr/local/bin/user_setup

This file was deleted.

0 comments on commit 45c37d0

Please sign in to comment.