Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reuploading unchanged files #352

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/hashicorp/go-version v1.4.0 // indirect
github.com/k0sproject/dig v0.2.0
github.com/k0sproject/rig v0.5.0
github.com/k0sproject/rig v0.6.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
github.com/masterzen/winrm v0.0.0-20211231115050-232efb40349e // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/k0sproject/dig v0.2.0 h1:cNxEIl96g9kqSMfPSZLhpnZ0P8bWXKv08nxvsMHop5w=
github.com/k0sproject/dig v0.2.0/go.mod h1:rBcqaQlJpcKdt2x/OE/lPvhGU50u/e95CSm5g/r4s78=
github.com/k0sproject/rig v0.5.0 h1:rtcX8W+UrU7uxJcBBCpyQNB2JDspyKpqgB+2kCuwp7I=
github.com/k0sproject/rig v0.5.0/go.mod h1:myOVP6gFhWATqL2iOuwKL/2hg90ZkyMqqTssx2fjWjM=
github.com/k0sproject/rig v0.6.0 h1:KpnEeFYguLft95pvF+Z6OsfkthAsFK/Hz30eJWx9chs=
github.com/k0sproject/rig v0.6.0/go.mod h1:myOVP6gFhWATqL2iOuwKL/2hg90ZkyMqqTssx2fjWjM=
github.com/k0sproject/version v0.3.0 h1:6HAn8C29+WVksGCzbQvQ9feEJpUZ0iHD8GebIQMiftQ=
github.com/k0sproject/version v0.3.0/go.mod h1:oEjuz2ItQQtAnGyRgwEV9m5R6/9rjoFC6EiEEzbkFdI=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down Expand Up @@ -471,7 +471,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand Down
22 changes: 19 additions & 3 deletions phase/upload_binaries.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package phase

import (
"fmt"
"os"

"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
"github.com/k0sproject/rig/exec"
Expand Down Expand Up @@ -38,15 +41,28 @@ func (p *UploadBinaries) Run() error {
}

func (p *UploadBinaries) uploadBinary(h *cluster.Host) error {
log.Infof("%s: uploading k0s binary from %s", h, h.UploadBinaryPath)
if err := h.Upload(h.UploadBinaryPath, h.Configurer.K0sBinaryPath(), exec.Sudo(h)); err != nil {
return err
stat, err := os.Stat(h.UploadBinaryPath)
if err != nil {
return fmt.Errorf("failed to stat %s: %w", h.UploadBinaryPath, err)
}
if h.FileChanged(h.UploadBinaryPath, h.Configurer.K0sBinaryPath()) {
log.Infof("%s: uploading k0s binary from %s", h, h.UploadBinaryPath)
if err := h.Upload(h.UploadBinaryPath, h.Configurer.K0sBinaryPath(), exec.Sudo(h)); err != nil {
return err
}
} else {
log.Infof("%s: k0s binary %s already exists on the target and hasn't been changed, skipping upload", h, h.UploadBinaryPath)
}

if err := h.Configurer.Chmod(h, h.Configurer.K0sBinaryPath(), "0700", exec.Sudo(h)); err != nil {
return err
}

log.Debugf("%s: touching %s", h, h.Configurer.K0sBinaryPath())
if err := h.Configurer.Touch(h, h.Configurer.K0sBinaryPath(), stat.ModTime(), exec.Sudo(h)); err != nil {
return fmt.Errorf("failed to touch %s: %w", h.Configurer.K0sBinaryPath(), err)
}

h.Metadata.K0sBinaryVersion = p.Config.Spec.K0s.Version

return nil
Expand Down
17 changes: 15 additions & 2 deletions phase/uploadfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package phase

import (
"fmt"
"os"
"path"

"github.com/alessio/shellescape"
Expand Down Expand Up @@ -107,8 +108,12 @@ func (p *UploadFiles) uploadFile(h *cluster.Host, f *cluster.UploadFile) error {
return err
}

if err := h.Upload(path.Join(f.Base, s.Path), dest, exec.Sudo(h)); err != nil {
return err
if h.FileChanged(src, dest) {
if err := h.Upload(path.Join(f.Base, s.Path), dest, exec.Sudo(h)); err != nil {
return err
}
} else {
log.Infof("%s: file already exists and hasn't been changed, skipping upload", h)
}

if owner != "" {
Expand All @@ -121,6 +126,14 @@ func (p *UploadFiles) uploadFile(h *cluster.Host, f *cluster.UploadFile) error {
if err := h.Configurer.Chmod(h, dest, s.PermMode, exec.Sudo(h)); err != nil {
return err
}
stat, err := os.Stat(src)
if err != nil {
return fmt.Errorf("failed to stat %s: %s", src, err)
}
log.Debugf("%s: touching %s", h, dest)
if err := h.Configurer.Touch(h, dest, stat.ModTime(), exec.Sudo(h)); err != nil {
return fmt.Errorf("failed to touch %s: %w", dest, err)
}
}

return nil
Expand Down
30 changes: 30 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cluster
import (
"encoding/json"
"fmt"
gos "os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -112,6 +113,8 @@ type configurer interface {
TempFile(os.Host) (string, error)
UpdateServiceEnvironment(os.Host, string, map[string]string) error
CleanupServiceEnvironment(os.Host, string) error
Stat(os.Host, string, ...exec.Option) (*os.FileInfo, error)
Touch(os.Host, string, time.Time, ...exec.Option) error
}

// HostMetadata resolved metadata for host
Expand Down Expand Up @@ -494,3 +497,30 @@ func (h *Host) WaitKubeAPIReady(port int) error {
// thus we need to accept both 200 and 401 as valid statuses when checking kube api
return h.WaitHTTPStatus(fmt.Sprintf("https://localhost:%d/version", port), 200, 401)
}

// FileChanged returns true when a remote file has different size or mtime compared to local
// or if an error occurs
func (h *Host) FileChanged(lpath, rpath string) bool {
lstat, err := gos.Stat(lpath)
if err != nil {
log.Debugf("%s: local stat failed: %s", h, err)
return true
}
rstat, err := h.Configurer.Stat(h, rpath, exec.Sudo(h))
if err != nil {
log.Debugf("%s: remote stat failed: %s", h, err)
return true
}

if lstat.Size() != rstat.Size() {
log.Debugf("%s: file sizes for %s differ (%d vs %d)", h, lpath, lstat.Size(), rstat.Size())
return true
}

if !lstat.ModTime().Equal(rstat.ModTime()) {
log.Debugf("%s: file modtimes for %s differ (%s vs %s)", h, lpath, lstat.ModTime(), rstat.ModTime())
return true
}

return false
}