add display server specs and added common print function
This commit is contained in:
46
client/ServerSpecs.go
Normal file
46
client/ServerSpecs.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
|
||||
)
|
||||
|
||||
func client() {
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -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
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
2
go.mod
2
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
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user