зеркало из https://github.com/docker/compose-cli.git
Add base cli and connection logic
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Родитель
be8e73292c
Коммит
37591c714b
7
Makefile
7
Makefile
|
@ -29,7 +29,10 @@ GOOS ?= $(shell go env GOOS)
|
||||||
|
|
||||||
export GO111MODULE=off
|
export GO111MODULE=off
|
||||||
|
|
||||||
all: protos
|
all: protos example cli
|
||||||
|
|
||||||
|
cli:
|
||||||
|
cd cmd && go build -v -o ../bin/docker
|
||||||
|
|
||||||
protos:
|
protos:
|
||||||
@protobuild --quiet ${PACKAGES}
|
@protobuild --quiet ${PACKAGES}
|
||||||
|
@ -39,4 +42,4 @@ example:
|
||||||
|
|
||||||
FORCE:
|
FORCE:
|
||||||
|
|
||||||
.PHONY: protos example
|
.PHONY: protos example cli
|
||||||
|
|
|
@ -27,21 +27,45 @@
|
||||||
|
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import "google.golang.org/grpc"
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
v1 "github.com/docker/api/backend/v1"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewContext is a context that is canceled when a signal is
|
||||||
|
// sent to the process
|
||||||
|
func NewContext() (context.Context, func()) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
s := make(chan os.Signal)
|
||||||
|
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
go func() {
|
||||||
|
<-s
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
return ctx, cancel
|
||||||
|
}
|
||||||
|
|
||||||
// New returns a GRPC client
|
// New returns a GRPC client
|
||||||
func New(address string) (*Client, error) {
|
func New(address string, timeout time.Duration) (*Client, error) {
|
||||||
conn, err := grpc.Dial(address, grpc.WithInsecure())
|
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(timeout))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Client{
|
return &Client{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
BackendClient: v1.NewBackendClient(conn),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
|
v1.BackendClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
|
|
|
@ -0,0 +1,119 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2019 Docker Inc.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use, copy,
|
||||||
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED,
|
||||||
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE,
|
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||||
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/api/client"
|
||||||
|
"github.com/gogo/protobuf/types"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// initial hack to get the path of the project's bin dir
|
||||||
|
// into the env of this cli for development
|
||||||
|
|
||||||
|
path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/docker/api/bin")
|
||||||
|
if err := os.Setenv("PATH", fmt.Sprintf("$PATH:%s", path)); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var contextCommand = cli.Command{
|
||||||
|
Name: "context",
|
||||||
|
Usage: "manage contexts",
|
||||||
|
Action: func(clix *cli.Context) error {
|
||||||
|
// return information for the current context
|
||||||
|
ctx, cancel := client.NewContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// get our current context
|
||||||
|
ctx = current(ctx)
|
||||||
|
|
||||||
|
client, err := connect(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "cannot connect to backend")
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
info, err := client.BackendInformation(ctx, &types.Empty{})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "fetch backend information")
|
||||||
|
}
|
||||||
|
enc := json.NewEncoder(os.Stdout)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
return enc.Encode(info)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// mock information for getting context
|
||||||
|
// factor out this into a context store package
|
||||||
|
func current(ctx context.Context) context.Context {
|
||||||
|
// test backend address
|
||||||
|
return context.WithValue(ctx, backendAddressKey{}, "127.0.0.1:7654")
|
||||||
|
}
|
||||||
|
|
||||||
|
func connect(ctx context.Context) (*client.Client, error) {
|
||||||
|
address, err := BackendAddress(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "no backend address")
|
||||||
|
}
|
||||||
|
c, err := client.New(address, 500*time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
if err != context.DeadlineExceeded {
|
||||||
|
return nil, errors.Wrap(err, "connect to backend")
|
||||||
|
}
|
||||||
|
// the backend is not running so start it
|
||||||
|
cmd := exec.Command("backend-example", "--address", address)
|
||||||
|
go cmd.Wait()
|
||||||
|
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "start backend")
|
||||||
|
}
|
||||||
|
return client.New(address, 2*time.Second)
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type backendAddressKey struct{}
|
||||||
|
|
||||||
|
func BackendAddress(ctx context.Context) (string, error) {
|
||||||
|
v, ok := ctx.Value(backendAddressKey{}).(string)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("no backend address key")
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2019 Docker Inc.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use, copy,
|
||||||
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED,
|
||||||
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE,
|
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||||
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := cli.NewApp()
|
||||||
|
app.Name = "docker"
|
||||||
|
app.Usage = "Docker for the 2020s"
|
||||||
|
app.UseShortOptionHandling = true
|
||||||
|
app.EnableBashCompletion = true
|
||||||
|
app.Flags = []cli.Flag{
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "debug",
|
||||||
|
Usage: "enable debug output in the logs",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
app.Before = func(clix *cli.Context) error {
|
||||||
|
if clix.GlobalBool("debug") {
|
||||||
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
app.Commands = []cli.Command{
|
||||||
|
contextCommand,
|
||||||
|
}
|
||||||
|
if err := app.Run(os.Args); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,12 +32,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
|
v1 "github.com/docker/api/backend/v1"
|
||||||
|
"github.com/docker/api/client"
|
||||||
"github.com/docker/api/server"
|
"github.com/docker/api/server"
|
||||||
_ "github.com/gogo/googleapis/google/rpc"
|
_ "github.com/gogo/googleapis/google/rpc"
|
||||||
_ "github.com/gogo/protobuf/types"
|
"github.com/gogo/protobuf/types"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
@ -67,7 +67,7 @@ func main() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
app.Action = func(clix *cli.Context) error {
|
app.Action = func(clix *cli.Context) error {
|
||||||
ctx, cancel := cancelContext()
|
ctx, cancel := client.NewContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// create a new GRPC server with the provided server package
|
// create a new GRPC server with the provided server package
|
||||||
|
@ -80,6 +80,12 @@ func main() {
|
||||||
}
|
}
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
|
|
||||||
|
// create our instance of the backend server implementation
|
||||||
|
backend := &backend{}
|
||||||
|
|
||||||
|
// register our instance with the GRPC server
|
||||||
|
v1.RegisterBackendServer(s, backend)
|
||||||
|
|
||||||
// handle context being closed or canceled
|
// handle context being closed or canceled
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
|
@ -98,15 +104,11 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cancelContext is a context that is canceled when a signal is
|
type backend struct {
|
||||||
// sent to the process
|
}
|
||||||
func cancelContext() (context.Context, func()) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
func (b *backend) BackendInformation(ctx context.Context, _ *types.Empty) (*v1.BackendInformationResponse, error) {
|
||||||
s := make(chan os.Signal)
|
return &v1.BackendInformationResponse{
|
||||||
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
|
ID: "com.docker.api.backend.example.v1",
|
||||||
go func() {
|
}, nil
|
||||||
<-s
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
return ctx, cancel
|
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче