def get_hosts_from_aliyun(): """ 从阿里云获取ECS实例并入库 :return: """ ecs = ECSHandler(*ALICLOUD['access_key'], ALICLOUD['region']) page = 1 while True: instances, exception, next_page = ecs.get_instances(page=page, page_size=10) print(instances) # print(exception) # print(next_page) if instances: for i in instances: i['public_cloud'] = 'aliyun' print(i) host, created = Host.objects.update_or_create( instance_id=i['instance_id'], defaults=i) if created: print('阿里云 {} 新主机入库'.format(host.instance_name)) else: print('阿里云 {} 更新主机入库'.format(host.instance_name)) page += 1 if not next_page: break return True
def get(self, request, **kwargs): ecs = ECSHandler(ALICLOUD['access_key_id'], ALICLOUD['access_key'], ALICLOUD['region']) instance = Host.objects.get(instance_id=kwargs['pk']) if instance.status == "Running": ecs.stop_host(kwargs['pk']) res = {"code": 0, "msg": "操作成功,正在关机~"} new_flush = AliyunSDK() new_flush.get(request) else: res = {"code": 1, "errmsg": "服务器繁忙,请稍后重试"} return render(request, settings.JUMP_PAGE, res)
def __init__(self): self.aliyun_ecs = ECSHandler(ALICLOUD['access_key_id'], ALICLOUD['access_key'], ALICLOUD['region']) self.instances = self.aliyun_ecs.get_instances()[0] self.clouds_asset_count = [] self.business_line_host_nums = [] self.each_type_assets_count = [] self.tag_cloud = [] self.aliyun_rds = None self.tencent_ecs = None self.huawei_ecs = None
def get(self, request): ecs = ECSHandler(ALICLOUD['access_key_id'], ALICLOUD['access_key'], ALICLOUD['region']) # return data, True, len(instances) >= page_size instances = ecs.get_instances()[0] print(ecs.get_instances()) for instance in instances: is_oldhost = Host.objects.filter(public_ip=instance['public_ip'], private_ip=instance['private_ip']) if not is_oldhost: Host.objects.create(**instance) res = { "code": 0, "msg": "主机列表刷新成功,有新增主机{}".format(instance['hostname']) } else: Host.objects.update(**instance) # object_list = Host.objects.all() res = {"code": 0, "msg": "刷新完成,主机数不变~"} return render(request, settings.JUMP_PAGE, res)