Skip to content

Commit

Permalink
Merge branch 'indigo3' of https://github.com/grycap/im into indigo3
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Dec 11, 2017
2 parents 178e865 + e0161a8 commit c574780
Show file tree
Hide file tree
Showing 39 changed files with 962 additions and 523 deletions.
105 changes: 51 additions & 54 deletions IM/ConfManager.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions IM/InfrastructureInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,7 @@ def Contextualize(self, auth, vm_list=None):
break

if not ctxt:
InfrastructureInfo.logger.debug(
"Inf ID: " + str(self.id) + ": Contextualization disabled by the RADL.")
InfrastructureInfo.logger.info("Inf ID: " + str(self.id) + ": Contextualization disabled by the RADL.")
self.cont_out = "Contextualization disabled by the RADL."
self.configured = True
for vm in self.get_vm_list():
Expand Down
177 changes: 83 additions & 94 deletions IM/InfrastructureManager.py

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion IM/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,17 @@ def RESTGetInfrastructureProperty(infid=None, prop=None):

try:
if prop == "contmsg":
res = InfrastructureManager.GetInfrastructureContMsg(infid, auth)
headeronly = False
if "headeronly" in bottle.request.params.keys():
str_headeronly = bottle.request.params.get("headeronly").lower()
if str_headeronly in ['yes', 'true', '1']:
headeronly = True
elif str_headeronly in ['no', 'false', '0']:
headeronly = False
else:
return return_error(400, "Incorrect value in context parameter")

res = InfrastructureManager.GetInfrastructureContMsg(infid, auth, headeronly)
elif prop == "radl":
res = InfrastructureManager.GetInfrastructureRADL(infid, auth)
elif prop == "state":
Expand Down
5 changes: 3 additions & 2 deletions IM/ServiceRequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ class Request_GetInfrastructureContMsg(IMBaseRequest):

def _call_function(self):
self._error_mesage = "Error gettinf the Inf. cont msg"
(inf_id, auth_data) = self.arguments
(inf_id, auth_data, headeronly) = self.arguments
return IM.InfrastructureManager.InfrastructureManager.GetInfrastructureContMsg(inf_id,
Authentication(auth_data))
Authentication(auth_data),
headeronly)


class Request_StartVM(IMBaseRequest):
Expand Down
13 changes: 6 additions & 7 deletions IM/VirtualMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def update_status(self, auth, force=False):
updated = True
self.last_update = now
elif self.creating:
self.log_debug("VM is in creation process, set pending state")
self.log_info("VM is in creation process, set pending state")
state = VirtualMachine.PENDING
else:
self.log_error("Error updating VM status: %s" % new_vm)
Expand Down Expand Up @@ -633,8 +633,7 @@ def kill_check_ctxt_process(self):
if self.ctxt_pid != self.WAIT_TO_PID:
ssh = self.get_ssh_ansible_master()
try:
self.log_debug(
"Killing ctxt process with pid: " + str(self.ctxt_pid))
self.log_info("Killing ctxt process with pid: " + str(self.ctxt_pid))

# Try to get PGID to kill all child processes
pgkill_success = False
Expand Down Expand Up @@ -691,7 +690,7 @@ def check_ctxt_process(self):
ssh = self.get_ssh_ansible_master()

try:
self.log_debug("Getting status of ctxt process with pid: " + str(ctxt_pid))
self.log_info("Getting status of ctxt process with pid: " + str(ctxt_pid))
(_, _, exit_status) = ssh.execute("ps " + str(ctxt_pid))
except:
self.log_warn("Error getting status of ctxt process with pid: " + str(ctxt_pid))
Expand All @@ -710,7 +709,7 @@ def check_ctxt_process(self):

if exit_status != 0:
# The process has finished, get the outputs
self.log_debug("The process %s has finished, get the outputs" % ctxt_pid)
self.log_info("The process %s has finished, get the outputs" % ctxt_pid)
ctxt_log = self.get_ctxt_log(remote_dir, True)
msg = self.get_ctxt_output(remote_dir, True)
if ctxt_log:
Expand All @@ -724,11 +723,11 @@ def check_ctxt_process(self):
# dynamically
if Config.UPDATE_CTXT_LOG_INTERVAL > 0 and wait > Config.UPDATE_CTXT_LOG_INTERVAL:
wait = 0
self.log_debug("Get the log of the ctxt process with pid: " + str(ctxt_pid))
self.log_info("Get the log of the ctxt process with pid: " + str(ctxt_pid))
ctxt_log = self.get_ctxt_log(remote_dir)
self.cont_out = initial_count_out + ctxt_log
# The process is still running, wait
self.log_debug("The process %s is still running. wait." % ctxt_pid)
self.log_info("The process %s is still running. wait." % ctxt_pid)
time.sleep(Config.CHECK_CTXT_PROCESS_INTERVAL)
wait += Config.CHECK_CTXT_PROCESS_INTERVAL
else:
Expand Down
2 changes: 1 addition & 1 deletion IM/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
'InfrastructureInfo', 'InfrastructureManager', 'recipe', 'request', 'REST', 'retry',
'ServiceRequests', 'SSH', 'SSHRetry', 'timedcall', 'UnixHTTPAdapter', 'uriparse',
'VirtualMachine', 'VMRC', 'xmlobject']
__version__ = '1.6.3'
__version__ = '1.6.5'
__author__ = 'Miguel Caballer'
22 changes: 22 additions & 0 deletions IM/ansible_utils/ansible_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def get_play_prereqs_2(self, options):

variable_manager = VariableManager()
variable_manager.extra_vars = self.extra_vars
variable_manager.options_vars = {'ansible_version': self.version_info(ansible_version)}

# Add this to avoid the Ansible bug: no host vars as host is not in inventory
# In version 2.0.1 it must be fixed
Expand Down Expand Up @@ -186,9 +187,30 @@ def get_play_prereqs_2_4(self, options):
# the code, ensuring a consistent view of global variables
variable_manager = VariableManager(loader=loader, inventory=inventory)
variable_manager.extra_vars = self.extra_vars
variable_manager.options_vars = {'ansible_version': self.version_info(ansible_version)}

return loader, inventory, variable_manager

def version_info(self, ansible_version_string):
''' return full ansible version info '''
ansible_ver = ansible_version_string.split()[0]
ansible_versions = ansible_ver.split('.')
for counter in range(len(ansible_versions)):
if ansible_versions[counter] == "":
ansible_versions[counter] = 0
try:
ansible_versions[counter] = int(ansible_versions[counter])
except:
pass
if len(ansible_versions) < 3:
for counter in range(len(ansible_versions), 3):
ansible_versions.append(0)
return {'string': ansible_version_string.strip(),
'full': ansible_ver,
'major': ansible_versions[0],
'minor': ansible_versions[1],
'revision': ansible_versions[2]}

def launch_playbook_v2(self):
''' run ansible-playbook operations v2.X'''
# create parser for CLI options
Expand Down
2 changes: 1 addition & 1 deletion IM/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Config:
IM_PATH = os.path.dirname(os.path.realpath(__file__))
LOG_FILE = '/var/log/im/inf.log'
LOG_FILE_MAX_SIZE = 10485760
LOG_LEVEL = "DEBUG"
LOG_LEVEL = "INFO"
CONTEXTUALIZATION_DIR = '/usr/share/im/contextualization'
RECIPES_DIR = CONTEXTUALIZATION_DIR + '/AnsibleRecipes'
RECIPES_DB_FILE = CONTEXTUALIZATION_DIR + '/recipes_ansible.db'
Expand Down
60 changes: 29 additions & 31 deletions IM/connectors/Azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,18 @@ def get_instance_type(self, system, credentials, subscription_id):
instace_types = list(compute_client.virtual_machine_sizes.list(location))
instace_types.sort(key=lambda x: (x.number_of_cores, x.memory_in_mb, x.resource_disk_size_in_mb))

res = None
default = None
for instace_type in instace_types:
if instace_type.name == self.INSTANCE_TYPE:
default = instace_type
# get the instance type with the lowest Memory
if res is None:
str_compare = "instace_type.number_of_cores " + cpu_op + " cpu "
str_compare += " and instace_type.memory_in_mb " + memory_op + " memory "
str_compare += " and instace_type.resource_disk_size_in_mb " + disk_free_op + " disk_free"

if eval(str_compare):
if not instance_type_name or instace_type.name == instance_type_name:
return instace_type
str_compare = "instace_type.number_of_cores " + cpu_op + " cpu "
str_compare += " and instace_type.memory_in_mb " + memory_op + " memory "
str_compare += " and instace_type.resource_disk_size_in_mb " + disk_free_op + " disk_free"

if eval(str_compare):
if not instance_type_name or instace_type.name == instance_type_name:
return instace_type

return default

Expand Down Expand Up @@ -449,7 +447,7 @@ def get_azure_vm_create_json(self, storage_account, vm_name, nics, radl, instanc
data_disks = []
while system.getValue("disk." + str(cont) + ".size"):
disk_size = system.getFeature("disk." + str(cont) + ".size").getValue('G')
self.log_debug("Adding a %s GB disk." % disk_size)
self.log_info("Adding a %s GB disk." % disk_size)
data_disks.append({
'name': '%s_disk_%d' % (vm_name, cont),
'disk_size_gb': disk_size,
Expand Down Expand Up @@ -548,7 +546,7 @@ def create_vms(self, inf, radl, requested_radl, num_vm, location, storage_accoun
vm_name,
vm_parameters)

self.log_debug("VM ID: %s created." % vm.id)
self.log_info("VM ID: %s created." % vm.id)
inf.add_vm(vm)
vms.append((True, (vm, async_vm_creation)))
except Exception as ex:
Expand All @@ -557,7 +555,7 @@ def create_vms(self, inf, radl, requested_radl, num_vm, location, storage_accoun

# Delete Resource group and everything in it
if group_name:
self.log_debug("Delete Resource group %s and everything in it." % group_name)
self.log_info("Delete Resource group %s and everything in it." % group_name)
try:
resource_client.resource_groups.delete(group_name).wait()
except:
Expand Down Expand Up @@ -587,15 +585,15 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
with inf._lock:
# Create resource group for the Infrastructure if it does not exists
if not self.get_rg("rg-%s" % inf.id, credentials, subscription_id):
self.log_debug("Creating Inf RG: %s" % "rg-%s" % inf.id)
self.log_info("Creating Inf RG: %s" % "rg-%s" % inf.id)
resource_client.resource_groups.create_or_update("rg-%s" % inf.id, {'location': location})

# Create an storage_account per Infrastructure
storage_account = self.get_storage_account("rg-%s" % inf.id, storage_account_name,
credentials, subscription_id)

if not storage_account:
self.log_debug("Creating storage account: %s" % storage_account_name)
self.log_info("Creating storage account: %s" % storage_account_name)
try:
storage_client = StorageManagementClient(credentials, subscription_id)
storage_client.storage_accounts.create("rg-%s" % inf.id,
Expand All @@ -606,7 +604,7 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
).wait()
except:
self.log_exception("Error creating storage account: %s" % storage_account)
self.log_debug("Delete Inf RG group %s" % "rg-%s" % inf.id)
self.log_info("Delete Inf RG group %s" % "rg-%s" % inf.id)
try:
resource_client.resource_groups.delete("rg-%s" % inf.id)
except:
Expand All @@ -626,29 +624,29 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
if success:
vm, async_vm_creation = data
try:
self.log_debug("Waiting VM ID %s to be created." % vm.id)
self.log_info("Waiting VM ID %s to be created." % vm.id)
async_vm_creation.wait()
res.append((True, vm))
remaining_vms -= 1
except:
self.log_exception("Error waiting the VM %s." % vm.id)

self.log_debug("End of retry %d of %d" % (retries, Config.MAX_VM_FAILS))
self.log_info("End of retry %d of %d" % (retries, Config.MAX_VM_FAILS))

if remaining_vms > 0:
# Remove the general group
self.log_debug("Delete Inf RG group %s" % "rg-%s" % inf.id)
self.log_info("Delete Inf RG group %s" % "rg-%s" % inf.id)
try:
resource_client.resource_groups.delete("rg-%s" % inf.id)
except:
pass
else:
self.log_debug("All VMs created successfully.")
self.log_info("All VMs created successfully.")

return res

def updateVMInfo(self, vm, auth_data):
self.log_debug("Get the VM info with the id: " + vm.id)
self.log_info("Get the VM info with the id: " + vm.id)
group_name = vm.id.split('/')[0]
vm_name = vm.id.split('/')[1]

Expand All @@ -661,9 +659,9 @@ def updateVMInfo(self, vm, auth_data):
self.log_exception("Error getting the VM info: " + vm.id)
return (False, "Error getting the VM info: " + vm.id + ". " + str(ex))

self.log_debug("VM info: " + vm.id + " obtained.")
self.log_info("VM info: " + vm.id + " obtained.")
vm.state = self.PROVISION_STATE_MAP.get(virtual_machine.provisioning_state, VirtualMachine.UNKNOWN)
self.log_debug("The VM state is: " + vm.state)
self.log_info("The VM state is: " + vm.state)

instance_type = self.get_instance_type_by_name(virtual_machine.hardware_profile.vm_size,
virtual_machine.location, credentials, subscription_id)
Expand Down Expand Up @@ -699,11 +697,11 @@ def add_dns_entries(self, vm, credentials, subscription_id):
except Exception:
pass
if not zone:
self.log_debug("Creating DNS zone %s" % domain)
self.log_info("Creating DNS zone %s" % domain)
zone = dns_client.zones.create_or_update(group_name, domain,
{'location': 'global'})
else:
self.log_debug("DNS zone %s exists. Do not create." % domain)
self.log_info("DNS zone %s exists. Do not create." % domain)

if zone:
record = None
Expand All @@ -712,11 +710,11 @@ def add_dns_entries(self, vm, credentials, subscription_id):
except Exception:
pass
if not record:
self.log_debug("Creating DNS record %s." % hostname)
self.log_info("Creating DNS record %s." % hostname)
record_data = {"ttl": 300, "arecords": [{"ipv4_address": ip}]}
dns_client.record_sets.create_or_update(group_name, domain, hostname, 'A', record_data)
else:
self.log_debug("DNS record %s exists. Do not create." % hostname)
self.log_info("DNS record %s exists. Do not create." % hostname)

return True
except Exception:
Expand Down Expand Up @@ -752,25 +750,25 @@ def setIPs(self, vm, network_profile, credentials, subscription_id):

def finalize(self, vm, last, auth_data):
try:
self.log_debug("Terminate VM: " + vm.id)
self.log_info("Terminate VM: " + vm.id)
group_name = vm.id.split('/')[0]
credentials, subscription_id = self.get_credentials(auth_data)
resource_client = ResourceManagementClient(credentials, subscription_id)

# Delete Resource group and everything in it
if self.get_rg(group_name, credentials, subscription_id):
self.log_debug("Removing RG: %s" % group_name)
self.log_info("Removing RG: %s" % group_name)
resource_client.resource_groups.delete(group_name).wait()
else:
self.log_debug("RG: %s does not exist. Do not remove." % group_name)
self.log_info("RG: %s does not exist. Do not remove." % group_name)

# if it is the last VM delete the RG of the Inf
if last:
if self.get_rg("rg-%s" % vm.inf.id, credentials, subscription_id):
self.log_debug("Removing Inf. RG: %s" % "rg-%s" % vm.inf.id)
self.log_info("Removing Inf. RG: %s" % "rg-%s" % vm.inf.id)
resource_client.resource_groups.delete("rg-%s" % vm.inf.id)
else:
self.log_debug("RG: %s does not exist. Do not remove." % "rg-%s" % vm.inf.id)
self.log_info("RG: %s does not exist. Do not remove." % "rg-%s" % vm.inf.id)

except Exception as ex:
self.log_exception("Error terminating the VM")
Expand Down
Loading

0 comments on commit c574780

Please sign in to comment.