You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.1 KiB

package daemon
10 years ago
import (
"errors"
"fmt"
"os"
10 years ago
"strconv"
"github.com/subgraph/oz/ipc"
)
func clientConnect() (*ipc.MsgConn, error) {
return ipc.Connect(SocketName, messageFactory, nil)
}
func clientSend(msg interface{}) (*ipc.Message, error) {
10 years ago
c, err := clientConnect()
if err != nil {
return nil, err
}
defer c.Close()
rr, err := c.ExchangeMsg(msg)
if err != nil {
return nil, err
}
10 years ago
resp := <-rr.Chan()
rr.Done()
10 years ago
return resp, nil
}
func ListProfiles() ([]Profile, error) {
10 years ago
resp, err := clientSend(new(ListProfilesMsg))
if err != nil {
return nil, err
}
10 years ago
body, ok := resp.Body.(*ListProfilesResp)
if !ok {
return nil, errors.New("ListProfiles response was not expected type")
}
return body.Profiles, nil
}
func ListSandboxes() ([]SandboxInfo, error) {
10 years ago
resp, err := clientSend(&ListSandboxesMsg{})
if err != nil {
return nil, err
}
10 years ago
body, ok := resp.Body.(*ListSandboxesResp)
if !ok {
return nil, errors.New("ListSandboxes response was not expected type")
}
return body.Sandboxes, nil
}
func Launch(arg string, args, env []string) error {
10 years ago
idx, name, err := parseProfileArg(arg)
if err != nil {
return err
}
pwd, _ := os.Getwd()
10 years ago
resp, err := clientSend(&LaunchMsg{
Index: idx,
10 years ago
Name: name,
Pwd: pwd,
Args: args,
Env: env,
})
if err != nil {
return err
}
switch body := resp.Body.(type) {
case *ErrorMsg:
fmt.Printf("error was %s\n", body.Msg)
case *OkMsg:
fmt.Println("ok received")
default:
fmt.Printf("Unexpected message received %+v", body)
}
return nil
}
func Clean(arg string) error {
10 years ago
idx, name, err := parseProfileArg(arg)
if err != nil {
return err
}
10 years ago
resp, err := clientSend(&CleanMsg{
Index: idx,
10 years ago
Name: name,
})
if err != nil {
return err
}
// TODO collapse this logic into a function like clientSend
switch body := resp.Body.(type) {
case *ErrorMsg:
return errors.New(body.Msg)
case *OkMsg:
return nil
default:
return fmt.Errorf("Unexpected message received %+v", body)
}
}
func KillSandbox(id int) error {
resp, err := clientSend(&KillSandboxMsg{Id: id})
if err != nil {
return err
}
switch body := resp.Body.(type) {
case *ErrorMsg:
return errors.New(body.Msg)
case *OkMsg:
return nil
default:
return fmt.Errorf("Unexpected message received %+v", body)
}
}
func parseProfileArg(arg string) (int, string, error) {
if len(arg) == 0 {
return 0, "", errors.New("profile argument needed")
}
10 years ago
if n, err := strconv.Atoi(arg); err == nil {
return n, "", nil
}
return 0, arg, nil
}
func Logs(count int, follow bool) (chan string, error) {
10 years ago
c, err := clientConnect()
if err != nil {
return nil, err
}
10 years ago
rr, err := c.ExchangeMsg(&LogsMsg{Count: count, Follow: follow})
if err != nil {
return nil, err
}
out := make(chan string)
go dumpLogs(out, rr)
return out, nil
}
func dumpLogs(out chan<- string, rr ipc.ResponseReader) {
for resp := range rr.Chan() {
switch body := resp.Body.(type) {
case *OkMsg:
rr.Done()
close(out)
return
case *LogData:
for _, ll := range body.Lines {
out <- ll
}
default:
out <- fmt.Sprintf("Unexpected response type (%T)", body)
}
}
}