class EC2(object): def __init__(self, configpath): configpath = os.path.abspath(configpath) if not os.path.exists(configpath): log.error("Config '%s' doesn't exist." % configpath) sys.exit(1) if os.path.isdir(configpath): configpath = os.path.join(configpath, 'aws.conf') self.config = Config(configpath) self.known_hosts = os.path.join(self.config.path, 'known_hosts') self.instances = {} for sid in self.config.get('instance', {}): self.instances[sid] = Instance(self, sid) self.servers = {} for sid in self.config.get('server', {}): self.servers[sid] = Server(self, sid) intersection = set(self.instances).intersection(set(self.servers)) if len(intersection) > 0: log.error("Instance and server names must be unique, the following names are duplicated:") log.error("\n".join(intersection)) sys.exit(1) self.all = {} self.all.update(self.instances) self.all.update(self.servers) self.clusters = {} for cid in self.config.get('cluster',{}): self.clusters[cid] = Cluster(self,cid)
class EC2(object): def __init__(self, configpath): configpath = os.path.abspath(configpath) if not os.path.exists(configpath): log.error("Config '%s' doesn't exist." % configpath) sys.exit(1) if os.path.isdir(configpath): configpath = os.path.join(configpath, 'aws.conf') self.config = Config(configpath) self.known_hosts = os.path.join(self.config.path, 'known_hosts') self.instances = {} for sid in self.config.get('instance', {}): self.instances[sid] = Instance(self, sid) self.servers = {} for sid in self.config.get('server', {}): self.servers[sid] = Server(self, sid) intersection = set(self.instances).intersection(set(self.servers)) if len(intersection) > 0: log.error("Instance and server names must be unique, the following names are duplicated:") log.error("\n".join(intersection)) sys.exit(1) self.all = {} self.all.update(self.instances) self.all.update(self.servers) @lazy def credentials(self): aws_id = None aws_key = None if 'AWS_ACCESS_KEY_ID' not in os.environ or 'AWS_SECRET_ACCESS_KEY' not in os.environ: try: id_file = self.config['global']['aws']['access-key-id'] key_file = self.config['global']['aws']['secret-access-key'] except KeyError: log.error("You need to either set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables or add the path to files containing them to the config. You can find the values at http://aws.amazon.com under 'Your Account'-'Security Credentials'.") sys.exit(1) id_file = os.path.abspath(os.path.expanduser(os.path.expandvars(id_file))) if not os.path.exists(id_file): log.error("The access-key-id file at '%s' doesn't exist." % id_file) sys.exit(1) key_file = os.path.abspath(os.path.expanduser(os.path.expandvars(key_file))) if not os.path.exists(key_file): log.error("The secret-access-key file at '%s' doesn't exist." % key_file) sys.exit(1) aws_id = open(id_file).readline().strip() aws_key = open(key_file).readline().strip() return (aws_id, aws_key) @lazy def regions(self): (aws_id, aws_key) = self.credentials return dict((x.name, x) for x in boto.ec2.regions( aws_access_key_id=aws_id, aws_secret_access_key=aws_key )) @property def snapshots(self): return dict((x.id, x) for x in self.conn.get_all_snapshots(owner="self")) @lazy def conn(self): (aws_id, aws_key) = self.credentials region_id = self.config.get( 'global', {}).get( 'aws', {}).get( 'region', None) if region_id is None: log.error("No region set in global config") sys.exit(1) region = self.regions[region_id] return region.connect( aws_access_key_id=aws_id, aws_secret_access_key=aws_key )