|
|
@ -5,9 +5,7 @@ import ( |
|
|
|
"flag" |
|
|
|
"flag" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"mime/multipart" |
|
|
|
|
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
@ -20,18 +18,23 @@ type commanderSetting struct { |
|
|
|
port string |
|
|
|
port string |
|
|
|
configFile string |
|
|
|
configFile string |
|
|
|
configs [][]string |
|
|
|
configs [][]string |
|
|
|
sessionID string |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type sessionInfo struct { |
|
|
|
|
|
|
|
id string |
|
|
|
|
|
|
|
uploadFolder string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
setting commanderSetting |
|
|
|
setting commanderSetting |
|
|
|
|
|
|
|
session sessionInfo |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func readConfigFile() [][]string { |
|
|
|
func readConfigFile() [][]string { |
|
|
|
file, err := os.Open(setting.configFile) |
|
|
|
file, err := os.Open(setting.configFile) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Println("Failed to read config file") |
|
|
|
log.Fatal("Failed to read config file", setting.configFile, |
|
|
|
return nil |
|
|
|
"\nNOTE: The config path should be relative to commander.") |
|
|
|
} |
|
|
|
} |
|
|
|
fscanner := bufio.NewScanner(file) |
|
|
|
fscanner := bufio.NewScanner(file) |
|
|
|
|
|
|
|
|
|
|
@ -52,9 +55,17 @@ func handleCommand(command string) { |
|
|
|
switch cmd := args[0]; cmd { |
|
|
|
switch cmd := args[0]; cmd { |
|
|
|
case "init": |
|
|
|
case "init": |
|
|
|
{ |
|
|
|
{ |
|
|
|
setting.sessionID = time.Now().Format("20060102-150405") |
|
|
|
session.id = time.Now().Format("150405-20060102") |
|
|
|
log.Println("New session", setting.sessionID) |
|
|
|
// create upload folder
|
|
|
|
dictateNodes(fmt.Sprintf("init %v %v %v %v", setting.ip, setting.port, setting.configFile, setting.sessionID)) |
|
|
|
session.uploadFolder = fmt.Sprintf("upload/%s", session.id) |
|
|
|
|
|
|
|
err := os.MkdirAll(session.uploadFolder, os.ModePerm) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Println("Failed to create upload folder", session.uploadFolder) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
log.Println("New session", session.id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dictateNodes(fmt.Sprintf("init %v %v %v %v", setting.ip, setting.port, setting.configFile, session.id)) |
|
|
|
} |
|
|
|
} |
|
|
|
case "ping", "kill", "log": |
|
|
|
case "ping", "kill", "log": |
|
|
|
{ |
|
|
|
{ |
|
|
@ -115,14 +126,6 @@ func handleUploadRequest(w http.ResponseWriter, r *http.Request) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// create upload folder
|
|
|
|
|
|
|
|
uploadFolder := fmt.Sprintf("upload/%s", setting.sessionID) |
|
|
|
|
|
|
|
err := os.MkdirAll(uploadFolder, os.ModePerm) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, err.Error()) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reader, err := r.MultipartReader() |
|
|
|
reader, err := r.MultipartReader() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
jsonResponse(w, http.StatusBadRequest, err.Error()) |
|
|
|
jsonResponse(w, http.StatusBadRequest, err.Error()) |
|
|
@ -135,7 +138,7 @@ func handleUploadRequest(w http.ResponseWriter, r *http.Request) { |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dst, err := os.Create(fmt.Sprintf("%s/%s", uploadFolder, part.FileName())) |
|
|
|
dst, err := os.Create(fmt.Sprintf("%s/%s", session.uploadFolder, part.FileName())) |
|
|
|
log.Println(part.FileName()) |
|
|
|
log.Println(part.FileName()) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
jsonResponse(w, http.StatusInternalServerError, err.Error()) |
|
|
|
jsonResponse(w, http.StatusInternalServerError, err.Error()) |
|
|
@ -150,20 +153,6 @@ func handleUploadRequest(w http.ResponseWriter, r *http.Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func saveFile(w http.ResponseWriter, file multipart.File, handle *multipart.FileHeader) { |
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(file) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, err.Error()) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
err = ioutil.WriteFile("./files/"+handle.Filename, data, 0666) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, err.Error()) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
jsonResponse(w, http.StatusCreated, "File uploaded successfully!") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func jsonResponse(w http.ResponseWriter, code int, message string) { |
|
|
|
func jsonResponse(w http.ResponseWriter, code int, message string) { |
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
w.WriteHeader(code) |
|
|
|
w.WriteHeader(code) |
|
|
@ -182,8 +171,8 @@ func serve() { |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
ip := flag.String("ip", "127.0.0.1", "The ip of commander, i.e. this machine") |
|
|
|
ip := flag.String("ip", "127.0.0.1", "The ip of commander, i.e. this machine") |
|
|
|
port := flag.String("port", "8080", "The port where you want to host the config file") |
|
|
|
port := flag.String("port", "8080", "The port which the commander uses to communicate with soldiers") |
|
|
|
configFile := flag.String("config_file", "test.txt", "The file name of config file which should be put in the same of folder as commander") |
|
|
|
configFile := flag.String("config_file", "distribution_config.txt", "The file name of config file") |
|
|
|
flag.Parse() |
|
|
|
flag.Parse() |
|
|
|
|
|
|
|
|
|
|
|
config(*ip, *port, *configFile) |
|
|
|
config(*ip, *port, *configFile) |
|
|
|