Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions internal/boxcli/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type serviceUpFlags struct {
background bool
processComposeFile string
processComposeFlags []string
pcport int
}

type serviceStopFlags struct {
Expand All @@ -38,6 +39,8 @@ func (flags *serviceUpFlags) register(cmd *cobra.Command) {
&flags.background, "background", "b", false, "run service in background")
cmd.Flags().StringArrayVar(
&flags.processComposeFlags, "pcflags", []string{}, "pass flags directly to process compose")
cmd.Flags().IntVarP(
&flags.pcport, "pcport", "p", 0, "specify the port for process-compose to use. You can also set the pcport by exporting PC_PORT_NUM")
}

func (flags *serviceStopFlags) register(cmd *cobra.Command) {
Expand Down Expand Up @@ -245,6 +248,10 @@ func startProcessManager(
return err
}

if flags.pcport < 0 {
return errors.Errorf("invalid pcport %d: ports cannot be less than 0", flags.pcport)
}

box, err := devbox.Open(&devopt.Opts{
Dir: servicesFlags.config.path,
Env: env,
Expand All @@ -263,6 +270,7 @@ func startProcessManager(
devopt.ProcessComposeOpts{
Background: flags.background,
ExtraFlags: flags.processComposeFlags,
PCPort: flags.pcport,
},
)
}
1 change: 1 addition & 0 deletions internal/devbox/devopt/devboxopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Opts struct {
type ProcessComposeOpts struct {
ExtraFlags []string
Background bool
PCPort int
}

type GenerateOpts struct {
Expand Down
5 changes: 5 additions & 0 deletions internal/devbox/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devbox
import (
"context"
"fmt"
"strconv"
"text/tabwriter"

"go.jetpack.io/devbox/internal/boxcli/usererr"
Expand Down Expand Up @@ -217,6 +218,9 @@ func (d *Devbox) StartProcessManager(
for _, flag := range processComposeOpts.ExtraFlags {
args = append(args, "--pcflags", flag)
}
if processComposeOpts.PCPort != 0 {
args = append(args, "--pcport", strconv.Itoa(processComposeOpts.PCPort))
}

return d.runDevboxServicesScript(ctx, args)
}
Expand Down Expand Up @@ -257,6 +261,7 @@ func (d *Devbox) StartProcessManager(
BinPath: processComposeBinPath,
Background: processComposeOpts.Background,
ExtraFlags: processComposeOpts.ExtraFlags,
PCPort: processComposeOpts.PCPort,
},
)
}
Expand Down
10 changes: 7 additions & 3 deletions internal/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ProcessComposeOpts struct {
BinPath string
ExtraFlags []string
Background bool
PCPort int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProcesssComposePort

}

func newGlobalProcessComposeConfig() *globalProcessComposeConfig {
Expand Down Expand Up @@ -128,10 +129,13 @@ func StartProcessManager(
config := readGlobalProcessComposeJSON(configFile)
config.File = configFile

// Get the port to use for this project
port, err := getAvailablePort()
port, err := selectPort(processComposeConfig.PCPort)
if err != nil {
return err
return fmt.Errorf("failed to select port: %v", err)
}

if !isPortAvailable(port) {
return fmt.Errorf("port %d is already in use", port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove, call inside selectPort

}

// Start building the process-compose command
Expand Down
31 changes: 31 additions & 0 deletions internal/services/ports.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package services

import (
"fmt"
"net"
"os"
"strconv"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -60,6 +63,34 @@ func getAvailablePort() (int, error) {
return 0, errors.New("no available port")
}

func selectPort(configPort int) (int, error) {
if configPort != 0 {
return configPort, nil
}

if portStr, exists := os.LookupEnv("PC_PORT_NUM"); exists {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on naming this DEVBOX_PC_PORT_NUM

Reasons:

  • If user sets PC_PORT_NUM outside of devbox, it's going to make starting devbox services on multiple projects fail. Since this is the env name set by process compose itself, it's not unreasonable that people will set it.
  • Keeping track of env variables is easier if we prefix them
  • Avoids collisions

port, err := strconv.Atoi(portStr)
if err != nil {
return 0, fmt.Errorf("invalid PC_PORT_NUM environment variable: %v", err)
}
if port <= 0 {
return 0, fmt.Errorf("invalid PC_PORT_NUM environment variable: ports cannot be less than 0")
}
return port, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return port, isPortAvailable(port) so you don't have to call it outside.

}

return getAvailablePort()
}

func isAllowed(port int) bool {
return port > 1024 && disallowedPorts[port] == ""
}

func isPortAvailable(port int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make getAvailablePort call this.

ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return false
}
ln.Close()
return true
}
Loading