backend.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if *container.Name == singleContainerName {
  96. containerID = *containerGroup.Name
  97. } else {
  98. containerID = *containerGroup.Name + "_" + *container.Name
  99. }
  100. status := "Unknown"
  101. if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
  102. status = *container.InstanceView.CurrentState.State
  103. }
  104. res = append(res, containers.Container{
  105. ID: containerID,
  106. Image: *container.Image,
  107. Status: status,
  108. Ports: convert.ToPorts(group.IPAddress, *container.Ports),
  109. })
  110. }
  111. }
  112. return res, nil
  113. }
  114. func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  115. var ports []types.ServicePortConfig
  116. for _, p := range r.Ports {
  117. ports = append(ports, types.ServicePortConfig{
  118. Target: p.ContainerPort,
  119. Published: p.HostPort,
  120. })
  121. }
  122. projectVolumes, serviceConfigVolumes, err := convert.GetRunVolumes(r.Volumes)
  123. if err != nil {
  124. return err
  125. }
  126. project := compose.Project{
  127. Name: r.ID,
  128. Config: types.Config{
  129. Services: []types.ServiceConfig{
  130. {
  131. Name: singleContainerName,
  132. Image: r.Image,
  133. Ports: ports,
  134. Labels: r.Labels,
  135. Volumes: serviceConfigVolumes,
  136. },
  137. },
  138. Volumes: projectVolumes,
  139. },
  140. }
  141. logrus.Debugf("Running container %q with name %q\n", r.Image, r.ID)
  142. groupDefinition, err := convert.ToContainerGroup(cs.ctx, project)
  143. if err != nil {
  144. return err
  145. }
  146. return createACIContainers(ctx, cs.ctx, groupDefinition)
  147. }
  148. func (cs *aciContainerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  149. return errdefs.ErrNotImplemented
  150. }
  151. func getGroupAndContainerName(containerID string) (groupName string, containerName string) {
  152. tokens := strings.Split(containerID, "_")
  153. groupName = tokens[0]
  154. if len(tokens) > 1 {
  155. containerName = tokens[len(tokens)-1]
  156. groupName = containerID[:len(containerID)-(len(containerName)+1)]
  157. } else {
  158. containerName = singleContainerName
  159. }
  160. return groupName, containerName
  161. }
  162. func (cs *aciContainerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  163. groupName, containerAciName := getGroupAndContainerName(name)
  164. containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, groupName, containerAciName)
  165. if err != nil {
  166. return err
  167. }
  168. return exec(
  169. context.Background(),
  170. *containerExecResponse.WebSocketURI,
  171. *containerExecResponse.Password,
  172. reader,
  173. writer,
  174. )
  175. }
  176. func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
  177. groupName, containerAciName := getGroupAndContainerName(containerName)
  178. logs, err := getACIContainerLogs(ctx, cs.ctx, groupName, containerAciName)
  179. if err != nil {
  180. return err
  181. }
  182. if req.Tail != "all" {
  183. tail, err := strconv.Atoi(req.Tail)
  184. if err != nil {
  185. return err
  186. }
  187. lines := strings.Split(logs, "\n")
  188. // If asked for less lines than exist, take only those lines
  189. if tail <= len(lines) {
  190. logs = strings.Join(lines[len(lines)-tail:], "\n")
  191. }
  192. }
  193. _, err = fmt.Fprint(req.Writer, logs)
  194. return err
  195. }
  196. func (cs *aciContainerService) Delete(ctx context.Context, containerID string, _ bool) error {
  197. cg, err := deleteACIContainerGroup(ctx, cs.ctx, containerID)
  198. if err != nil {
  199. return err
  200. }
  201. if cg.StatusCode == http.StatusNoContent {
  202. return ErrNoSuchContainer
  203. }
  204. return err
  205. }
  206. func (cs *aciContainerService) Inspect(ctx context.Context, containerID string) (containers.Container, error) {
  207. groupName, containerName := getGroupAndContainerName(containerID)
  208. cg, err := getACIContainerGroup(ctx, cs.ctx, groupName)
  209. if err != nil {
  210. return containers.Container{}, err
  211. }
  212. if cg.StatusCode == http.StatusNoContent {
  213. return containers.Container{}, ErrNoSuchContainer
  214. }
  215. var cc containerinstance.Container
  216. var found = false
  217. for _, c := range *cg.Containers {
  218. if to.String(c.Name) == containerName {
  219. cc = c
  220. found = true
  221. break
  222. }
  223. }
  224. if !found {
  225. return containers.Container{}, ErrNoSuchContainer
  226. }
  227. return convert.ContainerGroupToContainer(containerID, cg, cc)
  228. }
  229. type aciComposeService struct {
  230. ctx store.AciContext
  231. }
  232. func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  233. project, err := compose.ProjectFromOptions(&opts)
  234. if err != nil {
  235. return err
  236. }
  237. logrus.Debugf("Up on project with name %q\n", project.Name)
  238. groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
  239. if err != nil {
  240. return err
  241. }
  242. return createOrUpdateACIContainers(ctx, cs.ctx, groupDefinition)
  243. }
  244. func (cs *aciComposeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  245. project, err := compose.ProjectFromOptions(&opts)
  246. if err != nil {
  247. return err
  248. }
  249. logrus.Debugf("Down on project with name %q\n", project.Name)
  250. cg, err := deleteACIContainerGroup(ctx, cs.ctx, project.Name)
  251. if err != nil {
  252. return err
  253. }
  254. if cg.StatusCode == http.StatusNoContent {
  255. return ErrNoSuchContainer
  256. }
  257. return err
  258. }
  259. type aciCloudService struct {
  260. loginService login.AzureLoginService
  261. }
  262. func (cs *aciCloudService) Login(ctx context.Context, params map[string]string) error {
  263. return cs.loginService.Login(ctx)
  264. }
  265. func (cs *aciCloudService) CreateContextData(ctx context.Context, params map[string]string) (interface{}, string, error) {
  266. contextHelper := newContextCreateHelper()
  267. return contextHelper.createContextData(ctx, params)
  268. }