-
Notifications
You must be signed in to change notification settings - Fork 27
/
frontend_installer.go
132 lines (122 loc) · 3.84 KB
/
frontend_installer.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
package main
import (
"path/filepath"
"os"
"fmt"
"github.com/project-nano/framework"
"encoding/json"
"io/ioutil"
)
const (
PortalPortBegin = 5870
PortalPortEnd = 5899
FrontEndFilesPath = "frontend_files"
FrontEndWebPath = "web_root"
)
func FrontendInstaller(session *SessionInfo) (ranges []PortRange, err error){
const (
ModulePathName = "frontend"
ConfigPathName = "config"
ModuleExecuteName = "frontend"
)
fmt.Println("installing frontend module...")
var workingPath = filepath.Join(session.ProjectPath, ModulePathName)
if err = ensurePath(workingPath, "module", session.UID, session.GID);err != nil{
return
}
var sourceFile = filepath.Join(session.BinaryPath, ModuleExecuteName)
if _, err = os.Stat(sourceFile); os.IsNotExist(err){
return
}
var targetFile = filepath.Join(workingPath, ModuleExecuteName)
if err = copyFile(sourceFile, targetFile);err != nil{
return
}
if err = enableExecuteAccess(session, targetFile);err != nil{
fmt.Printf("enable execute access fail: %s\n", err.Error())
return
}
fmt.Printf("binary '%s' copied\n", targetFile)
if err = copyResources(session, workingPath); err != nil{
return
}
var configPath = filepath.Join(workingPath, ConfigPathName)
if err = ensurePath(configPath, "config", session.UID, session.GID);err != nil{
return
}
if err = writeFrontEndConfig(session, configPath);err != nil{
return
}
ranges = []PortRange{{PortalPortBegin, PortalPortEnd, "tcp"}}
fmt.Println("frontend module installed")
return ranges, nil
}
func copyResources(session *SessionInfo, workingPath string) (err error){
var sourcePath = filepath.Join(session.BinaryPath, FrontEndFilesPath, FrontEndWebPath)
if _, err = os.Stat(sourcePath);os.IsNotExist(err){
return
}
var targetPath = filepath.Join(workingPath, FrontEndWebPath)
return copyDir(sourcePath, targetPath)
}
func writeFrontEndConfig(session *SessionInfo, configPath string) (err error){
const (
ConfigFileName = "frontend.cfg"
DefaultBackEndPort = 5850
DefaultFrontEndPort = 5870
)
type FrontEndConfig struct {
ListenAddress string `json:"address"`
ListenPort int `json:"port"`
ServiceHost string `json:"service_host"`
ServicePort int `json:"service_port"`
}
var configFile = filepath.Join(configPath, ConfigFileName)
if _, err = os.Stat(configFile); os.IsNotExist(err) {
fmt.Println("No configures available, following instructions to generate a new one.")
var config = FrontEndConfig{}
if session.LocalAddress != ""{
config.ListenAddress = session.LocalAddress
fmt.Printf("using %s as portal listen address\n", session.LocalAddress)
}else{
config.ListenAddress, err = framework.ChooseIPV4Address("Portal listen address")
if err != nil{
return
}
session.LocalAddress = config.ListenAddress
}
if config.ListenPort, err = framework.InputNetworkPort(fmt.Sprintf("Portal listen port (%d ~ %d)", PortalPortBegin, PortalPortEnd),
DefaultFrontEndPort); err !=nil{
return
}
if session.APIAddress != ""{
//same host
config.ServiceHost = session.APIAddress
fmt.Printf("using %s as api address\n", session.APIAddress)
}else{
if config.ServiceHost, err = framework.InputIPAddress("Backend API Host Address", config.ListenAddress); err !=nil{
return
}
session.APIAddress = config.ServiceHost
}
if 0 != session.APIPort{
config.ServicePort = session.APIPort
fmt.Printf("using %d as backend api port\n", session.APIPort)
}else{
if config.ServicePort, err = framework.InputNetworkPort("Backend API port", DefaultBackEndPort); err != nil{
return
}
session.APIPort = config.ServicePort
}
//write
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
if err = ioutil.WriteFile(configFile, data, DefaultFilePerm); err != nil {
return err
}
fmt.Printf("default configure '%s' generated\n", configFile)
}
return
}