Example #1
0
    def run(self, section=None, **kwargs):
        conf = env.config_object
        conn = get_ec2_connection(server_type='ec2', **kwargs)

        if section:
            sections = [section]
        else:
            sections = conf.server_sections()

        for section in sections:

            open_ports = conf.get_list(section, conf.OPEN_PORTS)
            restricted_ports = conf.get_list(section, conf.RESTRICTED_PORTS)

            if (not open_ports and not restricted_ports
                    or section == 'load-balancer'):
                continue

            host_sg = get_security_group(conn, section)
            if open_ports:
                for port in open_ports:
                    try:
                        host_sg.authorize('tcp', port, port, '0.0.0.0/0')
                    except:
                        pass

            if restricted_ports:
                for s in conf.get_list(section, conf.ALLOWED_SECTIONS):
                    if s == 'load-balancer':
                        guest_sg = self._get_lb_sg(**kwargs)
                    else:
                        guest_sg = get_security_group(conn, s)

                    if guest_sg:
                        for port in restricted_ports:
                            try:
                                if s == 'load-balancer':
                                    conn.authorize_security_group(
                                        host_sg.name,
                                        src_security_group_name='amazon-elb-sg',
                                        src_security_group_owner_id=
                                        'amazon-elb',
                                        from_port=port,
                                        to_port=port,
                                        ip_protocol='tcp')
                                else:
                                    host_sg.authorize('tcp',
                                                      port,
                                                      port,
                                                      src_group=guest_sg)
                            except:
                                pass
Example #2
0
    def run(self, section=None, **kwargs):
        conf = env.config_object
        conn = get_ec2_connection(server_type='ec2', **kwargs)

        if section:
            sections = [section]
        else:
            sections = conf.server_sections()

        for section in sections:

            open_ports = conf.get_list(section, conf.OPEN_PORTS)
            restricted_ports = conf.get_list(section, conf.RESTRICTED_PORTS)

            if (not open_ports and not restricted_ports
                or section == 'load-balancer'):
                continue

            host_sg = get_security_group(conn, section)
            if open_ports:
                for port in open_ports:
                    try:
                        host_sg.authorize('tcp', port, port, '0.0.0.0/0')
                    except:
                        pass

            if restricted_ports:
                for s in conf.get_list(section, conf.ALLOWED_SECTIONS):
                    if s == 'load-balancer':
                        guest_sg = self._get_lb_sg(**kwargs)
                    else:
                        guest_sg = get_security_group(conn, s)

                    if guest_sg:
                        for port in restricted_ports:
                            try:
                                if s == 'load-balancer':
                                    conn.authorize_security_group(host_sg.name,
                                          src_security_group_name='amazon-elb-sg',
                                          src_security_group_owner_id='amazon-elb',
                                          from_port=port, to_port=port,
                                          ip_protocol='tcp')
                                else:
                                    host_sg.authorize('tcp', port, port,
                                                      src_group=guest_sg)
                            except:
                                pass
Example #3
0
 def get_security_group(self, section):
     if not section in self._groups:
         if section == 'load-balancer':
             self._groups[section] = self._get_lb_sg()
         else:
             conn = get_ec2_connection(server_type='ec2')
             self._groups[section] = get_security_group(conn, section)
     return self._groups[section]
Example #4
0
 def get_security_group(self, section):
     if not section in self._groups:
         if section == 'load-balancer':
             self._groups[section] = self._get_lb_sg()
         else:
             conn = get_ec2_connection(server_type='ec2')
             self._groups[section] = get_security_group(conn, section)
     return self._groups[section]
def create_instance():
    utils.clear_screen()
    # Get instance info from the user
    instance_name = input("Enter the name of your instance: ")
    key_path = make_key_read_only(utils.get_valid_key("Enter path to your private key: "))
    key_name = utils.get_file_name_from_path(key_path)

    try:
        instance = ec2.create_instances(
            ImageId='ami-acd005d5',
            MinCount=1,
            MaxCount=1,
            InstanceType='t2.micro',
            KeyName=key_name,  # Name of the key to enable ssh
            TagSpecifications=[
                {
                    'ResourceType': 'instance',
                    'Tags': [
                        {
                            'Key': 'Name',
                            'Value': instance_name
                        },
                    ]
                },
            ],
            SecurityGroupIds=[
                utils.get_security_group(),  # call util method to create or get security group id
            ],
            UserData='''#!/bin/bash
                        yum -y update
                        yum install -y python35
                        yum install -y nginx'''
        )
        created_instance = instance[0]
        utils.print_and_log('Created instance Id: ' + created_instance.id)
        instance_public_ip = wait_till_public_ip(created_instance)  # store the instance public ip

        # Ssh related
        check_ssh(instance_public_ip, key_path)
        copy_check_webserver(instance_public_ip, key_path)
    except Exception as error:
        utils.print_and_log('Instance creation failed - exiting')
        utils.print_and_log(error)
Example #6
0
    def run(self, **kwargs):
        assert not env.hosts
        conn = get_ec2_connection(server_type='ec2', **kwargs)

        type = kwargs.get('type')
        setup_name = 'setup.%s' % type

        instance_type = DEFAULT_INSTANCE_TYPE

        ami_id = kwargs.get('ami_id')
        if not ami_id:
            ami_id = DEFAULT_AMI

        task = functions.get_task_instance(setup_name)
        if task:
            if hasattr(task, 'instance_type'):
                instance_type = task.instance_type
            if hasattr(task, 'ami'):
                ami_id = task.ami
        else:
            print "I don't know how to add a %s server" % type
            sys.exit(1)

        amzn = env.get('AWS_CREDENTIAL',
                       os.path.join(env.deploy_path, 'amazon.ini'))
        parser = ConfigParser()
        parser.read(amzn)
        key_name = parser.get('amazon-aws', 'ec2-key-name')
        key_file = parser.get('amazon-aws', 'ec2-key-file')

        if not key_name:
            print "Sorry. You need to create key pair with create_key first."
            sys.exit(1)
        elif not os.path.exists(key_file):
            print(
                "I find key %s in server.ini file, but the key file is not"
                " on its location %s. There is something wrong. Please fix "
                "it, or recreate key pair" % (key_name, key_file))
            sys.exit(1)

        image = conn.get_image(ami_id)
        security_group = get_security_group(conn, task.config_section)

        name = functions.get_remote_name(None,
                                         task.config_section,
                                         name=kwargs.get('name'))
        SERVER = {
            'image_id': image.id,
            'instance_type': instance_type,
            'security_groups': [security_group],
            'key_name': key_name,
        }

        reservation = conn.run_instances(**SERVER)
        print reservation

        instance = reservation.instances[0]
        while instance.state != 'running':
            time.sleep(5)
            instance.update()
            print "...instance state: %s" % (instance.state)

        conn.create_tags([instance.id], {"Name": name})

        if not kwargs.get('static_ip', False):
            ip = instance.ip_address
        else:
            elastic_ip = conn.allocate_address()
            print "...Elastic IP %s allocated" % elastic_ip
            elastic_ip.associate(instance.id)
            ip = elastic_ip.public_ip

        print "...EC2 instance is successfully created."
        print "...wait 5 seconds for the server to be ready"
        print "...while waiting, you may want to note down the following info"
        time.sleep(5)
        print "..."
        print "...Instance using image: %s" % image.name
        print "...Added into security group: %s" % security_group.name
        print "...Instance ID: %s" % instance.id
        print "...Public IP: %s" % ip

        host_string = 'ubuntu@%s' % instance.public_dns_name
        execute(setup_name, name=name, hosts=[host_string])
Example #7
0
    def run(self, **kwargs):
        assert not env.hosts
        conn = get_ec2_connection(server_type='ec2', **kwargs)

        type = kwargs.get('type')
        setup_name = 'servers.%s.setup' % type
        config_name = 'servers.%s.api_config' % kwargs.get('type')

        instance_type = DEFAULT_INSTANCE_TYPE

        ami_id = kwargs.get('ami_id')
        if not ami_id:
            ami_id = DEFAULT_AMI
        user = kwargs.get('user', 'ubuntu')

        task = functions.get_task_instance(setup_name)
        if task:
            results = execute(config_name, hosts=['fake'])['fake']
            config_section = results['config_section']
            if 'instance_type' in results:
                instance_type = results['instance_type']
            if 'ami' in results:
                ami_id = results['ami']
            if 'user' in results:
                user = results['user']
        else:
            print "I don't know how to add a %s server" % type
            sys.exit(1)

        assert config_section
        amzn = env.get('AWS_CREDENTIAL',
                       os.path.join(env.deploy_path, 'amazon.ini'))

        parser = ConfigParser()
        parser.read(amzn)
        key_name = parser.get('amazon-aws', 'ec2-key-name')
        key_file = parser.get('amazon-aws', 'ec2-key-file')

        if not key_name:
            print "Sorry. You need to create key pair with create_key first."
            sys.exit(1)
        elif not os.path.exists(key_file):
            print ("I find key %s in server.ini file, but the key file is not"
                   " on its location %s. There is something wrong. Please fix "
                   "it, or recreate key pair" % (key_name, key_file))
            sys.exit(1)

        image = conn.get_image(ami_id)
        security_group = get_security_group(conn, config_section)

        name = functions.get_remote_name(None, config_section,
                                         name=kwargs.get('name'))
        SERVER = {
            'image_id':         image.id,
            'instance_type':    instance_type,
            'security_groups':  [security_group],
            'key_name':         key_name,}

        reservation = conn.run_instances(**SERVER)
        print reservation

        instance = reservation.instances[0]
        while instance.state != 'running':
            time.sleep(5)
            instance.update()
            print "...instance state: %s" % (instance.state)

        conn.create_tags([instance.id], {"Name": name})

        if not kwargs.get('static_ip', False):
            ip = instance.ip_address
        else:
            elastic_ip = conn.allocate_address()
            print "...Elastic IP %s allocated" % elastic_ip
            elastic_ip.associate(instance.id)
            ip = elastic_ip.public_ip

        print "...EC2 instance is successfully created."
        print "...wait 5 seconds for the server to be ready"
        print "...while waiting, you may want to note down the following info"
        time.sleep(5)
        print "..."
        print "...Instance using image: %s" % image.name
        print "...Added into security group: %s" %security_group.name
        print "...Instance ID: %s" % instance.id
        print "...Public IP: %s" % ip

        host_string = '{0}@{1}'.format(user, instance.public_dns_name)
        execute(setup_name, hosts=[host_string])