github.com/naphatkrit/deis@v1.12.3/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      coreos_template_w_url = None
    60      with open(coreos_template.name, 'r') as file:
    61          coreos_template_w_url = file.read()
    62  
    63      coreos_template_w_url = coreos_template_w_url.replace('#DISCOVERY_URL', url)
    64  
    65      configuration_coreos_template = yaml.safe_load(coreos_template_w_url)
    66      configuration_gce_template = yaml.safe_load(gce_template)
    67  
    68      configuration = combine_dicts(configuration_coreos_template,
    69                                    configuration_gce_template)
    70  
    71      dump = yaml.dump(configuration, default_flow_style=False,
    72                       default_style='|')
    73      dump = dump.replace('#DISCOVERY_URL', url)
    74  
    75      with gce_user_data as outfile:
    76          try:
    77              outfile.write("#cloud-config\n\n" + dump)
    78          except (IOError, ValueError):
    79              print "There was an issue writing to file " + gce_user_data.name
    80              return 1
    81          else:
    82              print "Wrote file \"%s\" with url \"%s\"" %\
    83                  (gce_user_data.name, url)
    84  
    85  if __name__ == "__main__":
    86      sys.exit(main())