From dcb9737ae58fea03b714675eb4cc8d7b6f52ccf4 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 14 Sep 2011 03:06:50 +0200 Subject: //hyper/process main.go: first steps --- hyper/process/main.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 hyper/process/main.go (limited to 'hyper/process/main.go') diff --git a/hyper/process/main.go b/hyper/process/main.go new file mode 100644 index 00000000..297be2cf --- /dev/null +++ b/hyper/process/main.go @@ -0,0 +1,52 @@ +package main + +import "fmt" +import "os" + + +func reader(file *os.File) { + var b []byte = make([]byte, 1024) + var err os.Error = nil + for err == nil { + var n int + n, err = file.Read(b) + fmt.Printf("data: %d, %s\n", n, b) + } +} + +func main() { + var name = "/usr/bin/bc" + var argv = []string{ "bc" } + var envv = []string{ "FOO=23" } + //var chroot = false + var dir = "/var/empty" + var files [3][2]*os.File + var err os.Error + + for i, _ := range files { + files[i][0], files[i][1], err = os.Pipe() + err = err + } + + var attr = &os.ProcAttr{ + Dir: dir, + Env: envv, + Files: []*os.File{ /*files[0][0] */ os.Stdin, files[1][1], files[2][1]}, + } + + var p *os.Process + + p, err = os.StartProcess(name, argv, attr) + + for _, file := range attr.Files { + file.Close() + } + + p=p + + go reader(files[1][0]) + reader(files[2][0]) + + fmt.Printf("hello, world\n") + +} -- cgit v1.2.3 From 1e9f32bf57ab45ff1d3a61294e323b9c0cd3309b Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 14 Sep 2011 21:47:41 +0200 Subject: //hyper/process: Emergency Commit #2 --- hyper/process/main.go | 80 ++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'hyper/process/main.go') diff --git a/hyper/process/main.go b/hyper/process/main.go index 297be2cf..ebeeb6d6 100644 --- a/hyper/process/main.go +++ b/hyper/process/main.go @@ -1,52 +1,60 @@ package main -import "fmt" +import "json" +import "log" +import "http" +import "gorilla.googlecode.com/hg/gorilla/mux" import "os" +import "fmt" + +import "hyper/process" +var proc = map[string] *hyper.Process{} -func reader(file *os.File) { - var b []byte = make([]byte, 1024) - var err os.Error = nil - for err == nil { - var n int - n, err = file.Read(b) - fmt.Printf("data: %d, %s\n", n, b) +// TODO Retrieve Process, Write, Kill [autokill], get exit code + +func RespondJSON(res http.ResponseWriter, v interface{}) os.Error { + content, err := json.Marshal(v) + if err == nil { + log.Printf("< %s", content) + res.Header().Set("Content-Type", "application/json; charset=\"utf-8\"") + res.WriteHeader(http.StatusOK) + res.Write(content) + } else { + log.Printf("%s while json.Marshal(%s)", err, v) } + return err } -func main() { - var name = "/usr/bin/bc" - var argv = []string{ "bc" } - var envv = []string{ "FOO=23" } - //var chroot = false - var dir = "/var/empty" - var files [3][2]*os.File - var err os.Error - - for i, _ := range files { - files[i][0], files[i][1], err = os.Pipe() - err = err +func CreateProcessHandler(res http.ResponseWriter, req *http.Request) { + if p, err := hyper.NewProcess(req); err == nil { + id := p.Id() + proc[id] = p + RespondJSON(res, &map[string]string{ + "path": fmt.Sprintf("/proc/%s", id), + }) + } else { + log.Printf("%s", err) + res.WriteHeader(http.StatusInternalServerError) } +} - var attr = &os.ProcAttr{ - Dir: dir, - Env: envv, - Files: []*os.File{ /*files[0][0] */ os.Stdin, files[1][1], files[2][1]}, +func RetrieveProcess(res http.ResponseWriter, req *http.Request) { + if p := proc[mux.Vars(req)["id"]]; p != nil { + RespondJSON(res, p) + } else { + res.WriteHeader(http.StatusNotFound) } +} - var p *os.Process +func main() { - p, err = os.StartProcess(name, argv, attr) + // Gorilla + mux.HandleFunc("/proc", CreateProcessHandler).Methods("POST") + mux.HandleFunc("/proc/{id}", RetrieveProcess).Methods("GET") - for _, file := range attr.Files { - file.Close() + err := http.ListenAndServe(":8888", mux.DefaultRouter) + if err != nil { + log.Fatal("ListenAndServe: ", err.String()) } - - p=p - - go reader(files[1][0]) - reader(files[2][0]) - - fmt.Printf("hello, world\n") - } -- cgit v1.2.3 From 668ca493744636a5fe090d431f9d36e97be7907b Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 15 Sep 2011 00:15:36 +0200 Subject: //hyper/process: route POST /proc/{id} to stdin --- hyper/process/main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'hyper/process/main.go') diff --git a/hyper/process/main.go b/hyper/process/main.go index ebeeb6d6..5420f681 100644 --- a/hyper/process/main.go +++ b/hyper/process/main.go @@ -6,6 +6,7 @@ import "http" import "gorilla.googlecode.com/hg/gorilla/mux" import "os" import "fmt" +import "bytes" import "hyper/process" @@ -47,13 +48,29 @@ func RetrieveProcess(res http.ResponseWriter, req *http.Request) { } } +func FeedProcess(res http.ResponseWriter, req *http.Request) { + if p := proc[mux.Vars(req)["id"]]; p != nil { + body := make([]byte, 4096) + if _, err := req.Body.Read(body); err == nil { + body = bytes.TrimRight(body, string([]byte{0})) + p.Write(body) + //if err := p.Write(body); err == nil { + RespondJSON(res, true) + //} + } + } else { + res.WriteHeader(http.StatusNotFound) + } +} + func main() { // Gorilla mux.HandleFunc("/proc", CreateProcessHandler).Methods("POST") mux.HandleFunc("/proc/{id}", RetrieveProcess).Methods("GET") + mux.HandleFunc("/proc/{id}", FeedProcess).Methods("POST") - err := http.ListenAndServe(":8888", mux.DefaultRouter) + err := http.ListenAndServe("0.0.0.0:8888", mux.DefaultRouter) if err != nil { log.Fatal("ListenAndServe: ", err.String()) } -- cgit v1.2.3