mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-24 22:51:19 +08:00
feat: The script library synchronizes and adds task logs (#8584)
This commit is contained in:
parent
f9a035f380
commit
123a2e8998
8 changed files with 39 additions and 15 deletions
|
|
@ -33,11 +33,15 @@ func (u *ScriptRepo) Get(opts ...DBOption) (model.ScriptLibrary, error) {
|
||||||
|
|
||||||
func (u *ScriptRepo) List(opts ...DBOption) ([]model.ScriptLibrary, error) {
|
func (u *ScriptRepo) List(opts ...DBOption) ([]model.ScriptLibrary, error) {
|
||||||
var ops []model.ScriptLibrary
|
var ops []model.ScriptLibrary
|
||||||
itemDB := global.DB
|
|
||||||
if global.IsMaster {
|
if global.IsMaster {
|
||||||
itemDB = global.CoreDB
|
db := global.CoreDB.Model(&model.ScriptLibrary{})
|
||||||
|
for _, opt := range opts {
|
||||||
|
db = opt(db)
|
||||||
}
|
}
|
||||||
db := itemDB.Model(&model.ScriptLibrary{})
|
err := db.Where("is_interactive = ?", false).Find(&ops).Error
|
||||||
|
return ops, err
|
||||||
|
}
|
||||||
|
db := global.DB.Model(&model.ScriptLibrary{})
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
db = opt(db)
|
db = opt(db)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,13 +90,19 @@ func (b *BaseApi) DeleteScript(c *gin.Context) {
|
||||||
|
|
||||||
// @Tags ScriptLibrary
|
// @Tags ScriptLibrary
|
||||||
// @Summary Sync script from remote
|
// @Summary Sync script from remote
|
||||||
|
// @Accept json
|
||||||
|
// @Param request body dto.OperateByTaskID true "request"
|
||||||
// @Success 200
|
// @Success 200
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Security Timestamp
|
// @Security Timestamp
|
||||||
// @Router /script/sync [post]
|
// @Router /script/sync [post]
|
||||||
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"同步脚本库脚本","formatEN":"sync scripts"}
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"同步脚本库脚本","formatEN":"sync scripts"}
|
||||||
func (b *BaseApi) SyncScript(c *gin.Context) {
|
func (b *BaseApi) SyncScript(c *gin.Context) {
|
||||||
if err := scriptService.Sync(); err != nil {
|
var req dto.OperateByTaskID
|
||||||
|
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := scriptService.Sync(req); err != nil {
|
||||||
helper.InternalServer(c, err)
|
helper.InternalServer(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,3 +51,6 @@ type OperateByID struct {
|
||||||
type OperateByIDs struct {
|
type OperateByIDs struct {
|
||||||
IDs []uint `json:"ids"`
|
IDs []uint `json:"ids"`
|
||||||
}
|
}
|
||||||
|
type OperateByTaskID struct {
|
||||||
|
TaskID string `json:"taskID"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ type IScriptService interface {
|
||||||
Create(req dto.ScriptOperate) error
|
Create(req dto.ScriptOperate) error
|
||||||
Update(req dto.ScriptOperate) error
|
Update(req dto.ScriptOperate) error
|
||||||
Delete(ids dto.OperateByIDs) error
|
Delete(ids dto.OperateByIDs) error
|
||||||
Sync() error
|
Sync(req dto.OperateByTaskID) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIScriptService() IScriptService {
|
func NewIScriptService() IScriptService {
|
||||||
|
|
@ -173,8 +173,8 @@ func LoadScriptInfo(id uint) (model.ScriptLibrary, error) {
|
||||||
return scriptRepo.Get(repo.WithByID(id))
|
return scriptRepo.Get(repo.WithByID(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ScriptService) Sync() error {
|
func (u *ScriptService) Sync(req dto.OperateByTaskID) error {
|
||||||
syncTask, err := task.NewTaskWithOps(i18n.GetMsgByKey("ScriptLibrary"), task.TaskSync, task.TaskScopeScript, "", 0)
|
syncTask, err := task.NewTaskWithOps(i18n.GetMsgByKey("ScriptLibrary"), task.TaskSync, task.TaskScopeScript, req.TaskID, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.LOG.Errorf("create sync task failed %v", err)
|
global.LOG.Errorf("create sync task failed %v", err)
|
||||||
return err
|
return err
|
||||||
|
|
@ -189,7 +189,7 @@ func (u *ScriptService) Sync() error {
|
||||||
var scriptSetting model.Setting
|
var scriptSetting model.Setting
|
||||||
_ = global.DB.Where("key = ?", "ScriptVersion").First(&scriptSetting).Error
|
_ = global.DB.Where("key = ?", "ScriptVersion").First(&scriptSetting).Error
|
||||||
if scriptSetting.Value == string(versionRes) {
|
if scriptSetting.Value == string(versionRes) {
|
||||||
syncTask.Log("The local and remote versions are detected to be consistent. Skip...")
|
syncTask.Logf("The local-%s and remote-%s versions are detected to be consistent. Skip...", scriptSetting.Value, versionRes)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package job
|
package job
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/1Panel-dev/1Panel/core/app/dto"
|
||||||
"github.com/1Panel-dev/1Panel/core/app/service"
|
"github.com/1Panel-dev/1Panel/core/app/service"
|
||||||
"github.com/1Panel-dev/1Panel/core/global"
|
"github.com/1Panel-dev/1Panel/core/global"
|
||||||
)
|
)
|
||||||
|
|
@ -12,7 +13,7 @@ func NewScriptJob() *script {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *script) Run() {
|
func (s *script) Run() {
|
||||||
if err := service.NewIScriptService().Sync(); err != nil {
|
if err := service.NewIScriptService().Sync(dto.OperateByTaskID{}); err != nil {
|
||||||
global.LOG.Errorf("sync scripts from remote failed, err: %v", err)
|
global.LOG.Errorf("sync scripts from remote failed, err: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,8 +71,8 @@ export const searchScript = (params: SearchWithPage) => {
|
||||||
export const addScript = (params: Cronjob.ScriptOperate) => {
|
export const addScript = (params: Cronjob.ScriptOperate) => {
|
||||||
return http.post(`/core/script`, params);
|
return http.post(`/core/script`, params);
|
||||||
};
|
};
|
||||||
export const syncScript = () => {
|
export const syncScript = (taskID: string) => {
|
||||||
return http.post(`/core/script/sync`, {}, TimeoutEnum.T_60S);
|
return http.post(`/core/script/sync`, { taskID: taskID }, TimeoutEnum.T_60S);
|
||||||
};
|
};
|
||||||
export const editScript = (params: Cronjob.ScriptOperate) => {
|
export const editScript = (params: Cronjob.ScriptOperate) => {
|
||||||
return http.post(`/core/script/update`, params);
|
return http.post(`/core/script/update`, params);
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,6 @@ const prop = defineProps({
|
||||||
});
|
});
|
||||||
|
|
||||||
const toDoc = () => {
|
const toDoc = () => {
|
||||||
router.push({ name: 'Library' });
|
router.push({ name: 'Library', query: { uncached: 'true' } });
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -98,12 +98,14 @@
|
||||||
<GroupDialog @search="loadGroupOptions" ref="dialogGroupRef" />
|
<GroupDialog @search="loadGroupOptions" ref="dialogGroupRef" />
|
||||||
<CodemirrorDrawer ref="myDetail" />
|
<CodemirrorDrawer ref="myDetail" />
|
||||||
<TerminalDialog ref="runRef" />
|
<TerminalDialog ref="runRef" />
|
||||||
|
<TaskLog ref="taskLogRef" width="70%" @close="search" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { dateFormat, deepCopy, getCurrentDateFormatted } from '@/utils/util';
|
import { dateFormat, deepCopy, getCurrentDateFormatted, newUUID } from '@/utils/util';
|
||||||
import GroupDialog from '@/components/group/index.vue';
|
import GroupDialog from '@/components/group/index.vue';
|
||||||
|
import TaskLog from '@/components/log/task/index.vue';
|
||||||
import OperateDialog from '@/views/cronjob/library/operate/index.vue';
|
import OperateDialog from '@/views/cronjob/library/operate/index.vue';
|
||||||
import TerminalDialog from '@/views/cronjob/library/run/index.vue';
|
import TerminalDialog from '@/views/cronjob/library/run/index.vue';
|
||||||
import { deleteScript, searchScript, syncScript } from '@/api/modules/cronjob';
|
import { deleteScript, searchScript, syncScript } from '@/api/modules/cronjob';
|
||||||
|
|
@ -113,6 +115,7 @@ import i18n from '@/lang';
|
||||||
import { GlobalStore } from '@/store';
|
import { GlobalStore } from '@/store';
|
||||||
import { getGroupList } from '@/api/modules/group';
|
import { getGroupList } from '@/api/modules/group';
|
||||||
import CodemirrorDrawer from '@/components/codemirror-pro/drawer.vue';
|
import CodemirrorDrawer from '@/components/codemirror-pro/drawer.vue';
|
||||||
|
import { MsgSuccess } from '@/utils/message';
|
||||||
|
|
||||||
const globalStore = GlobalStore();
|
const globalStore = GlobalStore();
|
||||||
const mobile = computed(() => {
|
const mobile = computed(() => {
|
||||||
|
|
@ -125,6 +128,7 @@ const selects = ref<any>([]);
|
||||||
const opRef = ref();
|
const opRef = ref();
|
||||||
|
|
||||||
const runRef = ref();
|
const runRef = ref();
|
||||||
|
const taskLogRef = ref();
|
||||||
|
|
||||||
const data = ref();
|
const data = ref();
|
||||||
const paginationConfig = reactive({
|
const paginationConfig = reactive({
|
||||||
|
|
@ -196,10 +200,12 @@ const onSync = async () => {
|
||||||
type: 'info',
|
type: 'info',
|
||||||
}).then(async () => {
|
}).then(async () => {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
await syncScript()
|
let taskID = newUUID();
|
||||||
|
await syncScript(taskID)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
search();
|
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
|
||||||
|
openTaskLog(taskID);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
|
|
@ -207,6 +213,10 @@ const onSync = async () => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const openTaskLog = (taskID: string) => {
|
||||||
|
taskLogRef.value.openWithTaskID(taskID);
|
||||||
|
};
|
||||||
|
|
||||||
const search = async () => {
|
const search = async () => {
|
||||||
let params = {
|
let params = {
|
||||||
info: searchInfo.value,
|
info: searchInfo.value,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue