Skip to content

Commit

Permalink
cli: fix ssh config bug for multiple profiles (#675)
Browse files Browse the repository at this point in the history
* cli: fix ssh config bug for multiple profiles

* ci: remove redundant integration tests

* ci: fix integration tests

* ci: revert "ci: fix integration tests"

This reverts commit fdc4ac0.

* cli: fix ssh config regression
  • Loading branch information
abiosoft committed Mar 29, 2023
1 parent ce79bf3 commit 9971eca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 57 deletions.
69 changes: 18 additions & 51 deletions .github/workflows/integration.yml
Expand Up @@ -119,31 +119,22 @@ jobs:
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}

- name: Start Colima
run: colima start --runtime docker
run: colima start --runtime docker --layer

- name: Delay
run: sleep 5

run: sleep 10
- name: Validate Docker
run: docker ps && docker info

- name: Validate DNS
run: colima ssh -- nslookup host.docker.internal
run: colima ssh --layer=false -- nslookup host.docker.internal

- name: Build Image
run: docker build integration

- name: Stop
run: colima stop

- name: Start Colima with Layer
run: colima start --runtime docker --layer

- name: Delay
run: sleep 10

- name: Validate Layer
run: colima ssh -- cat /etc/os-release
run: colima ssh --layer -- cat /etc/os-release

- name: Stop
run: colima stop
Expand Down Expand Up @@ -176,10 +167,10 @@ jobs:
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}

- name: Start Colima
run: colima start --runtime docker --arch aarch64
run: colima start --runtime docker --arch aarch64 --layer

- name: Delay
run: sleep 10
run: sleep 20

- name: Validate Docker
run: docker ps && docker info
Expand All @@ -188,19 +179,10 @@ jobs:
run: docker build integration

- name: Validate DNS
run: colima ssh -- nslookup host.docker.internal

- name: Stop
run: colima stop

- name: Start Colima with Layer
run: colima start --runtime docker --arch aarch64 --layer

- name: Delay
run: sleep 10
run: colima ssh --layer=false -- nslookup host.docker.internal

- name: Validate Layer
run: colima ssh -- cat /etc/os-release
run: colima ssh --layer -- cat /etc/os-release

- name: Stop
run: colima stop
Expand Down Expand Up @@ -233,28 +215,22 @@ jobs:
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}

- name: Start Colima
run: colima start --runtime containerd
run: colima start --runtime containerd --layer

- name: Delay
run: sleep 5
run: sleep 10

- name: Validate Containerd
run: colima nerdctl ps && colima nerdctl info

- name: Validate DNS
run: colima ssh --layer=false -- nslookup host.docker.internal

- name: Build Image
run: colima nerdctl -- build integration

- name: Stop
run: colima stop

- name: Start Colima with Layer
run: colima start --runtime containerd --layer

- name: Delay
run: sleep 10

- name: Validate Layer
run: colima ssh -- cat /etc/os-release
run: colima ssh --layer -- cat /etc/os-release

- name: Stop
run: colima stop
Expand Down Expand Up @@ -287,31 +263,22 @@ jobs:
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}

- name: Start Colima
run: colima start --runtime containerd --arch aarch64

- name: Delay
run: sleep 10
run: colima start --runtime containerd --arch aarch64 --layer

- name: Validate Containerd
run: colima nerdctl ps && colima nerdctl info

- name: Validate DNS
run: colima ssh -- nslookup host.docker.internal
run: colima ssh --layer=false -- nslookup host.docker.internal

- name: Build Image
run: colima nerdctl -- build integration

- name: Stop
run: colima stop

- name: Start Colima with Layer
run: colima start --runtime containerd --arch aarch64 --layer

- name: Delay
run: sleep 10

- name: Validate Layer
run: colima ssh -- cat /etc/os-release
run: colima ssh --layer -- cat /etc/os-release

- name: Stop
run: colima stop
Expand Down
19 changes: 13 additions & 6 deletions environment/vm/lima/limautil/limautil.go
Expand Up @@ -125,13 +125,16 @@ func ShowSSH(profileID string, layer bool, format string) (resp struct {
return resp, nil
}

func replaceSSHCmd(cmd string, name string, ip string, port int) string {
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=") {
s = "ControlPath=" + strconv.Quote(filepath.Join(config.Dir(), "ssh.sock"))
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)
Expand All @@ -144,13 +147,16 @@ func replaceSSHCmd(cmd string, name string, ip string, port int) string {
out = append(out, s)
}

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

return strings.Join(out, " ")
}
func replaceSSHConfig(conf string, name string, ip string, port int) string {
func replaceSSHConfig(conf string, profileID string, ip string, port int) string {
profileID = config.Profile(profileID).ID
name := config.Profile(profileID).ShortName

var out bytes.Buffer
scanner := bufio.NewScanner(strings.NewReader(conf))

Expand All @@ -165,12 +171,13 @@ func replaceSSHConfig(conf string, name string, ip string, port int) string {
line := scanner.Text()

if strings.HasPrefix(line, "Host ") {
line = "Host " + name
line = "Host " + profileID
}

if port > 0 {
if pad, ok := hasPrefix(line, "ControlPath "); ok {
line = pad + "ControlPath " + strconv.Quote(filepath.Join(config.Dir(), "ssh.sock"))
configDir := filepath.Join(filepath.Dir(config.Dir()), name)
line = pad + "ControlPath " + strconv.Quote(filepath.Join(configDir, "ssh.sock"))
}

if pad, ok := hasPrefix(line, "Hostname "); ok {
Expand Down

0 comments on commit 9971eca

Please sign in to comment.