yanghui
2021-06-28 b568d251606f82968b8103c485a0d70027c1d57e
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
package config
 
 
import (
    "basic.com/valib/logger.git"
    "bytes"
    "embed"
    "fmt"
    "gopkg.in/ini.v1"
    "io"
    "io/ioutil"
    "os"
    "path"
    "path/filepath"
    "strconv"
)
 
var (
    baseDir        string
    configFile     string
    baseApiPort    uint16
    baseP2pPort    uint16
    baseDebugPort  uint16
)
 
const (
    defaultRootDir = "/var/lib/bee"
    nodePrefix = "node"
 
    defaultPortStep  uint16    = 10
    defaultApiPort   uint16    = 1633
    defaultP2pPort   uint16    = 1634
    defaultDebugPort uint16    = 1635
)
 
//go:embed static
var local embed.FS
 
func init() {
    dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    if nil != err {
        os.Exit(1)
    }
    configFile = path.Join(dir, "config.ini")
    cfg, err := ini.LooseLoad(configFile)
    if nil != err {
        logger.Error("failed to load ini file, err:", err)
        os.Exit(2)
    }
 
    baseDir := cfg.Section("data").Key("dir").MustString(defaultRootDir);
    err = os.MkdirAll(baseDir, os.ModePerm)
    if nil != err {
        logger.Error("failed to mkdir path:", baseDir, ", err:", err)
        os.Exit(3)
    }
 
    fmt.Println("base dir:", baseDir)
 
    baseApiPort = uint16(cfg.Section("port").Key("api").MustUint(uint(defaultApiPort)))
    baseP2pPort = uint16(cfg.Section("port").Key("p2p").MustUint(uint(defaultP2pPort)))
    baseDebugPort = uint16(cfg.Section("port").Key("debug").MustUint(uint(defaultDebugPort)))
}
 
func GetNodeDir(id uint8) string {
    dir := fmt.Sprintf("%s%d", nodePrefix, id)
 
    return path.Join(baseDir, dir)
}
 
func GetApiPort(id uint8) uint16 {
    port := baseApiPort + (uint16(id) - 1) * defaultPortStep
 
    return port
}
 
func GetP2pPort(id uint8) uint16 {
    port := baseP2pPort + (uint16(id) - 1) * defaultPortStep
 
    return port
}
 
func GetDebugPort(id uint8) uint16 {
    port := baseDebugPort + (uint16(id) - 1) * defaultPortStep
 
    return port
}
 
func GenerateDockerFile(dir string, id uint8) error {
    dockerFile := path.Join(dir, "docker-compose.yml")
    err := copyResource(dockerFile, "static/docker-compose.yml", "")
    if nil != err {
        logger.Error("copyResource failed to read static resource, err:", err)
        return err
    }
 
    multipleLine := generateEnvVariable(id)
    envFile := path.Join(dir, ".env")
    err = copyResource(envFile, "static/env", multipleLine)
 
    return nil
}
 
func generateEnvVariable(id uint8) string {
    var b bytes.Buffer
 
    apiPort := strconv.Itoa(int(GetApiPort(id)))
    p2pPort := strconv.Itoa(int(GetP2pPort(id)))
    debugPort := strconv.Itoa(int(GetDebugPort(id)))
 
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    b.WriteString(`## HTTP API listen "address" (default :1633)`)
    b.WriteString("\r\n")
    b.WriteString(` BEE_API_ADDR=:`)
    b.WriteString(apiPort)
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    b.WriteString(`## P2P listen address (default :1634)`)
    b.WriteString("\r\n")
    b.WriteString(` BEE_P2P_ADDR=:`)
    b.WriteString(p2pPort)
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    b.WriteString(`## debug HTTP API listen address (default :1635)`)
    b.WriteString("\r\n")
    b.WriteString(` BEE_DEBUG_API_ADDR=:`)
    b.WriteString(debugPort)
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    b.WriteString(` API_ADDR=:`)
    b.WriteString(apiPort)
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    b.WriteString(` P2P_ADDR=:`)
    b.WriteString(p2pPort)
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    b.WriteString(` DEBUG_API_ADDR=:`)
    b.WriteString(debugPort)
    b.WriteString("\r\n")
    b.WriteString("\r\n")
 
    return b.String()
}
 
func copyResource(dest, src string, content string) error {
    in, err := local.Open(src)
    if nil != err {
        logger.Error("copyResource failed to read static resource, err:", err)
        return err
    }
    defer in.Close()
 
    out, err := os.Create(dest)
    if nil != err {
        logger.Error("copyResource failed to create local copy, err:", err)
        return err
    }
    defer out.Close()
 
    if len(content) > 0 {
        bytes, err := ioutil.ReadAll(in)
        if nil != err {
            logger.Error("copyResource failed to read from source file, err:", err)
            return err
        }
 
        _, err = out.WriteString(string(bytes))
        if nil != err {
            logger.Error("copyResource failed to write , err:", err)
            return err
        }
 
        _, err = out.WriteString(content)
        if nil != err {
            logger.Error("copyResource failed to read from source file, err:", err)
            return err
        }
    } else {
        _, err = io.Copy(out, in)
        if nil != err {
            logger.Error("copyResource failed to copy file, err:", err)
            return err
        }
    }
 
    return nil
}