Merge pull request #64 from Akilan1999/executeGroups
Execute groups when running plugins
This commit is contained in:
@@ -144,7 +144,7 @@ p2prc --tc
|
||||
|
||||
### Running plugin
|
||||
```
|
||||
p2prc --plugin <plugin name> --id <container id>
|
||||
p2prc --plugin <plugin name> --id <container id or group id>
|
||||
```
|
||||
|
||||
### Create group
|
||||
@@ -165,7 +165,7 @@ p2prc --group <group id>
|
||||
```
|
||||
### Delete container from group
|
||||
```
|
||||
p2prc --rmcgroup --group <group id> --id <contianer id>
|
||||
p2prc --rmcgroup --group <group id> --id <container id>
|
||||
```
|
||||
### Delete entire group
|
||||
```
|
||||
|
||||
@@ -163,3 +163,14 @@ func GetContainerInformation(ID string) (*TrackContainer, error) {
|
||||
}
|
||||
return nil, errors.New("Container not found. ")
|
||||
}
|
||||
|
||||
// CheckID Checks if the ID belongs to a group or a single container
|
||||
func CheckID(ID string) (string,error) {
|
||||
// For group checks if the 1st characters is "grp"
|
||||
if ID[0:3] == "grp" {
|
||||
return "group", nil
|
||||
} else {
|
||||
return "container", nil
|
||||
}
|
||||
return "",nil
|
||||
}
|
||||
@@ -102,3 +102,19 @@ func TestRemoveTrackedContainer(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// Test function that checks if the ID belongs to
|
||||
// a group or container running
|
||||
func TestCheckID(t *testing.T) {
|
||||
id := "grp123"
|
||||
checkID, err := CheckID(id)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
if checkID == "group" {
|
||||
fmt.Println("pass")
|
||||
} else {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,9 +167,9 @@ var CliAction = func(ctx *cli.Context) error {
|
||||
//Executing plugin when the plugin flag is called
|
||||
// --plugin
|
||||
if ExecutePlugin != "" {
|
||||
// The execute plugin requires the container ID provided when being executed
|
||||
// To execute plugin requires the container ID or group ID provided when being executed
|
||||
if ID != "" {
|
||||
err := plugin.RunPluginCli(ExecutePlugin, ID)
|
||||
err := plugin.CheckRunPlugin(ExecutePlugin, ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
|
||||
@@ -232,9 +232,9 @@ func ReadHost(filename string) (*Host, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// RunPluginCli Runs ansible plugin based on plugin name and contianer name which
|
||||
// RunPluginContainer Runs ansible plugin based on plugin name and contianer name which
|
||||
// is derived from the tracked containers file
|
||||
func RunPluginCli(PluginName string, ContainerID string) error {
|
||||
func RunPluginContainer(PluginName string, ContainerID string) error {
|
||||
// Gets container information based on container ID
|
||||
ContainerInformation, err := client.GetContainerInformation(ContainerID)
|
||||
if err != nil {
|
||||
@@ -267,3 +267,37 @@ func RunPluginCli(PluginName string, ContainerID string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckRunPlugin Checks if the ID belongs to the group or container
|
||||
// calls the plugin function the appropriate amount of times
|
||||
func CheckRunPlugin(PluginName string, ID string) error {
|
||||
// Check if the ID belongs to the group or container ID
|
||||
id, err := client.CheckID(ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// When the ID belongs to a group
|
||||
if id == "group" {
|
||||
// gets the group information
|
||||
group, err := client.GetGroup(ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Iterate through each container information in the group
|
||||
// and run the plugin in each of them
|
||||
for _, container := range group.TrackContainerList {
|
||||
// runs plugin for each container
|
||||
err := RunPluginContainer(PluginName, container.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else { // This means the following ID is a container ID
|
||||
err := RunPluginContainer(PluginName, ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ func TestExecuteIP_ModifyHost(t *testing.T) {
|
||||
|
||||
// Test to ensure the cli function runs as intended and executes
|
||||
// the test ansible script
|
||||
func TestRunPluginCli(t *testing.T) {
|
||||
func TestRunPluginContainer(t *testing.T) {
|
||||
// Create docker container and get SSH port
|
||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
||||
if err != nil {
|
||||
@@ -110,7 +110,7 @@ func TestRunPluginCli(t *testing.T) {
|
||||
}
|
||||
|
||||
// Running test Ansible script
|
||||
err = RunPluginCli("TestAnsible", container1.ID)
|
||||
err = RunPluginContainer("TestAnsible", container1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
@@ -130,3 +130,109 @@ func TestRunPluginCli(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// Testing the function can plugin can run with
|
||||
// group ID and container ID
|
||||
func TestCheckRunPlugin(t *testing.T) {
|
||||
// Create docker container and get SSH port
|
||||
container1 ,err := docker.BuildRunContainer(0,"false","")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
// Create docker container and get SSH port
|
||||
container2 ,err := docker.BuildRunContainer(0,"false","")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Ensuring created container1 is the added to the tracked list
|
||||
err = client.AddTrackContainer(container1, "0.0.0.0")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
// Ensuring created container2 is the added to the tracked list
|
||||
err = client.AddTrackContainer(container2, "0.0.0.0")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Create group to add created containers
|
||||
group, err := client.CreateGroup()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
|
||||
// Add container 1 to the group
|
||||
_, err = client.AddContainerToGroup(container1.ID, group.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Add container 2 to the group
|
||||
_, err = client.AddContainerToGroup(container2.ID, group.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// -------------------------- Main test cases -------------------------------
|
||||
|
||||
// Checking function against container ID
|
||||
err = CheckRunPlugin("TestAnsible", container1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Checking function against group ID
|
||||
err = CheckRunPlugin("TestAnsible", group.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Remove created group
|
||||
err = client.RemoveGroup(group.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Removes container1 information from the tracker IP addresses
|
||||
err = client.RemoveTrackedContainer(container1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Removing container1 after Ansible is executed
|
||||
err = docker.StopAndRemoveContainer(container1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Removes container2 information from the tracker IP addresses
|
||||
err = client.RemoveTrackedContainer(container2.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Removing container2 after Ansible is executed
|
||||
err = docker.StopAndRemoveContainer(container2.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user