From 08b08c78b571a2b339508412acc59f02b8d9435a Mon Sep 17 00:00:00 2001
From: Carlos Cesario <carloscesario@gmail.com>
Date: Sat, 26 Mar 2016 16:41:46 -0300
Subject: [PATCH] Identify container through  PS1 shell variable

As suggested it was created the variable KOLLA_SERVICE_NAME to identify
the container service name through  PS1 shell variable.
This method it was previously discussed in IRC.
https://goo.gl/k7AdEg

The other option it was usage hostname param in kolla_docker, but
currently docker does not support it due this issue:
https://github.com/docker/compose/issues/2460

The final result is like this:

$ docker exec -it heka /bin/bash
(heka)[heka@kolla-control /]$

$ docker exec -it mariadb /bin/bash
(mariadb)[mysql@kolla-control /]$

More details can be accessed through this link
http://paste.openstack.org/show/493689/

Closes-Bug: #1557454
Change-Id: I6aab8d640a8ebb17baa9d6d4f1edd6e331674713
---
 ansible/library/kolla_docker.py |  9 ++++++++-
 docker/base/Dockerfile.j2       | 16 ++++++++++++++++
 docker/base/kolla_bashrc        |  6 ++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 docker/base/kolla_bashrc

diff --git a/ansible/library/kolla_docker.py b/ansible/library/kolla_docker.py
index ddb88f901f..844bb633ec 100644
--- a/ansible/library/kolla_docker.py
+++ b/ansible/library/kolla_docker.py
@@ -477,8 +477,15 @@ class DockerWorker(object):
 
         return self.dc.create_host_config(**options)
 
+    def _inject_env_var(self, environment_info):
+        newenv = {
+            'KOLLA_SERVICE_NAME': self.params.get('name').replace('_', '-')
+        }
+        environment_info.update(newenv)
+        return environment_info
+
     def _format_env_vars(self):
-        env = self.params.get('environment')
+        env = self._inject_env_var(self.params.get('environment'))
         return {k: "" if env[k] is None else env[k] for k in env}
 
     def build_container_options(self):
diff --git a/docker/base/Dockerfile.j2 b/docker/base/Dockerfile.j2
index afcc5b30b1..34bfac9f90 100644
--- a/docker/base/Dockerfile.j2
+++ b/docker/base/Dockerfile.j2
@@ -10,6 +10,14 @@ ENV KOLLA_BASE_DISTRO {{ base_distro }}
 ENV KOLLA_INSTALL_TYPE {{ install_type }}
 ENV KOLLA_INSTALL_METATYPE {{ install_metatype }}
 
+#### Customize PS1 to be used with bash shell
+COPY kolla_bashrc /tmp/
+RUN cat /tmp/kolla_bashrc >> /etc/skel/.bashrc
+RUN cat /tmp/kolla_bashrc >> /root/.bashrc
+
+# PS1 var when used /bin/sh shell
+ENV PS1="$(tput bold)($(printenv KOLLA_SERVICE_NAME))$(tput sgr0)[$(id -un)@$(hostname -s) $(pwd)]$ "
+
 # For RPM Variants, enable the correct repositories - this should all be done
 # in the base image so repos are consistent throughout the system.  This also
 # enables to provide repo overrides at a later date in a simple fashion if we
@@ -17,6 +25,9 @@ ENV KOLLA_INSTALL_METATYPE {{ install_metatype }}
 
 {% if base_distro in ['fedora', 'centos', 'oraclelinux', 'rhel'] %}
 
+# Customize PS1 bash shell
+RUN cat /tmp/kolla_bashrc >> /etc/bashrc
+
 #### BEGIN REPO ENABLEMENT
 # Turns on Elasticsearch repos
 COPY elasticsearch.yum.repo /etc/yum.repos.d/elasticsearch.repo
@@ -152,6 +163,9 @@ RUN yum -y install \
 {# endif for base_distro centos,fedora,oraclelinux,rhel #}
 {% elif base_distro in ['ubuntu', 'debian'] %}
 
+# Customize PS1 bash shell
+RUN cat /tmp/kolla_bashrc >> /etc/bash.bashrc
+
 # This will prevent questions from being asked during the install
 ENV DEBIAN_FRONTEND noninteractive
 
@@ -189,4 +203,6 @@ RUN touch /usr/local/bin/kolla_extend_start \
     && chmod 440 /etc/sudoers \
     && groupadd kolla
 
+RUN rm -f /tmp/kolla_bashrc
+
 CMD ["kolla_start"]
diff --git a/docker/base/kolla_bashrc b/docker/base/kolla_bashrc
new file mode 100644
index 0000000000..c797e51a9b
--- /dev/null
+++ b/docker/base/kolla_bashrc
@@ -0,0 +1,6 @@
+
+#### Custom data added by Kolla
+_KBOLD="\[$(tput bold)\]"
+_KRESET="\[$(tput sgr0)\]"
+
+PS1="${_KBOLD}(${KOLLA_SERVICE_NAME})${_KRESET}[\u@\h \W]\\$ "
-- 
GitLab