Esempio n. 1
0
def reboot(instance):
    rc = create_rc_by_instance(instance)
    server = instance_get(instance)
    if server:
        nova.server_reboot(rc, instance.uuid)

    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        server = instance_get(instance)
        status = server.status.upper() if server else "None"
        LOG.info(
            "Instance action [reboot] synchronize status, "
            "[Count:%s][%s][status: %s].", count, instance, status)

        if status == u"ACTIVE":
            instance.status = INSTANCE_STATE_RUNNING
            instance.save()
            break

        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            break

    return True
Esempio n. 2
0
def instance_create(instance, password):
    user_data_format = "#cloud-config\npassword: %s\nchpasswd: { expire: False }\nssh_pwauth: True\n"
    user_data = user_data_format % password
    rc = create_rc_by_instance(instance)
    try:
        if instance.image.os_type ==  LINUX:
            server = nova.server_create(rc,
                    name=instance.name,
                    image=instance.image.uuid,
                    flavor=instance.flavor_id,
                    key_name=None,
                    security_groups=[],
                    nics = [{"net-id": instance.network.network_id, "v4-fixed-ip": ""}],
                    user_data = user_data
                ) 
        elif instance.image.os_type == WINDOWS:
            server = nova.server_create(rc,
                    name=instance.name,
                    image=instance.image.uuid,
                    flavor=instance.flavor_id,
                    key_name=None,
                    user_data=None,
                    security_groups=[],
                    nics = [{"net-id": instance.network.network_id, "v4-fixed-ip": ""}],
                    meta = {"admin_pass": password},
                )
        else:
            raise Exception("unknown image os type.")
        return server
    except Exception as e:
        LOG.exception(e)
        return False
Esempio n. 3
0
def _server_delete(instance):
    rc = create_rc_by_instance(instance)
    server = instance_get(instance)
    
    if server:
        nova.server_delete(rc, instance.uuid)
    
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        server = instance_get(instance)
        status = server.status.upper() if server else "None"
        LOG.info("Instance action [reboot] synchronize status, "
                "[Count:%s][%s][status: %s].",
                 count, instance, status)

        if server is None:
            instance.status = INSTANCE_STATE_DELETE
            instance.terminate_date = datetime.datetime.now()
            instance.deleted = True
            instance.save()
            instance_deleted_release_resource(instance)
            break

        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            break
   
    return True
Esempio n. 4
0
def _server_delete(instance):
    rc = create_rc_by_instance(instance)
    server = instance_get(instance)

    if server:
        nova.server_delete(rc, instance.uuid)

    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        server = instance_get(instance)
        status = server.status.upper() if server else "None"
        LOG.info(
            "Instance action [reboot] synchronize status, "
            "[Count:%s][%s][status: %s].", count, instance, status)

        if server is None:
            instance.status = INSTANCE_STATE_DELETE
            instance.terminate_date = datetime.datetime.now()
            instance.deleted = True
            instance.save()
            instance_deleted_release_resource(instance)
            break

        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            break

    return True
Esempio n. 5
0
def instance_get_vnc_console(instance):
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_vnc_console(rc, instance_id=instance.uuid)
    except Exception as e:
        LOG.exception(e)
        return None
Esempio n. 6
0
def instance_get_console_log(instance, tail_length=None):
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_console_output(rc, instance.uuid, tail_length=tail_length)
    except Exception as e:
        LOG.exception(e)
        return None
Esempio n. 7
0
def reboot(instance):
    rc = create_rc_by_instance(instance)
    server = instance_get(instance)
    if server:
        nova.server_reboot(rc, instance.uuid) 
    
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        server = instance_get(instance)
        status = server.status.upper() if server else "None"
        LOG.info("Instance action [reboot] synchronize status, "
                "[Count:%s][%s][status: %s].",
                 count, instance, status)

        if status == u"ACTIVE":
            instance.status = INSTANCE_STATE_RUNNING
            instance.save()
            break

        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            break

    return True
Esempio n. 8
0
def instance_get_vnc_console(instance):
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_vnc_console(rc, instance_id=instance.uuid)
    except Exception as e:
        LOG.exception(e)
        return None
Esempio n. 9
0
def flavor_delete(instance):
    rc = create_rc_by_instance(instance)
    try:
        nova.flavor_delete(rc, instance.flavor_id)
        return True
    except Exception as e:
        LOG.exception(e)
        return False
Esempio n. 10
0
def flavor_delete(instance):
    rc = create_rc_by_instance(instance)
    try:
        nova.flavor_delete(rc, instance.flavor_id)
        return True
    except Exception as e:
        LOG.exception(e)
        return False
Esempio n. 11
0
def instance_get(instance):
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_get(rc, instance.uuid)
    except nova.nova_exceptions.NotFound as e:
        return None
    except Exception as e:
        LOG.exception(e)
        return None
Esempio n. 12
0
def volume_attach_or_detach_task(instance, volume, action, **kwargs):

    if not instance:
        return False
    if not volume:
        return False
    rc = create_rc_by_instance(instance)
    LOG.info("volume attach or detach operation,instance: [%s][%s] - volume: [%s][%s] - action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))

    '''
    action ==‘attach’ attach volume ,update local volume status ,create VolumeAttachment;
    action =='detach' detach volume ,update local volume status ,delete VolumeAttachment
    '''
    if 'attach' == action:
        nova.instance_volume_attach(rc, volume_id=volume.volume_id, instance_id=instance.uuid, device='')
        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)

            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            if cinder and cinder.status.upper() == u'IN-USE':
                volume.status = VOLUME_STATE_IN_USE
                volume.instance = instance
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    elif 'detach' == action:
        nova.instance_volume_detach(rc, instance_id=instance.uuid, att_id=volume.volume_id)

        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)

            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            if cinder and cinder.status.upper() == u'AVAILABLE':
                volume.status = VOLUME_STATE_AVAILABLE
                volume.instance = None
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    return True
Esempio n. 13
0
def instance_get(instance):
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_get(rc, instance.uuid)
    except nova.nova_exceptions.NotFound as e:
        return None
    except Exception as e:
        LOG.exception(e)
        return None
Esempio n. 14
0
def instance_get_port(instance):
    assert instance
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    try:
        return nova.instance_interface_list(rc, instance.uuid)
    except nova.nova_exceptions.NotFound:
        return None
Esempio n. 15
0
def instance_get(instance):
    assert instance
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    try:
        return nova.server_get(rc, instance.uuid)
    except nova.nova_exceptions.NotFound:
        return None
Esempio n. 16
0
def instance_get(instance):
    assert instance
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    try:
        return nova.server_get(rc, instance.uuid)
    except nova.nova_exceptions.NotFound:
        return None
Esempio n. 17
0
def instance_get_port(instance):
    assert instance
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    try:
        return nova.instance_interface_list(rc, instance.uuid)
    except nova.nova_exceptions.NotFound:
        return None
Esempio n. 18
0
def instance_get_vnc_console(instance):
    assert instance
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    try:
        return nova.server_vnc_console(rc, instance_id=instance.uuid)
    except Exception:
        LOG.exception("Failed to get vnc console, [%s].", instance)
        return None
Esempio n. 19
0
def instance_get_vnc_console(instance):
    assert instance
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    try:
        return nova.server_vnc_console(rc, instance_id=instance.uuid)
    except Exception:
        LOG.exception("Failed to get vnc console, [%s].", instance)
        return None
Esempio n. 20
0
def instance_get_console_log(instance, tail_length=None):
    assert instance
    if instance.uuid is None:
        return None
    
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_console_output(rc, instance.uuid,
                                          tail_length=tail_length)
    except Exception:
        LOG.exception("Failed to get console output, [%s].", instance)
        return None
Esempio n. 21
0
def instance_get_console_log(instance, tail_length=None):
    assert instance
    if instance.uuid is None:
        return None
    
    rc = create_rc_by_instance(instance)
    try:
        return nova.server_console_output(rc, instance.uuid,
                                          tail_length=tail_length)
    except Exception:
        LOG.exception("Failed to get console output, [%s].", instance)
        return None
Esempio n. 22
0
def flavor_create(instance):
    assert instance

    def _generate_name(instance):
        name = u"%s.cpu-%s-ram-%s-disk-%s" % (settings.OS_NAME_PREFIX,
                                              instance.cpu, instance.memory,
                                              instance.sys_disk)
        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

    LOG.info(u"Flavor create start, [%s].", instance)
    begin = datetime.datetime.now()
    rc = create_rc_by_instance(instance)
    name = _generate_name(instance)
    flavor = _get_flavor_by_name(instance, name)

    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)
        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
Esempio n. 23
0
def instance_get_vnc_console(instance):
    assert instance
    LOG.info("*** start to get novnc ****")
    if instance.uuid is None:
        return None

    rc = create_rc_by_instance(instance)
    LOG.info("  rc is " + str(rc))
    try:
        return nova.server_vnc_console(rc, instance_id=instance.uuid)
    except Exception:
        LOG.exception("Failed to get vnc console, [%s].", instance)
        return None
Esempio n. 24
0
def alarm_create_task(alarm, **kwargs):
    assert alarm
    #monitor.save()
    LOG.info("----------------------  CREATE ALARM -----------------")
    rc = create_rc_by_instance(alarm)
    LOG.info("************ rc is *************" + str(rc))
    #LOG.info(monitor)
    #LOG.info(monitor.name)
    #LOG.info(monitor.meter_name)
    #LOG.info(monitor.comparison_operator)
    #LOG.info(monitor.threshold)
    #LOG.info(rc)
    client = ceilometer.ceilometerclient(rc)
    LOG.info("***********" + str(alarm))
    alarm_id = alarm.id
    LOG.info("***********")
    name = alarm.alarmname
    notification = alarm.alarm_notification
    LOG.info("***********" + str(name))
    comparison_operator = alarm.comparison_operator
    LOG.info("***********" + str(comparison_operator))
    threshold = alarm.threshold
    LOG.info("***********" + str(threshold))
    meter_name = alarm.meter_name
    LOG.info("***********" + str(meter_name))
    severity = alarm.severity
    LOG.info("***********" + str(severity))
    alarm_actions = "http://192.168.223.108:8081" 
    LOG.info("***********")
    statistic = alarm.statistic
    LOG.info("*********** statistic is " + str(statistic))
    description = "test"
    project_id = rc['tenant_uuid']
    LOG.info("******** after get project_id *********")
    period = 60
    name = name + ";" + notification
    LOG.info("******** new alarm name is ************" + str(name))
    meta = {"alarm_actions": [settings.ALARM_ACTIONS], "severity": str(severity), "description": "test", "enabled": True, "threshold_rule": {"threshold": int(threshold), "meter_name": meter_name, "period": 60, "statistic": statistic}, "repeat_actions": False, "type": "threshold", "name": name, "comparison_operator": comparison_operator}
    LOG.info("************* befor create alarm ************")
    
    try:
        alarm_to_create = client.alarms.create(**meta)
        LOG.info("************ after alarm create **************")
    except:
        raise
    alarm.alarm_ins_id = alarm_to_create.alarm_id
    #LOG.info(alarm.alarm_ins_id)
    alarm.save()
#    except:
#	traceback.print_exc()
    return True
Esempio n. 25
0
def flavor_create(instance=None):
    rc = create_rc_by_instance(instance)
    try:
        flavor = nova.flavor_create(rc,
                                 name="flavor-%04d%04d" % (
                                     instance.user.id, instance.id),
                                 memory=instance.memory,
                                 vcpu=instance.cpu,
                                 disk=instance.sys_disk,
                                 is_public=False)
        return flavor
    except Exception as e:
        LOG.exception(e)
        return False
Esempio n. 26
0
def flavor_create(instance=None):
    rc = create_rc_by_instance(instance)
    try:
        flavor = nova.flavor_create(rc,
                                 name="flavor-%04d%04d" % (
                                     instance.user.id, instance.id),
                                 memory=instance.memory,
                                 vcpu=instance.cpu,
                                 disk=instance.sys_disk,
                                 is_public=False)
        return flavor
    except Exception as e:
        LOG.exception(e)
        return False
Esempio n. 27
0
def flavor_create(instance):
    assert instance  
    def _generate_name(instance):
        name = u"%s.cpu-%s-ram-%s-disk-%s" % (settings.OS_NAME_PREFIX,
                    instance.cpu, instance.memory, instance.sys_disk)
        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

    LOG.info(u"Flavor create start, [%s].", instance)
    begin = datetime.datetime.now()
    rc = create_rc_by_instance(instance)
    name = _generate_name(instance)
    flavor = _get_flavor_by_name(instance, name)


    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)
        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
Esempio n. 28
0
    def _get_flavor_by_name(instance, name):
        rc = create_rc_by_instance(instance)
        flavor = None
        try:
            flavors = nova.flavor_list(rc)
        except Exception:
            flavors = []
            raise

        if flavors is not None:
            for f in flavors:
                if f.name == name:
                    flavor = f
                    break
        return flavor
Esempio n. 29
0
 def _get_flavor_by_name(instance, name):
     rc = create_rc_by_instance(instance)
     flavor = None
     try:
         flavors = nova.flavor_list(rc)
     except Exception:
         flavors = []
         raise
     
     if flavors is not None:
         for f in flavors:
             if f.name == name:
                 flavor = f
                 break
     return flavor
Esempio n. 30
0
def instance_create(instance, password):
    if instance.image.os_type not in (LINUX, WINDOWS):
        raise ValueError(u"Unknown image os type, [%s].", instance)

    user_data_format = "#cloud-config\n"\
                       "password: %s\n"\
                       "chpasswd: { expire: False }\n"\
                       "ssh_pwauth: True\n"
    user_data = user_data_format % password
    rc = create_rc_by_instance(instance)

    # nic is alias of Network interface card
    nics = None
    if neutron.is_neutron_enabled(rc):
        nics = [{"net-id": instance.network.network_id, "v4-fixed-ip": ""}]

    if instance.image.os_type == LINUX:
        server = nova.server_create(rc,
                                    name=instance.name,
                                    image=instance.image.uuid,
                                    flavor=instance.flavor_id,
                                    key_name=None,
                                    security_groups=[],
                                    nics=nics,
                                    user_data=user_data)
    else:
        new_pwd = []
        for c in password:
            if c in ["&", "|", "(", ")", "<", ">", "^"]:
                new_pwd.append("^")
            new_pwd.append(c)
        password = "".join(new_pwd)
        LOG.info(u"Windows complexity password, [%s][%s].", instance, password)
        server = nova.server_create(rc,
                                    name=instance.name,
                                    image=instance.image.uuid,
                                    flavor=instance.flavor_id,
                                    key_name=None,
                                    user_data=None,
                                    security_groups=[],
                                    nics=nics,
                                    meta={"admin_pass": password})
    return server
Esempio n. 31
0
def instance_create(instance, password):
    user_data_format = "#cloud-config\npassword: %s\nchpasswd: { expire: False }\nssh_pwauth: True\n"
    user_data = user_data_format % password
    rc = create_rc_by_instance(instance)
    try:
        nics = None
        if neutron.is_neutron_enabled(rc):
            nics = [{"net-id": instance.network.network_id, "v4-fixed-ip": ""}]
        
        if instance.image.os_type ==  LINUX:
            server = nova.server_create(rc,
                    name=instance.name,
                    image=instance.image.uuid,
                    flavor=instance.flavor_id,
                    key_name=None,
                    security_groups=[],
                    nics = nics,
                    user_data = user_data,
                ) 
        elif instance.image.os_type == WINDOWS:
            server = nova.server_create(rc,
                    name=instance.name,
                    image=instance.image.uuid,
                    flavor=instance.flavor_id,
                    key_name=None,
                    user_data=None,
                    security_groups=[],
                    nics = nics,
                    meta = {"admin_pass": password},
                )
        else:
            raise Exception("unknown image os type.")
        return server
    except Exception as e:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(e)
        return False
Esempio n. 32
0
def instance_create(instance, password):
    if instance.image.os_type not in (LINUX, WINDOWS):
        raise ValueError(u"Unknown image os type, [%s].", instance)

    user_data_format = "#cloud-config\n"\
                       "password: %s\n"\
                       "chpasswd: { expire: False }\n"\
                       "ssh_pwauth: True\n"
    user_data = user_data_format % password
    rc = create_rc_by_instance(instance)

    # nic is alias of Network interface card
    nics = None
    if neutron.is_neutron_enabled(rc):
        nics = [{"net-id": instance.network.network_id, "v4-fixed-ip": ""}]

    if instance.image.os_type == LINUX:
        server = nova.server_create(rc, name=instance.name,
                                    image=instance.image.uuid,
                                    flavor=instance.flavor_id, key_name=None,
                                    security_groups=[], nics=nics,
                                    user_data=user_data)
    else: 
        new_pwd = []
        for c in password:
            if c in ["&", "|", "(", ")", "<", ">", "^"]:
                new_pwd.append("^")
            new_pwd.append(c)
        password = "".join(new_pwd)
        LOG.info(u"Windows complexity password, [%s][%s].",
                        instance, password)
        server = nova.server_create(rc, name=instance.name,
                                    image=instance.image.uuid,
                                    flavor=instance.flavor_id,
                                    key_name=None, user_data=None,
                                    security_groups=[], nics=nics,
                                    meta={"admin_pass": password})
    return server
Esempio n. 33
0
def detach_volume_from_instance(volume):
    assert volume is not None
    assert volume.instance is not None

    instance = volume.instance
    rc = create_rc_by_instance(instance)
    LOG.info("Volume detach start, [%s][%s].", instance, volume)

    begin = datetime.datetime.now()
    try:
        nova.instance_volume_detach(rc, instance.uuid, volume.volume_id)
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception(
            "Volume detach api call failed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info(
            "Volume detach api call succeed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else 'None'

        end = datetime.datetime.now()

        if status == u'ERROR':
            LOG.info(
                "Volume detach failed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.change_status(VOLUME_STATE_ERROR)
            break
        elif status == u'AVAILABLE':
            LOG.info(
                "Volume detach succeed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.instance = None
            volume.change_status(VOLUME_STATE_AVAILABLE)
            break
        elif status == "None":
            LOG.info(
                "Volume detach volume is None, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.instance = None
            volume.delete = True
            volume.save()
            break
        else:
            LOG.info(
                "Volume detach synchronize status, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)

    else:
        end = datetime.datetime.now()
        LOG.info("Volume detach timeout, "
                 "[%s][%s], apply [%s] seconds.", instance, volume,
                 (end - begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume detach failed, "
                  "[%s][%s], apply [%s] seconds.", instance, volume,
                  (end - begin).seconds)
        return False
    else:
        return True
Esempio n. 34
0
def detach_volume_from_instance(volume):
    assert volume is not None
    assert volume.instance is not None

    instance = volume.instance
    rc = create_rc_by_instance(instance)
    LOG.info("Volume detach start, [%s][%s].",
             instance, volume)
    
    begin = datetime.datetime.now()
    try:
        nova.instance_volume_detach(rc, instance.uuid, volume.volume_id)
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception("Volume detach api call failed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info("Volume detach api call succeed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else 'None'

        end = datetime.datetime.now()
        
        if status == u'ERROR':
            LOG.info("Volume detach failed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)
            volume.change_status(VOLUME_STATE_ERROR)
            break
        elif status == u'AVAILABLE':
            LOG.info("Volume detach succeed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)
            volume.instance = None
            volume.change_status(VOLUME_STATE_AVAILABLE)
            break
        elif status == "None":
            LOG.info("Volume detach volume is None, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)
            volume.instance = None
            volume.delete = True
            volume.save()
            break
        else:
            LOG.info("Volume detach synchronize status, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count,instance, volume, status, (end-begin).seconds)

    else:
        end = datetime.datetime.now()
        LOG.info("Volume detach timeout, "
                    "[%s][%s], apply [%s] seconds.",
                    instance, volume, (end-begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume detach failed, "
                    "[%s][%s], apply [%s] seconds.",
                    instance, volume, (end-begin).seconds)
        return False
    else:
        return True
Esempio n. 35
0
def instance_create_task(instance, **kwargs):
    password = kwargs.get("password", None)
    if not instance or not password:
        return

    LOG.info('begin to start create instance:[%s][%s][pwd:%s]' % (
                    instance.id, instance.name, password))
     
    rc = create_rc_by_instance(instance)
    
    # create flavor
    flavor = flavor_create(instance)
    if not flavor:
        LOG.error('create flavor error!!! instance:[%s][%s]' % (
                    instance.id, instance.name))
        return
    
    instance.flavor_id = flavor.id
    LOG.info('flavor ok instance:[%s][%s]' % (instance.id, instance.name))

    neutron_enabled = neutron.is_neutron_enabled(rc)
    
    # get default private network
    if instance.network_id == 0 and neutron_enabled:
        network = create_default_private_network(instance)
        instance.network_id = network.id

    if not instance.firewall_group:
        f = Firewall.objects.filter(is_default=True,
                            user=instance.user,
                            user_data_center=instance.user_data_center,
                            deleted=False)
        if len(f) > 0:
            instance.firewall_group = f[0]

    instance.save() 
    LOG.info('network ok instance:[%s][%s]' % (instance.id, instance.name))
    
    # create instance  
    server = instance_create(instance, password)
    if not server:
        LOG.info('server create error instance:[%s][%s]' % (instance.id, instance.name))
        return

    instance.uuid = server.id
    instance.status = INSTANCE_STATE_BOOTING
    instance.save()

    count = 0
    while True:
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        srv = instance_get(instance)
        st = srv.status.upper()
        LOG.info('server status rsync instance:[%s][%s][status: %s]' % (
                                        instance.id, instance.name, st))
        if st == "ACTIVE":
            instance.status = INSTANCE_STATE_RUNNING
            try:
                if neutron_enabled:
                    private_net = "network-%s" % instance.network.id
                else:
                    private_net = "private"
                instance.private_ip = srv.addresses.\
                            get(private_net)[0].get("addr", "---")
            except Exception as ex:
                LOG.exception(ex)
                pass
            instance.save()
            count = settings.MAX_COUNT_SYNC + 1
        elif st == "ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            count = settings.MAX_COUNT_SYNC + 1
        elif st == "BUILD":
            pass
        
        count += 1
        if count > settings.MAX_COUNT_SYNC:
            break

    r = flavor_delete(instance)
    LOG.info("delete flavor instance:[%s][%s][result: %s]" % (instance.id, instance.name, r))
Esempio n. 36
0
def _server_pause(instance):
    rc = create_rc_by_instance(instance)
    server = instance_get(instance)
    if server:
        nova.server_pause(rc, instance.uuid)
Esempio n. 37
0
def attach_volume_to_instance(volume, instance):
    rc = create_rc_by_instance(instance)
    LOG.info("Volume attach start, [%s][%s].",
                    instance, volume)

    begin = datetime.datetime.now()
    try:
        nova.instance_volume_attach(rc, volume_id=volume.volume_id,
                                    instance_id=instance.uuid, device='')
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception("Volume attach api call failed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info("Volume attach api call succeed, [%s][%s], "
                      "apply [%s] seconds.",
                      instance, volume, (end-begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else "None"
        
        end = datetime.datetime.now()
        if status == u'ERROR':
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info("Volume attach failed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            break
        elif status == u'ERROR_ATTACHING':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info("Volume attach failed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            break
        elif status == u'IN-USE':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_IN_USE)
            LOG.info("Volume attach succeed, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            break
        elif status == "None":
            LOG.info("Volume attach failed, volume is None, "
                    "[Count:%s][%s][%s][status: %s], apply [%s] seconds.",
                    count, instance, volume, status, (end-begin).seconds)
            volume.instance = None
            volume.deleted = True
            volume.save()
            break 
        else:
             LOG.info("Volume attach synchronize status, "
                "[Count:%s][%s][%s][status: %s].",
                 count, instance, volume, status)
    else:
        end = datetime.datetime.now()
        LOG.info("Volume attach timeout, [%s][%s], apply [%s] seconds.",
                 instance, volume, (end-begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume attach failed, [%s][%s], apply [%s] seconds.",
                  instance, volume, (end-begin).seconds)
        return False
    else: 
        return True
Esempio n. 38
0
def volume_attach_or_detach_task(instance, volume, action, **kwargs):

    if not instance:
        return False
    if not volume:
        return False
    rc = create_rc_by_instance(instance)
    LOG.info("volume attach or detach operation,instance: [%s][%s] - volume: [%s][%s] - action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))

    '''
    action ==‘attach’ attach volume ,update local volume status ,create VolumeAttachment;
    action =='detach' detach volume ,update local volume status ,delete VolumeAttachment
    '''
    if 'attach' == action:
        nova.instance_volume_attach(rc, volume_id=volume.volume_id, instance_id=instance.uuid, device='')
        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)
            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            elif cinder and cinder.status.upper() == u'ERROR_ATTACHING':
                volume.status = VOLUME_STATE_ERROR
                volume.instance = instance
                volume.save()
                break
            elif cinder and cinder.status.upper() == u'IN-USE':
                volume.status = VOLUME_STATE_IN_USE
                volume.instance = instance
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    elif 'detach' == action:
        nova.instance_volume_detach(rc, instance_id=instance.uuid, att_id=volume.volume_id)

        count = 0
        while True:
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            cinder = volume_task.volume_get(volume)

            if cinder and cinder.status.upper() == u'ERROR':
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                break
            if cinder and cinder.status.upper() == u'AVAILABLE':
                volume.status = VOLUME_STATE_AVAILABLE
                volume.instance = None
                volume.save()
                break
            count += 1
            if count > settings.MAX_COUNT_SYNC:
                LOG.info("volume attach or detach,instance: [%s][%s] - volume: [%s][%s] - timeout for action:[%s]" % (instance.id, instance.name, volume.id, volume.name, action))
                break

    return True
Esempio n. 39
0
def instance_action_task(instance, action, **kwargs):
    LOG.info("instance: [%s][%s] - Start action [%s]" % (instance.id, instance.name, action))
    rc = create_rc_by_instance(instance)
    try:
        if action == INSTANCE_ACTIONS_DICT["POWER_OFF"]:
            nova.server_stop(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["POWER_ON"]:
            nova.server_start(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["REBOOT"]:
            nova.server_reboot(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["PAUSE"]:
            nova.server_pause(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["RESTORE"]:
            nova.server_unpause(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["TERMINATE"]:
            nova.server_delete(rc, instance.uuid)
        else:
            LOG.error("instance: [%s][%s] - unsupported action [%s]" % (instance.id, instance.name, action))
            return
    except:
        LOG.error("instance: [%s][%s] - error for action [%s] : %s" % (instance.id, instance.name, action,
                                                                     traceback.format_exc()))
        instance.status = INSTANCE_STATE_ERROR
        instance.save()

    count = 0
    while True:
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        server = instance_get(instance)

        if server and server.status == u'ERROR':
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            LOG.error("instance: [%s][%s] - error for action %s" % (instance.id, instance.name, action))
            break

        if server and server.status == u'ACTIVE':
            if action == INSTANCE_ACTIONS_DICT["POWER_ON"] or \
                action == INSTANCE_ACTIONS_DICT["REBOOT"] or \
                action == INSTANCE_ACTIONS_DICT["RESTORE"]:
                instance.status = INSTANCE_STATE_RUNNING
                instance.save()
                LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
                break

        if server and server.status == u'SHUTOFF':
            if action == INSTANCE_ACTIONS_DICT["POWER_OFF"]:
                instance.status = INSTANCE_STATE_POWEROFF
                instance.save()
                LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
                break

        if server and server.status == u'PAUSED':
            if action  == INSTANCE_ACTIONS_DICT["PAUSE"]:
                instance.status = INSTANCE_STATE_PAUSED
                instance.save()
                LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
                break
    
        if not server and action == INSTANCE_ACTIONS_DICT["TERMINATE"]:
            instance.status = INSTANCE_STATE_DELETE
            instance.deleted = 1
            instance.save()
            LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
            instance_deleted_release_resource(instance)
            break

        count += 1
        if count > settings.MAX_COUNT_SYNC:
            LOG.info("instance: [%s][%s] - timeout for action: [%s]" % (instance.id, instance.name, action))
            break

    return True
Esempio n. 40
0
def instance_create_task(instance, **kwargs):
    password = kwargs.get("password", None)
    assert instance
    assert password

    begin = datetime.datetime.now()
    LOG.info(u"Instance create start, [%s][pwd:%s].",
                        instance, password)
     
    rc = create_rc_by_instance(instance)
    try: 
        flavor = flavor_create(instance)
        instance.flavor_id = flavor.id 
    except Exception:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instance create failed by flavor exception, [%s].",
                        instance)
        return False

    neutron_enabled = neutron.is_neutron_enabled(rc)
    
    if neutron_enabled:
        network = make_sure_default_private_network(instance)
        instance.network_id = network.id
        instance.save() 
        LOG.info(u"Instance create set network passed, [%s][%s].",
                    instance, network)

    if not instance.firewall_group:
        instance.set_default_firewall()
 
    try:
        server = instance_create(instance, password)
    except Exception as ex:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instace create api call raise an exception, [%s][%s].",
                        instance, ex.message)
        return False
    else:
        status = server.status.upper() if server else u"None"
        instance.uuid = server.id
        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            end = datetime.datetime.now()
            LOG.info(u"Instance create api call failed, [%s][%s], "
                     "apply [%s] seconds.",
                    instance, status, (end-begin).seconds)
        else:
            instance.status = INSTANCE_STATE_BOOTING
            instance.save()
            end = datetime.datetime.now()
            LOG.info(u"Instance create api call passed, [%s][%s], "
                    "apply [%s] seconds.",
                    instance, status, (end-begin).seconds)
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            instance_create_sync_status_task.delay(
                instance, neutron_enabled, retry_count=1)
            billing_task.charge_resource(instance.id, Instance)

    return instance
Esempio n. 41
0
def alarm_create_task(alarm, **kwargs):
    assert alarm
    #monitor.save()
    LOG.info("----------------------  CREATE ALARM -----------------")
    rc = create_rc_by_instance(alarm)
    LOG.info("************ rc is *************" + str(rc))
    #LOG.info(monitor)
    #LOG.info(monitor.name)
    #LOG.info(monitor.meter_name)
    #LOG.info(monitor.comparison_operator)
    #LOG.info(monitor.threshold)
    #LOG.info(rc)
    client = ceilometer.ceilometerclient(rc)
    LOG.info("***********" + str(alarm))
    alarm_id = alarm.id
    LOG.info("***********")
    name = alarm.alarmname
    notification = alarm.alarm_notification
    LOG.info("***********" + str(name))
    comparison_operator = alarm.comparison_operator
    LOG.info("***********" + str(comparison_operator))
    threshold = alarm.threshold
    LOG.info("***********" + str(threshold))
    meter_name = alarm.meter_name
    LOG.info("***********" + str(meter_name))
    severity = alarm.severity
    LOG.info("***********" + str(severity))
    alarm_actions = "http://192.168.223.108:8081"
    LOG.info("***********")
    statistic = alarm.statistic
    LOG.info("*********** statistic is " + str(statistic))
    description = "test"
    project_id = rc['tenant_uuid']
    LOG.info("******** after get project_id *********")
    period = 60
    name = name + ";" + notification
    LOG.info("******** new alarm name is ************" + str(name))
    meta = {
        "alarm_actions": [settings.ALARM_ACTIONS],
        "severity": str(severity),
        "description": "test",
        "enabled": True,
        "threshold_rule": {
            "threshold": int(threshold),
            "meter_name": meter_name,
            "period": 60,
            "statistic": statistic
        },
        "repeat_actions": False,
        "type": "threshold",
        "name": name,
        "comparison_operator": comparison_operator
    }
    LOG.info("************* befor create alarm ************")

    try:
        alarm_to_create = client.alarms.create(**meta)
        LOG.info("************ after alarm create **************")
    except:
        raise
    alarm.alarm_ins_id = alarm_to_create.alarm_id
    #LOG.info(alarm.alarm_ins_id)
    alarm.save()
    #    except:
    #	traceback.print_exc()
    return True
Esempio n. 42
0
def instance_create_task(instance, **kwargs):
    LOG.info(
        "*************** I am instance create in instance_create_task ****************"
    )
    password = kwargs.get("password", None)
    assert instance
    assert password

    user_tenant_uuid = kwargs.get("user_tenant_uuid", None)

    LOG.info("**** user_tenant_uuid in instance_create_task is ****" +
             str(user_tenant_uuid))
    begin = datetime.datetime.now()
    LOG.info(u"Instance create start, [%s][pwd:%s].", instance, password)

    rc = create_rc_by_instance(instance)
    try:
        flavor = flavor_create(instance)
        instance.flavor_id = flavor.id
    except Exception:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instance create failed by flavor exception, [%s].",
                      instance)
        return False

    # First check if network exists or not.
    neutron_enabled = neutron.is_neutron_enabled(rc)
    LOG.info("********** check neutron is enabled or not **************" +
             str(neutron_enabled))

    # If not exists, create a new one for that tenant.
    if neutron_enabled:
        LOG.info("********** neutron_enabled *************")
        LOG.info(
            "********** start to make sure make_sure_default_private_network ***********"
        )
        network = make_sure_default_private_network(instance, rc,
                                                    user_tenant_uuid)
        #network = neutron.network_list_for_tenant(rc, tenant_id=user_tenant_uuid)
        #LOG.info("********** network is ******************" + str(network))
        #network_id = None
        #for net in network:
        #    LOG.info("***** net is *******" + str(net))
        #    network_id = net.id
        #LOG.info("********* network_id is *********" + str(network_id))
        LOG.info("**** network is ****" + str(network))
        instance.network_id = network.id
        instance.save()
        LOG.info(u"Instance create set network passed, [%s][%s].", instance,
                 network)

    if not instance.firewall_group:
        LOG.info("********** start to set default firewall ************")
        instance.set_default_firewall()

    try:
        LOG.info("********** start to create instance *****************")
        server = instance_create(instance, password)
    except Exception as ex:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instace create api call raise an exception, [%s][%s].",
                      instance, ex.message)
        return False
    else:
        status = server.status.upper() if server else u"None"
        instance.uuid = server.id
        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            end = datetime.datetime.now()
            LOG.info(
                u"Instance create api call failed, [%s][%s], "
                "apply [%s] seconds.", instance, status, (end - begin).seconds)
        else:
            instance.status = INSTANCE_STATE_BOOTING
            instance.save()
            end = datetime.datetime.now()
            LOG.info(
                u"Instance create api call passed, [%s][%s], "
                "apply [%s] seconds.", instance, status, (end - begin).seconds)
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            instance_create_sync_status_task.delay(instance,
                                                   neutron_enabled,
                                                   user_tenant_uuid,
                                                   rc,
                                                   retry_count=1)
            billing_task.charge_resource(instance.id, Instance)

    return instance
Esempio n. 43
0
def flavor_create(instance):
    assert instance  
    def _generate_name(instance):
        gpu = 'no'
        if instance.gpu == True:
            gpu = 'yes'
        else:
            gpu = 'no'
        name = u"%s.cpu-%s-ram-%s-disk-%s-core-%s-socket-%s-gpu-%s" % (settings.OS_NAME_PREFIX,
                    instance.cpu, instance.memory, instance.sys_disk, instance.core, instance.socket, gpu)
        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

    LOG.info(u"Flavor create start, [%s].", instance)
    begin = datetime.datetime.now()
    rc = create_rc_by_instance(instance)
    LOG.info("*** rc is ***" + str(rc))
    name = _generate_name(instance)
    flavor = _get_flavor_by_name(instance, name)
    metadata = {"hw:cpu_cores":int(instance.core),"hw:cpu_sockets":int(instance.socket)}
    if instance.gpu:
        #metadata['pci_passthrough:alias'] = ['alias_1:1']
        metadata['pci_passthrough:alias'] = settings.GPU
    LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii')
    LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii')
    LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii')
    LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii')
    LOG.info(metadata)
    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
Esempio n. 44
0
def instance_create_task(instance, **kwargs):
    password = kwargs.get("password", None)
    assert instance
    assert password

    begin = datetime.datetime.now()
    LOG.info(u"Instance create start, [%s][pwd:%s].", instance, password)

    rc = create_rc_by_instance(instance)
    try:
        flavor = flavor_create(instance)
        instance.flavor_id = flavor.id
    except Exception:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instance create failed by flavor exception, [%s].",
                      instance)
        return False

    neutron_enabled = neutron.is_neutron_enabled(rc)

    if neutron_enabled:
        network = make_sure_default_private_network(instance)
        instance.network_id = network.id
        instance.save()
        LOG.info(u"Instance create set network passed, [%s][%s].", instance,
                 network)

    if not instance.firewall_group:
        instance.set_default_firewall()

    try:
        server = instance_create(instance, password)
    except Exception as ex:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instace create api call raise an exception, [%s][%s].",
                      instance, ex.message)
        return False
    else:
        status = server.status.upper() if server else u"None"
        instance.uuid = server.id
        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            end = datetime.datetime.now()
            LOG.info(
                u"Instance create api call failed, [%s][%s], "
                "apply [%s] seconds.", instance, status, (end - begin).seconds)
        else:
            instance.status = INSTANCE_STATE_BOOTING
            instance.save()
            end = datetime.datetime.now()
            LOG.info(
                u"Instance create api call passed, [%s][%s], "
                "apply [%s] seconds.", instance, status, (end - begin).seconds)
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            instance_create_sync_status_task.delay(instance,
                                                   neutron_enabled,
                                                   retry_count=1)
            billing_task.charge_resource(instance.id, Instance)

    return instance
Esempio n. 45
0
def _server_pause(instance):
    rc = create_rc_by_instance(instance)
    server = instance_get(instance)
    if server:
        nova.server_pause(rc, instance.uuid)
Esempio n. 46
0
def instance_create_task(instance, **kwargs):
    LOG.info("*************** I am instance create in instance_create_task ****************")
    password = kwargs.get("password", None)
    assert instance
    assert password

    user_tenant_uuid = kwargs.get("user_tenant_uuid", None)
 
    LOG.info("**** user_tenant_uuid in instance_create_task is ****" + str(user_tenant_uuid))
    begin = datetime.datetime.now()
    LOG.info(u"Instance create start, [%s][pwd:%s].",
                        instance, password)
     
    rc = create_rc_by_instance(instance)
    try: 
        flavor = flavor_create(instance)
        instance.flavor_id = flavor.id 
    except Exception:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instance create failed by flavor exception, [%s].",
                        instance)
        return False


    # First check if network exists or not.
    neutron_enabled = neutron.is_neutron_enabled(rc)
    LOG.info("********** check neutron is enabled or not **************" + str(neutron_enabled))
    
    # If not exists, create a new one for that tenant.
    if neutron_enabled:
        LOG.info("********** neutron_enabled *************")
        LOG.info("********** start to make sure make_sure_default_private_network ***********")
        network = make_sure_default_private_network(instance, rc, user_tenant_uuid)
        #network = neutron.network_list_for_tenant(rc, tenant_id=user_tenant_uuid)
        #LOG.info("********** network is ******************" + str(network))
        #network_id = None
        #for net in network:
        #    LOG.info("***** net is *******" + str(net))
        #    network_id = net.id
        #LOG.info("********* network_id is *********" + str(network_id))
        LOG.info("**** network is ****" + str(network))
        instance.network_id = network.id
        instance.save() 
        LOG.info(u"Instance create set network passed, [%s][%s].",
                    instance, network)

    if not instance.firewall_group:
        LOG.info("********** start to set default firewall ************")
        instance.set_default_firewall()
 
    try:
        LOG.info("********** start to create instance *****************")
        server = instance_create(instance, password)
    except Exception as ex:
        instance.status = INSTANCE_STATE_ERROR
        instance.save()
        LOG.exception(u"Instace create api call raise an exception, [%s][%s].",
                        instance, ex.message)
        return False
    else:
        status = server.status.upper() if server else u"None"
        instance.uuid = server.id
        if status == u"ERROR":
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            end = datetime.datetime.now()
            LOG.info(u"Instance create api call failed, [%s][%s], "
                     "apply [%s] seconds.",
                    instance, status, (end-begin).seconds)
        else:
            instance.status = INSTANCE_STATE_BOOTING
            instance.save()
            end = datetime.datetime.now()
            LOG.info(u"Instance create api call passed, [%s][%s], "
                    "apply [%s] seconds.",
                    instance, status, (end-begin).seconds)
            time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
            instance_create_sync_status_task.delay(
                instance, neutron_enabled, user_tenant_uuid, rc, retry_count=1)
            billing_task.charge_resource(instance.id, Instance)

    return instance
Esempio n. 47
0
def attach_volume_to_instance(volume, instance):
    rc = create_rc_by_instance(instance)
    LOG.info("Volume attach start, [%s][%s].", instance, volume)

    begin = datetime.datetime.now()
    try:
        nova.instance_volume_attach(rc,
                                    volume_id=volume.volume_id,
                                    instance_id=instance.uuid,
                                    device='')
    except Exception:
        volume.change_status(VOLUME_STATE_ERROR)
        end = datetime.datetime.now()
        LOG.exception(
            "Volume attach api call failed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)
        return False
    else:
        end = datetime.datetime.now()
        LOG.info(
            "Volume attach api call succeed, [%s][%s], "
            "apply [%s] seconds.", instance, volume, (end - begin).seconds)

    begin = datetime.datetime.now()
    for count in xrange(settings.MAX_COUNT_SYNC):
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        vol = volume_task.volume_get(volume)
        status = vol.status.upper() if vol else "None"

        end = datetime.datetime.now()
        if status == u'ERROR':
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info(
                "Volume attach failed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            break
        elif status == u'ERROR_ATTACHING':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_ERROR)
            LOG.info(
                "Volume attach failed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            break
        elif status == u'IN-USE':
            volume.instance = instance
            volume.change_status(VOLUME_STATE_IN_USE)
            LOG.info(
                "Volume attach succeed, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            break
        elif status == "None":
            LOG.info(
                "Volume attach failed, volume is None, "
                "[Count:%s][%s][%s][status: %s], apply [%s] seconds.", count,
                instance, volume, status, (end - begin).seconds)
            volume.instance = None
            volume.deleted = True
            volume.save()
            break
        else:
            LOG.info(
                "Volume attach synchronize status, "
                "[Count:%s][%s][%s][status: %s].", count, instance, volume,
                status)
    else:
        end = datetime.datetime.now()
        LOG.info("Volume attach timeout, [%s][%s], apply [%s] seconds.",
                 instance, volume, (end - begin).seconds)
        return False

    end = datetime.datetime.now()
    if volume.status == VOLUME_STATE_ERROR:
        LOG.error("Volume attach failed, [%s][%s], apply [%s] seconds.",
                  instance, volume, (end - begin).seconds)
        return False
    else:
        return True
Esempio n. 48
0
def instance_action_task(instance, action, **kwargs):
    LOG.info("instance: [%s][%s] - Start action [%s]" % (instance.id, instance.name, action))
    rc = create_rc_by_instance(instance)
    try:
        if action == INSTANCE_ACTIONS_DICT["POWER_OFF"]:
            nova.server_stop(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["POWER_ON"]:
            nova.server_start(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["REBOOT"]:
            nova.server_reboot(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["PAUSE"]:
            nova.server_pause(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["RESTORE"]:
            nova.server_unpause(rc, instance_id=instance.uuid)
        elif action == INSTANCE_ACTIONS_DICT["TERMINATE"]:
            nova.server_delete(rc, instance.uuid)
        else:
            LOG.error("instance: [%s][%s] - unsupported action [%s]" % (instance.id, instance.name, action))
            return
    except:
        LOG.error("instance: [%s][%s] - error for action [%s] : %s" % (instance.id, instance.name, action,
                                                                     traceback.format_exc()))
        instance.status = INSTANCE_STATE_ERROR
        instance.save()

    count = 0
    while True:
        time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND)
        server = instance_get(instance)

        if server and server.status == u'ERROR':
            instance.status = INSTANCE_STATE_ERROR
            instance.save()
            LOG.error("instance: [%s][%s] - error for action %s" % (instance.id, instance.name, action))
            break

        if server and server.status == u'ACTIVE':
            if action == INSTANCE_ACTIONS_DICT["POWER_ON"] or \
                action == INSTANCE_ACTIONS_DICT["REBOOT"] or \
                action == INSTANCE_ACTIONS_DICT["RESTORE"]:
                instance.status = INSTANCE_STATE_RUNNING
                instance.save()
                LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
                break

        if server and server.status == u'SHUTOFF':
            if action == INSTANCE_ACTIONS_DICT["POWER_OFF"]:
                instance.status = INSTANCE_STATE_POWEROFF
                instance.save()
                LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
                break

        if server and server.status == u'PAUSED':
            if action  == INSTANCE_ACTIONS_DICT["PAUSE"]:
                instance.status = INSTANCE_STATE_PAUSED
                instance.save()
                LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
                break
    
        if not server and action == INSTANCE_ACTIONS_DICT["TERMINATE"]:
            instance.status = INSTANCE_STATE_DELETE
            instance.deleted = 1
            instance.save()
            LOG.info("instance: [%s][%s] - successfully for action: [%s]" % (instance.id, instance.name, action))
            instance_deleted_release_resource(instance)
            break

        count += 1
        if count > settings.MAX_COUNT_SYNC:
            LOG.info("instance: [%s][%s] - timeout for action: [%s]" % (instance.id, instance.name, action))
            break

    return True