|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/1Panel-dev/1Panel/core/app/api/v2/helper" |
| 10 | + "github.com/1Panel-dev/1Panel/core/app/dto" |
| 11 | + "github.com/1Panel-dev/1Panel/core/app/service" |
| 12 | + "github.com/1Panel-dev/1Panel/core/global" |
| 13 | + "github.com/1Panel-dev/1Panel/core/utils/ssh" |
| 14 | + "github.com/1Panel-dev/1Panel/core/utils/terminal" |
| 15 | + "github.com/1Panel-dev/1Panel/core/utils/xpack" |
| 16 | + "github.com/gin-gonic/gin" |
| 17 | + "github.com/pkg/errors" |
| 18 | +) |
| 19 | + |
| 20 | +// @Tags ScriptLibrary |
| 21 | +// @Summary Add script |
| 22 | +// @Accept json |
| 23 | +// @Param request body dto.ScriptOperate true "request" |
| 24 | +// @Success 200 |
| 25 | +// @Security ApiKeyAuth |
| 26 | +// @Security Timestamp |
| 27 | +// @Router /script [post] |
| 28 | +// @x-panel-log {"bodyKeys":["tag","name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"添加脚本库脚本 [tag][name]","formatEN":"add script [tag][name]"} |
| 29 | +func (b *BaseApi) CreateScript(c *gin.Context) { |
| 30 | + var req dto.ScriptOperate |
| 31 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if err := scriptService.Create(req); err != nil { |
| 36 | + helper.InternalServer(c, err) |
| 37 | + return |
| 38 | + } |
| 39 | + helper.SuccessWithOutData(c) |
| 40 | +} |
| 41 | + |
| 42 | +// @Tags ScriptLibrary |
| 43 | +// @Summary Page script |
| 44 | +// @Accept json |
| 45 | +// @Param request body dto.SearchPageWithGroup true "request" |
| 46 | +// @Success 200 {object} dto.PageResult |
| 47 | +// @Security ApiKeyAuth |
| 48 | +// @Security Timestamp |
| 49 | +// @Router /script/search [post] |
| 50 | +func (b *BaseApi) SearchScript(c *gin.Context) { |
| 51 | + var req dto.SearchPageWithGroup |
| 52 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + total, list, err := scriptService.Search(req) |
| 57 | + if err != nil { |
| 58 | + helper.InternalServer(c, err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + helper.SuccessWithData(c, dto.PageResult{ |
| 63 | + Items: list, |
| 64 | + Total: total, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +// @Tags ScriptLibrary |
| 69 | +// @Summary Delete script |
| 70 | +// @Accept json |
| 71 | +// @Param request body dto.BatchDeleteReq true "request" |
| 72 | +// @Success 200 |
| 73 | +// @Security ApiKeyAuth |
| 74 | +// @Security Timestamp |
| 75 | +// @Router /script/del [post] |
| 76 | +// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"script_librarys","output_column":"name","output_value":"names"}],"formatZH":"删除脚本库脚本 [names]","formatEN":"delete script [names]"} |
| 77 | +func (b *BaseApi) DeleteScript(c *gin.Context) { |
| 78 | + var req dto.OperateByIDs |
| 79 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if err := scriptService.Delete(req); err != nil { |
| 84 | + helper.InternalServer(c, err) |
| 85 | + return |
| 86 | + } |
| 87 | + helper.SuccessWithOutData(c) |
| 88 | +} |
| 89 | + |
| 90 | +// @Tags ScriptLibrary |
| 91 | +// @Summary Update script |
| 92 | +// @Accept json |
| 93 | +// @Param request body dto.ScriptOperate true "request" |
| 94 | +// @Success 200 |
| 95 | +// @Security ApiKeyAuth |
| 96 | +// @Security Timestamp |
| 97 | +// @Router /script/update [post] |
| 98 | +// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"更新脚本库脚本 [name]","formatEN":"update script [name]"} |
| 99 | +func (b *BaseApi) UpdateScript(c *gin.Context) { |
| 100 | + var req dto.ScriptOperate |
| 101 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + if err := scriptService.Update(req); err != nil { |
| 106 | + helper.InternalServer(c, err) |
| 107 | + return |
| 108 | + } |
| 109 | + helper.SuccessWithOutData(c) |
| 110 | +} |
| 111 | + |
| 112 | +func (b *BaseApi) RunScript(c *gin.Context) { |
| 113 | + wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) |
| 114 | + if err != nil { |
| 115 | + global.LOG.Errorf("gin context http handler failed, err: %v", err) |
| 116 | + return |
| 117 | + } |
| 118 | + defer wsConn.Close() |
| 119 | + |
| 120 | + if global.CONF.Base.IsDemo { |
| 121 | + if wshandleError(wsConn, errors.New(" demo server, prohibit this operation!")) { |
| 122 | + return |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + cols, err := strconv.Atoi(c.DefaultQuery("cols", "80")) |
| 127 | + if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) { |
| 128 | + return |
| 129 | + } |
| 130 | + rows, err := strconv.Atoi(c.DefaultQuery("rows", "40")) |
| 131 | + if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) { |
| 132 | + return |
| 133 | + } |
| 134 | + scriptID := c.Query("script_id") |
| 135 | + currentNode := c.Query("current_node") |
| 136 | + intNum, _ := strconv.Atoi(scriptID) |
| 137 | + if intNum == 0 { |
| 138 | + if wshandleError(wsConn, fmt.Errorf(" no such script %v in library, please check and try again!", scriptID)) { |
| 139 | + return |
| 140 | + } |
| 141 | + } |
| 142 | + scriptItem, err := service.LoadScriptInfo(uint(intNum)) |
| 143 | + if wshandleError(wsConn, err) { |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + fileName := strings.ReplaceAll(scriptItem.Name, " ", "_") |
| 148 | + quitChan := make(chan bool, 3) |
| 149 | + if currentNode == "local" { |
| 150 | + tmpFile := path.Join(global.CONF.Base.InstallDir, "1panel/tmp/script") |
| 151 | + initCmd := fmt.Sprintf("d=%s && mkdir -p $d && echo %s > $d/%s && clear && bash $d/%s", tmpFile, scriptItem.Script, fileName, fileName) |
| 152 | + slave, err := terminal.NewCommand(initCmd) |
| 153 | + if wshandleError(wsConn, err) { |
| 154 | + return |
| 155 | + } |
| 156 | + defer slave.Close() |
| 157 | + |
| 158 | + tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, true) |
| 159 | + if wshandleError(wsConn, err) { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + quitChan := make(chan bool, 3) |
| 164 | + tty.Start(quitChan) |
| 165 | + go slave.Wait(quitChan) |
| 166 | + } else { |
| 167 | + connInfo, installDir, err := xpack.LoadNodeInfo(currentNode) |
| 168 | + if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) { |
| 169 | + return |
| 170 | + } |
| 171 | + tmpFile := path.Join(installDir, "1panel/tmp/script") |
| 172 | + initCmd := fmt.Sprintf("d=%s && mkdir -p $d && echo %s > $d/%s && clear && bash $d/%s", tmpFile, scriptItem.Script, fileName, fileName) |
| 173 | + client, err := ssh.NewClient(*connInfo) |
| 174 | + if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) { |
| 175 | + return |
| 176 | + } |
| 177 | + defer client.Close() |
| 178 | + |
| 179 | + sws, err := terminal.NewLogicSshWsSession(cols, rows, client.Client, wsConn, initCmd) |
| 180 | + if wshandleError(wsConn, err) { |
| 181 | + return |
| 182 | + } |
| 183 | + defer sws.Close() |
| 184 | + sws.Start(quitChan) |
| 185 | + go sws.Wait(quitChan) |
| 186 | + } |
| 187 | + |
| 188 | + <-quitChan |
| 189 | + |
| 190 | + global.LOG.Info("websocket finished") |
| 191 | + if wshandleError(wsConn, err) { |
| 192 | + return |
| 193 | + } |
| 194 | +} |
0 commit comments