create default config if it does not already exist (#1304)

This commit is contained in:
Tiger Wang 2023-07-31 16:53:03 +08:00 committed by GitHub
parent 5dc6297047
commit 0cf353c56e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 90 deletions

View file

@ -113,24 +113,22 @@ builds:
goarm: goarm:
- "7" - "7"
archives: archives:
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}" - name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-v{{ .Version }}
id: casaos id: casaos
builds: builds:
- casaos-amd64 - casaos-amd64
- casaos-arm64 - casaos-arm64
- casaos-arm-7 - casaos-arm-7
replacements:
arm: arm-7
files: files:
- build/**/* - build/**/*
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}" - name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}
id: casaos-migration-tool id: casaos-migration-tool
builds: builds:
- casaos-migration-tool-amd64 - casaos-migration-tool-amd64
- casaos-migration-tool-arm64 - casaos-migration-tool-arm64
- casaos-migration-tool-arm-7 - casaos-migration-tool-arm-7
replacements:
arm: arm-7
files: files:
- build/sysroot/etc/**/* - build/sysroot/etc/**/*
checksum: checksum:

View file

@ -143,24 +143,22 @@ builds:
goarm: goarm:
- "7" - "7"
archives: archives:
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}" - name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-v{{ .Version }}
id: casaos id: casaos
builds: builds:
- casaos-amd64 - casaos-amd64
- casaos-arm64 - casaos-arm64
- casaos-arm-7 - casaos-arm-7
replacements:
arm: arm-7
files: files:
- build/**/* - build/**/*
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}" - name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}
id: casaos-migration-tool id: casaos-migration-tool
builds: builds:
- casaos-migration-tool-amd64 - casaos-migration-tool-amd64
- casaos-migration-tool-arm64 - casaos-migration-tool-arm64
- casaos-migration-tool-arm-7 - casaos-migration-tool-arm-7
replacements:
arm: arm-7
files: files:
- build/sysroot/etc/**/* - build/sysroot/etc/**/*
checksum: checksum:

View file

@ -1,7 +1,6 @@
[Unit] [Unit]
After=casaos-message-bus.service After=casaos-message-bus.service
After=rclone.service After=rclone.service
ConditionFileNotEmpty=/etc/casaos/casaos.conf
Description=CasaOS Main Service Description=CasaOS Main Service
[Service] [Service]

View file

@ -77,7 +77,7 @@ func init() {
} }
} }
config.InitSetup(configFlag) config.InitSetup(configFlag, "")
if len(dbFlag) == 0 { if len(dbFlag) == 0 {
dbFlag = config.AppInfo.DBPath + "/db" dbFlag = config.AppInfo.DBPath + "/db"

View file

@ -48,6 +48,9 @@ var (
//go:embed api/casaos/openapi.yaml //go:embed api/casaos/openapi.yaml
_docYAML string _docYAML string
//go:embed build/sysroot/etc/casaos/casaos.conf.sample
_confSample string
configFlag = flag.String("c", "", "config address") configFlag = flag.String("c", "", "config address")
dbFlag = flag.String("db", "", "db path") dbFlag = flag.String("db", "", "db path")
versionFlag = flag.Bool("v", false, "version") versionFlag = flag.Bool("v", false, "version")
@ -63,7 +66,7 @@ func init() {
println("git commit:", commit) println("git commit:", commit)
println("build date:", date) println("build date:", date)
config.InitSetup(*configFlag) config.InitSetup(*configFlag, _confSample)
logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt) logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt)
if len(*dbFlag) == 0 { if len(*dbFlag) == 0 {

View file

@ -10,6 +10,10 @@
*/ */
package config package config
const ( import (
USERCONFIGURL = "/etc/casaos/casaos.conf" "path/filepath"
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
) )
var CasaOSConfigFilePath = filepath.Join(constants.DefaultConfigPath, "casaos.conf")

View file

@ -14,80 +14,72 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime"
"strings"
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
"github.com/IceWhaleTech/CasaOS/common"
"github.com/IceWhaleTech/CasaOS/model" "github.com/IceWhaleTech/CasaOS/model"
"github.com/go-ini/ini" "github.com/go-ini/ini"
) )
// 系统配置 var (
var SysInfo = &model.SysInfoModel{} SysInfo = &model.SysInfoModel{}
AppInfo = &model.APPModel{
DBPath: constants.DefaultDataPath,
LogPath: constants.DefaultLogPath,
LogSaveName: common.SERVICENAME,
LogFileExt: "log",
ShellPath: "/usr/share/casaos/shell",
UserDataPath: filepath.Join(constants.DefaultDataPath, "conf"),
}
CommonInfo = &model.CommonModel{
RuntimePath: constants.DefaultRuntimePath,
}
ServerInfo = &model.ServerModel{}
SystemConfigInfo = &model.SystemConfig{}
FileSettingInfo = &model.FileSetting{}
// 用户相关 Cfg *ini.File
var AppInfo = &model.APPModel{} ConfigFilePath string
)
var CommonInfo = &model.CommonModel{}
// var RedisInfo = &model.RedisModel{}
// server相关
var ServerInfo = &model.ServerModel{}
var SystemConfigInfo = &model.SystemConfig{}
var FileSettingInfo = &model.FileSetting{}
var Cfg *ini.File
// 初始化设置,获取系统的部分信息。 // 初始化设置,获取系统的部分信息。
func InitSetup(config string) { func InitSetup(config string, sample string) {
configDir := USERCONFIGURL ConfigFilePath = CasaOSConfigFilePath
if len(config) > 0 { if len(config) > 0 {
configDir = config ConfigFilePath = config
} }
if runtime.GOOS == "darwin" {
configDir = "./conf/conf.conf" // create default config file if not exist
} if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) {
var err error fmt.Println("config file not exist, create it")
// 读取文件 // create config file
Cfg, err = ini.Load(configDir) file, err := os.Create(ConfigFilePath)
if err != nil {
Cfg, err = ini.Load("/etc/casaos.conf")
if err != nil { if err != nil {
Cfg, err = ini.Load("/casaOS/server/conf/conf.ini") panic(err)
if err != nil { }
fmt.Printf("Fail to read file: %v", err) defer file.Close()
os.Exit(1)
} // write default config
_, err = file.WriteString(sample)
if err != nil {
panic(err)
} }
} }
var err error
// 读取文件
Cfg, err = ini.Load(ConfigFilePath)
if err != nil {
panic(err)
}
mapTo("app", AppInfo) mapTo("app", AppInfo)
// mapTo("redis", RedisInfo)
mapTo("server", ServerInfo) mapTo("server", ServerInfo)
mapTo("system", SystemConfigInfo) mapTo("system", SystemConfigInfo)
mapTo("file", FileSettingInfo) mapTo("file", FileSettingInfo)
mapTo("common", CommonInfo) mapTo("common", CommonInfo)
SystemConfigInfo.ConfigPath = configDir
if len(AppInfo.DBPath) == 0 {
AppInfo.DBPath = "/var/lib/casaos"
}
if len(AppInfo.LogPath) == 0 {
AppInfo.LogPath = "/var/log/casaos/"
}
if len(AppInfo.ShellPath) == 0 {
AppInfo.ShellPath = "/usr/share/casaos/shell"
}
if len(AppInfo.UserDataPath) == 0 {
AppInfo.UserDataPath = "/var/lib/casaos/conf"
}
if len(CommonInfo.RuntimePath) == 0 {
CommonInfo.RuntimePath = "/var/run/casaos"
}
Cfg.SaveTo(configDir)
// AppInfo.ProjectPath = getCurrentDirectory() //os.Getwd()
} }
// 映射 // 映射
@ -97,21 +89,3 @@ func mapTo(section string, v interface{}) {
log.Fatalf("Cfg.MapTo %s err: %v", section, err) log.Fatalf("Cfg.MapTo %s err: %v", section, err)
} }
} }
// 获取当前执行文件绝对路径go run
func getCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}
func getCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return strings.Replace(dir, "\\", "/", -1)
}