backend.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package azure
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "strconv"
  7. "strings"
  8. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  9. "github.com/Azure/go-autorest/autorest/azure/auth"
  10. "github.com/compose-spec/compose-go/types"
  11. "github.com/pkg/errors"
  12. "github.com/sirupsen/logrus"
  13. "github.com/docker/api/azure/convert"
  14. "github.com/docker/api/backend"
  15. "github.com/docker/api/compose"
  16. "github.com/docker/api/containers"
  17. apicontext "github.com/docker/api/context"
  18. "github.com/docker/api/context/store"
  19. )
  20. type aciApiService struct {
  21. containerGroupsClient containerinstance.ContainerGroupsClient
  22. ctx store.AciContext
  23. }
  24. func init() {
  25. backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
  26. return New(ctx)
  27. })
  28. }
  29. func getter() interface{} {
  30. return &store.AciContext{}
  31. }
  32. type AciService interface {
  33. containers.ContainerService
  34. compose.Service
  35. }
  36. // New creates a backend that can manage containers on ACI
  37. func New(ctx context.Context) (AciService, error) {
  38. currentContext := apicontext.CurrentContext(ctx)
  39. contextStore, err := store.New()
  40. if err != nil {
  41. return nil, err
  42. }
  43. metadata, err := contextStore.Get(currentContext, getter)
  44. if err != nil {
  45. return nil, errors.Wrap(err, "wrong context type")
  46. }
  47. aciContext, _ := metadata.Metadata.Data.(store.AciContext)
  48. auth, _ := auth.NewAuthorizerFromCLI()
  49. containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
  50. containerGroupsClient.Authorizer = auth
  51. return &aciApiService{
  52. containerGroupsClient: containerGroupsClient,
  53. ctx: aciContext,
  54. }, nil
  55. }
  56. func (cs *aciApiService) List(ctx context.Context) ([]containers.Container, error) {
  57. var containerGroups []containerinstance.ContainerGroup
  58. result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
  59. if err != nil {
  60. return []containers.Container{}, err
  61. }
  62. for result.NotDone() {
  63. containerGroups = append(containerGroups, result.Values()...)
  64. if err := result.NextWithContext(ctx); err != nil {
  65. return []containers.Container{}, err
  66. }
  67. }
  68. var res []containers.Container
  69. for _, containerGroup := range containerGroups {
  70. group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
  71. if err != nil {
  72. return []containers.Container{}, err
  73. }
  74. for _, container := range *group.Containers {
  75. status := "Unknown"
  76. if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
  77. status = *container.InstanceView.CurrentState.State
  78. }
  79. res = append(res, containers.Container{
  80. ID: *container.Name,
  81. Image: *container.Image,
  82. Status: status,
  83. })
  84. }
  85. }
  86. return res, nil
  87. }
  88. func (cs *aciApiService) Run(ctx context.Context, r containers.ContainerConfig) error {
  89. var ports []types.ServicePortConfig
  90. for _, p := range r.Ports {
  91. ports = append(ports, types.ServicePortConfig{
  92. Target: p.Destination,
  93. Published: p.Source,
  94. })
  95. }
  96. project := compose.Project{
  97. Name: r.ID,
  98. Config: types.Config{
  99. Services: []types.ServiceConfig{
  100. {
  101. Name: r.ID,
  102. Image: r.Image,
  103. Ports: ports,
  104. },
  105. },
  106. },
  107. }
  108. logrus.Debugf("Running container %q with name %q\n", r.Image, r.ID)
  109. groupDefinition, err := convert.ToContainerGroup(cs.ctx, project)
  110. if err != nil {
  111. return err
  112. }
  113. _, err = createACIContainers(ctx, cs.ctx, groupDefinition)
  114. return err
  115. }
  116. func (cs *aciApiService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  117. containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, name, name)
  118. if err != nil {
  119. return err
  120. }
  121. return exec(
  122. context.Background(),
  123. *containerExecResponse.WebSocketURI,
  124. *containerExecResponse.Password,
  125. reader,
  126. writer,
  127. )
  128. }
  129. func (cs *aciApiService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
  130. logs, err := getACIContainerLogs(ctx, cs.ctx, containerName, containerName)
  131. if err != nil {
  132. return err
  133. }
  134. if req.Tail != "all" {
  135. tail, err := strconv.Atoi(req.Tail)
  136. if err != nil {
  137. return err
  138. }
  139. lines := strings.Split(logs, "\n")
  140. // If asked for less lines than exist, take only those lines
  141. if tail <= len(lines) {
  142. logs = strings.Join(lines[len(lines)-tail:], "\n")
  143. }
  144. }
  145. _, err = fmt.Fprint(req.Writer, logs)
  146. return err
  147. }
  148. func (cs *aciApiService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  149. project, err := compose.ProjectFromOptions(&opts)
  150. if err != nil {
  151. return err
  152. }
  153. logrus.Debugf("Up on project with name %q\n", project.Name)
  154. groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
  155. if err != nil {
  156. return err
  157. }
  158. _, err = createACIContainers(ctx, cs.ctx, groupDefinition)
  159. return err
  160. }
  161. func (cs *aciApiService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  162. project, err := compose.ProjectFromOptions(&opts)
  163. if err != nil {
  164. return err
  165. }
  166. logrus.Debugf("Down on project with name %q\n", project.Name)
  167. _, err = deleteACIContainerGroup(ctx, cs.ctx, project.Name)
  168. return err
  169. }