github.com/arkadijs/deis@v1.5.1/contrib/ec2/gen-json.py (about)

     1  #!/usr/bin/env python
     2  import json
     3  import os
     4  import yaml
     5  
     6  CURR_DIR = os.path.dirname(os.path.realpath(__file__))
     7  
     8  # Add EC2-specific units to the shared user-data
     9  FORMAT_EPHEMERAL_VOLUME = '''
    10    [Unit]
    11    Description=Formats the ephemeral volume
    12    ConditionPathExists=!/etc/ephemeral-volume-formatted
    13    [Service]
    14    Type=oneshot
    15    RemainAfterExit=yes
    16    ExecStart=/usr/sbin/wipefs -f /dev/xvdb
    17    ExecStart=/usr/sbin/mkfs.ext4 -i 4096 -b 4096 /dev/xvdb
    18    ExecStart=/bin/touch /etc/ephemeral-volume-formatted
    19  '''
    20  MOUNT_EPHEMERAL_VOLUME = '''
    21    [Unit]
    22    Description=Formats and mounts the ephemeral drive
    23    Requires=format-ephemeral-volume.service
    24    After=format-ephemeral-volume.service
    25    [Mount]
    26    What=/dev/xvdb
    27    Where=/media/ephemeral
    28    Type=ext4
    29  '''
    30  PREPARE_ETCD_DATA_DIRECTORY = '''
    31    [Unit]
    32    Description=Prepares the etcd data directory
    33    Requires=media-ephemeral.mount
    34    After=media-ephemeral.mount
    35    Before=etcd.service
    36    [Service]
    37    Type=oneshot
    38    RemainAfterExit=yes
    39    ExecStart=/usr/bin/mkdir -p /media/ephemeral/etcd
    40    ExecStart=/usr/bin/chown -R etcd:etcd /media/ephemeral/etcd
    41  '''
    42  FORMAT_DOCKER_VOLUME = '''
    43    [Unit]
    44    Description=Formats the added EBS volume for Docker
    45    ConditionPathExists=!/etc/docker-volume-formatted
    46    [Service]
    47    Type=oneshot
    48    RemainAfterExit=yes
    49    ExecStart=/usr/sbin/wipefs -f /dev/xvdf
    50    ExecStart=/usr/sbin/mkfs.ext4 -i 4096 -b 4096 /dev/xvdf
    51    ExecStart=/bin/touch /etc/docker-volume-formatted
    52  '''
    53  MOUNT_DOCKER_VOLUME = '''
    54    [Unit]
    55    Description=Mount Docker volume to /var/lib/docker
    56    Requires=format-docker-volume.service
    57    After=format-docker-volume.service
    58    Before=docker.service
    59    [Mount]
    60    What=/dev/xvdf
    61    Where=/var/lib/docker
    62    Type=ext4
    63  '''
    64  
    65  new_units = [
    66    dict({'name': 'format-ephemeral-volume.service', 'command': 'start', 'content': FORMAT_EPHEMERAL_VOLUME}),
    67    dict({'name': 'media-ephemeral.mount', 'command': 'start', 'content': MOUNT_EPHEMERAL_VOLUME}),
    68    dict({'name': 'prepare-etcd-data-directory.service', 'command': 'start', 'content': PREPARE_ETCD_DATA_DIRECTORY}),
    69    dict({'name': 'format-docker-volume.service', 'command': 'start', 'content': FORMAT_DOCKER_VOLUME}),
    70    dict({'name': 'var-lib-docker.mount', 'command': 'start', 'content': MOUNT_DOCKER_VOLUME})
    71  ]
    72  
    73  data = yaml.load(file(os.path.join(CURR_DIR, '..', 'coreos', 'user-data'), 'r'))
    74  
    75  # coreos-cloudinit will start the units in order, so we want these to be processed before etcd/fleet
    76  # are started
    77  data['coreos']['units'] = new_units + data['coreos']['units']
    78  
    79  # configure etcd to use the ephemeral drive
    80  data['coreos']['etcd']['data-dir'] = '/media/ephemeral/etcd'
    81  
    82  header = ["#cloud-config", "---"]
    83  dump = yaml.dump(data, default_flow_style=False)
    84  
    85  template = json.load(open(os.path.join(CURR_DIR, 'deis.template.json'),'r'))
    86  
    87  template['Resources']['CoreOSServerLaunchConfig']['Properties']['UserData']['Fn::Base64']['Fn::Join'] = [ "\n", header + dump.split("\n") ]
    88  template['Parameters']['ClusterSize']['Default'] = str(os.getenv('DEIS_NUM_INSTANCES', 3))
    89  
    90  VPC_ID = os.getenv('VPC_ID', None)
    91  VPC_SUBNETS = os.getenv('VPC_SUBNETS', None)
    92  VPC_PRIVATE_SUBNETS = os.getenv('VPC_PRIVATE_SUBNETS', VPC_SUBNETS)
    93  VPC_ZONES = os.getenv('VPC_ZONES', None)
    94  
    95  if VPC_ID and VPC_SUBNETS and VPC_ZONES and len(VPC_SUBNETS.split(',')) == len(VPC_ZONES.split(',')):
    96    # skip VPC, subnet, route, and internet gateway creation
    97    del template['Resources']['VPC']
    98    del template['Resources']['Subnet1']
    99    del template['Resources']['Subnet2']
   100    del template['Resources']['Subnet1RouteTableAssociation']
   101    del template['Resources']['Subnet2RouteTableAssociation']
   102    del template['Resources']['InternetGateway']
   103    del template['Resources']['GatewayToInternet']
   104    del template['Resources']['PublicRouteTable']
   105    del template['Resources']['PublicRoute']
   106    del template['Resources']['CoreOSServerLaunchConfig']['DependsOn']
   107    del template['Resources']['DeisWebELB']['DependsOn']
   108  
   109    # update VpcId fields
   110    template['Resources']['DeisWebELBSecurityGroup']['Properties']['VpcId'] = VPC_ID
   111    template['Resources']['VPCSecurityGroup']['Properties']['VpcId'] = VPC_ID
   112  
   113    # update subnets and zones
   114    template['Resources']['CoreOSServerAutoScale']['Properties']['AvailabilityZones'] = VPC_ZONES.split(',')
   115    template['Resources']['CoreOSServerAutoScale']['Properties']['VPCZoneIdentifier'] = VPC_PRIVATE_SUBNETS.split(',')
   116    template['Resources']['DeisWebELB']['Properties']['Subnets'] = VPC_SUBNETS.split(',')
   117  
   118  print json.dumps(template)