context.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright (c) 2019 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package main
  25. import (
  26. "context"
  27. "encoding/json"
  28. "fmt"
  29. "os"
  30. "os/exec"
  31. "path/filepath"
  32. "time"
  33. "github.com/docker/api/client"
  34. "github.com/gogo/protobuf/types"
  35. "github.com/pkg/errors"
  36. "github.com/urfave/cli"
  37. )
  38. func init() {
  39. // initial hack to get the path of the project's bin dir
  40. // into the env of this cli for development
  41. path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/docker/api/bin")
  42. if err := os.Setenv("PATH", fmt.Sprintf("$PATH:%s", path)); err != nil {
  43. panic(err)
  44. }
  45. }
  46. var contextCommand = cli.Command{
  47. Name: "context",
  48. Usage: "manage contexts",
  49. Action: func(clix *cli.Context) error {
  50. // return information for the current context
  51. ctx, cancel := client.NewContext()
  52. defer cancel()
  53. // get our current context
  54. ctx = current(ctx)
  55. client, err := connect(ctx)
  56. if err != nil {
  57. return errors.Wrap(err, "cannot connect to backend")
  58. }
  59. defer client.Close()
  60. info, err := client.BackendInformation(ctx, &types.Empty{})
  61. if err != nil {
  62. return errors.Wrap(err, "fetch backend information")
  63. }
  64. enc := json.NewEncoder(os.Stdout)
  65. enc.SetIndent("", " ")
  66. return enc.Encode(info)
  67. },
  68. }
  69. // mock information for getting context
  70. // factor out this into a context store package
  71. func current(ctx context.Context) context.Context {
  72. // test backend address
  73. return context.WithValue(ctx, backendAddressKey{}, "127.0.0.1:7654")
  74. }
  75. func connect(ctx context.Context) (*client.Client, error) {
  76. address, err := BackendAddress(ctx)
  77. if err != nil {
  78. return nil, errors.Wrap(err, "no backend address")
  79. }
  80. c, err := client.New(address, 500*time.Millisecond)
  81. if err != nil {
  82. if err != context.DeadlineExceeded {
  83. return nil, errors.Wrap(err, "connect to backend")
  84. }
  85. // the backend is not running so start it
  86. cmd := exec.Command("backend-example", "--address", address)
  87. go cmd.Wait()
  88. if err := cmd.Start(); err != nil {
  89. return nil, errors.Wrap(err, "start backend")
  90. }
  91. return client.New(address, 2*time.Second)
  92. }
  93. return c, nil
  94. }
  95. type backendAddressKey struct{}
  96. func BackendAddress(ctx context.Context) (string, error) {
  97. v, ok := ctx.Value(backendAddressKey{}).(string)
  98. if !ok {
  99. return "", errors.New("no backend address key")
  100. }
  101. return v, nil
  102. }