aci.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package azure
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  11. "github.com/Azure/go-autorest/autorest"
  12. "github.com/Azure/go-autorest/autorest/to"
  13. tm "github.com/buger/goterm"
  14. "github.com/gobwas/ws"
  15. "github.com/gobwas/ws/wsutil"
  16. "github.com/pkg/errors"
  17. "github.com/docker/api/azure/login"
  18. "github.com/docker/api/context/store"
  19. )
  20. const aciDockerUserAgent = "docker-cli"
  21. func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error {
  22. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  23. if err != nil {
  24. return errors.Wrapf(err, "cannot get container group client")
  25. }
  26. // Check if the container group already exists
  27. _, err = containerGroupsClient.Get(ctx, aciContext.ResourceGroup, *groupDefinition.Name)
  28. if err != nil {
  29. if err, ok := err.(autorest.DetailedError); ok {
  30. if err.StatusCode != http.StatusNotFound {
  31. return err
  32. }
  33. } else {
  34. return err
  35. }
  36. } else {
  37. return fmt.Errorf("container group %q already exists", *groupDefinition.Name)
  38. }
  39. future, err := containerGroupsClient.CreateOrUpdate(
  40. ctx,
  41. aciContext.ResourceGroup,
  42. *groupDefinition.Name,
  43. groupDefinition,
  44. )
  45. if err != nil {
  46. return err
  47. }
  48. err = future.WaitForCompletionRef(ctx, containerGroupsClient.Client)
  49. if err != nil {
  50. return err
  51. }
  52. containerGroup, err := future.Result(containerGroupsClient)
  53. if err != nil {
  54. return err
  55. }
  56. if len(*containerGroup.Containers) > 1 {
  57. var commands []string
  58. for _, container := range *containerGroup.Containers {
  59. commands = append(commands, fmt.Sprintf("echo 127.0.0.1 %s >> /etc/hosts", *container.Name))
  60. }
  61. commands = append(commands, "exit")
  62. containers := *containerGroup.Containers
  63. container := containers[0]
  64. response, err := execACIContainer(ctx, aciContext, "/bin/sh", *containerGroup.Name, *container.Name)
  65. if err != nil {
  66. return err
  67. }
  68. if err = execCommands(
  69. ctx,
  70. *response.WebSocketURI,
  71. *response.Password,
  72. commands,
  73. ); err != nil {
  74. return err
  75. }
  76. }
  77. return err
  78. }
  79. func deleteACIContainerGroup(ctx context.Context, aciContext store.AciContext, containerGroupName string) (containerinstance.ContainerGroup, error) {
  80. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  81. if err != nil {
  82. return containerinstance.ContainerGroup{}, fmt.Errorf("cannot get container group client: %v", err)
  83. }
  84. return containerGroupsClient.Delete(ctx, aciContext.ResourceGroup, containerGroupName)
  85. }
  86. func execACIContainer(ctx context.Context, aciContext store.AciContext, command, containerGroup string, containerName string) (c containerinstance.ContainerExecResponse, err error) {
  87. containerClient, err := getContainerClient(aciContext.SubscriptionID)
  88. if err != nil {
  89. return c, errors.Wrapf(err, "cannot get container client")
  90. }
  91. rows, cols := getTermSize()
  92. containerExecRequest := containerinstance.ContainerExecRequest{
  93. Command: to.StringPtr(command),
  94. TerminalSize: &containerinstance.ContainerExecRequestTerminalSize{
  95. Rows: rows,
  96. Cols: cols,
  97. },
  98. }
  99. return containerClient.ExecuteCommand(
  100. ctx,
  101. aciContext.ResourceGroup,
  102. containerGroup,
  103. containerName,
  104. containerExecRequest)
  105. }
  106. func getTermSize() (*int32, *int32) {
  107. rows := tm.Height()
  108. cols := tm.Width()
  109. return to.Int32Ptr(int32(rows)), to.Int32Ptr(int32(cols))
  110. }
  111. type commandSender struct {
  112. commands string
  113. }
  114. func (cs *commandSender) Read(p []byte) (int, error) {
  115. if len(cs.commands) == 0 {
  116. return 0, io.EOF
  117. }
  118. var command string
  119. if len(p) >= len(cs.commands) {
  120. command = cs.commands
  121. cs.commands = ""
  122. } else {
  123. command = cs.commands[:len(p)]
  124. cs.commands = cs.commands[len(p):]
  125. }
  126. copy(p, command)
  127. return len(command), nil
  128. }
  129. func execCommands(ctx context.Context, address string, password string, commands []string) error {
  130. writer := ioutil.Discard
  131. reader := &commandSender{
  132. commands: strings.Join(commands, "\n"),
  133. }
  134. return exec(ctx, address, password, reader, writer)
  135. }
  136. func exec(ctx context.Context, address string, password string, reader io.Reader, writer io.Writer) error {
  137. conn, _, _, err := ws.DefaultDialer.Dial(ctx, address)
  138. if err != nil {
  139. return err
  140. }
  141. err = wsutil.WriteClientMessage(conn, ws.OpText, []byte(password))
  142. if err != nil {
  143. return err
  144. }
  145. downstreamChannel := make(chan error, 10)
  146. upstreamChannel := make(chan error, 10)
  147. go func() {
  148. for {
  149. msg, _, err := wsutil.ReadServerData(conn)
  150. if err != nil {
  151. if err == io.EOF {
  152. downstreamChannel <- nil
  153. return
  154. }
  155. downstreamChannel <- err
  156. return
  157. }
  158. fmt.Fprint(writer, string(msg))
  159. }
  160. }()
  161. go func() {
  162. for {
  163. // We send each byte, byte-per-byte over the
  164. // websocket because the console is in raw mode
  165. buffer := make([]byte, 1)
  166. n, err := reader.Read(buffer)
  167. if err != nil {
  168. if err == io.EOF {
  169. upstreamChannel <- nil
  170. return
  171. }
  172. upstreamChannel <- err
  173. return
  174. }
  175. if n > 0 {
  176. err := wsutil.WriteClientMessage(conn, ws.OpText, buffer)
  177. if err != nil {
  178. upstreamChannel <- err
  179. return
  180. }
  181. }
  182. }
  183. }()
  184. for {
  185. select {
  186. case err := <-downstreamChannel:
  187. return errors.Wrap(err, "failed to read input from container")
  188. case err := <-upstreamChannel:
  189. return errors.Wrap(err, "failed to send input to container")
  190. }
  191. }
  192. }
  193. func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string) (string, error) {
  194. containerClient, err := getContainerClient(aciContext.SubscriptionID)
  195. if err != nil {
  196. return "", errors.Wrapf(err, "cannot get container client")
  197. }
  198. logs, err := containerClient.ListLogs(ctx, aciContext.ResourceGroup, containerGroupName, containerName, nil)
  199. if err != nil {
  200. return "", fmt.Errorf("cannot get container logs: %v", err)
  201. }
  202. return *logs.Content, err
  203. }
  204. func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
  205. auth, err := login.NewAuthorizerFromLogin()
  206. if err != nil {
  207. return containerinstance.ContainerGroupsClient{}, err
  208. }
  209. containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID)
  210. containerGroupsClient.Authorizer = auth
  211. containerGroupsClient.UserAgent= aciDockerUserAgent
  212. containerGroupsClient.PollingDelay = 5 * time.Second
  213. containerGroupsClient.RetryAttempts = 30
  214. containerGroupsClient.RetryDuration = 1 * time.Second
  215. return containerGroupsClient, nil
  216. }
  217. func getContainerClient(subscriptionID string) (containerinstance.ContainerClient, error) {
  218. auth, err := login.NewAuthorizerFromLogin()
  219. if err != nil {
  220. return containerinstance.ContainerClient{}, err
  221. }
  222. containerClient := containerinstance.NewContainerClient(subscriptionID)
  223. containerClient.UserAgent=aciDockerUserAgent
  224. containerClient.Authorizer = auth
  225. return containerClient, nil
  226. }