def server_deploy(request): if request.method == 'GET': return render(request, 'X-admin/server-deploy.html') elif request.method == 'POST': template_id = request.POST['template'] account = request.POST['account'] vpc_id = request.POST['vpc'] subnet_id = request.POST['subnet'] try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) tpl_obj = models.Template.objects.get(pk=template_id) instance_list = models.Instance.objects.filter(template=tpl_obj) instance_list = [{'name': instance.name, 'disk': instance.disk, 'image_id': instance.image_id, 'instance_type': instance.instance_type, 'key_name': instance.key_name, 'count': instance.count} for instance in instance_list] res_list = client.create_instance_from_template(instance_template_list=instance_list, vpc_id=vpc_id, subnet_id=subnet_id) if len(res_list) > 0: result = {'code': 0, 'msg': "部署成功!"} else: result = {'code': 1, 'msg': "部署失败!"} except Exception as e: print(e) result = {'code': 1, 'msg': "部署失败!"} return HttpResponse(json.dumps(result))
def security_group(request): if request.method == 'GET': return render(request, 'X-admin/security-group-list.html') elif request.method == 'POST': page = int(request.POST.get('page', 1)) limit = int(request.POST.get('limit', 10)) account = request.POST['account'] groupname = request.POST.get('groupname', '') to_limit = int(request.POST.get('to_limit', 1)) try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) if to_limit: res_dict = client.get_security_group() res_list = res_dict['SecurityGroups'] count = len(res_list) data_list = [{'name': data['GroupName'], 'group_id': data['GroupId']} for data in res_list[limit * (page - 1):limit * page]] else: param_dict = {'group-name': groupname, 'vpc-id': request.POST['vpc']} res_dict = client.get_security_group(**param_dict) res_list = res_dict['SecurityGroups'] count = len(res_list) data_list = [{'name': data['GroupName'], 'group_id': data['GroupId']} for data in res_list] result = {'code': '0', 'msg': 'success', 'count': count, 'data': data_list} except Exception as e: print(e) result = {"code": '1', "msg": "获取失败!"} return HttpResponse(json.dumps(result))
def vpc(request): if request.method == 'POST': account = request.POST['account'] try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) res_dict = client.get_vpc() count = len(res_dict['Vpcs']) data_list = [{'vpc_id': data['VpcId'], 'name': data['Tags'][0]['Value'] if 'Tags' in data.keys() else data['VpcId']} for data in res_dict['Vpcs']] result = {'code': '0', 'msg': 'success', 'count': count, 'data': data_list} except Exception as e: print(e) result = {"code": '0', 'msg': 'success', 'count': 0, 'data': []} return HttpResponse(json.dumps(result))
def subnet(request): if request.method == 'POST': account = request.POST['account'] vpc_id = request.POST['vpc'] try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) res_dict = client.get_subnet(vpc_id) count = len(res_dict['Subnets']) data_list = [{'subnet_id': data['SubnetId'], 'name': data['Tags'][0]['Value'] if 'Tags' in data.keys() else data['SubnetId'], 'cidr_block': data['CidrBlock'], 'avail_zone': data['AvailabilityZone']} for data in res_dict['Subnets']] result = {'code': '0', 'msg': 'success', 'count': count, 'data': data_list} except Exception as e: print(e) result = {"code": '0', 'msg': 'success', 'count': 0, 'data': []} return HttpResponse(json.dumps(result))
def server_update(request): if request.method == 'POST': account = request.POST['account'] instance_id = request.POST['instance_id'] security_group_list = request.POST['security_group_list'] try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) res = client.modified_security_group(instance_id, security_group_list.split(",")) if res['code'] == 0: result = {'code': 0, 'message': "更新成功!"} else: result = {'code': 1, 'message': res['msg']} except Exception as e: print(e) result = {'code': 1, 'msg': str(e)} return HttpResponse(json.dumps(result))
def server(request): if request.method == 'GET': return render(request, 'X-admin/server-list.html') elif request.method == 'POST': page = int(request.POST.get('page', 1)) limit = int(request.POST.get('limit', 10)) account = request.POST['account'] vpc_id = request.POST['vpc'] servername = request.POST.get('servername', '') try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) if servername == '': res_list = client.get_instance_by_resource(vpc_id) count = len(res_list) data_list = [{'id': data.instance_id, 'name': data.tags[0]['Value'], 'instance_type': data.instance_type, 'zone': data.placement['AvailabilityZone'], 'image_id': data.image_id, 'key_name': data.key_name, 'security_group': ",".join([group['GroupName'] for group in data.security_groups]), 'private_address': data.private_ip_address if data.state['Name'] != 'terminated' else '', 'public_address': data.public_ip_address if data.state['Name'] == 'running' else '', 'status': data.state['Name']} for data in res_list[limit * (page - 1):limit * page]] else: res_dict = client.get_instance(vpc_id, servername) res_list = res_dict['Reservations'] count = len(res_list) data_list = [{'id': data['Instances'][0]['InstanceId'], 'name': data['Instances'][0]['Tags'][0]['Value'], 'instance_type': data['Instances'][0]['InstanceType'], 'zone': data['Instances'][0]['Placement']['AvailabilityZone'], 'image_id': data['Instances'][0]['ImageId'], 'key_name': data['Instances'][0]['KeyName'], 'security_group': ",".join([group['GroupName'] for group in data['Instances'][0]['SecurityGroups']]), 'private_address': data['Instances'][0]['PrivateIpAddress'] if data['Instances'][0]['State']['Name'] != 'terminated' else '', 'public_address': data['Instances'][0]['PublicIpAddress'] if data['Instances'][0]['State']['Name'] == 'running' else '', 'status': data['Instances'][0]['State']['Name']} for data in res_list[limit * (page - 1):limit * page]] result = {'code': '0', 'msg': 'success', 'count': count, 'data': data_list} except Exception as e: print(e) result = {"code": '1', "msg": "获取失败!"} return HttpResponse(json.dumps(result))
def server_add(request): if request.method == 'POST': instance_dict = {} account = request.POST['account'] vpc_id = request.POST['vpc'] subnet_id = request.POST['subnet'] instance_dict['name'] = request.POST['name'] instance_dict['instance_type'] = request.POST['instance_type'] instance_dict['disk'] = int(request.POST['disk']) instance_dict['image_id'] = request.POST['image_id'] instance_dict['key_name'] = request.POST['key_name'] instance_dict['count'] = int(request.POST['count']) try: account_obj = models.AwsAccount.objects.get(name=account) client = AwsEc2(account_obj.access_key, account_obj.secret_key) res = client.create_instance(instance_dict=instance_dict, vpc_id=vpc_id, subnet_id=subnet_id) if res['code'] == 0: result = {'code': 0, 'message': "添加成功!"} else: result = {'code': 1, 'message': res['msg']} except Exception as e: print(e) result = {'code': 1, 'msg': "添加失败!"} return HttpResponse(json.dumps(result))