2021-11-09 10:57:50 +00:00
|
|
|
|
package route
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"encoding/xml"
|
2022-01-20 10:38:59 +00:00
|
|
|
|
"fmt"
|
2021-11-09 10:57:50 +00:00
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/model/system_app"
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/docker"
|
2021-12-09 11:02:41 +00:00
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
|
2021-11-09 10:57:50 +00:00
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/env_helper"
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/port"
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
|
|
|
|
model2 "github.com/IceWhaleTech/CasaOS/service/model"
|
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func InitFunction() {
|
|
|
|
|
go checkSystemApp()
|
2021-12-09 11:02:41 +00:00
|
|
|
|
Update2_3()
|
2021-12-29 08:42:20 +00:00
|
|
|
|
CheckSerialDiskMount()
|
|
|
|
|
|
2022-03-16 07:41:14 +00:00
|
|
|
|
CheckToken2_11()
|
2022-02-17 10:43:25 +00:00
|
|
|
|
|
2021-11-09 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var syncIsExistence = false
|
|
|
|
|
|
|
|
|
|
func installSyncthing(appId string) {
|
|
|
|
|
|
|
|
|
|
var appInfo model.ServerAppList
|
|
|
|
|
m := model.CustomizationPostData{}
|
|
|
|
|
var dockerImage string
|
|
|
|
|
var dockerImageVersion string
|
2022-03-09 08:37:03 +00:00
|
|
|
|
appInfo = service.MyService.Casa().GetServerAppInfo(appId, "system", "us_en")
|
2021-11-09 10:57:50 +00:00
|
|
|
|
dockerImage = appInfo.Image
|
2022-01-20 10:38:59 +00:00
|
|
|
|
dockerImageVersion = appInfo.ImageVersion
|
2021-11-09 10:57:50 +00:00
|
|
|
|
|
|
|
|
|
if len(appInfo.ImageVersion) == 0 {
|
|
|
|
|
dockerImageVersion = "latest"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if appInfo.NetworkModel != "host" {
|
|
|
|
|
for i := 0; i < len(appInfo.Ports); i++ {
|
|
|
|
|
if p, _ := strconv.Atoi(appInfo.Ports[i].ContainerPort); port.IsPortAvailable(p, appInfo.Ports[i].Protocol) {
|
|
|
|
|
appInfo.Ports[i].CommendPort = strconv.Itoa(p)
|
|
|
|
|
} else {
|
|
|
|
|
if appInfo.Ports[i].Protocol == "tcp" {
|
|
|
|
|
if p, err := port.GetAvailablePort("tcp"); err == nil {
|
|
|
|
|
appInfo.Ports[i].CommendPort = strconv.Itoa(p)
|
|
|
|
|
}
|
|
|
|
|
} else if appInfo.Ports[i].Protocol == "upd" {
|
|
|
|
|
if p, err := port.GetAvailablePort("udp"); err == nil {
|
|
|
|
|
appInfo.Ports[i].CommendPort = strconv.Itoa(p)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if appInfo.Ports[i].Type == 0 {
|
|
|
|
|
appInfo.PortMap = appInfo.Ports[i].CommendPort
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(appInfo.Devices); i++ {
|
|
|
|
|
if !file.CheckNotExist(appInfo.Devices[i].ContainerPath) {
|
|
|
|
|
appInfo.Devices[i].Path = appInfo.Devices[i].ContainerPath
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(appInfo.Tip) > 0 {
|
|
|
|
|
appInfo.Tip = env_helper.ReplaceStringDefaultENV(appInfo.Tip)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appInfo.MaxMemory = service.MyService.ZiMa().GetMemInfo().Total >> 20
|
|
|
|
|
|
|
|
|
|
id := uuid.NewV4().String()
|
|
|
|
|
|
|
|
|
|
installLog := model2.AppNotify{}
|
|
|
|
|
|
|
|
|
|
// step:下载镜像
|
|
|
|
|
err := service.MyService.Docker().DockerPullImage(dockerImage+":"+dockerImageVersion, installLog)
|
|
|
|
|
if err != nil {
|
|
|
|
|
//pull image error
|
2022-01-20 10:38:59 +00:00
|
|
|
|
fmt.Println("pull image error", err, dockerImage, dockerImageVersion)
|
2021-11-09 10:57:50 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for !service.MyService.Docker().IsExistImage(dockerImage + ":" + dockerImageVersion) {
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.CpuShares = 50
|
|
|
|
|
m.Envs = appInfo.Envs
|
|
|
|
|
m.Memory = int64(appInfo.MaxMemory)
|
|
|
|
|
m.Origin = "system"
|
|
|
|
|
m.PortMap = appInfo.PortMap
|
|
|
|
|
m.Ports = appInfo.Ports
|
2021-12-01 08:25:56 +00:00
|
|
|
|
m.Restart = "always"
|
2021-11-09 10:57:50 +00:00
|
|
|
|
m.Volumes = appInfo.Volumes
|
|
|
|
|
|
|
|
|
|
containerId, err := service.MyService.Docker().DockerContainerCreate(dockerImage+":"+dockerImageVersion, id, m, appInfo.NetworkModel)
|
|
|
|
|
if err != nil {
|
2022-01-20 10:38:59 +00:00
|
|
|
|
fmt.Println("container create error", err)
|
2021-11-09 10:57:50 +00:00
|
|
|
|
// create container error
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//step:start container
|
|
|
|
|
err = service.MyService.Docker().DockerContainerStart(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
//start container error
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
portsStr, _ := json.Marshal(appInfo.Ports)
|
|
|
|
|
envsStr, _ := json.Marshal(appInfo.Envs)
|
|
|
|
|
volumesStr, _ := json.Marshal(appInfo.Volumes)
|
|
|
|
|
devicesStr, _ := json.Marshal(appInfo.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,
|
|
|
|
|
PortMap: appInfo.PortMap,
|
|
|
|
|
Label: appInfo.Title,
|
|
|
|
|
EnableUPNP: false,
|
|
|
|
|
Ports: string(portsStr),
|
|
|
|
|
Envs: string(envsStr),
|
|
|
|
|
Volumes: string(volumesStr),
|
|
|
|
|
Position: true,
|
|
|
|
|
NetModel: appInfo.NetworkModel,
|
|
|
|
|
Restart: m.Restart,
|
|
|
|
|
CpuShares: 50,
|
|
|
|
|
Memory: int64(appInfo.MaxMemory),
|
|
|
|
|
Devices: string(devicesStr),
|
|
|
|
|
Origin: m.Origin,
|
|
|
|
|
CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
|
|
|
|
UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
|
|
|
|
}
|
|
|
|
|
service.MyService.App().SaveContainer(md)
|
|
|
|
|
|
|
|
|
|
checkSystemApp()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if the system application is installed
|
|
|
|
|
func checkSystemApp() {
|
|
|
|
|
list := service.MyService.App().GetSystemAppList()
|
|
|
|
|
for _, v := range *list {
|
|
|
|
|
if v.Image == "linuxserver/syncthing" {
|
2021-12-01 08:25:56 +00:00
|
|
|
|
if v.State != "running" {
|
|
|
|
|
//step:start container
|
|
|
|
|
service.MyService.Docker().DockerContainerStart(v.CustomId)
|
|
|
|
|
}
|
2021-11-09 10:57:50 +00:00
|
|
|
|
syncIsExistence = true
|
|
|
|
|
if config.SystemConfigInfo.SyncPort != v.Port {
|
|
|
|
|
config.SystemConfigInfo.SyncPort = v.Port
|
|
|
|
|
}
|
|
|
|
|
var paths []model.PathMap
|
|
|
|
|
json.Unmarshal([]byte(v.Volumes), &paths)
|
|
|
|
|
path := ""
|
|
|
|
|
for _, i := range paths {
|
|
|
|
|
if i.ContainerPath == "/config" {
|
2022-01-20 10:38:59 +00:00
|
|
|
|
path = docker.GetDir(v.CustomId, i.Path) + "/config.xml"
|
2021-11-09 10:57:50 +00:00
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
if file.CheckNotExist(path) {
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
content := file.ReadFullFile(path)
|
|
|
|
|
syncConfig := &system_app.SyncConfig{}
|
|
|
|
|
xml.Unmarshal(content, &syncConfig)
|
|
|
|
|
config.SystemConfigInfo.SyncKey = syncConfig.Key
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !syncIsExistence {
|
2021-12-29 08:42:20 +00:00
|
|
|
|
installSyncthing("74")
|
2021-11-09 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-09 11:02:41 +00:00
|
|
|
|
func CheckSerialDiskMount() {
|
2021-12-29 08:42:20 +00:00
|
|
|
|
// check mount point
|
|
|
|
|
dbList := service.MyService.Disk().GetSerialAll()
|
|
|
|
|
|
2022-01-28 06:06:27 +00:00
|
|
|
|
list := service.MyService.Disk().LSBLK(true)
|
2021-12-29 08:42:20 +00:00
|
|
|
|
mountPoint := make(map[string]string, len(dbList))
|
2022-01-20 10:38:59 +00:00
|
|
|
|
//remount
|
|
|
|
|
for _, v := range dbList {
|
2022-01-28 06:06:27 +00:00
|
|
|
|
mountPoint[v.UUID] = v.MountPoint
|
2022-01-20 10:38:59 +00:00
|
|
|
|
}
|
2021-12-29 08:42:20 +00:00
|
|
|
|
for _, v := range list {
|
2022-01-20 10:38:59 +00:00
|
|
|
|
command.ExecEnabledSMART(v.Path)
|
2021-12-29 08:42:20 +00:00
|
|
|
|
if v.Children != nil {
|
|
|
|
|
for _, h := range v.Children {
|
2022-01-20 10:38:59 +00:00
|
|
|
|
if len(h.MountPoint) == 0 && len(v.Children) == 1 && h.FsType == "ext4" {
|
2022-01-28 06:06:27 +00:00
|
|
|
|
if m, ok := mountPoint[h.UUID]; ok {
|
2022-01-20 10:38:59 +00:00
|
|
|
|
//mount point check
|
|
|
|
|
volume := m
|
|
|
|
|
if !file.CheckNotExist(m) {
|
|
|
|
|
for i := 0; file.CheckNotExist(volume); i++ {
|
|
|
|
|
volume = m + strconv.Itoa(i+1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
service.MyService.Disk().MountDisk(h.Path, volume)
|
|
|
|
|
if volume != m {
|
|
|
|
|
ms := model2.SerialDisk{}
|
2022-01-28 06:06:27 +00:00
|
|
|
|
ms.UUID = v.UUID
|
|
|
|
|
ms.MountPoint = volume
|
2022-01-20 10:38:59 +00:00
|
|
|
|
service.MyService.Disk().UpdateMountPoint(ms)
|
|
|
|
|
}
|
2021-12-29 08:42:20 +00:00
|
|
|
|
|
2022-01-20 10:38:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-29 08:42:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-20 10:38:59 +00:00
|
|
|
|
service.MyService.Disk().RemoveLSBLKCache()
|
|
|
|
|
command.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;AutoRemoveUnuseDir")
|
2021-12-29 08:42:20 +00:00
|
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
|
}
|
|
|
|
|
func Update2_3() {
|
|
|
|
|
command.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/assist.sh")
|
|
|
|
|
}
|
2022-03-16 07:41:14 +00:00
|
|
|
|
func CheckToken2_11() {
|
2022-02-17 10:43:25 +00:00
|
|
|
|
if len(config.ServerInfo.Token) == 0 {
|
|
|
|
|
token := uuid.NewV4().String
|
|
|
|
|
config.ServerInfo.Token = token()
|
|
|
|
|
config.Cfg.Section("server").Key("Token").SetValue(token())
|
|
|
|
|
config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
|
|
|
|
|
}
|
2022-03-16 07:41:14 +00:00
|
|
|
|
if len(config.AppInfo.RootPath) == 0 {
|
|
|
|
|
config.Cfg.Section("app").Key("RootPath").SetValue("/casaOS")
|
|
|
|
|
config.AppInfo.RootPath = "/casaOS"
|
|
|
|
|
config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
|
|
|
|
|
}
|
|
|
|
|
// str := []string{}
|
|
|
|
|
// str = append(str, "ddd")
|
|
|
|
|
// str = append(str, "aaa")
|
|
|
|
|
// ddd := strings.Join(str, "|")
|
|
|
|
|
// config.Cfg.Section("file").Key("ShareDir").SetValue(ddd)
|
|
|
|
|
|
|
|
|
|
// config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
|
|
|
|
|
|
2022-02-17 10:43:25 +00:00
|
|
|
|
}
|