mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
use utils.boto in asg, and ec2
This commit is contained in:
parent
00f170bfb4
commit
0249e84d37
@ -33,11 +33,14 @@ Connection module for Amazon Autoscale Groups
|
||||
|
||||
:depends: boto
|
||||
'''
|
||||
# keep lint from choking on _get_conn and _cache_id
|
||||
#pylint disable=F821
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import
|
||||
import logging
|
||||
import json
|
||||
import sys
|
||||
import email.mime.multipart
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -66,6 +69,10 @@ def __virtual__():
|
||||
'''
|
||||
if not HAS_BOTO:
|
||||
return False
|
||||
|
||||
__utils__['boto.assign_funcs'](__name__, 'asg', module='ec2.autoscale')
|
||||
setattr(sys.modules[__name__], '_get_ec2_conn',
|
||||
__utils__['boto.get_connection_func']('ec2'))
|
||||
return True
|
||||
|
||||
|
||||
@ -77,9 +84,7 @@ def exists(name, region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
salt myminion boto_asg.exists myasg region=us-east-1
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
_conn = conn.get_all_groups(names=[name])
|
||||
if _conn:
|
||||
@ -101,9 +106,7 @@ def get_config(name, region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
salt myminion boto_asg.get_config myasg region=us-east-1
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return None
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
asg = conn.get_all_groups(names=[name])
|
||||
if asg:
|
||||
@ -173,9 +176,7 @@ def create(name, launch_config_name, availability_zones, min_size, max_size,
|
||||
|
||||
salt myminion boto_asg.create myasg mylc '["us-east-1a", "us-east-1e"]' 1 10 load_balancers='["myelb", "myelb2"]' tags='[{"key": "Name", value="myasg", "propagate_at_launch": True}]'
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
if isinstance(availability_zones, six.string_types):
|
||||
availability_zones = json.loads(availability_zones)
|
||||
if isinstance(load_balancers, six.string_types):
|
||||
@ -250,7 +251,7 @@ def update(name, launch_config_name, availability_zones, min_size, max_size,
|
||||
salt myminion boto_asg.update myasg mylc '["us-east-1a", "us-east-1e"]' 1 10 load_balancers='["myelb", "myelb2"]' tags='[{"key": "Name", value="myasg", "propagate_at_launch": True}]'
|
||||
'''
|
||||
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
if not conn:
|
||||
return False, "failed to connect to AWS"
|
||||
if isinstance(availability_zones, six.string_types):
|
||||
@ -346,9 +347,7 @@ def delete(name, force=False, region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
salt myminion boto_asg.delete myasg region=us-east-1
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
conn.delete_auto_scaling_group(name, force)
|
||||
msg = 'Deleted autoscale group {0}.'.format(name)
|
||||
@ -404,9 +403,7 @@ def launch_configuration_exists(name, region=None, key=None, keyid=None,
|
||||
|
||||
salt myminion boto_asg.launch_configuration_exists mylc
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
lc = conn.get_all_launch_configurations(names=[name])
|
||||
if lc:
|
||||
@ -439,9 +436,7 @@ def create_launch_configuration(name, image_id, key_name=None,
|
||||
|
||||
salt myminion boto_asg.create_launch_configuration mylc image_id=ami-0b9c9f62 key_name='mykey' security_groups='["mygroup"]' instance_type='c3.2xlarge'
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
if isinstance(security_groups, six.string_types):
|
||||
security_groups = json.loads(security_groups)
|
||||
if isinstance(block_device_mappings, six.string_types):
|
||||
@ -488,9 +483,7 @@ def delete_launch_configuration(name, region=None, key=None, keyid=None,
|
||||
|
||||
salt myminion boto_asg.delete_launch_configuration mylc
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
conn.delete_launch_configuration(name)
|
||||
log.info('Deleted LC {0}'.format(name))
|
||||
@ -513,7 +506,7 @@ def get_scaling_policy_arn(as_group, scaling_policy_name, region=None,
|
||||
|
||||
salt '*' boto_asg.get_scaling_policy_arn mygroup mypolicy
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
policies = conn.get_all_policies(as_group=as_group)
|
||||
for policy in policies:
|
||||
if policy.name == scaling_policy_name:
|
||||
@ -522,74 +515,6 @@ def get_scaling_policy_arn(as_group, scaling_policy_name, region=None,
|
||||
return None
|
||||
|
||||
|
||||
def _get_conn(region, key, keyid, profile):
|
||||
'''
|
||||
Get a boto connection to autoscale.
|
||||
'''
|
||||
if profile:
|
||||
if isinstance(profile, six.string_types):
|
||||
_profile = __salt__['config.option'](profile)
|
||||
elif isinstance(profile, dict):
|
||||
_profile = profile
|
||||
key = _profile.get('key', None)
|
||||
keyid = _profile.get('keyid', None)
|
||||
region = _profile.get('region', None)
|
||||
|
||||
if not region and __salt__['config.option']('asg.region'):
|
||||
region = __salt__['config.option']('asg.region')
|
||||
|
||||
if not region:
|
||||
region = 'us-east-1'
|
||||
|
||||
if not key and __salt__['config.option']('asg.key'):
|
||||
key = __salt__['config.option']('asg.key')
|
||||
if not keyid and __salt__['config.option']('asg.keyid'):
|
||||
keyid = __salt__['config.option']('asg.keyid')
|
||||
|
||||
try:
|
||||
conn = autoscale.connect_to_region(region, aws_access_key_id=keyid,
|
||||
aws_secret_access_key=key)
|
||||
except boto.exception.NoAuthHandlerFound:
|
||||
log.error('No authentication credentials found when attempting to'
|
||||
' make boto autoscale connection.')
|
||||
return None
|
||||
return conn
|
||||
|
||||
|
||||
def _get_ec2_conn(region, key, keyid, profile):
|
||||
'''
|
||||
Get a boto connection to ec2. Needed for get_instances
|
||||
'''
|
||||
if profile:
|
||||
if isinstance(profile, six.string_types):
|
||||
_profile = __salt__['config.option'](profile)
|
||||
elif isinstance(profile, dict):
|
||||
_profile = profile
|
||||
key = _profile.get('key', None)
|
||||
keyid = _profile.get('keyid', None)
|
||||
region = _profile.get('region', None)
|
||||
|
||||
if not region and __salt__['config.option']('secgroup.region'):
|
||||
region = __salt__['config.option']('secgroup.region')
|
||||
|
||||
if not region:
|
||||
region = 'us-east-1'
|
||||
|
||||
if not key and __salt__['config.option']('secgroup.key'):
|
||||
key = __salt__['config.option']('secgroup.key')
|
||||
if not keyid and __salt__['config.option']('secgroup.keyid'):
|
||||
keyid = __salt__['config.option']('secgroup.keyid')
|
||||
|
||||
try:
|
||||
conn = boto.ec2.connect_to_region(region, aws_access_key_id=keyid,
|
||||
aws_secret_access_key=key)
|
||||
except boto.exception.NoAuthHandlerFound:
|
||||
log.error('No authentication credentials found when attempting to'
|
||||
' make ec2 connection for security groups.')
|
||||
return None
|
||||
return conn
|
||||
|
||||
|
||||
def get_instances(name, lifecycle_state="InService", health_status="Healthy", attribute="private_ip_address", region=None, key=None, keyid=None, profile=None):
|
||||
"""return attribute of all instances in the named autoscale group.
|
||||
|
||||
@ -598,10 +523,8 @@ def get_instances(name, lifecycle_state="InService", health_status="Healthy", at
|
||||
salt-call boto_asg.get_instances my_autoscale_group_name
|
||||
|
||||
"""
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
ec2_conn = _get_ec2_conn(region, key, keyid, profile)
|
||||
if not conn or not ec2_conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
ec2_conn = _get_ec2_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
asgs = conn.get_all_groups(names=[name])
|
||||
except boto.exception.BotoServerError as e:
|
||||
|
@ -34,10 +34,11 @@ Connection module for Amazon EC2
|
||||
:depends: boto
|
||||
|
||||
'''
|
||||
# keep lint from choking on _get_conn and _cache_id
|
||||
#pylint disable=F821
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import
|
||||
import hashlib
|
||||
import logging
|
||||
import time
|
||||
from distutils.version import LooseVersion as _LooseVersion # pylint: disable=import-error,no-name-in-module
|
||||
@ -74,6 +75,7 @@ def __virtual__():
|
||||
elif _LooseVersion(boto.__version__) < _LooseVersion(required_boto_version):
|
||||
return False
|
||||
else:
|
||||
__utils__['boto.assign_funcs'](__name__, 'ec2')
|
||||
return True
|
||||
|
||||
|
||||
@ -86,9 +88,7 @@ def get_zones(region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
salt myminion boto_ec2.get_zones
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
return [z.name for z in conn.get_all_zones()]
|
||||
|
||||
@ -107,9 +107,7 @@ def find_instances(instance_id=None, name=None, tags=None, region=None,
|
||||
salt myminion boto_ec2.find_instances tags='{"mytag": "value"}'
|
||||
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
try:
|
||||
filter_parameters = {'filters': {}}
|
||||
@ -179,10 +177,6 @@ def get_id(name=None, tags=None, region=None, key=None,
|
||||
salt myminion boto_ec2.get_id myinstance
|
||||
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return None
|
||||
|
||||
instance_ids = find_instances(name=name, tags=tags, region=region, key=key,
|
||||
keyid=keyid, profile=profile)
|
||||
if instance_ids:
|
||||
@ -211,10 +205,6 @@ def exists(instance_id=None, name=None, tags=None, region=None, key=None,
|
||||
|
||||
salt myminion boto_ec2.exists myinstance
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
|
||||
instances = find_instances(instance_id=instance_id, name=name, tags=tags)
|
||||
if instances:
|
||||
log.info('instance exists.')
|
||||
@ -241,9 +231,7 @@ def run(image_id, name=None, tags=None, instance_type='m1.small',
|
||||
'''
|
||||
#TODO: support multi-instance reservations
|
||||
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
reservation = conn.run_instances(image_id, instance_type=instance_type,
|
||||
key_name=key_name,
|
||||
@ -279,9 +267,7 @@ def get_key(key_name, region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
salt myminion boto_ec2.get_key mykey
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
try:
|
||||
key = conn.get_key_pair(key_name)
|
||||
@ -303,9 +289,8 @@ def create_key(key_name, save_path, region=None, key=None, keyid=None,
|
||||
|
||||
salt myminion boto_ec2.create mykey /root/
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
try:
|
||||
key = conn.create_key_pair(key_name)
|
||||
log.debug("the key to return is : {0}".format(key))
|
||||
@ -330,9 +315,8 @@ def import_key(key_name, public_key_material, region=None, key=None,
|
||||
|
||||
salt myminion boto_ec2.import mykey publickey
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
try:
|
||||
key = conn.import_key_pair(key_name, public_key_material)
|
||||
log.debug("the key to return is : {0}".format(key))
|
||||
@ -349,9 +333,8 @@ def delete_key(key_name, region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
salt myminion boto_ec2.delete_key mykey
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
try:
|
||||
key = conn.delete_key_pair(key_name)
|
||||
log.debug("the key to return is : {0}".format(key))
|
||||
@ -376,9 +359,8 @@ def get_keys(keynames=None, filters=None, region=None, key=None,
|
||||
|
||||
salt myminion boto_ec2.get_keys
|
||||
'''
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
|
||||
try:
|
||||
keys = conn.get_all_key_pairs(keynames, filters)
|
||||
log.debug("the key to return is : {0}".format(keys))
|
||||
@ -390,47 +372,3 @@ def get_keys(keynames=None, filters=None, region=None, key=None,
|
||||
except boto.exception.BotoServerError as e:
|
||||
log.debug(e)
|
||||
return False
|
||||
|
||||
|
||||
def _get_conn(region, key, keyid, profile):
|
||||
'''
|
||||
Get a boto connection to ec2.
|
||||
'''
|
||||
if profile:
|
||||
if isinstance(profile, six.string_types):
|
||||
_profile = __salt__['config.option'](profile)
|
||||
elif isinstance(profile, dict):
|
||||
_profile = profile
|
||||
key = _profile.get('key', None)
|
||||
keyid = _profile.get('keyid', None)
|
||||
region = _profile.get('region', None)
|
||||
|
||||
if not region and __salt__['config.option']('ec2.region'):
|
||||
region = __salt__['config.option']('ec2.region')
|
||||
|
||||
if not region:
|
||||
region = 'us-east-1'
|
||||
|
||||
if not key and __salt__['config.option']('ec2.key'):
|
||||
key = __salt__['config.option']('ec2.key')
|
||||
if not keyid and __salt__['config.option']('ec2.keyid'):
|
||||
keyid = __salt__['config.option']('ec2.keyid')
|
||||
|
||||
# avoid repeatedly creating new connections
|
||||
if keyid:
|
||||
cxkey = 'boto_ec2:' + hashlib.md5(region + keyid + key).hexdigest()
|
||||
else:
|
||||
cxkey = 'boto_ec2:' + region
|
||||
|
||||
if cxkey in __context__:
|
||||
return __context__[cxkey]
|
||||
|
||||
try:
|
||||
conn = boto.ec2.connect_to_region(region, aws_access_key_id=keyid,
|
||||
aws_secret_access_key=key)
|
||||
except boto.exception.NoAuthHandlerFound:
|
||||
log.error('No authentication credentials found when attempting to'
|
||||
' make boto ec2 connection.')
|
||||
return None
|
||||
__context__[cxkey] = conn
|
||||
return conn
|
||||
|
@ -34,7 +34,7 @@ Connection module for Amazon VPC
|
||||
:depends: boto
|
||||
|
||||
'''
|
||||
# keep linter from choking on _get_conn and _cache_id
|
||||
# keep lint from choking on _get_conn and _cache_id
|
||||
#pylint disable=F821
|
||||
|
||||
# Import Python libs
|
||||
|
@ -220,7 +220,7 @@ def get_exception(e):
|
||||
return CommandExecutionError(message)
|
||||
|
||||
|
||||
def assign_funcs(module, service):
|
||||
def assign_funcs(modname, service, module=None):
|
||||
'''
|
||||
Assign _get_conn and _cache_id functions to the named module.
|
||||
|
||||
@ -228,6 +228,6 @@ def assign_funcs(module, service):
|
||||
|
||||
_utils__['boto.assign_partials'](__name__, 'ec2')
|
||||
'''
|
||||
mod = sys.modules[module]
|
||||
setattr(mod, '_get_conn', get_connection_func(service))
|
||||
mod = sys.modules[modname]
|
||||
setattr(mod, '_get_conn', get_connection_func(service, module=module))
|
||||
setattr(mod, '_cache_id', cache_id_func(service))
|
||||
|
Loading…
Reference in New Issue
Block a user