1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-23 09:21:56 +01:00

add drone server env command

This commit is contained in:
Brad Rydzewski 2018-03-04 13:59:20 -08:00
parent 334089f7ac
commit 2ebe6e12ea
5 changed files with 146 additions and 4 deletions

4
Gopkg.lock generated

@ -85,7 +85,7 @@
[[projects]]
name = "github.com/drone/drone-go"
packages = ["drone"]
revision = "5b15e044e3275274bc54fc711d88ebf73e2061eb"
revision = "9b0e0d8b9a0e1ee7e24eb12f3d2f617501a1dcd0"
[[projects]]
branch = "master"
@ -216,6 +216,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "47c9239909c8a1ddd8394df9c26e3a3d8a0cfa12997a8ad7273beafde35384be"
inputs-digest = "adf49f8b501fa5d3ee778c63607482e72d073343bf7171e180d8bec26138da91"
solver-name = "gps-cdcl"
solver-version = 1

@ -7,7 +7,7 @@
[[constraint]]
name = "github.com/drone/drone-go"
revision = "5b15e044e3275274bc54fc711d88ebf73e2061eb"
revision = "9b0e0d8b9a0e1ee7e24eb12f3d2f617501a1dcd0"
[[constraint]]
branch = "master"

@ -12,5 +12,6 @@ var Command = cli.Command{
serverOpenCmd,
serverCreateCmd,
serverDestroyCmd,
serverEnvCmd,
},
}

137
drone/server/server_env.go Normal file

@ -0,0 +1,137 @@
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"github.com/urfave/cli"
"github.com/drone/drone-cli/drone/internal"
"github.com/drone/drone-go/drone"
)
var serverEnvCmd = cli.Command{
Name: "env",
ArgsUsage: "<servername>",
Action: serverEnv,
Flags: []cli.Flag{
cli.StringFlag{
Name: "shell",
Usage: "specify the shell [bash, fish]",
Value: "bash",
},
cli.BoolFlag{
Name: "clear",
Usage: "clear cert cache",
},
},
}
func serverEnv(c *cli.Context) error {
u, err := user.Current()
if err != nil {
return err
}
name := c.Args().First()
if len(name) == 0 {
return fmt.Errorf("Missing or invalid server name")
}
home := path.Join(u.HomeDir, ".drone", "certs")
base := path.Join(home, name)
if c.Bool("clean") {
os.RemoveAll(home)
}
server := new(drone.Server)
if _, err := os.Stat(base); err == nil {
data, err := ioutil.ReadFile(path.Join(base, "server.json"))
if err != nil {
return err
}
err = json.Unmarshal(data, server)
if err != nil {
return err
}
} else {
client, err := internal.NewAutoscaleClient(c)
if err != nil {
return err
}
server, err = client.Server(name)
if err != nil {
return err
}
data, err := json.Marshal(server)
if err != nil {
return err
}
err = os.MkdirAll(base, 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(base, "server.json"), data, 0644)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(base, "ca.pem"), server.CACert, 0644)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(base, "cert.pem"), server.TLSCert, 0644)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(base, "key.pem"), server.TLSKey, 0644)
if err != nil {
return err
}
}
switch c.String("shell") {
case "fish":
fmt.Fprintf(os.Stdout, fishf, base, server.Address, server.Name)
case "powershell":
fmt.Fprintf(os.Stdout, powershellf, base, server.Address, server.Name)
default:
fmt.Fprintf(os.Stdout, bashf, base, server.Address, server.Name)
}
return nil
}
var bashf = `
export DOCKER_TLS=1
export DOCKER_TLS_VERIFY=
export DOCKER_CERT_PATH=%q
export DOCKER_HOST=tcp://%s:2376
# Run this command to configure your shell:
# eval "$(drone server env %s)"
`
var fishf = `
sex -x DOCKER_TLS "1";
set -x DOCKER_TLS_VERIFY "";
set -x DOCKER_CERT_PATH %q;
set -x DOCKER_HOST tcp://%s:2376;
# Run this command to configure your shell:
# eval "$(drone server env %s --shell=fish)"
`
var powershellf = `
$Env:DOCKER_TLS = "1"
$Env:DOCKER_TLS_VERIFY = ""
$Env:DOCKER_CERT_PATH = %q
$Env:DOCKER_HOST = "tcp://%s:2376"
# Run this command to configure your shell:
# drone server env %s --shell=powershell | Invoke-Expression
`

@ -152,7 +152,11 @@ type (
Address string `json:"address"`
Capacity int `json:"capacity"`
Secret string `json:"secret"`
Error string `json:"Error"`
Error string `json:"error"`
CAKey []byte `json:"ca_key"`
CACert []byte `json:"ca_cert"`
TLSKey []byte `json:"tls_key"`
TLSCert []byte `json:"tls_cert"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Started int64 `json:"started"`