containers.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 aci
  14. import (
  15. "context"
  16. "fmt"
  17. "net/http"
  18. "strconv"
  19. "strings"
  20. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  21. "github.com/Azure/go-autorest/autorest"
  22. "github.com/Azure/go-autorest/autorest/to"
  23. "github.com/pkg/errors"
  24. "github.com/sirupsen/logrus"
  25. "github.com/docker/compose-cli/aci/convert"
  26. "github.com/docker/compose-cli/aci/login"
  27. "github.com/docker/compose-cli/api/containers"
  28. "github.com/docker/compose-cli/context/store"
  29. "github.com/docker/compose-cli/errdefs"
  30. )
  31. type aciContainerService struct {
  32. ctx store.AciContext
  33. }
  34. func (cs *aciContainerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
  35. containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
  36. if err != nil {
  37. return nil, err
  38. }
  39. var res []containers.Container
  40. for _, group := range containerGroups {
  41. if group.Containers == nil || len(*group.Containers) == 0 {
  42. return nil, fmt.Errorf("no containers found in ACI container group %s", *group.Name)
  43. }
  44. for _, container := range *group.Containers {
  45. if isContainerVisible(container, group, all) {
  46. continue
  47. }
  48. c := convert.ContainerGroupToContainer(getContainerID(group, container), group, container)
  49. res = append(res, c)
  50. }
  51. }
  52. return res, nil
  53. }
  54. func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  55. if strings.Contains(r.ID, composeContainerSeparator) {
  56. return fmt.Errorf("invalid container name. ACI container name cannot include %q", composeContainerSeparator)
  57. }
  58. project, err := convert.ContainerToComposeProject(r)
  59. if err != nil {
  60. return err
  61. }
  62. logrus.Debugf("Running container %q with name %q", r.Image, r.ID)
  63. groupDefinition, err := convert.ToContainerGroup(ctx, cs.ctx, project)
  64. if err != nil {
  65. return err
  66. }
  67. addTag(&groupDefinition, singleContainerTag)
  68. return createACIContainers(ctx, cs.ctx, groupDefinition)
  69. }
  70. func (cs *aciContainerService) Start(ctx context.Context, containerID string) error {
  71. groupName, containerName := getGroupAndContainerName(containerID)
  72. if groupName != containerID {
  73. msg := "cannot start specified service %q from compose application %q, you can update and restart the entire compose app with docker compose up --project-name %s"
  74. return fmt.Errorf(msg, containerName, groupName, groupName)
  75. }
  76. containerGroupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  77. if err != nil {
  78. return err
  79. }
  80. future, err := containerGroupsClient.Start(ctx, cs.ctx.ResourceGroup, containerName)
  81. if err != nil {
  82. var aerr autorest.DetailedError
  83. if ok := errors.As(err, &aerr); ok {
  84. if aerr.StatusCode == http.StatusNotFound {
  85. return errdefs.ErrNotFound
  86. }
  87. }
  88. return err
  89. }
  90. return future.WaitForCompletionRef(ctx, containerGroupsClient.Client)
  91. }
  92. func (cs *aciContainerService) Stop(ctx context.Context, containerID string, timeout *uint32) error {
  93. if timeout != nil && *timeout != uint32(0) {
  94. return fmt.Errorf("the ACI integration does not support setting a timeout to stop a container before killing it")
  95. }
  96. groupName, containerName := getGroupAndContainerName(containerID)
  97. if groupName != containerID {
  98. msg := "cannot stop service %q from compose application %q, you can stop the entire compose app with docker stop %s"
  99. return fmt.Errorf(msg, containerName, groupName, groupName)
  100. }
  101. return stopACIContainerGroup(ctx, cs.ctx, groupName)
  102. }
  103. func (cs *aciContainerService) Kill(ctx context.Context, containerID string, _ string) error {
  104. groupName, containerName := getGroupAndContainerName(containerID)
  105. if groupName != containerID {
  106. msg := "cannot kill service %q from compose application %q, you can kill the entire compose app with docker kill %s"
  107. return fmt.Errorf(msg, containerName, groupName, groupName)
  108. }
  109. return stopACIContainerGroup(ctx, cs.ctx, groupName) // As ACI doesn't have a kill command, we are using the stop implementation instead
  110. }
  111. func (cs *aciContainerService) Exec(ctx context.Context, name string, request containers.ExecRequest) error {
  112. err := verifyExecCommand(request.Command)
  113. if err != nil {
  114. return err
  115. }
  116. groupName, containerAciName := getGroupAndContainerName(name)
  117. containerExecResponse, err := execACIContainer(ctx, cs.ctx, request.Command, groupName, containerAciName)
  118. if err != nil {
  119. return err
  120. }
  121. return exec(
  122. context.Background(),
  123. *containerExecResponse.WebSocketURI,
  124. *containerExecResponse.Password,
  125. request,
  126. )
  127. }
  128. func verifyExecCommand(command string) error {
  129. tokens := strings.Split(command, " ")
  130. if len(tokens) > 1 {
  131. return errors.New("ACI exec command does not accept arguments to the command. " +
  132. "Only the binary should be specified")
  133. }
  134. return nil
  135. }
  136. func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
  137. groupName, containerAciName := getGroupAndContainerName(containerName)
  138. var tail *int32
  139. if req.Follow {
  140. return streamLogs(ctx, cs.ctx, groupName, containerAciName, req)
  141. }
  142. if req.Tail != "all" {
  143. reqTail, err := strconv.Atoi(req.Tail)
  144. if err != nil {
  145. return err
  146. }
  147. i32 := int32(reqTail)
  148. tail = &i32
  149. }
  150. logs, err := getACIContainerLogs(ctx, cs.ctx, groupName, containerAciName, tail)
  151. if err != nil {
  152. return err
  153. }
  154. _, err = fmt.Fprint(req.Writer, logs)
  155. return err
  156. }
  157. func (cs *aciContainerService) Delete(ctx context.Context, containerID string, request containers.DeleteRequest) error {
  158. groupName, containerName := getGroupAndContainerName(containerID)
  159. if groupName != containerID {
  160. msg := "cannot delete service %q from compose application %q, you can delete the entire compose app with docker compose down --project-name %s"
  161. return fmt.Errorf(msg, containerName, groupName, groupName)
  162. }
  163. if !request.Force {
  164. containerGroupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  165. if err != nil {
  166. return err
  167. }
  168. cg, err := containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, groupName)
  169. if err != nil {
  170. if cg.StatusCode == http.StatusNotFound {
  171. return errdefs.ErrNotFound
  172. }
  173. return err
  174. }
  175. for _, container := range *cg.Containers {
  176. status := convert.GetStatus(container, cg)
  177. if status == convert.StatusRunning {
  178. return errdefs.ErrForbidden
  179. }
  180. }
  181. }
  182. cg, err := deleteACIContainerGroup(ctx, cs.ctx, groupName)
  183. // Delete returns `StatusNoContent` if the group is not found
  184. if cg.StatusCode == http.StatusNoContent {
  185. return errdefs.ErrNotFound
  186. }
  187. if err != nil {
  188. return err
  189. }
  190. return err
  191. }
  192. func (cs *aciContainerService) Inspect(ctx context.Context, containerID string) (containers.Container, error) {
  193. groupName, containerName := getGroupAndContainerName(containerID)
  194. cg, err := getACIContainerGroup(ctx, cs.ctx, groupName)
  195. if err != nil {
  196. return containers.Container{}, err
  197. }
  198. if cg.StatusCode == http.StatusNoContent {
  199. return containers.Container{}, errdefs.ErrNotFound
  200. }
  201. var cc containerinstance.Container
  202. var found = false
  203. for _, c := range *cg.Containers {
  204. if to.String(c.Name) == containerName {
  205. cc = c
  206. found = true
  207. break
  208. }
  209. }
  210. if !found {
  211. return containers.Container{}, errdefs.ErrNotFound
  212. }
  213. return convert.ContainerGroupToContainer(containerID, cg, cc), nil
  214. }