Skip to content

Instantly share code, notes, and snippets.

@niedbalski
Last active December 17, 2015 03:28
Show Gist options
  • Save niedbalski/b40216e1f055d646534f to your computer and use it in GitHub Desktop.
Save niedbalski/b40216e1f055d646534f to your computer and use it in GitHub Desktop.
vpc_id, vpc_subnet_id for image generation
diff -r ae783041933e nimbic_pkg/nimbic/amis/create_nimbic_amis.py
--- a/nimbic_pkg/nimbic/amis/create_nimbic_amis.py Thu Apr 04 14:10:08 2013 -0300
+++ b/nimbic_pkg/nimbic/amis/create_nimbic_amis.py Wed May 08 16:34:12 2013 -0300
@@ -181,6 +181,9 @@
else:
self.security_group_name = context.sec_group
+ self.sec_group_vpc_id = context.sec_group_vpc_id
+ self.sec_group_vpc_subnet_id = context.sec_group_vpc_subnet_id
+
# Now run all the config from our config file
config = self._run_plugins('before_config', config)
@@ -188,12 +191,19 @@
pprint(config)
return
- # Spin up the instance
- self.resv = self.context.conn.run_instances(
- self.ami,
- key_name=self.key_pair_name,
- security_groups=[self.security_group_name],
- instance_type=config['instance_type'])
+ if self.sec_group_vpc_id and self.sec_group_vpc_subnet_id:
+ self.resv = self.context.conn.run_instances(
+ self.ami,
+ key_name=self.key_pair_name,
+ subnet_id=self.sec_group_vpc_subnet_id,
+ security_group_ids=[self.sec_group_vpc_id],
+ instance_type=config['instance_type'])
+ else:
+ self.resv = self.context.conn.run_instances(
+ self.ami,
+ key_name=self.key_pair_name,
+ security_groups=[self.security_group_name],
+ instance_type=config['instance_type'])
self.instance = self.resv.instances[0]
block_until_running(self.instance, verbose=True)
@@ -220,13 +230,19 @@
config["copy_files"]["target_dir"])
# Set up environment for fabric's sudo command
- env.host_string = self.instance.public_dns_name
- env.hosts = [self.instance.public_dns_name]
env.user = environ.USER
env.key_filename = self.key_file_name
+ if self.sec_group_vpc_id:
+ host = self.instance.private_ip_address
+ else:
+ host = self.instance.public_dns_name
+
+ env.host_string = host
+ env.hosts = [host]
+
# Must wait for instance to boot and be reachable by ssh
- wait_until_sudo(self.instance.public_dns_name, verbose=True)
+ wait_until_sudo(host, verbose=True)
sudo("mkdir -p " + environ.TARGET_DIR)
@@ -389,12 +405,15 @@
class ConnectionContext(object):
def __init__(self, creds, region_name, key_name,
- key_file, sec_group, debug):
+ key_file, sec_group, sec_group_vpc_id, sec_group_vpc_subnet_id,
+ debug):
self.creds = creds
self.region = region_name
self.key_name = key_name
self.key_file = key_file
self.sec_group = sec_group
+ self.sec_group_vpc_id = sec_group_vpc_id
+ self.sec_group_vpc_subnet_id = sec_group_vpc_subnet_id
self.refresh_connection()
self.active = True
self.debug = debug
@@ -418,17 +437,29 @@
if sec_group == None:
self.tmp_sec_name = random_name(15)
- self.conn.create_security_group(
- self.tmp_sec_name,
- 'Temporary security group')
- # Open up ssh port
- self.conn.authorize_security_group(
- self.tmp_sec_name,
+ args = {}
+ if self.sec_group_vpc_id:
+ args.update({'vpc_id': self.sec_group_vpc_id})
+
+ group = self.conn.create_security_group(
+ self.tmp_sec_name,
+ 'Temporary security group', **args)
+
+ if self.sec_group_vpc_id:
+ self.conn.authorize_security_group(group_id=group.id,
ip_protocol='tcp',
from_port=22,
to_port=22,
cidr_ip='0.0.0.0/0')
+ self.sec_group_vpc_id = group.id
+ else:
+ self.conn.authorize_security_group(self.tmp_sec_name,
+ ip_protocol='tcp',
+ from_port=22,
+ to_port=22,
+ cidr_ip='0.0.0.0/0')
+
else:
# Check to see if provided security group actually exists
group_list = self.conn.get_all_security_groups([sec_group])
@@ -466,6 +497,8 @@
key_name,
key_file,
sec_group,
+ sec_group_vpc_id,
+ sec_group_vpc_subnet_id,
tag_name,
account,
out_specs,
@@ -491,6 +524,8 @@
key_name,
key_file,
sec_group,
+ sec_group_vpc_id,
+ sec_group_vpc_subnet_id,
debug)
# collect all the current ami names for checking
nimbic_images = {}
@@ -568,9 +603,19 @@
help='Use the specified security group instead of auto generated one.',
default=None)
+ parser.add_option("-v", '--sec_group_vpc_id', dest="sec_group_vpc_id",
+ help='Specify the VPC ID of the security group',
+ default=None)
+
+ parser.add_option("-u", '--sec_group_vpc_subnet_id',
+ dest="sec_group_vpc_subnet_id",
+ help='Specify VPC subnet ID',
+ default=None)
+
parser.add_option("-t", '--tag_name', dest="tag_name",
help='Append this tag name to the created ami name for disambiguation.',
default=None)
+
parser.add_option("--dry-run", dest="dryrun",
help="Pretend that you are going to create the ami",
default=False)
@@ -623,6 +668,8 @@
key_name,
options.key_file,
options.sec_group,
+ options.sec_group_vpc_id,
+ options.sec_group_vpc_subnet_id,
options.tag_name,
options.account,
options.outfile,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment