added feature to detect if a port is used by a plugin or not

This commit is contained in:
2021-10-24 23:36:23 +04:00
parent 56be275759
commit 0144e59d71
4 changed files with 40 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ plugin
|___<plugin name> |___<plugin name>
|___ site.yml |___ site.yml
|___ hosts |___ hosts
|___ ports.json
|___ description.txt |___ description.txt
. .
. .
@@ -70,6 +71,16 @@ main:
ansible_sudo_pass: password ansible_sudo_pass: password
``` ```
## Ports.json
The ```ports.json``` file is intended to mention the number of ports required
by the plugin.
```
{
"NumOfPorts": <number of ports>
}
```
## Description file ## Description file
This is a simple text file used to describe what the module does. This is a simple text file used to describe what the module does.
When the client is looking at various commands via the ClI. When the client is looking at various commands via the ClI.

View File

@@ -178,6 +178,8 @@ Format of the ports.json file
"PortName": "<Port name>", "PortName": "<Port name>",
"InternalPort": <internal port>, "InternalPort": <internal port>,
"Type": "<tcp/udp>", "Type": "<tcp/udp>",
"ExternalPort": <external port>,
"IsUsed": "<boolean value (i.e true or false)>",
"Description": "<description about the port>" "Description": "<description about the port>"
}, ... n }, ... n
] ]

View File

@@ -281,6 +281,8 @@ func (grp *Groups)RemoveContainerGroups(Container *TrackContainer) error {
return nil return nil
} }
// ModifyContainerGroups Modifies container information is all groups
// available
func (TC *TrackContainer)ModifyContainerGroups() error { func (TC *TrackContainer)ModifyContainerGroups() error {
group, err := ReadGroup() group, err := ReadGroup()
if err != nil { if err != nil {

View File

@@ -262,6 +262,7 @@ func ReadHost(filename string) (*Host, error) {
// 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
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)
@@ -361,17 +362,24 @@ func (p *Plugin)AutoSetPorts(containerID string) error {
// 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
PortTaken := 0
// setting all external ports available in an array // setting all external ports available in an array
for _, port := range container.Container.Ports.PortSet { for i, port := range container.Container.Ports.PortSet {
if port.IsUsed == false { if port.IsUsed == false {
ports = append(ports, port.ExternalPort) // Ensuring we break outside the loop once the ports
// are set.
if PortTaken >= p.NumOfPorts {
break
}
// Setting the following port flag to true // Setting the following port flag to true
port.IsUsed = true container.Container.Ports.PortSet[i].IsUsed = true
// Incrementing the variable PortTaken
PortTaken++
ports = append(ports, port.ExternalPort)
} }
} }
client
// 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 {
@@ -387,6 +395,18 @@ func (p *Plugin)AutoSetPorts(containerID string) error {
if err != nil { if err != nil {
return err return err
} }
// Once the following is done set port to taken
// n tracked container list
err = container.ModifyContainerInformation()
if err != nil {
return err
}
// Once the following is done set port to taken
// I(Groups)
err = container.ModifyContainerGroups()
if err != nil {
return err
}
return nil return nil
} }