feat: update pure prompt zsh naming nad other guest os files (#6)

This commit is contained in:
Hari 2026-04-08 18:19:41 -04:00 committed by GitHub
parent 9acbf232eb
commit 9382de7eba
4 changed files with 88 additions and 0 deletions

View file

@ -80,6 +80,9 @@ func (d *Daemon) CreateMachine(ctx context.Context, req contracthost.CreateMachi
if err := injectGuestConfig(ctx, systemVolumePath, guestConfig); err != nil { if err := injectGuestConfig(ctx, systemVolumePath, guestConfig); err != nil {
return nil, err return nil, err
} }
if err := injectMachineIdentity(ctx, systemVolumePath, req.MachineID); err != nil {
return nil, err
}
spec, err := d.buildMachineSpec(req.MachineID, artifact, userVolumes, systemVolumePath) spec, err := d.buildMachineSpec(req.MachineID, artifact, userVolumes, systemVolumePath)
if err != nil { if err != nil {

View file

@ -153,6 +153,20 @@ func TestCreateMachineStagesArtifactsAndPersistsState(t *testing.T) {
if !strings.Contains(authorizedKeys, "daemon-test") { if !strings.Contains(authorizedKeys, "daemon-test") {
t.Fatalf("authorized_keys missing request override key: %q", authorizedKeys) t.Fatalf("authorized_keys missing request override key: %q", authorizedKeys)
} }
machineName, err := readExt4File(runtime.lastSpec.RootFSPath, "/etc/microagent/machine-name")
if err != nil {
t.Fatalf("read injected machine-name: %v", err)
}
if machineName != "vm-1\n" {
t.Fatalf("machine-name mismatch: got %q want %q", machineName, "vm-1\n")
}
hosts, err := readExt4File(runtime.lastSpec.RootFSPath, "/etc/hosts")
if err != nil {
t.Fatalf("read injected hosts: %v", err)
}
if !strings.Contains(hosts, "127.0.1.1 vm-1") {
t.Fatalf("hosts missing machine identity: %q", hosts)
}
artifact, err := fileStore.GetArtifact(context.Background(), response.Machine.Artifact) artifact, err := fileStore.GetArtifact(context.Background(), response.Machine.Artifact)
if err != nil { if err != nil {

View file

@ -296,6 +296,41 @@ func injectGuestConfig(ctx context.Context, imagePath string, config *contractho
return nil return nil
} }
func injectMachineIdentity(ctx context.Context, imagePath string, machineID contracthost.MachineID) error {
machineName := strings.TrimSpace(string(machineID))
if machineName == "" {
return fmt.Errorf("machine_id is required")
}
stagingDir, err := os.MkdirTemp(filepath.Dir(imagePath), "machine-identity-*")
if err != nil {
return fmt.Errorf("create machine identity staging dir: %w", err)
}
defer os.RemoveAll(stagingDir)
identityFiles := map[string]string{
"/etc/microagent/machine-name": machineName + "\n",
"/etc/hostname": machineName + "\n",
"/etc/hosts": fmt.Sprintf(
"127.0.0.1 localhost\n127.0.1.1 %s\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n",
machineName,
),
}
for targetPath, payload := range identityFiles {
sourceName := strings.TrimPrefix(strings.ReplaceAll(targetPath, "/", "_"), "_")
sourcePath := filepath.Join(stagingDir, sourceName)
if err := os.WriteFile(sourcePath, []byte(payload), 0o644); err != nil {
return fmt.Errorf("write machine identity staging file for %q: %w", targetPath, err)
}
if err := replaceExt4File(ctx, imagePath, sourcePath, targetPath); err != nil {
return err
}
}
return nil
}
func replaceExt4File(ctx context.Context, imagePath string, sourcePath string, targetPath string) error { func replaceExt4File(ctx context.Context, imagePath string, sourcePath string, targetPath string) error {
_ = runDebugFS(ctx, imagePath, fmt.Sprintf("rm %s", targetPath)) _ = runDebugFS(ctx, imagePath, fmt.Sprintf("rm %s", targetPath))
if err := runDebugFS(ctx, imagePath, fmt.Sprintf("write %s %s", sourcePath, targetPath)); err != nil { if err := runDebugFS(ctx, imagePath, fmt.Sprintf("write %s %s", sourcePath, targetPath)); err != nil {

View file

@ -58,6 +58,42 @@ func TestInjectGuestConfigWritesAuthorizedKeysAndWebhook(t *testing.T) {
} }
} }
func TestInjectMachineIdentityWritesHostnameFiles(t *testing.T) {
root := t.TempDir()
imagePath := filepath.Join(root, "rootfs.ext4")
if err := buildTestExt4Image(root, imagePath); err != nil {
t.Fatalf("build ext4 image: %v", err)
}
if err := injectMachineIdentity(context.Background(), imagePath, "kiruru"); err != nil {
t.Fatalf("inject machine identity: %v", err)
}
machineName, err := readExt4File(imagePath, "/etc/microagent/machine-name")
if err != nil {
t.Fatalf("read machine-name: %v", err)
}
if machineName != "kiruru\n" {
t.Fatalf("machine-name mismatch: got %q want %q", machineName, "kiruru\n")
}
hostname, err := readExt4File(imagePath, "/etc/hostname")
if err != nil {
t.Fatalf("read hostname: %v", err)
}
if hostname != "kiruru\n" {
t.Fatalf("hostname mismatch: got %q want %q", hostname, "kiruru\n")
}
hosts, err := readExt4File(imagePath, "/etc/hosts")
if err != nil {
t.Fatalf("read hosts: %v", err)
}
if !strings.Contains(hosts, "127.0.1.1 kiruru") {
t.Fatalf("hosts missing machine name: %q", hosts)
}
}
func buildTestExt4Image(root string, imagePath string) error { func buildTestExt4Image(root string, imagePath string) error {
sourceDir := filepath.Join(root, "source") sourceDir := filepath.Join(root, "source")
if err := os.MkdirAll(filepath.Join(sourceDir, "etc", "microagent"), 0o755); err != nil { if err := os.MkdirAll(filepath.Join(sourceDir, "etc", "microagent"), 0o755); err != nil {