class AMIPackerTest(unittest.TestCase): def setUp(self): logging.config.fileConfig(os.path.join(os.getcwd(), 'logging.ini')) self.name = 'test-amipacker' self.aws_key = Credential() self.region = 'us-east-1' self.ami = None self.helper = EC2(self.aws_key.aws_access_key_id, self.aws_key.aws_secret_access_key, self.region) self.helper.create_key_pair(self.name) self.helper.create_sg(self.name) self.instance_id = None def tearDown(self): # drop everything if self.instance_id: self.helper.terminate_instance(self.instance_id, 180) self.helper.delete_key_pair(self.name) self.helper.delete_sg(self.name) if self.ami: ami = AMI(self.region, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) ami.deregister_ami(self.ami) def _clean_previous_ami(self, name): ami = AMI(self.region, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) image_id = ami.get_ami_id(name) if image_id: ami.deregister_ami(image_id) def test_run(self): # build ami AAA via ami_packer result_queue = Queue.Queue() config = ConfigParser.SafeConfigParser() config.read(os.path.join(os.getcwd(), 'config', 'config.ini')) try: packer_path = config.get('Packer_Config', 'path') packer_bin = os.path.join(packer_path, 'packer') except: packer_bin = 'packer' timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d') amipacker = AMIPacker(os.path.join(os.getcwd(), 'tests', 'template', 'test_template.json'), 'ubuntu/images/ebs/ubuntu-trusty-14.04-amd64-server-20140416.1', '123456', timestamp, self.region, result_queue, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key(), packer_bin) self._clean_previous_ami('test_omelet_ami') amipacker.run() if not result_queue.empty(): (region, result, message, existence) = result_queue.get() print message self.ami = message # launching instance B for check ami = AMI(self.region, self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) assert ami.check_image(message, 'available') self.instance_id = self.helper.launch_instance(message, self.name, self.name) # check modules are in instance B std_out = self.helper.run_command(self.instance_id, 'dpkg -l | grep newrelic', self.name, 'ubuntu') print std_out assert 'newrelic-sysmond' in std_out
def notify(topic, subject, message): configs = OmeletConfigParser() aws_key = Credential() omelet_notifier = Notifier(configs.get_sns_region(), aws_key.get_aws_access_key(), aws_key.get_aws_secret_key()) omelet_notifier.send(configs.get_sns_topic('patch'), subject, message)
class SNSTest(unittest.TestCase): def setUp(self): # set dummy credentials os.environ["AWS_ACCESS_KEY_ID"] = "THISISTESTKEY" os.environ["AWS_SECRET_ACCESS_KEY"] = "THISISTESTSECRET" pass @mock_sns def testSend(self): self.aws_key = Credential() notify = Notifier("us-east-1", self.aws_key.get_aws_access_key(), self.aws_key.get_aws_secret_key()) notify.send("omelet", "hello", "can you see my message?")
def _get_db_accessor(): #TODO singleton? aws_key = Credential() db_configs = DBConfigParser() db = DBAccessor() db.connect(db_configs.get_dynamodb_region(), aws_key.get_aws_access_key(), aws_key.get_aws_secret_key()) db.table_init(db_configs.get_dynamodb_tables()['image_table'], db_configs.get_dynamodb_tables()['tree_table'], db_configs.get_dynamodb_tables()['history_table']) return db
def main(logger): #objects configs = OmeletConfigParser() region = configs.get_glacier_region() aws_key = Credential() #parse argument parser = argparse.ArgumentParser() parser.add_argument('filename', type=str, help='file to upload to Glacier') parser.add_argument('--vault', required=True, help='vault name') args = parser.parse_args() #parameters filename = args.filename vault = args.vault #exec glacier_helper = Glacier(_aws_access_key_id=aws_key.get_aws_access_key(), _aws_secret_access_key=aws_key.get_aws_secret_key(), _region=region) glacier_helper.upload(vault, filename, True)
# should be ~/clone or ~/src/github.com/TrendMicroDCS parser.add_argument("-r", "--repo", type=str, help="target packer template repo master", required=True) # configs args = parser.parse_args() configs = OmeletConfigParser() # logging logging.config.fileConfig(os.path.join(configs.get_omelet_config_dir(), 'logging.ini')) logger = logging.getLogger(__name__) # aws key aws_key = Credential() # mail sender omelet_notifier = Notifier(configs.get_sns_region(), aws_key.get_aws_access_key(), aws_key.get_aws_secret_key()) try: amipacker_launcher = OmeletAMIPacker(configs, aws_key, args.repo) ret = amipacker_launcher.launch() if ret: omelet_notifier.send(configs.get_sns_topic("amipacker"), "Validation error: template is in the wrong path", ret) except Exception as err: logger.error(err) cause = "Omelet AMIPacker Error:\n\t%s\n\nTraceback:\n\t%s" % (err, traceback.format_exc()) omelet_notifier.send(configs.get_sns_topic("admin"), "Omelet Program Error", cause)
def main(region, id): aws_key = Credential() image = AMI(region, aws_key.get_aws_access_key(), aws_key.get_aws_secret_key()) image.get_ami_obj(id)