Skip to content

Commit 38e1ecc

Browse files
committed
Add skipIfMissing to copyToHost rule
Instead of creating empty config files. Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent 7e07beb commit 38e1ecc

5 files changed

Lines changed: 16 additions & 11 deletions

File tree

pkg/hostagent/hostagent.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ ln -sf "${SSH_AUTH_SOCK}" /run/host-services/ssh-auth.sock`
696696
// Copy all config files _after_ the requirements are done
697697
for _, rule := range a.instConfig.CopyToHost {
698698
sshAddress, sshPort := a.sshAddressPort()
699-
if err := copyToHost(ctx, a.sshConfig, sshAddress, sshPort, rule.HostFile, rule.GuestFile); err != nil {
699+
if err := copyToHost(ctx, a.sshConfig, sshAddress, sshPort, rule.HostFile, rule.GuestFile, rule.SkipIfMissing); err != nil {
700700
errs = append(errs, err)
701701
}
702702
}
@@ -1266,7 +1266,7 @@ func isDeactivatedCloudInitMainService(line string) bool {
12661266
return strings.HasPrefix(line, "cloud-init-main.service: consumed")
12671267
}
12681268

1269-
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote string) error {
1269+
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote string, skipIfMissing bool) error {
12701270
args := sshConfig.Args()
12711271
args = append(args,
12721272
"-p", strconv.Itoa(sshPort),
@@ -1296,6 +1296,12 @@ func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string
12961296
cmd = exec.CommandContext(ctx, sshConfig.Binary(), sudoArgs...)
12971297
out, err = cmd.Output()
12981298
}
1299+
if errors.As(err, &exitErr) && strings.Contains(string(exitErr.Stderr), "No such file or directory") {
1300+
if skipIfMissing {
1301+
logrus.Infof("Skip missing %s (no such file or directory)", remote)
1302+
return nil
1303+
}
1304+
}
12991305
if err != nil {
13001306
return fmt.Errorf("failed to run %v: %#q: %w", cmd.Args, string(out), err)
13011307
}

pkg/limatype/lima_yaml.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ type PortForward struct {
313313
}
314314

315315
type CopyToHost struct {
316-
GuestFile string `yaml:"guest,omitempty" json:"guest,omitempty"`
317-
HostFile string `yaml:"host,omitempty" json:"host,omitempty"`
318-
DeleteOnStop bool `yaml:"deleteOnStop,omitempty" json:"deleteOnStop,omitempty"`
316+
GuestFile string `yaml:"guest,omitempty" json:"guest,omitempty"`
317+
HostFile string `yaml:"host,omitempty" json:"host,omitempty"`
318+
SkipIfMissing bool `yaml:"skipIfMissing,omitempty" json:"skipIfMissing,omitempty"`
319+
DeleteOnStop bool `yaml:"deleteOnStop,omitempty" json:"deleteOnStop,omitempty"`
319320
}
320321

321322
type Network struct {

templates/default.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,11 @@ networks:
554554
# copyToHost:
555555
# - guest: "/etc/myconfig.cfg"
556556
# host: "{{.Dir}}/copied-from-guest/myconfig"
557+
# # skipIfMissing: false
557558
# # deleteOnStop: false
558559
# # "guest" can include these template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
559560
# # "host" can include {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
561+
# # "skipIfMissing" will skip copying the file from the guest if the file is missing.
560562
# # "deleteOnStop" will delete the file from the host when the instance is stopped.
561563

562564
# Message. Information to be shown to the user, given as a Go template for the instance.

templates/k3s.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ probes:
5252
echo >&2 "k3s is not running yet"
5353
exit 1
5454
fi
55-
{{else}}
56-
# create an empty file so that the "copyToHost" does not fail
57-
sudo mkdir -p /etc/rancher/k3s && sudo touch /etc/rancher/k3s/k3s.yaml
5855
{{end}}
5956
hint: |
6057
The k3s kubeconfig file has not yet been created.
@@ -63,6 +60,7 @@ probes:
6360
copyToHost:
6461
- guest: "/etc/rancher/k3s/k3s.yaml"
6562
host: "{{.Dir}}/copied-from-guest/kubeconfig.yaml"
63+
skipIfMissing: true
6664
deleteOnStop: true
6765
message: |
6866
{{- if not ( and .Param.url .Param.token ) -}}

templates/k8s.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ probes:
204204
echo >&2 "k8s is not running yet"
205205
exit 1
206206
fi
207-
{{else}}
208-
# create an empty file so that the "copyToHost" does not fail
209-
sudo mkdir -p /root/.kube && sudo touch /root/.kube/config.localhost
210207
{{end}}
211208
hint: |
212209
The k8s kubeconfig file has not yet been created.
@@ -230,6 +227,7 @@ probes:
230227
copyToHost:
231228
- guest: "/root/.kube/config.localhost"
232229
host: "{{.Dir}}/copied-from-guest/kubeconfig.yaml"
230+
skipIfMissing: true
233231
deleteOnStop: true
234232
message: |
235233
{{- if not ( and .Param.url .Param.token )}}

0 commit comments

Comments
 (0)