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

     1  #!/usr/bin/env python
     2  """
     3  Create CoreOS user-data by merging contric/coreos/user-data and gce-user-data
     4  
     5  Usage: create-gce-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] = (orig_dict[key] + val)
    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      gce_user_data = get_file("gce-user-data", "w", True)
    56      gce_template = get_file("gce-user-data-template")
    57      coreos_template = get_file("../coreos/user-data.example")
    58  
    59      configuration_coreos_template = yaml.safe_load(coreos_template)
    60      configuration_gce_template = yaml.safe_load(gce_template)
    61  
    62      configuration = combine_dicts(configuration_coreos_template,
    63                                    configuration_gce_template)
    64  
    65      dump = yaml.dump(configuration, default_flow_style=False,
    66                       default_style='|')
    67      dump = dump.replace('#DISCOVERY_URL', url)
    68  
    69      with gce_user_data as outfile:
    70          try:
    71              outfile.write("#cloud-config\n\n" + dump)
    72          except (IOError, ValueError):
    73              print "There was an issue writing to file " + gce_user_data.name
    74              return 1
    75          else:
    76              print "Wrote file \"%s\" with url \"%s\"" %\
    77                  (gce_user_data.name, url)
    78  
    79  if __name__ == "__main__":
    80      sys.exit(main())