From dbafc98217cdcedae38144ba91afc27eb353ece5 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 11 Nov 2020 17:46:10 -0500 Subject: [PATCH] added native copy command for multi-arch support --- command/command.go | 1 + command/copy.go | 74 +++++++++++++++++++++++++++++++++++++ engine/compiler/compiler.go | 4 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 command/copy.go diff --git a/command/command.go b/command/command.go index 6fa9212..762346d 100644 --- a/command/command.go +++ b/command/command.go @@ -25,6 +25,7 @@ func Command() { app := kingpin.New("drone", "drone docker runner") registerCompile(app) registerExec(app) + registerCopy(app) daemon.Register(app) kingpin.Version(version) diff --git a/command/copy.go b/command/copy.go new file mode 100644 index 0000000..9a06138 --- /dev/null +++ b/command/copy.go @@ -0,0 +1,74 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Polyform License +// that can be found in the LICENSE file. + +package command + +import ( + "io" + "os" + + "gopkg.in/alecthomas/kingpin.v2" +) + +type copyCommand struct { + source string + target string +} + +func (c *copyCommand) run(*kingpin.ParseContext) error { + return Copy(c.source, c.target) +} + +func Copy(src, dst string) error { + in, err := os.Open(src) + if err != nil { + return err + } + defer in.Close() + + out, err := os.Create(dst) + if err != nil { + return err + } + defer out.Close() + + _, err = io.Copy(out, in) + if err != nil { + return err + } + + err = out.Sync() + if err != nil { + return err + } + + info, err := os.Stat(src) + if err != nil { + return err + } + + err = os.Chmod(dst, info.Mode()) + if err != nil { + return err + } + + return out.Close() +} + +// Register registers the copy command. +func registerCopy(app *kingpin.Application) { + c := new(copyCommand) + + cmd := app.Command("copy", "entrypoint copy"). + Hidden(). + Action(c.run) + + cmd.Flag("source", "source binary path"). + Default("/bin/tmate"). + StringVar(&c.source) + + cmd.Flag("target", "target binary path"). + Default("/usr/drone/bin/tmate"). + StringVar(&c.target) +} diff --git a/engine/compiler/compiler.go b/engine/compiler/compiler.go index 3a4245e..69a9aaa 100644 --- a/engine/compiler/compiler.go +++ b/engine/compiler/compiler.go @@ -327,8 +327,8 @@ func (c *Compiler) Compile(ctx context.Context, args runtime.CompilerArgs) runti Labels: labels, Pull: engine.PullIfNotExists, Image: image.Expand(c.Tmate.Image), - Entrypoint: []string{"/bin/sh", "-c"}, - Command: []string{"cp /bin/tmate /usr/drone/bin/"}, + Entrypoint: []string{"/bin/drone-runner-docker"}, + Command: []string{"copy"}, Network: "none", })