-
Notifications
You must be signed in to change notification settings - Fork 27
/
module_updater.go
216 lines (198 loc) · 5.13 KB
/
module_updater.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"github.com/project-nano/framework"
"fmt"
"os"
"path/filepath"
"path"
"os/exec"
"bytes"
"strings"
"github.com/pkg/errors"
"crypto/sha1"
"io"
"time"
)
type ResourcePath struct {
Source string
Target string
}
type ModuleBinary struct {
Module string
Binary string
Resources []ResourcePath
}
func UpdateAllModules(forcibly bool) {
var modules = map[string]ModuleBinary{
"core": ModuleBinary{"core", "core", nil},
"cell": ModuleBinary{"cell", "cell", nil},
"frontend": {"frontend", "frontend", []ResourcePath{{path.Join("bin", FrontEndFilesPath, FrontEndWebPath), FrontEndWebPath}}},
}
var moduleOrder = []string{"core", "cell", "frontend"}
const (
DefaultProjectPath = "/opt/nano"
)
var projectPath string
var err error
projectPath, err = framework.InputString("Project Installed Path", DefaultProjectPath)
if err != nil{
fmt.Printf("get installed path fail: %s\n", err.Error())
return
}
if _, err = os.Stat(projectPath); os.IsNotExist(err){
fmt.Printf("project path '%s' not exists\n", projectPath)
return
}
var binaries []ModuleBinary
for _, moduleName := range moduleOrder{
if _, err = os.Stat(filepath.Join(projectPath, moduleName)); os.IsNotExist(err){
continue
}else if binary, exists := modules[moduleName]; exists{
binaries = append(binaries, binary)
}else{
fmt.Printf("invalid module '%s' in path '%s'\n", moduleName, projectPath)
return
}
}
if 0 == len(binaries){
fmt.Println("no module binary available")
return
}
for _, binary := range binaries {
err = updateModule(projectPath, binary, forcibly)
if err != nil{
fmt.Printf("update module '%s' fail: %s\n", binary.Module, err.Error())
return
}
}
fmt.Printf("%d module(s) updated success\n", len(binaries))
}
func updateModule(projectPath string, binary ModuleBinary, forcibly bool) (err error) {
var sourceBinary = path.Join(BinaryPathName, binary.Binary)
var binaryName = path.Join(projectPath, binary.Module, binary.Binary)
if !forcibly {
isIdentical, err := isIdentical(sourceBinary, binaryName)
if err != nil{
return err
}
if isIdentical{
fmt.Printf("module %s already updated\n", binary.Module)
return nil
}
}
isRunning, err := isModuleRunning(binaryName)
if err != nil{
return err
}
if isRunning{
//stop first
if err = stopModule(binaryName); err != nil{
err = fmt.Errorf("stop binary '%s' fail: %s\n", binaryName, err.Error())
return
}
fmt.Printf("module %s stopped\n", binary.Module)
const (
StopGap = time.Millisecond * 300
)
time.Sleep(StopGap)
}
if err = copyFile(sourceBinary, binaryName); err != nil{
err = fmt.Errorf("overwrite binary '%s' fail: %s\n", binaryName, err.Error())
return
}
fmt.Printf("overwrite %s success\n", binaryName)
if 0 != len(binary.Resources){
for _, resource := range binary.Resources{
var targetPath = path.Join(projectPath, binary.Module, resource.Target)
if err = copyDir(resource.Source, targetPath); err != nil{
err = fmt.Errorf("overwrite to resource path '%s' fail: %s\n", targetPath, err.Error())
return
}
fmt.Printf("resoure %s overwritten to '%s'\n", resource.Source, targetPath)
}
}
if isRunning{
//start again
if err = startModule(binaryName); err != nil{
err = fmt.Errorf("restart binary '%s' fail: %s\n", binaryName, err.Error())
return
}
fmt.Printf("module %s restarted\n", binary.Module)
}
fmt.Printf("module %s update success\n", binary.Module)
return nil
}
func isIdentical(source, target string) (identical bool, err error){
var files = []string{target, source}
var hashResult [][]byte
for _, filename := range files{
var fileStream *os.File
fileStream, err = os.Open(filename)
if err != nil{
return
}
defer fileStream.Close()
var hashLoader = sha1.New()
if _, err = io.Copy(hashLoader, fileStream); err != nil{
return
}
var fileHash = hashLoader.Sum(nil)
hashResult = append(hashResult, fileHash)
}
const (
TargetHashOffset = iota
SourceHashOffset
ValidHashCount = 2
)
if ValidHashCount != len(hashResult){
err = fmt.Errorf("invalid hash count %d", len(hashResult))
return false, err
}
return bytes.Equal(hashResult[TargetHashOffset], hashResult[SourceHashOffset]), nil
}
func isModuleRunning(binaryPath string) (running bool, err error) {
var cmd = exec.Command(binaryPath, "status")
var output bytes.Buffer
cmd.Stdout = &output
if err = cmd.Run(); err != nil{
return
}
const (
Keyword = "running"
)
return strings.Contains(output.String(), Keyword), nil
}
func startModule(binaryPath string) (err error){
var cmd = exec.Command(binaryPath, "start")
var output bytes.Buffer
cmd.Stdout = &output
if err = cmd.Run(); err != nil{
return
}
const (
Keyword = "fail"
)
var content = output.String()
if strings.Contains(content, Keyword){
//fail
return errors.New(content)
}
return nil
}
func stopModule(binaryPath string) (err error){
var cmd = exec.Command(binaryPath, "stop")
var output bytes.Buffer
cmd.Stdout = &output
if err = cmd.Run(); err != nil{
return
}
const (
Keyword = "fail"
)
var content = output.String()
if strings.Contains(content, Keyword){
//fail
return errors.New(content)
}
return nil
}