diff --git a/client/ServerSpecs.go b/client/ServerSpecs.go new file mode 100644 index 0000000..e732b61 --- /dev/null +++ b/client/ServerSpecs.go @@ -0,0 +1,46 @@ +package client + +import ( + "encoding/json" + "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server" + "io/ioutil" + "net/http" +) + +func GetSpecs(IP string)(*server.SysInfo,error) { + URL := "http://" + IP + ":" + serverPort + "/server_info" + resp, err := http.Get(URL) + if err != nil { + return nil,err + } + + // Convert response to byte value + byteValue, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil,err + } + + // Create variable for result response type + var serverSpecsResult server.SysInfo + + // Adds byte value to docker.DockerVM struct + json.Unmarshal(byteValue, &serverSpecsResult) + if err != nil { + return nil,err + } + + return &serverSpecsResult, nil +} + +// print the contents of the obj +func PrettyPrint(data interface{}) { + var p []byte + // var err := error + p, err := json.MarshalIndent(data, "", "\t") + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("%s \n", p) +} \ No newline at end of file diff --git a/client/container.go b/client/container.go index 0d15d85..2c7bbe5 100644 --- a/client/container.go +++ b/client/container.go @@ -16,13 +16,12 @@ const ( var client = http.Client{} - -// Start container using REST api Implementation +// StartContainer Start container using REST api Implementation // From the selected server IP address // TODO: Test cases for this function -func StartContainer(Ip string,Num_ports int, GPU bool) (*docker.DockerVM ,error) { +func StartContainer(Ip string, NumPorts int, GPU bool) (*docker.DockerVM ,error) { // Passes URL with number of TCP ports to allocated and to give GPU access to the docker container - URL := "http://" + Ip + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(Num_ports) + "&GPU=" + strconv.FormatBool(GPU) + URL := "http://" + Ip + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) resp, err := http.Get(URL) // Convert response to byte value @@ -43,7 +42,7 @@ func StartContainer(Ip string,Num_ports int, GPU bool) (*docker.DockerVM ,error) return &dockerResult, nil } -// Prints results Generated container +// PrintStartContainer Prints results Generated container func PrintStartContainer(d *docker.DockerVM){ fmt.Println("ID : " + fmt.Sprint(d.ID)) fmt.Println("SSH port: " + fmt.Sprint(d.SSHPort)) diff --git a/client/go/client.go b/client/go/client.go deleted file mode 100644 index 148514b..0000000 --- a/client/go/client.go +++ /dev/null @@ -1,8 +0,0 @@ -package client - -import ( - -) - -func client() { -} \ No newline at end of file diff --git a/client/go/mDNS.go b/client/go/mDNS.go deleted file mode 100644 index 689cb0b..0000000 --- a/client/go/mDNS.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "github.com/grandcat/zeroconf" - "log" - "context" - "time" -) - -func main() { - // Discover all services on the network (e.g. _workstation._tcp) - resolver, err := zeroconf.NewResolver(nil) - if err != nil { - log.Fatalln("Failed to initialize resolver:", err.Error()) - } - - entries := make(chan *zeroconf.ServiceEntry) - go func(results <-chan *zeroconf.ServiceEntry) { - for entry := range results { - log.Println(entry) - } - log.Println("No more entries.") - }(entries) - - ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) - defer cancel() - err = resolver.Browse(ctx, "_workstation._tcp", "local.", entries) - if err != nil { - log.Fatalln("Failed to browse:", err.Error()) - } - - <-ctx.Done() - -} \ No newline at end of file diff --git a/client/python/__pycache__/ipfs_config.cpython-38.pyc b/client/python/__pycache__/ipfs_config.cpython-38.pyc deleted file mode 100644 index 6904f34..0000000 Binary files a/client/python/__pycache__/ipfs_config.cpython-38.pyc and /dev/null differ diff --git a/client/python/ipfs_config.py b/client/python/ipfs_config.py deleted file mode 100644 index 2e0f8e5..0000000 --- a/client/python/ipfs_config.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -This scrypt creates basic config for IPFS server -''' -import ipfsApi - -def config(): - api = ipfsApi.Client('127.0.0.1', 5001) - return api diff --git a/client/python/list-server.py b/client/python/list-server.py deleted file mode 100644 index 0ba33ca..0000000 --- a/client/python/list-server.py +++ /dev/null @@ -1,18 +0,0 @@ -''' -This scrypt uses curl to list servers in the IPFS network -''' -import requests -import json - -def list_servers(): - peers = requests.post('http://127.0.0.1:5001/api/v0/swarm/peers') - # TODO: Detect nodes that are servers - - peers_dict = peers.json() - # Ping for each node - for i in peers_dict['Peers']: - ping = requests.post('http://127.0.0.1:5001/api/v0/ping?arg='+i['Peer']+'&count=1') - # Create a replace function TODO - print(str(ping.content)[2:-3]) - -list_servers() \ No newline at end of file diff --git a/cmd/action.go b/cmd/action.go index 50b2aca..ffafda7 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -42,7 +42,18 @@ var CliAction = func(ctx *cli.Context) error { if err != nil { fmt.Print(err) } - client.PrintStartContainer(imageRes) + client.PrettyPrint(imageRes) + } + + //Call if specs flag is called + if Specs != "" { + specs, err := client.GetSpecs(Specs) + if err != nil { + return err + } + + // Pretty print + client.PrettyPrint(specs) } diff --git a/cmd/flags.go b/cmd/flags.go index 5c53be3..02d5745 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -9,6 +9,7 @@ var ( IpAddress string Ports string Mode string + Specs string GPU bool ListServers bool ) @@ -46,4 +47,10 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"USE_GPU"}, Destination: &GPU, }, + &cli.StringFlag{ + Name: "Specs", + Usage: "Specs of the server node", + EnvVars: []string{"SPECS"}, + Destination: &Specs, + }, } \ No newline at end of file diff --git a/go.mod b/go.mod index 5c2a97b..e9a2abd 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/google/go-cmp v0.5.4 // indirect github.com/google/uuid v1.1.2 github.com/gorilla/mux v1.8.0 // indirect - github.com/grandcat/zeroconf v1.0.0 + github.com/grandcat/zeroconf v1.0.0 // indirect github.com/lithammer/shortuuid v3.0.0+incompatible github.com/moby/sys/mount v0.2.0 // indirect github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf // indirect diff --git a/server/server.go b/server/server.go index e637567..4b25969 100644 --- a/server/server.go +++ b/server/server.go @@ -94,6 +94,7 @@ func Server() error{ resp, err := docker.BuildRunContainer(PortsInt,GPU) if err != nil { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) }