backend.go 7.1 KB

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