Skip to content

Commit

Permalink
Merge pull request #362 from aerospike/master
Browse files Browse the repository at this point in the history
mrge master to v7.6.0
  • Loading branch information
robertglonek authored Jul 17, 2024
2 parents 8d15cc9 + 6f79f5e commit 08631da
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
!bin/aerolabrpm
.DS_Store
.vscode
VERSION.md
.idea
VERSION.md
2 changes: 1 addition & 1 deletion src/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *showcommandsCmd) Execute(args []string) error {
return fmt.Errorf("failed to get absolute path os self: %s", err)
}
log.Printf("Discovered absolute path: %s", cur)
for _, dest := range []string{"showconf", "showsysinfo", "showinterrupts"} {
for _, dest := range []string{"showconf", "showsysinfo", "showinterrupts", "aerolab-ansible"} {
d := filepath.Join(c.DestDir, dest)
log.Printf("> ln -s %s %s", cur, d)
if _, err := os.Stat(d); err == nil {
Expand Down
1 change: 1 addition & 0 deletions src/cmdInventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type inventoryCmd struct {
List inventoryListCmd `command:"list" subcommands-optional:"true" description:"List clusters, clients and templates" webicon:"fas fa-list"`
Ansible inventoryAnsibleCmd `command:"ansible" subcommands-optional:"true" description:"Export inventory as ansible inventory" webicon:"fas fa-list"`
InstanceTypes inventoryInstanceTypesCmd `command:"instance-types" subcommands-optional:"true" description:"Lookup GCP|AWS available instance types" webicon:"fas fa-table-list"`
Help helpCmd `command:"help" subcommands-optional:"true" description:"Print help"`
}
Expand Down
141 changes: 141 additions & 0 deletions src/cmdInventoryExport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package main

import (
"encoding/json"
"fmt"
"os"
)

type HostVars map[string]interface{}
type Hosts map[string]HostVars

type Group struct {
Hosts []string `json:"hosts,omitempty"`
Vars map[string]interface{} `json:"vars,omitempty"`
}

type Groups map[string]Group

type Meta struct {
Hostvars Hosts `json:"hostvars"`
}

type AnsibleInventory struct {
Groups Groups `json:"groups"`
Meta Meta `json:"_meta"`
}

func (a AnsibleInventory) MarshalJSON() ([]byte, error) {
aux := make(map[string]interface{})

for key, value := range a.Groups {
aux[key] = value
}

aux["_meta"] = a.Meta

return json.Marshal(aux)
}

type inventoryAnsibleCmd struct{}

func (c *inventoryAnsibleCmd) Execute(args []string) error {
if earlyProcess(args) {
return nil
}
return c.run()
}

func (c *inventoryAnsibleCmd) run() error {
projectName := os.Getenv("PROJECT_NAME")

inventoryItems := []int{}
inventoryItems = append(inventoryItems, InventoryItemClusters)
inventoryItems = append(inventoryItems, InventoryItemClients)

inventory, err := b.Inventory("", inventoryItems)
if err != nil {
return fmt.Errorf("b.Inventory: %s", err)
}

inv := AnsibleInventory{
Groups: Groups{},
Meta: Meta{
Hostvars: Hosts{},
},
}

processCluster := func(clusterName, groupName, project, nodeNo, privateIP, instanceId, sshKeyPath string) {
inv.Meta.Hostvars[privateIP] = HostVars{
"ansible_host": privateIP,
"instance_id": instanceId,
"node_name": fmt.Sprintf("%s-%s", clusterName, nodeNo),
"ansible_user": "root",
"aerolab_cluster": clusterName,
}

if project != "" {
inv.Meta.Hostvars[privateIP]["project"] = project
}

if sshKeyPath != "" {
inv.Meta.Hostvars[privateIP]["ansible_ssh_private_key_file"] = sshKeyPath
}

if _, exists := inv.Groups[groupName]; !exists {
inv.Groups[groupName] = Group{Hosts: []string{}, Vars: map[string]interface{}{}}
}
group := inv.Groups[groupName]
group.Hosts = append(group.Hosts, privateIP)
inv.Groups[groupName] = group
}

for _, cluster := range inventory.Clusters {
project := searchField("project", cluster.AwsTags, cluster.GcpLabels, cluster.DockerLabels)

if projectName != "" {
if project != projectName {
continue
}
}

groupName := "aerospike"
if cluster.Features&ClusterFeatureAGI > 0 {
groupName = "agi"
}

processCluster(cluster.ClusterName, groupName, project, cluster.NodeNo, cluster.PrivateIp, cluster.InstanceId, cluster.SSHKeyPath)
}

for _, cluster := range inventory.Clients {
project := searchField("project", cluster.AwsTags, cluster.GcpLabels, cluster.DockerLabels)

if projectName != "" {
if project != projectName {
continue
}
}

processCluster(cluster.ClientName, cluster.ClientType, project, cluster.NodeNo, cluster.PrivateIp, cluster.InstanceId, cluster.SSHKeyPath)
}

inventoryJson, err := json.MarshalIndent(inv, "", " ")
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}

fmt.Println(string(inventoryJson))

return nil
}

func searchField(field string, maps ...map[string]string) string {
for _, m := range maps {
if value, ok := m[field]; ok {
return value
}
}

return ""
}
7 changes: 6 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
"sync"
"time"

"github.com/aerospike/aerolab/eksexpiry"
"github.com/aws/aws-sdk-go/aws"
"github.com/bestmethod/inslice"
"github.com/google/uuid"
flags "github.com/rglonek/jeddevdk-goflags"

"github.com/aerospike/aerolab/eksexpiry"
)

type helpCmd struct{}
Expand Down Expand Up @@ -138,6 +139,10 @@ func main() {
showcommands()
case "eksexpiry":
eksexpiry.Expiry()
case "aerolab-ansible":
os.Args = []string{os.Args[0], "inventory", "ansible"}

fallthrough
default:
if beepenv := os.Getenv("AEROLAB_BEEP"); beepenv != "" {
bp, err := strconv.Atoi(beepenv)
Expand Down

0 comments on commit 08631da

Please sign in to comment.