added possibility to add a new container group
This commit is contained in:
111
client/GroupTrackContainer.go
Normal file
111
client/GroupTrackContainer.go
Normal file
@@ -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
|
||||
}
|
||||
16
client/GroupTrackContainer_test.go
Normal file
16
client/GroupTrackContainer_test.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
0
client/grouptrackcontainers.json
Normal file
0
client/grouptrackcontainers.json
Normal file
8
client/trackcontainers/grouptrackcontainers.json
Normal file
8
client/trackcontainers/grouptrackcontainers.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Groups": [
|
||||
{
|
||||
"ID": "grp72f16ae0-4f68-4245-b570-93fb31de887e",
|
||||
"TrackContainer": null
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user