backend.go 5.9 KB

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