增加重置密钥对按钮

This commit is contained in:
jingrow 2025-07-28 21:27:25 +08:00
parent caefb4a408
commit 273c144271
2 changed files with 65 additions and 4 deletions

View File

@ -762,7 +762,7 @@ def create_aliyun_server(order_name):
raise Exception("找不到对应的服务器记录")
# 从服务器记录中获取配置信息
region_id = server.region or 'cn-shanghai'
region_id = server.region
image_id = server.image_id or 'e9363571cf2444aba422b17470285465'
plan_id = server.planid or 'swas.s.c2m1s30b1.linux'
period = server.period or 1
@ -863,7 +863,7 @@ def update_server_record(instance_ids):
jingrow.log_error("更新服务器信息失败", f"找不到实例ID为 {instance_id} 的Jsite Server记录")
return {"success": False, "message": f"找不到实例ID为 {instance_id} 的Jsite Server记录"}
region_id = server.region or 'cn-shanghai'
region_id = server.region
# 获取阿里云实例详细信息
instance_details = get_aliyun_instance_details(instance_ids, region_id)
@ -931,3 +931,19 @@ def update_server_record(instance_ids):
except Exception as e:
jingrow.log_error("更新服务器信息失败", f"更新服务器信息时发生错误: {str(e)}")
return {"success": False, "message": str(e)}
@jingrow.whitelist()
def reset_server_key_pair(instance_id):
"""重置服务器密钥对:先删除旧的,再创建新的"""
try:
# 第一步:删除旧的密钥对(如果存在)
delete_aliyun_instance_key_pair(instance_id)
# 第二步:创建新的密钥对
result = create_server_key_pair(instance_id)
return result
except Exception as e:
jingrow.log_error("重置密钥对失败", f"重置实例 {instance_id} 密钥对时发生错误: {str(e)}")
return {"success": False, "message": str(e)}

View File

@ -38,6 +38,16 @@ jingrow.ui.form.on("Jsite Server", {
__('重置服务器密码')
);
});
frm.add_custom_button(__('重置密钥对'), function() {
// 弹出确认对话框
jingrow.confirm(
__('确定要重置密钥对吗?这将删除旧的密钥对并创建新的密钥对。重置后需要使用新的私钥才能连接服务器。'),
function() {
reset_key_pair(frm);
}
);
});
}
// 为password字段添加眼睛图标
@ -87,7 +97,7 @@ function restart_server(frm) {
method: 'jcloud.api.aliyun_server_light.reboot_aliyun_instance',
args: {
instance_id: frm.pg.instance_id,
region_id: frm.pg.region || 'cn-shanghai'
region_id: frm.pg.region
},
callback: function(r) {
if (r.success) {
@ -123,7 +133,7 @@ function reset_password(frm, new_password) {
args: {
instance_id: frm.pg.instance_id,
password: new_password,
region_id: frm.pg.region || 'cn-shanghai'
region_id: frm.pg.region
},
callback: function(r) {
if (r.success) {
@ -153,3 +163,38 @@ function reset_password(frm, new_password) {
}
});
}
function reset_key_pair(frm) {
jingrow.call({
method: 'jcloud.api.aliyun_server_light.reset_server_key_pair',
args: {
instance_id: frm.pg.instance_id
},
callback: function(r) {
if (r.success) {
jingrow.msgprint({
title: __('成功'),
message: __('密钥对重置成功,新的私钥已保存到服务器记录中'),
indicator: 'green'
});
// 刷新页面以显示新的密钥对信息
setTimeout(function() {
frm.reload_pg();
}, 2000);
} else {
jingrow.msgprint({
title: __('重置失败'),
message: r.message || r.error || __('未知错误'),
indicator: 'red'
});
}
},
error: function(err) {
jingrow.msgprint({
title: __('重置失败'),
message: err.message,
indicator: 'red'
});
}
});
}