From 10e5c72c90431a19124c18a60f47f1e85a8a29db Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar Date: Wed, 11 Aug 2021 23:16:36 +0400 Subject: [PATCH] added possibility to add a new container group --- client/GroupTrackContainer.go | 111 ++++++++++++++++++ client/GroupTrackContainer_test.go | 16 +++ client/TrackContainers.go | 16 +-- client/grouptrackcontainers.json | 0 .../trackcontainers/grouptrackcontainers.json | 8 ++ client/trackcontainers/trackcontainers.json | 41 ------- config/config.go | 24 ++-- 7 files changed, 159 insertions(+), 57 deletions(-) create mode 100644 client/GroupTrackContainer.go create mode 100644 client/GroupTrackContainer_test.go create mode 100644 client/grouptrackcontainers.json create mode 100644 client/trackcontainers/grouptrackcontainers.json diff --git a/client/GroupTrackContainer.go b/client/GroupTrackContainer.go new file mode 100644 index 0000000..05c35f7 --- /dev/null +++ b/client/GroupTrackContainer.go @@ -0,0 +1,111 @@ +package client + +import ( + "encoding/json" + "git.sr.ht/~akilan1999/p2p-rendering-computation/config" + "github.com/google/uuid" + "io/ioutil" + "os" +) + +// Groups Data Structure type +type Groups struct { + GroupList []*Group `json:"Groups"` +} + +// Group Information about a single group +type Group struct { + ID string `json:"ID"` + TrackContainerList []*TrackContainer `json:"TrackContainer"` +} + +// CreateGroup Creates a new group to add a set of trackcontainers +func CreateGroup() (*Group, error){ + // Creating variable of type new group + var NewGroup Group + // Generate new UUID for group ID + id := uuid.New() + // Add new group id and prepend with the string "grp" + // The reason this is done is to differentiate between a + // group ID and docker container ID + NewGroup.ID = "grp" + id.String() + // Adding the new group to the + // GroupTrackContainer File + err := NewGroup.AddGroupToFile() + if err != nil { + return nil, err + } + + return &NewGroup,nil +} + +// AddGroupToFile Adds Group struct to the GroupTrackContainer File +func (grp *Group) AddGroupToFile() error { + // Gets all group information from the + // GroupTrackContainer JSON file + groups, err := ReadGroup() + if err != nil { + return err + } + // Appending the newly created group + groups.GroupList = append(groups.GroupList, grp) + // Writing Group information to the GroupTrackContainer + // JSON file + err = groups.WriteGroup() + if err != nil { + return err + } + + return nil +} + +// ReadGroup Function reads grouptrackcontainers.json and converts +// result to Groups +func ReadGroup() (*Groups,error) { + // Get Path from config + config, err := config.ConfigInit() + if err != nil { + return nil,err + } + jsonFile, err := os.Open(config.GroupTrackContainersPath) + // if we os.Open returns an error then handle it + if err != nil { + return nil,err + } + + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + // read our opened xmlFile as a byte array. + byteValue, _ := ioutil.ReadAll(jsonFile) + + // we initialize our Users array + var groups Groups + + // we unmarshal our byteArray which contains our + // jsonFile's content into 'users' which we defined above + json.Unmarshal(byteValue, &groups) + return &groups, nil +} + + +// WriteGroup Function to write type Groups to the grouptrackcontainers.json file +func (grp *Groups)WriteGroup() error { + file, err := json.MarshalIndent(grp, "", " ") + if err != nil { + return err + } + + // Get Path from config + config, err := config.ConfigInit() + if err != nil { + return err + } + // Writes to the appropriate file + err = ioutil.WriteFile(config.GroupTrackContainersPath, file, 0644) + if err != nil { + return err + } + + return nil +} \ No newline at end of file diff --git a/client/GroupTrackContainer_test.go b/client/GroupTrackContainer_test.go new file mode 100644 index 0000000..a9de776 --- /dev/null +++ b/client/GroupTrackContainer_test.go @@ -0,0 +1,16 @@ +package client + +import ( + "fmt" + "testing" +) + +// Testing out if a new group is getting created +func TestCreateGroup(t *testing.T) { + group, err := CreateGroup() + if err != nil { + fmt.Println(err) + t.Fail() + } + PrettyPrint(group) +} diff --git a/client/TrackContainers.go b/client/TrackContainers.go index c37c78c..389675d 100644 --- a/client/TrackContainers.go +++ b/client/TrackContainers.go @@ -12,7 +12,7 @@ import ( // TrackContainers This struct stores arrays of current containers running type TrackContainers struct { - TrackcontianerList []TrackContainer `json:"TrackContainer"` + TrackContainerList []TrackContainer `json:"TrackContainer"` } // TrackContainer Stores information of current containers @@ -63,7 +63,7 @@ func AddTrackContainer(d *docker.DockerVM,ipAddress string) error { if &trackContainer == nil { return errors.New("trackContainer variable is nil") } - trackContainers.TrackcontianerList = append(trackContainers.TrackcontianerList, trackContainer) + trackContainers.TrackContainerList = append(trackContainers.TrackContainerList, trackContainer) // write modified information to the tracked json file data,err := json.MarshalIndent(trackContainers, "", "\t") @@ -86,12 +86,12 @@ func RemoveTrackedContainer(id string) error { return err } // Getting tracked container struct - trackedContianers, err := ReadTrackContainers(config.TrackContainersPath) + trackedContainers, err := ReadTrackContainers(config.TrackContainersPath) // Storing index of element to remove var removeElement int removeElement = -1 - for i := range trackedContianers.TrackcontianerList { - if trackedContianers.TrackcontianerList[i].Id == id { + for i := range trackedContainers.TrackContainerList { + if trackedContainers.TrackContainerList[i].Id == id { removeElement = i break } @@ -101,10 +101,10 @@ func RemoveTrackedContainer(id string) error { return errors.New("Container ID not found in the tracked list") } // Remove the detected element from the struct - trackedContianers.TrackcontianerList = append(trackedContianers.TrackcontianerList[:removeElement], trackedContianers.TrackcontianerList[removeElement+1:]...) + trackedContainers.TrackContainerList = append(trackedContainers.TrackContainerList[:removeElement], trackedContainers.TrackContainerList[removeElement+1:]...) // write modified information to the tracked json file - data,err := json.MarshalIndent(trackedContianers, "", "\t") + data,err := json.MarshalIndent(trackedContainers, "", "\t") if err != nil { return err } @@ -156,7 +156,7 @@ func GetContainerInformation(ID string) (*TrackContainer, error) { } // Iterating through all tracked containers to get the container information // of the ID passed through the function parameter - for _, container := range CurrentContainers.TrackcontianerList { + for _, container := range CurrentContainers.TrackContainerList { if container.Container.ID == ID { return &container, nil } diff --git a/client/grouptrackcontainers.json b/client/grouptrackcontainers.json new file mode 100644 index 0000000..e69de29 diff --git a/client/trackcontainers/grouptrackcontainers.json b/client/trackcontainers/grouptrackcontainers.json new file mode 100644 index 0000000..0401c79 --- /dev/null +++ b/client/trackcontainers/grouptrackcontainers.json @@ -0,0 +1,8 @@ +{ + "Groups": [ + { + "ID": "grp72f16ae0-4f68-4245-b570-93fb31de887e", + "TrackContainer": null + } + ] +} \ No newline at end of file diff --git a/client/trackcontainers/trackcontainers.json b/client/trackcontainers/trackcontainers.json index 918b120..e69de29 100644 --- a/client/trackcontainers/trackcontainers.json +++ b/client/trackcontainers/trackcontainers.json @@ -1,41 +0,0 @@ -{ - "TrackContainer": [ - { - "ID": "858912730fb34f3cce38ef6d405e04519e44f50c2a496cd400e75aaa0def3c80", - "Container": { - "SSHUsername": "master", - "SSHPassword": "password", - "ID": "858912730fb34f3cce38ef6d405e04519e44f50c2a496cd400e75aaa0def3c80", - "TagName": "p2p-ubuntu", - "ImagePath": "/home/akilan/Documents/p2p-rendering-computation/server/docker/containers/docker-ubuntu-sshd/", - "Ports": { - "Port": [ - { - "PortName": "SSH", - "InternalPort": 22, - "Type": "tcp", - "ExternalPort": 44269, - "Description": "SSH Port" - }, - { - "PortName": "NoVNC", - "InternalPort": 5901, - "Type": "tcp", - "ExternalPort": 40725, - "Description": "NoVNC port" - }, - { - "PortName": "NoVNC", - "InternalPort": 6081, - "Type": "tcp", - "ExternalPort": 40053, - "Description": "NoVNC port" - } - ] - }, - "GPU": "false" - }, - "IpAddress": "0.0.0.0" - } - ] -} \ No newline at end of file diff --git a/config/config.go b/config/config.go index 03cb262..72c34a7 100644 --- a/config/config.go +++ b/config/config.go @@ -21,14 +21,15 @@ var ( ) type Config struct { - IPTable string - DockerContainers string - DefaultDockerFile string - SpeedTestFile string - IPV6Address string - PluginPath string - TrackContainersPath string - ServerPort string + IPTable string + DockerContainers string + DefaultDockerFile string + SpeedTestFile string + IPV6Address string + PluginPath string + TrackContainersPath string + ServerPort string + GroupTrackContainersPath string //NetworkInterface string //NetworkInterfaceIPV6Index int } @@ -88,6 +89,12 @@ func SetDefaults() error { return err } + //Creates a copy of trackcontainers.json in the appropriate directory + err = Copy("client/grouptrackcontainers.json","client/trackcontainers/grouptrackcontainers.json") + if err != nil { + return err + } + //Setting default paths for the config file defaults["IPTable"] = defaultPath + "p2p/iptable/ip_table.json" @@ -97,6 +104,7 @@ func SetDefaults() error { defaults["IPV6Address"] = "" defaults["PluginPath"] = defaultPath + "plugin/deploy" defaults["TrackContainersPath"] = defaultPath + "client/trackcontainers/trackcontainers.json" + defaults["GroupTrackContainersPath"] = defaultPath + "client/trackcontainers/grouptrackcontainers.json" defaults["ServerPort"] = "8088" //defaults["NetworkInterface"] = "wlp0s20f3" //defaults["NetworkInterfaceIPV6Index"] = "2"