def terminate_instances(self, prefix, blocking): i = InfrastructureManager(blocking=blocking) params1 = {'infrastructure': prefix} self.assertRaises(AgentConfigurationException, i.terminate_instances, params1, 'secret') params2 = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://ec2.url.com', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'}, 'infrastructure': prefix, 'instance_ids': ['i-12345'], 'region' : 'my-zone-1', 'keyname': 'bookeyname' } reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-id', state='terminated', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') reservation.instances = [instance] self.fake_ec2.should_receive('get_all_instances').and_return([reservation]) flexmock(i).should_receive('_InfrastructureManager__kill_vms') result = i.terminate_instances(params2, 'secret') if not blocking: time.sleep(.1) self.assertTrue(result['success'])
def terminate_instances(self, prefix, blocking): i = InfrastructureManager(blocking=blocking) params1 = {'infrastructure': prefix} result1 = i.terminate_instances(params1, 'secret') self.assertFalse(result1['success']) self.assertEquals(result1['reason'], 'no credentials') params2 = { 'credentials': {'a': 'b', 'EC2_URL': 'http://ec2.url.com', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'}, 'infrastructure': prefix, 'instance_ids': ['i-12345'] } result2 = i.terminate_instances(params2, 'secret') if not blocking: time.sleep(2) self.assertTrue(result2['success'])
def terminate_instances(self, prefix, blocking): i = InfrastructureManager(blocking=blocking) params1 = {'infrastructure': prefix} result1 = i.terminate_instances(params1, 'secret') self.assertFalse(result1['success']) self.assertEquals(result1['reason'], 'no credentials') params2 = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://ec2.url.com', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'}, 'infrastructure': prefix, 'instance_ids': ['i-12345'], 'region' : 'my-zone-1' } result2 = i.terminate_instances(params2, 'secret') if not blocking: time.sleep(.1) self.assertTrue(result2['success'])
def stopMachines(self, params, block=False): """ This method would terminate all the instances associated with the account that have a keyname prefixed with stochss (all instances created by the backend service) params must contain credentials key/value """ try: i = InfrastructureManager(blocking=block) res = i.terminate_instances(params, backendservices.KEYPREFIX) return True except Exception, e: logging.error("Terminate machine failed with error : %s", str(e)) return False
def stopMachines(self, params): ''' This method would terminate all the instances associated with the account It expects the following two fields in the parameter argument params ={"infrastructure":"ec2", 'credentials':{"EC2_ACCESS_KEY":"______________", "EC2_SECRET_KEY":"__________"}, } ''' try: i = InfrastructureManager(blocking=True) res = i.terminate_instances(params) print str(res) return True except Exception, e: logging.error("Terminate machine failed with error : %s", str(e)) return False
def stopMachines(self, params, block=False): ''' This method would terminate all the instances associated with the account that have a keyname prefixed with stochss (all instances created by the backend service) params must contain credentials key/value ''' key_prefix = self.KEYPREFIX if "key_prefix" in params and not params["key_prefix"].startswith(key_prefix): key_prefix += params["key_prefix"] elif "key_prefix" in params: key_prefix = params["key_prefix"] try: logging.info("Stopping compute nodes with key_prefix: {0}".format(key_prefix)) i = InfrastructureManager(blocking=block) res = i.terminate_instances(params,key_prefix) return True except Exception, e: logging.error("Terminate machine failed with error : %s", str(e)) return False