Compare commits
5 Commits
simulation
...
untracked-
| Author | SHA1 | Date | |
|---|---|---|---|
| d2d1b5bc9f | |||
| 888ad8acb7 | |||
| e5d4381fd9 | |||
| c7be50d3b7 | |||
| 1c06d5b882 |
10
.gitignore
vendored
10
.gitignore
vendored
@@ -11,13 +11,23 @@ config.json
|
|||||||
|
|
||||||
#ignore generated iptables
|
#ignore generated iptables
|
||||||
p2p/iptable/
|
p2p/iptable/
|
||||||
|
!p2p/iptable/.gitkeep
|
||||||
#ignore plugins added
|
#ignore plugins added
|
||||||
plugin/deploy/
|
plugin/deploy/
|
||||||
|
!plugin/deploy/.gitkeep
|
||||||
#ignore track container file
|
#ignore track container file
|
||||||
client/trackcontainers/
|
client/trackcontainers/
|
||||||
|
!client/trackcontainers/.gitkeep
|
||||||
# Test generated files
|
# Test generated files
|
||||||
generate/p2prctest
|
generate/p2prctest
|
||||||
|
!generate/p2prctest/.gitkeep
|
||||||
generate/Test
|
generate/Test
|
||||||
|
!generate/Test/.gitkeep
|
||||||
|
|
||||||
#ignore windows exe files
|
#ignore windows exe files
|
||||||
*.exe
|
*.exe
|
||||||
dist/
|
dist/
|
||||||
|
|
||||||
|
#MACOS .idea file
|
||||||
|
.DS_Store
|
||||||
|
.gitkeep
|
||||||
|
|||||||
BIN
artwork/p2prc-logos/.DS_Store
vendored
BIN
artwork/p2prc-logos/.DS_Store
vendored
Binary file not shown.
@@ -3,7 +3,7 @@ package client
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@@ -16,7 +16,7 @@ type Groups struct {
|
|||||||
|
|
||||||
// Group Information about a single group
|
// Group Information about a single group
|
||||||
type Group struct {
|
type Group struct {
|
||||||
ID string `json:"ID"`
|
ID string `json:"ID"`
|
||||||
TrackContainerList []*TrackContainer `json:"TrackContainer"`
|
TrackContainerList []*TrackContainer `json:"TrackContainer"`
|
||||||
// Sneaky as required only when removing the element
|
// Sneaky as required only when removing the element
|
||||||
// Set when GetGroup function is called
|
// Set when GetGroup function is called
|
||||||
@@ -24,7 +24,7 @@ type Group struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateGroup Creates a new group to add a set of track containers
|
// CreateGroup Creates a new group to add a set of track containers
|
||||||
func CreateGroup() (*Group, error){
|
func CreateGroup() (*Group, error) {
|
||||||
// Creating variable of type new group
|
// Creating variable of type new group
|
||||||
var NewGroup Group
|
var NewGroup Group
|
||||||
// Generate new UUID for group ID
|
// Generate new UUID for group ID
|
||||||
@@ -40,7 +40,7 @@ func CreateGroup() (*Group, error){
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &NewGroup,nil
|
return &NewGroup, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveGroup Removes group based on the group ID provided
|
// RemoveGroup Removes group based on the group ID provided
|
||||||
@@ -57,7 +57,7 @@ func RemoveGroup(GroupID string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Remove Group struct from the groups variable
|
// Remove Group struct from the groups variable
|
||||||
groups.GroupList = append(groups.GroupList[:group.index], groups.GroupList[group.index+1:]...)
|
groups.GroupList = append(groups.GroupList[:group.index], groups.GroupList[group.index+1:]...)
|
||||||
|
|
||||||
// Write new groups to the grouptrackcontainer json file
|
// Write new groups to the grouptrackcontainer json file
|
||||||
@@ -89,7 +89,7 @@ func AddContainerToGroup(ContainerID string, GroupID string) (*Group, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Updating specific element in the group list with the added container
|
// Updating specific element in the group list with the added container
|
||||||
groups.GroupList[group.index] = group
|
groups.GroupList[group.index] = group
|
||||||
// Write groups information on the grouptrackcontainer.json file
|
// Write groups information on the grouptrackcontainer.json file
|
||||||
err = groups.WriteGroup()
|
err = groups.WriteGroup()
|
||||||
@@ -101,21 +101,21 @@ func AddContainerToGroup(ContainerID string, GroupID string) (*Group, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoveContainerGroup Remove Container from the group ID specified
|
// RemoveContainerGroup Remove Container from the group ID specified
|
||||||
func RemoveContainerGroup(ContainerID string, GroupID string) (*Group,error) {
|
func RemoveContainerGroup(ContainerID string, GroupID string) (*Group, error) {
|
||||||
// Get container information based on container ID provided
|
// Get container information based on container ID provided
|
||||||
containerInfo, err := GetContainerInformation(ContainerID)
|
containerInfo, err := GetContainerInformation(ContainerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Gets group information based on the group ID provided
|
// Gets group information based on the group ID provided
|
||||||
group, err := GetGroup(GroupID)
|
group, err := GetGroup(GroupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Remove container from the appropriate group
|
// Remove container from the appropriate group
|
||||||
err = group.RemoveContainerGroup(containerInfo)
|
err = group.RemoveContainerGroup(containerInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Get Groups information from reading the grouptrackcontainer.json file
|
// Get Groups information from reading the grouptrackcontainer.json file
|
||||||
groups, err := ReadGroup()
|
groups, err := ReadGroup()
|
||||||
@@ -163,7 +163,7 @@ func RemoveContainerGroups(ContainerID string) error {
|
|||||||
|
|
||||||
// GetGroup Gets group information based on
|
// GetGroup Gets group information based on
|
||||||
// group id provided
|
// group id provided
|
||||||
func GetGroup(GroupID string) (*Group,error) {
|
func GetGroup(GroupID string) (*Group, error) {
|
||||||
// Read group information from the
|
// Read group information from the
|
||||||
//grouptrackcontainer json file
|
//grouptrackcontainer json file
|
||||||
groups, err := ReadGroup()
|
groups, err := ReadGroup()
|
||||||
@@ -179,16 +179,16 @@ func GetGroup(GroupID string) (*Group,error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil,errors.New("Group not found. ")
|
return nil, errors.New("Group not found. ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddGroupToFile Adds Group struct to the GroupTrackContainer File
|
// AddGroupToFile Adds Group struct to the GroupTrackContainer File
|
||||||
func (grp *Group) AddGroupToFile() error {
|
func (grp *Group) AddGroupToFile() error {
|
||||||
// Gets all group information from the
|
// Gets all group information from the
|
||||||
// GroupTrackContainer JSON file
|
// GroupTrackContainer JSON file
|
||||||
groups, err := ReadGroup()
|
groups, err := ReadGroup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Appending the newly created group
|
// Appending the newly created group
|
||||||
groups.GroupList = append(groups.GroupList, grp)
|
groups.GroupList = append(groups.GroupList, grp)
|
||||||
@@ -204,16 +204,16 @@ func (grp *Group) AddGroupToFile() error {
|
|||||||
|
|
||||||
// ReadGroup Function reads grouptrackcontainers.json and converts
|
// ReadGroup Function reads grouptrackcontainers.json and converts
|
||||||
// result to Groups
|
// result to Groups
|
||||||
func ReadGroup() (*Groups,error) {
|
func ReadGroup() (*Groups, error) {
|
||||||
// Get Path from config
|
// Get Path from config
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
jsonFile, err := os.Open(config.GroupTrackContainersPath)
|
jsonFile, err := os.Open(config.GroupTrackContainersPath)
|
||||||
// if we os.Open returns an error then handle it
|
// if we os.Open returns an error then handle it
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
// defer the closing of our jsonFile so that we can parse it later on
|
// defer the closing of our jsonFile so that we can parse it later on
|
||||||
defer jsonFile.Close()
|
defer jsonFile.Close()
|
||||||
@@ -227,9 +227,8 @@ func ReadGroup() (*Groups,error) {
|
|||||||
return &groups, nil
|
return &groups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// WriteGroup Function to write type Groups to the grouptrackcontainers.json file
|
// WriteGroup Function to write type Groups to the grouptrackcontainers.json file
|
||||||
func (grp *Groups)WriteGroup() error {
|
func (grp *Groups) WriteGroup() error {
|
||||||
file, err := json.MarshalIndent(grp, "", " ")
|
file, err := json.MarshalIndent(grp, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -239,7 +238,7 @@ func (grp *Groups)WriteGroup() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Writes to the appropriate file
|
// Writes to the appropriate file
|
||||||
err = ioutil.WriteFile(config.GroupTrackContainersPath, file, 0644)
|
err = ioutil.WriteFile(config.GroupTrackContainersPath, file, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -248,13 +247,13 @@ func (grp *Groups)WriteGroup() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddContainer Adds a container to the Tracked container list of the group
|
// AddContainer Adds a container to the Tracked container list of the group
|
||||||
func (grp *Group)AddContainer(Container *TrackContainer) error {
|
func (grp *Group) AddContainer(Container *TrackContainer) error {
|
||||||
grp.TrackContainerList = append(grp.TrackContainerList, Container)
|
grp.TrackContainerList = append(grp.TrackContainerList, Container)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveContainerGroup Removes container information from the group
|
// RemoveContainerGroup Removes container information from the group
|
||||||
func (grp *Group)RemoveContainerGroup(Container *TrackContainer) error {
|
func (grp *Group) RemoveContainerGroup(Container *TrackContainer) error {
|
||||||
// Iterating through all container in the Group of Tracked Container
|
// Iterating through all container in the Group of Tracked Container
|
||||||
for i, container := range grp.TrackContainerList {
|
for i, container := range grp.TrackContainerList {
|
||||||
// If the container ID matches then remove the container from the group
|
// If the container ID matches then remove the container from the group
|
||||||
@@ -266,9 +265,9 @@ func (grp *Group)RemoveContainerGroup(Container *TrackContainer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoveContainerGroups removes container found in all groups
|
// RemoveContainerGroups removes container found in all groups
|
||||||
func (grp *Groups)RemoveContainerGroups(Container *TrackContainer) error {
|
func (grp *Groups) RemoveContainerGroups(Container *TrackContainer) error {
|
||||||
// Iterating through all groups
|
// Iterating through all groups
|
||||||
for i,group := range grp.GroupList {
|
for i, group := range grp.GroupList {
|
||||||
// Removes the container in the following group
|
// Removes the container in the following group
|
||||||
// if it exists
|
// if it exists
|
||||||
err := group.RemoveContainerGroup(Container)
|
err := group.RemoveContainerGroup(Container)
|
||||||
@@ -283,7 +282,7 @@ func (grp *Groups)RemoveContainerGroups(Container *TrackContainer) error {
|
|||||||
|
|
||||||
// ModifyContainerGroups Modifies container information is all groups
|
// ModifyContainerGroups Modifies container information is all groups
|
||||||
// available
|
// available
|
||||||
func (TC *TrackContainer)ModifyContainerGroups() error {
|
func (TC *TrackContainer) ModifyContainerGroups() error {
|
||||||
group, err := ReadGroup()
|
group, err := ReadGroup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -293,7 +292,7 @@ func (TC *TrackContainer)ModifyContainerGroups() error {
|
|||||||
// ID matches
|
// ID matches
|
||||||
for i, _ := range group.GroupList {
|
for i, _ := range group.GroupList {
|
||||||
// Checking in each group if the modified container ID exists
|
// Checking in each group if the modified container ID exists
|
||||||
for j, _:= range group.GroupList[i].TrackContainerList {
|
for j, _ := range group.GroupList[i].TrackContainerList {
|
||||||
// If there is match then change them
|
// If there is match then change them
|
||||||
if group.GroupList[i].TrackContainerList[j].Id == TC.Id {
|
if group.GroupList[i].TrackContainerList[j].Id == TC.Id {
|
||||||
group.GroupList[i].TrackContainerList[j] = TC
|
group.GroupList[i].TrackContainerList[j] = TC
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,14 +46,14 @@ func TestAddContainerToGroup(t *testing.T) {
|
|||||||
|
|
||||||
// Creating and adding the container to the
|
// Creating and adding the container to the
|
||||||
// tracked list
|
// tracked list
|
||||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the AddTrackContainer Function and adding the first container created
|
// Testing the AddTrackContainer Function and adding the first container created
|
||||||
err = AddTrackContainer(container1,"0.0.0.0")
|
err = AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
@@ -66,7 +66,7 @@ func TestAddContainerToGroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Adds container information to the group
|
// Adds container information to the group
|
||||||
Group, err := AddContainerToGroup(container1.ID,group.ID)
|
Group, err := AddContainerToGroup(container1.ID, group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@@ -109,14 +109,14 @@ func TestGroup_RemoveContainerGroup(t *testing.T) {
|
|||||||
|
|
||||||
// Creating and adding the container to the
|
// Creating and adding the container to the
|
||||||
// tracked list
|
// tracked list
|
||||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the AddTrackContainer Function and adding the first container created
|
// Testing the AddTrackContainer Function and adding the first container created
|
||||||
err = AddTrackContainer(container1,"0.0.0.0")
|
err = AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
@@ -129,7 +129,7 @@ func TestGroup_RemoveContainerGroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Adds container information to the group
|
// Adds container information to the group
|
||||||
Group, err := AddContainerToGroup(container1.ID,group.ID)
|
Group, err := AddContainerToGroup(container1.ID, group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@@ -138,7 +138,7 @@ func TestGroup_RemoveContainerGroup(t *testing.T) {
|
|||||||
PrettyPrint(Group)
|
PrettyPrint(Group)
|
||||||
|
|
||||||
// Removing docker container from the group
|
// Removing docker container from the group
|
||||||
Group, err = RemoveContainerGroup(container1.ID,group.ID)
|
Group, err = RemoveContainerGroup(container1.ID, group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@@ -182,7 +182,7 @@ func TestGroups_RemoveContainerGroups(t *testing.T) {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
// Created another group assigned to variable group 1
|
// Created another group assigned to variable group 1
|
||||||
group1, err := CreateGroup()
|
group1, err := CreateGroup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -191,14 +191,14 @@ func TestGroups_RemoveContainerGroups(t *testing.T) {
|
|||||||
|
|
||||||
// Creating and adding the container to the
|
// Creating and adding the container to the
|
||||||
// tracked list
|
// tracked list
|
||||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the AddTrackContainer Function and adding the first container created
|
// Testing the AddTrackContainer Function and adding the first container created
|
||||||
err = AddTrackContainer(container1,"0.0.0.0")
|
err = AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
@@ -211,7 +211,7 @@ func TestGroups_RemoveContainerGroups(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Adds container information to the group
|
// Adds container information to the group
|
||||||
Group, err := AddContainerToGroup(container1.ID,group.ID)
|
Group, err := AddContainerToGroup(container1.ID, group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@@ -220,7 +220,7 @@ func TestGroups_RemoveContainerGroups(t *testing.T) {
|
|||||||
PrettyPrint(Group)
|
PrettyPrint(Group)
|
||||||
|
|
||||||
// Adds container information to the group
|
// Adds container information to the group
|
||||||
Group1, err := AddContainerToGroup(container1.ID,group1.ID)
|
Group1, err := AddContainerToGroup(container1.ID, group1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package client
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server"
|
"github.com/Akilan1999/p2p-rendering-computation/server"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,38 +4,38 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TrackContainers This struct stores arrays of current containers running
|
// TrackContainers This struct stores arrays of current containers running
|
||||||
type TrackContainers struct {
|
type TrackContainers struct {
|
||||||
TrackContainerList []TrackContainer `json:"TrackContainer"`
|
TrackContainerList []TrackContainer `json:"TrackContainer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrackContainer Stores information of current containers
|
// TrackContainer Stores information of current containers
|
||||||
type TrackContainer struct {
|
type TrackContainer struct {
|
||||||
Id string `json:"ID"`
|
Id string `json:"ID"`
|
||||||
Container *docker.DockerVM `json:"Container"`
|
Container *docker.DockerVM `json:"Container"`
|
||||||
IpAddress string `json:"IpAddress"`
|
IpAddress string `json:"IpAddress"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTrackContainer Adds new container which has been added to the track container
|
// AddTrackContainer Adds new container which has been added to the track container
|
||||||
func AddTrackContainer(d *docker.DockerVM,ipAddress string) error {
|
func AddTrackContainer(d *docker.DockerVM, ipAddress string) error {
|
||||||
// Checking if pointer d is null
|
// Checking if pointer d is null
|
||||||
if d == nil {
|
if d == nil {
|
||||||
return errors.New("d is nil")
|
return errors.New("d is nil")
|
||||||
}
|
}
|
||||||
//Get config information to derive paths for track containers json file
|
//Get config information to derive paths for track containers json file
|
||||||
config,err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getting information about the file trackcontainers.json file
|
// Getting information about the file trackcontainers.json file
|
||||||
stat, err := os.Stat(config.TrackContainersPath)
|
stat, err := os.Stat(config.TrackContainersPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -66,11 +66,11 @@ func AddTrackContainer(d *docker.DockerVM,ipAddress string) error {
|
|||||||
trackContainers.TrackContainerList = append(trackContainers.TrackContainerList, trackContainer)
|
trackContainers.TrackContainerList = append(trackContainers.TrackContainerList, trackContainer)
|
||||||
|
|
||||||
// write modified information to the tracked json file
|
// write modified information to the tracked json file
|
||||||
data,err := json.MarshalIndent(trackContainers, "", "\t")
|
data, err := json.MarshalIndent(trackContainers, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(config.TrackContainersPath,data,0777)
|
err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ func AddTrackContainer(d *docker.DockerVM,ipAddress string) error {
|
|||||||
// RemoveTrackedContainer This function removos tracked container from the trackcontainer JSON file
|
// RemoveTrackedContainer This function removos tracked container from the trackcontainer JSON file
|
||||||
func RemoveTrackedContainer(id string) error {
|
func RemoveTrackedContainer(id string) error {
|
||||||
//Get config information to derive paths for track containers json file
|
//Get config information to derive paths for track containers json file
|
||||||
config,err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -104,11 +104,11 @@ func RemoveTrackedContainer(id string) error {
|
|||||||
trackedContainers.TrackContainerList = append(trackedContainers.TrackContainerList[:removeElement], trackedContainers.TrackContainerList[removeElement+1:]...)
|
trackedContainers.TrackContainerList = append(trackedContainers.TrackContainerList[:removeElement], trackedContainers.TrackContainerList[removeElement+1:]...)
|
||||||
|
|
||||||
// write modified information to the tracked json file
|
// write modified information to the tracked json file
|
||||||
data,err := json.MarshalIndent(trackedContainers, "", "\t")
|
data, err := json.MarshalIndent(trackedContainers, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(config.TrackContainersPath,data,0777)
|
err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -117,17 +117,17 @@ func RemoveTrackedContainer(id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ViewTrackedContainers View Containers currently tracked
|
// ViewTrackedContainers View Containers currently tracked
|
||||||
func ViewTrackedContainers() (error,*TrackContainers) {
|
func ViewTrackedContainers() (error, *TrackContainers) {
|
||||||
config,err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err,nil
|
return err, nil
|
||||||
}
|
}
|
||||||
trackedContianers, err := ReadTrackContainers(config.TrackContainersPath)
|
trackedContianers, err := ReadTrackContainers(config.TrackContainersPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err,nil
|
return err, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil,trackedContianers
|
return nil, trackedContianers
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTrackContainers Reads containers which are currently tracked
|
// ReadTrackContainers Reads containers which are currently tracked
|
||||||
@@ -167,7 +167,7 @@ func GetContainerInformation(ID string) (*TrackContainer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ModifyContainerInformation Modifies information inside the container
|
// ModifyContainerInformation Modifies information inside the container
|
||||||
func (TC *TrackContainer)ModifyContainerInformation() error {
|
func (TC *TrackContainer) ModifyContainerInformation() error {
|
||||||
// Gets all the information of tracker containers
|
// Gets all the information of tracker containers
|
||||||
err, t := ViewTrackedContainers()
|
err, t := ViewTrackedContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -192,15 +192,15 @@ func (TC *TrackContainer)ModifyContainerInformation() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteContainers Write information back to the config file
|
// WriteContainers Write information back to the config file
|
||||||
func (TC *TrackContainers)WriteContainers() error {
|
func (TC *TrackContainers) WriteContainers() error {
|
||||||
// Initialize config file
|
// Initialize config file
|
||||||
config,err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
// write modified information to the tracked json file
|
// write modified information to the tracked json file
|
||||||
data,err := json.MarshalIndent(TC, "", "\t")
|
data, err := json.MarshalIndent(TC, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(config.TrackContainersPath,data,0777)
|
err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -209,12 +209,12 @@ func (TC *TrackContainers)WriteContainers() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckID Checks if the ID belongs to a group or a single container
|
// CheckID Checks if the ID belongs to a group or a single container
|
||||||
func CheckID(ID string) (string,error) {
|
func CheckID(ID string) (string, error) {
|
||||||
// For group checks if the 1st characters is "grp"
|
// For group checks if the 1st characters is "grp"
|
||||||
if ID[0:3] == "grp" {
|
if ID[0:3] == "grp" {
|
||||||
return "group", nil
|
return "group", nil
|
||||||
} else {
|
} else {
|
||||||
return "container", nil
|
return "container", nil
|
||||||
}
|
}
|
||||||
return "",nil
|
return "", nil
|
||||||
}
|
}
|
||||||
@@ -2,20 +2,20 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tests a scenario where the container are getting tracked
|
// Tests a scenario where the container are getting tracked
|
||||||
func TestAddTrackContainer(t *testing.T) {
|
func TestAddTrackContainer(t *testing.T) {
|
||||||
// Create docker container and get SSH port
|
// Create docker container and get SSH port
|
||||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
// Testing the AddTrackContainer Function
|
// Testing the AddTrackContainer Function
|
||||||
err = AddTrackContainer(container1,"0.0.0.0")
|
err = AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
@@ -26,7 +26,7 @@ func TestAddTrackContainer(t *testing.T) {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -38,20 +38,20 @@ func TestAddTrackContainer(t *testing.T) {
|
|||||||
// NOTE: This test can also be considered as a whole flow on the process of
|
// NOTE: This test can also be considered as a whole flow on the process of
|
||||||
// tracked containers
|
// tracked containers
|
||||||
func TestRemoveTrackedContainer(t *testing.T) {
|
func TestRemoveTrackedContainer(t *testing.T) {
|
||||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
container2 ,err := docker.BuildRunContainer(0,"false","")
|
container2, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the AddTrackContainer Function and adding the first container created
|
// Testing the AddTrackContainer Function and adding the first container created
|
||||||
err = AddTrackContainer(container1,"0.0.0.0")
|
err = AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
@@ -70,7 +70,7 @@ func TestRemoveTrackedContainer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Testing the AddTrackContainer Function and the adding the second container created
|
// Testing the AddTrackContainer Function and the adding the second container created
|
||||||
err = AddTrackContainer(container2,"0.0.0.0")
|
err = AddTrackContainer(container2, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Killing docker container created
|
// Killing docker container created
|
||||||
err = docker.StopAndRemoveContainer(container2.ID)
|
err = docker.StopAndRemoveContainer(container2.ID)
|
||||||
@@ -106,7 +106,7 @@ func TestRemoveTrackedContainer(t *testing.T) {
|
|||||||
// Test function that checks if the ID belongs to
|
// Test function that checks if the ID belongs to
|
||||||
// a group or container running
|
// a group or container running
|
||||||
func TestCheckID(t *testing.T) {
|
func TestCheckID(t *testing.T) {
|
||||||
id := "grp123"
|
id := "grp123"
|
||||||
checkID, err := CheckID(id)
|
checkID, err := CheckID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package clientIPTable
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package client
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
"github.com/Akilan1999/p2p-rendering-computation/client"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client/clientIPTable"
|
"github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/generate"
|
"github.com/Akilan1999/p2p-rendering-computation/generate"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/plugin"
|
"github.com/Akilan1999/p2p-rendering-computation/plugin"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server"
|
"github.com/Akilan1999/p2p-rendering-computation/server"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package generate
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
@@ -17,15 +17,15 @@ import (
|
|||||||
|
|
||||||
// NewProject Struct information required when creating a new project
|
// NewProject Struct information required when creating a new project
|
||||||
type NewProject struct {
|
type NewProject struct {
|
||||||
Name string
|
Name string
|
||||||
Module string
|
Module string
|
||||||
NewDir string
|
NewDir string
|
||||||
P2PRCPath string
|
P2PRCPath string
|
||||||
CurrentModule string
|
CurrentModule string
|
||||||
Option *copy.Options
|
Option *copy.Options
|
||||||
Token *token.FileSet
|
Token *token.FileSet
|
||||||
AST *ast.File
|
AST *ast.File
|
||||||
FileNameAST string
|
FileNameAST string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateNewProject creates a new copy of the P2PRC
|
// GenerateNewProject creates a new copy of the P2PRC
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package generate
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ func TestCreateFolder(t *testing.T) {
|
|||||||
// Testing if a new project is created successfully
|
// Testing if a new project is created successfully
|
||||||
func TestGenerateNewProject(t *testing.T) {
|
func TestGenerateNewProject(t *testing.T) {
|
||||||
// Checking if a new project is created successfully
|
// Checking if a new project is created successfully
|
||||||
err := GenerateNewProject("p2prctest","p2prctest")
|
err := GenerateNewProject("p2prctest", "p2prctest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -29,7 +29,7 @@ func TestGenerateNewProject(t *testing.T) {
|
|||||||
// Testing AST function to ensure imports are
|
// Testing AST function to ensure imports are
|
||||||
// working as intended
|
// working as intended
|
||||||
func TestChangingImportAST(t *testing.T) {
|
func TestChangingImportAST(t *testing.T) {
|
||||||
// Create a new variable of type NewProject
|
// Create a new variable of type NewProject
|
||||||
var np NewProject
|
var np NewProject
|
||||||
// Get current directory
|
// Get current directory
|
||||||
path, err := config.GetCurrentPath()
|
path, err := config.GetCurrentPath()
|
||||||
@@ -38,7 +38,7 @@ func TestChangingImportAST(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
// Create testcase scenario
|
// Create testcase scenario
|
||||||
err = config.Copy(path + "testcaseAST.go", path + "/Test/testcaseAST.go")
|
err = config.Copy(path+"testcaseAST.go", path+"/Test/testcaseAST.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module git.sr.ht/~akilan1999/p2p-rendering-computation
|
module github.com/Akilan1999/p2p-rendering-computation
|
||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/cmd"
|
"github.com/Akilan1999/p2p-rendering-computation/cmd"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -12,7 +12,7 @@ var VERSION = "1.5.0"
|
|||||||
var mode string
|
var mode string
|
||||||
|
|
||||||
// Varaibles if mode is client
|
// Varaibles if mode is client
|
||||||
var OS, Pull_location ,Run_script string
|
var OS, Pull_location, Run_script string
|
||||||
var List_servers, Ip_table bool
|
var List_servers, Ip_table bool
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package frp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"github.com/fatedier/frp/client"
|
"github.com/fatedier/frp/client"
|
||||||
"github.com/fatedier/frp/pkg/config"
|
"github.com/fatedier/frp/pkg/config"
|
||||||
"github.com/phayes/freeport"
|
"github.com/phayes/freeport"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package p2p
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package p2p
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -29,7 +29,7 @@ func DownloadPlugin(pluginurl string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// clones a repo and stores it at the plugin directory
|
// clones a repo and stores it at the plugin directory
|
||||||
_, err = git.PlainClone(config.PluginPath + "/" + folder, false, &git.CloneOptions{
|
_, err = git.PlainClone(config.PluginPath+"/"+folder, false, &git.CloneOptions{
|
||||||
URL: pluginurl,
|
URL: pluginurl,
|
||||||
Progress: os.Stdout,
|
Progress: os.Stdout,
|
||||||
})
|
})
|
||||||
@@ -38,7 +38,6 @@ func DownloadPlugin(pluginurl string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
704
plugin/plugin.go
704
plugin/plugin.go
@@ -1,362 +1,362 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
"github.com/Akilan1999/p2p-rendering-computation/client"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/apenella/go-ansible/pkg/execute"
|
"github.com/apenella/go-ansible/pkg/execute"
|
||||||
"github.com/apenella/go-ansible/pkg/options"
|
"github.com/apenella/go-ansible/pkg/options"
|
||||||
"github.com/apenella/go-ansible/pkg/playbook"
|
"github.com/apenella/go-ansible/pkg/playbook"
|
||||||
"github.com/apenella/go-ansible/pkg/stdoutcallback/results"
|
"github.com/apenella/go-ansible/pkg/stdoutcallback/results"
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Plugins Array of all plugins detected
|
// Plugins Array of all plugins detected
|
||||||
type Plugins struct {
|
type Plugins struct {
|
||||||
PluginsDetected []*Plugin
|
PluginsDetected []*Plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin Information about the plugins available
|
// Plugin Information about the plugins available
|
||||||
type Plugin struct {
|
type Plugin struct {
|
||||||
FolderName string
|
FolderName string
|
||||||
PluginDescription string
|
PluginDescription string
|
||||||
path string
|
path string
|
||||||
Execute []*ExecuteIP
|
Execute []*ExecuteIP
|
||||||
NumOfPorts int
|
NumOfPorts int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecuteIP IP Address to execute Ansible instruction
|
// ExecuteIP IP Address to execute Ansible instruction
|
||||||
type ExecuteIP struct {
|
type ExecuteIP struct {
|
||||||
ContainerID string
|
ContainerID string
|
||||||
IPAddress string
|
IPAddress string
|
||||||
SSHPortNo string
|
SSHPortNo string
|
||||||
Success bool
|
Success bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Host Struct for ansible host
|
// Host Struct for ansible host
|
||||||
// Generated from https://zhwt.github.io/yaml-to-go/
|
// Generated from https://zhwt.github.io/yaml-to-go/
|
||||||
type Host struct {
|
type Host struct {
|
||||||
All struct {
|
All struct {
|
||||||
Vars struct {
|
Vars struct {
|
||||||
AnsiblePythonInterpreter string `yaml:"ansible_python_interpreter"`
|
AnsiblePythonInterpreter string `yaml:"ansible_python_interpreter"`
|
||||||
} `yaml:"vars"`
|
} `yaml:"vars"`
|
||||||
} `yaml:"all"`
|
} `yaml:"all"`
|
||||||
Main struct {
|
Main struct {
|
||||||
Hosts struct {
|
Hosts struct {
|
||||||
Host1 struct {
|
Host1 struct {
|
||||||
AnsibleHost string `yaml:"ansible_host"`
|
AnsibleHost string `yaml:"ansible_host"`
|
||||||
AnsiblePort int `yaml:"ansible_port"`
|
AnsiblePort int `yaml:"ansible_port"`
|
||||||
AnsibleUser string `yaml:"ansible_user"`
|
AnsibleUser string `yaml:"ansible_user"`
|
||||||
AnsibleSSHPass string `yaml:"ansible_ssh_pass"`
|
AnsibleSSHPass string `yaml:"ansible_ssh_pass"`
|
||||||
AnsibleSudoPass string `yaml:"ansible_sudo_pass"`
|
AnsibleSudoPass string `yaml:"ansible_sudo_pass"`
|
||||||
} `yaml:"host1"`
|
} `yaml:"host1"`
|
||||||
} `yaml:"hosts"`
|
} `yaml:"hosts"`
|
||||||
} `yaml:"main"`
|
} `yaml:"main"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DetectPlugins Detects all the plugins available
|
// DetectPlugins Detects all the plugins available
|
||||||
func DetectPlugins() (*Plugins, error) {
|
func DetectPlugins() (*Plugins, error) {
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
folders, err := ioutil.ReadDir(config.PluginPath)
|
folders, err := ioutil.ReadDir(config.PluginPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var plugins *Plugins = new(Plugins)
|
var plugins *Plugins = new(Plugins)
|
||||||
|
|
||||||
for _, f := range folders {
|
for _, f := range folders {
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
//Declare variable plugin of type Plugin
|
//Declare variable plugin of type Plugin
|
||||||
var plugin Plugin
|
var plugin Plugin
|
||||||
|
|
||||||
// Setting name of folder to plugin
|
// Setting name of folder to plugin
|
||||||
plugin.FolderName = f.Name()
|
plugin.FolderName = f.Name()
|
||||||
// Getting Description from file description.txt
|
// Getting Description from file description.txt
|
||||||
Description, err := ioutil.ReadFile(config.PluginPath + "/" + plugin.FolderName + "/description.txt")
|
Description, err := ioutil.ReadFile(config.PluginPath + "/" + plugin.FolderName + "/description.txt")
|
||||||
// if we os.Open returns an error then handle it
|
// if we os.Open returns an error then handle it
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Description from description.txt
|
// Get Description from description.txt
|
||||||
plugin.PluginDescription = string(Description)
|
plugin.PluginDescription = string(Description)
|
||||||
// Set plugin path
|
// Set plugin path
|
||||||
plugin.path = config.PluginPath + "/" + plugin.FolderName
|
plugin.path = config.PluginPath + "/" + plugin.FolderName
|
||||||
|
|
||||||
plugins.PluginsDetected = append(plugins.PluginsDetected, &plugin)
|
plugins.PluginsDetected = append(plugins.PluginsDetected, &plugin)
|
||||||
// Get the number of ports the plugin needs
|
// Get the number of ports the plugin needs
|
||||||
err = plugin.NumPorts()
|
err = plugin.NumPorts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugins, nil
|
return plugins, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchPlugin Detects plugin information based on the
|
// SearchPlugin Detects plugin information based on the
|
||||||
// name provided on the parameter
|
// name provided on the parameter
|
||||||
func SearchPlugin(pluginname string) (*Plugin, error) {
|
func SearchPlugin(pluginname string) (*Plugin, error) {
|
||||||
plugins, err := DetectPlugins()
|
plugins, err := DetectPlugins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop ot find the plugin name that matches
|
// loop ot find the plugin name that matches
|
||||||
for _, plugin := range plugins.PluginsDetected {
|
for _, plugin := range plugins.PluginsDetected {
|
||||||
if pluginname == plugin.FolderName {
|
if pluginname == plugin.FolderName {
|
||||||
return plugin, nil
|
return plugin, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New("plugin not detected")
|
return nil, errors.New("plugin not detected")
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunPlugin Executes plugins based on the plugin name provided
|
// RunPlugin Executes plugins based on the plugin name provided
|
||||||
func RunPlugin(pluginName string, IPAddresses []*ExecuteIP) (*Plugin, error) {
|
func RunPlugin(pluginName string, IPAddresses []*ExecuteIP) (*Plugin, error) {
|
||||||
plugins, err := DetectPlugins()
|
plugins, err := DetectPlugins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable to store struct information about the plugin
|
// Variable to store struct information about the plugin
|
||||||
var plugindetected *Plugin
|
var plugindetected *Plugin
|
||||||
for _, plugin := range plugins.PluginsDetected {
|
for _, plugin := range plugins.PluginsDetected {
|
||||||
if plugin.FolderName == pluginName {
|
if plugin.FolderName == pluginName {
|
||||||
plugindetected = plugin
|
plugindetected = plugin
|
||||||
plugindetected.Execute = IPAddresses
|
plugindetected.Execute = IPAddresses
|
||||||
// Get Execute plugin path from config file
|
// Get Execute plugin path from config file
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
plugindetected.path = config.PluginPath
|
plugindetected.path = config.PluginPath
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if plugindetected == nil {
|
if plugindetected == nil {
|
||||||
return nil, errors.New("Plugin not detected")
|
return nil, errors.New("Plugin not detected")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create copy of the plugin the tmp directory
|
// Create copy of the plugin the tmp directory
|
||||||
// To ensure we execute the plugin from there
|
// To ensure we execute the plugin from there
|
||||||
err = plugindetected.CopyToTmpPlugin()
|
err = plugindetected.CopyToTmpPlugin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executing the plugin
|
// Executing the plugin
|
||||||
err = plugindetected.ExecutePlugin()
|
err = plugindetected.ExecutePlugin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugindetected, nil
|
return plugindetected, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecutePlugin Function to execute plugins that are called
|
// ExecutePlugin Function to execute plugins that are called
|
||||||
func (p *Plugin) ExecutePlugin() error {
|
func (p *Plugin) ExecutePlugin() error {
|
||||||
|
|
||||||
// Run ip address to execute ansible inside
|
// Run ip address to execute ansible inside
|
||||||
for _, execute := range p.Execute {
|
for _, execute := range p.Execute {
|
||||||
// Modify ansible hosts before executing
|
// Modify ansible hosts before executing
|
||||||
err := execute.ModifyHost(p)
|
err := execute.ModifyHost(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// sets the ports to the plugin folder
|
// sets the ports to the plugin folder
|
||||||
err = p.AutoSetPorts(execute.ContainerID)
|
err = p.AutoSetPorts(execute.ContainerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = execute.RunAnsible(p)
|
err = execute.RunAnsible(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// If ran successfully then change success flag to true
|
// If ran successfully then change success flag to true
|
||||||
execute.Success = true
|
execute.Success = true
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunAnsible Executes based on credentials on the struct
|
// RunAnsible Executes based on credentials on the struct
|
||||||
func (e *ExecuteIP) RunAnsible(p *Plugin) error {
|
func (e *ExecuteIP) RunAnsible(p *Plugin) error {
|
||||||
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
|
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
|
||||||
User: "master",
|
User: "master",
|
||||||
}
|
}
|
||||||
|
|
||||||
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
|
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
|
||||||
Inventory: p.path + "/" + p.FolderName + "/hosts",
|
Inventory: p.path + "/" + p.FolderName + "/hosts",
|
||||||
ExtraVars: map[string]interface{}{"ansible_ssh_common_args": "-o StrictHostKeyChecking=no"},
|
ExtraVars: map[string]interface{}{"ansible_ssh_common_args": "-o StrictHostKeyChecking=no"},
|
||||||
}
|
}
|
||||||
|
|
||||||
ansiblePlaybookPrivilegeEscalationOptions := &options.AnsiblePrivilegeEscalationOptions{
|
ansiblePlaybookPrivilegeEscalationOptions := &options.AnsiblePrivilegeEscalationOptions{
|
||||||
Become: true,
|
Become: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
playbook := &playbook.AnsiblePlaybookCmd{
|
playbook := &playbook.AnsiblePlaybookCmd{
|
||||||
Playbooks: []string{p.path + "/" + p.FolderName + "/site.yml"},
|
Playbooks: []string{p.path + "/" + p.FolderName + "/site.yml"},
|
||||||
ConnectionOptions: ansiblePlaybookConnectionOptions,
|
ConnectionOptions: ansiblePlaybookConnectionOptions,
|
||||||
PrivilegeEscalationOptions: ansiblePlaybookPrivilegeEscalationOptions,
|
PrivilegeEscalationOptions: ansiblePlaybookPrivilegeEscalationOptions,
|
||||||
Options: ansiblePlaybookOptions,
|
Options: ansiblePlaybookOptions,
|
||||||
Exec: execute.NewDefaultExecute(
|
Exec: execute.NewDefaultExecute(
|
||||||
execute.WithTransformers(
|
execute.WithTransformers(
|
||||||
results.Prepend("success"),
|
results.Prepend("success"),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := playbook.Run(context.TODO())
|
err := playbook.Run(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModifyHost adds IP address , port no to the config file
|
// ModifyHost adds IP address , port no to the config file
|
||||||
func (e *ExecuteIP) ModifyHost(p *Plugin) error {
|
func (e *ExecuteIP) ModifyHost(p *Plugin) error {
|
||||||
host, err := ReadHost(p.path + "/" + p.FolderName + "/hosts")
|
host, err := ReadHost(p.path + "/" + p.FolderName + "/hosts")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Setting ansible host
|
// Setting ansible host
|
||||||
host.Main.Hosts.Host1.AnsibleHost = e.IPAddress
|
host.Main.Hosts.Host1.AnsibleHost = e.IPAddress
|
||||||
// Setting SSH port no
|
// Setting SSH port no
|
||||||
host.Main.Hosts.Host1.AnsiblePort, err = strconv.Atoi(e.SSHPortNo)
|
host.Main.Hosts.Host1.AnsiblePort, err = strconv.Atoi(e.SSHPortNo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Setting SSH user name
|
// Setting SSH user name
|
||||||
host.Main.Hosts.Host1.AnsibleUser = "master"
|
host.Main.Hosts.Host1.AnsibleUser = "master"
|
||||||
// Setting SSH password
|
// Setting SSH password
|
||||||
host.Main.Hosts.Host1.AnsibleSSHPass = "password"
|
host.Main.Hosts.Host1.AnsibleSSHPass = "password"
|
||||||
// Setting SSH sudo password
|
// Setting SSH sudo password
|
||||||
host.Main.Hosts.Host1.AnsibleSudoPass = "password"
|
host.Main.Hosts.Host1.AnsibleSudoPass = "password"
|
||||||
|
|
||||||
// write modified information to the hosts yaml file
|
// write modified information to the hosts yaml file
|
||||||
data, err := yaml.Marshal(host)
|
data, err := yaml.Marshal(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(p.path+"/"+p.FolderName+"/hosts", data, 0777)
|
err = ioutil.WriteFile(p.path+"/"+p.FolderName+"/hosts", data, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadHost Reads host file and adds
|
// ReadHost Reads host file and adds
|
||||||
func ReadHost(filename string) (*Host, error) {
|
func ReadHost(filename string) (*Host, error) {
|
||||||
buf, err := ioutil.ReadFile(filename)
|
buf, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &Host{}
|
c := &Host{}
|
||||||
err = yaml.Unmarshal(buf, c)
|
err = yaml.Unmarshal(buf, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("in file %q: %v", filename, err)
|
return nil, fmt.Errorf("in file %q: %v", filename, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunPluginContainer Runs ansible plugin based on plugin name and container name which
|
// RunPluginContainer Runs ansible plugin based on plugin name and container name which
|
||||||
// is derived from the tracked containers file
|
// is derived from the tracked containers file
|
||||||
// We pass in the group ID as a parameter because when we modify the ports taken
|
// We pass in the group ID as a parameter because when we modify the ports taken
|
||||||
func RunPluginContainer(PluginName string, ContainerID string) error {
|
func RunPluginContainer(PluginName string, ContainerID string) error {
|
||||||
// Gets container information based on container ID
|
// Gets container information based on container ID
|
||||||
ContainerInformation, err := client.GetContainerInformation(ContainerID)
|
ContainerInformation, err := client.GetContainerInformation(ContainerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting Up IP's for which the plugins will be executed
|
// Setting Up IP's for which the plugins will be executed
|
||||||
var ExecuteIPs []*ExecuteIP
|
var ExecuteIPs []*ExecuteIP
|
||||||
var ExecuteIP ExecuteIP
|
var ExecuteIP ExecuteIP
|
||||||
// Getting port no of SSH port
|
// Getting port no of SSH port
|
||||||
for _, port := range ContainerInformation.Container.Ports.PortSet {
|
for _, port := range ContainerInformation.Container.Ports.PortSet {
|
||||||
if port.PortName == "SSH" {
|
if port.PortName == "SSH" {
|
||||||
ExecuteIP.SSHPortNo = fmt.Sprint(port.ExternalPort)
|
ExecuteIP.SSHPortNo = fmt.Sprint(port.ExternalPort)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Handle error if SSH port is not provided
|
// Handle error if SSH port is not provided
|
||||||
if ExecuteIP.SSHPortNo == "" {
|
if ExecuteIP.SSHPortNo == "" {
|
||||||
return errors.New("SSH port not found")
|
return errors.New("SSH port not found")
|
||||||
}
|
}
|
||||||
// Split the port no from ip address since current the IP address
|
// Split the port no from ip address since current the IP address
|
||||||
// field is populated as
|
// field is populated as
|
||||||
// <ip address>:<port no>
|
// <ip address>:<port no>
|
||||||
|
|
||||||
host, _, err := net.SplitHostPort(ContainerInformation.IpAddress)
|
host, _, err := net.SplitHostPort(ContainerInformation.IpAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// IP address of the container
|
// IP address of the container
|
||||||
ExecuteIP.IPAddress = host
|
ExecuteIP.IPAddress = host
|
||||||
// Set container ID to ExecutorIP
|
// Set container ID to ExecutorIP
|
||||||
ExecuteIP.ContainerID = ContainerInformation.Id
|
ExecuteIP.ContainerID = ContainerInformation.Id
|
||||||
// Append IP to list of executor IP
|
// Append IP to list of executor IP
|
||||||
ExecuteIPs = append(ExecuteIPs, &ExecuteIP)
|
ExecuteIPs = append(ExecuteIPs, &ExecuteIP)
|
||||||
// Run plugin to execute plugin
|
// Run plugin to execute plugin
|
||||||
_, err = RunPlugin(PluginName, ExecuteIPs)
|
_, err = RunPlugin(PluginName, ExecuteIPs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckRunPlugin Checks if the ID belongs to the group or container
|
// CheckRunPlugin Checks if the ID belongs to the group or container
|
||||||
// calls the plugin function the appropriate amount of times
|
// calls the plugin function the appropriate amount of times
|
||||||
func CheckRunPlugin(PluginName string, ID string) error {
|
func CheckRunPlugin(PluginName string, ID string) error {
|
||||||
// Check if the ID belongs to the group or container ID
|
// Check if the ID belongs to the group or container ID
|
||||||
id, err := client.CheckID(ID)
|
id, err := client.CheckID(ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// When the ID belongs to a group
|
// When the ID belongs to a group
|
||||||
if id == "group" {
|
if id == "group" {
|
||||||
// gets the group information
|
// gets the group information
|
||||||
group, err := client.GetGroup(ID)
|
group, err := client.GetGroup(ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Iterate through each container information in the group
|
// Iterate through each container information in the group
|
||||||
// and run the plugin in each of them
|
// and run the plugin in each of them
|
||||||
for _, container := range group.TrackContainerList {
|
for _, container := range group.TrackContainerList {
|
||||||
// runs plugin for each container
|
// runs plugin for each container
|
||||||
err := RunPluginContainer(PluginName, container.Id)
|
err := RunPluginContainer(PluginName, container.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { // This means the following ID is a container ID
|
} else { // This means the following ID is a container ID
|
||||||
err := RunPluginContainer(PluginName, ID)
|
err := RunPluginContainer(PluginName, ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyToTmpPlugin This function would ensure that we create a copy of the
|
// CopyToTmpPlugin This function would ensure that we create a copy of the
|
||||||
@@ -364,101 +364,101 @@ func CheckRunPlugin(PluginName string, ID string) error {
|
|||||||
// from there. This due to the reason of automating port allocation
|
// from there. This due to the reason of automating port allocation
|
||||||
// when running plugins
|
// when running plugins
|
||||||
func (p *Plugin) CopyToTmpPlugin() error {
|
func (p *Plugin) CopyToTmpPlugin() error {
|
||||||
// generate rand to UUID this is debug the ansible file if needed
|
// generate rand to UUID this is debug the ansible file if needed
|
||||||
id := uuid.New()
|
id := uuid.New()
|
||||||
// copies the plugin to the tmp directory
|
// copies the plugin to the tmp directory
|
||||||
err := copy.Copy(p.path+"/"+p.FolderName, "/tmp/"+id.String()+"_"+p.FolderName)
|
err := copy.Copy(p.path+"/"+p.FolderName, "/tmp/"+id.String()+"_"+p.FolderName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the plugin execution to the tmp location
|
// Set the plugin execution to the tmp location
|
||||||
p.path = "/tmp"
|
p.path = "/tmp"
|
||||||
p.FolderName = id.String() + "_" + p.FolderName
|
p.FolderName = id.String() + "_" + p.FolderName
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AutoSetPorts Automatically maps free ports to site.yml file
|
// AutoSetPorts Automatically maps free ports to site.yml file
|
||||||
func (p *Plugin) AutoSetPorts(containerID string) error {
|
func (p *Plugin) AutoSetPorts(containerID string) error {
|
||||||
container, err := client.GetContainerInformation(containerID)
|
container, err := client.GetContainerInformation(containerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// variable that would have a list of ports
|
// variable that would have a list of ports
|
||||||
// to be allocated to the plugin system
|
// to be allocated to the plugin system
|
||||||
var ports []int
|
var ports []int
|
||||||
// Counted that increments when a port is taken
|
// Counted that increments when a port is taken
|
||||||
PortTaken := 0
|
PortTaken := 0
|
||||||
// setting all external ports available in an array
|
// setting all external ports available in an array
|
||||||
for i, port := range container.Container.Ports.PortSet {
|
for i, port := range container.Container.Ports.PortSet {
|
||||||
if port.IsUsed == false {
|
if port.IsUsed == false {
|
||||||
// Ensuring we break outside the loop once the ports
|
// Ensuring we break outside the loop once the ports
|
||||||
// are set.
|
// are set.
|
||||||
if PortTaken >= p.NumOfPorts {
|
if PortTaken >= p.NumOfPorts {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Setting the following port flag to true
|
// Setting the following port flag to true
|
||||||
container.Container.Ports.PortSet[i].IsUsed = true
|
container.Container.Ports.PortSet[i].IsUsed = true
|
||||||
// Incrementing the variable PortTaken
|
// Incrementing the variable PortTaken
|
||||||
PortTaken++
|
PortTaken++
|
||||||
// Maps to internal since
|
// Maps to internal since
|
||||||
// Inside the machine
|
// Inside the machine
|
||||||
// internal port -> (maps) same internal port
|
// internal port -> (maps) same internal port
|
||||||
// TURN (i.e FRP) based approach (internal port -> maps to different external port)
|
// TURN (i.e FRP) based approach (internal port -> maps to different external port)
|
||||||
ports = append(ports, port.InternalPort)
|
ports = append(ports, port.InternalPort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parses the site.yml file in the tmp directory
|
// parses the site.yml file in the tmp directory
|
||||||
t, err := template.ParseFiles(p.path + "/" + p.FolderName + "/site.yml")
|
t, err := template.ParseFiles(p.path + "/" + p.FolderName + "/site.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// opens the output file
|
// opens the output file
|
||||||
f, err := os.Create(p.path + "/" + p.FolderName + "/site.yml")
|
f, err := os.Create(p.path + "/" + p.FolderName + "/site.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// sends the ports to the site.yml file to populate them
|
// sends the ports to the site.yml file to populate them
|
||||||
err = t.Execute(f, ports)
|
err = t.Execute(f, ports)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Once the following is done set port to taken
|
// Once the following is done set port to taken
|
||||||
// n tracked container list
|
// n tracked container list
|
||||||
err = container.ModifyContainerInformation()
|
err = container.ModifyContainerInformation()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Once the following is done set port to taken
|
// Once the following is done set port to taken
|
||||||
// I(Groups)
|
// I(Groups)
|
||||||
err = container.ModifyContainerGroups()
|
err = container.ModifyContainerGroups()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumPorts Gets the Number the ports the
|
// NumPorts Gets the Number the ports the
|
||||||
// plugin requires
|
// plugin requires
|
||||||
func (p *Plugin) NumPorts() error {
|
func (p *Plugin) NumPorts() error {
|
||||||
jsonFile, err := os.Open(p.path + "/ports.json")
|
jsonFile, err := os.Open(p.path + "/ports.json")
|
||||||
// if we os.Open returns an error then handle it
|
// if we os.Open returns an error then handle it
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// defer the closing of our jsonFile so that we can parse it later on
|
// defer the closing of our jsonFile so that we can parse it later on
|
||||||
defer jsonFile.Close()
|
defer jsonFile.Close()
|
||||||
|
|
||||||
// read our opened xmlFile as a byte array.
|
// read our opened xmlFile as a byte array.
|
||||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||||
|
|
||||||
// we unmarshal our byteArray which contains our
|
// we unmarshal our byteArray which contains our
|
||||||
// jsonFile's content into 'users' which we defined above
|
// jsonFile's content into 'users' which we defined above
|
||||||
json.Unmarshal(byteValue, &p)
|
json.Unmarshal(byteValue, &p)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,257 +1,257 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client"
|
"github.com/Akilan1999/p2p-rendering-computation/client"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Test if the dummy plugin added is detected
|
// Test if the dummy plugin added is detected
|
||||||
func TestDetectPlugins(t *testing.T) {
|
func TestDetectPlugins(t *testing.T) {
|
||||||
_, err := DetectPlugins()
|
_, err := DetectPlugins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ensures that the ansible are executed inside local containers
|
// Test ensures that the ansible are executed inside local containers
|
||||||
func TestRunPlugin(t *testing.T) {
|
func TestRunPlugin(t *testing.T) {
|
||||||
var testips []*ExecuteIP
|
var testips []*ExecuteIP
|
||||||
var testip1, testip2 ExecuteIP
|
var testip1, testip2 ExecuteIP
|
||||||
|
|
||||||
// Create docker container and get SSH port
|
// Create docker container and get SSH port
|
||||||
container1, err := docker.BuildRunContainer(0, "false", "")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Test IP 1 configuration
|
//Test IP 1 configuration
|
||||||
testip1.IPAddress = "0.0.0.0"
|
testip1.IPAddress = "0.0.0.0"
|
||||||
testip1.SSHPortNo = strconv.Itoa(container1.Ports.PortSet[0].ExternalPort)
|
testip1.SSHPortNo = strconv.Itoa(container1.Ports.PortSet[0].ExternalPort)
|
||||||
|
|
||||||
// Create docker container and get SSH port
|
// Create docker container and get SSH port
|
||||||
container2, err := docker.BuildRunContainer(0, "false", "")
|
container2, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
//Test IP 2 configuration
|
//Test IP 2 configuration
|
||||||
testip2.IPAddress = "0.0.0.0"
|
testip2.IPAddress = "0.0.0.0"
|
||||||
testip2.SSHPortNo = strconv.Itoa(container2.Ports.PortSet[0].ExternalPort)
|
testip2.SSHPortNo = strconv.Itoa(container2.Ports.PortSet[0].ExternalPort)
|
||||||
|
|
||||||
testips = append(testips, &testip1)
|
testips = append(testips, &testip1)
|
||||||
testips = append(testips, &testip2)
|
testips = append(testips, &testip2)
|
||||||
|
|
||||||
_, err = RunPlugin("TestAnsible", testips)
|
_, err = RunPlugin("TestAnsible", testips)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing container1 after Ansible is executed
|
// Removing container1 after Ansible is executed
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = docker.StopAndRemoveContainer(container2.ID)
|
err = docker.StopAndRemoveContainer(container2.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test to ensure that the ansible host file is modified to
|
// Test to ensure that the ansible host file is modified to
|
||||||
// the appropriate IP
|
// the appropriate IP
|
||||||
func TestExecuteIP_ModifyHost(t *testing.T) {
|
func TestExecuteIP_ModifyHost(t *testing.T) {
|
||||||
var plugin Plugin
|
var plugin Plugin
|
||||||
var testip ExecuteIP
|
var testip ExecuteIP
|
||||||
|
|
||||||
// Get plugin path from config file
|
// Get plugin path from config file
|
||||||
Config, err := config.ConfigInit()
|
Config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
//Set plugin name
|
//Set plugin name
|
||||||
plugin.FolderName = "TestAnsible"
|
plugin.FolderName = "TestAnsible"
|
||||||
plugin.path = Config.PluginPath
|
plugin.path = Config.PluginPath
|
||||||
|
|
||||||
//Test IP 1 configuration
|
//Test IP 1 configuration
|
||||||
testip.IPAddress = "0.0.0.0"
|
testip.IPAddress = "0.0.0.0"
|
||||||
testip.SSHPortNo = "41289"
|
testip.SSHPortNo = "41289"
|
||||||
|
|
||||||
err = testip.ModifyHost(&plugin)
|
err = testip.ModifyHost(&plugin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test to ensure the cli function runs as intended and executes
|
// Test to ensure the cli function runs as intended and executes
|
||||||
// the test ansible script
|
// the test ansible script
|
||||||
func TestRunPluginContainer(t *testing.T) {
|
func TestRunPluginContainer(t *testing.T) {
|
||||||
// Create docker container and get SSH port
|
// Create docker container and get SSH port
|
||||||
container1, err := docker.BuildRunContainer(0, "false", "")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensuring created container is the added to the tracked list
|
// Ensuring created container is the added to the tracked list
|
||||||
err = client.AddTrackContainer(container1, "0.0.0.0")
|
err = client.AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Running test Ansible script
|
// Running test Ansible script
|
||||||
err = RunPluginContainer("TestAnsible", container1.ID)
|
err = RunPluginContainer("TestAnsible", container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes container information from the tracker IP addresses
|
// Removes container information from the tracker IP addresses
|
||||||
err = client.RemoveTrackedContainer(container1.ID)
|
err = client.RemoveTrackedContainer(container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing container1 after Ansible is executed
|
// Removing container1 after Ansible is executed
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the function can plugin can run with
|
// Testing the function can plugin can run with
|
||||||
// group ID and container ID
|
// group ID and container ID
|
||||||
func TestCheckRunPlugin(t *testing.T) {
|
func TestCheckRunPlugin(t *testing.T) {
|
||||||
// Create docker container and get SSH port
|
// Create docker container and get SSH port
|
||||||
container1, err := docker.BuildRunContainer(0, "false", "")
|
container1, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
// Create docker container and get SSH port
|
// Create docker container and get SSH port
|
||||||
container2, err := docker.BuildRunContainer(0, "false", "")
|
container2, err := docker.BuildRunContainer(0, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensuring created container1 is the added to the tracked list
|
// Ensuring created container1 is the added to the tracked list
|
||||||
err = client.AddTrackContainer(container1, "0.0.0.0")
|
err = client.AddTrackContainer(container1, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
// Ensuring created container2 is the added to the tracked list
|
// Ensuring created container2 is the added to the tracked list
|
||||||
err = client.AddTrackContainer(container2, "0.0.0.0")
|
err = client.AddTrackContainer(container2, "0.0.0.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create group to add created containers
|
// Create group to add created containers
|
||||||
group, err := client.CreateGroup()
|
group, err := client.CreateGroup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add container 1 to the group
|
// Add container 1 to the group
|
||||||
_, err = client.AddContainerToGroup(container1.ID, group.ID)
|
_, err = client.AddContainerToGroup(container1.ID, group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add container 2 to the group
|
// Add container 2 to the group
|
||||||
_, err = client.AddContainerToGroup(container2.ID, group.ID)
|
_, err = client.AddContainerToGroup(container2.ID, group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------- Main test cases -------------------------------
|
// -------------------------- Main test cases -------------------------------
|
||||||
|
|
||||||
// Checking function against container ID
|
// Checking function against container ID
|
||||||
err = CheckRunPlugin("TestAnsible", container1.ID)
|
err = CheckRunPlugin("TestAnsible", container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checking function against group ID
|
// Checking function against group ID
|
||||||
err = CheckRunPlugin("TestAnsible", group.ID)
|
err = CheckRunPlugin("TestAnsible", group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// Remove created group
|
// Remove created group
|
||||||
err = client.RemoveGroup(group.ID)
|
err = client.RemoveGroup(group.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes container1 information from the tracker IP addresses
|
// Removes container1 information from the tracker IP addresses
|
||||||
err = client.RemoveTrackedContainer(container1.ID)
|
err = client.RemoveTrackedContainer(container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing container1 after Ansible is executed
|
// Removing container1 after Ansible is executed
|
||||||
err = docker.StopAndRemoveContainer(container1.ID)
|
err = docker.StopAndRemoveContainer(container1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes container2 information from the tracker IP addresses
|
// Removes container2 information from the tracker IP addresses
|
||||||
err = client.RemoveTrackedContainer(container2.ID)
|
err = client.RemoveTrackedContainer(container2.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing container2 after Ansible is executed
|
// Removing container2 after Ansible is executed
|
||||||
err = docker.StopAndRemoveContainer(container2.ID)
|
err = docker.StopAndRemoveContainer(container2.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownloadPlugin(t *testing.T) {
|
func TestDownloadPlugin(t *testing.T) {
|
||||||
err := DownloadPlugin("https://github.com/Akilan1999/laplace/")
|
err := DownloadPlugin("https://github.com/Akilan1999/laplace/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple test case implemented to the test if
|
// Simple test case implemented to the test if
|
||||||
// the port no can be extracted from the IP address.
|
// the port no can be extracted from the IP address.
|
||||||
func TestParseIP(t *testing.T) {
|
func TestParseIP(t *testing.T) {
|
||||||
host, port, err := net.SplitHostPort("12.34.23.13:5432")
|
host, port, err := net.SplitHostPort("12.34.23.13:5432")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %v\n", err)
|
fmt.Printf("Error: %v\n", err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Host: %s\nPort: %s\n", host, port)
|
fmt.Printf("Host: %s\nPort: %s\n", host, port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
@@ -22,21 +22,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DockerVM struct {
|
type DockerVM struct {
|
||||||
SSHUsername string `json:"SSHUsername"`
|
SSHUsername string `json:"SSHUsername"`
|
||||||
SSHPassword string `json:"SSHPassword"`
|
SSHPassword string `json:"SSHPassword"`
|
||||||
ID string `json:"ID"`
|
ID string `json:"ID"`
|
||||||
TagName string `json:"TagName"`
|
TagName string `json:"TagName"`
|
||||||
ImagePath string `json:"ImagePath"`
|
ImagePath string `json:"ImagePath"`
|
||||||
Ports Ports `json:"Ports"`
|
Ports Ports `json:"Ports"`
|
||||||
GPU string `json:"GPU"`
|
GPU string `json:"GPU"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DockerContainers struct {
|
type DockerContainers struct {
|
||||||
DockerContainer []DockerContainer `json:"DockerContainer"`
|
DockerContainer []DockerContainer `json:"DockerContainer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DockerContainer struct {
|
type DockerContainer struct {
|
||||||
ContainerName string `json:"DockerContainerName"`
|
ContainerName string `json:"DockerContainerName"`
|
||||||
ContainerDescription string `json:"ContainerDescription"`
|
ContainerDescription string `json:"ContainerDescription"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,11 +44,11 @@ type Ports struct {
|
|||||||
PortSet []Port `json:"Port"`
|
PortSet []Port `json:"Port"`
|
||||||
}
|
}
|
||||||
type Port struct {
|
type Port struct {
|
||||||
PortName string `json:"PortName"`
|
PortName string `json:"PortName"`
|
||||||
InternalPort int `json:"InternalPort"`
|
InternalPort int `json:"InternalPort"`
|
||||||
Type string `json:"Type"`
|
Type string `json:"Type"`
|
||||||
ExternalPort int `json:"ExternalPort"`
|
ExternalPort int `json:"ExternalPort"`
|
||||||
IsUsed bool `json:"IsUsed"`
|
IsUsed bool `json:"IsUsed"`
|
||||||
Description string `json:"Description"`
|
Description string `json:"Description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ var dockerRegistryUserID = ""
|
|||||||
|
|
||||||
// BuildRunContainer Function is incharge to invoke building and running contianer and also allocating external
|
// BuildRunContainer Function is incharge to invoke building and running contianer and also allocating external
|
||||||
// ports
|
// ports
|
||||||
func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerVM,error) {
|
func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerVM, error) {
|
||||||
//Docker Struct Variable
|
//Docker Struct Variable
|
||||||
var RespDocker *DockerVM = new(DockerVM)
|
var RespDocker *DockerVM = new(DockerVM)
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
|
|||||||
// Get Path from config
|
// Get Path from config
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
RespDocker.ImagePath = config.DefaultDockerFile
|
RespDocker.ImagePath = config.DefaultDockerFile
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
|
|||||||
if ContainerName != "" && ContainerName != "docker-ubuntu-sshd" {
|
if ContainerName != "" && ContainerName != "docker-ubuntu-sshd" {
|
||||||
Containers, err := ViewAllContainers()
|
Containers, err := ViewAllContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, dockerContainer := range Containers.DockerContainer {
|
for _, dockerContainer := range Containers.DockerContainer {
|
||||||
@@ -106,7 +106,7 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if RespDocker.ImagePath == config.DefaultDockerFile {
|
if RespDocker.ImagePath == config.DefaultDockerFile {
|
||||||
return nil, errors.New("Container " + ContainerName + " does not exist in the server")
|
return nil, errors.New("Container " + ContainerName + " does not exist in the server")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
|
|||||||
// Creates number of ports
|
// Creates number of ports
|
||||||
OpenPorts, err := freeport.GetFreePorts(count)
|
OpenPorts, err := freeport.GetFreePorts(count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Allocate external ports to ports available in the ports.json file
|
// Allocate external ports to ports available in the ports.json file
|
||||||
for i := range PortsInformation.PortSet {
|
for i := range PortsInformation.PortSet {
|
||||||
@@ -136,8 +136,8 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
|
|||||||
var TempPort Port
|
var TempPort Port
|
||||||
TempPort.PortName = "AutoGen Port"
|
TempPort.PortName = "AutoGen Port"
|
||||||
TempPort.Type = "tcp"
|
TempPort.Type = "tcp"
|
||||||
TempPort.InternalPort = OpenPorts[portFileLength + i]
|
TempPort.InternalPort = OpenPorts[portFileLength+i]
|
||||||
TempPort.ExternalPort = OpenPorts[portFileLength + i]
|
TempPort.ExternalPort = OpenPorts[portFileLength+i]
|
||||||
TempPort.Description = "Auto generated TCP port"
|
TempPort.Description = "Auto generated TCP port"
|
||||||
TempPort.IsUsed = false
|
TempPort.IsUsed = false
|
||||||
//Append temp port to port information
|
//Append temp port to port information
|
||||||
@@ -149,29 +149,28 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
|
|||||||
// Gets docker information from env variables
|
// Gets docker information from env variables
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Builds docker image
|
// Builds docker image
|
||||||
err = RespDocker.imageBuild(cli)
|
err = RespDocker.imageBuild(cli)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runs docker contianer
|
// Runs docker contianer
|
||||||
err = RespDocker.runContainer(cli)
|
err = RespDocker.runContainer(cli)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return RespDocker, nil
|
||||||
return RespDocker,nil
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Builds docker image (TODO: relative path for Dockerfile deploy)
|
// Builds docker image (TODO: relative path for Dockerfile deploy)
|
||||||
func (d *DockerVM)imageBuild(dockerClient *client.Client) error {
|
func (d *DockerVM) imageBuild(dockerClient *client.Client) error {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*2000)
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*2000)
|
||||||
//defer cancel()
|
//defer cancel()
|
||||||
|
|
||||||
@@ -203,16 +202,15 @@ func (d *DockerVM)imageBuild(dockerClient *client.Client) error {
|
|||||||
// Starts container and assigns port numbers
|
// Starts container and assigns port numbers
|
||||||
// Sample Docker run Command
|
// Sample Docker run Command
|
||||||
// docker run -d=true --name=Test123 --restart=always --gpus all
|
// docker run -d=true --name=Test123 --restart=always --gpus all
|
||||||
//-p 3443:6901 -p 3453:22 -p 3434:3434 -p 3245:3245 -v=/opt/data:/data
|
// -p 3443:6901 -p 3453:22 -p 3434:3434 -p 3245:3245 -v=/opt/data:/data
|
||||||
//p2p-ubuntu /start > /dev/null
|
// p2p-ubuntu /start > /dev/null
|
||||||
func (d *DockerVM)runContainer(dockerClient *client.Client) error{
|
func (d *DockerVM) runContainer(dockerClient *client.Client) error {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*2000)
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*2000)
|
||||||
|
|
||||||
|
|
||||||
// The first mode runs using the Docker Api. As the API supports using
|
// The first mode runs using the Docker Api. As the API supports using
|
||||||
// CPU and uses a shell script for GPU call because till this point of
|
// CPU and uses a shell script for GPU call because till this point of
|
||||||
// implementation docker api does not support the flag "--gpu all"
|
// implementation docker api does not support the flag "--gpu all"
|
||||||
if d.GPU != "true" {
|
if d.GPU != "true" {
|
||||||
//Exposed ports for docker config file
|
//Exposed ports for docker config file
|
||||||
var ExposedPort nat.PortSet
|
var ExposedPort nat.PortSet
|
||||||
|
|
||||||
@@ -239,7 +237,7 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{
|
|||||||
|
|
||||||
for i := range d.Ports.PortSet {
|
for i := range d.Ports.PortSet {
|
||||||
// Parameters "tcp or udp", external port
|
// Parameters "tcp or udp", external port
|
||||||
Port, err := nat.NewPort(d.Ports.PortSet[i].Type,fmt.Sprint(d.Ports.PortSet[i].InternalPort))
|
Port, err := nat.NewPort(d.Ports.PortSet[i].Type, fmt.Sprint(d.Ports.PortSet[i].InternalPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -249,7 +247,7 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{
|
|||||||
|
|
||||||
PortForwarding[Port] = []nat.PortBinding{
|
PortForwarding[Port] = []nat.PortBinding{
|
||||||
{
|
{
|
||||||
HostIP: "0.0.0.0",
|
HostIP: "0.0.0.0",
|
||||||
HostPort: fmt.Sprint(d.Ports.PortSet[i].ExternalPort),
|
HostPort: fmt.Sprint(d.Ports.PortSet[i].ExternalPort),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -286,11 +284,11 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{
|
|||||||
d.ID = id
|
d.ID = id
|
||||||
|
|
||||||
var cmd bytes.Buffer
|
var cmd bytes.Buffer
|
||||||
cmd.WriteString("docker run -d=true --name="+ id +" --restart=always --gpus all ")
|
cmd.WriteString("docker run -d=true --name=" + id + " --restart=always --gpus all ")
|
||||||
for i := range d.Ports.PortSet {
|
for i := range d.Ports.PortSet {
|
||||||
cmd.WriteString("-p " + fmt.Sprint(d.Ports.PortSet[i].ExternalPort) + ":" + fmt.Sprint(d.Ports.PortSet[i].InternalPort) + " ")
|
cmd.WriteString("-p " + fmt.Sprint(d.Ports.PortSet[i].ExternalPort) + ":" + fmt.Sprint(d.Ports.PortSet[i].InternalPort) + " ")
|
||||||
}
|
}
|
||||||
cmd.WriteString("-v=/opt/data:/data "+ d.TagName +" /start > /dev/null")
|
cmd.WriteString("-v=/opt/data:/data " + d.TagName + " /start > /dev/null")
|
||||||
//"-v=/opt/data:/data p2p-ubuntu /start > /dev/null"
|
//"-v=/opt/data:/data p2p-ubuntu /start > /dev/null"
|
||||||
cmdStr := cmd.String()
|
cmdStr := cmd.String()
|
||||||
_, err := exec.Command("/bin/sh", "-c", cmdStr).Output()
|
_, err := exec.Command("/bin/sh", "-c", cmdStr).Output()
|
||||||
@@ -330,11 +328,11 @@ func StopAndRemoveContainer(containername string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ViewAllContainers returns all containers runnable and which can be built
|
// ViewAllContainers returns all containers runnable and which can be built
|
||||||
func ViewAllContainers() (*DockerContainers, error){
|
func ViewAllContainers() (*DockerContainers, error) {
|
||||||
// Traverse the deploy path as per given in the config file
|
// Traverse the deploy path as per given in the config file
|
||||||
config, err := config.ConfigInit()
|
config, err := config.ConfigInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
folders, err := ioutil.ReadDir(config.DockerContainers)
|
folders, err := ioutil.ReadDir(config.DockerContainers)
|
||||||
@@ -348,11 +346,11 @@ func ViewAllContainers() (*DockerContainers, error){
|
|||||||
for _, f := range folders {
|
for _, f := range folders {
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
//Declare variable DockerContainer of type struct
|
//Declare variable DockerContainer of type struct
|
||||||
var Container DockerContainer
|
var Container DockerContainer
|
||||||
|
|
||||||
// Setting container name to deploy name
|
// Setting container name to deploy name
|
||||||
Container.ContainerName = f.Name()
|
Container.ContainerName = f.Name()
|
||||||
// Getting Description from file description.txt
|
// Getting Description from file description.txt
|
||||||
Description, err := ioutil.ReadFile(config.DockerContainers + "/" + Container.ContainerName + "/description.txt")
|
Description, err := ioutil.ReadFile(config.DockerContainers + "/" + Container.ContainerName + "/description.txt")
|
||||||
// if we os.Open returns an error then handle it
|
// if we os.Open returns an error then handle it
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -366,7 +364,7 @@ func ViewAllContainers() (*DockerContainers, error){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Containers,nil
|
return Containers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func print(rd io.Reader) error {
|
func print(rd io.Reader) error {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"net"
|
"net"
|
||||||
"net/rpc"
|
"net/rpc"
|
||||||
)
|
)
|
||||||
@@ -18,8 +18,8 @@ type Docker struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Starts container using RPC calls
|
// Starts container using RPC calls
|
||||||
func (l *Listener) StartContainer( reply *Docker) error {
|
func (l *Listener) StartContainer(reply *Docker) error {
|
||||||
vm, err := docker.BuildRunContainer(3,"false","")
|
vm, err := docker.BuildRunContainer(3, "false", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -28,8 +28,6 @@ func (l *Listener) StartContainer( reply *Docker) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func Rpc() {
|
func Rpc() {
|
||||||
rpcServer, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+port)
|
rpcServer, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -43,4 +41,3 @@ func Rpc() {
|
|||||||
rpc.Register(listener)
|
rpc.Register(listener)
|
||||||
rpc.Accept(inbound)
|
rpc.Accept(inbound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package server
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/client/clientIPTable"
|
"github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p/frp"
|
"github.com/Akilan1999/p2p-rendering-computation/p2p/frp"
|
||||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
|
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|||||||
Reference in New Issue
Block a user