-
Notifications
You must be signed in to change notification settings - Fork 2
/
cm-cicada.go
166 lines (143 loc) · 4.85 KB
/
cm-cicada.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package config
import (
"errors"
"fmt"
"github.com/cloud-barista/cm-cicada/common"
"github.com/jollaman999/utils/fileutil"
"gopkg.in/yaml.v3"
"net"
"net/netip"
"os"
"path/filepath"
"strconv"
"strings"
)
type cmCicadaConfig struct {
CMCicada struct {
AirflowServer struct {
Address string `yaml:"address"`
UseTLS string `yaml:"use_tls"`
SkipTLSVerify string `yaml:"skip_tls_verify"`
Timeout string `yaml:"timeout"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"airflow-server"`
DAGDirectoryHost string `yaml:"dag_directory_host"`
DAGDirectoryAirflow string `yaml:"dag_directory_airflow"`
Listen struct {
Port string `yaml:"port"`
} `yaml:"listen"`
} `yaml:"cm-cicada"`
}
var CMCicadaConfig cmCicadaConfig
var cmCicadaConfigFile = "cm-cicada.yaml"
func checkCMCicadaConfigFile() error {
if CMCicadaConfig.CMCicada.AirflowServer.Address == "" {
return errors.New("config error: cm-cicada.airflow-server.address is empty")
}
addrSplit := strings.Split(CMCicadaConfig.CMCicada.AirflowServer.Address, ":")
if len(addrSplit) < 2 {
return errors.New("config error: invalid cm-cicada.airflow-server.address must be {IP or IPv6 or Domain}:{Port} form")
}
port, err := strconv.Atoi(addrSplit[len(addrSplit)-1])
if err != nil || port < 1 || port > 65535 {
return errors.New("config error: cm-cicada.airflow-server.address has invalid port value")
}
addr, _ := strings.CutSuffix(CMCicadaConfig.CMCicada.AirflowServer.Address, ":"+strconv.Itoa(port))
_, err = netip.ParseAddr(addr)
if err != nil {
_, err = net.LookupIP(addr)
if err != nil {
return errors.New("config error: cm-cicada.airflow-server.address has invalid address value " +
"or can't find the domain (" + addr + ")")
}
}
useTLS, err := strconv.ParseBool(strings.ToLower(CMCicadaConfig.CMCicada.AirflowServer.UseTLS))
if err != nil {
return errors.New("config error: cm-cicada.airflow-server.use_tls has invalid value")
}
if useTLS {
_, err = strconv.ParseBool(strings.ToLower(CMCicadaConfig.CMCicada.AirflowServer.SkipTLSVerify))
if err != nil {
return errors.New("config error: cm-cicada.airflow-server.skip_tls_verify has invalid value")
}
}
if CMCicadaConfig.CMCicada.AirflowServer.Timeout == "" {
return errors.New("config error: cm-cicada.airflow-server.timeout is empty")
}
timeout, err := strconv.Atoi(CMCicadaConfig.CMCicada.AirflowServer.Timeout)
if err != nil || timeout < 1 {
return errors.New("config error: cm-cicada.airflow-server.timeout has invalid value")
}
if CMCicadaConfig.CMCicada.AirflowServer.Username == "" {
return errors.New("config error: cm-cicada.airflow-server.username is empty")
}
if CMCicadaConfig.CMCicada.AirflowServer.Password == "" {
return errors.New("config error: cm-cicada.airflow-server.password is empty")
}
dagDirectoryHost := CMCicadaConfig.CMCicada.DAGDirectoryHost
if dagDirectoryHost == "" {
return errors.New("config error: cm-cicada.dag_directory_host is empty")
}
if !fileutil.IsExist(dagDirectoryHost) {
return errors.New("DAG directory (" + dagDirectoryHost + ") is not exist")
}
dagDirectoryAirflow := CMCicadaConfig.CMCicada.DAGDirectoryAirflow
if dagDirectoryAirflow == "" {
CMCicadaConfig.CMCicada.DAGDirectoryAirflow = dagDirectoryHost
}
if CMCicadaConfig.CMCicada.Listen.Port == "" {
return errors.New("config error: cm-cicada.listen.port is empty")
}
port, err = strconv.Atoi(CMCicadaConfig.CMCicada.Listen.Port)
if err != nil || port < 1 || port > 65535 {
return errors.New("config error: cm-cicada.listen.port has invalid value")
}
return nil
}
func readCMCicadaConfigFile() error {
common.RootPath = os.Getenv(common.ModuleROOT)
if len(common.RootPath) == 0 {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
common.RootPath = homeDir + "/." + strings.ToLower(common.ModuleName)
}
err := fileutil.CreateDirIfNotExist(common.RootPath)
if err != nil {
return err
}
ex, err := os.Executable()
if err != nil {
return err
}
exPath := filepath.Dir(ex)
configDir := exPath + "/conf"
if !fileutil.IsExist(configDir) {
configDir = common.RootPath + "/conf"
}
data, err := os.ReadFile(configDir + "/" + cmCicadaConfigFile)
if err != nil {
return errors.New("can't find the config file (" + cmCicadaConfigFile + ")" + fmt.Sprintln() +
"Must be placed in '." + strings.ToLower(common.ModuleName) + "/conf' directory " +
"under user's home directory or 'conf' directory where running the binary " +
"or 'conf' directory where placed in the path of '" + common.ModuleROOT + "' environment variable")
}
err = yaml.Unmarshal(data, &CMCicadaConfig)
if err != nil {
return err
}
err = checkCMCicadaConfigFile()
if err != nil {
return err
}
return nil
}
func prepareCMCicadaConfig() error {
err := readCMCicadaConfigFile()
if err != nil {
return err
}
return nil
}