2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.base import Task
|
|
|
|
from bootstrapvz.common import phases
|
2013-06-24 23:12:39 +02:00
|
|
|
import host
|
2013-05-02 19:13:35 +02:00
|
|
|
|
|
|
|
|
2015-05-03 12:31:44 +02:00
|
|
|
class SilenceBotoDebug(Task):
|
|
|
|
description = 'Silence boto debug logging'
|
|
|
|
phase = phases.preparation
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
# Regardless of of loglevel, we don't want boto debug stuff, it's very noisy
|
|
|
|
import logging
|
|
|
|
logging.getLogger('boto').setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
2013-05-02 19:13:35 +02:00
|
|
|
class GetCredentials(Task):
|
2013-06-09 20:29:54 +02:00
|
|
|
description = 'Getting AWS credentials'
|
2013-06-23 22:12:29 +02:00
|
|
|
phase = phases.preparation
|
2015-05-03 12:31:44 +02:00
|
|
|
successors = [SilenceBotoDebug]
|
2013-06-09 20:29:54 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-07-14 23:16:37 +02:00
|
|
|
keys = ['access-key', 'secret-key']
|
|
|
|
if info.manifest.volume['backing'] == 's3':
|
|
|
|
keys.extend(['certificate', 'private-key', 'user-id'])
|
2014-01-05 15:57:11 +01:00
|
|
|
info.credentials = cls.get_credentials(info.manifest, keys)
|
2013-05-02 19:13:35 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def get_credentials(cls, manifest, keys):
|
2013-05-02 19:13:35 +02:00
|
|
|
from os import getenv
|
2013-07-14 23:16:37 +02:00
|
|
|
creds = {}
|
2014-07-12 19:40:53 +02:00
|
|
|
if 'credentials' in manifest.provider:
|
|
|
|
if all(key in manifest.provider['credentials'] for key in keys):
|
|
|
|
for key in keys:
|
|
|
|
creds[key] = manifest.provider['credentials'][key]
|
|
|
|
return creds
|
2013-11-04 17:31:32 +01:00
|
|
|
|
|
|
|
def env_key(key):
|
2013-12-14 19:37:23 +01:00
|
|
|
return ('aws-' + key).upper().replace('-', '_')
|
2013-11-04 17:31:32 +01:00
|
|
|
if all(getenv(env_key(key)) is not None for key in keys):
|
2013-07-14 23:16:37 +02:00
|
|
|
for key in keys:
|
2013-11-04 17:31:32 +01:00
|
|
|
creds[key] = getenv(env_key(key))
|
2013-07-14 23:16:37 +02:00
|
|
|
return creds
|
2014-06-30 20:23:44 +02:00
|
|
|
|
|
|
|
def provider_key(key):
|
|
|
|
return key.replace('-', '_')
|
|
|
|
import boto.provider
|
|
|
|
provider = boto.provider.Provider('aws')
|
|
|
|
if all(getattr(provider, provider_key(key)) is not None for key in keys):
|
|
|
|
for key in keys:
|
|
|
|
creds[key] = getattr(provider, provider_key(key))
|
2016-04-14 19:28:44 -04:00
|
|
|
if hasattr(provider, 'security_token'):
|
|
|
|
creds['security-token'] = provider.security_token
|
2014-06-30 20:23:44 +02:00
|
|
|
return creds
|
2013-07-14 23:16:37 +02:00
|
|
|
raise RuntimeError(('No ec2 credentials found, they must all be specified '
|
|
|
|
'exclusively via environment variables or through the manifest.'))
|
2013-05-02 19:13:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Connect(Task):
|
2013-06-09 20:29:54 +02:00
|
|
|
description = 'Connecting to EC2'
|
2013-06-23 22:12:29 +02:00
|
|
|
phase = phases.preparation
|
2014-05-03 10:43:14 +02:00
|
|
|
predecessors = [GetCredentials, host.GetInstanceMetadata, host.SetRegion]
|
2013-06-26 20:14:37 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-06-23 12:00:17 +02:00
|
|
|
from boto.ec2 import connect_to_region
|
2016-04-14 19:28:44 -04:00
|
|
|
connect_args = {
|
|
|
|
'aws_access_key_id': info.credentials['access-key'],
|
|
|
|
'aws_secret_access_key': info.credentials['secret-key']
|
|
|
|
}
|
|
|
|
|
|
|
|
if 'security-token' in info.credentials:
|
|
|
|
connect_args['security_token'] = info.credentials['security-token']
|
|
|
|
|
|
|
|
info._ec2['connection'] = connect_to_region(info._ec2['region'], **connect_args)
|