module.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "net"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "github.com/ethereum/go-ethereum/log"
  26. )
  27. var (
  28. // ErrServiceUnknown is returned when a service container doesn't exist.
  29. ErrServiceUnknown = errors.New("service unknown")
  30. // ErrServiceOffline is returned when a service container exists, but it is not
  31. // running.
  32. ErrServiceOffline = errors.New("service offline")
  33. // ErrServiceUnreachable is returned when a service container is running, but
  34. // seems to not respond to communication attempts.
  35. ErrServiceUnreachable = errors.New("service unreachable")
  36. // ErrNotExposed is returned if a web-service doesn't have an exposed port, nor
  37. // a reverse-proxy in front of it to forward requests.
  38. ErrNotExposed = errors.New("service not exposed, nor proxied")
  39. )
  40. // containerInfos is a heavily reduced version of the huge inspection dataset
  41. // returned from docker inspect, parsed into a form easily usable by puppeth.
  42. type containerInfos struct {
  43. running bool // Flag whether the container is running currently
  44. envvars map[string]string // Collection of environmental variables set on the container
  45. portmap map[string]int // Port mapping from internal port/proto combos to host binds
  46. volumes map[string]string // Volume mount points from container to host directories
  47. }
  48. // inspectContainer runs docker inspect against a running container
  49. func inspectContainer(client *sshClient, container string) (*containerInfos, error) {
  50. // Check whether there's a container running for the service
  51. out, err := client.Run(fmt.Sprintf("docker inspect %s", container))
  52. if err != nil {
  53. return nil, ErrServiceUnknown
  54. }
  55. // If yes, extract various configuration options
  56. type inspection struct {
  57. State struct {
  58. Running bool
  59. }
  60. Mounts []struct {
  61. Source string
  62. Destination string
  63. }
  64. Config struct {
  65. Env []string
  66. }
  67. HostConfig struct {
  68. PortBindings map[string][]map[string]string
  69. }
  70. }
  71. var inspects []inspection
  72. if err = json.Unmarshal(out, &inspects); err != nil {
  73. return nil, err
  74. }
  75. inspect := inspects[0]
  76. // Infos retrieved, parse the above into something meaningful
  77. infos := &containerInfos{
  78. running: inspect.State.Running,
  79. envvars: make(map[string]string),
  80. portmap: make(map[string]int),
  81. volumes: make(map[string]string),
  82. }
  83. for _, envvar := range inspect.Config.Env {
  84. if parts := strings.Split(envvar, "="); len(parts) == 2 {
  85. infos.envvars[parts[0]] = parts[1]
  86. }
  87. }
  88. for portname, details := range inspect.HostConfig.PortBindings {
  89. if len(details) > 0 {
  90. port, _ := strconv.Atoi(details[0]["HostPort"])
  91. infos.portmap[portname] = port
  92. }
  93. }
  94. for _, mount := range inspect.Mounts {
  95. infos.volumes[mount.Destination] = mount.Source
  96. }
  97. return infos, err
  98. }
  99. // tearDown connects to a remote machine via SSH and terminates docker containers
  100. // running with the specified name in the specified network.
  101. func tearDown(client *sshClient, network string, service string, purge bool) ([]byte, error) {
  102. // Tear down the running (or paused) container
  103. out, err := client.Run(fmt.Sprintf("docker rm -f %s_%s_1", network, service))
  104. if err != nil {
  105. return out, err
  106. }
  107. // If requested, purge the associated docker image too
  108. if purge {
  109. return client.Run(fmt.Sprintf("docker rmi %s/%s", network, service))
  110. }
  111. return nil, nil
  112. }
  113. // resolve retrieves the hostname a service is running on either by returning the
  114. // actual server name and port, or preferably an nginx virtual host if available.
  115. func resolve(client *sshClient, network string, service string, port int) (string, error) {
  116. // Inspect the service to get various configurations from it
  117. infos, err := inspectContainer(client, fmt.Sprintf("%s_%s_1", network, service))
  118. if err != nil {
  119. return "", err
  120. }
  121. if !infos.running {
  122. return "", ErrServiceOffline
  123. }
  124. // Container online, extract any environmental variables
  125. if vhost := infos.envvars["VIRTUAL_HOST"]; vhost != "" {
  126. return vhost, nil
  127. }
  128. return fmt.Sprintf("%s:%d", client.server, port), nil
  129. }
  130. // checkPort tries to connect to a remote host on a given
  131. func checkPort(host string, port int) error {
  132. log.Trace("Verifying remote TCP connectivity", "server", host, "port", port)
  133. conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), time.Second)
  134. if err != nil {
  135. return err
  136. }
  137. conn.Close()
  138. return nil
  139. }