-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboot.go
185 lines (166 loc) · 4.15 KB
/
boot.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
package main
import (
"fmt"
"github.com/colinmarc/hdfs/v2"
log "github.com/sirupsen/logrus"
"hdfs-mount/config"
"hdfs-mount/hdfs-fuse"
"hdfs-mount/hdfsclient"
"hdfs-mount/krb5"
"hdfs-mount/logger"
"hdfs-mount/nameserver"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"sync"
"syscall"
)
//var conf *config.HdfsMountConf
var hdfsPool *hdfsclient.HdfsPool
func CreateHdfsClient(conf *config.HdfsMountConf) *hdfsclient.DefaultHdfsClient {
var err error
// 开启了kerberos认证
kerberos := conf.Hdfs.Kerberos
var krb5Client *krb5.Krb5Client
if kerberos.Enable {
log.Info("kerberos enable")
krb5Client = &krb5.Krb5Client{
Krb5Conf: kerberos.Krb5Conf,
UserName: kerberos.UserName,
Password: kerberos.Password,
Realm: kerberos.Realm,
KeytabFile: kerberos.KeytabFile,
KeytabHex: kerberos.KeytabHex,
CCache: kerberos.CCache,
}
} else {
log.Info("kerberos disable")
}
// 开启了自定义的dns解析
nServer := conf.NameServer
if nServer.Enable {
log.Info("name server enable")
namedServer := nameserver.MemNameServer{
MemConfig: nServer.Ns,
}
if err = namedServer.Init(); err != nil {
log.Fatalf("load customer name server err:%v", err)
}
if err = nameserver.InstallNameServer(&namedServer); err != nil {
os.Exit(3)
}
} else {
log.Info("name server disable")
}
// 创建 hdfs Client
c := hdfsclient.DefaultHdfsClient{
BaseDir: conf.Hdfs.MountDir,
NnAddress: conf.Hdfs.NnAddress,
User: conf.Hdfs.User,
EnableKerberos: conf.Hdfs.Kerberos.Enable,
KerberosServicePrincipleName: conf.Hdfs.Kerberos.KerberosServicePrincipleName,
HdfsPool: nil,
Krb5Client: krb5Client,
}
err = c.Init()
if err != nil {
log.Errorf("init hdfs client err:%v", err)
os.Exit(4)
}
// hdfs连接池
pool := conf.Pool
hdfsPool, err = hdfsclient.NewHdfsPool(pool.MinNum, pool.MaxNum, pool.IdleOpenNum, func() (*hdfs.Client, error) {
return hdfs.NewClient(*c.GetClientOptions())
})
if err != nil {
log.Errorf("create hdfs pool err:%v", err)
os.Exit(5)
}
c.HdfsPool = hdfsPool
return &c
}
func InterruptProc() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
for range signalChan {
if err := umount(); err != nil {
continue
}
if hdfsPool != nil {
hdfsPool.Shutdown()
hdfsPool = nil
}
}
}()
}
var server *hdfs_fuse.Server
var mutex sync.Mutex
func umount() error {
mutex.Lock()
defer mutex.Unlock()
if server != nil {
if err := server.Unmount(); err != nil {
log.Errorf("umount err:%v", err)
return err
} else {
log.Infof("umount success")
}
server = nil
}
return nil
}
func start(conf *config.HdfsMountConf) {
var err error
// 初始化日志
logConf := conf.Log
logger.InitLog(&logConf.LogName, &logConf.LogLevel, logConf.MaxRemainNum)
// pprof
if conf.PProf.Enable {
addressAndPort := fmt.Sprintf("%s:%d", conf.PProf.Address, conf.PProf.Port)
webAddress := fmt.Sprintf("%s:%d", GetLoaclIP(), conf.PProf.Port)
log.Infof("pprof is enable,listen address http://%s", addressAndPort)
log.Infof("web access address http://%s", webAddress)
go http.ListenAndServe(addressAndPort, nil)
}
// 初始化Hdfs client
client := CreateHdfsClient(conf)
encrypt := conf.Hdfs.Encrypt
var enc *hdfs_fuse.Enc
if encrypt.Enable {
enc, err = hdfs_fuse.NewEnc(encrypt.Password, encrypt.KeyLen, encrypt.Enable)
if err != nil {
log.Errorf("creat encryptor error:%v", err)
os.Exit(10)
}
} else {
enc = &hdfs_fuse.Enc{}
}
// 挂载卷
mount := conf.Mount
server, err = hdfs_fuse.NewServer(&mount, client, enc)
if err != nil {
log.Errorf("mount error:%v", err)
os.Exit(15)
}
InterruptProc()
server.Wait()
umount()
}
func GetLoaclIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "127.0.0.1"
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return "127.0.0.1"
}