def vmcapture(self, obj_attr_list): ''' TBD ''' try: _status = 100 _fmsg = "An error has occurred, but no error message was captured" _time_mark_crs = int(time()) if obj_attr_list["captured_image_name"] == "auto": obj_attr_list["captured_image_name"] = obj_attr_list[ "name"] + "_at_" + str(_time_mark_crs) obj_attr_list[ "mgt_102_capture_request_sent"] = _time_mark_crs - obj_attr_list[ "mgt_101_capture_request_originated"] self.common_messages("VM", obj_attr_list, "capturing", 0, '') _img_uuid = self.generate_random_uuid( obj_attr_list["captured_image_name"]) _img_name = obj_attr_list["captured_image_name"] _vmc_attr_list = self.osci.get_object(obj_attr_list["cloud_name"], "VMC", False, obj_attr_list["vmc"], False) _map_uuid_to_name = str2dic(_vmc_attr_list["images_uuid2name"]) _map_name_to_uuid = str2dic(_vmc_attr_list["images_name2uuid"]) _map_uuid_to_name[_img_uuid] = _img_name _map_name_to_uuid[_img_name] = _img_uuid self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ obj_attr_list["vmc"], \ False, \ "images_uuid2name", \ dic2str(_map_uuid_to_name), \ False) self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ obj_attr_list["vmc"], \ False, \ "images_name2uuid", \ dic2str(_map_name_to_uuid), \ False) _time_mark_crc = int(time()) obj_attr_list[ "mgt_103_capture_request_completed"] = _time_mark_crc - _time_mark_crs _status = 0 except CldOpsException, obj: _status = obj.status _fmsg = str(obj.msg)
def imgdelete(self, obj_attr_list): ''' TBD ''' try: _status = 100 _fmsg = "An error has occurred, but no error message was captured" self.common_messages("IMG", obj_attr_list, "deleting", 0, '') _img_name = obj_attr_list["name"] _vmc_attr_list = self.osci.get_object(obj_attr_list["cloud_name"], "VMC", True, obj_attr_list["vmc_name"], False) _map_uuid_to_name = str2dic(_vmc_attr_list["images_uuid2name"]) _map_name_to_uuid = str2dic(_vmc_attr_list["images_name2uuid"]) if _img_name in _map_name_to_uuid: _img_uuid = _map_name_to_uuid[_img_name] obj_attr_list["boot_volume_imageid1"] = _img_uuid del _map_name_to_uuid[_img_name] del _map_uuid_to_name[_img_uuid] self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ _vmc_attr_list["uuid"], \ False, \ "images_uuid2name", \ dic2str(_map_uuid_to_name), \ False) self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ _vmc_attr_list["uuid"], \ False, \ "images_name2uuid", \ dic2str(_map_name_to_uuid), \ False) _status = 0 except CldOpsException as obj: _status = obj.status _fmsg = str(obj.msg) except Exception as e: _status = 23 _fmsg = str(e) finally: _status, _msg = self.common_messages("IMG", obj_attr_list, "deleted", _status, _fmsg) return _status, _msg
def vmcapture(self, obj_attr_list) : ''' TBD ''' try : _status = 100 _fmsg = "An error has occurred, but no error message was captured" _time_mark_crs = int(time()) if obj_attr_list["captured_image_name"] == "auto" : obj_attr_list["captured_image_name"] = obj_attr_list["name"] + "_at_" + str(_time_mark_crs) obj_attr_list["mgt_102_capture_request_sent"] = _time_mark_crs - obj_attr_list["mgt_101_capture_request_originated"] self.common_messages("VM", obj_attr_list, "capturing", 0, '') _img_uuid = self.generate_random_uuid(obj_attr_list["captured_image_name"]) _img_name = obj_attr_list["captured_image_name"] _vmc_attr_list = self.osci.get_object(obj_attr_list["cloud_name"], "VMC", False, obj_attr_list["vmc"], False) _map_uuid_to_name = str2dic(_vmc_attr_list["images_uuid2name"]) _map_name_to_uuid = str2dic(_vmc_attr_list["images_name2uuid"]) _map_uuid_to_name[_img_uuid] = _img_name _map_name_to_uuid[_img_name] = _img_uuid self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ obj_attr_list["vmc"], \ False, \ "images_uuid2name", \ dic2str(_map_uuid_to_name), \ False) self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ obj_attr_list["vmc"], \ False, \ "images_name2uuid", \ dic2str(_map_name_to_uuid), \ False) _time_mark_crc = int(time()) obj_attr_list["mgt_103_capture_request_completed"] = _time_mark_crc - _time_mark_crs _status = 0 except CldOpsException, obj : _status = obj.status _fmsg = str(obj.msg)
def check_images(self, vmc_name, vm_templates, vmc_defaults, vm_defaults): ''' TBD ''' self.common_messages("IMG", {"name": vmc_name}, "checking", 0, '') _map_name_to_id = {} _map_uuid_to_name = {} _registered_imageid_list = [] if True: for _vm_role in vm_templates.keys(): _imageid = str2dic(vm_templates[_vm_role])["imageid1"] if _imageid != "to_replace": if not self.is_cloud_image_uuid(_imageid): if _imageid in _map_name_to_id: vm_templates[_vm_role] = vm_templates[ _vm_role].replace(_imageid, _map_name_to_id[_imageid]) else: _map_name_to_id[ _imageid] = self.generate_random_uuid(_imageid) _map_uuid_to_name[ _map_name_to_id[_imageid]] = _imageid vm_templates[_vm_role] = vm_templates[ _vm_role].replace(_imageid, _map_name_to_id[_imageid]) if _map_name_to_id[ _imageid] not in _registered_imageid_list: _registered_imageid_list.append( _map_name_to_id[_imageid]) else: if _imageid not in _registered_imageid_list: _registered_imageid_list.append(_imageid) _map_name_to_id["baseimg"] = self.generate_random_uuid("baseimg") _map_uuid_to_name[self.generate_random_uuid("baseimg")] = "baseimg" _detected_imageids = self.base_check_images(vmc_name, vm_templates, _registered_imageid_list, _map_uuid_to_name, vm_defaults) if "images_uuid2name" not in vmc_defaults: vmc_defaults["images_uuid2name"] = dic2str(_map_uuid_to_name) if "images_name2uuid" not in vmc_defaults: vmc_defaults["images_name2uuid"] = dic2str(_map_name_to_id) return _detected_imageids
def imgdelete(self, obj_attr_list) : ''' TBD ''' try : _status = 100 _fmsg = "An error has occurred, but no error message was captured" self.common_messages("IMG", obj_attr_list, "deleting", 0, '') _img_name = obj_attr_list["name"] _vmc_attr_list = self.osci.get_object(obj_attr_list["cloud_name"], "VMC", True, obj_attr_list["vmc_name"], False) _map_uuid_to_name = str2dic(_vmc_attr_list["images_uuid2name"]) _map_name_to_uuid = str2dic(_vmc_attr_list["images_name2uuid"]) if _img_name in _map_name_to_uuid : _img_uuid = _map_name_to_uuid[_img_name] obj_attr_list["boot_volume_imageid1"] = _img_uuid del _map_name_to_uuid[_img_name] del _map_uuid_to_name[_img_uuid] self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ _vmc_attr_list["uuid"], \ False, \ "images_uuid2name", \ dic2str(_map_uuid_to_name), \ False) self.osci.update_object_attribute(obj_attr_list["cloud_name"], \ "VMC", \ _vmc_attr_list["uuid"], \ False, \ "images_name2uuid", \ dic2str(_map_name_to_uuid), \ False) _status = 0 except CldOpsException, obj : _status = obj.status _fmsg = str(obj.msg)
def check_images(self, vmc_name, vm_templates, vmc_defaults) : ''' TBD ''' self.common_messages("IMG", { "name": vmc_name }, "checking", 0, '') _map_name_to_id = {} _map_uuid_to_name = {} _registered_imageid_list = [] if True : for _vm_role in vm_templates.keys() : _imageid = str2dic(vm_templates[_vm_role])["imageid1"] if _imageid != "to_replace" : if not self.is_cloud_image_uuid(_imageid) : if _imageid in _map_name_to_id : vm_templates[_vm_role] = vm_templates[_vm_role].replace(_imageid, _map_name_to_id[_imageid]) else : _map_name_to_id[_imageid] = self.generate_random_uuid(_imageid) _map_uuid_to_name[_map_name_to_id[_imageid]] = _imageid vm_templates[_vm_role] = vm_templates[_vm_role].replace(_imageid, _map_name_to_id[_imageid]) if _map_name_to_id[_imageid] not in _registered_imageid_list : _registered_imageid_list.append(_map_name_to_id[_imageid]) else : if _imageid not in _registered_imageid_list : _registered_imageid_list.append(_imageid) _map_name_to_id["baseimg"] = self.generate_random_uuid("baseimg") _map_uuid_to_name[self.generate_random_uuid("baseimg")] = "baseimg" _detected_imageids = self.base_check_images(vmc_name, vm_templates, _registered_imageid_list, _map_uuid_to_name) if "images_uuid2name" not in vmc_defaults : vmc_defaults["images_uuid2name"] = dic2str(_map_uuid_to_name) if "images_name2uuid" not in vmc_defaults : vmc_defaults["images_name2uuid"] = dic2str(_map_name_to_id) return _detected_imageids
def take_action_if_requested(self, obj_type, obj_attr_list, current_step): ''' TBD ''' if "staging" not in obj_attr_list : return if not obj_attr_list["staging"].count(current_step) : return if obj_attr_list["staging"] + "_complete" in obj_attr_list : return try : _status = 100 _fmsg = "An error has occurred, but no error message was captured" if obj_attr_list["staging"] == "pause_" + current_step : if current_step == "provision_originated" : obj_attr_list["last_known_state"] = "waiting for signal" _max_tries = int(obj_attr_list["update_attempts"]) _wait = int(obj_attr_list["update_frequency"]) # Always subscribe for the VM channel, no matter the object _sub_channel = self.osci.subscribe(obj_attr_list["cloud_name"], "VM", "staging", _max_tries * _wait) if obj_type == "VM" and obj_attr_list["ai"] != "none" and current_step.count("all_vms") : _target_uuid = obj_attr_list["ai"] _target_name = obj_attr_list["ai_name"] _cloud_vm_uuid = obj_attr_list["cloud_vm_uuid"] else : _target_uuid = obj_attr_list["uuid"] _target_name = obj_attr_list["name"] _cloud_vm_uuid = _target_uuid self.osci.publish_message(obj_attr_list["cloud_name"], \ obj_type, \ "staging", \ _target_uuid + ";vmready;" + dic2str(obj_attr_list),\ 1, \ 3600) _msg = obj_type + ' ' + _cloud_vm_uuid + " (" _msg += _target_name + ") pausing on attach for continue signal ...." cbdebug(_msg, True) for _message in _sub_channel.listen() : _args = str(_message["data"]).split(";") if len(_args) != 3 : # cbdebug("Message is not for me: " + str(_args)) continue _id, _status, _info = _args if (_id == _target_uuid or _id == _target_name) and _status == "continue" : obj_attr_list[obj_attr_list["staging"] + "_complete"] = int(time()) if _info.count(":") : _add_obj_attr_list = str2dic(_info) obj_attr_list.update(_add_obj_attr_list) _status = 0 break _sub_channel.unsubscribe() _status = 0 elif obj_attr_list["staging"] == "execute_" + current_step : if current_step == "provision_originated" : obj_attr_list["last_known_state"] = "about to execute script" _proc_man = ProcessManagement(username = obj_attr_list["username"], \ cloud_name = obj_attr_list["cloud_name"]) _json_contents = copy.deepcopy(obj_attr_list) if obj_type == "AI" : _json_contents["vms"] = {} if "vms" in obj_attr_list and current_step != "deprovision_finished" : _vm_id_list = obj_attr_list["vms"].split(',') for _vm_id in _vm_id_list : _vm_uuid = _vm_id.split('|')[0] _vm_attr_list = self.osci.get_object(obj_attr_list["cloud_name"], "VM", False, _vm_uuid, False) _json_contents["vms"][_vm_attr_list["uuid"]] = _vm_attr_list obj_attr_list["execute_json_filename"] = "/tmp/" obj_attr_list["execute_json_filename"] += obj_attr_list["execute_json_filename_prefix"] obj_attr_list["execute_json_filename"] += "_vapp_" + obj_attr_list["cloud_name"] obj_attr_list["execute_json_filename"] += "_" + obj_attr_list["name"] + "_" obj_attr_list["execute_json_filename"] += obj_attr_list["uuid"] + ".json" _json_fh = open(obj_attr_list["execute_json_filename"], 'w') _json_fh.write(json.dumps(_json_contents, sort_keys = True, indent = 4)) _json_fh.close() _msg = "JSON contents written to " _msg += obj_attr_list["execute_json_filename"] + '.' cbdebug(_msg, True) _cmd = obj_attr_list["execute_script_name"] + ' ' _cmd += obj_attr_list["execute_json_filename"] _status, _result_stdout, _result_stderr = _proc_man.run_os_command(_cmd) _msg = "Command \"" + _cmd + "\" executed, with return code " + str(_status) cbdebug(_msg, True) obj_attr_list[obj_attr_list["staging"] + "_stdout"] = _result_stdout obj_attr_list[obj_attr_list["staging"] + "_stderr"] = _result_stderr obj_attr_list[obj_attr_list["staging"] + "_complete"] = int(time()) except self.osci.ObjectStoreMgdConnException, obj : _status = obj.status _fmsg = str(obj.msg)
def take_action_if_requested(self, obj_type, obj_attr_list, current_step): ''' TBD ''' if "staging" not in obj_attr_list: return if not obj_attr_list["staging"].count(current_step): return if obj_attr_list["staging"] + "_complete" in obj_attr_list: return try: _status = 100 _fmsg = "An error has occurred, but no error message was captured" if obj_attr_list["staging"] == "pause_" + current_step: _max_tries = int(obj_attr_list["update_attempts"]) _wait = int(obj_attr_list["update_frequency"]) # Always subscribe for the VM channel, no matter the object _sub_channel = self.osci.subscribe(obj_attr_list["cloud_name"], "VM", "staging", _max_tries * _wait) if obj_type == "VM" and obj_attr_list[ "ai"] != "none" and current_step.count("all_vms"): _target_uuid = obj_attr_list["ai"] _target_name = obj_attr_list["ai_name"] _cloud_vm_uuid = obj_attr_list["cloud_vm_uuid"] else: _target_uuid = obj_attr_list["uuid"] _target_name = obj_attr_list["name"] _cloud_vm_uuid = _target_uuid self.osci.publish_message(obj_attr_list["cloud_name"], \ obj_type, \ "staging", \ _target_uuid + ";vmready;" + dic2str(obj_attr_list),\ 1, \ 3600) _msg = obj_type + ' ' + _cloud_vm_uuid + " (" _msg += _target_name + ") pausing on attach for continue signal ...." cbdebug(_msg, True) for _message in _sub_channel.listen(): _args = str(_message["data"]).split(";") if len(_args) != 3: # cbdebug("Message is not for me: " + str(_args)) continue _id, _status, _info = _args if (_id == _target_uuid or _id == _target_name) and _status == "continue": obj_attr_list[obj_attr_list["staging"] + "_complete"] = int(time()) if _info.count(":"): _add_obj_attr_list = str2dic(_info) obj_attr_list.update(_add_obj_attr_list) _status = 0 break _sub_channel.unsubscribe() _status = 0 elif obj_attr_list["staging"] == "execute_" + current_step: _proc_man = ProcessManagement(username = obj_attr_list["username"], \ cloud_name = obj_attr_list["cloud_name"]) _json_contents = copy.deepcopy(obj_attr_list) if obj_type == "AI": _json_contents["vms"] = {} _vm_id_list = obj_attr_list["vms"].split(',') for _vm_id in _vm_id_list: _vm_uuid = _vm_id.split('|')[0] _vm_attr_list = self.osci.get_object( obj_attr_list["cloud_name"], "VM", False, _vm_uuid, False) _json_contents["vms"][ _vm_attr_list["uuid"]] = _vm_attr_list obj_attr_list["execute_json_filename"] = "/tmp/" obj_attr_list["execute_json_filename"] += obj_attr_list[ "execute_json_filename_prefix"] obj_attr_list[ "execute_json_filename"] += "_vapp_" + obj_attr_list[ "cloud_name"] obj_attr_list["execute_json_filename"] += "_" + obj_attr_list[ "name"] + "_" obj_attr_list[ "execute_json_filename"] += obj_attr_list["uuid"] + ".json" _json_fh = open(obj_attr_list["execute_json_filename"], 'w') _json_fh.write( json.dumps(_json_contents, sort_keys=True, indent=4)) _json_fh.close() _msg = "JSON contents written to " _msg += obj_attr_list["execute_json_filename"] + '.' cbdebug(_msg, True) _cmd = obj_attr_list["execute_script_name"] + ' ' _cmd += obj_attr_list["execute_json_filename"] _status, _result_stdout, _result_stderr = _proc_man.run_os_command( _cmd) _msg = "Command \"" + _cmd + "\" executed, with return code " + str( _status) cbdebug(_msg, True) obj_attr_list[obj_attr_list["staging"] + "_complete"] = int(time()) except self.osci.ObjectStoreMgdConnException, obj: _status = obj.status _fmsg = str(obj.msg)