From 726292a77d5c71fc4b416920ac057c694ce8aa18 Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar Date: Sun, 18 Jul 2021 10:06:14 +0400 Subject: [PATCH] added function to add tracked container --- client/TrackContainers.go | 95 +++++++++++++++++++ client/TrackContianers_test.go | 35 +++++++ client/trackcontainers.go | 1 - ...kcontianers.json => trackcontainers.json} | 0 client/trackcontainers/trackcontainers.json | 1 + config/config.go | 2 +- server/server.go | 2 +- 7 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 client/TrackContainers.go create mode 100644 client/TrackContianers_test.go delete mode 100644 client/trackcontainers.go rename client/{trackcontianers.json => trackcontainers.json} (100%) create mode 100644 client/trackcontainers/trackcontainers.json diff --git a/client/TrackContainers.go b/client/TrackContainers.go new file mode 100644 index 0000000..82cca1c --- /dev/null +++ b/client/TrackContainers.go @@ -0,0 +1,95 @@ +package client + +import ( + "encoding/json" + "errors" + "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/config" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" + "io/ioutil" + "os" +) + +// TrackContainers This struct stores arrays of current containers running +type TrackContainers struct { + TrackcontianerList []TrackContainer `json:"TrackContainer"` +} + +// TrackContainer Stores information of current containers +type TrackContainer struct { + Id string `json:"ID"` + Container *docker.DockerVM `json:"Container"` + IpAddress string `json:"IpAddress"` +} + +// AddTrackContainer Adds new container which has been added to the track container +func AddTrackContainer(d *docker.DockerVM,ipAddress string) error { + // Checking if pointer d is null + if d == nil { + return errors.New("d is nil") + } + //Get config information to derive paths for track containers json file + config,err := config.ConfigInit() + if err != nil { + return err + } + + // Getting information about the file trackcontainers.json file + stat, err := os.Stat(config.TrackContainersPath) + if err != nil { + return err + } + // Initialize variable for TrackContainers + var trackContainers TrackContainers + // If the trackcontainers.json file is not empty then + // Read from that file + if stat.Size() != 0 { + // Reads tracked container file + trackContainersFile, err := ReadTrackContainers(config.TrackContainersPath) + if err != nil { + return err + } + trackContainers = *trackContainersFile + } + + // Initialize new variable with type struct TrackContainers and + // add container struct and ip address + var trackContainer TrackContainer + trackContainer.Id = d.ID + trackContainer.Container = d + trackContainer.IpAddress = ipAddress + + // Adds new container as passed in the parameter to the struct + if &trackContainer == nil { + return errors.New("trackContainer variable is nil") + } + trackContainers.TrackcontianerList = append(trackContainers.TrackcontianerList, trackContainer) + + // write modified information to the tracked json file + data,err := json.Marshal(trackContainers) + if err != nil { + return err + } + err = ioutil.WriteFile(config.TrackContainersPath,data,0777) + if err != nil { + return err + } + + return nil +} + +// ReadTrackContainers Reads containers which are currently tracked +func ReadTrackContainers(filename string) (*TrackContainers, error) { + buf, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + + c := &TrackContainers{} + err = json.Unmarshal(buf, c) + if err != nil { + return nil, fmt.Errorf("in file %q: %v", filename, err) + } + + return c, nil +} \ No newline at end of file diff --git a/client/TrackContianers_test.go b/client/TrackContianers_test.go new file mode 100644 index 0000000..c62135a --- /dev/null +++ b/client/TrackContianers_test.go @@ -0,0 +1,35 @@ +package client + +import ( + "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" + "testing" +) + +// Tests a scenario where the container are getting tracked +func TestAddTrackContainer(t *testing.T) { + // Create docker container and get SSH port + container1 ,err := docker.BuildRunContainer(0,"false","") + if err != nil { + fmt.Println(err) + t.Fail() + } + // Testing the AddTrackContainer Function + err = AddTrackContainer(container1,"0.0.0.0") + if err != nil { + + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + fmt.Println(err) + t.Fail() + } + + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } +} diff --git a/client/trackcontainers.go b/client/trackcontainers.go deleted file mode 100644 index da13c8e..0000000 --- a/client/trackcontainers.go +++ /dev/null @@ -1 +0,0 @@ -package client diff --git a/client/trackcontianers.json b/client/trackcontainers.json similarity index 100% rename from client/trackcontianers.json rename to client/trackcontainers.json diff --git a/client/trackcontainers/trackcontainers.json b/client/trackcontainers/trackcontainers.json new file mode 100644 index 0000000..aaf05d1 --- /dev/null +++ b/client/trackcontainers/trackcontainers.json @@ -0,0 +1 @@ +{"TrackContainer":[{"ID":"b8c764b1f14f15cebc0fb7739b0ab1819906c8774c9b7ecf08c5503d5722fffc","Container":{"SSHPort":41731,"SSHUsername":"master","SSHPassword":"password","VNCPort":0,"VNCPassword":"","ID":"b8c764b1f14f15cebc0fb7739b0ab1819906c8774c9b7ecf08c5503d5722fffc","TagName":"p2p-ubuntu","ImagePath":"/home/akilan/Documents/p2p-rendering-computation/server/docker/containers/docker-ubuntu-sshd/","OpenPorts":null,"GPU":"false"},"IpAddress":"0.0.0.0"}]} \ No newline at end of file diff --git a/config/config.go b/config/config.go index 94af912..b512d36 100644 --- a/config/config.go +++ b/config/config.go @@ -82,7 +82,7 @@ func SetDefaults() error { } //Creates a copy of trackcontainers.json in the appropriate directory - err = Copy("p2p/trackcontainers.json","client/trackcontainers/trackcontainers.json") + err = Copy("client/trackcontainers.json","client/trackcontainers/trackcontainers.json") if err != nil { return err } diff --git a/server/server.go b/server/server.go index 8399862..0cd40ca 100644 --- a/server/server.go +++ b/server/server.go @@ -131,7 +131,7 @@ func Server() error{ c.JSON(http.StatusOK, resp) }) - // Port running on + //Port running on err := r.Run(":8088") if err != nil { return err