Added create docker image route and intergrated with the client
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ vendor/
|
||||
bin/
|
||||
p2p-rendering/
|
||||
./main
|
||||
server/docker/__pycache__
|
||||
server/docker/__pycache__
|
||||
./p2p-rendering-computation
|
||||
|
||||
77
client/container.go
Normal file
77
client/container.go
Normal file
@@ -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
|
||||
// }
|
||||
//
|
||||
//}
|
||||
@@ -1,3 +0,0 @@
|
||||
package client
|
||||
|
||||
func
|
||||
@@ -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
|
||||
}
|
||||
|
||||
10
cmd/flags.go
10
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,
|
||||
},
|
||||
}
|
||||
1
go.mod
1
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
|
||||
|
||||
48
main.go
48
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 {
|
||||
|
||||
BIN
p2p-rendering-computation
Executable file
BIN
p2p-rendering-computation
Executable file
Binary file not shown.
@@ -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())
|
||||
|
||||
47
server/rpc.go
Normal file
47
server/rpc.go
Normal file
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user