backend.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package azure
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "github.com/docker/api/context/cloud"
  10. "github.com/docker/api/errdefs"
  11. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  12. "github.com/compose-spec/compose-go/types"
  13. "github.com/pkg/errors"
  14. "github.com/sirupsen/logrus"
  15. "github.com/docker/api/azure/convert"
  16. "github.com/docker/api/azure/login"
  17. "github.com/docker/api/backend"
  18. "github.com/docker/api/compose"
  19. "github.com/docker/api/containers"
  20. apicontext "github.com/docker/api/context"
  21. "github.com/docker/api/context/store"
  22. )
  23. const singleContainerName = "single--container--aci"
  24. // ErrNoSuchContainer is returned when the mentioned container does not exist
  25. var ErrNoSuchContainer = errors.New("no such container")
  26. func init() {
  27. backend.Register("aci", "aci", func(ctx context.Context) (backend.Service, error) {
  28. return New(ctx)
  29. })
  30. }
  31. // New creates a backend that can manage containers
  32. func New(ctx context.Context) (backend.Service, error) {
  33. currentContext := apicontext.CurrentContext(ctx)
  34. contextStore := store.ContextStore(ctx)
  35. var aciContext store.AciContext
  36. if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
  37. return nil, err
  38. }
  39. return getAciAPIService(aciContext)
  40. }
  41. func getAciAPIService(aciCtx store.AciContext) (*aciAPIService, error) {
  42. service, err := login.NewAzureLoginService()
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &aciAPIService{
  47. aciContainerService: aciContainerService{
  48. ctx: aciCtx,
  49. },
  50. aciComposeService: aciComposeService{
  51. ctx: aciCtx,
  52. },
  53. aciCloudService: aciCloudService{
  54. loginService: service,
  55. },
  56. }, nil
  57. }
  58. type aciAPIService struct {
  59. aciContainerService
  60. aciComposeService
  61. aciCloudService
  62. }
  63. func (a *aciAPIService) ContainerService() containers.Service {
  64. return &a.aciContainerService
  65. }
  66. func (a *aciAPIService) ComposeService() compose.Service {
  67. return &a.aciComposeService
  68. }
  69. func (a *aciAPIService) CloudService() cloud.Service {
  70. return &a.aciCloudService
  71. }
  72. type aciContainerService struct {
  73. ctx store.AciContext
  74. }
  75. func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.Container, error) {
  76. groupsClient, err := getContainerGroupsClient(cs.ctx.SubscriptionID)
  77. if err != nil {
  78. return nil, err
  79. }
  80. var containerGroups []containerinstance.ContainerGroup
  81. result, err := groupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
  82. if err != nil {
  83. return []containers.Container{}, err
  84. }
  85. for result.NotDone() {
  86. containerGroups = append(containerGroups, result.Values()...)
  87. if err := result.NextWithContext(ctx); err != nil {
  88. return []containers.Container{}, err
  89. }
  90. }
  91. var res []containers.Container
  92. for _, containerGroup := range containerGroups {
  93. group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
  94. if err != nil {
  95. return []containers.Container{}, err
  96. }
  97. for _, container := range *group.Containers {
  98. var containerID string
  99. if *container.Name == singleContainerName {
  100. containerID = *containerGroup.Name
  101. } else {
  102. containerID = *containerGroup.Name + "_" + *container.Name
  103. }
  104. status := "Unknown"
  105. if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
  106. status = *container.InstanceView.CurrentState.State
  107. }
  108. res = append(res, containers.Container{
  109. ID: containerID,
  110. Image: *container.Image,
  111. Status: status,
  112. Ports: convert.ToPorts(group.IPAddress, *container.Ports),
  113. })
  114. }
  115. }
  116. return res, nil
  117. }
  118. func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  119. var ports []types.ServicePortConfig
  120. for _, p := range r.Ports {
  121. ports = append(ports, types.ServicePortConfig{
  122. Target: p.ContainerPort,
  123. Published: p.HostPort,
  124. })
  125. }
  126. projectVolumes, serviceConfigVolumes, err := convert.GetRunVolumes(r.Volumes)
  127. if err != nil {
  128. return err
  129. }
  130. project := compose.Project{
  131. Name: r.ID,
  132. Config: types.Config{
  133. Services: []types.ServiceConfig{
  134. {
  135. Name: singleContainerName,
  136. Image: r.Image,
  137. Ports: ports,
  138. Labels: r.Labels,
  139. Volumes: serviceConfigVolumes,
  140. },
  141. },
  142. Volumes: projectVolumes,
  143. },
  144. }
  145. logrus.Debugf("Running container %q with name %q\n", r.Image, r.ID)
  146. groupDefinition, err := convert.ToContainerGroup(cs.ctx, project)
  147. if err != nil {
  148. return err
  149. }
  150. return createACIContainers(ctx, cs.ctx, groupDefinition)
  151. }
  152. func (cs *aciContainerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  153. return errdefs.ErrNotImplemented
  154. }
  155. func getGroupAndContainerName(containerID string) (groupName string, containerName string) {
  156. tokens := strings.Split(containerID, "_")
  157. groupName = tokens[0]
  158. if len(tokens) > 1 {
  159. containerName = tokens[len(tokens)-1]
  160. groupName = containerID[:len(containerID)-(len(containerName)+1)]
  161. } else {
  162. containerName = singleContainerName
  163. }
  164. return groupName, containerName
  165. }
  166. func (cs *aciContainerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  167. groupName, containerAciName := getGroupAndContainerName(name)
  168. containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, groupName, containerAciName)
  169. if err != nil {
  170. return err
  171. }
  172. return exec(
  173. context.Background(),
  174. *containerExecResponse.WebSocketURI,
  175. *containerExecResponse.Password,
  176. reader,
  177. writer,
  178. )
  179. }
  180. func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
  181. groupName, containerAciName := getGroupAndContainerName(containerName)
  182. logs, err := getACIContainerLogs(ctx, cs.ctx, groupName, containerAciName)
  183. if err != nil {
  184. return err
  185. }
  186. if req.Tail != "all" {
  187. tail, err := strconv.Atoi(req.Tail)
  188. if err != nil {
  189. return err
  190. }
  191. lines := strings.Split(logs, "\n")
  192. // If asked for less lines than exist, take only those lines
  193. if tail <= len(lines) {
  194. logs = strings.Join(lines[len(lines)-tail:], "\n")
  195. }
  196. }
  197. _, err = fmt.Fprint(req.Writer, logs)
  198. return err
  199. }
  200. func (cs *aciContainerService) Delete(ctx context.Context, containerID string, _ bool) error {
  201. cg, err := deleteACIContainerGroup(ctx, cs.ctx, containerID)
  202. if err != nil {
  203. return err
  204. }
  205. if cg.StatusCode == http.StatusNoContent {
  206. return ErrNoSuchContainer
  207. }
  208. return err
  209. }
  210. type aciComposeService struct {
  211. ctx store.AciContext
  212. }
  213. func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  214. project, err := compose.ProjectFromOptions(&opts)
  215. if err != nil {
  216. return err
  217. }
  218. logrus.Debugf("Up on project with name %q\n", project.Name)
  219. groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
  220. if err != nil {
  221. return err
  222. }
  223. return createACIContainers(ctx, cs.ctx, groupDefinition)
  224. }
  225. func (cs *aciComposeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  226. project, err := compose.ProjectFromOptions(&opts)
  227. if err != nil {
  228. return err
  229. }
  230. logrus.Debugf("Down on project with name %q\n", project.Name)
  231. cg, err := deleteACIContainerGroup(ctx, cs.ctx, project.Name)
  232. if err != nil {
  233. return err
  234. }
  235. if cg.StatusCode == http.StatusNoContent {
  236. return ErrNoSuchContainer
  237. }
  238. return err
  239. }
  240. type aciCloudService struct {
  241. loginService login.AzureLoginService
  242. }
  243. func (cs *aciCloudService) Login(ctx context.Context, params map[string]string) error {
  244. return cs.loginService.Login(ctx)
  245. }