Browse Source

Merge "Helper scripts for modifying cloud-config-novajoin.json"

tags/1.2.0
Zuul 3 weeks ago
parent
commit
323efaad22
4 changed files with 112 additions and 0 deletions
  1. 14
    0
      files/README
  2. 81
    0
      files/cloud-config-novajoin.yaml
  3. 9
    0
      tools/cloud-config-pack.py
  4. 8
    0
      tools/cloud-config-unpack.py

+ 14
- 0
files/README View File

@@ -0,0 +1,14 @@
1
+This directory contains configuration files placed in /etc/
2
+(see setup.cfg for details)
3
+
4
+IMPORTANT: cloud-config-novajoin.json is manually generated from
5
+cloud-config-novajoin.yaml, by using tools/cloud-config-unpack.py and
6
+tools/cloud-config-pack.py scripts.
7
+
8
+Before editing cloud-config-novajoin.yaml, run tools/cloud-config-unpack.py.
9
+After editing cloud-config-novajoin.yaml, run tools/cloud-config-pack.py.
10
+This will place your edits in cloud-config-novajoin.json, which is the only
11
+file that is installed in /etc/.
12
+
13
+cloud-config-novajoin.json cannot be automatically generated, because we use a
14
+setup.py provided by the global requirements repo, which we cannot modify.

+ 81
- 0
files/cloud-config-novajoin.yaml View File

@@ -0,0 +1,81 @@
1
+#cloud-config
2
+packages:
3
+ - python-simplejson
4
+ - ipa-client
5
+ - ipa-admintools
6
+ - openldap-clients
7
+ - hostname
8
+write_files:
9
+ - content: |
10
+     #!/bin/sh
11
+     
12
+     function get_metadata_config_drive {
13
+         if [ -f /run/cloud-init/status.json ]; then
14
+             # Get metadata from config drive
15
+             data=`cat /run/cloud-init/status.json`
16
+             config_drive=`echo $data | python -c 'import json,re,sys;obj=json.load(sys.stdin);ds=obj.get("v1", {}).get("datasource"); print(re.findall(r"source=(.*)]", ds)[0])'`
17
+             if [[ -b $config_drive ]]; then
18
+                 temp_dir=`mktemp -d`
19
+                 mount $config_drive $temp_dir
20
+                 if [ -f $temp_dir/openstack/latest/vendor_data2.json ]; then
21
+                     data=`cat $temp_dir/openstack/latest/vendor_data2.json`
22
+                     umount $config_drive
23
+                     rmdir $temp_dir
24
+                 else
25
+                     umount $config_drive
26
+                     rmdir $temp_dir
27
+                 fi
28
+             else 
29
+                 echo "Unable to retrieve metadata from config drive."
30
+                 return 1
31
+             fi
32
+         else
33
+             echo "Unable to retrieve metadata from config drive."
34
+             return 1
35
+         fi
36
+     
37
+         return 0
38
+     }
39
+     
40
+     function get_metadata_network {
41
+         # Get metadata over the network
42
+         data=$(timeout 300 /bin/bash -c 'data=""; while [ -z "$data" ]; do sleep $[ ( $RANDOM % 10 )  + 1 ]s; data=`curl -s http://169.254.169.254/openstack/2016-10-06/vendor_data2.json 2>/dev/null`; done; echo $data')
43
+     
44
+         if [[ $? != 0 ]] ; then
45
+             echo "Unable to retrieve metadata from metadata service."
46
+             return 1
47
+         fi
48
+     }
49
+     
50
+     
51
+     if ! get_metadata_config_drive; then
52
+        if ! get_metadata_network; then
53
+            echo "FATAL: No metadata available"
54
+            exit 1
55
+        fi
56
+     fi
57
+     
58
+     # Get the instance hostname out of the metadata
59
+     fqdn=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj.get("join", {}).get("hostname", ""))'`
60
+      
61
+     if [ -z "$fqdn" ]; then
62
+         echo "Unable to determine hostname"
63
+         exit 1
64
+     fi
65
+      
66
+     realm=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj.get("join", {}).get("krb_realm", ""))'`
67
+     otp=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj.get("join", {}).get("ipaotp", ""))'`
68
+     
69
+     hostname=`/bin/hostname -f`
70
+      
71
+     # run ipa-client-install
72
+     OPTS="-U -w $otp --hostname $fqdn --mkhomedir"
73
+     if [ -n "$realm" ]; then
74
+         OPTS="$OPTS --realm=$realm"
75
+     fi
76
+     ipa-client-install $OPTS
77
+   path: /root/setup-ipa-client.sh
78
+   permissions: '0700'
79
+   owner: root:root
80
+runcmd:
81
+- sh -x /root/setup-ipa-client.sh > /var/log/setup-ipa-client.log 2>&1

+ 9
- 0
tools/cloud-config-pack.py View File

@@ -0,0 +1,9 @@
1
+#!/usr/bin/env python
2
+
3
+import json
4
+
5
+with open('files/cloud-config-novajoin.yaml') as config_unpacked:
6
+    config = config_unpacked.read()
7
+    with open('files/cloud-config-novajoin.json', 'w') as cloud_config:
8
+        json.dump({'cloud-init': config}, cloud_config)
9
+        cloud_config.write('\n')

+ 8
- 0
tools/cloud-config-unpack.py View File

@@ -0,0 +1,8 @@
1
+#!/usr/bin/env python
2
+
3
+import json
4
+
5
+with open('files/cloud-config-novajoin.json') as cloud_config:
6
+    config = json.load(cloud_config)['cloud-init']
7
+    with open('files/cloud-config-novajoin.yaml', 'w') as config_unpacked:
8
+        config_unpacked.write(config)

Loading…
Cancel
Save