From 8d349191294f98d63b6ef5e2463c67125e84fd1d Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Mon, 15 Jul 2024 18:58:05 +0100 Subject: [PATCH] core: support for configurable ssh port number (#1066) Signed-off-by: Abiola Ibrahim --- cmd/start.go | 10 ++++++---- config/config.go | 25 +++++++++++++------------ embedded/defaults/colima.yaml | 6 ++++++ environment/vm/lima/dependencies.go | 4 ++-- environment/vm/lima/yaml.go | 2 +- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cmd/start.go b/cmd/start.go index 5f261ba5c..70322fe79 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -175,11 +175,10 @@ func init() { startCmd.Flags().StringVar(&startCmdArgs.MountType, "mount-type", defaultMountTypeQEMU, "volume driver for the mount ("+mounts+")") startCmd.Flags().BoolVar(&startCmdArgs.MountINotify, "mount-inotify", true, "propagate inotify file events to the VM") - // ssh agent + // ssh startCmd.Flags().BoolVarP(&startCmdArgs.ForwardAgent, "ssh-agent", "s", false, "forward SSH agent to the VM") - - // ssh config generation startCmd.Flags().BoolVar(&startCmdArgs.SSHConfig, "ssh-config", true, "generate SSH config in ~/.ssh/config") + startCmd.Flags().IntVar(&startCmdArgs.SSHPort, "ssh-port", 0, "SSH server port") // k8s startCmd.Flags().BoolVarP(&startCmdArgs.Kubernetes.Enabled, "kubernetes", "k", false, "start with Kubernetes") @@ -241,7 +240,7 @@ func mountsFromFlag(mounts []string) []config.Mount { func setDefaults(cmd *cobra.Command) { if startCmdArgs.VMType == "" { startCmdArgs.VMType = defaultVMType - } + } if util.MacOS13OrNewer() { // changing to vz implies changing mount type to virtiofs @@ -378,6 +377,9 @@ func prepareConfig(cmd *cobra.Command) { if !cmd.Flag("ssh-config").Changed { startCmdArgs.SSHConfig = current.SSHConfig } + if !cmd.Flag("ssh-port").Changed { + startCmdArgs.SSHPort = current.SSHPort + } if !cmd.Flag("dns").Changed { startCmdArgs.Network.DNSResolvers = current.Network.DNSResolvers } diff --git a/config/config.go b/config/config.go index 0e6b21bb9..5a20f6b36 100644 --- a/config/config.go +++ b/config/config.go @@ -69,15 +69,19 @@ var ( // Config is the application config. type Config struct { - CPU int `yaml:"cpu,omitempty"` - Disk int `yaml:"disk,omitempty"` - Memory int `yaml:"memory,omitempty"` - Arch string `yaml:"arch,omitempty"` - CPUType string `yaml:"cpuType,omitempty"` - ForwardAgent bool `yaml:"forwardAgent,omitempty"` - Network Network `yaml:"network,omitempty"` - Env map[string]string `yaml:"env,omitempty"` // environment variables - Hostname string `yaml:"hostname"` + CPU int `yaml:"cpu,omitempty"` + Disk int `yaml:"disk,omitempty"` + Memory int `yaml:"memory,omitempty"` + Arch string `yaml:"arch,omitempty"` + CPUType string `yaml:"cpuType,omitempty"` + Network Network `yaml:"network,omitempty"` + Env map[string]string `yaml:"env,omitempty"` // environment variables + Hostname string `yaml:"hostname"` + + // SSH + SSHPort int `yaml:"sshPort,omitempty"` + ForwardAgent bool `yaml:"forwardAgent,omitempty"` + SSHConfig bool `yaml:"sshConfig,omitempty"` // config generation // VM VMType string `yaml:"vmType,omitempty"` @@ -100,9 +104,6 @@ type Config struct { // provision scripts Provision []Provision `yaml:"provision,omitempty"` - - // SSH config generation - SSHConfig bool `yaml:"sshConfig,omitempty"` } // Kubernetes is kubernetes configuration diff --git a/embedded/defaults/colima.yaml b/embedded/defaults/colima.yaml index b16ac0bad..335a07757 100644 --- a/embedded/defaults/colima.yaml +++ b/embedded/defaults/colima.yaml @@ -157,6 +157,12 @@ provision: [] # Default: true sshConfig: true +# The port number for the SSH server for the virtual machine. +# When set to 0, a random available port is used. +# +# Default: 0 +sshPort: 0 + # Configure volume mounts for the virtual machine. # Colima mounts user's home directory by default to provide a familiar # user experience. diff --git a/environment/vm/lima/dependencies.go b/environment/vm/lima/dependencies.go index 5a8e71f56..dd250f369 100644 --- a/environment/vm/lima/dependencies.go +++ b/environment/vm/lima/dependencies.go @@ -15,7 +15,7 @@ import ( // cacheDependencies downloads the ubuntu deb files to a path on the host. // The return value is the directory of the downloaded deb files. -func (l *limaVM) cacheDependencies(src deb.URISource, log *logrus.Entry, conf config.Config) (string, error) { +func (l *limaVM) cacheDependencies(src deb.URISource, conf config.Config) (string, error) { codename, err := l.RunOutput("sh", "-c", `grep "^UBUNTU_CODENAME" /etc/os-release | cut -d= -f2`) if err != nil { return "", fmt.Errorf("error retrieving OS version from vm: %w", err) @@ -80,7 +80,7 @@ func (l *limaVM) installDependencies(log *logrus.Entry, conf config.Config) erro } // cache dependencies - dir, err := l.cacheDependencies(src, log, conf) + dir, err := l.cacheDependencies(src, conf) if err != nil { log.Warnln(fmt.Errorf("error caching dependencies for %s: %w", src.Name(), err)) log.Warnln("falling back to normal package install") diff --git a/environment/vm/lima/yaml.go b/environment/vm/lima/yaml.go index 9a25fad02..2a6a35cb6 100644 --- a/environment/vm/lima/yaml.go +++ b/environment/vm/lima/yaml.go @@ -71,7 +71,7 @@ func newConf(ctx context.Context, conf config.Config) (l Config, err error) { if conf.Disk > 0 { l.Disk = fmt.Sprintf("%dGiB", conf.Disk) } - l.SSH = SSH{LocalPort: 0, LoadDotSSHPubKeys: false, ForwardAgent: conf.ForwardAgent} + l.SSH = SSH{LocalPort: conf.SSHPort, LoadDotSSHPubKeys: false, ForwardAgent: conf.ForwardAgent} l.Containerd = Containerd{System: false, User: false} l.DNS = conf.Network.DNSResolvers