added plugin to execute ansible code
This commit is contained in:
12
plugin/TestAnsible/hosts
Normal file
12
plugin/TestAnsible/hosts
Normal file
@@ -0,0 +1,12 @@
|
||||
---
|
||||
all:
|
||||
vars:
|
||||
ansible_python_interpreter: /usr/bin/python3
|
||||
main:
|
||||
hosts:
|
||||
host1:
|
||||
ansible_host: 0.0.0.0 # Replace with your remote IP
|
||||
ansible_port: 44003 # Replace with your remote SSH port
|
||||
ansible_user: master # Replace wtih your username
|
||||
ansible_ssh_pass: password
|
||||
ansible_sudo_pass: password
|
||||
@@ -1 +1 @@
|
||||
cp -r TestAnsible/ deploy/TestAnsbile
|
||||
cp -r TestAnsible/ deploy/
|
||||
|
||||
@@ -3,8 +3,11 @@ package plugin
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
|
||||
"github.com/apenella/go-ansible/pkg/execute"
|
||||
"github.com/apenella/go-ansible/pkg/options"
|
||||
@@ -31,6 +34,27 @@ type ExecuteIP struct {
|
||||
Success bool
|
||||
}
|
||||
|
||||
// Host Struct for ansible host
|
||||
// Generated from https://zhwt.github.io/yaml-to-go/
|
||||
type Host struct {
|
||||
All struct {
|
||||
Vars struct {
|
||||
AnsiblePythonInterpreter string `yaml:"ansible_python_interpreter"`
|
||||
} `yaml:"vars"`
|
||||
} `yaml:"all"`
|
||||
Main struct {
|
||||
Hosts struct {
|
||||
Host1 struct {
|
||||
AnsibleHost string `yaml:"ansible_host"`
|
||||
AnsiblePort int `yaml:"ansible_port"`
|
||||
AnsibleUser string `yaml:"ansible_user"`
|
||||
AnsibleSSHPass string `yaml:"ansible_ssh_pass"`
|
||||
AnsibleSudoPass string `yaml:"ansible_sudo_pass"`
|
||||
} `yaml:"host1"`
|
||||
} `yaml:"hosts"`
|
||||
} `yaml:"main"`
|
||||
}
|
||||
|
||||
// DetectPlugins Detects all the plugins available
|
||||
func DetectPlugins()(*Plugins,error){
|
||||
config, err:= config.ConfigInit()
|
||||
@@ -108,7 +132,13 @@ func (p *Plugin) ExecutePlugin() error {
|
||||
|
||||
// Run ip address to execute ansible inside
|
||||
for _,execute := range p.Execute {
|
||||
err := execute.RunAnsible(p.FolderName,config.PluginPath)
|
||||
// Modify ansible hosts before executing
|
||||
err = execute.ModifyHost(p,config.PluginPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = execute.RunAnsible(p,config.PluginPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -119,25 +149,22 @@ func (p *Plugin) ExecutePlugin() error {
|
||||
}
|
||||
|
||||
// RunAnsible Executes based on credentials on the struct
|
||||
func (e *ExecuteIP)RunAnsible(FolderName string,path string) error {
|
||||
func (e *ExecuteIP)RunAnsible(p *Plugin,path string) error {
|
||||
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
|
||||
User: "master",
|
||||
}
|
||||
|
||||
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
|
||||
Inventory: e.IPAddress + ",",
|
||||
ExtraVars: map[string]interface{}{
|
||||
"ansible_port":e.SSHPortNo,
|
||||
},
|
||||
Inventory: path + "/" + p.FolderName + "/hosts",
|
||||
ExtraVars: map[string]interface{}{"host_key_checking":"false"},
|
||||
}
|
||||
|
||||
ansiblePlaybookPrivilegeEscalationOptions := &options.AnsiblePrivilegeEscalationOptions{
|
||||
Become: true,
|
||||
AskBecomePass: true,
|
||||
}
|
||||
|
||||
playbook := &playbook.AnsiblePlaybookCmd{
|
||||
Playbooks: []string{path + "/" + FolderName + "site.yml"},
|
||||
Playbooks: []string{path + "/" + p.FolderName + "/site.yml"},
|
||||
ConnectionOptions: ansiblePlaybookConnectionOptions,
|
||||
PrivilegeEscalationOptions: ansiblePlaybookPrivilegeEscalationOptions,
|
||||
Options: ansiblePlaybookOptions,
|
||||
@@ -155,3 +182,51 @@ func (e *ExecuteIP)RunAnsible(FolderName string,path string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ModifyHost adds IP address , port no to the config file
|
||||
func (e *ExecuteIP)ModifyHost(p *Plugin, path string) error {
|
||||
host,err := ReadHost(path + "/" + p.FolderName + "/hosts")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Setting ansible host
|
||||
host.Main.Hosts.Host1.AnsibleHost = e.IPAddress
|
||||
// Setting SSH port no
|
||||
host.Main.Hosts.Host1.AnsiblePort, err = strconv.Atoi(e.SSHPortNo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Setting SSH user name
|
||||
host.Main.Hosts.Host1.AnsibleUser = "master"
|
||||
// Setting SSH password
|
||||
host.Main.Hosts.Host1.AnsibleSSHPass = "password"
|
||||
// Setting SSH sudo password
|
||||
host.Main.Hosts.Host1.AnsibleSudoPass = "password"
|
||||
|
||||
// write modified information to the hosts yaml file
|
||||
data,err := yaml.Marshal(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(path + "/" + p.FolderName + "/hosts",data,0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadHost Reads host file and adds
|
||||
func ReadHost(filename string) (*Host, error) {
|
||||
buf, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &Host{}
|
||||
err = yaml.Unmarshal(buf, c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("in file %q: %v", filename, err)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package plugin
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"fmt"
|
||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test if the dummy plugin added is detected
|
||||
func TestDetectPlugins(t *testing.T) {
|
||||
@@ -10,20 +14,51 @@ func TestDetectPlugins(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test ensures that the ansible are executed inside local containers
|
||||
func TestRunPlugin(t *testing.T) {
|
||||
var testips []*ExecuteIP
|
||||
var testip1,testip2 ExecuteIP
|
||||
|
||||
//Test IP 1 configuration
|
||||
testip1.IPAddress = "0.0.0.0"
|
||||
testip1.SSHPortNo = "41289"
|
||||
testip1.SSHPortNo = "33383"
|
||||
|
||||
//Test IP 2 configuration
|
||||
testip2.IPAddress = "0.0.0.0"
|
||||
testip1.SSHPortNo = "34447"
|
||||
testip2.SSHPortNo = "34447"
|
||||
|
||||
testips = append(testips, &testip1)
|
||||
testips = append(testips, &testip2)
|
||||
|
||||
RunPlugin("test",testips)
|
||||
_,err := RunPlugin("TestAnsible",testips)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// Test to ensure that the ansible host file is modified to
|
||||
// the appropriate IP
|
||||
func TestExecuteIP_ModifyHost(t *testing.T) {
|
||||
var plugin Plugin
|
||||
var testip ExecuteIP
|
||||
|
||||
// Get plugin path from config file
|
||||
Config, err := config.ConfigInit()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
//Set plugin name
|
||||
plugin.FolderName = "TestAnsible"
|
||||
|
||||
//Test IP 1 configuration
|
||||
testip.IPAddress = "0.0.0.0"
|
||||
testip.SSHPortNo = "41289"
|
||||
|
||||
err = testip.ModifyHost(&plugin,Config.PluginPath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user