github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/contrib/aws/gen-json.py (about) 1 #!/usr/bin/env python 2 import argparse 3 import json 4 import os 5 import urllib 6 import yaml 7 8 parser = argparse.ArgumentParser() 9 parser.add_argument('--channel', help='the CoreOS channel to use', default='stable') 10 parser.add_argument('--version', help='the CoreOS version to use', default='current') 11 args = vars(parser.parse_args()) 12 13 url = "http://{channel}.release.core-os.net/amd64-usr/{version}/coreos_production_ami_all.json".format(**args) 14 try: 15 amis = json.load(urllib.urlopen(url)) 16 except (IOError, ValueError): 17 print "The URL {} is invalid.".format(url) 18 raise 19 20 CURR_DIR = os.path.dirname(os.path.realpath(__file__)) 21 22 # Add AWS-specific units to the shared user-data 23 FORMAT_DOCKER_VOLUME = ''' 24 [Unit] 25 Description=Formats the added EBS volume for Docker 26 ConditionPathExists=!/etc/docker-volume-formatted 27 [Service] 28 Type=oneshot 29 RemainAfterExit=yes 30 ExecStart=/usr/sbin/wipefs -f /dev/xvdf 31 ExecStart=/usr/sbin/mkfs.ext4 -i 4096 -b 4096 /dev/xvdf 32 ExecStart=/bin/touch /etc/docker-volume-formatted 33 ''' 34 MOUNT_DOCKER_VOLUME = ''' 35 [Unit] 36 Description=Mount Docker volume to /var/lib/docker 37 Requires=format-docker-volume.service 38 After=format-docker-volume.service 39 Before=docker.service 40 [Mount] 41 What=/dev/xvdf 42 Where=/var/lib/docker 43 Type=ext4 44 ''' 45 DOCKER_DROPIN = ''' 46 [Unit] 47 Requires=var-lib-docker.mount 48 After=var-lib-docker.mount 49 ''' 50 FORMAT_ETCD_VOLUME = ''' 51 [Unit] 52 Description=Formats the etcd device 53 ConditionPathExists=!/etc/etcd-volume-formatted 54 [Service] 55 Type=oneshot 56 RemainAfterExit=yes 57 ExecStart=/usr/sbin/wipefs -f /dev/xvdg 58 ExecStart=/usr/sbin/mkfs.ext4 -i 4096 -b 4096 /dev/xvdg 59 ExecStart=/bin/touch /etc/etcd-volume-formatted 60 ''' 61 MOUNT_ETCD_VOLUME = ''' 62 [Unit] 63 Description=Mounts the etcd volume 64 Requires=format-etcd-volume.service 65 After=format-etcd-volume.service 66 [Mount] 67 What=/dev/xvdg 68 Where=/media/etcd 69 Type=ext4 70 ''' 71 PREPARE_ETCD_DATA_DIRECTORY = ''' 72 [Unit] 73 Description=Prepares the etcd data directory 74 Requires=media-etcd.mount 75 After=media-etcd.mount 76 Before=etcd2.service 77 [Service] 78 Type=oneshot 79 RemainAfterExit=yes 80 ExecStart=/usr/bin/chown -R etcd:etcd /media/etcd 81 ''' 82 ETCD_DROPIN = ''' 83 [Unit] 84 Requires=prepare-etcd-data-directory.service 85 After=prepare-etcd-data-directory.service 86 ''' 87 88 new_units = [ 89 dict({'name': 'format-docker-volume.service', 'command': 'start', 'content': FORMAT_DOCKER_VOLUME}), 90 dict({'name': 'var-lib-docker.mount', 'command': 'start', 'content': MOUNT_DOCKER_VOLUME}), 91 dict({'name': 'docker.service', 'drop-ins': [{'name': '90-after-docker-volume.conf', 'content': DOCKER_DROPIN}]}), 92 dict({'name': 'format-etcd-volume.service', 'command': 'start', 'content': FORMAT_ETCD_VOLUME}), 93 dict({'name': 'media-etcd.mount', 'command': 'start', 'content': MOUNT_ETCD_VOLUME}), 94 dict({'name': 'prepare-etcd-data-directory.service', 'command': 'start', 'content': PREPARE_ETCD_DATA_DIRECTORY}), 95 dict({'name': 'etcd2.service', 'drop-ins': [{'name': '90-after-etcd-volume.conf', 'content': ETCD_DROPIN}]}) 96 ] 97 98 with open(os.path.join(CURR_DIR, '..', 'coreos', 'user-data'), 'r') as f: 99 data = yaml.safe_load(f) 100 101 # coreos-cloudinit will start the units in order, so we want these to be processed before etcd/fleet 102 # are started 103 data['coreos']['units'] = new_units + data['coreos']['units'] 104 105 header = ["#cloud-config", "---"] 106 dump = yaml.dump(data, default_flow_style=False) 107 108 # configure etcd to use its EBS volume 109 dump = dump.replace('ETCD_HOST_DATA_DIR=/var/lib/etcd2', 'ETCD_HOST_DATA_DIR=/media/etcd') 110 111 template = json.load(open(os.path.join(CURR_DIR, 'deis.template.json'), 'r')) 112 113 template['Resources']['CoreOSServerLaunchConfig']['Properties']['UserData']['Fn::Base64']['Fn::Join'] = ["\n", header + dump.split("\n")] 114 template['Parameters']['ClusterSize']['Default'] = str(os.getenv('DEIS_NUM_INSTANCES', 3)) 115 template['Mappings']['CoreOSAMIs'] = dict(map(lambda n: (n['name'], dict(PV=n['pv'], HVM=n['hvm'])), amis['amis'])) 116 117 VPC_ID = os.getenv('VPC_ID', None) 118 VPC_SUBNETS = os.getenv('VPC_SUBNETS', None) 119 VPC_PRIVATE_SUBNETS = os.getenv('VPC_PRIVATE_SUBNETS', VPC_SUBNETS) 120 VPC_ZONES = os.getenv('VPC_ZONES', None) 121 122 if VPC_ID and VPC_SUBNETS and VPC_ZONES and len(VPC_SUBNETS.split(',')) == len(VPC_ZONES.split(',')): 123 # skip VPC, subnet, route, and internet gateway creation 124 del template['Resources']['VPC'] 125 del template['Resources']['Subnet1'] 126 del template['Resources']['Subnet2'] 127 del template['Resources']['Subnet1RouteTableAssociation'] 128 del template['Resources']['Subnet2RouteTableAssociation'] 129 del template['Resources']['InternetGateway'] 130 del template['Resources']['GatewayToInternet'] 131 del template['Resources']['PublicRouteTable'] 132 del template['Resources']['PublicRoute'] 133 del template['Resources']['CoreOSServerLaunchConfig']['DependsOn'] 134 del template['Resources']['DeisWebELB']['DependsOn'] 135 136 # update VpcId fields 137 template['Resources']['DeisWebELBSecurityGroup']['Properties']['VpcId'] = VPC_ID 138 template['Resources']['VPCSecurityGroup']['Properties']['VpcId'] = VPC_ID 139 140 # update subnets and zones 141 template['Resources']['CoreOSServerAutoScale']['Properties']['AvailabilityZones'] = VPC_ZONES.split(',') 142 template['Resources']['CoreOSServerAutoScale']['Properties']['VPCZoneIdentifier'] = VPC_PRIVATE_SUBNETS.split(',') 143 template['Resources']['DeisWebELB']['Properties']['Subnets'] = VPC_SUBNETS.split(',') 144 145 print json.dumps(template)