Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: minor optimization to not fetch qemu packages if they are already installed #393

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion pkg/config/lima_config_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,18 @@ func toggleUserModeEmulationInstallationScript(limaCfg *limayaml.LimaYAML, enabl
Mode: "system",
Script: fmt.Sprintf(`%s
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
Copy link
Contributor

Choose a reason for hiding this comment

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

we have repeat this section multiple times in other files. Is it possible to create as bash function. something like
https://unix.stackexchange.com/questions/599604/create-reusable-functions-bash
Btw, i am not sure whether this is even doable in your case.

if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think the current solution should be 100% fine for this current case, but I think it'd be a little safer to have qemu_pkgs be an array, e.g.

qemu_pkgs=()
qemu_pkgs=("${qemu_pkgs[@]}" "qemu-user-static-aarch64")
dnf install -y --setopt=install_weak_deps=False "${qemu_pkgs[@]}"

just because it allows us to quote the variable expansion.

fi
`, userModeEmulationProvisioningScriptHeader),
})
} else if hasScript && !enabled {
Expand Down
91 changes: 84 additions & 7 deletions pkg/config/lima_config_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@ func TestDiskLimaConfigApplier_Apply(t *testing.T) {
require.Equal(t, "system", limaCfg.Provision[0].Mode)
require.Equal(t, `# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`, limaCfg.Provision[0].Script)
},
want: nil,
Expand Down Expand Up @@ -100,7 +111,18 @@ provision:
script: |
# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`), 0o600)
require.NoError(t, err)
cmd.EXPECT().Output().Return([]byte("13.0.0"), nil)
Expand Down Expand Up @@ -169,7 +191,18 @@ rosetta:
require.Equal(t, "system", limaCfg.Provision[0].Mode)
require.Equal(t, `# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`, limaCfg.Provision[0].Script)
},
want: nil,
Expand Down Expand Up @@ -200,7 +233,18 @@ provision:
script: |
# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`), 0o600)
require.NoError(t, err)
},
Expand All @@ -218,7 +262,18 @@ provision:
require.Equal(t, "system", limaCfg.Provision[0].Mode)
require.Equal(t, `# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`, limaCfg.Provision[0].Script)
},
want: nil,
Expand Down Expand Up @@ -258,7 +313,18 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
require.Equal(t, "system", limaCfg.Provision[0].Mode)
require.Equal(t, `# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`, limaCfg.Provision[0].Script)
},
want: nil,
Expand Down Expand Up @@ -328,7 +394,18 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
require.Equal(t, "system", limaCfg.Provision[0].Mode)
require.Equal(t, `# cross-arch tools
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi

if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`, limaCfg.Provision[0].Script)
},
want: nil,
Expand Down