refactor(ansible): align AX naming examples
This commit is contained in:
parent
ac45fd9830
commit
e95c35c097
5 changed files with 59 additions and 59 deletions
|
|
@ -197,12 +197,12 @@ func parseExtraVarsValue(value string) (map[string]any, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(trimmed, "@") {
|
if strings.HasPrefix(trimmed, "@") {
|
||||||
path := trimSpace(strings.TrimPrefix(trimmed, "@"))
|
filePath := trimSpace(strings.TrimPrefix(trimmed, "@"))
|
||||||
if path == "" {
|
if filePath == "" {
|
||||||
return nil, coreerr.E("parseExtraVarsValue", "extra vars file path required", nil)
|
return nil, coreerr.E("parseExtraVarsValue", "extra vars file path required", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := coreio.Local.Read(path)
|
data, err := coreio.Local.Read(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, coreerr.E("parseExtraVarsValue", "read extra vars file", err)
|
return nil, coreerr.E("parseExtraVarsValue", "read extra vars file", err)
|
||||||
}
|
}
|
||||||
|
|
@ -358,26 +358,26 @@ func runPlaybookCommand(opts core.Options) core.Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load inventory
|
// Load inventory
|
||||||
if invPath := firstStringOption(opts, "inventory", "i"); invPath != "" {
|
if inventoryPath := firstStringOption(opts, "inventory", "i"); inventoryPath != "" {
|
||||||
if !pathIsAbs(invPath) {
|
if !pathIsAbs(inventoryPath) {
|
||||||
invPath = absPath(invPath)
|
inventoryPath = absPath(inventoryPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !coreio.Local.Exists(invPath) {
|
if !coreio.Local.Exists(inventoryPath) {
|
||||||
return core.Result{Value: coreerr.E("runPlaybookCommand", sprintf("inventory not found: %s", invPath), nil)}
|
return core.Result{Value: coreerr.E("runPlaybookCommand", sprintf("inventory not found: %s", inventoryPath), nil)}
|
||||||
}
|
}
|
||||||
|
|
||||||
if coreio.Local.IsDir(invPath) {
|
if coreio.Local.IsDir(inventoryPath) {
|
||||||
for _, name := range []string{"inventory.yml", "hosts.yml", "inventory.yaml", "hosts.yaml"} {
|
for _, name := range []string{"inventory.yml", "hosts.yml", "inventory.yaml", "hosts.yaml"} {
|
||||||
p := joinPath(invPath, name)
|
candidatePath := joinPath(inventoryPath, name)
|
||||||
if coreio.Local.Exists(p) {
|
if coreio.Local.Exists(candidatePath) {
|
||||||
invPath = p
|
inventoryPath = candidatePath
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := executor.SetInventory(invPath); err != nil {
|
if err := executor.SetInventory(inventoryPath); err != nil {
|
||||||
return core.Result{Value: coreerr.E("runPlaybookCommand", "load inventory", err)}
|
return core.Result{Value: coreerr.E("runPlaybookCommand", "load inventory", err)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -468,7 +468,7 @@ func runSSHTestCommand(opts core.Options) core.Result {
|
||||||
|
|
||||||
print("Testing SSH connection to %s...", host)
|
print("Testing SSH connection to %s...", host)
|
||||||
|
|
||||||
cfg := ansible.SSHConfig{
|
config := ansible.SSHConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
Port: opts.Int("port"),
|
Port: opts.Int("port"),
|
||||||
User: firstStringOption(opts, "user", "u"),
|
User: firstStringOption(opts, "user", "u"),
|
||||||
|
|
@ -477,7 +477,7 @@ func runSSHTestCommand(opts core.Options) core.Result {
|
||||||
Timeout: 30 * time.Second,
|
Timeout: 30 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := ansible.NewSSHClient(cfg)
|
client, err := ansible.NewSSHClient(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return core.Result{Value: coreerr.E("runSSHTestCommand", "create client", err)}
|
return core.Result{Value: coreerr.E("runSSHTestCommand", "create client", err)}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
executor.go
16
executor.go
|
|
@ -60,7 +60,7 @@ func (c *environmentSSHClient) RunScript(ctx context.Context, script string) (st
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// exec := NewExecutor("/workspace/playbooks")
|
// executor := NewExecutor("/workspace/playbooks")
|
||||||
type Executor struct {
|
type Executor struct {
|
||||||
parser *Parser
|
parser *Parser
|
||||||
inventory *Inventory
|
inventory *Inventory
|
||||||
|
|
@ -96,7 +96,7 @@ type Executor struct {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// exec := NewExecutor("/workspace/playbooks")
|
// executor := NewExecutor("/workspace/playbooks")
|
||||||
func NewExecutor(basePath string) *Executor {
|
func NewExecutor(basePath string) *Executor {
|
||||||
return &Executor{
|
return &Executor{
|
||||||
parser: NewParser(basePath),
|
parser: NewParser(basePath),
|
||||||
|
|
@ -116,7 +116,7 @@ func NewExecutor(basePath string) *Executor {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// err := exec.SetInventory("/workspace/inventory.yml")
|
// err := executor.SetInventory("/workspace/inventory.yml")
|
||||||
func (e *Executor) SetInventory(path string) error {
|
func (e *Executor) SetInventory(path string) error {
|
||||||
inv, err := e.parser.ParseInventory(path)
|
inv, err := e.parser.ParseInventory(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -133,7 +133,7 @@ func (e *Executor) SetInventory(path string) error {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// exec.SetInventoryDirect(&Inventory{All: &InventoryGroup{}})
|
// executor.SetInventoryDirect(&Inventory{All: &InventoryGroup{}})
|
||||||
func (e *Executor) SetInventoryDirect(inv *Inventory) {
|
func (e *Executor) SetInventoryDirect(inv *Inventory) {
|
||||||
e.mu.Lock()
|
e.mu.Lock()
|
||||||
e.inventoryPath = ""
|
e.inventoryPath = ""
|
||||||
|
|
@ -145,7 +145,7 @@ func (e *Executor) SetInventoryDirect(inv *Inventory) {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// exec.SetVar("env", "prod")
|
// executor.SetVar("env", "prod")
|
||||||
func (e *Executor) SetVar(key string, value any) {
|
func (e *Executor) SetVar(key string, value any) {
|
||||||
e.mu.Lock()
|
e.mu.Lock()
|
||||||
defer e.mu.Unlock()
|
defer e.mu.Unlock()
|
||||||
|
|
@ -333,7 +333,7 @@ func collectHostGroupNames(group *InventoryGroup, host, name string, names map[s
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// err := exec.Run(context.Background(), "/workspace/playbooks/site.yml")
|
// err := executor.Run(context.Background(), "/workspace/playbooks/site.yml")
|
||||||
func (e *Executor) Run(ctx context.Context, playbookPath string) error {
|
func (e *Executor) Run(ctx context.Context, playbookPath string) error {
|
||||||
plays, err := e.parser.ParsePlaybook(playbookPath)
|
plays, err := e.parser.ParsePlaybook(playbookPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -3870,7 +3870,7 @@ func (e *Executor) resetConnection(host string) {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// exec.Close()
|
// executor.Close()
|
||||||
func (e *Executor) Close() {
|
func (e *Executor) Close() {
|
||||||
e.mu.Lock()
|
e.mu.Lock()
|
||||||
defer e.mu.Unlock()
|
defer e.mu.Unlock()
|
||||||
|
|
@ -3885,7 +3885,7 @@ func (e *Executor) Close() {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// content, err := exec.TemplateFile("/workspace/templates/app.conf.j2", "web1", &Task{})
|
// content, err := executor.TemplateFile("/workspace/templates/app.conf.j2", "web1", &Task{})
|
||||||
func (e *Executor) TemplateFile(src, host string, task *Task) (string, error) {
|
func (e *Executor) TemplateFile(src, host string, task *Task) (string, error) {
|
||||||
src = e.resolveLocalPath(src)
|
src = e.resolveLocalPath(src)
|
||||||
if src == "" {
|
if src == "" {
|
||||||
|
|
|
||||||
36
parser.go
36
parser.go
|
|
@ -114,7 +114,7 @@ func (p *Parser) parsePlaybook(path string, seen map[string]bool) ([]Play, error
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// seq, err := parser.ParsePlaybookIter("/workspace/playbooks/site.yml")
|
// playsSeq, err := parser.ParsePlaybookIter("/workspace/playbooks/site.yml")
|
||||||
func (p *Parser) ParsePlaybookIter(path string) (iter.Seq[Play], error) {
|
func (p *Parser) ParsePlaybookIter(path string) (iter.Seq[Play], error) {
|
||||||
plays, err := p.ParsePlaybook(path)
|
plays, err := p.ParsePlaybook(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -133,7 +133,7 @@ func (p *Parser) ParsePlaybookIter(path string) (iter.Seq[Play], error) {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// inv, err := parser.ParseInventory("/workspace/inventory.yml")
|
// inventory, err := parser.ParseInventory("/workspace/inventory.yml")
|
||||||
func (p *Parser) ParseInventory(path string) (*Inventory, error) {
|
func (p *Parser) ParseInventory(path string) (*Inventory, error) {
|
||||||
path = p.resolveInventoryPath(path)
|
path = p.resolveInventoryPath(path)
|
||||||
|
|
||||||
|
|
@ -219,15 +219,15 @@ func (p *Parser) templatePath(value string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
exec := &Executor{vars: p.vars}
|
executor := &Executor{vars: p.vars}
|
||||||
return exec.templateString(value, "", nil)
|
return executor.templateString(value, "", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseTasksIter returns an iterator for tasks in a tasks file.
|
// ParseTasksIter returns an iterator for tasks in a tasks file.
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// seq, err := parser.ParseTasksIter("/workspace/roles/web/tasks/main.yml")
|
// tasksSeq, err := parser.ParseTasksIter("/workspace/roles/web/tasks/main.yml")
|
||||||
func (p *Parser) ParseTasksIter(path string) (iter.Seq[Task], error) {
|
func (p *Parser) ParseTasksIter(path string) (iter.Seq[Task], error) {
|
||||||
tasks, err := p.ParseTasks(path)
|
tasks, err := p.ParseTasks(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -927,27 +927,27 @@ func NormalizeModule(name string) string {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// hosts := GetHosts(inv, "webservers")
|
// hosts := GetHosts(inventory, "webservers")
|
||||||
func GetHosts(inv *Inventory, pattern string) []string {
|
func GetHosts(inventory *Inventory, pattern string) []string {
|
||||||
if pattern == "all" {
|
if pattern == "all" {
|
||||||
return getAllHosts(inv.All)
|
return getAllHosts(inventory.All)
|
||||||
}
|
}
|
||||||
if pattern == "localhost" {
|
if pattern == "localhost" {
|
||||||
return []string{"localhost"}
|
return []string{"localhost"}
|
||||||
}
|
}
|
||||||
|
|
||||||
if contains(pattern, ":") {
|
if contains(pattern, ":") {
|
||||||
return resolveHostPattern(inv, pattern)
|
return resolveHostPattern(inventory, pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's a group name
|
// Check if it's a group name
|
||||||
hosts := getGroupHosts(inv.All, pattern)
|
hosts := getGroupHosts(inventory.All, pattern)
|
||||||
if len(hosts) > 0 {
|
if len(hosts) > 0 {
|
||||||
return hosts
|
return hosts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's a specific host
|
// Check if it's a specific host
|
||||||
if hasHost(inv.All, pattern) {
|
if hasHost(inventory.All, pattern) {
|
||||||
return []string{pattern}
|
return []string{pattern}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1097,9 +1097,9 @@ func subtractHosts(base, extra []string) []string {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// seq := GetHostsIter(inv, "all")
|
// hostsSeq := GetHostsIter(inventory, "all")
|
||||||
func GetHostsIter(inv *Inventory, pattern string) iter.Seq[string] {
|
func GetHostsIter(inventory *Inventory, pattern string) iter.Seq[string] {
|
||||||
hosts := GetHosts(inv, pattern)
|
hosts := GetHosts(inventory, pattern)
|
||||||
return func(yield func(string) bool) {
|
return func(yield func(string) bool) {
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
if !yield(host) {
|
if !yield(host) {
|
||||||
|
|
@ -1145,7 +1145,7 @@ func collectAllHosts(group *InventoryGroup, seen map[string]bool, hosts *[]strin
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// seq := AllHostsIter(inv.All)
|
// hostsSeq := AllHostsIter(inventory.All)
|
||||||
func AllHostsIter(group *InventoryGroup) iter.Seq[string] {
|
func AllHostsIter(group *InventoryGroup) iter.Seq[string] {
|
||||||
return func(yield func(string) bool) {
|
return func(yield func(string) bool) {
|
||||||
for _, host := range getAllHosts(group) {
|
for _, host := range getAllHosts(group) {
|
||||||
|
|
@ -1198,12 +1198,12 @@ func hasHost(group *InventoryGroup, name string) bool {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// vars := GetHostVars(inv, "web1")
|
// hostVars := GetHostVars(inventory, "web1")
|
||||||
func GetHostVars(inv *Inventory, hostname string) map[string]any {
|
func GetHostVars(inventory *Inventory, hostname string) map[string]any {
|
||||||
vars := make(map[string]any)
|
vars := make(map[string]any)
|
||||||
|
|
||||||
// Collect vars from all levels
|
// Collect vars from all levels
|
||||||
collectHostVars(inv.All, hostname, vars)
|
collectHostVars(inventory.All, hostname, vars)
|
||||||
|
|
||||||
return vars
|
return vars
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
ssh.go
34
ssh.go
|
|
@ -38,7 +38,7 @@ type SSHClient struct {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// cfg := SSHConfig{Host: "web1", User: "deploy", Port: 22}
|
// config := SSHConfig{Host: "web1", User: "deploy", Port: 22}
|
||||||
type SSHConfig struct {
|
type SSHConfig struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
|
|
@ -56,27 +56,27 @@ type SSHConfig struct {
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// client, err := NewSSHClient(SSHConfig{Host: "web1", User: "deploy"})
|
// client, err := NewSSHClient(SSHConfig{Host: "web1", User: "deploy"})
|
||||||
func NewSSHClient(cfg SSHConfig) (*SSHClient, error) {
|
func NewSSHClient(config SSHConfig) (*SSHClient, error) {
|
||||||
if cfg.Port == 0 {
|
if config.Port == 0 {
|
||||||
cfg.Port = 22
|
config.Port = 22
|
||||||
}
|
}
|
||||||
if cfg.User == "" {
|
if config.User == "" {
|
||||||
cfg.User = "root"
|
config.User = "root"
|
||||||
}
|
}
|
||||||
if cfg.Timeout == 0 {
|
if config.Timeout == 0 {
|
||||||
cfg.Timeout = 30 * time.Second
|
config.Timeout = 30 * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &SSHClient{
|
client := &SSHClient{
|
||||||
host: cfg.Host,
|
host: config.Host,
|
||||||
port: cfg.Port,
|
port: config.Port,
|
||||||
user: cfg.User,
|
user: config.User,
|
||||||
password: cfg.Password,
|
password: config.Password,
|
||||||
keyFile: cfg.KeyFile,
|
keyFile: config.KeyFile,
|
||||||
become: cfg.Become,
|
become: config.Become,
|
||||||
becomeUser: cfg.BecomeUser,
|
becomeUser: config.BecomeUser,
|
||||||
becomePass: cfg.BecomePass,
|
becomePass: config.BecomePass,
|
||||||
timeout: cfg.Timeout,
|
timeout: config.Timeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
|
|
|
||||||
2
types.go
2
types.go
|
|
@ -227,7 +227,7 @@ type TaskResult struct {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// inv := Inventory{All: &InventoryGroup{Hosts: map[string]*Host{"web1": {AnsibleHost: "10.0.0.1"}}}}
|
// inventory := Inventory{All: &InventoryGroup{Hosts: map[string]*Host{"web1": {AnsibleHost: "10.0.0.1"}}}}
|
||||||
type Inventory struct {
|
type Inventory struct {
|
||||||
All *InventoryGroup `yaml:"all"`
|
All *InventoryGroup `yaml:"all"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue