-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.go
148 lines (124 loc) · 3.54 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
package main
import (
"encoding/json"
"io"
"os"
"runtime"
"strings"
"github.com/nettica-com/nettica-admin/model"
log "github.com/sirupsen/logrus"
)
func Migrate() {
log.Infof("*** Migrate ***")
if !cfg.loaded {
return
}
if _, err := os.Stat(GetDataPath() + "nettica.json"); err != nil {
if _, err := os.Stat(GetDataPath() + "nettica.conf"); err != nil {
if _, err := os.Stat(GetDataPath() + "keys.json"); err != nil {
if _, err := os.Stat(GetDataPath() + "nettica-service-host.json"); err != nil {
log.Info("No files to migrate")
return
}
}
}
}
original := GetDataPath() + "original\\"
stat, err := os.Stat(GetDataPath() + "original")
if err == nil && stat.IsDir() {
log.Info("Original directory already exists, skipping migration")
return
}
err = os.MkdirAll(GetDataPath()+"original", 0755)
if err != nil {
log.Error("Failed to create original directory: ", err)
}
// temporarily copy the file instead of renaming it so old agent can still read it
//err = os.Rename(GetDataPath()+"nettica.json", GetDataPath()+"my.nettica.com.json")
var msg model.Message
data, err := os.ReadFile(GetDataPath() + "nettica.json")
if err != nil {
log.Printf("Failed to read file %s: %v", "nettica.json", err)
} else {
err = json.Unmarshal(data, &msg)
if err == nil {
name := msg.Device.Server
name = strings.Replace(name, "https://", "", -1)
name = strings.Replace(name, "http://", "", -1)
err = os.WriteFile(GetDataPath()+name+".json", data, 0644)
if err != nil {
log.Errorf("Failed to create %s.json: %v", name, err)
}
} else {
log.Errorf("Failed to unmarshal %s: %v", "nettica.json", err)
}
}
err = Copy(original+"nettica.json", GetDataPath()+"nettica.json")
if err != nil {
log.Error("Failed to copy nettica.json: ", err)
} else {
err = os.Remove(GetDataPath() + "nettica.json")
if err != nil {
log.Error("Failed to remove nettica.json: ", err)
}
}
err = Copy(original+"keys.json", GetDataPath()+"keys.json")
if err != nil {
log.Error("Failed to copy keys.json: ", err)
}
err = Copy(original+"nettica.conf", GetDataPath()+"nettica.conf")
if err != nil {
log.Error("Failed to copy nettica.conf: ", err)
} else {
err = os.Remove(GetDataPath() + "nettica.conf")
if err != nil {
log.Error("Failed to remove nettica.conf: ", err)
}
}
err = Copy(original+"nettica-service-host.json", GetDataPath()+"nettica-service-host.json")
if err != nil {
log.Error("Failed to copy nettica-service-host.json: ", err)
}
err = os.Rename(GetDataPath()+"nettica-service-host.json", GetDataPath()+"my.nettica.com-service-host.json")
if err != nil {
log.Error("Failed to rename nettica-service-host.json: ", err)
}
err = os.Rename(GetDataPath()+"keys.json", GetDataPath()+"keys.keys")
if err != nil {
log.Error("Failed to rename keys.json: ", err)
}
log.Info("*** Migrate Done ***")
}
func Copy(dst string, src string) error {
// If platform is windows, fix the slashes
if runtime.GOOS == "windows" {
dst = strings.ReplaceAll(dst, "/", "\\")
src = strings.ReplaceAll(src, "/", "\\")
} else {
dst = strings.ReplaceAll(dst, "\\", "/")
src = strings.ReplaceAll(src, "\\", "/")
}
// Open the source file
sFile, err := os.Open(src)
if err != nil {
return err
}
defer sFile.Close()
// Create the destination file
dFile, err := os.Create(dst)
if err != nil {
return err
}
// Copy the file
_, err = io.Copy(dFile, sFile)
if err != nil {
dFile.Close()
return err
}
// Close the destination file
err = dFile.Close()
if err != nil {
return err
}
return nil
}