Ejemplo n.º 1
0
def host_add(request):
    if request.method == 'POST':
        f = AddHostForm(request.POST)
        if f.is_valid():
            cleaned_data = f.cleaned_data
            try:
                host = Host()
                host.ipaddr = cleaned_data['ipaddr']
                host.provider = cleaned_data['provider']
                host.root_password = cleaned_data['root_password']
                host.hostname = cleaned_data['hostname']
                host.status = cleaned_data['status']
                host.comment = cleaned_data['comment']
                host.save()
                messages.info(request, '修改成功')
            except:
                messages.error(request, '修改失败')
            finally:
                return redirect('/admin/cmdb/host')
    else:
        f = AddHostForm()
    return render(
        request, 'admin/cmdb/add_host.html', {
            "form": f,
            "status_choices": Host.STATUS_CHOICES,
            "provider_choices": Host.PROVIDER_CHOICES,
        })
Ejemplo n.º 2
0
def import_from_public_cloud_as_api(request):
    provider = request.GET.get('provider')
    region = request.GET.get('region')
    settingService = SettingService()
    auth_info = settingService.get_public_cloud_info()
    count = 0
    vps_list = []
    if provider == ALIYUN:
        aliyun = AliyunService(auth_info.aliyun_access_key_id,
                               auth_info.aliyun_access_key_secret)
        page = 1
        while page < 100:
            json_data = json.loads(aliyun.get_allhost(region, PageNumber=page))
            instances = json_data['Instances']['Instance']
            page += 1
            if instances:
                instances = json_data['Instances']['Instance']
                for item in instances:
                    try:
                        ipaddr = item['VpcAttributes']['PrivateIpAddress'][
                            'IpAddress'][0]
                    except:
                        continue

                    try:
                        hostname = item['HostName']
                        host = Host.objects.get(
                            Q(ipaddr=ipaddr) | Q(hostname=hostname))
                        host.hostname = hostname
                        host.comment = item['Description']
                        if item['Status'] == 'Running':
                            host.status = Host.STATUS_ENABLED
                        else:
                            host.status = Host.STATUS_DISABLED
                        host.save()
                    except:
                        host = Host()
                        host.provider = ALIYUN
                        host.ipaddr = ipaddr
                        host.hostname = item['HostName']
                        host.instance_id = item['InstanceId']
                        host.comment = item['Description']
                        if item['Status'] == 'Running':
                            host.status = Host.STATUS_ENABLED
                        else:
                            host.status = Host.STATUS_DISABLED
                        host.save()
                    finally:
                        count += 1
                        vps_list.append(ipaddr + '-' + item['HostName'])
            else:
                break

    elif provider == QCLOUD:
        qcloud = QcloudService(auth_info.qcloud_secret_id,
                               auth_info.qcloud_secret_key)
        try:
            json_data = json.loads(qcloud.get_allhost(region))
            if ('TotalCount' in json_data) and (json_data['TotalCount'] > 0):
                instances = json_data['InstanceSet']
                for item in instances:
                    try:
                        ipaddr = item['PrivateIpAddresses'][0]
                    except:
                        continue

                    try:
                        host = Host.objects.get(ipaddr=ipaddr)
                        host.hostname = item['InstanceName']
                        host.save()
                    except:
                        host = Host()
                        host.provider = QCLOUD
                        host.ipaddr = ipaddr
                        host.hostname = item['InstanceName']
                        host.instance_id = item['InstanceId']
                        host.status = Host.STATUS_ENABLED
                        host.save()
                    finally:
                        count += 1
                        vps_list.append(ipaddr + '-' + item['InstanceName'])
        except:
            pass
    else:
        pass
    res = {'count': count, 'vps_list': vps_list}
    return JsonResponse(res, safe=False)