containers.go 8.1 KB

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