diff --git a/.gitignore b/.gitignore index 7fc4b64..6893ecc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ vendor/ bin/ p2p-rendering/ ./main -server/docker/__pycache__ \ No newline at end of file +server/docker/__pycache__ +./p2p-rendering-computation diff --git a/client/container.go b/client/container.go new file mode 100644 index 0000000..a060bc1 --- /dev/null +++ b/client/container.go @@ -0,0 +1,77 @@ +package client + +import ( + "encoding/json" + "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" + "io/ioutil" + "net/http" +) + +const ( + serverPort = "8089" +) + + +var client = http.Client{} + + +// Start container using REST api Implementation +// From the selected server IP address +func StartContainer(Ip string) (*docker.DockerVM ,error) { + URL := "http://" + Ip + ":8088/startcontainer" + resp, err := http.Get(URL) + + // Convert response to byte value + byteValue, _ := ioutil.ReadAll(resp.Body) + + // Create variable for result response type + var dockerResult docker.DockerVM + + // Adds byte value to docker.DockerVM struct + json.Unmarshal(byteValue, &dockerResult) + if err != nil { + return nil,err + } + + return &dockerResult, nil +} + +// Prints results Generated container +func PrintStartContainer(d *docker.DockerVM){ + fmt.Println("ID : " + fmt.Sprint(d.ID)) + fmt.Println("SSH port: " + fmt.Sprint(d.SSHPort)) + fmt.Println("SSH username: " + fmt.Sprint(d.SSHUsername)) + fmt.Println("SSH password: " + fmt.Sprint(d.SSHPassword)) + fmt.Println("VNC port: " + fmt.Sprint(d.VNCPort)) + fmt.Println("VNC password: " + fmt.Sprint(d.VNCPassword)) +} + + +// TODO implementation using RPC calls +//func StartContainer(Ip string) (*docker.DockerVM,error){ +// client, err := rpc.DialHTTP("tcp",Ip + ":" + serverPort) +// +// if err != nil { +// return nil,err +// } +// +// in := bufio.NewReader(os.Stdin) +// +// for { +// line, _, err := in.ReadLine() +// if err != nil { +// return nil,err +// } +// +// var reply Docker +// +// err = client.Call("Listener.StartContainer", line, &reply) +// if err != nil { +// return nil,err +// } +// log.Printf("Reply: %v, Data: %v", reply, reply.docker) +// return reply.docker, nil +// } +// +//} diff --git a/client/list-servers.go b/client/list-servers.go deleted file mode 100644 index 74c892f..0000000 --- a/client/list-servers.go +++ /dev/null @@ -1,3 +0,0 @@ -package client - -func \ No newline at end of file diff --git a/cmd/action.go b/cmd/action.go index f324eb4..8e06316 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -1,19 +1,35 @@ package cmd import ( + "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/client" + "git.sr.ht/~akilan1999/p2p-rendering-computation/p2p" "git.sr.ht/~akilan1999/p2p-rendering-computation/server" "github.com/urfave/cli/v2" ) var CliAction = func(ctx *cli.Context) error { if Mode == "server" { + // TODO: with RPC calls server.Server() + //server.Rpc() + } + + //Listing servers avaliable + if List_servers { + p2p.PrintIpTable() } //Call function to create Docker container if IpAddress != "" { + imageRes, err := client.StartContainer(IpAddress) + if err != nil { + fmt.Print(err) + } + client.PrintStartContainer(imageRes) } + return nil } diff --git a/cmd/flags.go b/cmd/flags.go index d0510d9..a23b655 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -5,13 +5,13 @@ import ( ) var Mode,IpAddress string -var List_servers, Ip_table bool +var List_servers, Ip_table, Abspath bool var AppConfigFlags = []cli.Flag{ // Deprecated to be implemented using GRPC &cli.StringFlag{ Name: "Mode", - Value: "server", + Value: "client", Usage: "Specifies mode of running", EnvVars: []string{"P2P_MODE"}, Destination: &Mode, @@ -28,4 +28,10 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"CREATE_VM"}, Destination: &IpAddress, }, + &cli.BoolFlag{ + Name: "FilePath", + Usage: "Testing for absolute path", + EnvVars: []string{"CREATE_VM"}, + Destination: &Abspath, + }, } \ No newline at end of file diff --git a/go.mod b/go.mod index 8fb45ae..0e6e2a6 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/containerd/continuity v0.0.0-20210315143101-93e15499afd5 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/docker v20.10.0-beta1.0.20201113105859-b6bfff2a628f+incompatible github.com/docker/go-connections v0.4.0 diff --git a/main.go b/main.go index bf17e3e..2d5718d 100644 --- a/main.go +++ b/main.go @@ -16,60 +16,12 @@ var OS, Pull_location ,Run_script string var List_servers, Ip_table bool func main() { - app := cli.NewApp() app.Name = "p2p-rendering-computation" app.Usage = "p2p cli application to create and access VMs in other servers" app.Version = VERSION app.Flags = cmd.AppConfigFlags app.Action = cmd.CliAction - //app := &cli.App{ - // Name: "p2p-rendering-computation", - // Usage: "allows you to batch tasks in a peer to peer network", - // Version: VERSION, - // Flags: []cli.Flag { - // &cli.StringFlag{ - // Name: "mode", - // Value: "client", - // Usage: "Specifies mode of running", - // Destination: &mode, - // }, - // /* list servers */ - // &cli.BoolFlag{ - // Name: "List-servers", - // Usage: "List servers which can render tasks", - // Destination: &List_servers, - // }, - // /* List iptable */ - // &cli.BoolFlag{ - // Name: "Ip-table", - // Usage: "Listing servers that can be connected to", - // Destination: &Ip_table, - // }, - // - // }, - // /*action for all flags*/ - // Action: func(c *cli.Context) error { - // /* action when certain flags are selected */ - // /*if Run_script == "None" { - // fmt.Println("script not excuted as run script not selected") - // }*/ - // - // if Ip_table == true || List_servers == true{ - // err := p2p.PrintIpTable() - // if err != nil { - // log.Fatal(err) - // } - // } - // - // // If mode server is selected - // if mode == "server"{ - // server.Server() - // } - // - // return nil - // }, - //} err := app.Run(os.Args) if err != nil { diff --git a/p2p-rendering-computation b/p2p-rendering-computation new file mode 100755 index 0000000..ae33d16 Binary files /dev/null and b/p2p-rendering-computation differ diff --git a/server/docker/docker.go b/server/docker/docker.go index 92bad71..59c579c 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -18,7 +18,10 @@ import ( type DockerVM struct { SSHPort int `json:"SSHPort"` + SSHUsername string `json:"SSHUsername"` + SSHPassword string `json:"SSHPassword"` VNCPort int `json:"VNCPort"` + VNCPassword string `json:"VNCPassword"` ID string `json:"ID"` TagName string `json:"TagName"` ImagePath string `json:"ImagePath"` @@ -50,10 +53,15 @@ func BuildRunContainer() (*DockerVM,error) { // Sets Free port to Struct RespDocker.SSHPort = Ports[0] RespDocker.VNCPort = Ports[1] + // Sets appropriate username and password to the + // variables in the struct + RespDocker.SSHUsername = "master" + RespDocker.SSHPassword = "password" + RespDocker.VNCPassword = "vncpassword" //Default parameters RespDocker.TagName = "p2p-ubuntu" - RespDocker.ImagePath = "./containers/docker-ubuntu-sshd/" + RespDocker.ImagePath = "server/docker/containers/docker-ubuntu-sshd/" // Gets docker information from env variables cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) diff --git a/server/rpc.go b/server/rpc.go new file mode 100644 index 0000000..3bf7ebc --- /dev/null +++ b/server/rpc.go @@ -0,0 +1,47 @@ +package server + +import ( + "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" + "net" + "net/rpc" +) + +const ( + port = "8089" +) + +type Listener int + +type Docker struct { + docker *docker.DockerVM +} + +// Starts container using RPC calls +func (l *Listener) StartContainer( reply *Docker) error { + fmt.Print("here") + vm, err := docker.BuildRunContainer() + if err != nil { + return err + } + fmt.Printf("Receive: %v\n", vm) + *reply = Docker{vm} + return nil +} + + + +func Rpc() { + rpcServer, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+port) + if err != nil { + fmt.Print(err) + } + inbound, err := net.ListenTCP("tcp", rpcServer) + if err != nil { + fmt.Print(err) + } + listener := new(Listener) + rpc.Register(listener) + rpc.Accept(inbound) +} + diff --git a/server/server.go b/server/server.go index 2382181..ea64a3e 100644 --- a/server/server.go +++ b/server/server.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" "github.com/gin-gonic/gin" "net/http" //"fmt" @@ -31,7 +32,18 @@ func Server() { }) + r.GET("/startcontainer", func(c *gin.Context) { + resp, err := docker.BuildRunContainer() + + if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + + c.JSON(http.StatusOK, resp) + }) + + // Future feature /*r.GET("/create_vm/:virtualization", func(c *gin.Context) { virtualization := c.Param("virtualization") // Runs based on Preallocated VM size