MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / RunScript

Method RunScript

core/app/api/v2/script_library.go:147–246  ·  view source on GitHub ↗

@Tags ScriptLibrary @Summary Run script @Param cols query integer false "cols" @Param rows query integer false "rows" @Param script_id query integer false "script_id" @Param operateNode query string false "operateNode" @Success 200 @Security ApiKeyAuth @Security Timestamp @Router /core/script/run [g

(c *gin.Context)

Source from the content-addressed store, hash-verified

145// @Security Timestamp
146// @Router /core/script/run [get]
147func (b *BaseApi) RunScript(c *gin.Context) {
148 if !websocket.IsWebSocketUpgrade(c.Request) {
149 helper.Success(c)
150 return
151 }
152 wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
153 if err != nil {
154 global.LOG.Errorf("gin context http handler failed, err: %v", err)
155 return
156 }
157 defer wsConn.Close()
158
159 if global.CONF.Base.IsDemo {
160 if wshandleError(wsConn, errors.New(" demo server, prohibit this operation!")) {
161 return
162 }
163 }
164
165 cols, err := strconv.Atoi(c.DefaultQuery("cols", "80"))
166 if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) {
167 return
168 }
169 rows, err := strconv.Atoi(c.DefaultQuery("rows", "40"))
170 if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) {
171 return
172 }
173 scriptID := c.Query("script_id")
174 currentNode := c.Query("operateNode")
175 intNum, _ := strconv.Atoi(scriptID)
176 if intNum == 0 {
177 if wshandleError(wsConn, fmt.Errorf(" no such script %v in library, please check and try again!", scriptID)) {
178 return
179 }
180 }
181 scriptItem, err := service.LoadScriptInfo(uint(intNum))
182 if wshandleError(wsConn, err) {
183 return
184 }
185
186 quitChan := make(chan bool, 3)
187 if currentNode == "local" {
188 slave, err := terminal.NewCommand(scriptItem.Script)
189 if wshandleError(wsConn, err) {
190 return
191 }
192 defer slave.Close()
193
194 tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, true)
195 if wshandleError(wsConn, err) {
196 return
197 }
198
199 quitChan := make(chan bool, 3)
200 tty.Start(quitChan)
201 go slave.Wait(quitChan)
202 } else {
203 connInfo, _, err := xpack.MultiNodeProvider.LoadNodeInfo(currentNode)
204 if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) {

Callers

nothing calls this directly

Calls 12

StartMethod · 0.95
CloseMethod · 0.95
CloseMethod · 0.95
StartMethod · 0.95
WaitMethod · 0.95
SudoHandleCmdMethod · 0.80
RunfMethod · 0.80
wshandleErrorFunction · 0.70
UpgradeMethod · 0.65
CloseMethod · 0.65
LoadNodeInfoMethod · 0.65
WaitMethod · 0.45

Tested by

no test coverage detected