Skip to content

Commit f625177

Browse files
authored
Generic way to upload files to hosts (#109)
* Generic way to upload files to hosts Signed-off-by: Jussi Nummelin <[email protected]> * Use explicit dstDir attribute for file upload destination Signed-off-by: Jussi Nummelin <[email protected]> * bump k0s to 0.12.1-rc.1 Signed-off-by: Jussi Nummelin <[email protected]>
1 parent 57db321 commit f625177

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,24 @@ environment:
217217
HTTP_PROXY: 10.0.0.1:443
218218
```
219219

220+
###### `spec.hosts[*].files` &lt;sequence&gt; (optional)
221+
222+
List of files to be uploaded to the host.
223+
224+
Example:
225+
226+
```yaml
227+
- name: image-bundle
228+
src: airgap-images.tgz
229+
dstDir: /var/lib/k0s/images/
230+
perm: 0700
231+
```
232+
233+
* `name`: name of the file "bundle", used only for logging purposes (optional)
234+
* `src`: [Glob pattern](https://golang.org/pkg/path/filepath/#Match) to match files to be uploaded
235+
* `dstDir`: Destination directory for the file(s). `k0sctl` will create full directory structure if it does not already exist on the host.
236+
* `perm`: File permission mode for uploaded file(s) and created directories
237+
220238
##### `spec.hosts[*].ssh` &lt;mapping&gt; (optional)
221239

222240
SSH connection options.

cmd/apply.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var applyCommand = &cli.Command{
6464
&phase.DownloadBinaries{},
6565
&phase.UploadBinaries{},
6666
&phase.DownloadK0s{},
67+
&phase.UploadFiles{},
6768
&phase.ConfigureK0s{},
6869
&phase.InitializeK0s{},
6970
&phase.InstallControllers{},

config/cluster/host.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Host struct {
2626
UploadBinary bool `yaml:"uploadBinary,omitempty"`
2727
K0sBinaryPath string `yaml:"k0sBinaryPath,omitempty"`
2828
InstallFlags Flags `yaml:"installFlags,omitempty"`
29+
Files []UploadFile `yaml:"files,omitempty"`
2930

3031
Metadata HostMetadata `yaml:"-"`
3132
Configurer configurer `yaml:"-"`

config/cluster/uploadfile.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cluster
2+
3+
import (
4+
"path/filepath"
5+
)
6+
7+
// UploadFile describes a file to be uploaded for the host
8+
type UploadFile struct {
9+
Name string `yaml:"name,omitempty"`
10+
Source string `yaml:"src" validate:"required"`
11+
DestinationDir string `yaml:"dstDir" validate:"required"`
12+
PermMode string `yaml:"perm" default:"0755"`
13+
}
14+
15+
func (u *UploadFile) Resolve() ([]string, error) {
16+
sources, err := filepath.Glob(u.Source)
17+
if err != nil {
18+
return nil, err
19+
}
20+
return sources, nil
21+
}

phase/uploadfiles.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package phase
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/k0sproject/k0sctl/config"
7+
"github.com/k0sproject/k0sctl/config/cluster"
8+
9+
log "github.com/sirupsen/logrus"
10+
)
11+
12+
// UploadFiles implements a phase which upload files to hosts
13+
type UploadFiles struct {
14+
GenericPhase
15+
16+
hosts cluster.Hosts
17+
}
18+
19+
// Title for the phase
20+
func (p *UploadFiles) Title() string {
21+
return "Upload files to hosts"
22+
}
23+
24+
// Prepare the phase
25+
func (p *UploadFiles) Prepare(config *config.Cluster) error {
26+
p.Config = config
27+
p.hosts = p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool {
28+
return len(h.Files) > 0
29+
})
30+
31+
return nil
32+
}
33+
34+
// ShouldRun is true when there are workers
35+
func (p *UploadFiles) ShouldRun() bool {
36+
return len(p.hosts) > 0
37+
}
38+
39+
// Run the phase
40+
func (p *UploadFiles) Run() error {
41+
return p.Config.Spec.Hosts.ParallelEach(p.uploadFiles)
42+
}
43+
44+
func (p *UploadFiles) uploadFiles(h *cluster.Host) error {
45+
for _, f := range h.Files {
46+
log.Infof("%s: starting to upload %s", h, f.Name)
47+
files, err := f.Resolve()
48+
if err != nil {
49+
return err
50+
}
51+
52+
if err := h.Execf("install -d %s -m %s", f.DestinationDir, f.PermMode); err != nil {
53+
return err
54+
}
55+
56+
for _, file := range files {
57+
log.Debugf("%s: uploading %s to %s", h, file, f.DestinationDir)
58+
destination := filepath.Join(f.DestinationDir, filepath.Base(file))
59+
60+
if err := h.Upload(file, destination); err != nil {
61+
return err
62+
}
63+
64+
if err := h.Configurer.Chmod(h, destination, f.PermMode); err != nil {
65+
return err
66+
}
67+
}
68+
log.Infof("%s: %s upload done", h, f.Name)
69+
}
70+
return nil
71+
}

smoke-test/k0sctl.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec:
1515
port: 9023
1616
keyPath: ./id_rsa_k0s
1717
k0s:
18-
version: "0.11.0"
18+
version: "0.12.1-rc.1"
1919
config:
2020
images:
2121
konnectivity:

0 commit comments

Comments
 (0)