Skip to content

Commit

Permalink
core: support for configurable ssh port number
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
  • Loading branch information
abiosoft committed Jul 15, 2024
1 parent fd80766 commit f611d7c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
10 changes: 6 additions & 4 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 13 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"port,omitempty"`
ForwardAgent bool `yaml:"forwardAgent,omitempty"`
SSHConfig bool `yaml:"sshConfig,omitempty"` // config generation

// VM
VMType string `yaml:"vmType,omitempty"`
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions embedded/defaults/colima.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions environment/vm/lima/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f611d7c

Please sign in to comment.