aci.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package azure
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "net/http"
  19. "strings"
  20. "time"
  21. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  22. "github.com/Azure/go-autorest/autorest"
  23. "github.com/Azure/go-autorest/autorest/to"
  24. "github.com/buger/goterm"
  25. tm "github.com/buger/goterm"
  26. "github.com/gobwas/ws"
  27. "github.com/gobwas/ws/wsutil"
  28. "github.com/morikuni/aec"
  29. "github.com/pkg/errors"
  30. "github.com/docker/api/azure/convert"
  31. "github.com/docker/api/azure/login"
  32. "github.com/docker/api/containers"
  33. "github.com/docker/api/context/store"
  34. "github.com/docker/api/progress"
  35. )
  36. const aciDockerUserAgent = "docker-cli"
  37. func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error {
  38. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  39. if err != nil {
  40. return errors.Wrapf(err, "cannot get container group client")
  41. }
  42. // Check if the container group already exists
  43. _, err = containerGroupsClient.Get(ctx, aciContext.ResourceGroup, *groupDefinition.Name)
  44. if err != nil {
  45. if err, ok := err.(autorest.DetailedError); ok {
  46. if err.StatusCode != http.StatusNotFound {
  47. return err
  48. }
  49. } else {
  50. return err
  51. }
  52. } else {
  53. return fmt.Errorf("container group %q already exists", *groupDefinition.Name)
  54. }
  55. return createOrUpdateACIContainers(ctx, aciContext, groupDefinition)
  56. }
  57. func createOrUpdateACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error {
  58. w := progress.ContextWriter(ctx)
  59. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  60. if err != nil {
  61. return errors.Wrapf(err, "cannot get container group client")
  62. }
  63. w.Event(progress.Event{
  64. ID: *groupDefinition.Name,
  65. Status: progress.Working,
  66. StatusText: "Waiting",
  67. })
  68. future, err := containerGroupsClient.CreateOrUpdate(
  69. ctx,
  70. aciContext.ResourceGroup,
  71. *groupDefinition.Name,
  72. groupDefinition,
  73. )
  74. if err != nil {
  75. return err
  76. }
  77. w.Event(progress.Event{
  78. ID: *groupDefinition.Name,
  79. Status: progress.Done,
  80. StatusText: "Created",
  81. })
  82. for _, c := range *groupDefinition.Containers {
  83. if c.Name != nil && *c.Name != convert.ComposeDNSSidecarName {
  84. w.Event(progress.Event{
  85. ID: *c.Name,
  86. Status: progress.Working,
  87. StatusText: "Waiting",
  88. })
  89. }
  90. }
  91. err = future.WaitForCompletionRef(ctx, containerGroupsClient.Client)
  92. if err != nil {
  93. return err
  94. }
  95. for _, c := range *groupDefinition.Containers {
  96. if c.Name != nil && *c.Name != convert.ComposeDNSSidecarName {
  97. w.Event(progress.Event{
  98. ID: *c.Name,
  99. Status: progress.Done,
  100. StatusText: "Done",
  101. })
  102. }
  103. }
  104. return err
  105. }
  106. func getACIContainerGroup(ctx context.Context, aciContext store.AciContext, containerGroupName string) (containerinstance.ContainerGroup, error) {
  107. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  108. if err != nil {
  109. return containerinstance.ContainerGroup{}, fmt.Errorf("cannot get container group client: %v", err)
  110. }
  111. return containerGroupsClient.Get(ctx, aciContext.ResourceGroup, containerGroupName)
  112. }
  113. func deleteACIContainerGroup(ctx context.Context, aciContext store.AciContext, containerGroupName string) (containerinstance.ContainerGroup, error) {
  114. containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID)
  115. if err != nil {
  116. return containerinstance.ContainerGroup{}, fmt.Errorf("cannot get container group client: %v", err)
  117. }
  118. return containerGroupsClient.Delete(ctx, aciContext.ResourceGroup, containerGroupName)
  119. }
  120. func execACIContainer(ctx context.Context, aciContext store.AciContext, command, containerGroup string, containerName string) (c containerinstance.ContainerExecResponse, err error) {
  121. containerClient, err := getContainerClient(aciContext.SubscriptionID)
  122. if err != nil {
  123. return c, errors.Wrapf(err, "cannot get container client")
  124. }
  125. rows, cols := getTermSize()
  126. containerExecRequest := containerinstance.ContainerExecRequest{
  127. Command: to.StringPtr(command),
  128. TerminalSize: &containerinstance.ContainerExecRequestTerminalSize{
  129. Rows: rows,
  130. Cols: cols,
  131. },
  132. }
  133. return containerClient.ExecuteCommand(
  134. ctx,
  135. aciContext.ResourceGroup,
  136. containerGroup,
  137. containerName,
  138. containerExecRequest)
  139. }
  140. func getTermSize() (*int32, *int32) {
  141. rows := tm.Height()
  142. cols := tm.Width()
  143. return to.Int32Ptr(int32(rows)), to.Int32Ptr(int32(cols))
  144. }
  145. func exec(ctx context.Context, address string, password string, request containers.ExecRequest) error {
  146. conn, _, _, err := ws.DefaultDialer.Dial(ctx, address)
  147. if err != nil {
  148. return err
  149. }
  150. err = wsutil.WriteClientMessage(conn, ws.OpText, []byte(password))
  151. if err != nil {
  152. return err
  153. }
  154. downstreamChannel := make(chan error, 10)
  155. upstreamChannel := make(chan error, 10)
  156. go func() {
  157. for {
  158. msg, _, err := wsutil.ReadServerData(conn)
  159. if err != nil {
  160. if err == io.EOF {
  161. downstreamChannel <- nil
  162. return
  163. }
  164. downstreamChannel <- err
  165. return
  166. }
  167. fmt.Fprint(request.Stdout, string(msg))
  168. }
  169. }()
  170. if request.Interactive {
  171. go func() {
  172. for {
  173. // We send each byte, byte-per-byte over the
  174. // websocket because the console is in raw mode
  175. buffer := make([]byte, 1)
  176. n, err := request.Stdin.Read(buffer)
  177. if err != nil {
  178. if err == io.EOF {
  179. upstreamChannel <- nil
  180. return
  181. }
  182. upstreamChannel <- err
  183. return
  184. }
  185. if n > 0 {
  186. err := wsutil.WriteClientMessage(conn, ws.OpText, buffer)
  187. if err != nil {
  188. upstreamChannel <- err
  189. return
  190. }
  191. }
  192. }
  193. }()
  194. }
  195. for {
  196. select {
  197. case err := <-downstreamChannel:
  198. return errors.Wrap(err, "failed to read input from container")
  199. case err := <-upstreamChannel:
  200. return errors.Wrap(err, "failed to send input to container")
  201. }
  202. }
  203. }
  204. func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string, tail *int32) (string, error) {
  205. containerClient, err := getContainerClient(aciContext.SubscriptionID)
  206. if err != nil {
  207. return "", errors.Wrapf(err, "cannot get container client")
  208. }
  209. logs, err := containerClient.ListLogs(ctx, aciContext.ResourceGroup, containerGroupName, containerName, tail)
  210. if err != nil {
  211. return "", fmt.Errorf("cannot get container logs: %v", err)
  212. }
  213. return *logs.Content, err
  214. }
  215. func streamLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string, out io.Writer) error {
  216. terminalWidth := goterm.Width()
  217. numLines := 0
  218. for {
  219. select {
  220. case <-ctx.Done():
  221. return nil
  222. default:
  223. logs, err := getACIContainerLogs(ctx, aciContext, containerGroupName, containerName, nil)
  224. if err != nil {
  225. return err
  226. }
  227. logLines := strings.Split(logs, "\n")
  228. currentOutput := len(logLines)
  229. // Note: a backend should not do this normally, this breaks the log
  230. // streaming over gRPC but this is the only thing we can do with
  231. // the kind of logs ACI is giving us. Hopefully Azue will give us
  232. // a real logs streaming api soon.
  233. b := aec.EmptyBuilder
  234. b = b.Up(uint(numLines))
  235. fmt.Fprint(out, b.Column(0).ANSI)
  236. numLines = getBacktrackLines(logLines, terminalWidth)
  237. for i := 0; i < currentOutput-1; i++ {
  238. fmt.Fprintln(out, logLines[i])
  239. }
  240. select {
  241. case <-ctx.Done():
  242. return nil
  243. case <-time.After(2 * time.Second):
  244. }
  245. }
  246. }
  247. }
  248. func getBacktrackLines(lines []string, terminalWidth int) int {
  249. numLines := 0
  250. for i := 0; i < len(lines)-1; i++ {
  251. numLines++
  252. if len(lines[i]) > terminalWidth {
  253. numLines += len(lines[i]) / terminalWidth
  254. }
  255. }
  256. return numLines
  257. }
  258. func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
  259. containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID)
  260. err := setupClient(&containerGroupsClient.Client)
  261. if err != nil {
  262. return containerinstance.ContainerGroupsClient{}, err
  263. }
  264. containerGroupsClient.PollingDelay = 5 * time.Second
  265. containerGroupsClient.RetryAttempts = 30
  266. containerGroupsClient.RetryDuration = 1 * time.Second
  267. return containerGroupsClient, nil
  268. }
  269. func setupClient(aciClient *autorest.Client) error {
  270. aciClient.UserAgent = aciDockerUserAgent
  271. auth, err := login.NewAuthorizerFromLogin()
  272. if err != nil {
  273. return err
  274. }
  275. aciClient.Authorizer = auth
  276. return nil
  277. }
  278. func getContainerClient(subscriptionID string) (containerinstance.ContainerClient, error) {
  279. containerClient := containerinstance.NewContainerClient(subscriptionID)
  280. err := setupClient(&containerClient.Client)
  281. if err != nil {
  282. return containerinstance.ContainerClient{}, err
  283. }
  284. return containerClient, nil
  285. }