12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- package v1
- import (
- "bytes"
- json2 "encoding/json"
- "net/http"
- "reflect"
- "strconv"
- "strings"
- "time"
- "github.com/IceWhaleTech/CasaOS/model"
- "github.com/IceWhaleTech/CasaOS/pkg/docker"
- upnp2 "github.com/IceWhaleTech/CasaOS/pkg/upnp"
- "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
- ip_helper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/ip_helper"
- oasis_err2 "github.com/IceWhaleTech/CasaOS/pkg/utils/oasis_err"
- port2 "github.com/IceWhaleTech/CasaOS/pkg/utils/port"
- "github.com/IceWhaleTech/CasaOS/pkg/utils/random"
- "github.com/IceWhaleTech/CasaOS/service"
- "github.com/IceWhaleTech/CasaOS/service/docker_base"
- model2 "github.com/IceWhaleTech/CasaOS/service/model"
- "github.com/IceWhaleTech/CasaOS/types"
- "github.com/gin-gonic/gin"
- "github.com/gorilla/websocket"
- "github.com/jinzhu/copier"
- uuid "github.com/satori/go.uuid"
- "golang.org/x/crypto/ssh"
- )
- var upgrader = websocket.Upgrader{
- ReadBufferSize: 1024,
- WriteBufferSize: 1024,
- CheckOrigin: func(r *http.Request) bool { return true },
- HandshakeTimeout: time.Duration(time.Second * 5),
- }
- //打开docker的terminal
- func DockerTerminal(c *gin.Context) {
- col := c.DefaultQuery("cols", "100")
- row := c.DefaultQuery("rows", "30")
- conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
- return
- }
- defer conn.Close()
- container := c.Param("id")
- hr, err := service.Exec(container, row, col)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
- return
- }
- // 关闭I/O流
- defer hr.Close()
- // 退出进程
- defer func() {
- hr.Conn.Write([]byte("exit\r"))
- }()
- go func() {
- docker.WsWriterCopy(hr.Conn, conn)
- }()
- docker.WsReaderCopy(conn, hr.Conn)
- }
- //打开本机的ssh接口
- func WsSsh(c *gin.Context) {
- wsConn, _ := upgrader.Upgrade(c.Writer, c.Request, nil)
- var logBuff = new(bytes.Buffer)
- quitChan := make(chan bool, 3)
- user := ""
- password := ""
- var login int = 1
- cols, _ := strconv.Atoi(c.DefaultQuery("cols", "200"))
- rows, _ := strconv.Atoi(c.DefaultQuery("rows", "32"))
- var client *ssh.Client
- for login != 0 {
- var err error
- wsConn.WriteMessage(websocket.TextMessage, []byte("login:"))
- user = docker.ReceiveWsMsgUser(wsConn, logBuff)
- wsConn.WriteMessage(websocket.TextMessage, []byte("\r\n\x1b[0m"))
- wsConn.WriteMessage(websocket.TextMessage, []byte("password:"))
- password = docker.ReceiveWsMsgPassword(wsConn, logBuff)
- wsConn.WriteMessage(websocket.TextMessage, []byte("\r\n\x1b[0m"))
- client, err = docker.NewSshClient(user, password)
- if err != nil && client == nil {
- wsConn.WriteMessage(websocket.TextMessage, []byte(err.Error()))
- wsConn.WriteMessage(websocket.TextMessage, []byte("\r\n\x1b[0m"))
- } else {
- login = 0
- }
- }
- if client != nil {
- defer client.Close()
- }
- ssConn, _ := docker.NewSshConn(cols, rows, client)
- defer ssConn.Close()
- go ssConn.ReceiveWsMsg(wsConn, logBuff, quitChan)
- go ssConn.SendComboOutput(wsConn, quitChan)
- go ssConn.SessionWait(quitChan)
- <-quitChan
- }
- //安装进度推送
- func SpeedPush(c *gin.Context) {
- //token := c.Query("token")
- //if len(token) == 0 || token != config.UserInfo.Token {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR_AUTH_TOKEN, Message: oasis_err2.GetMsg(oasis_err2.ERROR_AUTH_TOKEN)})
- // return
- //}
- //ws, _ := upgrader.Upgrade(c.Writer, c.Request, nil)
- //defer ws.Close()
- //
- //for {
- // select {
- // case msg := <-WSMSG:
- // ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintln(msg)))
- // }
- //}
- }
- // @Summary 安装app(该接口需要post json数据)
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path int true "id"
- // @Param port formData int true "主端口"
- // @Param tcp formData string false "tcp端口"
- // @Param udp formData string false "udp端口"
- // @Param env formData string false "环境变量"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/install/{id} [post]
- func InstallApp(c *gin.Context) {
- appId := c.Param("id")
- var appInfo model.ServerAppList
- m := model.CustomizationPostData{}
- c.BindJSON(&m)
- const CUSTOM = "custom"
- var dockerImage string
- var dockerImageVersion string
- //检查端口
- if len(m.PortMap) > 0 && m.PortMap != "0" {
- //c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
- portMap, _ := strconv.Atoi(m.PortMap)
- if !port2.IsPortAvailable(portMap, "tcp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + m.PortMap})
- return
- }
- }
- //if len(m.Port) == 0 || m.Port == "0" {
- // m.Port = m.PortMap
- //}
- imageArr := strings.Split(m.Image, ":")
- if len(imageArr) == 2 {
- dockerImage = imageArr[0]
- dockerImageVersion = imageArr[1]
- } else {
- dockerImage = m.Image
- dockerImageVersion = "latest"
- }
- if m.Origin != "custom" {
- appInfo = service.MyService.App().GetServerAppInfo(appId)
- } else {
- appInfo.Title = m.Label
- appInfo.Description = m.Description
- appInfo.Icon = m.Icon
- appInfo.ScreenshotLink = model.Strings{}
- appInfo.NetworkModel = m.NetworkModel
- appInfo.Tags = model.Strings{}
- appInfo.Tagline = ""
- appInfo.Index = m.Index
- }
- for _, u := range m.Ports {
- if u.Protocol == "udp" {
- t, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(t, "udp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- } else if u.Protocol == "tcp" {
- te, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(te, "tcp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- } else if u.Protocol == "both" {
- t, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(t, "udp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- te, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(te, "tcp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- }
- }
- for _, device := range m.Devices {
- if file.CheckNotExist(device.Path) {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.DEVICE_NOT_EXIST, Message: device.Path + "," + oasis_err2.GetMsg(oasis_err2.DEVICE_NOT_EXIST)})
- return
- }
- }
- //restart := c.PostForm("restart") //always 总是重启, unless-stopped 除非用户手动停止容器,否则总是重新启动, on-failure:仅当容器退出代码非零时重新启动
- //if len(restart) > 0 {
- //
- //}
- //
- //privileged := c.PostForm("privileged") //是否处于特权模式
- //if len(privileged) > 0 {
- //
- //}
- id := uuid.NewV4().String()
- var relyMap = make(map[string]string)
- go func() {
- installLog := model2.AppNotify{}
- installLog.CustomId = id
- installLog.State = 0
- installLog.Message = "installing rely"
- installLog.Speed = 10
- installLog.Type = types.NOTIFY_TYPE_UNIMPORTANT
- installLog.CreatedAt = strconv.FormatInt(time.Now().Unix(), 10)
- installLog.UpdatedAt = strconv.FormatInt(time.Now().Unix(), 10)
- service.MyService.Notify().AddLog(installLog)
- if m.Origin != "custom" {
- for _, plugin := range appInfo.Plugins {
- if plugin == "mysql" {
- mid := uuid.NewV4().String()
- mc := docker_base.MysqlConfig{}
- mc.DataBasePassword = random.RandomString(6, false)
- mc.DataBaseDB = appInfo.Title
- mc.DataBaseUser = "root"
- mc.DataBasePort = "3306"
- mysqlContainerId, err := docker_base.MysqlCreate(mc, mid, m.CpuShares, m.Memory)
- if len(mysqlContainerId) > 0 && err == nil {
- mc.DataBaseHost = mid
- m.Envs = docker_base.MysqlFilter(mc, m.Envs)
- rely := model2.RelyDBModel{}
- rely.Type = types.RELY_TYPE_MYSQL
- rely.ContainerId = mysqlContainerId
- rely.CustomId = mid
- rely.ContainerCustomId = id
- var msqlConfig model2.MysqlConfigs
- //结构体转换
- copier.Copy(&msqlConfig, &mc)
- rely.Config = msqlConfig
- service.MyService.Rely().Create(rely)
- relyMap["mysql"] = mid
- } else {
- docker_base.MysqlDelete(mysqlContainerId)
- installLog.State = 0
- installLog.Speed = 30
- installLog.Message = err.Error()
- service.MyService.Notify().UpdateLog(installLog)
- }
- }
- }
- }
- installLog.Speed = 50
- installLog.Message = "pulling"
- service.MyService.Notify().UpdateLog(installLog)
- // step:下载镜像
- err := service.MyService.Docker().DockerPullImage(dockerImage+":"+dockerImageVersion, installLog)
- if err != nil {
- installLog.State = 0
- installLog.Speed = 70
- installLog.Message = err.Error()
- installLog.Type = types.NOTIFY_TYPE_ERROR
- service.MyService.Notify().UpdateLog(installLog)
- return
- }
- for !service.MyService.Docker().IsExistImage(dockerImage + ":" + dockerImageVersion) {
- time.Sleep(time.Second)
- }
- //if {
- //}
- //step:创建容器
- // networkName, err := service.MyService.Docker().GetNetWorkNameByNetWorkID(appInfo.NetworkModel)
- // if err != nil {
- // //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":80}", 100)
- // installLog.State = 0
- // installLog.Speed = 75
- // installLog.Type = types.NOTIFY_TYPE_ERROR
- // installLog.Message = err.Error()
- // service.MyService.Notify().UpdateLog(installLog)
- // return
- // }
- containerId, err := service.MyService.Docker().DockerContainerCreate(dockerImage+":"+dockerImageVersion, id, m, appInfo.NetworkModel)
- installLog.ContainerId = containerId
- if err != nil {
- //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":80}", 100)
- installLog.State = 0
- installLog.Speed = 80
- installLog.Type = types.NOTIFY_TYPE_ERROR
- installLog.Message = err.Error()
- service.MyService.Notify().UpdateLog(installLog)
- return
- } else {
- //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"starting\",\"speed\":80}", 100)
- installLog.Speed = 80
- installLog.Message = "starting"
- service.MyService.Notify().UpdateLog(installLog)
- }
- //step:启动容器
- err = service.MyService.Docker().DockerContainerStart(id)
- if err != nil {
- //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":90}", 100)
- installLog.State = 0
- installLog.Type = types.NOTIFY_TYPE_ERROR
- installLog.Speed = 90
- installLog.Message = err.Error()
- service.MyService.Notify().UpdateLog(installLog)
- return
- } else {
- //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"setting upnp\",\"speed\":90}", 100)
- installLog.Speed = 90
- if m.Origin != CUSTOM {
- installLog.Message = "setting upnp"
- } else {
- installLog.Message = "nearing completion"
- }
- service.MyService.Notify().UpdateLog(installLog)
- }
- if m.Origin != CUSTOM {
- //step:启动upnp
- if m.EnableUPNP {
- upnp, err := upnp2.Gateway()
- if err == nil {
- for _, p := range m.Ports {
- if p.Protocol == "udp" {
- upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- upnp.LocalHost = ip_helper2.GetLoclIp()
- tComment, _ := strconv.Atoi(p.CommendPort)
- upnp.AddPortMapping(tComment, tComment, "UDP")
- time.Sleep(time.Millisecond * 200)
- } else if p.Protocol == "tcp" {
- upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- upnp.LocalHost = ip_helper2.GetLoclIp()
- tComment, _ := strconv.Atoi(p.CommendPort)
- upnp.AddPortMapping(tComment, tComment, "TCP")
- time.Sleep(time.Millisecond * 200)
- } else if p.Protocol == "both" {
- upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- upnp.LocalHost = ip_helper2.GetLoclIp()
- tComment, _ := strconv.Atoi(p.CommendPort)
- upnp.AddPortMapping(tComment, tComment, "UDP")
- time.Sleep(time.Millisecond * 200)
- upnp.AddPortMapping(tComment, tComment, "TCP")
- time.Sleep(time.Millisecond * 200)
- }
- }
- }
- if err != nil {
- //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":95}", 100)
- installLog.State = 0
- installLog.Speed = 95
- installLog.Type = types.NOTIFY_TYPE_ERROR
- installLog.Message = err.Error()
- service.MyService.Notify().UpdateLog(installLog)
- } else {
- //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"checking\",\"speed\":95}", 100)
- installLog.Speed = 95
- installLog.Message = "checking"
- service.MyService.Notify().UpdateLog(installLog)
- }
- }
- }
- //step: 启动成功 检查容器状态确认启动成功
- container, err := service.MyService.Docker().DockerContainerInfo(id)
- if err != nil && container.ContainerJSONBase.State.Running {
- //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":100}", 100)
- installLog.State = 0
- installLog.Speed = 100
- installLog.Type = types.NOTIFY_TYPE_ERROR
- installLog.Message = err.Error()
- service.MyService.Notify().UpdateLog(installLog)
- return
- } else {
- //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"installed\",\"speed\":100}", 100)
- installLog.Speed = 100
- installLog.Message = "installed"
- service.MyService.Notify().UpdateLog(installLog)
- }
- rely := model.MapStrings{}
- copier.Copy(&rely, &relyMap)
- portsStr, _ := json2.Marshal(m.Ports)
- envsStr, _ := json2.Marshal(m.Envs)
- volumesStr, _ := json2.Marshal(m.Volumes)
- devicesStr, _ := json2.Marshal(m.Devices)
- //step: 保存数据到数据库
- md := model2.AppListDBModel{
- CustomId: id,
- Title: appInfo.Title,
- //ScreenshotLink: appInfo.ScreenshotLink,
- Slogan: appInfo.Tagline,
- Description: appInfo.Description,
- //Tags: appInfo.Tags,
- Icon: appInfo.Icon,
- Version: dockerImageVersion,
- ContainerId: containerId,
- Image: dockerImage,
- Index: appInfo.Index,
- //Port: m.Port,
- PortMap: m.PortMap,
- Label: m.Label,
- EnableUPNP: m.EnableUPNP,
- Ports: string(portsStr),
- Envs: string(envsStr),
- Volumes: string(volumesStr),
- Position: m.Position,
- NetModel: appInfo.NetworkModel,
- Restart: m.Restart,
- CpuShares: m.CpuShares,
- Memory: m.Memory,
- Devices: string(devicesStr),
- //Rely: rely,
- Origin: m.Origin,
- CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
- UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
- }
- //if appInfo.NetworkModel == "host" {
- // m.PortMap = m.Port
- //}
- service.MyService.App().SaveContainer(md)
- }()
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: id})
- }
- //// @Summary 自定义安装app(该接口需要post json数据)
- //// @Produce application/json
- //// @Accept application/json
- //// @Tags app
- //// @Param id path int true "id"
- //// @Param port formData int true "主端口"
- //// @Param tcp formData string false "tcp端口"
- //// @Param udp formData string false "udp端口"
- //// @Param env formData string false "环境变量"
- //// @Security ApiKeyAuth
- //// @Success 200 {string} string "ok"
- //// @Router /app/install/{id} [post]
- //func CustomInstallApp(c *gin.Context) {
- // //appId := c.Param("id")
- // // appInfo := service.MyService.App().GetServerAppInfo(appId)
- //
- // m := model.CustomizationPostData{}
- // c.BindJSON(&m)
- // //检查端口
- // if len(m.PortMap) == 0 || m.PortMap == "0" {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
- // return
- // }
- // if len(m.Port) == 0 || m.Port == "0" {
- // m.Port = m.PortMap
- // }
- //
- // portMap, _ := strconv.Atoi(m.PortMap)
- // if !port2.IsPortAvailable(portMap, "tcp") {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + m.PortMap})
- // return
- // }
- //
- // for _, u := range m.Udp {
- // t, _ := strconv.Atoi(u.CommendPort)
- // if !port2.IsPortAvailable(t, "udp") {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- // return
- // }
- // }
- //
- // for _, t := range m.Tcp {
- // te, _ := strconv.Atoi(t.CommendPort)
- // if !port2.IsPortAvailable(te, "tcp") {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + t.CommendPort})
- // return
- // }
- // }
- //
- // //restart := c.PostForm("restart") //always 总是重启, unless-stopped 除非用户手动停止容器,否则总是重新启动, on-failure:仅当容器退出代码非零时重新启动
- // //if len(restart) > 0 {
- // //
- // //}
- // //
- // //privileged := c.PostForm("privileged") //是否处于特权模式
- // //if len(privileged) > 0 {
- // //
- // //}
- //
- // err := service.MyService.Docker().DockerPullImage(m.Image)
- // if err != nil {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.PULL_IMAGE_ERROR, Message: oasis_err2.GetMsg(oasis_err2.PULL_IMAGE_ERROR)})
- // }
- //
- // id := uuid.NewV4().String()
- //
- // var relyMap = make(map[string]string)
- // go func() {
- // installLog := model2.AppNotify{}
- // installLog.CustomId = id
- // installLog.State = 0
- // installLog.Message = "installing rely"
- // installLog.Speed = 30
- // installLog.CreatedAt = time.Now()
- // installLog.UpdatedAt = time.Now()
- // service.MyService.Notify().AddLog(installLog)
- //
- // for !service.MyService.Docker().IsExistImage(m.Image) {
- // time.Sleep(time.Second)
- // }
- //
- // installLog.Speed = 50
- // installLog.Message = "pulling"
- // service.MyService.Notify().UpdateLog(installLog)
- // // step:下载镜像
- //
- // var cpd model.PostData
- // copier.Copy(&cpd, &m)
- // //step:创建容器
- // containerId, err := service.MyService.Docker().DockerContainerCreate(m.Image, id, cpd, m.NetworkModel, m.Image, "custom")
- // installLog.ContainerId = containerId
- // if err != nil {
- // //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":80}", 100)
- // installLog.State = 0
- // installLog.Speed = 80
- // installLog.Message = err.Error()
- // service.MyService.Notify().UpdateLog(installLog)
- // return
- // } else {
- // //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"starting\",\"speed\":80}", 100)
- // installLog.Speed = 80
- // installLog.Message = "starting"
- // service.MyService.Notify().UpdateLog(installLog)
- // }
- //
- // //step:启动容器
- // err = service.MyService.Docker().DockerContainerStart(id)
- // if err != nil {
- // //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":90}", 100)
- // installLog.State = 0
- // installLog.Speed = 90
- // installLog.Message = err.Error()
- // service.MyService.Notify().UpdateLog(installLog)
- // return
- // } else {
- // //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"setting upnp\",\"speed\":90}", 100)
- // installLog.Speed = 90
- // installLog.Message = "setting upnp"
- // service.MyService.Notify().UpdateLog(installLog)
- // }
- //
- // //step: 启动成功 检查容器状态确认启动成功
- // containerStatus, err := service.MyService.Docker().DockerContainerInfo(id)
- // if err != nil && containerStatus.ContainerJSONBase.State.Running {
- // //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":100}", 100)
- // installLog.State = 0
- // installLog.Speed = 100
- // installLog.Message = err.Error()
- // service.MyService.Notify().UpdateLog(installLog)
- // return
- // } else {
- // //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"installed\",\"speed\":100}", 100)
- // installLog.Speed = 100
- // installLog.Message = "installed"
- // service.MyService.Notify().UpdateLog(installLog)
- // }
- //
- // rely := model.MapStrings{}
- //
- // copier.Copy(&rely, &relyMap)
- //
- // //step: 保存数据到数据库
- // md := model2.AppListDBModel{
- // CustomId: id,
- // Title: m.Label,
- // // ScreenshotLink: []string,
- // Slogan: "",
- // Description: m.Description,
- // // Tags: ,
- // Icon: m.Icon,
- // Version: m.Image,
- // ContainerId: containerId,
- // Image: m.Image,
- // Index: "",
- // Port: m.Port,
- // PortMap: m.PortMap,
- // Label: m.Label,
- // EnableUPNP: m.EnableUPNP,
- // UdpPorts: m.Udp,
- // TcpPorts: m.Tcp,
- // Envs: m.Envs,
- // Volumes: m.Volumes,
- // Position: m.Position,
- // NetModel: m.NetworkModel,
- // Restart: m.Restart,
- // CpuShares: m.CpuShares,
- // Memory: m.Memory,
- // Devices: m.Devices,
- // Rely: rely,
- // Origin: "custom",
- // }
- // if m.NetworkModel == "host" {
- // m.PortMap = m.Port
- // }
- // service.MyService.App().SaveContainer(md)
- //
- // }()
- //
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: id})
- //
- //}
- // @Summary 卸载app
- // @Produce application/json
- // @Accept multipart/form-data
- // @Tags app
- // @Param id path string true "容器id"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/uninstall/{id} [delete]
- func UnInstallApp(c *gin.Context) {
- appId := c.Param("id")
- if len(appId) == 0 {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
- return
- }
- info := service.MyService.App().GetUninstallInfo(appId)
- //step:停止容器
- err := service.MyService.Docker().DockerContainerStop(appId)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.UNINSTALL_APP_ERROR, Message: oasis_err2.GetMsg(oasis_err2.UNINSTALL_APP_ERROR), Data: err.Error()})
- return
- }
- //step:删除容器
- err = service.MyService.Docker().DockerContainerRemove(appId)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.UNINSTALL_APP_ERROR, Message: oasis_err2.GetMsg(oasis_err2.UNINSTALL_APP_ERROR), Data: err.Error()})
- return
- }
- //存在镜像正在使用的情况
- // step:删除镜像
- service.MyService.Docker().DockerImageRemove(info.Image + ":" + info.Version)
- //step: 删除本地数据
- service.MyService.App().RemoveContainerById(appId)
- if info.Origin != "custom" {
- //step: 删除文件夹
- service.MyService.App().DelAppConfigDir(appId)
- //step: 删除install log
- service.MyService.Notify().DelLog(appId)
- // for k, v := range info.Rely {
- //
- // if k == "mysql" {
- // docker_base.MysqlDelete(v)
- // service.MyService.Rely().Delete(v)
- // }
- // }
- //if info.EnableUPNP {
- // upnp, err := upnp2.Gateway()
- // if err == nil {
- // for _, p := range info.Ports {
- // if p.Protocol == "udp" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.DelPortMapping(tComment, "UDP")
- // time.Sleep(time.Millisecond * 200)
- // } else if p.Protocol == "tcp" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.DelPortMapping(tComment, "TCP")
- // time.Sleep(time.Millisecond * 200)
- // } else if p.Protocol == "both" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.DelPortMapping(tComment, "UDP")
- //
- // upnp.DelPortMapping(tComment, "TCP")
- // time.Sleep(time.Millisecond * 200)
- // }
- // }
- // }
- //}
- }
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
- }
- // @Summary 修改app状态
- // @Produce application/json
- // @Accept multipart/form-data
- // @Tags app
- // @Param id path string true "appid"
- // @Param state query string false "是否停止 start stop restart"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/state/{id} [put]
- func ChangAppState(c *gin.Context) {
- appId := c.Param("id")
- state := c.DefaultPostForm("state", "stop")
- var err error
- if state == "stop" {
- err = service.MyService.Docker().DockerContainerStop(appId)
- } else if state == "start" {
- err = service.MyService.Docker().DockerContainerStart(appId)
- } else if state == "restart" {
- err = service.MyService.Docker().DockerContainerStop(appId)
- err = service.MyService.Docker().DockerContainerStart(appId)
- }
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
- return
- }
- info, err := service.MyService.App().GetContainerInfo(appId)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
- return
- }
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: info.State})
- }
- // @Summary 查看容器日志
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path string true "appid"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/logs/{id} [get]
- func ContainerLog(c *gin.Context) {
- appId := c.Param("id")
- log, _ := service.MyService.Docker().DockerContainerLog(appId)
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: log})
- }
- // @Summary 获取安装进度
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path string true "容器id"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/speed/{id} [get]
- func GetInstallSpeed(c *gin.Context) {
- id := c.Param("id")
- b := service.MyService.Notify().GetLog(id)
- b.Id = b.CustomId
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: b})
- }
- // @Summary 获取容器状态
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path string true "容器id"
- // @Param type query string false "type=1"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/state/{id} [get]
- func GetContainerState(c *gin.Context) {
- id := c.Param("id")
- t := c.DefaultQuery("type", "0")
- containerInfo, e := service.MyService.App().GetSimpleContainerInfo(id)
- if e != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: e.Error()})
- return
- }
- var data = make(map[string]interface{})
- data["state"] = containerInfo.State
- if t == "1" {
- appInfo := service.MyService.App().GetAppDBInfo(id)
- data["app"] = appInfo
- }
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: data})
- }
- // @Summary 更新设置
- // @Produce application/json
- // @Accept multipart/form-data
- // @Tags app
- // @Param id path string true "容器id"
- // @Param shares formData string false "cpu权重"
- // @Param mem formData string false "内存大小MB"
- // @Param restart formData string false "重启策略"
- // @Param label formData string false "应用名称"
- // @Param position formData bool true "是否放到首页"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/update/{id}/setting [put]
- func UpdateSetting(c *gin.Context) {
- id := c.Param("id")
- const CUSTOM = "custom"
- m := model.CustomizationPostData{}
- c.BindJSON(&m)
- if len(id) == 0 {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
- return
- }
- var cpd model.CustomizationPostData
- copier.Copy(&cpd, &m)
- appInfo := service.MyService.App().GetAppDBInfo(id)
- var containerId string
- containerId = appInfo.ContainerId
- service.MyService.Docker().DockerContainerStop(id)
- portMap, _ := strconv.Atoi(m.PortMap)
- if !port2.IsPortAvailable(portMap, "tcp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + m.PortMap})
- return
- }
- for _, u := range m.Ports {
- if u.Protocol == "udp" {
- t, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(t, "udp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- } else if u.Protocol == "tcp" {
- te, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(te, "tcp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- } else if u.Protocol == "both" {
- t, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(t, "udp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- te, _ := strconv.Atoi(u.CommendPort)
- if !port2.IsPortAvailable(te, "tcp") {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: "Duplicate port:" + u.CommendPort})
- return
- }
- }
- }
- //如果容器端口均未修改,这不进行处理
- portsStr, _ := json2.Marshal(m.Ports)
- envsStr, _ := json2.Marshal(m.Envs)
- volumesStr, _ := json2.Marshal(m.Volumes)
- devicesStr, _ := json2.Marshal(m.Devices)
- if !reflect.DeepEqual(string(portsStr), appInfo.Ports) || !reflect.DeepEqual(string(envsStr), appInfo.Envs) || !reflect.DeepEqual(string(volumesStr), appInfo.Volumes) || m.PortMap != appInfo.PortMap || m.NetworkModel != appInfo.NetModel {
- var newUUid = uuid.NewV4().String()
- var err error
- // networkName, err := service.MyService.Docker().GetNetWorkNameByNetWorkID(appInfo.NetModel)
- // if err != nil {
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
- // return
- // }
- containerId, err = service.MyService.Docker().DockerContainerCreate(appInfo.Image+":"+appInfo.Version, newUUid, cpd, m.NetworkModel)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
- return
- }
- err = service.MyService.Docker().DockerContainerRemove(id)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
- return
- }
- service.MyService.Docker().DockerContainerUpdateName(appInfo.CustomId, newUUid)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
- return
- }
- } else if !reflect.DeepEqual(string(devicesStr), appInfo.Devices) || m.CpuShares != appInfo.CpuShares || m.Memory != appInfo.Memory || m.Restart != appInfo.Restart {
- service.MyService.Docker().DockerContainerUpdate(cpd, id)
- }
- err := service.MyService.Docker().DockerContainerStart(id)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
- return
- }
- //更新upnp
- if m.Origin != CUSTOM {
- //if appInfo.EnableUPNP != appInfo.EnableUPNP {
- // if appInfo.EnableUPNP {
- // upnp, err := upnp2.Gateway()
- // if err == nil {
- //
- // for _, p := range appInfo.Ports {
- // if p.Protocol == "udp" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.AddPortMapping(tComment, tComment, "UDP")
- // time.Sleep(time.Millisecond * 200)
- // } else if p.Protocol == "tcp" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.AddPortMapping(tComment, tComment, "TCP")
- // time.Sleep(time.Millisecond * 200)
- // } else if p.Protocol == "both" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.AddPortMapping(tComment, tComment, "UDP")
- // time.Sleep(time.Millisecond * 200)
- //
- // upnp.AddPortMapping(tComment, tComment, "TCP")
- // time.Sleep(time.Millisecond * 200)
- // }
- // }
- // }
- // } else {
- // upnp, err := upnp2.Gateway()
- // if err == nil {
- // for _, p := range appInfo.Ports {
- // if p.Protocol == "udp" {
- //
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.DelPortMapping(tComment, "UDP")
- // time.Sleep(time.Millisecond * 200)
- // } else if p.Protocol == "tcp" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.DelPortMapping(tComment, "TCP")
- // time.Sleep(time.Millisecond * 200)
- // } else if p.Protocol == "both" {
- // upnp.CtrlUrl = upnp2.GetCtrlUrl(upnp.GatewayHost, upnp.DeviceDescUrl)
- // upnp.LocalHost = ip_helper2.GetLoclIp()
- // tComment, _ := strconv.Atoi(p.CommendPort)
- // upnp.DelPortMapping(tComment, "UDP")
- // time.Sleep(time.Millisecond * 200)
- //
- // upnp.DelPortMapping(tComment, "TCP")
- // time.Sleep(time.Millisecond * 200)
- // }
- // }
- // }
- // }
- //}
- }
- appInfo.ContainerId = containerId
- appInfo.PortMap = m.PortMap
- appInfo.Label = m.Label
- appInfo.Index = m.Index
- appInfo.Ports = string(portsStr)
- appInfo.Envs = string(envsStr)
- appInfo.Icon = m.Icon
- appInfo.Volumes = string(volumesStr)
- appInfo.Devices = string(devicesStr)
- appInfo.NetModel = m.NetworkModel
- appInfo.Position = m.Position
- appInfo.EnableUPNP = m.EnableUPNP
- appInfo.Restart = m.Restart
- appInfo.Memory = m.Memory
- appInfo.CpuShares = m.CpuShares
- appInfo.UpdatedAt = strconv.FormatInt(time.Now().Unix(), 10)
- service.MyService.App().UpdateApp(appInfo)
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
- }
- // @Summary 获取容器详情
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path string true "appid"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/info/{id} [get]
- func ContainerInfo(c *gin.Context) {
- appId := c.Param("id")
- appInfo := service.MyService.App().GetAppDBInfo(appId)
- containerInfo, _ := service.MyService.Docker().DockerContainerStats(appId)
- var cpuModel = "arm"
- if cpu := service.MyService.ZiMa().GetCpuInfo(); len(cpu) > 0 {
- if strings.Count(strings.ToLower(strings.TrimSpace(cpu[0].ModelName)), "intel") > 0 {
- cpuModel = "intel"
- } else if strings.Count(strings.ToLower(strings.TrimSpace(cpu[0].ModelName)), "amd") > 0 {
- cpuModel = "amd"
- }
- }
- info, err := service.MyService.Docker().DockerContainerInfo(appId)
- if err != nil {
- //todo 需要自定义错误
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: err.Error()})
- return
- }
- con := struct {
- Status string `json:"status"`
- StartedAt string `json:"started_at"`
- CPUShares int64 `json:"cpu_shares"`
- Memory int64 `json:"memory"`
- Restart string `json:"restart"`
- }{Status: info.State.Status, StartedAt: info.State.StartedAt, CPUShares: info.HostConfig.CPUShares, Memory: info.HostConfig.Memory >> 20, Restart: info.HostConfig.RestartPolicy.Name}
- data := make(map[string]interface{}, 5)
- data["app"] = appInfo
- data["cpu"] = cpuModel
- data["memory"] = service.MyService.ZiMa().GetMemInfo().Total
- data["container"] = json2.RawMessage(containerInfo)
- data["info"] = con
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: data})
- }
- // @Summary 获取安装所需要的数据
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/install/config [get]
- func GetDockerInstallConfig(c *gin.Context) {
- networks := service.MyService.Docker().DockerNetworkModelList()
- data := make(map[string]interface{}, 2)
- list := []map[string]string{}
- for _, network := range networks {
- if network.Driver != "null" {
- list = append(list, map[string]string{"name": network.Name, "driver": network.Driver, "id": network.ID})
- }
- }
- data["networks"] = list
- data["memory"] = service.MyService.ZiMa().GetMemInfo()
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: data})
- }
- // @Summary 获取依赖数据
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path string true "rely id"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/rely/{id}/info [get]
- func ContainerRelyInfo(c *gin.Context) {
- id := c.Param("id")
- appInfo := service.MyService.Rely().GetInfo(id)
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: appInfo})
- }
- // @Summary 获取可更新数据
- // @Produce application/json
- // @Accept application/json
- // @Tags app
- // @Param id path string true "appid"
- // @Security ApiKeyAuth
- // @Success 200 {string} string "ok"
- // @Router /app/update/{id}/info [get]
- func ContainerUpdateInfo(c *gin.Context) {
- appId := c.Param("id")
- appInfo := service.MyService.App().GetAppDBInfo(appId)
- info, err := service.MyService.Docker().DockerContainerInfo(appId)
- if err != nil {
- //todo 需要自定义错误
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: err.Error()})
- return
- }
- var port model.PortArrey
- json2.Unmarshal([]byte(appInfo.Ports), &port)
- var envs model.EnvArrey
- json2.Unmarshal([]byte(appInfo.Envs), &envs)
- var vol model.PathArrey
- json2.Unmarshal([]byte(appInfo.Volumes), &vol)
- var dir model.PathArrey
- json2.Unmarshal([]byte(appInfo.Devices), &dir)
- //volumesStr, _ := json2.Marshal(m.Volumes)
- //devicesStr, _ := json2.Marshal(m.Devices)
- m := model.CustomizationPostData{}
- m.Index = appInfo.Index
- m.Icon = appInfo.Icon
- m.Ports = port
- m.Image = appInfo.Image + ":" + appInfo.Version
- m.Origin = appInfo.Origin
- m.NetworkModel = appInfo.NetModel
- m.Description = appInfo.Description
- m.Label = appInfo.Label
- m.PortMap = appInfo.PortMap
- m.Devices = dir //appInfo.Devices
- m.Envs = envs
- m.Memory = info.HostConfig.Memory >> 20
- m.CpuShares = info.HostConfig.CPUShares
- m.Volumes = vol //appInfo.Volumes
- m.Restart = info.HostConfig.RestartPolicy.Name
- m.EnableUPNP = appInfo.EnableUPNP
- m.Position = appInfo.Position
- c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: m})
- }
- ////准备安装(暂时不需要)
- //func ReadyInstall(c *gin.Context) {
- // _, tcp, udp := service.MyService.GetManifestJsonByRepo()
- // data := make(map[string]interface{}, 2)
- // if t := gjson.Parse(tcp).Array(); len(t) > 0 {
- // //tcpList := []model.TcpPorts{}
- // //e := json2.Unmarshal([]byte(tcp), tcpList)
- // //if e!=nil {
- // // return
- // //}
- // //for _, port := range tcpList {
- // // if port.ContainerPort>0&&port.ExtranetPort {
- // //
- // // }
- // //}
- // var inarr []interface{}
- // for _, result := range t {
- //
- // var p int
- // ok := true
- // for ok {
- // p, _ = port.GetAvailablePort()
- // ok = !port.IsPortAvailable(p)
- // }
- // pm := model.PortMap{gjson.Get(result.Raw, "container_port").Int(), p}
- // inarr = append(inarr, pm)
- // }
- // data["tcp"] = inarr
- // }
- // if u := gjson.Parse(udp).Array(); len(u) > 0 {
- // //udpList := []model.UdpPorts{}
- // //e := json2.Unmarshal([]byte(udp), udpList)
- // //if e != nil {
- // // return
- // //}
- // var inarr []model.PortMap
- // for _, result := range u {
- // var p int
- // ok := true
- // for ok {
- // p, _ = port.GetAvailablePort()
- // ok = !port.IsPortAvailable(p)
- // }
- // pm := model.PortMap{gjson.Get(result.Raw, "container_port").Int(), p}
- // inarr = append(inarr, pm)
- // }
- // data["udp"] = inarr
- // }
- // c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS), Data: data})
- //}
|