增加重置系统按钮及功能
This commit is contained in:
parent
273c144271
commit
5d8e22f579
@ -200,15 +200,16 @@ class AliyunLightServerManager:
|
||||
jingrow.log_error("续费实例失败", f"续费实例 {instance_id} 时发生错误: {str(e)}")
|
||||
return {'success': False, 'error': str(e), 'message': '实例续费失败'}
|
||||
|
||||
def reset_system(self, instance_id, image_id, password=None, region_id='cn-shanghai'):
|
||||
def reset_system(self, instance_id, image_id=None, password=None, region_id='cn-shanghai'):
|
||||
"""重置系统"""
|
||||
client = self._get_client(region_id)
|
||||
try:
|
||||
request = swas__open20200601_models.ResetSystemRequest(
|
||||
region_id=region_id,
|
||||
instance_id=instance_id,
|
||||
image_id=image_id
|
||||
instance_id=instance_id
|
||||
)
|
||||
if image_id:
|
||||
request.image_id = image_id
|
||||
if password:
|
||||
request.password = password
|
||||
runtime = util_models.RuntimeOptions()
|
||||
@ -448,12 +449,12 @@ def reboot_aliyun_instance(instance_id, region_id='cn-shanghai'):
|
||||
manager = _get_manager()
|
||||
result = manager.reboot_instance(instance_id, region_id)
|
||||
|
||||
# 4. 启动后台任务监控重启状态
|
||||
# 4. 启动后台任务监控重启状态(延迟10秒开始)
|
||||
time.sleep(10)
|
||||
jingrow.enqueue(
|
||||
"jcloud.api.aliyun_server_light.monitor_reboot_status",
|
||||
"jcloud.api.aliyun_server_light.monitor_server_status",
|
||||
instance_id=instance_id,
|
||||
region_id=region_id,
|
||||
queue="long",
|
||||
timeout=600
|
||||
)
|
||||
|
||||
@ -468,8 +469,8 @@ def reboot_aliyun_instance(instance_id, region_id='cn-shanghai'):
|
||||
return {"success": False, "error": str(e), "message": "重启实例失败"}
|
||||
|
||||
|
||||
def monitor_reboot_status(instance_id, region_id):
|
||||
"""简单的重启状态监控 - 直接循环查询"""
|
||||
def monitor_server_status(instance_id, region_id):
|
||||
"""通用的服务器状态监控 - 监控服务器状态直到Running"""
|
||||
try:
|
||||
# 通过instance_id查找服务器记录
|
||||
server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id})
|
||||
@ -478,7 +479,9 @@ def monitor_reboot_status(instance_id, region_id):
|
||||
return
|
||||
|
||||
manager = _get_manager()
|
||||
max_retries = 30 # 最多查询30次
|
||||
max_retries = 60 # 最多查询60次(10分钟)
|
||||
# 阿里云轻量应用服务器可能的状态值
|
||||
waiting_states = ['Starting', 'Stopping', 'Resetting', 'Stopped', 'Pending']
|
||||
|
||||
for retry in range(max_retries):
|
||||
# 查询实例状态
|
||||
@ -498,30 +501,24 @@ def monitor_reboot_status(instance_id, region_id):
|
||||
|
||||
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']:
|
||||
elif status in waiting_states:
|
||||
# 继续等待
|
||||
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.log_error("监控状态失败", f"监控实例 {instance_id} 状态时发生错误: {str(e)}")
|
||||
|
||||
@jingrow.whitelist()
|
||||
def upgrade_aliyun_instance(instance_id, plan_id, region_id='cn-shanghai'):
|
||||
@ -536,10 +533,41 @@ def renew_aliyun_instance(instance_id, period=1, period_unit='Month', region_id=
|
||||
return manager.renew_instance(instance_id, period, period_unit, region_id)
|
||||
|
||||
@jingrow.whitelist()
|
||||
def reset_aliyun_instance_system(instance_id, image_id, password=None, region_id='cn-shanghai'):
|
||||
"""重置系统"""
|
||||
manager = _get_manager()
|
||||
return manager.reset_system(instance_id, image_id, password, region_id)
|
||||
def reset_aliyun_instance_system(instance_id, image_id=None, password=None, region_id='cn-shanghai'):
|
||||
"""重置系统并更新状态"""
|
||||
try:
|
||||
# 1. 查找对应的Jsite Server记录
|
||||
server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id})
|
||||
if not server:
|
||||
return {"success": False, "message": "找不到对应的服务器记录"}
|
||||
|
||||
# 2. 立即更新状态为"重置中"
|
||||
server.status = "Resetting"
|
||||
server.save(ignore_permissions=True)
|
||||
jingrow.db.commit()
|
||||
|
||||
# 3. 调用阿里云API重置系统
|
||||
manager = _get_manager()
|
||||
result = manager.reset_system(instance_id, image_id, password, region_id)
|
||||
|
||||
# 4. 启动后台任务监控重置状态(延迟10秒开始)
|
||||
time.sleep(10)
|
||||
jingrow.enqueue(
|
||||
"jcloud.api.aliyun_server_light.monitor_server_status",
|
||||
instance_id=instance_id,
|
||||
region_id=region_id,
|
||||
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": "重置系统失败"}
|
||||
|
||||
@jingrow.whitelist()
|
||||
def update_aliyun_instance_password(instance_id, password, region_id='cn-shanghai'):
|
||||
|
||||
@ -48,6 +48,16 @@ jingrow.ui.form.on("Jsite Server", {
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
frm.add_custom_button(__('重置系统'), function() {
|
||||
// 弹出确认对话框
|
||||
jingrow.confirm(
|
||||
__('确定要重置系统吗?这将清除所有数据并重新安装系统,操作不可逆!'),
|
||||
function() {
|
||||
reset_system(frm);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// 为password字段添加眼睛图标
|
||||
@ -198,3 +208,39 @@ function reset_key_pair(frm) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function reset_system(frm) {
|
||||
jingrow.call({
|
||||
method: 'jcloud.api.aliyun_server_light.reset_aliyun_instance_system',
|
||||
args: {
|
||||
instance_id: frm.pg.instance_id,
|
||||
region_id: frm.pg.region
|
||||
},
|
||||
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'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user