def check_create_aws_sec_group(conn): """ Check whether the NGAS security group exists """ default_if_empty(env, 'AWS_SEC_GROUP', DEFAULT_AWS_SEC_GROUP) ngas_secgroup = env.AWS_SEC_GROUP sec = conn.get_all_security_groups() conn.close() for sg in sec: if sg.name.upper() == ngas_secgroup: puts( green("AWS Security Group {0} exists ({1})".format( ngas_secgroup, sg.id))) return sg.id # Not found, create a new one ngassg = conn.create_security_group(ngas_secgroup, 'NGAS default permissions') ngassg.authorize('tcp', 22, 22, '0.0.0.0/0') ngassg.authorize('tcp', 80, 80, '0.0.0.0/0') ngassg.authorize('tcp', 5678, 5678, '0.0.0.0/0') ngassg.authorize('tcp', 7777, 7777, '0.0.0.0/0') return ngassg.id
def check_create_aws_sec_group(conn): """ Check whether the APP security group exists """ default_if_empty(env, 'AWS_SEC_GROUP', DEFAULT_AWS_SEC_GROUP) app_secgroup = env.AWS_SEC_GROUP sec = conn.get_all_security_groups() conn.close() appsg = None for sg in sec: if sg.name.upper() == app_secgroup: puts(green("AWS Security Group {0} exists ({1})". format(app_secgroup, sg.id))) appsg = sg # Not found, create a new one if appsg is None: appsg = conn.create_security_group(app_secgroup, '{0} default permissions'. format(APP_name)) for p in DEFAULT_PORTS.values(): appsg.authorize('tcp', p, p, '0.0.0.0/0') appsg.authorize('tcp', 22, 22, '0.0.0.0/0') appsg.authorize('tcp', 80, 80, '0.0.0.0/0') return appsg.id
def create_aws_instances(): """ Create AWS instances and let Fabric point to them This method creates AWS instances and points the fabric environment to them with the current public IP and username. """ default_if_empty(env, 'AWS_KEY_NAME', DEFAULT_AWS_KEY_NAME) default_if_empty(env, 'AWS_INSTANCE_NAME', default_instance_name) # Create the key pair and security group if necessary conn = connect() aws_create_key_pair(conn) sgid = check_create_aws_sec_group(conn) # Create the instance in AWS host_names = create_instances(conn, sgid) # Update our fabric environment so from now on we connect to the # AWS machine using the correct user and SSH private key env.hosts = host_names env.key_filename = key_filename(env.AWS_KEY_NAME) if env.AWS_AMI_NAME in ['CentOS', 'SLES']: env.user = '******' else: env.user = '******' # Instances have started, but are not usable yet, make sure SSH has started puts('Started the instance(s) now waiting for the SSH daemon to start.') execute(check_ssh, timeout=300)
def ngas_user(): default_if_empty(env, 'NGAS_USER', NGAS_USER) return env.NGAS_USER
def ngas_revision(): default_if_empty(env, 'NGAS_REV', default_ngas_revision) return env.NGAS_REV
def ngas_server_type(): default_if_empty(env, 'NGAS_SERVER_TYPE', NGAS_SERVER_TYPE) return env.NGAS_SERVER_TYPE
def connect(): import boto.ec2 default_if_empty(env, 'AWS_PROFILE', DEFAULT_AWS_PROFILE) default_if_empty(env, 'AWS_REGION', DEFAULT_AWS_REGION) return boto.ec2.connect_to_region(env.AWS_REGION, profile_name=env.AWS_PROFILE)
def create_instances(conn, sgid): """ Create one or more EC2 instances """ default_if_empty(env, 'AWS_AMI_NAME', DEFAULT_AWS_AMI_NAME) default_if_empty(env, 'AWS_INSTANCE_TYPE', DEFAULT_AWS_INSTANCE_TYPE) default_if_empty(env, 'AWS_INSTANCES', DEFAULT_AWS_INSTANCES) n_instances = int(env.AWS_INSTANCES) if n_instances > 1: names = ["%s_%d" % (env.AWS_INSTANCE_NAME, i) for i in xrange(n_instances)] else: names = [env.AWS_INSTANCE_NAME] puts('Creating instances {0}'.format(names)) public_ips = None if 'AWS_ELASTIC_IPS' in env: public_ips = env.AWS_ELASTIC_IPS.split(',') if len(public_ips) != n_instances: abort("n_instances != #AWS_ELASTIC_IPS (%d != %d)" % (n_instances, len(public_ips))) # Disassociate the public IPs for public_ip in public_ips: if not conn.disassociate_address(public_ip=public_ip): abort('Could not disassociate the IP {0}'.format(public_ip)) reservations = conn.run_instances(AMI_IDs[env.AWS_AMI_NAME], instance_type=env.AWS_INSTANCE_TYPE, key_name=env.AWS_KEY_NAME, security_group_ids=[sgid], min_count=n_instances, max_count=n_instances) instances = reservations.instances # Sleep so Amazon recognizes the new instance for i in range(4): fastprint('.') time.sleep(5) # Are we running yet? iid = [x.id for x in instances] stat = conn.get_all_instance_status(iid) running = [x.state_name == 'running' for x in stat] puts('\nWaiting for instances to be fully available:\n') while sum(running) != n_instances: fastprint('.') time.sleep(5) stat = conn.get_all_instance_status(iid) running = [x.state_name == 'running' for x in stat] puts('.') # enforce the line-end # Local user and host userAThost = userAtHost() # We save the user under which we install APP for later display nuser = APP_user() # Tag the instance for name, instance in zip(names, instances): conn.create_tags([instance.id], {'Name': name, 'Created By': userAThost, 'APP User': nuser, }) # Associate the IP if needed if public_ips: for instance, public_ip in zip(instances, public_ips): puts('Current DNS name is {0}. About to associate the Elastic IP'. format(instance.dns_name)) if not conn.associate_address(instance_id=instance.id, public_ip=public_ip): abort('Could not associate the IP {0} to the instance {1}'. format(public_ip, instance.id)) # Load the new instance data as the dns_name may have changed host_names = [] for instance in instances: instance.update(True) print_instance(instance) host_names.append(str(instance.dns_name)) return host_names
def APP_user(): default_if_empty(env, 'APP_USER', APP_USER) return env.APP_USER
def APP_name(): default_if_empty(env, 'APP_NAME', APP) return env.APP_NAME
def APP_revision(): default_if_empty(env, 'APP_REV', default_APP_revision) return env.APP_REV
def docker_image_repository(): default_if_empty(env, 'DOCKER_IMAGE_REPOSITORY', 'icrar/APP') return env.DOCKER_IMAGE_REPOSITORY