github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/contrib/azure/create-azure-user-data (about)

     1  #!/usr/bin/env python
     2  """
     3  Create CoreOS user-data by merging contrib/coreos/user-data and azure-user-data
     4  
     5  Usage: create-azure-user-data.py <discovery_url>
     6  
     7  Arguments:
     8      <discovery_url> This is the CoreOS etcd discovery URL. You should generate
     9      a new URL at https://discovery.etcd.io/new and pass it as the argument.
    10  """
    11  
    12  import sys
    13  import os
    14  import re
    15  import yaml
    16  import collections
    17  
    18  
    19  def combine_dicts(orig_dict, new_dict):
    20      for key, val in new_dict.iteritems():
    21          if isinstance(val, collections.Mapping):
    22              tmp = combine_dicts(orig_dict.get(key, {}), val)
    23              orig_dict[key] = tmp
    24          elif isinstance(val, list):
    25              orig_dict[key] = (val + orig_dict[key])
    26          else:
    27              orig_dict[key] = new_dict[key]
    28      return orig_dict
    29  
    30  
    31  def get_file(name, mode="r", abspath=False):
    32      current_dir = os.path.dirname(__file__)
    33  
    34      if abspath:
    35          return file(os.path.abspath(os.path.join(current_dir, name)), mode)
    36      else:
    37          return file(os.path.join(current_dir, name), mode)
    38  
    39  
    40  def main():
    41      try:
    42          url = sys.argv[1]
    43      except (NameError, IndexError):
    44          print __doc__
    45          return 1
    46  
    47      url_pattern = 'http[|s]\:[\/]{2}discovery\.etcd\.io\/[0-9a-f]{32}'
    48  
    49      m = re.match(url_pattern, url)
    50  
    51      if not m:
    52          print "Discovery URL invalid."
    53          return 1
    54  
    55      azure_user_data = get_file("azure-user-data", "w", True)
    56      azure_template = get_file("azure-user-data-template")
    57      coreos_template = get_file("../coreos/user-data.example")
    58  
    59      configuration_coreos_template = yaml.safe_load(coreos_template)
    60      configuration_azure_template = yaml.safe_load(azure_template)
    61  
    62      configuration = combine_dicts(configuration_coreos_template,
    63                                    configuration_azure_template)
    64  
    65      dump = yaml.dump(configuration, default_flow_style=False)
    66      dump = dump.replace('#DISCOVERY_URL', url)
    67  
    68      with azure_user_data as outfile:
    69          try:
    70              outfile.write("#cloud-config\n\n" + dump)
    71          except (IOError, ValueError):
    72              print "There was an issue writing to file " + azure_user_data.name
    73              return 1
    74          else:
    75              print "Wrote file \"%s\" with url \"%s\"" %\
    76                  (azure_user_data.name, url)
    77  
    78  if __name__ == "__main__":
    79      sys.exit(main())