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/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. 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. var 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. if r.DomainName != "" {
  76. groupDefinition.ContainerGroupProperties.IPAddress.DNSNameLabel = &r.DomainName
  77. }
  78. return createACIContainers(ctx, cs.ctx, groupDefinition)
  79. }
  80. func (cs *aciContainerService) Start(ctx context.Context, containerID string) error {
  81. groupName, containerName := getGroupAndContainerName(containerID)
  82. if groupName != containerID {
  83. 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"
  84. return fmt.Errorf(msg, containerName, groupName, groupName)
  85. }
  86. containerGroupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  87. if err != nil {
  88. return err
  89. }
  90. future, err := containerGroupsClient.Start(ctx, cs.ctx.ResourceGroup, containerName)
  91. if err != nil {
  92. var aerr autorest.DetailedError
  93. if ok := errors.As(err, &aerr); ok {
  94. if aerr.StatusCode == http.StatusNotFound {
  95. return errdefs.ErrNotFound
  96. }
  97. }
  98. return err
  99. }
  100. return future.WaitForCompletionRef(ctx, containerGroupsClient.Client)
  101. }
  102. func (cs *aciContainerService) Stop(ctx context.Context, containerID string, timeout *uint32) error {
  103. if timeout != nil && *timeout != uint32(0) {
  104. return fmt.Errorf("the ACI integration does not support setting a timeout to stop a container before killing it")
  105. }
  106. groupName, containerName := getGroupAndContainerName(containerID)
  107. if groupName != containerID {
  108. msg := "cannot stop service %q from compose application %q, you can stop the entire compose app with docker stop %s"
  109. return fmt.Errorf(msg, containerName, groupName, groupName)
  110. }
  111. return stopACIContainerGroup(ctx, cs.ctx, groupName)
  112. }
  113. func (cs *aciContainerService) Kill(ctx context.Context, containerID string, _ string) error {
  114. groupName, containerName := getGroupAndContainerName(containerID)
  115. if groupName != containerID {
  116. msg := "cannot kill service %q from compose application %q, you can kill the entire compose app with docker kill %s"
  117. return fmt.Errorf(msg, containerName, groupName, groupName)
  118. }
  119. return stopACIContainerGroup(ctx, cs.ctx, groupName) // As ACI doesn't have a kill command, we are using the stop implementation instead
  120. }
  121. func (cs *aciContainerService) Exec(ctx context.Context, name string, request containers.ExecRequest) error {
  122. err := verifyExecCommand(request.Command)
  123. if err != nil {
  124. return err
  125. }
  126. groupName, containerAciName := getGroupAndContainerName(name)
  127. containerExecResponse, err := execACIContainer(ctx, cs.ctx, request.Command, groupName, containerAciName)
  128. if err != nil {
  129. return err
  130. }
  131. return exec(
  132. context.Background(),
  133. *containerExecResponse.WebSocketURI,
  134. *containerExecResponse.Password,
  135. request,
  136. )
  137. }
  138. func verifyExecCommand(command string) error {
  139. tokens := strings.Split(command, " ")
  140. if len(tokens) > 1 {
  141. return errors.New("ACI exec command does not accept arguments to the command. " +
  142. "Only the binary should be specified")
  143. }
  144. return nil
  145. }
  146. func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
  147. groupName, containerAciName := getGroupAndContainerName(containerName)
  148. var tail *int32
  149. if req.Follow {
  150. return streamLogs(ctx, cs.ctx, groupName, containerAciName, req)
  151. }
  152. if req.Tail != "all" {
  153. reqTail, err := strconv.Atoi(req.Tail)
  154. if err != nil {
  155. return err
  156. }
  157. i32 := int32(reqTail)
  158. tail = &i32
  159. }
  160. logs, err := getACIContainerLogs(ctx, cs.ctx, groupName, containerAciName, tail)
  161. if err != nil {
  162. return err
  163. }
  164. _, err = fmt.Fprint(req.Writer, logs)
  165. return err
  166. }
  167. func (cs *aciContainerService) Delete(ctx context.Context, containerID string, request containers.DeleteRequest) error {
  168. groupName, containerName := getGroupAndContainerName(containerID)
  169. if groupName != containerID {
  170. msg := "cannot delete service %q from compose application %q, you can delete the entire compose app with docker compose down --project-name %s"
  171. return fmt.Errorf(msg, containerName, groupName, groupName)
  172. }
  173. if !request.Force {
  174. containerGroupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  175. if err != nil {
  176. return err
  177. }
  178. cg, err := containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, groupName)
  179. if err != nil {
  180. if cg.StatusCode == http.StatusNotFound {
  181. return errdefs.ErrNotFound
  182. }
  183. return err
  184. }
  185. for _, container := range *cg.Containers {
  186. status := convert.GetStatus(container, cg)
  187. if status == convert.StatusRunning {
  188. return errdefs.ErrForbidden
  189. }
  190. }
  191. }
  192. cg, err := deleteACIContainerGroup(ctx, cs.ctx, groupName)
  193. // Delete returns `StatusNoContent` if the group is not found
  194. if cg.StatusCode == http.StatusNoContent {
  195. return errdefs.ErrNotFound
  196. }
  197. if err != nil {
  198. return err
  199. }
  200. return err
  201. }
  202. func (cs *aciContainerService) Inspect(ctx context.Context, containerID string) (containers.Container, error) {
  203. groupName, containerName := getGroupAndContainerName(containerID)
  204. cg, err := getACIContainerGroup(ctx, cs.ctx, groupName)
  205. if err != nil {
  206. return containers.Container{}, err
  207. }
  208. if cg.StatusCode == http.StatusNoContent {
  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. }