2012-08-07 16:29:22 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-08-13 12:27:47 +08:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2013-12-02 17:37:36 +08:00
|
|
|
"code.google.com/p/weed-fs/go/weed/weed_server"
|
2013-12-02 06:41:47 +08:00
|
|
|
"github.com/gorilla/mux"
|
2012-08-24 13:46:54 +08:00
|
|
|
"net/http"
|
2012-09-17 08:31:15 +08:00
|
|
|
"os"
|
2012-10-07 01:50:52 +08:00
|
|
|
"runtime"
|
2012-08-24 13:46:54 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2012-08-07 16:29:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2012-08-24 13:46:54 +08:00
|
|
|
cmdVolume.Run = runVolume // break init cycle
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdVolume = &Command{
|
2012-09-26 16:55:56 +08:00
|
|
|
UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
|
2012-08-24 13:46:54 +08:00
|
|
|
Short: "start a volume server",
|
|
|
|
Long: `start a volume server to provide storage spaces
|
2012-08-07 16:29:22 +08:00
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2013-08-14 00:22:06 +08:00
|
|
|
vport = cmdVolume.Flag.Int("port", 8080, "http listen port")
|
2013-08-29 01:39:15 +08:00
|
|
|
volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
|
2013-08-14 00:22:06 +08:00
|
|
|
maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
|
|
|
|
ip = cmdVolume.Flag.String("ip", "localhost", "ip or server name")
|
|
|
|
publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible <ip|server_name>:<port>")
|
|
|
|
masterNode = cmdVolume.Flag.String("mserver", "localhost:9333", "master server location")
|
|
|
|
vpulse = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than the master's setting")
|
|
|
|
vReadTimeout = cmdVolume.Flag.Int("readTimeout", 3, "connection read timeout in seconds. Increase this if uploading large files.")
|
|
|
|
vMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
|
|
|
|
dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
|
|
|
|
rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
|
|
|
|
volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
|
2012-08-07 16:29:22 +08:00
|
|
|
|
2013-08-14 00:22:06 +08:00
|
|
|
volumeWhiteList []string
|
2012-08-07 16:29:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func runVolume(cmd *Command, args []string) bool {
|
2012-11-07 17:51:43 +08:00
|
|
|
if *vMaxCpu < 1 {
|
|
|
|
*vMaxCpu = runtime.NumCPU()
|
|
|
|
}
|
2012-10-07 01:50:52 +08:00
|
|
|
runtime.GOMAXPROCS(*vMaxCpu)
|
2013-07-14 02:38:01 +08:00
|
|
|
folders := strings.Split(*volumeFolders, ",")
|
|
|
|
maxCountStrings := strings.Split(*maxVolumeCounts, ",")
|
|
|
|
maxCounts := make([]int, 0)
|
|
|
|
for _, maxString := range maxCountStrings {
|
|
|
|
if max, e := strconv.Atoi(maxString); e == nil {
|
|
|
|
maxCounts = append(maxCounts, max)
|
|
|
|
} else {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("The max specified in -max not a valid number %s", max)
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
2013-07-14 02:38:01 +08:00
|
|
|
if len(folders) != len(maxCounts) {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
|
|
|
for _, folder := range folders {
|
|
|
|
fileInfo, err := os.Stat(folder)
|
|
|
|
if err != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("No Existing Folder:%s", folder)
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
|
|
|
if !fileInfo.IsDir() {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("Volume Folder should not be a file:%s", folder)
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
|
|
|
perm := fileInfo.Mode().Perm()
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Volume Folder", folder)
|
|
|
|
glog.V(0).Infoln("Permission:", perm)
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
2012-09-26 18:27:10 +08:00
|
|
|
|
2012-09-26 17:29:16 +08:00
|
|
|
if *publicUrl == "" {
|
2012-09-26 18:27:10 +08:00
|
|
|
*publicUrl = *ip + ":" + strconv.Itoa(*vport)
|
2012-09-26 17:29:16 +08:00
|
|
|
}
|
2013-08-14 00:22:06 +08:00
|
|
|
if *volumeWhiteListOption != "" {
|
|
|
|
volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
|
2013-08-13 14:48:10 +08:00
|
|
|
}
|
2012-09-20 17:11:08 +08:00
|
|
|
|
2013-12-02 06:41:47 +08:00
|
|
|
r := mux.NewRouter()
|
2012-08-24 13:46:54 +08:00
|
|
|
|
2013-12-02 17:37:36 +08:00
|
|
|
weed_server.NewVolumeServer(r, VERSION, *ip, *vport, *publicUrl, folders, maxCounts,
|
|
|
|
*masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList,
|
|
|
|
)
|
2012-08-24 13:46:54 +08:00
|
|
|
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Start Weed volume server", VERSION, "at http://"+*ip+":"+strconv.Itoa(*vport))
|
2012-09-29 00:13:17 +08:00
|
|
|
srv := &http.Server{
|
2013-12-03 14:58:27 +08:00
|
|
|
Addr: *ip + ":" + strconv.Itoa(*vport),
|
2013-12-02 06:41:47 +08:00
|
|
|
Handler: r,
|
2012-09-29 01:21:06 +08:00
|
|
|
ReadTimeout: (time.Duration(*vReadTimeout) * time.Second),
|
|
|
|
}
|
2012-09-29 00:13:17 +08:00
|
|
|
e := srv.ListenAndServe()
|
2012-08-24 13:46:54 +08:00
|
|
|
if e != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("Fail to start:%s", e.Error())
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
|
|
|
return true
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|