|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/GoCodeAlone/workflow/config" |
| 13 | + "github.com/GoCodeAlone/workflow/interfaces" |
| 14 | +) |
| 15 | + |
| 16 | +func runLogs(args []string) error { |
| 17 | + return runLogsWithOutput(args, os.Stdout) |
| 18 | +} |
| 19 | + |
| 20 | +func runLogsWithOutput(args []string, out io.Writer) error { |
| 21 | + if len(args) < 1 { |
| 22 | + return logsUsage() |
| 23 | + } |
| 24 | + switch args[0] { |
| 25 | + case "capture": |
| 26 | + return runLogsCapture(args[1:], out) |
| 27 | + default: |
| 28 | + return logsUsage() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func logsUsage() error { |
| 33 | + fmt.Fprintf(flag.CommandLine.Output(), `Usage: wfctl logs <action> [options] |
| 34 | +
|
| 35 | +Actions: |
| 36 | + capture Capture provider logs for an infrastructure resource |
| 37 | +
|
| 38 | +Options: |
| 39 | + --config <file> Config file (default: infra.yaml or config/infra.yaml) |
| 40 | + --env <name> Environment name for provider config resolution |
| 41 | + --resource <name> infra.container_service resource name |
| 42 | + --component <name> Provider component name (for example App Platform service) |
| 43 | + --type <type> Log type: BUILD, DEPLOY, RUN, RUN_RESTARTED (default RUN) |
| 44 | + --tail <n> Tail line count (default 300) |
| 45 | + --follow Follow live logs until --duration expires |
| 46 | + --duration <d> Max follow duration (default 2m) |
| 47 | + --deployment <id> Provider deployment ID when supported |
| 48 | + --plugin-dir <dir> External plugin directory |
| 49 | +`) |
| 50 | + return fmt.Errorf("missing or unknown logs action") |
| 51 | +} |
| 52 | + |
| 53 | +func runLogsCapture(args []string, out io.Writer) error { |
| 54 | + fs := flag.NewFlagSet("logs capture", flag.ContinueOnError) |
| 55 | + fs.SetOutput(flag.CommandLine.Output()) |
| 56 | + var configFile, envName, resourceName, componentName, logType, deploymentID, pluginDir string |
| 57 | + var tailLines int |
| 58 | + var follow bool |
| 59 | + var duration time.Duration |
| 60 | + fs.StringVar(&configFile, "config", "", "Config file") |
| 61 | + fs.StringVar(&configFile, "c", "", "Config file") |
| 62 | + fs.StringVar(&envName, "env", "", "Environment name") |
| 63 | + fs.StringVar(&resourceName, "resource", "", "infra.container_service resource name") |
| 64 | + fs.StringVar(&componentName, "component", "", "Provider component name") |
| 65 | + fs.StringVar(&logType, "type", "RUN", "Log type") |
| 66 | + fs.IntVar(&tailLines, "tail", 300, "Tail line count") |
| 67 | + fs.BoolVar(&follow, "follow", false, "Follow live logs") |
| 68 | + fs.DurationVar(&duration, "duration", 2*time.Minute, "Max follow duration") |
| 69 | + fs.StringVar(&deploymentID, "deployment", "", "Provider deployment ID") |
| 70 | + fs.StringVar(&pluginDir, "plugin-dir", "", "External plugin directory") |
| 71 | + if err := fs.Parse(args); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + if resourceName == "" { |
| 75 | + return fmt.Errorf("logs capture: --resource is required") |
| 76 | + } |
| 77 | + normalizedLogType, err := normalizeLogCaptureType(logType) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + cfgFile, err := resolveInfraConfig(fs, configFile) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + cfg, err := config.LoadFromFile(cfgFile) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("load config: %w", err) |
| 88 | + } |
| 89 | + spec, providerRef, err := resolveLogCaptureResource(cfg, envName, resourceName) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + providerDefs, _, disabled := resolveProviderDefs(cfg, envName) |
| 94 | + if _, ok := disabled[providerRef]; ok { |
| 95 | + return fmt.Errorf("logs capture: provider %q is disabled for environment %q", providerRef, envName) |
| 96 | + } |
| 97 | + def, ok := providerDefs[providerRef] |
| 98 | + if !ok || def.provType == "" { |
| 99 | + return fmt.Errorf("logs capture: resource %q references unknown iac.provider %q", resourceName, providerRef) |
| 100 | + } |
| 101 | + |
| 102 | + prevPluginDir := currentInfraPluginDir |
| 103 | + currentInfraPluginDir = pluginDir |
| 104 | + defer func() { currentInfraPluginDir = prevPluginDir }() |
| 105 | + |
| 106 | + ctx := context.Background() |
| 107 | + if follow && duration > 0 { |
| 108 | + var cancel context.CancelFunc |
| 109 | + ctx, cancel = context.WithTimeout(ctx, duration) |
| 110 | + defer cancel() |
| 111 | + } |
| 112 | + durationSeconds := int64(0) |
| 113 | + if follow { |
| 114 | + durationSeconds = int64(duration / time.Second) |
| 115 | + } |
| 116 | + provider, closer, err := resolveIaCProvider(ctx, def.provType, def.provCfg) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("load provider %q: %w", def.provType, err) |
| 119 | + } |
| 120 | + if closer != nil { |
| 121 | + defer closer.Close() |
| 122 | + } |
| 123 | + capturer, ok := provider.(interfaces.LogCaptureProvider) |
| 124 | + if !ok { |
| 125 | + return fmt.Errorf("provider %q does not support log capture", def.provType) |
| 126 | + } |
| 127 | + req := interfaces.LogCaptureRequest{ |
| 128 | + ResourceName: logCaptureResourceCloudName(spec), |
| 129 | + ResourceType: spec.Type, |
| 130 | + ProviderID: logCaptureString(spec.Config["provider_id"]), |
| 131 | + ComponentName: componentName, |
| 132 | + LogType: normalizedLogType, |
| 133 | + TailLines: tailLines, |
| 134 | + Follow: follow, |
| 135 | + DurationSeconds: durationSeconds, |
| 136 | + DeploymentID: deploymentID, |
| 137 | + } |
| 138 | + return capturer.CaptureLogs(ctx, req, writerLogSink{out: out}) |
| 139 | +} |
| 140 | + |
| 141 | +func normalizeLogCaptureType(s string) (string, error) { |
| 142 | + switch strings.ToUpper(strings.TrimSpace(s)) { |
| 143 | + case "", "RUN": |
| 144 | + return "RUN", nil |
| 145 | + case "BUILD": |
| 146 | + return "BUILD", nil |
| 147 | + case "DEPLOY": |
| 148 | + return "DEPLOY", nil |
| 149 | + case "RUN_RESTARTED": |
| 150 | + return "RUN_RESTARTED", nil |
| 151 | + default: |
| 152 | + return "", fmt.Errorf("logs capture: unsupported --type %q (want BUILD, DEPLOY, RUN, or RUN_RESTARTED)", s) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func resolveLogCaptureResource(cfg *config.WorkflowConfig, envName, name string) (interfaces.ResourceSpec, string, error) { |
| 157 | + for i := range cfg.Modules { |
| 158 | + m := cfg.Modules[i] |
| 159 | + if m.Name != name { |
| 160 | + continue |
| 161 | + } |
| 162 | + resolved := m.Config |
| 163 | + if envName != "" { |
| 164 | + envResolved, ok := m.ResolveForEnv(envName) |
| 165 | + if !ok { |
| 166 | + return interfaces.ResourceSpec{}, "", fmt.Errorf("logs capture: resource %q is disabled for environment %q", name, envName) |
| 167 | + } |
| 168 | + resolved = envResolved.Config |
| 169 | + } |
| 170 | + cfgMap := config.ExpandEnvInMap(resolved) |
| 171 | + providerRef := resolveIaCProviderRef(cfgMap) |
| 172 | + if providerRef == "" { |
| 173 | + return interfaces.ResourceSpec{}, "", fmt.Errorf("logs capture: resource %q missing iac_provider/provider", name) |
| 174 | + } |
| 175 | + return interfaces.ResourceSpec{Name: m.Name, Type: m.Type, Config: cfgMap}, providerRef, nil |
| 176 | + } |
| 177 | + return interfaces.ResourceSpec{}, "", fmt.Errorf("logs capture: resource %q not found", name) |
| 178 | +} |
| 179 | + |
| 180 | +func logCaptureResourceCloudName(spec interfaces.ResourceSpec) string { |
| 181 | + for _, key := range []string{"app_name", "name"} { |
| 182 | + if v := logCaptureString(spec.Config[key]); v != "" { |
| 183 | + return v |
| 184 | + } |
| 185 | + } |
| 186 | + return spec.Name |
| 187 | +} |
| 188 | + |
| 189 | +func logCaptureString(v any) string { |
| 190 | + s, _ := v.(string) |
| 191 | + return s |
| 192 | +} |
| 193 | + |
| 194 | +type writerLogSink struct { |
| 195 | + out io.Writer |
| 196 | +} |
| 197 | + |
| 198 | +func (s writerLogSink) WriteLogChunk(chunk interfaces.LogChunk) error { |
| 199 | + if len(chunk.Data) == 0 { |
| 200 | + return nil |
| 201 | + } |
| 202 | + _, err := s.out.Write(chunk.Data) |
| 203 | + return err |
| 204 | +} |
0 commit comments