Ejemplo n.º 1
0
def floating_associate(floating, **kwargs):
    resource_type_dict = dict(RESOURCE_TYPE)
    resource_type = kwargs.get('resource_type')[0]
    resource = kwargs.get('resource')[0]
    if resource:
        rc = create_rc_by_floating(floating)
        ports = None
        resource_obj = None

        if resource_type_dict[str(resource_type)] == 'INSTANCE':
            ins = Instance.objects.get(pk=resource)
            resource_obj = ins
            if neutron.is_neutron_enabled(rc):
                ports = network.floating_ip_target_get_by_instance(rc, ins.uuid)
            else:
                ports = ins.uuid
        elif resource_type_dict[resource_type] == 'LOADBALANCER':
            pool = BalancerPool.objects.get(pk=resource)
            if not pool or not pool.vip:
                floating.status = FLOATING_AVAILABLE
                floating.save()
                return None
            resource_obj = pool
            ports = pool.vip.port_id+"_"+pool.vip.address

        if not ports:
            LOG.info("floating action, resourceType[%s],[%s][associate][ins:%s] ports is None" % (resource_type_dict[resource_type], floating.id, resource));
            floating.status = FLOATING_AVAILABLE
            floating.save()
            return

        LOG.info("floating action, [%s][associate][ins:%s][ports:%s]" % (
                            floating.id, resource, ports))
        try:
            network.floating_ip_associate(rc, floating.uuid, ports)
            if len(ports.split('_')) > 1:
                port, fixed_ip = ports.split('_')
            else:
                port, fixed_ip = ports, ports
            floating.resource = resource
            floating.resource_type = resource_type
            floating.status = FLOATING_BINDED
            floating.fixed_ip = fixed_ip
            floating.port_id = port
            floating.save()
            if resource_type_dict[str(resource_type)] == 'INSTANCE':
                resource_obj.public_ip = floating.ip
                resource_obj.save()
            elif resource_type_dict[resource_type] == 'LOADBALANCER':
                vip = BalancerVIP.objects.get(pk=resource_obj.vip.id)
                vip.public_address = floating.ip
                vip.save()
        except Exception as e:
            LOG.exception(e)
            floating.status = FLOATING_AVAILABLE
            floating.save()
    else:
        LOG.info("floating action, [%s][associate] no ins_id" % floating.id);
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def floating_associate(floating, **kwargs):
    resource_type_dict = dict(RESOURCE_TYPE)
    resource_type = kwargs.get('resource_type')[0]
    resource = kwargs.get('resource')[0]
    if resource:
        rc = create_rc_by_floating(floating)
        ports = None
        resource_obj = None

        if resource_type_dict[str(resource_type)] == 'INSTANCE':
            ins = Instance.objects.get(pk=resource)
            resource_obj = ins
            if neutron.is_neutron_enabled(rc):
                ports = network.floating_ip_target_get_by_instance(rc, ins.uuid)
            else:
                ports = ins.uuid
        elif resource_type_dict[resource_type] == 'LOADBALANCER':
            pool = BalancerPool.objects.get(pk=resource)
            if not pool or not pool.vip:
                floating.status = FLOATING_AVAILABLE
                floating.save()
                return None
            resource_obj = pool
            ports = pool.vip.port_id+"_"+pool.vip.address

        if not ports:
            LOG.info("floating action, resourceType[%s],[%s][associate][ins:%s] ports is None" % (resource_type_dict[resource_type], floating.id, resource));
            floating.status = FLOATING_AVAILABLE
            floating.resource = None
            floating.resource_type = None
            floating.save()
            return

        LOG.info("floating action, [%s][associate][ins:%s][ports:%s]" % (
                            floating.id, resource, ports))
        try:
            network.floating_ip_associate(rc, floating.uuid, ports)
            if len(ports.split('_')) > 1:
                port, fixed_ip = ports.split('_')
            else:
                port, fixed_ip = ports, ports
            floating.resource = resource
            floating.resource_type = resource_type
            floating.status = FLOATING_BINDED
            floating.fixed_ip = fixed_ip
            floating.port_id = port
            floating.save()
            if resource_type_dict[str(resource_type)] == 'INSTANCE':
                resource_obj.public_ip = floating.ip
                resource_obj.save()
            elif resource_type_dict[resource_type] == 'LOADBALANCER':
                vip = BalancerVIP.objects.get(pk=resource_obj.vip.id)
                vip.public_address = floating.ip
                vip.save()
        except Exception as e:
            LOG.exception(e)
            floating.status = FLOATING_AVAILABLE
            floating.resource = None
            floating.resource_type = None
            floating.save()
    else:
        LOG.info("floating action, [%s][associate] no ins_id" % floating.id);
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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))