Skip to content

Commit

Permalink
vm: use ssh -F for custom ssh sessions (#822)
Browse files Browse the repository at this point in the history
* vm: use ssh -F for custom ssh sessions

* core: update iso images

* vm: temp workaround for cgroups v2

* ci: use macos 13 runners

* vm: revert cgroups v2 workaround

* ssh: fix regression when layer is enabled
  • Loading branch information
abiosoft authored Oct 5, 2023
1 parent f2c91a1 commit dfe8579
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: go test -v ./...

build-macos:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:

binaries-macos:
needs: "build-macos"
runs-on: macos-12
runs-on: macos-13

steps:
- uses: actions/checkout@v3
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ on:

jobs:
kubernetes-docker:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
run: colima delete -f

kubernetes-containerd:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -95,7 +95,7 @@ jobs:
run: colima delete -f

docker:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -143,7 +143,7 @@ jobs:
run: colima delete -f

docker-aarch64:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
run: colima delete -f

containerd:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -239,7 +239,7 @@ jobs:
run: colima delete -f

containerd-aarch64:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3

Expand Down
14 changes: 6 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,21 @@ func (c colimaApp) SSH(layer bool, args ...string) error {
return c.guest.SSH(workDir, args...)
}

resp, err := limautil.ShowSSH(config.CurrentProfile().ID, layer, "args")
resp, err := limautil.ShowSSH(config.CurrentProfile().ID, layer)
if err != nil {
return fmt.Errorf("error getting ssh config: %w", err)
}
if !resp.Layer {
return c.guest.RunInteractive(args...)
}

cmdArgs := resp.Output

if len(args) > 0 {
args = append([]string{"-q", "-t", resp.IPAddress, "--"}, args...)
args = append([]string{"-q", "-t", config.CurrentProfile().ID, "--"}, args...)
} else if workDir != "" {
args = []string{"-q", "-t", resp.IPAddress, "--", "cd " + workDir + " 2> /dev/null; \"$SHELL\" --login"}
args = []string{"-q", "-t", config.CurrentProfile().ID, "--", "cd " + workDir + " 2> /dev/null; \"$SHELL\" --login"}
}

args = append(util.ShellSplit(cmdArgs), args...)
args = append([]string{"-F", resp.File.Colima}, args...)
return cli.CommandInteractive("ssh", args...).Run()
}

Expand Down Expand Up @@ -489,7 +487,7 @@ func generateSSHConfig(modifySSHConfig bool) error {
continue
}

resp, err := limautil.ShowSSH(profile.ID, conf.Layer, "config")
resp, err := limautil.ShowSSH(profile.ID, conf.Layer)
if err != nil {
log.Trace(fmt.Errorf("error retrieving SSH config for '%s': %w", i.Name, err))
continue
Expand All @@ -498,7 +496,7 @@ func generateSSHConfig(modifySSHConfig bool) error {
fmt.Fprintln(&buf, resp.Output)
}

sshFileColima := filepath.Join(filepath.Dir(config.Dir()), "ssh_config")
sshFileColima := config.SSHConfigFile()
if err := os.WriteFile(sshFileColima, buf.Bytes(), 0644); err != nil {
return fmt.Errorf("error writing ssh_config file: %w", err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/ssh-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var sshConfigCmd = &cobra.Command{
Long: `Show configuration of the SSH connection to the VM.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := limautil.ShowSSH(config.CurrentProfile().ID, sshConfigCmdArgs.layer, sshConfigCmdArgs.format)
resp, err := limautil.ShowSSH(config.CurrentProfile().ID, sshConfigCmdArgs.layer)
if err == nil {
fmt.Println(resp.Output)
}
Expand All @@ -25,13 +25,11 @@ var sshConfigCmd = &cobra.Command{
}

var sshConfigCmdArgs struct {
format string
layer bool
layer bool
}

func init() {
root.Cmd().AddCommand(sshConfigCmd)

sshConfigCmd.Flags().StringVarP(&sshConfigCmdArgs.format, "format", "f", "config", "format (config, cmd)")
sshConfigCmd.Flags().BoolVarP(&sshConfigCmdArgs.layer, "layer", "l", true, "config for the Ubuntu layer (if enabled)")
}
3 changes: 3 additions & 0 deletions config/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ func WrapperDir() string { return wrapperDir.Dir() }
const ConfigFileName = "colima.yaml"

func configFile() string { return filepath.Join(configDir.Dir(), ConfigFileName) }

// SSHConfigFile returns the path to generated ssh config.
func SSHConfigFile() string { return filepath.Join(filepath.Dir(configDir.Dir()), "ssh_config") }
78 changes: 32 additions & 46 deletions environment/vm/lima/limautil/limautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ func (i InstanceInfo) Config() (config.Config, error) {

// ShowSSH runs the show-ssh command in Lima.
// returns the ssh output, if in layer, and an error if any
func ShowSSH(profileID string, layer bool, format string) (resp struct {
func ShowSSH(profileID string, layer bool) (resp struct {
Output string
IPAddress string
Layer bool
File struct {
Lima string
Colima string
}
}, err error) {
var buf bytes.Buffer
cmd := cli.Command("limactl", "show-ssh", "--format", format, profileID)
cmd.Stdout = &buf
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
ssh := sshConfig(profileID)
sshConf, err := ssh.Contents()
if err != nil {
return resp, fmt.Errorf("error retrieving ssh config: %w", err)
}

Expand All @@ -109,50 +110,14 @@ func ShowSSH(profileID string, layer bool, format string) (resp struct {
ip = "127.0.0.1"
}

out := buf.String()
switch format {
case "config":
out = replaceSSHConfig(out, profileID, ip, port)
case "cmd", "args":
out = replaceSSHCmd(out, profileID, ip, port)
default:
return resp, fmt.Errorf("unsupported format '%v'", format)
}

resp.Output = out
resp.Output = replaceSSHConfig(sshConf, profileID, ip, port)
resp.IPAddress = ip
resp.Layer = port > 0
resp.File.Lima = ssh.File()
resp.File.Colima = config.SSHConfigFile()
return resp, nil
}

func replaceSSHCmd(cmd string, profileID string, ip string, port int) string {
profileID = config.Profile(profileID).ID
name := config.Profile(profileID).ShortName
var out []string

for _, s := range util.ShellSplit(cmd) {
if port > 0 {
if strings.HasPrefix(s, "ControlPath=") {
configDir := filepath.Join(filepath.Dir(config.Dir()), name)
s = "ControlPath=" + strconv.Quote(filepath.Join(configDir, "ssh.sock"))
}
if strings.HasPrefix(s, "Port=") {
s = "Port=" + strconv.Itoa(port)
}
if strings.HasPrefix(s, "Hostname=") {
s = "Hostname=" + ip
}
}

out = append(out, s)
}

if out[len(out)-1] == "lima-"+profileID {
out[len(out)-1] = ip
}

return strings.Join(out, " ")
}
func replaceSSHConfig(conf string, profileID string, ip string, port int) string {
profileID = config.Profile(profileID).ID
name := config.Profile(profileID).ShortName
Expand Down Expand Up @@ -370,3 +335,24 @@ const colimaDiffDisk = "diffdisk"
func ColimaDiffDisk(profileID string) string {
return filepath.Join(LimaHome(), config.Profile(profileID).ID, colimaDiffDisk)
}

const sshConfigFile = "ssh.config"

// sshConfig is the ssh configuration file for a Colima profile.
type sshConfig string

// Contents returns the content of the SSH config file.
func (s sshConfig) Contents() (string, error) {
profile := config.Profile(string(s))
b, err := os.ReadFile(s.File())
if err != nil {
return "", fmt.Errorf("error retrieving Lima SSH config file for profile '%s': %w", strings.TrimPrefix(profile.DisplayName, "lima"), err)
}
return string(b), nil
}

// File returns the path to the SSH config file.
func (s sshConfig) File() string {
profile := config.Profile(string(s))
return filepath.Join(LimaHome(), profile.ID, sshConfigFile)
}
4 changes: 2 additions & 2 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func newConf(ctx context.Context, conf config.Config) (l Config, err error) {
}

l.Images = append(l.Images,
File{Arch: environment.AARCH64, Location: "https://github.com/abiosoft/alpine-lima/releases/download/colima-v0.5.5/alpine-lima-clm-3.18.0-aarch64.iso", Digest: "sha512:84c93e8aaa09446618bf87daa993e260da69b50e95670aed5df6671b2cff9464810752cbf70f6ee5ddf9d3e1c91d98104b3c573cc024c5f0687ad3f4d2e93ebc"},
File{Arch: environment.X8664, Location: "https://github.com/abiosoft/alpine-lima/releases/download/colima-v0.5.5/alpine-lima-clm-3.18.0-x86_64.iso", Digest: "sha512:f761b807fe9ba345968df72c07f8c5abcae0c4a44976fe5595c0ff748ef693841221a70e663986c700b027cea32b7cac24d5490d4c721593c39f2b8840c362a2"},
File{Arch: environment.AARCH64, Location: "https://github.com/abiosoft/alpine-lima/releases/download/colima-v0.5.6/alpine-lima-clm-3.18.3-aarch64.iso", Digest: "sha512:376cc8cb777380757dbcdb87825f076c25e97283dfef8d51f025fae8bad6953462d095187643a5f7a9be35b86687e0f5b654758fc55ded67aa390f657e0b59b3"},
File{Arch: environment.X8664, Location: "https://github.com/abiosoft/alpine-lima/releases/download/colima-v0.5.6/alpine-lima-clm-3.18.3-x86_64.iso", Digest: "sha512:48bf6c7468fc8acc05d14b3b138958cf4417fa26e478d864c8458a0c7aa8e9742c19058f792debc7585614e0a4ba6ad9608c2e6ff695a2d7ae8daafb8ad64db2"},
)

if conf.CPU > 0 {
Expand Down

0 comments on commit dfe8579

Please sign in to comment.