本文整理汇总了Python中infrastructure_manager.InfrastructureManager类的典型用法代码示例。如果您正苦于以下问题:Python InfrastructureManager类的具体用法?Python InfrastructureManager怎么用?Python InfrastructureManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了InfrastructureManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_instances
def run_instances(self, prefix, blocking, success=True):
i = InfrastructureManager(blocking=blocking)
reservation = Reservation()
instance = flexmock(name='instance', private_dns_name='private-ip',
public_dns_name='public-ip', id='i-id', state='running',
key_name='bookeyname', ip_address='public-ip',
private_ip_address='private-ip')
new_instance = flexmock(name='new-instance', private_dns_name='new-private-ip',
public_dns_name='new-public-ip', id='new-i-id',
state='running', key_name='bookeyname',
ip_address='new-public-ip',
private_ip_address='new-private-ip')
reservation.instances = [instance]
new_reservation = Reservation()
new_reservation.instances = [instance, new_instance]
self.fake_ec2.should_receive('get_all_instances').and_return([]) \
.and_return([reservation]).and_return([new_reservation])
# first, validate that the run_instances call goes through successfully
# and gives the user a reservation id
full_params = {
'credentials': {
'a': 'b', 'EC2_URL': 'http://testing.appscale.com:8773/foo/bar',
'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'},
'group': 'boogroup',
'image_id': 'booid',
'infrastructure': prefix,
'instance_type': 'booinstance_type',
'keyname': 'bookeyname',
'num_vms': '1',
'use_spot_instances': False,
'region' : 'my-zone-1',
'zone' : 'my-zone-1b',
'autoscale_agent': True
}
id = '0000000000' # no longer randomly generated
full_result = {
'success': True,
'reservation_id': id,
'reason': 'none'
}
if success:
self.assertEquals(full_result, i.run_instances(full_params, 'secret'))
# next, look at run_instances internally to make sure it actually is
# updating its reservation info
if not blocking:
time.sleep(.1)
if success:
self.assertEquals(InfrastructureManager.STATE_RUNNING,
i.reservations.get(id)['state'])
vm_info = i.reservations.get(id)['vm_info']
self.assertEquals(['new-public-ip'], vm_info['public_ips'])
self.assertEquals(['new-private-ip'], vm_info['private_ips'])
self.assertEquals(['new-i-id'], vm_info['instance_ids'])
else:
if blocking:
self.assertRaises(AgentRuntimeException, i.run_instances, full_params, 'secret')
示例2: validateCredentials
def validateCredentials(self, params):
'''
This method verifies the validity of ec2 credentials
'''
if params['infrastructure'] is None:
logging.error("validateCredentials: infrastructure param not set")
return False
creds = params['credentials']
if creds is None:
logging.error("validateCredentials: credentials param not set")
return False
if creds['EC2_ACCESS_KEY'] is None:
logging.error("validateCredentials: credentials EC2_ACCESS_KEY not set")
return False
if creds['EC2_SECRET_KEY'] is None:
logging.error("validateCredentials: credentials EC2_ACCESS_KEY not set")
return False
logging.debug("validateCredentials: inside method with params : %s", str(params))
try:
i = InfrastructureManager()
logging.debug("validateCredentials: exiting with result : %s", str(i))
return i.validate_credentials(params)
except Exception, e:
logging.error("validateCredentials: exiting with error : %s", str(e))
return False
示例3: test_ec2_run_instances
def test_ec2_run_instances(self):
i = InfrastructureManager(blocking=True)
# first, validate that the run_instances call goes through successfully
# and gives the user a reservation id
full_params = {
'credentials': {'a': 'b', 'EC2_URL': 'http://testing.appscale.com:8773/foo/bar',
'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'},
'group': 'boogroup',
'image_id': 'booid',
'infrastructure': 'ec2',
'instance_type': 'booinstance_type',
'keyname': 'bookeyname',
'num_vms': '1',
'use_spot_instances': 'True',
'max_spot_price' : '1.23',
'zone' : 'my-zone-1b'
}
id = '0000000000' # no longer randomly generated
full_result = {
'success': True,
'reservation_id': id,
'reason': 'none'
}
self.assertEquals(full_result, i.run_instances(full_params, 'secret'))
# next, look at run_instances internally to make sure it actually is
# updating its reservation info
self.assertEquals(InfrastructureManager.STATE_RUNNING, i.reservations.get(id)['state'])
vm_info = i.reservations.get(id)['vm_info']
self.assertEquals(['public-ip'], vm_info['public_ips'])
self.assertEquals(['private-ip'], vm_info['private_ips'])
self.assertEquals(['i-id'], vm_info['instance_ids'])
示例4: terminate_instances
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'])
示例5: deregister_flex_cloud
def deregister_flex_cloud(self, parameters, blocking=True):
try:
i = InfrastructureManager(blocking=blocking)
res = i.deregister_instances(parameters=parameters, terminate=False)
ret = True
except Exception, e:
logging.error("deregister_flex_cloud() failed with error : %s", str(e))
ret = False
示例6: validateCredentials
def validateCredentials(self, params):
'''
This method verifies the validity of ec2 credentials
'''
logging.info("validateCredentials: inside method with params : %s", str(params))
try:
i = InfrastructureManager()
logging.info("validateCredentials: exiting with result : %s", str(i))
return i.validate_Credentials(params)
except Exception, e:
logging.error("validateCredentials: exiting with error : %s", str(e))
return False
示例7: stopMachines
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
示例8: test_attach_persistent_disk
def test_attach_persistent_disk(self):
# mock out interactions with GCE
# first, mock out the oauth library calls
fake_credentials = flexmock(name='fake_credentials', invalid=False)
fake_storage = flexmock(name='fake_storage')
fake_storage.should_receive('get').and_return(fake_credentials)
flexmock(oauth2client.file)
oauth2client.file.should_receive('Storage').with_args(
GCEAgent.OAUTH2_STORAGE_LOCATION).and_return(fake_storage)
# next, mock out http calls to GCE
fake_http = flexmock(name='fake_http')
fake_authorized_http = flexmock(name='fake_authorized_http')
flexmock(httplib2)
httplib2.should_receive('Http').and_return(fake_http)
fake_credentials.should_receive('authorize').with_args(fake_http) \
.and_return(fake_authorized_http)
fake_instances = flexmock(name='fake_instances')
fake_gce = flexmock(name='fake_gce')
fake_gce.should_receive('instances').and_return(fake_instances)
fake_attach_disk_request = flexmock(name='fake_attach_disk_request')
fake_instances.should_receive('get').and_return(fake_attach_disk_request)
attach_disk_info = {
'status': 'DONE',
'disks': []
}
fake_attach_disk_request.should_receive('execute').with_args(
fake_authorized_http).and_return(attach_disk_info)
fake_instances.should_receive('attachDisk').with_args(project=self.project,
body=dict, instance='my-instance', zone=str).and_return(
fake_attach_disk_request)
# finally, inject our fake GCE connection
flexmock(discovery)
discovery.should_receive('build').with_args('compute',
GCEAgent.API_VERSION).and_return(fake_gce)
iaas = InfrastructureManager(blocking=True)
disk_name = 'my-disk-name'
instance_id = 'my-instance'
expected = '/dev/sdb'
actual = iaas.attach_disk(self.params, disk_name, instance_id, 'secret')
self.assertTrue(actual['success'])
self.assertEquals(expected, actual['location'])
示例9: describe_machines_from_db
def describe_machines_from_db(self, infrastructure, force=False):
parameters = {
"infrastructure": infrastructure,
"credentials": self.get_credentials(),
"key_prefix": self.user_data.user_id,
"user_id": self.user_data.user_id,
}
if infrastructure == AgentTypes.FLEX:
parameters['flex_cloud_machine_info'] = self.user_data.get_flex_cloud_machine_info()
parameters['reservation_id'] = self.user_data.reservation_id
i = InfrastructureManager()
i.synchronize_db(parameters, force=force)
all_vms = VMStateModel.get_all(parameters)
return all_vms
示例10: startMachines
def startMachines(self, params, block=False):
'''
This method instantiates ec2 instances
'''
logging.info("startMachines : inside method with params : %s", str(params))
try:
#make sure that any keynames we use are prefixed with stochss so that
#we can do a terminate all based on keyname prefix
key_name = params["keyname"]
if not key_name.startswith(self.KEYPREFIX):
params['keyname'] = self.KEYPREFIX + key_name
# NOTE: We are forcing blocking mode within the InfrastructureManager class
# for the launching of VMs because of how GAE joins on all threads before
# returning a response from a request.
i = InfrastructureManager(blocking=block)
res = {}
# NOTE: We need to make sure that the RabbitMQ server is running if any compute
# nodes are running as we are using the AMQP broker option for Celery.
compute_check_params = {
"credentials": params["credentials"],
"key_prefix": params["key_prefix"]
}
if self.isQueueHeadRunning(compute_check_params):
res = i.run_instances(params,[])
else:
# Need to start the queue head (RabbitMQ)
params["queue_head"] = True
vms_requested = int(params["num_vms"])
requested_key_name = params["keyname"]
# Only want one queue head, and it must have its own key so
# it can be differentiated if necessary
params["num_vms"] = 1
params["keyname"] = requested_key_name+'-'+self.QUEUEHEAD_KEY_TAG
res = i.run_instances(params,[])
#NOTE: This relies on the InfrastructureManager being run in blocking mode...
queue_head_ip = res["vm_info"]["public_ips"][0]
self.__update_celery_config_with_queue_head_ip(queue_head_ip)
params["keyname"] = requested_key_name
params["queue_head"] = False
if vms_requested > 1:
params["num_vms"] = vms_requested - 1
res = i.run_instances(params,[])
params["num_vms"] = vms_requested
logging.info("startMachines : exiting method with result : %s", str(res))
return res
except Exception, e:
logging.error("startMachines : exiting method with error : {0}".format(str(e)))
print "startMachines : exiting method with error :", str(e)
return None
示例11: describeMachines
def describeMachines(self, params):
"""
This method gets the status of all the instances of ec2
"""
# add calls to the infrastructure manager for getting details of
# machines
logging.info("describeMachines : inside method with params : %s", str(params))
try:
i = InfrastructureManager()
res = i.describe_instances(params, [], backendservices.KEYPREFIX)
logging.info("describeMachines : exiting method with result : %s", str(res))
return res
except Exception, e:
logging.error("describeMachines : exiting method with error : %s", str(e))
return None
示例12: startMachines
def startMachines(self, params):
'''
This method instantiates ec2 instances
'''
#this will basically start an instance in ec2
# add call from the infrastructure manager here
logging.info("startMachines : inside method with params : %s", str(params))
try:
i = InfrastructureManager(blocking=True)
res = i.run_instances(params,params)
return res
logging.info("startMachines : exiting method with result : %s", str(res))
except Exception, e:
logging.error("startMachines : exiting method with error : %s", str(e))
return None
示例13: describeMachines
def describeMachines(self, params):
'''
This method gets the status of all the instances of ec2
'''
# add calls to the infrastructure manager for getting details of
# machines
logging.info("describeMachines : inside method with params : %s", str(params))
try:
i = InfrastructureManager()
secret =[]
res = i.describe_instances(params, secret)
logging.info("describeMachines : exiting method with result : %s", str(res))
return res
except Exception, e:
logging.error("describeMachines : exiting method with error : %s", str(e))
return None
示例14: stopMachines
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
示例15: stop_ec2_vms
def stop_ec2_vms(self, params, blocking=False):
'''
This method would terminate all the EC2 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 = AgentConfig.get_agent_key_prefix(agent_type=AgentTypes.EC2,
key_prefix=params.get('key_prefix', ''))
try:
logging.debug("Stopping compute nodes with key_prefix: {0}".format(key_prefix))
i = InfrastructureManager(blocking=blocking)
res = i.deregister_instances(parameters=params, terminate=True)
ret = True
except Exception, e:
logging.error("Terminate machine failed with error : %s", str(e))
ret = False