From f6e9e389b96b5be598ec79464b1e8bb883d62da8 Mon Sep 17 00:00:00 2001
From: "James E. Blair" <jeblair@linux.vnet.ibm.com>
Date: Wed, 24 Feb 2016 10:05:28 -0800
Subject: [PATCH] Set hostname in launch node

Since nova does not believe in the existence of hostnames, we need
to set them ourselves when we boot new servers in launch-node.

Change-Id: Ib318224a09c1b0b748ab31e1ed507975b3190784
---
 launch/shade-launch-node.py | 7 +++++++
 launch/sshclient.py         | 8 ++++++++
 2 files changed, 15 insertions(+)

diff --git a/launch/shade-launch-node.py b/launch/shade-launch-node.py
index f8f3392d4b..e88f629229 100644
--- a/launch/shade-launch-node.py
+++ b/launch/shade-launch-node.py
@@ -84,6 +84,13 @@ def bootstrap_server(server, key, cert, environment, name,
     ssh_client.ssh('bash -x install_puppet.sh')
 
     certname = cert[:(0 - len('.pem'))]
+    shortname = name.split('.')[0]
+    with ssh_client.open('/etc/hosts', 'w') as f:
+        f.write('127.0.0.1 localhost\n')
+        f.write('127.0.1.1 %s %s\n' % (name, shortname))
+    with ssh_client.open('/etc/hostname', 'w') as f:
+        f.write('%s\n' % (shortname,))
+    ssh_client.ssh("hostname %s" % (name,))
     ssh_client.ssh("mkdir -p /var/lib/puppet/ssl/certs")
     ssh_client.ssh("mkdir -p /var/lib/puppet/ssl/private_keys")
     ssh_client.ssh("mkdir -p /var/lib/puppet/ssl/public_keys")
diff --git a/launch/sshclient.py b/launch/sshclient.py
index 81afe4301f..49d6bf6359 100644
--- a/launch/sshclient.py
+++ b/launch/sshclient.py
@@ -18,6 +18,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import contextlib
 import sys
 
 import paramiko
@@ -55,3 +56,10 @@ class SSHClient(object):
         ftp = self.client.open_sftp()
         ftp.put(source, dest)
         ftp.close()
+
+    @contextlib.contextmanager
+    def open(self, path, mode):
+        ftp = self.client.open_sftp()
+        f = ftp.open(path, mode)
+        yield f
+        ftp.close()