feat: wire mysql delivery to awx callbacks

This commit is contained in:
mac
2026-07-28 14:45:17 +08:00
parent 5a260c443f
commit 8e09c30d21
13 changed files with 676 additions and 218 deletions
+46 -1
View File
@@ -87,7 +87,13 @@ func (c *AWXClient) Configured() bool {
}
func (c *AWXClient) Launch(ctx context.Context, templateID uint64, input AWXLaunchRequest) (*AWXJob, error) {
body := map[string]any{"inventory": input.InventoryID, "extra_vars": input.ExtraVars}
body := map[string]any{}
if input.InventoryID != 0 {
body["inventory"] = input.InventoryID
}
if input.ExtraVars != nil {
body["extra_vars"] = input.ExtraVars
}
if input.Limit != "" {
body["limit"] = input.Limit
}
@@ -141,6 +147,31 @@ func (c *AWXClient) Cancel(ctx context.Context, jobID string) error {
return c.request(ctx, http.MethodPost, "/api/v2/jobs/"+jobID+"/cancel/", map[string]any{}, nil)
}
func (c *AWXClient) WaitJob(ctx context.Context, jobID string, timeout time.Duration) (*AWXJob, error) {
if timeout <= 0 {
timeout = 45 * time.Second
}
deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
job, err := c.GetJob(deadlineCtx, jobID)
if err != nil {
return nil, err
}
switch job.Status {
case "successful", "failed", "error", "canceled":
return job, nil
}
select {
case <-deadlineCtx.Done():
return nil, fmt.Errorf("AWX job %s did not finish within %s", jobID, timeout)
case <-ticker.C:
}
}
}
func (c *AWXClient) ListJobTemplates(ctx context.Context) ([]AWXJobTemplate, error) {
var out []AWXJobTemplate
path := "/api/v2/job_templates/?page_size=200"
@@ -186,6 +217,20 @@ func (c *AWXClient) ListInventoryHosts(ctx context.Context, inventoryID uint64)
return out, nil
}
func (c *AWXClient) GetHostFacts(ctx context.Context, hostID uint64) (map[string]any, error) {
if hostID == 0 {
return nil, fmt.Errorf("invalid AWX host id")
}
var facts map[string]any
if err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/hosts/%d/ansible_facts/", hostID), nil, &facts); err != nil {
return nil, err
}
if nested, ok := facts["ansible_facts"].(map[string]any); ok {
return nested, nil
}
return facts, nil
}
func (c *AWXClient) request(ctx context.Context, method, path string, payload any, output any) error {
raw, err := c.requestRaw(ctx, method, path, payload, "application/json")
if err != nil {