def delete_alarms(request): #ids = request.data.getlist('ids[]') err = False ids = request.data.get("ids[]") #LOG.info(ids) alarms = Alarm.objects.filter(id=ids) LOG.info("---------------- alarms are -------------" + str(alarms)) for alarm in alarms: LOG.info("----------------------------DELETE-------------------") LOG.info("---------------- alarm id is ------------------------" + str(alarm.alarm_ins_id)) rc = create_rc_by_instance(alarm) LOG.info("--------------- rc is ------------" + str(rc)) try: result = ceilometer.alarm_delete(rc, alarm.alarm_ins_id) alarm.delete() #LOG.info(ceilometer.alarm_list(rc)) except: traceback.print_exc() return Response({ 'success': False, "msg": _('Fail to Delete Alarm!') }) return Response({ 'success': True, "msg": _('Alarms have been deleted!') }, status=status.HTTP_201_CREATED)
def flavor_create(instance): assert instance def _generate_name(instance): name = u"%s.cpu-%s-ram-%s-disk-%s-core-%s-socket-%s" % ( settings.OS_NAME_PREFIX, instance.cpu, instance.memory, instance.sys_disk, instance.core, instance.socket) return name def _get_flavor_by_name(instance, name): rc = create_rc_by_instance(instance) flavor = None novaAdmin = get_nova_admin(instance) try: flavors = novaAdmin.flavors.list(rc) except Exception: flavors = [] raise if flavors is not None: for f in flavors: if f.name == name: flavor = f break return flavor begin = datetime.datetime.now() rc = create_rc_by_instance(instance) name = _generate_name(instance) flavor = _get_flavor_by_name(instance, name) metadata = { "hw:cpu_cores": instance.core, "hw:cpu_sockets": instance.socket } if flavor is None: try: LOG.info(u"Flavor not exist, create new, [%s][%s].", instance, name) novaadmin = get_nova_admin(instance) flavor = novaadmin.flavors.create(ram=instance.memory, name=name, vcpus=instance.cpu, disk=instance.sys_disk, is_public=True) flavor.set_keys(metadata) #LOG.info(flavor.get_keys(flavor)) except nova.nova_exceptions.Conflict: LOG.info(u"Flavor name conflict, [%s][%s].", instance, name) flavor = _get_flavor_by_name(instance, name) except: raise end = datetime.datetime.now() LOG.info(u"Flavor create end, [%s][%s], apply [%s] seconds.", instance, name, (end - begin).seconds) return flavor
def update(request): try: LOG.info("---------------------- UPDATE -----------------------") #LOG.info(request.data) resource_id = request.data.get("resource_id") has_resource = request.data.get("has_resource") resource_name = request.data.get("resource_name") alarmname = request.data.get("alarmname") #LOG.info("------------------ ALARM MODAL --------------------") update_alarm = Alarm.objects.get(id=request.data.get("id")) #LOG.info(update_alarm) rc = create_rc_by_instance(update_alarm) LOG.info("------------------ RC -----------------------------") #LOG.info(rc) #threshold_rule = update_alarm.threshold_rule #meter_name = update_alarm.meter_name #comparison_operator = update_alarm.comparison_operator #threshold = update_alarm.threshold #statistic = update_alarm.statistic #threshold_rule = ceilometer.make_threshold_rule(meter_name, comparison_operator, threshold, statistic = statistic) #threshold_rule['query'] = ceilometer.make_query(resource_id = resource_id) #LOG.info("------------------ THRESHOLD RULE -------------------------") #LOG.info(threshold_rule) #LOG.info("------------------ ORIGIN ALARM ---------------------------") #LOG.info(ceilometer.alarm_list(rc)) #LOG.info("------------------ UPDATED ALARM --------------------------") resource_list = ["resource_id::" + resource_id] LOG.info("------------ resource_list is -----------" + str(resource_list)) LOG.info("--------- alarmname is ------------" + str(alarmname)) name = alarmname + ";" + str(update_alarm.alarm_notification) + ";" + str(resource_id) LOG.info("888888") homeland = {'name':name, 'query': resource_list} LOG.info("*********** befor update the alarm ***********") try: #LOG.info(ceilometer.alarm_update_query(rc, update_alarm.alarm_ins_id, alarmname, threshold_rule)) ceilometer.alarm_update(rc, update_alarm.alarm_ins_id, **homeland) LOG.info("************ after update **************") except: traceback.print_exc() update_alarm.has_resource = has_resource update_alarm.resource_id = resource_id update_alarm.alarm_name = alarmname update_alarm.resource_name = resource_name update_alarm.save() #LOG.info("------------------ ALL ALARMS -----------------------------") #LOG.info(ceilometer.alarm_list(rc)) return Response( {'success': True, "msg": _('Alarm is updated successfully!')}, status=status.HTTP_201_CREATED) except Exception as e: return Response({"success": False, "msg": _( 'Failed to update alarm for unknown reason.')})
def _get_flavor_by_name(instance, name): rc = create_rc_by_instance(instance) flavor = None novaAdmin = get_nova_admin(instance) try: flavors = novaAdmin.flavors.list(rc) except Exception: flavors = [] raise if flavors is not None: for f in flavors: if f.name == name: flavor = f break return flavor
def instance_resize(request): ins = Instance.objects.get(id=request.data['id']) LOG.info(request.data) if ins.sys_disk > int(request.data['sys_disk']): return Response({ "success": False, "msg": "Disk too smal.Please enter a bigger disk size." }) if int(request.data['vcpu']) == ins.cpu and int( request.data['core']) == ins.core and int( request.data['socket']) == ins.socket and int( request.data['memory']) == ins.memory and int( request.data['sys_disk']) == ins.sys_disk: return Response({"success": False, "msg": "No change!"}) org_cpu = ins.cpu org_core = ins.core org_socket = ins.socket org_memory = ins.memory org_sys_disk = ins.sys_disk ins.cpu = request.data['vcpu'] ins.core = request.data['core'] ins.socket = request.data['socket'] ins.memory = request.data['memory'] ins.sys_disk = request.data['sys_disk'] ins.save() #ins_ser = InstanceSerializer(ins) #LOG.info(ins_ser.data) rc = create_rc_by_instance(ins) try: new_flavor = flavor_create(ins) instance = nova.server_get(rc, ins.uuid) nova.server_resize(rc, ins.uuid, new_flavor) ins.status = 14 ins.save() except Exception as e: #traceback.print_exc() ins.cpu = org_cpu ins.core = org_core ins.socket = org_socket ins.memory = org_memory ins.sys_disk = org_sys_disk ins.save() return Response({"success": False, "msg": str(e)}) #return Response(serializer.data) return Response({"success": True})
def delete_alarms(request): #ids = request.data.getlist('ids[]') err = False ids = request.data.get("ids[]") #LOG.info(ids) alarms = Alarm.objects.filter(id=ids) LOG.info("---------------- alarms are -------------" + str(alarms)) for alarm in alarms: LOG.info("----------------------------DELETE-------------------") LOG.info("---------------- alarm id is ------------------------" + str(alarm.alarm_ins_id)) rc = create_rc_by_instance(alarm) LOG.info("--------------- rc is ------------" + str(rc)) try: result = ceilometer.alarm_delete(rc, alarm.alarm_ins_id) alarm.delete() #LOG.info(ceilometer.alarm_list(rc)) except: traceback.print_exc() return Response({'success': False, "msg":_('Fail to Delete Alarm!')}) return Response({'success': True, "msg": _('Alarms have been deleted!')}, status=status.HTTP_201_CREATED)
def instance_verify_resize(request): ins = Instance.objects.get(id=request.data['id']) rc = create_rc_by_instance(ins) try: LOG.info(request.data['action']) instance = nova.server_get(rc, ins.uuid) if request.data['action'] == 'confirm': nova.server_confirm_resize(rc, ins.uuid) elif request.data['action'] == 'revert': nova.server_revert_resize(rc, ins.uuid) else: return Response({"success": False, "msg": "Wrong action!!!"}) time.sleep(3) instance = nova.server_get(rc, ins.uuid) ins.status = get_ins_status(instance) if ins.status == 99: LOG.info("--------------- WAIT --------------------") ins.status = 11 return Response({"success": False, "msg": "Verify timeout!"}) flavor = nova.flavor_get(rc, instance.flavor['id']) LOG.info(flavor.vcpus) LOG.info(flavor.ram) extra = nova.flavor_get_extras(rc, flavor.id, True) core = extra['hw:cpu_cores'] socket = extra['hw:cpu_sockets'] LOG.info(core) LOG.info(socket) ins.cpu = flavor.vcpus ins.memory = flavor.ram ins.core = core ins.socket = socket ins.status = get_ins_status(instance) ins.save() return Response({"success": True, "msg": "Verify resize!"}) except: traceback.print_exc() return Response({"success": False, "msg": "Fail to verify resize!"})
def update(request): try: LOG.info("---------------------- UPDATE -----------------------") #LOG.info(request.data) resource_id = request.data.get("resource_id") has_resource = request.data.get("has_resource") resource_name = request.data.get("resource_name") alarmname = request.data.get("alarmname") #LOG.info("------------------ ALARM MODAL --------------------") update_alarm = Alarm.objects.get(id=request.data.get("id")) #LOG.info(update_alarm) rc = create_rc_by_instance(update_alarm) LOG.info("------------------ RC -----------------------------") #LOG.info(rc) #threshold_rule = update_alarm.threshold_rule #meter_name = update_alarm.meter_name #comparison_operator = update_alarm.comparison_operator #threshold = update_alarm.threshold #statistic = update_alarm.statistic #threshold_rule = ceilometer.make_threshold_rule(meter_name, comparison_operator, threshold, statistic = statistic) #threshold_rule['query'] = ceilometer.make_query(resource_id = resource_id) #LOG.info("------------------ THRESHOLD RULE -------------------------") #LOG.info(threshold_rule) #LOG.info("------------------ ORIGIN ALARM ---------------------------") #LOG.info(ceilometer.alarm_list(rc)) #LOG.info("------------------ UPDATED ALARM --------------------------") resource_list = ["resource_id::" + resource_id] LOG.info("------------ resource_list is -----------" + str(resource_list)) LOG.info("--------- alarmname is ------------" + str(alarmname)) name = alarmname + ";" + str( update_alarm.alarm_notification) + ";" + str(resource_id) LOG.info("888888") homeland = {'name': name, 'query': resource_list} LOG.info("*********** befor update the alarm ***********") try: #LOG.info(ceilometer.alarm_update_query(rc, update_alarm.alarm_ins_id, alarmname, threshold_rule)) ceilometer.alarm_update(rc, update_alarm.alarm_ins_id, **homeland) LOG.info("************ after update **************") except: traceback.print_exc() update_alarm.has_resource = has_resource update_alarm.resource_id = resource_id update_alarm.alarm_name = alarmname update_alarm.resource_name = resource_name update_alarm.save() #LOG.info("------------------ ALL ALARMS -----------------------------") #LOG.info(ceilometer.alarm_list(rc)) return Response( { 'success': True, "msg": _('Alarm is updated successfully!') }, status=status.HTTP_201_CREATED) except Exception as e: return Response({ "success": False, "msg": _('Failed to update alarm for unknown reason.') })
def instance_action(user, instance_id, action): if action not in ALLOWED_INSTANCE_ACTIONS.keys(): return {"OPERATION_STATUS": OPERATION_FAILED, "status": "Unsupported action [%s]" % action} #instance = Instance.objects.get(pk=instance_id, user=user, deleted=False) instance = Instance.objects.get(pk=instance_id, deleted=False) # restoring instance can't do any action! if BackupItem.is_any_restoring(instance): return {"OPERATION_STATUS": OPERATION_FORBID, "MSG": _("Cannot operate this instance because it's in " "restore process."), "status": instance.status} if action in ('reboot', 'power_on'): for volume in instance.volume_set.all(): if BackupItem.is_any_restoring(volume): return {"OPERATION_STATUS": OPERATION_FAILED, "MSG": _("Cannot operate this instance because " "one volume attached to it is in " "restore process."), "status": instance.status} if action == 'terminate' and \ BackupItem.living.filter( resource_id=instance.id, resource_type=Instance.__name__).exists(): return {"OPERATION_STATUS": OPERATION_FAILED, "MSG": _("This instance have backup chains, please delete " "these first."), "status": instance.status} if action == 'terminate': Order.disable_order_and_bills(instance) Operation.log(instance, obj_name=instance.name, action=action) if action == "vnc_console": return get_instance_vnc_console(instance) _ACTION_MAPPING = { "reboot": _server_reboot, "terminate": _server_delete, "power_on": _server_start, "power_off": _server_stop, "restore": _server_unpause, "pause": _server_pause, } try: rc = create_rc_by_instance(instance) act = _ACTION_MAPPING.get(action) act(rc, instance) instance.status = INSTANCE_ACTION_NEXT_STATE[action] instance.save() instance_status_synchronize_task.delay(instance, action) except Exception as ex: LOG.exception("Instance action [%s] raise exception, [%s].", action, instance) return {"OPERATION_STATUS": OPERATION_FAILED, "status": "%s" % ex.message} else: return {"OPERATION_STATUS": OPERATION_SUCCESS, "status": instance.status}