forked from cosandr/go-motd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
181 lines (173 loc) · 4.34 KB
/
migrate.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
func readCfgOld(path string) (c OldConf, err error) {
c.Init()
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("config file error: %v ", err)
return
}
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
err = fmt.Errorf("cannot parse %s: %v", path, err)
return
}
return
}
func dumpCfgOld(c *OldConf, writeFile string) {
d, err := yaml.Marshal(c)
if err != nil {
fmt.Printf("Config parse error: %v\n", err)
return
}
if writeFile != "" {
err = ioutil.WriteFile(writeFile, d, 0644)
if err != nil {
fmt.Printf("Config dumped failed: %v\n", err)
return
}
fmt.Printf("Config dumped to: %s\n", writeFile)
} else {
fmt.Printf("%s\n", string(d))
}
return
}
func dumpCfg(c *Conf, writeFile string) {
d, err := yaml.Marshal(c)
if err != nil {
fmt.Printf("Config parse error: %v\n", err)
return
}
if writeFile != "" && writeFile != "--" {
err = ioutil.WriteFile(writeFile, d, 0644)
if err != nil {
fmt.Printf("Config dumped failed: %v\n", err)
return
}
fmt.Printf("Config dumped to: %s\n", writeFile)
} else {
fmt.Printf("%s\n", string(d))
}
return
}
func readCfg(path string) (c Conf, err error) {
c.Init()
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("config file error: %v ", err)
return
}
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
err = fmt.Errorf("cannot parse %s: %v", path, err)
return
}
return
}
func convertCfg(oc *OldConf) (c Conf) {
c.Init()
// Global
c.WarnOnly = oc.FailedOnly
c.ShowOrder = oc.ShowOrder
c.ColDef = oc.ColDef
c.ColPad = oc.ColPad
// BTRFS
c.BTRFS.WarnOnly = oc.BTRFS.FailedOnly
c.BTRFS.PadHeader = oc.BTRFS.Header
c.BTRFS.PadContent = oc.BTRFS.Content
c.BTRFS.Warn = oc.BTRFS.Warn
c.BTRFS.Crit = oc.BTRFS.Crit
// CPU
c.CPU.WarnOnly = oc.CPU.FailedOnly
c.CPU.PadHeader = oc.CPU.Header
c.CPU.PadContent = oc.CPU.Content
c.CPU.Warn = oc.CPU.Warn
c.CPU.Crit = oc.CPU.Crit
c.CPU.Exec = oc.CPU.Exec
// Disk
c.Disk.WarnOnly = oc.Disk.FailedOnly
c.Disk.PadHeader = oc.Disk.Header
c.Disk.PadContent = oc.Disk.Content
c.Disk.Warn = oc.Disk.Warn
c.Disk.Crit = oc.Disk.Crit
c.Disk.Ignore = oc.Disk.Ignore
c.Disk.Sys = oc.Disk.Sys
// Docker
c.Docker.WarnOnly = oc.Docker.FailedOnly
c.Docker.PadHeader = oc.Docker.Header
c.Docker.PadContent = oc.Docker.Content
c.Docker.Ignore = oc.Docker.Ignore
c.Docker.Exec = oc.Docker.Exec
// Podman
c.Podman.WarnOnly = oc.Podman.FailedOnly
c.Podman.PadHeader = oc.Podman.Header
c.Podman.PadContent = oc.Podman.Content
c.Podman.Ignore = oc.Podman.Ignore
c.Podman.Sudo = oc.Podman.Sudo
c.Podman.IncludeSudo = oc.Podman.IncludeSudo
// SysInfo
c.SysInfo.WarnOnly = oc.SysInfo.FailedOnly
c.SysInfo.PadHeader = oc.SysInfo.Header
c.SysInfo.PadContent = oc.SysInfo.Content
// Systemd
c.Systemd.WarnOnly = oc.Systemd.FailedOnly
c.Systemd.PadHeader = oc.Systemd.Header
c.Systemd.PadContent = oc.Systemd.Content
c.Systemd.Units = oc.Systemd.Units
c.Systemd.HideExt = oc.Systemd.HideExt
c.Systemd.InactiveOK = oc.Systemd.InactiveOK
c.Systemd.ShowFailed = oc.Systemd.ShowFailed
// Updates
c.Updates.WarnOnly = oc.Podman.FailedOnly
c.Updates.PadHeader = oc.Podman.Header
c.Updates.PadContent = oc.Podman.Content
c.Updates.Show = oc.Updates.Show
c.Updates.File = oc.Updates.File
// ZFS
c.ZFS.WarnOnly = oc.ZFS.FailedOnly
c.ZFS.PadHeader = oc.ZFS.Header
c.ZFS.PadContent = oc.ZFS.Content
c.ZFS.Warn = oc.ZFS.Warn
c.ZFS.Crit = oc.ZFS.Crit
return
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Path to old config is required")
os.Exit(1)
}
oldPath := os.Args[1]
if oldPath == "-h" || oldPath == "--help" {
fmt.Printf(`Usage %s: old_config [new_config]
If new_config is '--', it will be written to stdout
If omitted, .old is appended to old_config and the new config written in its place
`, os.Args[0])
os.Exit(0)
}
oldCfg, err := readCfgOld(oldPath)
if err != nil {
fmt.Printf("Config parse error: %v", err)
os.Exit(1)
}
var newPath string
// We have a destination as well
if len(os.Args) > 2 {
newPath = os.Args[2]
} else {
// Rename old config
err = os.Rename(oldPath, oldPath+".old")
if err != nil {
fmt.Printf("Cannot backup old config: %v", err)
os.Exit(1)
}
newPath = oldPath
}
//dumpCfgOld(&oldCfg, "old_config.yaml")
newCfg := convertCfg(&oldCfg)
dumpCfg(&newCfg, newPath)
}