Browse Source

Use config-drive to get metadata if available

Metadata available via the metadata url 169.254.169.254/32, but this
does not work for IPv6 networks (see bug:
  https://bugs.launchpad.net/neutron/+bug/1460177).

Metadata can also be available via the config drive, non-dependent
of IPv4 networking and NAT redirect rules on the metadata server.

This changes the novajoin cloud config generated "setup-ipa-client.sh"
script. The script will initially try to load metadata of the
config-drive, and fall back to loading the metadata from the metadata
service url in case the config-drive method fails.

Related-Bug: #1795722
Change-Id: Idaca9de2f9c50b27475fd27ab8b388cb85849d70
Harald Jensås 6 months ago
parent
commit
b9098eb15c
1 changed files with 1 additions and 1 deletions
  1. 1
    1
      files/cloud-config-novajoin.json

+ 1
- 1
files/cloud-config-novajoin.json View File

@@ -1 +1 @@
1
-{"cloud-init": "#cloud-config\npackages:\n - python-simplejson\n - ipa-client\n - ipa-admintools\n - openldap-clients\n - hostname\nwrite_files:\n - content: |\n     #!/bin/sh\n     \n     # Get the instance hostname out of the metadata\n     #data=`curl http://169.254.169.254/openstack/latest/meta_data.json 2>/dev/null`\n     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')\n     if [[ $? != 0 ]] ; then\n         echo \"Unable to retrieve metadata\"\n         exit 1\n     fi\n     \n     fqdn=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get(\"join\", {}).get(\"hostname\", \"\")'`\n     \n     if [ -z \"$fqdn\" ]; then\n         echo \"Unable to determine hostname\"\n         exit 1\n     fi\n     \n     realm=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get(\"join\", {}).get(\"krb_realm\", \"\")'`\n     otp=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get(\"join\", {}).get(\"ipaotp\", \"\")'`\n\n     hostname=`/bin/hostname -f`\n\n     # run ipa-client-install\n     OPTS=\"-U -w $otp\"\n     if [ $hostname != $fqdn ]; then\n         OPTS=\"$OPTS --hostname $fqdn\"\n     fi\n     if [ -n \"$realm\" ]; then\n         OPTS=\"$OPTS --realm=$realm\"\n     fi\n     ipa-client-install $OPTS\n   path: /root/setup-ipa-client.sh\n   permissions: '0700'\n   owner: root:root\nruncmd:\n- sh -x /root/setup-ipa-client.sh > /var/log/setup-ipa-client.log 2>&1"}
1
+{"cloud-init": "#cloud-config\npackages:\n - python-simplejson\n - ipa-client\n - ipa-admintools\n - openldap-clients\n - hostname\nwrite_files:\n - content: |\n     #!/bin/sh\n     \n     function get_metadata_config_drive {\n         if [ -f /run/cloud-init/status.json ]; then\n             # Get metadata from config drive\n             data=`cat /run/cloud-init/status.json`\n             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]'`\n             if [ -b $config_drive ]; then\n                 temp_dir=`mktemp -d`\n                 mount $config_drive $temp_dir\n                 if [ -f $temp_dir/openstack/latest/vendor_data2.json ]; then\n                     data=`cat $temp_dir/openstack/latest/vendor_data2.json`\n                     umount $config_drive\n                     rmdir $temp_dir\n                 else\n                     umount $config_drive\n                     rmdir $temp_dir\n                 fi\n             else \n                 echo \"Unable to retrieve metadata from config drive.\"\n                 return 1\n             fi\n         else\n             echo \"Unable to retrieve metadata from config drive.\"\n             return 1\n         fi\n     \n         return 0\n     }\n     \n     function get_metadata_network {\n         # Get metadata over the network\n         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')\n     \n         if [[ $? != 0 ]] ; then\n             echo \"Unable to retrieve metadata from metadata service.\"\n             return 1\n         fi\n     }\n     \n     \n     if ! get_metadata_config_drive; then\n        if ! get_metadata_network; then\n            echo \"FATAL: No metadata available\"\n            exit 1\n        fi\n     fi\n     \n     # Get the instance hostname out of the metadata\n     fqdn=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get(\"join\", {}).get(\"hostname\", \"\")'`\n      \n     if [ -z \"$fqdn\" ]; then\n         echo \"Unable to determine hostname\"\n         exit 1\n     fi\n      \n     realm=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get(\"join\", {}).get(\"krb_realm\", \"\")'`\n     otp=`echo $data | python -c 'import json,sys;obj=json.load(sys.stdin);print obj.get(\"join\", {}).get(\"ipaotp\", \"\")'`\n     \n     hostname=`/bin/hostname -f`\n      \n     # run ipa-client-install\n     OPTS=\"-U -w $otp\"\n     if [ $hostname != $fqdn ]; then\n         OPTS=\"$OPTS --hostname $fqdn\"\n     fi\n     if [ -n \"$realm\" ]; then\n         OPTS=\"$OPTS --realm=$realm\"\n     fi\n     ipa-client-install $OPTS\n   path: /root/setup-ipa-client.sh\n   permissions: '0700'\n   owner: root:root\nruncmd:\n- sh -x /root/setup-ipa-client.sh > /var/log/setup-ipa-client.log 2>&1"}

Loading…
Cancel
Save