backend.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package azure
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "github.com/Azure/go-autorest/autorest/to"
  10. "github.com/docker/api/context/cloud"
  11. "github.com/docker/api/errdefs"
  12. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  13. "github.com/compose-spec/compose-go/types"
  14. "github.com/pkg/errors"
  15. "github.com/sirupsen/logrus"
  16. "github.com/docker/api/azure/convert"
  17. "github.com/docker/api/azure/login"
  18. "github.com/docker/api/backend"
  19. "github.com/docker/api/compose"
  20. "github.com/docker/api/containers"
  21. apicontext "github.com/docker/api/context"
  22. "github.com/docker/api/context/store"
  23. )
  24. const singleContainerName = "single--container--aci"
  25. // ErrNoSuchContainer is returned when the mentioned container does not exist
  26. var ErrNoSuchContainer = errors.New("no such container")
  27. func init() {
  28. backend.Register("aci", "aci", service, getCloudService)
  29. }
  30. func service(ctx context.Context) (backend.Service, error) {
  31. contextStore := store.ContextStore(ctx)
  32. currentContext := apicontext.CurrentContext(ctx)
  33. var aciContext store.AciContext
  34. if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
  35. return nil, err
  36. }
  37. return getAciAPIService(aciContext), nil
  38. }
  39. func getCloudService() (cloud.Service, error) {
  40. service, err := login.NewAzureLoginService()
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &aciCloudService{
  45. loginService: service,
  46. }, nil
  47. }
  48. func getAciAPIService(aciCtx store.AciContext) *aciAPIService {
  49. return &aciAPIService{
  50. aciContainerService: &aciContainerService{
  51. ctx: aciCtx,
  52. },
  53. aciComposeService: &aciComposeService{
  54. ctx: aciCtx,
  55. },
  56. }
  57. }
  58. type aciAPIService struct {
  59. *aciContainerService
  60. *aciComposeService
  61. }
  62. func (a *aciAPIService) ContainerService() containers.Service {
  63. return a.aciContainerService
  64. }
  65. func (a *aciAPIService) ComposeService() compose.Service {
  66. return a.aciComposeService
  67. }
  68. type aciContainerService struct {
  69. ctx store.AciContext
  70. }
  71. func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.Container, error) {
  72. groupsClient, err := getContainerGroupsClient(cs.ctx.SubscriptionID)
  73. if err != nil {
  74. return nil, err
  75. }
  76. var containerGroups []containerinstance.ContainerGroup
  77. result, err := groupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
  78. if err != nil {
  79. return []containers.Container{}, err
  80. }
  81. for result.NotDone() {
  82. containerGroups = append(containerGroups, result.Values()...)
  83. if err := result.NextWithContext(ctx); err != nil {
  84. return []containers.Container{}, err
  85. }
  86. }
  87. var res []containers.Container
  88. for _, containerGroup := range containerGroups {
  89. group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
  90. if err != nil {
  91. return []containers.Container{}, err
  92. }
  93. for _, container := range *group.Containers {
  94. var containerID string
  95. // don't list sidecar container
  96. if *container.Name == convert.ComposeDNSSidecarName {
  97. continue
  98. }
  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. func (cs *aciContainerService) Inspect(ctx context.Context, containerID string) (containers.Container, error) {
  211. groupName, containerName := getGroupAndContainerName(containerID)
  212. cg, err := getACIContainerGroup(ctx, cs.ctx, groupName)
  213. if err != nil {
  214. return containers.Container{}, err
  215. }
  216. if cg.StatusCode == http.StatusNoContent {
  217. return containers.Container{}, ErrNoSuchContainer
  218. }
  219. var cc containerinstance.Container
  220. var found = false
  221. for _, c := range *cg.Containers {
  222. if to.String(c.Name) == containerName {
  223. cc = c
  224. found = true
  225. break
  226. }
  227. }
  228. if !found {
  229. return containers.Container{}, ErrNoSuchContainer
  230. }
  231. return convert.ContainerGroupToContainer(containerID, cg, cc)
  232. }
  233. type aciComposeService struct {
  234. ctx store.AciContext
  235. }
  236. func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  237. project, err := compose.ProjectFromOptions(&opts)
  238. if err != nil {
  239. return err
  240. }
  241. logrus.Debugf("Up on project with name %q\n", project.Name)
  242. groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
  243. if err != nil {
  244. return err
  245. }
  246. return createOrUpdateACIContainers(ctx, cs.ctx, groupDefinition)
  247. }
  248. func (cs *aciComposeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  249. project, err := compose.ProjectFromOptions(&opts)
  250. if err != nil {
  251. return err
  252. }
  253. logrus.Debugf("Down on project with name %q\n", project.Name)
  254. cg, err := deleteACIContainerGroup(ctx, cs.ctx, project.Name)
  255. if err != nil {
  256. return err
  257. }
  258. if cg.StatusCode == http.StatusNoContent {
  259. return ErrNoSuchContainer
  260. }
  261. return err
  262. }
  263. type aciCloudService struct {
  264. loginService login.AzureLoginService
  265. }
  266. func (cs *aciCloudService) Login(ctx context.Context, params map[string]string) error {
  267. return cs.loginService.Login(ctx)
  268. }
  269. func (cs *aciCloudService) CreateContextData(ctx context.Context, params map[string]string) (interface{}, string, error) {
  270. contextHelper := newContextCreateHelper()
  271. return contextHelper.createContextData(ctx, params)
  272. }