Skip to content

Commit f4e50c2

Browse files
authored
Merge pull request #2817 from AkihiroSuda/fix-2813-alt
Allow "." and "_" in instance names, disallow them in hostnames
2 parents 6a798c3 + d22d643 commit f4e50c2

File tree

16 files changed

+65
-26
lines changed

16 files changed

+65
-26
lines changed

cmd/limactl/guessarg/guessarg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func InstNameFromURL(urlStr string) (string, error) {
5656
func InstNameFromYAMLPath(yamlPath string) (string, error) {
5757
s := strings.ToLower(filepath.Base(yamlPath))
5858
s = strings.TrimSuffix(strings.TrimSuffix(s, ".yml"), ".yaml")
59-
s = strings.ReplaceAll(s, ".", "-")
59+
// "." is allowed in instance names, but replaced to "-" for hostnames.
60+
// e.g., yaml: "ubuntu-24.04.yaml" , instance name: "ubuntu-24.04", hostname: "lima-ubuntu-24-04"
6061
if err := identifiers.Validate(s); err != nil {
6162
return "", fmt.Errorf("filename %q is invalid: %w", yamlPath, err)
6263
}

cmd/limactl/show-ssh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
8686
}
8787
return err
8888
}
89-
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s lima-%s`.",
90-
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Name)
89+
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s %s`.",
90+
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
9191
opts, err := sshutil.SSHOpts(
9292
inst.Dir,
9393
*inst.Config.SSH.LoadDotSSHPubKeys,

cmd/limactl/tunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
146146
default:
147147
fmt.Fprintf(stdout, "Set `ALL_PROXY=socks5h://127.0.0.1:%d`, etc.\n", port)
148148
}
149-
fmt.Fprintf(stdout, "The instance can be connected from the host as <http://lima-%s.internal> via a web browser.\n", inst.Name)
149+
fmt.Fprintf(stdout, "The instance can be connected from the host as <http://%s.internal> via a web browser.\n", inst.Hostname)
150150

151151
// TODO: show the port in `limactl list --json` ?
152152
// TODO: add `--stop` flag to shut down the tunnel
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
instance-id: {{.IID}}
2-
local-hostname: lima-{{.Name}}
2+
local-hostname: {{.Hostname}}

pkg/cidata/cidata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/docker/go-units"
1919
"github.com/lima-vm/lima/pkg/debugutil"
20+
"github.com/lima-vm/lima/pkg/identifierutil"
2021
"github.com/lima-vm/lima/pkg/iso9660util"
2122
"github.com/lima-vm/lima/pkg/limayaml"
2223
"github.com/lima-vm/lima/pkg/localpathutil"
@@ -128,6 +129,7 @@ func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.L
128129
Debug: debugutil.Debug,
129130
BootScripts: bootScripts,
130131
Name: name,
132+
Hostname: identifierutil.HostnameFromInstName(name), // TODO: support customization
131133
User: u.Username,
132134
UID: uid,
133135
Home: fmt.Sprintf("/home/%s.linux", u.Username),

pkg/cidata/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Disk struct {
5656
type TemplateArgs struct {
5757
Debug bool
5858
Name string // instance name
59+
Hostname string // instance hostname
5960
IID string // instance id
6061
User string // user name
6162
Home string // home directory

pkg/hostagent/hostagent.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
hostagentapi "github.com/lima-vm/lima/pkg/hostagent/api"
2929
"github.com/lima-vm/lima/pkg/hostagent/dns"
3030
"github.com/lima-vm/lima/pkg/hostagent/events"
31+
"github.com/lima-vm/lima/pkg/identifierutil"
3132
"github.com/lima-vm/lima/pkg/limayaml"
3233
"github.com/lima-vm/lima/pkg/networks"
3334
"github.com/lima-vm/lima/pkg/osutil"
@@ -288,7 +289,8 @@ func (a *HostAgent) Run(ctx context.Context) error {
288289
if limayaml.FirstUsernetIndex(a.instConfig) == -1 && *a.instConfig.HostResolver.Enabled {
289290
hosts := a.instConfig.HostResolver.Hosts
290291
hosts["host.lima.internal"] = networks.SlirpGateway
291-
hosts[fmt.Sprintf("lima-%s", a.instName)] = networks.SlirpIPAddress
292+
hostname := identifierutil.HostnameFromInstName(a.instName) // TODO: support customization
293+
hosts[hostname] = networks.SlirpIPAddress
292294
srvOpts := dns.ServerOptions{
293295
UDPPort: a.udpDNSLocalPort,
294296
TCPPort: a.tcpDNSLocalPort,

pkg/identifierutil/identifierutil.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package identifierutil
2+
3+
import "strings"
4+
5+
func HostnameFromInstName(instName string) string {
6+
s := strings.ReplaceAll(instName, ".", "-")
7+
s = strings.ReplaceAll(s, "_", "-")
8+
return "lima-" + s
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package identifierutil
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestHostnameFromInstName(t *testing.T) {
10+
assert.Equal(t, "lima-default", HostnameFromInstName("default"))
11+
assert.Equal(t, "lima-ubuntu-24-04", HostnameFromInstName("ubuntu-24.04"))
12+
assert.Equal(t, "lima-foo-bar-baz", HostnameFromInstName("foo_bar.baz"))
13+
}

pkg/instance/ansible.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func runAnsiblePlaybook(ctx context.Context, inst *store.Instance, playbook stri
4141
func createAnsibleInventory(inst *store.Instance) (string, error) {
4242
vars := map[string]interface{}{
4343
"ansible_connection": "ssh",
44-
"ansible_host": "lima-" + inst.Name,
44+
"ansible_host": inst.Hostname,
4545
"ansible_ssh_common_args": "-F " + inst.SSHConfigFile,
4646
}
4747
hosts := map[string]interface{}{

0 commit comments

Comments
 (0)