-
Notifications
You must be signed in to change notification settings - Fork 6
lstk az Azure CLI proxy
#251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carole-lavillonniere
wants to merge
4
commits into
main
Choose a base branch
from
flc-656-register-azure-custom-cloud-in-lstk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad41209
az cli proxy
carole-lavillonniere f986533
clarify dns error message for azure commands
carole-lavillonniere 4904457
support non-interactive setup azure for ci
carole-lavillonniere 0cdf9e0
report setup azure failures once instead of warning plus error
carole-lavillonniere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/localstack/lstk/internal/azurecli" | ||
| "github.com/localstack/lstk/internal/azureconfig" | ||
| "github.com/localstack/lstk/internal/config" | ||
| "github.com/localstack/lstk/internal/container" | ||
| "github.com/localstack/lstk/internal/endpoint" | ||
| "github.com/localstack/lstk/internal/env" | ||
| "github.com/localstack/lstk/internal/output" | ||
| "github.com/localstack/lstk/internal/runtime" | ||
| "github.com/localstack/lstk/internal/terminal" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newAzCmd(cfg *env.Env) *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "az [args...]", | ||
| Short: "Run Azure CLI commands against LocalStack", | ||
| Long: `Run Azure CLI commands against the LocalStack Azure emulator. | ||
|
|
||
| Runs 'az <args>' with an isolated AZURE_CONFIG_DIR in which a custom Azure cloud | ||
| is registered against LocalStack's endpoints, so your global ~/.azure | ||
| configuration is left untouched and plain 'az' commands keep talking to real | ||
| Azure. | ||
|
|
||
| Run 'lstk setup azure' once before using this command. | ||
|
|
||
| Examples: | ||
| lstk az group list | ||
| lstk az storage account list`, | ||
| DisableFlagParsing: true, | ||
| PreRunE: initConfig(nil), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| rt, err := runtime.NewDockerRuntime(cfg.DockerHost) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| appCfg, err := config.Get() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get config: %w", err) | ||
| } | ||
|
|
||
| azureContainer := config.ContainerConfig{Type: config.EmulatorAzure, Port: config.DefaultPort} | ||
| for _, c := range appCfg.Containers { | ||
| if c.Type == config.EmulatorAzure { | ||
| azureContainer = c | ||
| break | ||
| } | ||
| } | ||
|
|
||
| sink := output.NewPlainSink(os.Stdout) | ||
|
|
||
| configDir, err := config.ConfigDir() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to resolve config directory: %w", err) | ||
| } | ||
| azureConfigDir := azureconfig.ConfigDir(configDir) | ||
| if !azureconfig.IsSetUp(azureConfigDir) { | ||
| sink.Emit(output.ErrorEvent{ | ||
| Title: "Azure CLI integration is not set up", | ||
| Actions: []output.ErrorAction{ | ||
| {Label: "Set it up:", Value: "lstk setup azure"}, | ||
| }, | ||
| }) | ||
| return output.NewSilentError(fmt.Errorf("azure CLI integration not set up")) | ||
| } | ||
|
|
||
| if err := rt.IsHealthy(cmd.Context()); err != nil { | ||
| rt.EmitUnhealthyError(sink, err) | ||
| return output.NewSilentError(fmt.Errorf("runtime not healthy: %w", err)) | ||
| } | ||
|
|
||
| runningName, err := container.ResolveRunningContainerName(cmd.Context(), rt, azureContainer) | ||
| if err != nil { | ||
| return fmt.Errorf("checking emulator status: %w", err) | ||
| } | ||
| if runningName == "" { | ||
| sink.Emit(output.ErrorEvent{ | ||
| Title: fmt.Sprintf("%s is not running", azureContainer.DisplayName()), | ||
| Actions: []output.ErrorAction{ | ||
| {Label: "Start LocalStack:", Value: "lstk"}, | ||
| {Label: "See help:", Value: "lstk -h"}, | ||
| }, | ||
| }) | ||
| return output.NewSilentError(fmt.Errorf("%s is not running", azureContainer.Name())) | ||
| } | ||
|
|
||
| _, dnsOK := endpoint.ResolveHost(cmd.Context(), azureContainer.Port, cfg.LocalStackHost) | ||
| if !dnsOK { | ||
| sink.Emit(output.ErrorEvent{ | ||
| Title: "DNS resolution required for 'lstk az'", | ||
| Actions: []output.ErrorAction{ | ||
| {Label: "Note:", Value: "Could not resolve *." + endpoint.Hostname + " to 127.0.0.1."}, | ||
| {Label: "Why:", Value: "the Azure emulator serves endpoints under *." + endpoint.Hostname + ", which the Azure CLI must be able to resolve"}, | ||
| {Label: "Fix:", Value: "configure DNS or set LOCALSTACK_HOST"}, | ||
| }, | ||
| }) | ||
| return output.NewSilentError(fmt.Errorf("dns resolution required for 'lstk az'")) | ||
| } | ||
|
|
||
| azEnv := azureconfig.Env(azureConfigDir) | ||
|
|
||
| stdout, stderr := io.Writer(os.Stdout), io.Writer(os.Stderr) | ||
| if terminal.IsTerminal(os.Stderr) { | ||
| s := terminal.NewSpinner(os.Stderr, "Loading service...", 4*time.Second) | ||
| s.Start() | ||
| defer s.Stop() | ||
| stdout = &terminal.StopOnWriteWriter{W: os.Stdout, Spinner: s} | ||
| stderr = &terminal.StopOnWriteWriter{W: os.Stderr, Spinner: s} | ||
| } | ||
|
|
||
| return azurecli.Exec(cmd.Context(), azEnv, os.Stdin, stdout, stderr, args...) | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package azurecli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/codes" | ||
| ) | ||
|
|
||
| // ErrNotInstalled is returned when the `az` binary cannot be found on PATH. | ||
| var ErrNotInstalled = errors.New("az CLI not found in PATH — install it from https://learn.microsoft.com/cli/azure/install-azure-cli") | ||
|
|
||
| // CheckInstalled returns ErrNotInstalled if the `az` binary is not on PATH. | ||
| // Callers should use this before performing setup work to avoid leaving partial state. | ||
| func CheckInstalled() error { | ||
| if _, err := exec.LookPath("az"); err != nil { | ||
| return ErrNotInstalled | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Exec runs `az <args...>`. extraEnv is appended to the inherited process environment | ||
| // (later entries win), letting callers inject AZURE_CONFIG_DIR, proxy, and CA settings | ||
| // without mutating the user's global Azure CLI configuration. | ||
| func Exec(ctx context.Context, extraEnv []string, stdin io.Reader, stdout, stderr io.Writer, args ...string) error { | ||
| ctx, span := otel.Tracer("github.com/localstack/lstk/internal/azurecli").Start(ctx, "az cli") | ||
| defer span.End() | ||
|
|
||
| azBin, err := exec.LookPath("az") | ||
| if err != nil { | ||
| span.RecordError(err) | ||
| span.SetStatus(codes.Error, err.Error()) | ||
| return ErrNotInstalled | ||
| } | ||
|
|
||
| span.SetAttributes(attribute.StringSlice("az.args", args)) | ||
|
|
||
| cmd := exec.CommandContext(ctx, azBin, args...) | ||
| cmd.Stdin = stdin | ||
| cmd.Stdout = stdout | ||
| cmd.Stderr = stderr | ||
| if len(extraEnv) > 0 { | ||
| cmd.Env = append(os.Environ(), extraEnv...) | ||
| } | ||
| if err := cmd.Run(); err != nil { | ||
| var exitErr *exec.ExitError | ||
| if errors.As(err, &exitErr) { | ||
| span.SetAttributes(attribute.Int("az.exit_code", exitErr.ExitCode())) | ||
| span.SetStatus(codes.Error, "az cli exited non-zero") | ||
| } else { | ||
| span.RecordError(err) | ||
| span.SetStatus(codes.Error, err.Error()) | ||
| } | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Run executes `az <args...>` with extraEnv and returns the captured stdout, stderr, | ||
| // and any error. On non-zero exit, the error wraps stderr to aid debugging. | ||
| func Run(ctx context.Context, extraEnv []string, args ...string) (stdout, stderr string, err error) { | ||
| var outBuf, errBuf bytes.Buffer | ||
| runErr := Exec(ctx, extraEnv, nil, &outBuf, &errBuf, args...) | ||
| stdout = outBuf.String() | ||
| stderr = errBuf.String() | ||
| if runErr != nil { | ||
| var exitErr *exec.ExitError | ||
| if errors.As(runErr, &exitErr) && stderr != "" { | ||
| return stdout, stderr, fmt.Errorf("az %v: %w: %s", args, runErr, stderr) | ||
| } | ||
| return stdout, stderr, runErr | ||
| } | ||
| return stdout, stderr, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.