|
| 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 | +} |
0 commit comments