Skip to content

Commit

Permalink
Add option to use inventory files with exec commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oleewere committed May 12, 2019
1 parent 5ecf033 commit 270322b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
41 changes: 29 additions & 12 deletions cm/discovery.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package cm

// GetFilteredHosts obtain specific hosts based on different filters
func (c CMServer) GetFilteredHosts(filter Filter) map[string]bool {
func (c CMServer) GetFilteredHosts(filter Filter, inventory *Inventory) map[string]bool {
finalHosts := make(map[string]bool)
hosts := make(map[string]bool) // use boolean map as a set
if filter.Server {
hosts[c.Hostname] = true
finalHosts[c.Hostname] = true
} else if len(filter.Hosts) > 0 {
cmAgents := c.ListHosts()
cmAgents := c.getCMHosts(inventory)
calculateAndFillFinalHosts(cmAgents, filter, hosts, finalHosts)
} else if len(filter.Clusters) > 0 {
cmAgents := c.ListHosts()
cmAgents := c.getCMHosts(inventory)
for _, cluster := range filter.Clusters {
inventory := Inventory{}
inventory = GetInventoryWithHostsForCluster(inventory, cluster, cmAgents)
if inventory == nil {
inventory := Inventory{}
inventory = GetInventoryWithHostsForCluster(inventory, cluster, cmAgents)
}
for _, host := range inventory.Hosts {
hosts[host.HostName] = true
}
calculateAndFillFinalHosts(cmAgents, filter, hosts, finalHosts)
}
} else if len(filter.Roles) > 0 {
cmAgents := c.ListHosts()
deployment := c.GetDeployment()
inventories := CreateInventoriesFromDeploymentsAndHosts(deployment, cmAgents)
cmAgents := c.getCMHosts(inventory)
inventories := c.getInventories(inventory, cmAgents)
for _, inventory := range inventories {
if len(filter.Clusters) > 0 && !SliceContains(inventory.ClusterName, filter.Clusters) {
continue
Expand All @@ -39,9 +40,8 @@ func (c CMServer) GetFilteredHosts(filter Filter) map[string]bool {
}
calculateAndFillFinalHosts(cmAgents, filter, hosts, finalHosts)
} else if len(filter.Services) > 0 {
cmAgents := c.ListHosts()
deployment := c.GetDeployment()
inventories := CreateInventoriesFromDeploymentsAndHosts(deployment, cmAgents)
cmAgents := c.getCMHosts(inventory)
inventories := c.getInventories(inventory, cmAgents)
for _, inventory := range inventories {
if len(filter.Clusters) > 0 && !SliceContains(inventory.ClusterName, filter.Clusters) {
continue
Expand All @@ -57,12 +57,29 @@ func (c CMServer) GetFilteredHosts(filter Filter) map[string]bool {
}
calculateAndFillFinalHosts(cmAgents, filter, hosts, finalHosts)
} else {
cmAgents := c.ListHosts()
cmAgents := c.getCMHosts(inventory)
calculateAndFillFinalHosts(cmAgents, filter, hosts, finalHosts)
}
return finalHosts
}

func (c CMServer) getCMHosts(inventory *Inventory) []Host {
if inventory != nil {
return inventory.Hosts
}
return c.ListHosts()
}

func (c CMServer) getInventories(inventory *Inventory, cmAgents []Host) []Inventory {
inventories := make([]Inventory, 0)
if inventory != nil {
inventories = append(inventories, *inventory)
return inventories
}
deployment := c.GetDeployment()
return CreateInventoriesFromDeploymentsAndHosts(deployment, cmAgents)
}

func calculateAndFillFinalHosts(cmAgents []Host, filter Filter, hosts map[string]bool, finalHosts map[string]bool) {
for _, cmAgent := range cmAgents {
if len(filter.Hosts) > 0 {
Expand Down
16 changes: 8 additions & 8 deletions cm/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ func LoadPlaybookFile(location string, varsInput string) Playbook {
}

// ExecutePlaybook runs tasks on CM hosts based on a playbook object
func (c CMServer) ExecutePlaybook(playbook Playbook, inventories []Inventory) {
func (c CMServer) ExecutePlaybook(playbook Playbook, inventory *Inventory) {
tasks := playbook.Tasks
for _, task := range tasks {
if len(task.Type) > 0 {
filteredHosts := make(map[string]bool)
if !task.CMAgentFilter {
filter := CreateFilter(task.ClusterFilter, task.ServiceFilter, task.RoleTypeFilter, task.HostFilter, task.CMServerFilter)
filteredHosts = c.GetFilteredHosts(filter)
filteredHosts = c.GetFilteredHosts(filter, inventory)
}
if task.Type == RemoteCommand {
c.ExecuteRemoteCommandTask(task, filteredHosts)
c.ExecuteRemoteCommandTask(task, filteredHosts, inventory)
}
if task.Type == LocalCommand {
ExecuteLocalCommandTask(task)
Expand All @@ -124,7 +124,7 @@ func (c CMServer) ExecutePlaybook(playbook Playbook, inventories []Inventory) {
ExecuteDownloadFileTask(task)
}
if task.Type == Upload {
c.ExecuteUploadFileTask(task, filteredHosts)
c.ExecuteUploadFileTask(task, filteredHosts, inventory)
}
/*
if task.Type == Config {
Expand Down Expand Up @@ -203,15 +203,15 @@ func (c CMServer) ExecuteConfigCommand(task Task) {
*/

// ExecuteRemoteCommandTask executes a remote command on filtered hosts
func (c CMServer) ExecuteRemoteCommandTask(task Task, filteredHosts map[string]bool) {
func (c CMServer) ExecuteRemoteCommandTask(task Task, filteredHosts map[string]bool, inventory *Inventory) {
if len(task.Command) > 0 {
fmt.Println("Execute remote command: " + task.Command)
c.RunRemoteHostCommand(task.Command, filteredHosts, task.CMServerFilter)
c.RunRemoteHostCommand(task.Command, filteredHosts, task.CMServerFilter, inventory)
}
}

// ExecuteUploadFileTask upload a file to specific (filtered) hosts
func (c CMServer) ExecuteUploadFileTask(task Task, filteredHosts map[string]bool) {
func (c CMServer) ExecuteUploadFileTask(task Task, filteredHosts map[string]bool, inventory *Inventory) {
if task.Parameters != nil {
haveSourceFile := false
haveTargetFile := false
Expand All @@ -226,7 +226,7 @@ func (c CMServer) ExecuteUploadFileTask(task Task, filteredHosts map[string]bool
cmServerGatewayAddress = c.Hostname
}
// TODO if server filter is used with gateway ?
c.CopyToRemote(sourceVal, targetVal, filteredHosts, cmServerGatewayAddress)
c.CopyToRemote(sourceVal, targetVal, filteredHosts, cmServerGatewayAddress, inventory)
}
}
if !haveSourceFile {
Expand Down
8 changes: 4 additions & 4 deletions cm/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c CMServer) CopyToCMGateway(source string, dest string) {
}

// RunRemoteHostCommand executes bash commands on CM hosts
func (c CMServer) RunRemoteHostCommand(command string, filteredHosts map[string]bool, skipJump bool) map[string]RemoteResponse {
func (c CMServer) RunRemoteHostCommand(command string, filteredHosts map[string]bool, skipJump bool, inventory *Inventory) map[string]RemoteResponse {
connectionProfileID := c.ConnectionProfile
if len(connectionProfileID) == 0 {
fmt.Println("No connection profile is attached for the active CM server entry!")
Expand All @@ -101,7 +101,7 @@ func (c CMServer) RunRemoteHostCommand(command string, filteredHosts map[string]
if len(filteredHosts) > 0 {
hosts = filteredHosts
} else {
hosts = c.GetFilteredHosts(Filter{})
hosts = c.GetFilteredHosts(Filter{}, inventory)
}
response := make(map[string]RemoteResponse)
var wg sync.WaitGroup
Expand Down Expand Up @@ -154,7 +154,7 @@ func DownloadViaScp(sshConfig *easyssh.MakeConfig, source string, dest string, s
}

// CopyToRemote copy local file to remote host(s)
func (c CMServer) CopyToRemote(source string, dest string, filteredHosts map[string]bool, gateway string) {
func (c CMServer) CopyToRemote(source string, dest string, filteredHosts map[string]bool, gateway string, inventory *Inventory) {
connectionProfileID := c.ConnectionProfile
if len(connectionProfileID) == 0 {
fmt.Println("No connection profile is attached for the active CM server entry!")
Expand All @@ -165,7 +165,7 @@ func (c CMServer) CopyToRemote(source string, dest string, filteredHosts map[str
if len(filteredHosts) > 0 {
hosts = filteredHosts
} else {
hosts = c.GetFilteredHosts(Filter{})
hosts = c.GetFilteredHosts(Filter{}, inventory)
}
var wg sync.WaitGroup
wg.Add(len(hosts))
Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func main() {
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "inventory, i", Usage: "Use hosts inventory file"},
cli.StringFlag{Name: "inventory, i", Usage: "Hosts inventory file"},
cli.StringFlag{Name: "clusters, c", Usage: "Cluster filter for roles"},
cli.StringFlag{Name: "services, s", Usage: "Service filter for roles"},
},
Expand Down Expand Up @@ -727,7 +727,7 @@ func main() {
},
Flags: []cli.Flag{
cli.StringFlag{Name: "command, c", Usage: "Command: start/stop/restart"},
cli.StringFlag{Name: "inventory, i", Usage: "Use hosts inventory file"},
cli.StringFlag{Name: "inventory, i", Usage: "Hosts inventory file"},
cli.StringFlag{Name: "clusters", Usage: "Clusters filter (comma separated)"},
cli.StringFlag{Name: "service, s", Usage: "Services filter"},
cli.StringFlag{Name: "roles, r", Usage: "Role type filter (comma separated)"},
Expand Down Expand Up @@ -1158,18 +1158,25 @@ func main() {
cmServer := cm.GetActiveCM()
validateActiveCM(cmServer)
command := c.String("command")
inventoryFile := c.String("inventory")
if len(command) == 0 {
fmt.Println("Command parameter is missing! (use 'command' or 'c')")
os.Exit(1)
}
var inventory *cm.Inventory
if len(inventoryFile) > 0 {
inventoryVal := cm.ReadInventoryFromFile(inventoryFile)
inventory = &inventoryVal
}
filter := cm.CreateFilter(c.String("clusters"), c.String("services"), c.String("roles"), c.String("hosts"), c.Bool("server"))
filter.Roles = cm.UpperAllInSlice(filter.Roles)
hosts := cmServer.GetFilteredHosts(filter)
cmServer.RunRemoteHostCommand(command, hosts, filter.Server)
hosts := cmServer.GetFilteredHosts(filter, inventory)
cmServer.RunRemoteHostCommand(command, hosts, filter.Server, inventory)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "command, c", Usage: "Command to execute on the remote hosts"},
cli.StringFlag{Name: "inventory, i", Usage: "Hosts inventory file"},
cli.BoolFlag{Name: "server", Usage: "Filter on CM server"},
cli.StringFlag{Name: "clusters", Usage: "Filter on clusters (comma separated)"},
cli.StringFlag{Name: "services", Usage: "Filter on services (comma separated)"},
Expand Down

0 comments on commit 270322b

Please sign in to comment.