From 723db1317cd62a8772c764847c80a339eae4c03f Mon Sep 17 00:00:00 2001
From: Steven Hardy <shardy@redhat.com>
Date: Thu, 2 Apr 2015 09:52:18 -0400
Subject: [PATCH] Enable passing optional first-boot user-data

Currently all the OS::Nova::Server resource created don't pass any
user-data.  It's possible to pass user-data as well as using heat
SoftwareConfig/SoftwareDeployment resources, and this can be useful
when you have simple "first boot" tasks which are possible either via
cloud-init, or via simple run-once scripts.

This enables passing such data by implementing a new provider resource
OS::TripleO::NodeUserData, which defaults to passing an empty mime
archive (thus it's a no-op).  An example of non no-op usage is also
provided.

Change-Id: Id0caba69768630e3a10439ba1fc2547a609c0cfe
---
 ceph-storage.yaml                       |  7 ++-
 cinder-storage.yaml                     |  7 ++-
 compute.yaml                            |  4 ++
 controller.yaml                         |  4 ++
 firstboot/userdata_default.yaml         | 20 +++++++++
 firstboot/userdata_example.yaml         | 60 +++++++++++++++++++++++++
 nagios3.yaml                            |  7 ++-
 overcloud-resource-registry-puppet.yaml |  1 +
 overcloud-resource-registry.yaml        |  1 +
 puppet/ceph-storage-puppet.yaml         |  5 +++
 puppet/cinder-storage-puppet.yaml       |  6 ++-
 puppet/compute-puppet.yaml              |  4 ++
 puppet/controller-puppet.yaml           |  4 ++
 puppet/swift-storage-puppet.yaml        |  6 ++-
 swift-storage.yaml                      |  7 ++-
 15 files changed, 137 insertions(+), 6 deletions(-)
 create mode 100644 firstboot/userdata_default.yaml
 create mode 100644 firstboot/userdata_example.yaml

diff --git a/ceph-storage.yaml b/ceph-storage.yaml
index d047488b13..93b4deb6f6 100644
--- a/ceph-storage.yaml
+++ b/ceph-storage.yaml
@@ -36,9 +36,14 @@ resources:
         {get_param: Image}
       flavor: {get_param: OvercloudCephStorageFlavor}
       key_name: {get_param: KeyName}
-      user_data_format: SOFTWARE_CONFIG
       networks:
         - network: ctlplane
+      user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
+
   CephStorageDeployment:
     type: OS::Heat::StructuredDeployment
     properties:
diff --git a/cinder-storage.yaml b/cinder-storage.yaml
index 7eab89a602..24739115c7 100644
--- a/cinder-storage.yaml
+++ b/cinder-storage.yaml
@@ -103,9 +103,14 @@ resources:
         {get_param: Image}
       flavor: {get_param: Flavor}
       key_name: {get_param: KeyName}
-      user_data_format: SOFTWARE_CONFIG
       networks:
         - network: ctlplane
+      user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
+
   BlockStorageDeployment:
     type: OS::Heat::StructuredDeployment
     properties:
diff --git a/compute.yaml b/compute.yaml
index 3723169c01..a4190893de 100644
--- a/compute.yaml
+++ b/compute.yaml
@@ -259,6 +259,10 @@ resources:
       networks:
         - network: ctlplane
       user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
 
   NetworkConfig:
     type: OS::TripleO::Net::SoftwareConfig
diff --git a/controller.yaml b/controller.yaml
index 483ecc8fdd..37c0debee5 100644
--- a/controller.yaml
+++ b/controller.yaml
@@ -418,6 +418,10 @@ resources:
       networks:
         - network: ctlplane
       user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
 
   NetworkConfig:
     type: OS::TripleO::Net::SoftwareConfig
diff --git a/firstboot/userdata_default.yaml b/firstboot/userdata_default.yaml
new file mode 100644
index 0000000000..140d2bf8fa
--- /dev/null
+++ b/firstboot/userdata_default.yaml
@@ -0,0 +1,20 @@
+heat_template_version: 2014-10-16
+
+description: >
+  This is a default no-op template which provides empty user-data
+  which can be passed to the OS::Nova::Server resources.
+  This template can be replaced with a different implementation via
+  the resource registry, such that deployers may customize their
+  first-boot configuration.
+
+resources:
+  userdata:
+    type: OS::Heat::MultipartMime
+
+outputs:
+  # This means get_resource from the parent template will get the userdata, see:
+  # http://docs.openstack.org/developer/heat/template_guide/composition.html#making-your-template-resource-more-transparent
+  # Note this is new-for-kilo, an alternative is returning a value then using
+  # get_attr in the parent template instead.
+  OS::stack_id:
+    value: {get_resource: userdata}
diff --git a/firstboot/userdata_example.yaml b/firstboot/userdata_example.yaml
new file mode 100644
index 0000000000..a0d8c7acda
--- /dev/null
+++ b/firstboot/userdata_example.yaml
@@ -0,0 +1,60 @@
+heat_template_version: 2014-10-16
+
+# NOTE: You don't need to pass the parameter explicitly from the
+# parent template, it can be specified via the parameter_defaults
+# in the resource_registry instead, if you want to override the default
+# and/or share values with other templates in the tree.
+parameters:
+  extra_username:
+    type: string
+    default: extrauser
+
+description: >
+  This is an example showing how you can do firstboot configuration
+  of the nodes via cloud-init.  To enable this, replace the default
+  mapping of OS::TripleO::NodeUserData in ../overcloud_resource_registry*
+
+resources:
+  userdata:
+    type: OS::Heat::MultipartMime
+    properties:
+      parts:
+      - config: {get_resource: user_config}
+      - config: {get_resource: ssh_config}
+
+  # Get cloud-init to create an extra user, in addition to the default for the
+  # distro.  Note there are various options, including configuring ssh keys,
+  # but atm I can only see how to specify the keys explicitly, not via metadata
+  user_config:
+    type: OS::Heat::CloudConfig
+    properties:
+      cloud_config:
+        users:
+        - default
+        - name: {get_param: extra_username}
+
+  # Setup ssh key for the extra user to match the key installed for the default
+  # user, e.g that provided via the nova keypair on instance boot
+  ssh_config:
+    type: OS::Heat::SoftwareConfig
+    properties:
+      config:
+        str_replace:
+          template: |
+            #!/bin/bash
+            curl http://169.254.169.254/openstack/2012-08-10/meta_data.json -o /root/meta_data.json
+            mkdir -p /home/$user/.ssh
+            chmod 700 /home/$user/.ssh
+            cat /root/meta_data.json | jq -r ".keys[0].data" > /home/$user/.ssh/authorized_keys
+            chmod 600 /home/$user/.ssh/authorized_keys
+            chown -R $user:$user /home/$user/.ssh
+          params:
+            $user: {get_param: extra_username}
+
+outputs:
+  # This means get_resource from the parent template will get the userdata, see:
+  # http://docs.openstack.org/developer/heat/template_guide/composition.html#making-your-template-resource-more-transparent
+  # Note this is new-for-kilo, an alternative is returning a value then using
+  # get_attr in the parent template instead.
+  OS::stack_id:
+    value: {get_resource: userdata}
diff --git a/nagios3.yaml b/nagios3.yaml
index 0db22b8bb9..e2ba8ccfc0 100644
--- a/nagios3.yaml
+++ b/nagios3.yaml
@@ -105,10 +105,15 @@ resources:
       flavor: { get_param: flavor }
       image: { get_param: image }
       key_name: { get_param: key_name }
-      user_data_format: SOFTWARE_CONFIG
       networks:
         - network: { get_param: server_network }
           port: { get_resource: nagios_net_port }
+      user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
+
   nagios_floating_ip:
     type: OS::Neutron::FloatingIP
     properties:
diff --git a/overcloud-resource-registry-puppet.yaml b/overcloud-resource-registry-puppet.yaml
index c64cb49469..2536053003 100644
--- a/overcloud-resource-registry-puppet.yaml
+++ b/overcloud-resource-registry-puppet.yaml
@@ -15,6 +15,7 @@ resource_registry:
   OS::TripleO::CephClusterConfig::SoftwareConfig: puppet/ceph-cluster-config.yaml
   OS::TripleO::AllNodes::SoftwareConfig: puppet/all-nodes-config.yaml
   OS::TripleO::BootstrapNode::SoftwareConfig: puppet/bootstrap-config.yaml
+  OS::TripleO::NodeUserData: firstboot/userdata_default.yaml
 
 parameter_defaults:
   EnablePackageInstall: false
diff --git a/overcloud-resource-registry.yaml b/overcloud-resource-registry.yaml
index d4f75e0735..aba2212a53 100644
--- a/overcloud-resource-registry.yaml
+++ b/overcloud-resource-registry.yaml
@@ -15,3 +15,4 @@ resource_registry:
   OS::TripleO::CephClusterConfig::SoftwareConfig: ceph-cluster-config.yaml
   OS::TripleO::AllNodes::SoftwareConfig: all-nodes-config.yaml
   OS::TripleO::BootstrapNode::SoftwareConfig: bootstrap-config.yaml
+  OS::TripleO::NodeUserData: firstboot/userdata_default.yaml
diff --git a/puppet/ceph-storage-puppet.yaml b/puppet/ceph-storage-puppet.yaml
index e9f82c2ef7..4ce1622519 100644
--- a/puppet/ceph-storage-puppet.yaml
+++ b/puppet/ceph-storage-puppet.yaml
@@ -40,6 +40,11 @@ resources:
       networks:
         - network: ctlplane
       user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
+
   CephStorageDeployment:
     type: OS::Heat::StructuredDeployment
     properties:
diff --git a/puppet/cinder-storage-puppet.yaml b/puppet/cinder-storage-puppet.yaml
index ecea763b4c..3e55ec85d0 100644
--- a/puppet/cinder-storage-puppet.yaml
+++ b/puppet/cinder-storage-puppet.yaml
@@ -115,9 +115,13 @@ resources:
         {get_param: Image}
       flavor: {get_param: Flavor}
       key_name: {get_param: KeyName}
-      user_data_format: SOFTWARE_CONFIG
       networks:
         - network: ctlplane
+      user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
 
   BlockStorageDeployment:
     type: OS::Heat::StructuredDeployment
diff --git a/puppet/compute-puppet.yaml b/puppet/compute-puppet.yaml
index 382a6b53e9..11910d79a1 100644
--- a/puppet/compute-puppet.yaml
+++ b/puppet/compute-puppet.yaml
@@ -262,6 +262,10 @@ resources:
       networks:
         - network: ctlplane
       user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
 
   NetworkConfig:
     type: OS::TripleO::Net::SoftwareConfig
diff --git a/puppet/controller-puppet.yaml b/puppet/controller-puppet.yaml
index d395546719..a904e041c4 100644
--- a/puppet/controller-puppet.yaml
+++ b/puppet/controller-puppet.yaml
@@ -425,6 +425,10 @@ resources:
       networks:
         - network: ctlplane
       user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
 
   NetworkConfig:
     type: OS::TripleO::Net::SoftwareConfig
diff --git a/puppet/swift-storage-puppet.yaml b/puppet/swift-storage-puppet.yaml
index eba44e6c40..6f861d889c 100644
--- a/puppet/swift-storage-puppet.yaml
+++ b/puppet/swift-storage-puppet.yaml
@@ -60,9 +60,13 @@ resources:
       image: {get_param: Image}
       flavor: {get_param: Flavor}
       key_name: {get_param: KeyName}
-      user_data_format: SOFTWARE_CONFIG
       networks:
         - network: ctlplane
+      user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
 
   SwiftStorageHieraConfig:
     type: OS::Heat::StructuredConfig
diff --git a/swift-storage.yaml b/swift-storage.yaml
index 42a78666e6..db8ff5d34d 100644
--- a/swift-storage.yaml
+++ b/swift-storage.yaml
@@ -108,9 +108,14 @@ resources:
       image: {get_param: Image}
       flavor: {get_param: Flavor}
       key_name: {get_param: KeyName}
-      user_data_format: SOFTWARE_CONFIG
       networks:
         - network: ctlplane
+      user_data_format: SOFTWARE_CONFIG
+      user_data: {get_resource: NodeUserData}
+
+  NodeUserData:
+    type: OS::TripleO::NodeUserData
+
   SwiftStorageDeploy:
     type: OS::Heat::StructuredDeployment
     properties: