github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/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")
    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      configuration["coreos"]["etcd"]["discovery_url"] = url
    66  
    67      with gce_user_data as outfile:
    68          try:
    69              outfile.write("#cloud-config\n\n" +
    70                            yaml.safe_dump(configuration,
    71                                           default_flow_style=False,
    72                                           default_style='|'))
    73          except (IOError, ValueError):
    74              print "There was an issue writing to file " + gce_user_data.name
    75              return 1
    76          else:
    77              print "Wrote file \"%s\" with url \"%s\"" %\
    78                  (gce_user_data.name, url)
    79  
    80  if __name__ == "__main__":
    81      sys.exit(main())