main #2
@ -3,6 +3,7 @@ import os
|
||||
import sys
|
||||
import json
|
||||
import random
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any
|
||||
|
||||
@ -216,20 +217,7 @@ class AliyunLightServerManager:
|
||||
jingrow.log_error("重置系统失败", f"重置实例 {instance_id} 系统时发生错误: {str(e)}")
|
||||
return {'success': False, 'error': str(e), 'message': '系统重置失败'}
|
||||
|
||||
def get_instance_info(self, instance_id, region_id='cn-shanghai'):
|
||||
"""获取实例信息"""
|
||||
client = self._get_client(region_id)
|
||||
try:
|
||||
request = swas__open20200601_models.GetInstanceRequest(
|
||||
region_id=region_id,
|
||||
instance_id=instance_id
|
||||
)
|
||||
runtime = util_models.RuntimeOptions()
|
||||
response = client.get_instance_with_options(request, runtime)
|
||||
return {'success': True, 'data': self._convert_response_to_dict(response.body), 'message': '获取实例信息成功'}
|
||||
except Exception as e:
|
||||
jingrow.log_error("获取实例信息失败", f"获取实例 {instance_id} 信息时发生错误: {str(e)}")
|
||||
return {'success': False, 'error': str(e), 'message': '获取实例信息失败'}
|
||||
|
||||
|
||||
def list_instances(self, page_number=1, page_size=20, region_id='cn-shanghai'):
|
||||
"""获取实例列表"""
|
||||
@ -411,9 +399,96 @@ def stop_aliyun_instance(instance_id, region_id='cn-shanghai'):
|
||||
|
||||
@jingrow.whitelist()
|
||||
def reboot_aliyun_instance(instance_id, region_id='cn-shanghai'):
|
||||
"""重启实例"""
|
||||
manager = _get_manager()
|
||||
return manager.reboot_instance(instance_id, region_id)
|
||||
"""重启实例并更新状态"""
|
||||
try:
|
||||
# 1. 查找对应的Jsite Server记录
|
||||
server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id})
|
||||
if not server:
|
||||
return {"success": False, "message": "找不到对应的服务器记录"}
|
||||
|
||||
# 2. 立即更新状态为"重启中"
|
||||
server.status = "Starting"
|
||||
server.save(ignore_permissions=True)
|
||||
jingrow.db.commit()
|
||||
|
||||
# 3. 调用阿里云API重启实例
|
||||
manager = _get_manager()
|
||||
result = manager.reboot_instance(instance_id, region_id)
|
||||
|
||||
# 4. 启动后台任务监控重启状态
|
||||
jingrow.enqueue(
|
||||
"jcloud.api.aliyun_server_light.monitor_reboot_status",
|
||||
instance_id=instance_id,
|
||||
region_id=region_id,
|
||||
queue="long",
|
||||
timeout=600
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": "重启命令已发送,正在监控重启状态...",
|
||||
"server_name": server.name
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
jingrow.log_error("重启实例失败", f"重启实例 {instance_id} 时发生错误: {str(e)}")
|
||||
return {"success": False, "error": str(e), "message": "重启实例失败"}
|
||||
|
||||
|
||||
def monitor_reboot_status(instance_id, region_id):
|
||||
"""简单的重启状态监控 - 直接循环查询"""
|
||||
try:
|
||||
# 通过instance_id查找服务器记录
|
||||
server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id})
|
||||
if not server:
|
||||
jingrow.log_error("找不到服务器记录", f"无法找到实例ID为 {instance_id} 的服务器记录")
|
||||
return
|
||||
|
||||
manager = _get_manager()
|
||||
max_retries = 30 # 最多查询30次
|
||||
|
||||
for retry in range(max_retries):
|
||||
# 查询实例状态
|
||||
instance_info = manager.get_instance_details([instance_id], region_id)
|
||||
|
||||
if not instance_info or not instance_info.get('success'):
|
||||
jingrow.log_error("获取实例状态失败", f"无法获取实例 {instance_id} 的状态信息")
|
||||
time.sleep(10)
|
||||
continue
|
||||
|
||||
# 解析实例状态
|
||||
instances = instance_info.get('data', {}).get('instances', [])
|
||||
if not instances:
|
||||
jingrow.log_error("获取实例状态失败", f"实例 {instance_id} 的状态信息为空")
|
||||
time.sleep(10)
|
||||
continue
|
||||
|
||||
instance = instances[0] # 取第一个实例
|
||||
status = instance.get('status', '')
|
||||
|
||||
# 更新服务器状态
|
||||
if status == 'Running':
|
||||
server.status = 'Running'
|
||||
server.save(ignore_permissions=True)
|
||||
jingrow.db.commit()
|
||||
jingrow.log_error("重启完成", f"服务器 {server.name} 重启完成")
|
||||
return
|
||||
elif status in ['Starting', 'Stopping']:
|
||||
# 继续等待
|
||||
time.sleep(10)
|
||||
else:
|
||||
# 其他状态,记录日志并停止
|
||||
server.status = status
|
||||
server.save(ignore_permissions=True)
|
||||
jingrow.db.commit()
|
||||
jingrow.log_error("重启状态异常", f"服务器 {server.name} 状态为: {status}")
|
||||
return
|
||||
|
||||
# 超时处理
|
||||
jingrow.log_error("监控超时", f"服务器 {server.name} 重启监控超时,已查询 {max_retries} 次")
|
||||
|
||||
except Exception as e:
|
||||
jingrow.log_error("监控重启状态失败", f"监控实例 {instance_id} 重启状态时发生错误: {str(e)}")
|
||||
|
||||
@jingrow.whitelist()
|
||||
def upgrade_aliyun_instance(instance_id, plan_id, region_id='cn-shanghai'):
|
||||
@ -433,11 +508,7 @@ def reset_aliyun_instance_system(instance_id, image_id, password=None, region_id
|
||||
manager = _get_manager()
|
||||
return manager.reset_system(instance_id, image_id, password, region_id)
|
||||
|
||||
@jingrow.whitelist()
|
||||
def get_aliyun_instance_info(instance_id, region_id='cn-shanghai'):
|
||||
"""获取实例信息"""
|
||||
manager = _get_manager()
|
||||
return manager.get_instance_info(instance_id, region_id)
|
||||
|
||||
|
||||
@jingrow.whitelist()
|
||||
def list_aliyun_instances(page_number=1, page_size=20, region_id='cn-shanghai'):
|
||||
|
||||
@ -1,8 +1,53 @@
|
||||
// Copyright (c) 2025, Jingrow and contributors
|
||||
// For license information, please see license.txt
|
||||
|
||||
// jingrow.ui.form.on("Jsite Server", {
|
||||
// refresh(frm) {
|
||||
jingrow.ui.form.on("Jsite Server", {
|
||||
refresh(frm) {
|
||||
if (frm.pg.instance_id) {
|
||||
frm.add_custom_button(__('重启'), function() {
|
||||
// 弹出确认对话框
|
||||
jingrow.confirm(
|
||||
__('确定要重启服务器吗?重启过程中服务器将暂时不可用。'),
|
||||
function() {
|
||||
restart_server(frm);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// },
|
||||
// });
|
||||
function restart_server(frm) {
|
||||
jingrow.call({
|
||||
method: 'jcloud.api.aliyun_server_light.reboot_aliyun_instance',
|
||||
args: {
|
||||
instance_id: frm.pg.instance_id,
|
||||
region_id: frm.pg.region || 'cn-shanghai'
|
||||
},
|
||||
callback: function(r) {
|
||||
if (r.success) {
|
||||
jingrow.msgprint({
|
||||
title: __('成功'),
|
||||
message: __('重启命令已发送,请稍后刷新页面查看状态'),
|
||||
indicator: 'green'
|
||||
});
|
||||
setTimeout(function() {
|
||||
frm.reload_pg();
|
||||
}, 3000);
|
||||
} else {
|
||||
jingrow.msgprint({
|
||||
title: __('重启失败'),
|
||||
message: r.message || r.error || __('未知错误'),
|
||||
indicator: 'red'
|
||||
});
|
||||
}
|
||||
},
|
||||
error: function(err) {
|
||||
jingrow.msgprint({
|
||||
title: __('重启失败'),
|
||||
message: err.message,
|
||||
indicator: 'red'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -184,7 +184,7 @@
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2025-07-28 15:36:38.598173",
|
||||
"modified": "2025-07-28 16:21:14.934079",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Jcloud",
|
||||
"name": "Jsite Server",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user