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, "@") {
|
||||
path := trimSpace(strings.TrimPrefix(trimmed, "@"))
|
||||
if path == "" {
|
||||
filePath := trimSpace(strings.TrimPrefix(trimmed, "@"))
|
||||
if filePath == "" {
|
||||
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 {
|
||||
return nil, coreerr.E("parseExtraVarsValue", "read extra vars file", err)
|
||||
}
|
||||
|
|
@ -358,26 +358,26 @@ func runPlaybookCommand(opts core.Options) core.Result {
|
|||
}
|
||||
|
||||
// Load inventory
|
||||
if invPath := firstStringOption(opts, "inventory", "i"); invPath != "" {
|
||||
if !pathIsAbs(invPath) {
|
||||
invPath = absPath(invPath)
|
||||
if inventoryPath := firstStringOption(opts, "inventory", "i"); inventoryPath != "" {
|
||||
if !pathIsAbs(inventoryPath) {
|
||||
inventoryPath = absPath(inventoryPath)
|
||||
}
|
||||
|
||||
if !coreio.Local.Exists(invPath) {
|
||||
return core.Result{Value: coreerr.E("runPlaybookCommand", sprintf("inventory not found: %s", invPath), nil)}
|
||||
if !coreio.Local.Exists(inventoryPath) {
|
||||
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"} {
|
||||
p := joinPath(invPath, name)
|
||||
if coreio.Local.Exists(p) {
|
||||
invPath = p
|
||||
candidatePath := joinPath(inventoryPath, name)
|
||||
if coreio.Local.Exists(candidatePath) {
|
||||
inventoryPath = candidatePath
|
||||
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)}
|
||||
}
|
||||
}
|
||||
|
|
@ -468,7 +468,7 @@ func runSSHTestCommand(opts core.Options) core.Result {
|
|||
|
||||
print("Testing SSH connection to %s...", host)
|
||||
|
||||
cfg := ansible.SSHConfig{
|
||||
config := ansible.SSHConfig{
|
||||
Host: host,
|
||||
Port: opts.Int("port"),
|
||||
User: firstStringOption(opts, "user", "u"),
|
||||
|
|
@ -477,7 +477,7 @@ func runSSHTestCommand(opts core.Options) core.Result {
|
|||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
client, err := ansible.NewSSHClient(cfg)
|
||||
client, err := ansible.NewSSHClient(config)
|
||||
if err != nil {
|
||||
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:
|
||||
//
|
||||
// exec := NewExecutor("/workspace/playbooks")
|
||||
// executor := NewExecutor("/workspace/playbooks")
|
||||
type Executor struct {
|
||||
parser *Parser
|
||||
inventory *Inventory
|
||||
|
|
@ -96,7 +96,7 @@ type Executor struct {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// exec := NewExecutor("/workspace/playbooks")
|
||||
// executor := NewExecutor("/workspace/playbooks")
|
||||
func NewExecutor(basePath string) *Executor {
|
||||
return &Executor{
|
||||
parser: NewParser(basePath),
|
||||
|
|
@ -116,7 +116,7 @@ func NewExecutor(basePath string) *Executor {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// err := exec.SetInventory("/workspace/inventory.yml")
|
||||
// err := executor.SetInventory("/workspace/inventory.yml")
|
||||
func (e *Executor) SetInventory(path string) error {
|
||||
inv, err := e.parser.ParseInventory(path)
|
||||
if err != nil {
|
||||
|
|
@ -133,7 +133,7 @@ func (e *Executor) SetInventory(path string) error {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// exec.SetInventoryDirect(&Inventory{All: &InventoryGroup{}})
|
||||
// executor.SetInventoryDirect(&Inventory{All: &InventoryGroup{}})
|
||||
func (e *Executor) SetInventoryDirect(inv *Inventory) {
|
||||
e.mu.Lock()
|
||||
e.inventoryPath = ""
|
||||
|
|
@ -145,7 +145,7 @@ func (e *Executor) SetInventoryDirect(inv *Inventory) {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// exec.SetVar("env", "prod")
|
||||
// executor.SetVar("env", "prod")
|
||||
func (e *Executor) SetVar(key string, value any) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
|
@ -333,7 +333,7 @@ func collectHostGroupNames(group *InventoryGroup, host, name string, names map[s
|
|||
//
|
||||
// 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 {
|
||||
plays, err := e.parser.ParsePlaybook(playbookPath)
|
||||
if err != nil {
|
||||
|
|
@ -3870,7 +3870,7 @@ func (e *Executor) resetConnection(host string) {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// exec.Close()
|
||||
// executor.Close()
|
||||
func (e *Executor) Close() {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
|
@ -3885,7 +3885,7 @@ func (e *Executor) Close() {
|
|||
//
|
||||
// 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) {
|
||||
src = e.resolveLocalPath(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:
|
||||
//
|
||||
// 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) {
|
||||
plays, err := p.ParsePlaybook(path)
|
||||
if err != nil {
|
||||
|
|
@ -133,7 +133,7 @@ func (p *Parser) ParsePlaybookIter(path string) (iter.Seq[Play], error) {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// inv, err := parser.ParseInventory("/workspace/inventory.yml")
|
||||
// inventory, err := parser.ParseInventory("/workspace/inventory.yml")
|
||||
func (p *Parser) ParseInventory(path string) (*Inventory, error) {
|
||||
path = p.resolveInventoryPath(path)
|
||||
|
||||
|
|
@ -219,15 +219,15 @@ func (p *Parser) templatePath(value string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
exec := &Executor{vars: p.vars}
|
||||
return exec.templateString(value, "", nil)
|
||||
executor := &Executor{vars: p.vars}
|
||||
return executor.templateString(value, "", nil)
|
||||
}
|
||||
|
||||
// ParseTasksIter returns an iterator for tasks in a tasks file.
|
||||
//
|
||||
// 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) {
|
||||
tasks, err := p.ParseTasks(path)
|
||||
if err != nil {
|
||||
|
|
@ -927,27 +927,27 @@ func NormalizeModule(name string) string {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// hosts := GetHosts(inv, "webservers")
|
||||
func GetHosts(inv *Inventory, pattern string) []string {
|
||||
// hosts := GetHosts(inventory, "webservers")
|
||||
func GetHosts(inventory *Inventory, pattern string) []string {
|
||||
if pattern == "all" {
|
||||
return getAllHosts(inv.All)
|
||||
return getAllHosts(inventory.All)
|
||||
}
|
||||
if pattern == "localhost" {
|
||||
return []string{"localhost"}
|
||||
}
|
||||
|
||||
if contains(pattern, ":") {
|
||||
return resolveHostPattern(inv, pattern)
|
||||
return resolveHostPattern(inventory, pattern)
|
||||
}
|
||||
|
||||
// Check if it's a group name
|
||||
hosts := getGroupHosts(inv.All, pattern)
|
||||
hosts := getGroupHosts(inventory.All, pattern)
|
||||
if len(hosts) > 0 {
|
||||
return hosts
|
||||
}
|
||||
|
||||
// Check if it's a specific host
|
||||
if hasHost(inv.All, pattern) {
|
||||
if hasHost(inventory.All, pattern) {
|
||||
return []string{pattern}
|
||||
}
|
||||
|
||||
|
|
@ -1097,9 +1097,9 @@ func subtractHosts(base, extra []string) []string {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// seq := GetHostsIter(inv, "all")
|
||||
func GetHostsIter(inv *Inventory, pattern string) iter.Seq[string] {
|
||||
hosts := GetHosts(inv, pattern)
|
||||
// hostsSeq := GetHostsIter(inventory, "all")
|
||||
func GetHostsIter(inventory *Inventory, pattern string) iter.Seq[string] {
|
||||
hosts := GetHosts(inventory, pattern)
|
||||
return func(yield func(string) bool) {
|
||||
for _, host := range hosts {
|
||||
if !yield(host) {
|
||||
|
|
@ -1145,7 +1145,7 @@ func collectAllHosts(group *InventoryGroup, seen map[string]bool, hosts *[]strin
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// seq := AllHostsIter(inv.All)
|
||||
// hostsSeq := AllHostsIter(inventory.All)
|
||||
func AllHostsIter(group *InventoryGroup) iter.Seq[string] {
|
||||
return func(yield func(string) bool) {
|
||||
for _, host := range getAllHosts(group) {
|
||||
|
|
@ -1198,12 +1198,12 @@ func hasHost(group *InventoryGroup, name string) bool {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// vars := GetHostVars(inv, "web1")
|
||||
func GetHostVars(inv *Inventory, hostname string) map[string]any {
|
||||
// hostVars := GetHostVars(inventory, "web1")
|
||||
func GetHostVars(inventory *Inventory, hostname string) map[string]any {
|
||||
vars := make(map[string]any)
|
||||
|
||||
// Collect vars from all levels
|
||||
collectHostVars(inv.All, hostname, vars)
|
||||
collectHostVars(inventory.All, hostname, vars)
|
||||
|
||||
return vars
|
||||
}
|
||||
|
|
|
|||
34
ssh.go
34
ssh.go
|
|
@ -38,7 +38,7 @@ type SSHClient struct {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// cfg := SSHConfig{Host: "web1", User: "deploy", Port: 22}
|
||||
// config := SSHConfig{Host: "web1", User: "deploy", Port: 22}
|
||||
type SSHConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
|
|
@ -56,27 +56,27 @@ type SSHConfig struct {
|
|||
// Example:
|
||||
//
|
||||
// client, err := NewSSHClient(SSHConfig{Host: "web1", User: "deploy"})
|
||||
func NewSSHClient(cfg SSHConfig) (*SSHClient, error) {
|
||||
if cfg.Port == 0 {
|
||||
cfg.Port = 22
|
||||
func NewSSHClient(config SSHConfig) (*SSHClient, error) {
|
||||
if config.Port == 0 {
|
||||
config.Port = 22
|
||||
}
|
||||
if cfg.User == "" {
|
||||
cfg.User = "root"
|
||||
if config.User == "" {
|
||||
config.User = "root"
|
||||
}
|
||||
if cfg.Timeout == 0 {
|
||||
cfg.Timeout = 30 * time.Second
|
||||
if config.Timeout == 0 {
|
||||
config.Timeout = 30 * time.Second
|
||||
}
|
||||
|
||||
client := &SSHClient{
|
||||
host: cfg.Host,
|
||||
port: cfg.Port,
|
||||
user: cfg.User,
|
||||
password: cfg.Password,
|
||||
keyFile: cfg.KeyFile,
|
||||
become: cfg.Become,
|
||||
becomeUser: cfg.BecomeUser,
|
||||
becomePass: cfg.BecomePass,
|
||||
timeout: cfg.Timeout,
|
||||
host: config.Host,
|
||||
port: config.Port,
|
||||
user: config.User,
|
||||
password: config.Password,
|
||||
keyFile: config.KeyFile,
|
||||
become: config.Become,
|
||||
becomeUser: config.BecomeUser,
|
||||
becomePass: config.BecomePass,
|
||||
timeout: config.Timeout,
|
||||
}
|
||||
|
||||
return client, nil
|
||||
|
|
|
|||
2
types.go
2
types.go
|
|
@ -227,7 +227,7 @@ type TaskResult struct {
|
|||
//
|
||||
// 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 {
|
||||
All *InventoryGroup `yaml:"all"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue