Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions convox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
# Pinned to an immutable release tag. scripts/bump-version.sh sets it to the new
# version (:vX.Y.Z); the release workflow publishes that tag when the v* git tag is pushed.
# :e327add is the v0.1.1 release (identical to :latest when this was pinned).
image: docker.io/docspringcom/rack-gateway:v0.1.33
image: docker.io/docspringcom/rack-gateway:v0.1.34
command: ./scripts/start-gateway.sh
environment:
- PORT=8080
Expand All @@ -54,7 +54,7 @@ services:

admin:
# Uses same image as gateway service (kept in sync by scripts/bump-version.sh)
image: docker.io/docspringcom/rack-gateway:v0.1.33
image: docker.io/docspringcom/rack-gateway:v0.1.34
command: echo "Use this service to run migrations, database admin tasks, etc."
environment:
- PORT=8080
Expand Down
3 changes: 3 additions & 0 deletions docs/src/content/docs/integrations/deploy-approvals/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ pending request can be approved; one that is already approved or deployed counts
# Every listed app's request for the commit, on every listed rack
rack-gateway deploy-approval approve --app web,worker --commit <sha> --rack us,eu

# The same, with one --rack per rack
rack-gateway deploy-approval approve --app web,worker --commit <sha> --rack us --rack eu

# Every app's request for the commit
rack-gateway deploy-approval approve --all-apps --commit <sha> --rack staging
```
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/user-guide/cli/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ These flags work with any command:

| Flag | Short | Description |
|------|-------|-------------|
| `--rack` | `-r` | Override the current rack (deploy approval commands accept a comma-separated list, or `all`) |
| `--rack` | `-r` | Override the current rack. Deploy approval commands take several: repeat `--rack`, give a comma-separated list, or use `all` |
| `--config` | | Override config directory |
| `--api-token` | | Use API token for authentication |
| `--mfa-code` | | Provide MFA code for step-up auth |
Expand Down
3 changes: 2 additions & 1 deletion docs/src/content/docs/user-guide/cli/multi-rack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ rack-gateway logs -a myapp -r eu-prod
```

The `--rack` flag doesn't change your default; it only affects that one command. Deploy approval commands also
take a comma-separated list of racks, or `all`.
take several racks: repeat the flag (`--rack us --rack eu`), separate names with commas (`--rack us,eu`), or use
`all`. Repeated racks count once. Commands that use one rack refuse several, rather than picking one of them.

## Environment Variable Override

Expand Down
6 changes: 6 additions & 0 deletions internal/cli/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ cx deploy-approval approve --app web,worker --commit <40-char sha> --rack us,eu

The `all` value expands to all configured racks in `gateways`, excluding any racks listed in `all_racks_exclude`. This is useful for avoiding local development racks when operating on production infrastructure.

`--rack` is a `rackFlagValue` (`rack_flag.go`): a repeated flag adds to the list instead of replacing it, so
`--rack us --rack eu`, `--rack us,eu` and `--rack us --rack us,eu` all select US and EU (`parseRackList` drops
repeats, keeping the order given). Deploy approval commands read the list through `resolveRacks`. Every other command
uses `SelectedRack`, which refuses several racks: a plain string flag kept only the last `--rack`, so
`approve --rack us --rack eu` once approved EU alone with no warning. Login refuses rack names with commas.

Without `--commit`, deploy approval commands use the full SHA of `HEAD` (`git rev-parse HEAD`): the gateway matches
`--commit` as a prefix, so a short SHA could also match another commit's request. `wait --approve` in a terminal asks
for y/N on every request whose commit isn't exactly the full `--commit` (found by `--branch`, or by a short
Expand Down
19 changes: 15 additions & 4 deletions internal/cli/cli_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func resolveLoginTarget(args []string) (string, string, error) {
return rack, gatewayURL, nil
case 1:
rack := rackArg
if rack == "" {
return "", "", fmt.Errorf("rack name cannot be empty")
if err := checkLoginRackName(rack); err != nil {
return "", "", err
}
gatewayURL, err := resolveLoginGatewayURL(rack)
if err != nil {
Expand All @@ -143,8 +143,8 @@ func resolveLoginTarget(args []string) (string, string, error) {
case 2:
rack := rackArg
gatewayURL := gatewayArg
if rack == "" {
return "", "", fmt.Errorf("rack name cannot be empty")
if err := checkLoginRackName(rack); err != nil {
return "", "", err
}
if gatewayURL == "" {
return "", "", fmt.Errorf("gateway URL cannot be empty")
Expand All @@ -155,6 +155,17 @@ func resolveLoginTarget(args []string) (string, string, error) {
}
}

// checkLoginRackName refuses a rack name that --rack couldn't select: --rack separates rack names with commas.
func checkLoginRackName(rack string) error {
if rack == "" {
return fmt.Errorf("rack name cannot be empty")
}
if strings.Contains(rack, ",") {
return fmt.Errorf("rack name %q can't contain a comma: --rack uses commas to separate racks", rack)
}
return nil
}

func normalizeLoginArgs(args []string) (string, string) {
var rack string
var gatewayURL string
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/deploy_approvals_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ app on every listed rack, shows them, and approves the pending ones: one PIN, th
Requests that are already approved or deployed count as done. If a listed app has no request on a listed rack,
nothing is approved and the command fails.

--rack takes several racks: repeat it (--rack us --rack eu), separate names with commas (--rack us,eu), or use all.

From a terminal it asks for Enter first. Without a terminal (scripts, AI agents) it doesn't, and it needs an
explicit --app list and a full 40-character --commit (no --branch or --all-apps). The macOS PIN dialog then lists
every approval the command makes (branch, app, rack, commit and message) above the command line.
Expand All @@ -66,6 +68,9 @@ Examples:
# Approve two apps' requests for a commit on US and EU (one PIN, one touch per approval)
cx deploy-approval approve --app web,worker --commit <sha> --rack us,eu

# The same, with one --rack per rack
cx deploy-approval approve --app web,worker --commit <sha> --rack us --rack eu

# Approve every app's request for a commit
cx deploy-approval approve --all-apps --commit <sha> --rack staging`,
Args: cobra.MaximumNArgs(1),
Expand Down
4 changes: 3 additions & 1 deletion internal/cli/deploy_approvals_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func newDeployApprovalListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List deploy approval requests",
Long: "List deploy approval requests with optional filtering by status, app and commit.",
Long: `List deploy approval requests with optional filtering by status, app and commit.

--rack takes several racks: repeat it (--rack us --rack eu), separate names with commas (--rack us,eu), or use all.`,
RunE: func(cmd *cobra.Command, _ []string) error {
return executeDeployApprovalList(cmd, opts)
},
Expand Down
43 changes: 17 additions & 26 deletions internal/cli/deploy_approvals_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"os/exec"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -143,28 +144,33 @@ var errGatewayURLWithManyRacks = errors.New(
"RACK_GATEWAY_URL names a single gateway; unset it to use --rack all or a list of racks",
)

// resolveRacks parses the selected rack. Deploy approval commands accept comma-separated rack names
// through the global --rack flag. Special value "all" expands to all configured racks. RACK_GATEWAY_URL names one
// gateway, so it is refused with several racks: each rack's request would go to that one host.
// errAllWithOtherRacks explains why "all" can't be listed with rack names.
var errAllWithOtherRacks = errors.New("--rack all already selects every rack; don't list other racks with it")

// resolveRacks parses the selected racks for deploy approval commands: the global --rack flag, repeated or with
// comma-separated names, or RACK_GATEWAY_RACK (rackSelection). Repeats are dropped, keeping the order given. The
// special value "all" expands to all configured racks. RACK_GATEWAY_URL names one gateway, so it is refused with
// several racks: each rack's request would go to that one host.
func resolveRacks() ([]string, error) {
rack, err := SelectedRack()
selection, err := rackSelection()
if err != nil {
return nil, err
}

trimmed := strings.TrimSpace(rack)
racks, err := parseRackList(selection)
if err != nil {
return nil, err
}
envURL := strings.TrimSpace(os.Getenv("RACK_GATEWAY_URL"))
if trimmed == "all" {
if slices.Contains(racks, "all") {
if len(racks) > 1 {
return nil, errAllWithOtherRacks
}
if envURL != "" {
return nil, errGatewayURLWithManyRacks
}
return expandAllRacks()
}

racks, err := parseRackList(trimmed)
if err != nil {
return nil, err
}
if envURL != "" {
// The rack name is a label for that gateway, and LoadRackAuth checks it against a stored session.
if len(racks) > 1 {
Expand All @@ -178,21 +184,6 @@ func resolveRacks() ([]string, error) {
return racks, nil
}

func parseRackList(raw string) ([]string, error) {
parts := strings.Split(raw, ",")
racks := make([]string, 0, len(parts))
for _, part := range parts {
value := strings.TrimSpace(part)
if value != "" {
racks = append(racks, value)
}
}
if len(racks) == 0 {
return nil, fmt.Errorf("no valid rack names provided")
}
return racks, nil
}

// expandAllRacks returns all configured racks except those in all_racks_exclude.
func expandAllRacks() ([]string, error) {
cfg, _, err := LoadConfig()
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/deploy_approvals_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func newDeployApprovalShowCommand() *cobra.Command {
If no ID is provided, searches for the latest approval request matching the current app and git branch.
Use --branch or --commit to search by specific criteria instead.

--rack takes several racks: repeat it (--rack us --rack eu), separate names with commas (--rack us,eu), or use all.

Examples:
# Show by ID
cx deploy-approval show abc123-def456-...
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/deploy_approvals_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ In a terminal, --approve asks you to confirm (y/N) each request whose commit isn
--branch and no --commit it finds the newest request for the branch whatever its commit, and a short --commit
matches any commit that starts with it.

--rack takes several racks: repeat it (--rack us --rack eu), separate names with commas (--rack us,eu), or use all.

Examples:
# Approve two apps' requests for a commit on staging (one PIN, one touch per approval)
rack-gateway deploy-approval wait --approve --app web,worker --commit <sha> --rack staging`,
Expand Down
21 changes: 20 additions & 1 deletion internal/cli/rack.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,27 @@ func currentRack() (string, error) {
return GetCurrentRack()
}

// SelectedRack determines which rack to use based on flags, env vars, or config
// SelectedRack is the one rack a command uses: --rack, else RACK_GATEWAY_RACK, else the current rack. Several racks
// (a repeated --rack, or comma-separated names) are refused rather than using one of them and ignoring the rest;
// deploy approval commands take several through resolveRacks.
func SelectedRack() (string, error) {
selection, err := rackSelection()
if err != nil || !strings.Contains(selection, ",") {
return selection, err
}
racks, err := parseRackList(selection)
if err != nil {
return "", err
}
if len(racks) > 1 {
return "", fmt.Errorf("this command uses one rack, but several were selected: %s", strings.Join(racks, ", "))
}
return racks[0], nil
}

// rackSelection is the rack selection as given: --rack (several racks are joined with commas), else
// RACK_GATEWAY_RACK, else the current rack.
func rackSelection() (string, error) {
if RackFlag != "" {
return RackFlag, nil
}
Expand Down
64 changes: 64 additions & 0 deletions internal/cli/rack_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cli

import (
"fmt"
"strings"

"github.com/spf13/pflag"
)

const rackFlagHelp = "Rack to use. Deploy approval commands take several: repeat --rack, " +
"give a comma-separated list, or all"

// rackFlagValue is the global --rack flag. A repeated flag adds to the list instead of replacing it, so
// `--rack us --rack eu` selects the same racks as `--rack us,eu`. Deploy approval commands act on each rack
// (resolveRacks), and commands that use one rack refuse a list (SelectedRack) instead of quietly using the last.
type rackFlagValue struct {
value *string
set bool
}

// addRackFlag registers --rack (-r) on flags, storing the selection in target.
func addRackFlag(flags *pflag.FlagSet, target *string) {
*target = ""
flags.VarP(&rackFlagValue{value: target}, "rack", "r", rackFlagHelp)
}

func (r *rackFlagValue) String() string {
if r.value == nil {
return ""
}
return *r.value
}

func (r *rackFlagValue) Set(value string) error {
if r.set {
*r.value += "," + value
} else {
*r.value = value
r.set = true
}
return nil
}

func (*rackFlagValue) Type() string {
return "string"
}

// parseRackList splits a rack selection on commas, dropping blanks and repeats and keeping the order given.
func parseRackList(raw string) ([]string, error) {
parts := strings.Split(raw, ",")
racks := make([]string, 0, len(parts))
seen := make(map[string]bool, len(parts))
for _, part := range parts {
value := strings.TrimSpace(part)
if value != "" && !seen[value] {
seen[value] = true
racks = append(racks, value)
}
}
if len(racks) == 0 {
return nil, fmt.Errorf("no valid rack names provided")
}
return racks, nil
}
Loading