From 649a6e4291b46d7afdc67aad812a0874ec91c387 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 7 Sep 2022 23:58:30 -0700 Subject: [PATCH] Dropgz deploy cmd - Clearing the dest before writing into it to avoid race condition --- dropgz/README.md | 10 ++++++++++ dropgz/pkg/embed/payload.go | 16 ++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 dropgz/README.md diff --git a/dropgz/README.md b/dropgz/README.md new file mode 100644 index 0000000000..7ca59da397 --- /dev/null +++ b/dropgz/README.md @@ -0,0 +1,10 @@ +### Running the dropgz locally + +Select the file(for example azure-ipam binary) you want to deploy using the dropgz. + +1. Copy the file (i.e azure-ipam) to the directory `/dropgz/pkg/embed/fs` +2. Add the sha of the file to the sum.txt file.(`sha256sum * > sum.txt`) +3. You need to gzip the file, so run the cmd `gzip --verbose --best --recursive azure-ipam` and rename the output .gz file to original file name. +4. Do the step 3 for `sum.txt` file as well. +5. go to dropgz directory and build it. (`go build .`) +6. You can now test the dropgz command locally. (`./dropgz deploy azure-ipam -o ./azure-ipam`) \ No newline at end of file diff --git a/dropgz/pkg/embed/payload.go b/dropgz/pkg/embed/payload.go index 7e5341a593..952d65da26 100644 --- a/dropgz/pkg/embed/payload.go +++ b/dropgz/pkg/embed/payload.go @@ -15,8 +15,9 @@ import ( ) const ( - cwd = "fs" - pathPrefix = cwd + string(filepath.Separator) + cwd = "fs" + pathPrefix = cwd + string(filepath.Separator) + OLD_FILE_SUFFIX = ".old" ) var ErrArgsMismatched = errors.New("mismatched argument count") @@ -86,6 +87,17 @@ func deploy(src, dest string) error { return err } defer rc.Close() + if _, err := os.Stat(dest); err == nil || !os.IsNotExist(err) { + old_file_dest := dest + OLD_FILE_SUFFIX + if err = os.RemoveAll(old_file_dest); err != nil { + return errors.Wrapf(err, "not able to remove the %s", &old_file_dest) + } + if err = os.Rename(dest, old_file_dest); err != nil { + return errors.Wrapf(err, "not able to rename the %s to %s", dest, old_file_dest) + } + } else { + return err + } target, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755) //nolint:gomnd // executable file bitmask if err != nil { return errors.Wrapf(err, "failed to create file %s", dest)