create.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "github.com/compose-spec/compose-go/types"
  21. moby "github.com/docker/docker/api/types"
  22. "github.com/docker/docker/api/types/container"
  23. "github.com/docker/docker/api/types/filters"
  24. "github.com/docker/docker/api/types/mount"
  25. "github.com/docker/docker/api/types/network"
  26. "github.com/docker/docker/api/types/strslice"
  27. volume_api "github.com/docker/docker/api/types/volume"
  28. "github.com/docker/docker/errdefs"
  29. "github.com/docker/go-connections/nat"
  30. "github.com/pkg/errors"
  31. "github.com/sirupsen/logrus"
  32. "github.com/docker/compose-cli/api/compose"
  33. "github.com/docker/compose-cli/api/progress"
  34. convert "github.com/docker/compose-cli/local/moby"
  35. )
  36. func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  37. err := s.ensureImagesExists(ctx, project)
  38. if err != nil {
  39. return err
  40. }
  41. prepareNetworks(project)
  42. err = prepareVolumes(project)
  43. if err != nil {
  44. return err
  45. }
  46. if err := s.ensureNetworks(ctx, project.Networks); err != nil {
  47. return err
  48. }
  49. if err := s.ensureProjectVolumes(ctx, project); err != nil {
  50. return err
  51. }
  52. var observedState Containers
  53. observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  54. Filters: filters.NewArgs(projectFilter(project.Name)),
  55. All: true,
  56. })
  57. if err != nil {
  58. return err
  59. }
  60. orphans := observedState.filter(isNotService(project.ServiceNames()...))
  61. if len(orphans) > 0 {
  62. if opts.RemoveOrphans {
  63. w := progress.ContextWriter(ctx)
  64. err := s.removeContainers(ctx, w, orphans)
  65. if err != nil {
  66. return err
  67. }
  68. } else {
  69. logrus.Warnf("Found orphan containers (%s) for this project. If "+
  70. "you removed or renamed this service in your compose "+
  71. "file, you can run this command with the "+
  72. "--remove-orphans flag to clean it up.", orphans.names())
  73. }
  74. }
  75. return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  76. return s.ensureService(c, observedState, project, service, opts.Recreate)
  77. })
  78. }
  79. func prepareVolumes(p *types.Project) error {
  80. for i := range p.Services {
  81. volumesFrom, dependServices, err := getVolumesFrom(p, p.Services[i].VolumesFrom)
  82. if err != nil {
  83. return err
  84. }
  85. p.Services[i].VolumesFrom = volumesFrom
  86. if len(dependServices) > 0 {
  87. if p.Services[i].DependsOn == nil {
  88. p.Services[i].DependsOn = make(types.DependsOnConfig, len(dependServices))
  89. }
  90. for _, service := range p.Services {
  91. if contains(dependServices, service.Name) {
  92. p.Services[i].DependsOn[service.Name] = types.ServiceDependency{
  93. Condition: types.ServiceConditionStarted,
  94. }
  95. }
  96. }
  97. }
  98. }
  99. return nil
  100. }
  101. func prepareNetworks(project *types.Project) {
  102. for k, network := range project.Networks {
  103. network.Labels = network.Labels.Add(networkLabel, k)
  104. network.Labels = network.Labels.Add(projectLabel, project.Name)
  105. network.Labels = network.Labels.Add(versionLabel, ComposeVersion)
  106. project.Networks[k] = network
  107. }
  108. }
  109. func (s *composeService) ensureNetworks(ctx context.Context, networks types.Networks) error {
  110. for _, network := range networks {
  111. err := s.ensureNetwork(ctx, network)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. return nil
  117. }
  118. func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project) error {
  119. for k, volume := range project.Volumes {
  120. volume.Labels = volume.Labels.Add(volumeLabel, k)
  121. volume.Labels = volume.Labels.Add(projectLabel, project.Name)
  122. volume.Labels = volume.Labels.Add(versionLabel, ComposeVersion)
  123. err := s.ensureVolume(ctx, volume)
  124. if err != nil {
  125. return err
  126. }
  127. }
  128. return nil
  129. }
  130. func getImageName(service types.ServiceConfig, projectName string) string {
  131. imageName := service.Image
  132. if imageName == "" {
  133. imageName = projectName + "_" + service.Name
  134. }
  135. return imageName
  136. }
  137. func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig, number int, inherit *moby.Container,
  138. autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
  139. hash, err := jsonHash(service)
  140. if err != nil {
  141. return nil, nil, nil, err
  142. }
  143. labels := map[string]string{}
  144. for k, v := range service.Labels {
  145. labels[k] = v
  146. }
  147. labels[projectLabel] = p.Name
  148. labels[serviceLabel] = service.Name
  149. labels[versionLabel] = ComposeVersion
  150. if _, ok := service.Labels[oneoffLabel]; !ok {
  151. labels[oneoffLabel] = "False"
  152. }
  153. labels[configHashLabel] = hash
  154. labels[workingDirLabel] = p.WorkingDir
  155. labels[configFilesLabel] = strings.Join(p.ComposeFiles, ",")
  156. labels[containerNumberLabel] = strconv.Itoa(number)
  157. var (
  158. runCmd strslice.StrSlice
  159. entrypoint strslice.StrSlice
  160. )
  161. if len(service.Command) > 0 {
  162. runCmd = strslice.StrSlice(service.Command)
  163. }
  164. if len(service.Entrypoint) > 0 {
  165. entrypoint = strslice.StrSlice(service.Entrypoint)
  166. }
  167. var (
  168. tty = service.Tty
  169. stdinOpen = service.StdinOpen
  170. attachStdin = false
  171. )
  172. volumeMounts, binds, mounts, err := s.buildContainerVolumes(ctx, *p, service, inherit)
  173. if err != nil {
  174. return nil, nil, nil, err
  175. }
  176. containerConfig := container.Config{
  177. Hostname: service.Hostname,
  178. Domainname: service.DomainName,
  179. User: service.User,
  180. ExposedPorts: buildContainerPorts(service),
  181. Tty: tty,
  182. OpenStdin: stdinOpen,
  183. StdinOnce: true,
  184. AttachStdin: attachStdin,
  185. AttachStderr: true,
  186. AttachStdout: true,
  187. Cmd: runCmd,
  188. Image: getImageName(service, p.Name),
  189. WorkingDir: service.WorkingDir,
  190. Entrypoint: entrypoint,
  191. NetworkDisabled: service.NetworkMode == "disabled",
  192. MacAddress: service.MacAddress,
  193. Labels: labels,
  194. StopSignal: service.StopSignal,
  195. Env: convert.ToMobyEnv(service.Environment),
  196. Healthcheck: convert.ToMobyHealthCheck(service.HealthCheck),
  197. Volumes: volumeMounts,
  198. StopTimeout: convert.ToSeconds(service.StopGracePeriod),
  199. }
  200. portBindings := buildContainerPortBindingOptions(service)
  201. resources := getDeployResources(service)
  202. networkMode := getNetworkMode(p, service)
  203. hostConfig := container.HostConfig{
  204. AutoRemove: autoRemove,
  205. Binds: binds,
  206. Mounts: mounts,
  207. CapAdd: strslice.StrSlice(service.CapAdd),
  208. CapDrop: strslice.StrSlice(service.CapDrop),
  209. NetworkMode: networkMode,
  210. Init: service.Init,
  211. ReadonlyRootfs: service.ReadOnly,
  212. // ShmSize: , TODO
  213. Sysctls: service.Sysctls,
  214. PortBindings: portBindings,
  215. Resources: resources,
  216. VolumeDriver: service.VolumeDriver,
  217. VolumesFrom: service.VolumesFrom,
  218. }
  219. networkConfig := buildDefaultNetworkConfig(service, networkMode, getContainerName(p.Name, service, number))
  220. return &containerConfig, &hostConfig, networkConfig, nil
  221. }
  222. func getDeployResources(s types.ServiceConfig) container.Resources {
  223. resources := container.Resources{}
  224. if s.Deploy == nil {
  225. return resources
  226. }
  227. reservations := s.Deploy.Resources.Reservations
  228. if reservations == nil || len(reservations.Devices) == 0 {
  229. return resources
  230. }
  231. for _, device := range reservations.Devices {
  232. resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
  233. Capabilities: [][]string{device.Capabilities},
  234. Count: int(device.Count),
  235. DeviceIDs: device.IDs,
  236. Driver: device.Driver,
  237. })
  238. }
  239. return resources
  240. }
  241. func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
  242. ports := nat.PortSet{}
  243. for _, p := range s.Ports {
  244. p := nat.Port(fmt.Sprintf("%d/%s", p.Target, p.Protocol))
  245. ports[p] = struct{}{}
  246. }
  247. return ports
  248. }
  249. func buildContainerPortBindingOptions(s types.ServiceConfig) nat.PortMap {
  250. bindings := nat.PortMap{}
  251. for _, port := range s.Ports {
  252. p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol))
  253. bind := []nat.PortBinding{}
  254. binding := nat.PortBinding{}
  255. if port.Published > 0 {
  256. binding.HostPort = fmt.Sprint(port.Published)
  257. }
  258. bind = append(bind, binding)
  259. bindings[p] = bind
  260. }
  261. return bindings
  262. }
  263. func getVolumesFrom(project *types.Project, volumesFrom []string) ([]string, []string, error) {
  264. var volumes = []string{}
  265. var services = []string{}
  266. // parse volumes_from
  267. if len(volumesFrom) == 0 {
  268. return volumes, services, nil
  269. }
  270. for _, vol := range volumesFrom {
  271. spec := strings.Split(vol, ":")
  272. if len(spec) == 0 {
  273. continue
  274. }
  275. if spec[0] == "container" {
  276. volumes = append(volumes, strings.Join(spec[1:], ":"))
  277. continue
  278. }
  279. serviceName := spec[0]
  280. services = append(services, serviceName)
  281. service, err := project.GetService(serviceName)
  282. if err != nil {
  283. return nil, nil, err
  284. }
  285. firstContainer := getContainerName(project.Name, service, 1)
  286. v := fmt.Sprintf("%s:%s", firstContainer, strings.Join(spec[1:], ":"))
  287. volumes = append(volumes, v)
  288. }
  289. return volumes, services, nil
  290. }
  291. func (s *composeService) buildContainerVolumes(ctx context.Context, p types.Project, service types.ServiceConfig,
  292. inherit *moby.Container) (map[string]struct{}, []string, []mount.Mount, error) {
  293. var mounts = []mount.Mount{}
  294. image := getImageName(service, p.Name)
  295. imgInspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, image)
  296. if err != nil {
  297. return nil, nil, nil, err
  298. }
  299. mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
  300. if err != nil {
  301. return nil, nil, nil, err
  302. }
  303. // filter binds and volumes mount targets
  304. volumeMounts := map[string]struct{}{}
  305. binds := []string{}
  306. for _, m := range mountOptions {
  307. if m.Type == mount.TypeVolume {
  308. volumeMounts[m.Target] = struct{}{}
  309. if m.Source != "" {
  310. binds = append(binds, fmt.Sprintf("%s:%s:rw", m.Source, m.Target))
  311. }
  312. }
  313. }
  314. for _, m := range mountOptions {
  315. if m.Type == mount.TypeBind || m.Type == mount.TypeTmpfs {
  316. mounts = append(mounts, m)
  317. }
  318. }
  319. return volumeMounts, binds, mounts, nil
  320. }
  321. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
  322. var mounts = map[string]mount.Mount{}
  323. if inherit != nil {
  324. for _, m := range inherit.Mounts {
  325. if m.Type == "tmpfs" {
  326. continue
  327. }
  328. src := m.Source
  329. if m.Type == "volume" {
  330. src = m.Name
  331. }
  332. mounts[m.Destination] = mount.Mount{
  333. Type: m.Type,
  334. Source: src,
  335. Target: m.Destination,
  336. ReadOnly: !m.RW,
  337. }
  338. }
  339. }
  340. if img.ContainerConfig != nil {
  341. for k := range img.ContainerConfig.Volumes {
  342. m, err := buildMount(p, types.ServiceVolumeConfig{
  343. Type: types.VolumeTypeVolume,
  344. Target: k,
  345. })
  346. if err != nil {
  347. return nil, err
  348. }
  349. mounts[k] = m
  350. }
  351. }
  352. mounts, err := fillBindMounts(p, s, mounts)
  353. if err != nil {
  354. return nil, err
  355. }
  356. values := make([]mount.Mount, 0, len(mounts))
  357. for _, v := range mounts {
  358. values = append(values, v)
  359. }
  360. return values, nil
  361. }
  362. func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
  363. for _, v := range s.Volumes {
  364. bindMount, err := buildMount(p, v)
  365. if err != nil {
  366. return nil, err
  367. }
  368. m[bindMount.Target] = bindMount
  369. }
  370. secrets, err := buildContainerSecretMounts(p, s)
  371. if err != nil {
  372. return nil, err
  373. }
  374. for _, s := range secrets {
  375. if _, found := m[s.Target]; found {
  376. continue
  377. }
  378. m[s.Target] = s
  379. }
  380. configs, err := buildContainerConfigMounts(p, s)
  381. if err != nil {
  382. return nil, err
  383. }
  384. for _, c := range configs {
  385. if _, found := m[c.Target]; found {
  386. continue
  387. }
  388. m[c.Target] = c
  389. }
  390. return m, nil
  391. }
  392. func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  393. var mounts = map[string]mount.Mount{}
  394. configsBaseDir := "/"
  395. for _, config := range s.Configs {
  396. target := config.Target
  397. if config.Target == "" {
  398. target = filepath.Join(configsBaseDir, config.Source)
  399. } else if !filepath.IsAbs(config.Target) {
  400. target = filepath.Join(configsBaseDir, config.Target)
  401. }
  402. definedConfig := p.Configs[config.Source]
  403. if definedConfig.External.External {
  404. return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
  405. }
  406. bindMount, err := buildMount(p, types.ServiceVolumeConfig{
  407. Type: types.VolumeTypeBind,
  408. Source: definedConfig.File,
  409. Target: target,
  410. ReadOnly: true,
  411. })
  412. if err != nil {
  413. return nil, err
  414. }
  415. mounts[target] = bindMount
  416. }
  417. values := make([]mount.Mount, 0, len(mounts))
  418. for _, v := range mounts {
  419. values = append(values, v)
  420. }
  421. return values, nil
  422. }
  423. func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  424. var mounts = map[string]mount.Mount{}
  425. secretsDir := "/run/secrets"
  426. for _, secret := range s.Secrets {
  427. target := secret.Target
  428. if secret.Target == "" {
  429. target = filepath.Join(secretsDir, secret.Source)
  430. } else if !filepath.IsAbs(secret.Target) {
  431. target = filepath.Join(secretsDir, secret.Target)
  432. }
  433. definedSecret := p.Secrets[secret.Source]
  434. if definedSecret.External.External {
  435. return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
  436. }
  437. mount, err := buildMount(p, types.ServiceVolumeConfig{
  438. Type: types.VolumeTypeBind,
  439. Source: definedSecret.File,
  440. Target: target,
  441. ReadOnly: true,
  442. })
  443. if err != nil {
  444. return nil, err
  445. }
  446. mounts[target] = mount
  447. }
  448. values := make([]mount.Mount, 0, len(mounts))
  449. for _, v := range mounts {
  450. values = append(values, v)
  451. }
  452. return values, nil
  453. }
  454. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  455. source := volume.Source
  456. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) {
  457. // volume source has already been prefixed with workdir if required, by compose-go project loader
  458. var err error
  459. source, err = filepath.Abs(source)
  460. if err != nil {
  461. return mount.Mount{}, err
  462. }
  463. }
  464. if volume.Type == types.VolumeTypeVolume {
  465. if volume.Source != "" {
  466. pVolume, ok := project.Volumes[volume.Source]
  467. if ok {
  468. source = pVolume.Name
  469. }
  470. }
  471. }
  472. return mount.Mount{
  473. Type: mount.Type(volume.Type),
  474. Source: source,
  475. Target: volume.Target,
  476. ReadOnly: volume.ReadOnly,
  477. Consistency: mount.Consistency(volume.Consistency),
  478. BindOptions: buildBindOption(volume.Bind),
  479. VolumeOptions: buildVolumeOptions(volume.Volume),
  480. TmpfsOptions: buildTmpfsOptions(volume.Tmpfs),
  481. }, nil
  482. }
  483. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  484. if bind == nil {
  485. return nil
  486. }
  487. return &mount.BindOptions{
  488. Propagation: mount.Propagation(bind.Propagation),
  489. // NonRecursive: false, FIXME missing from model ?
  490. }
  491. }
  492. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  493. if vol == nil {
  494. return nil
  495. }
  496. return &mount.VolumeOptions{
  497. NoCopy: vol.NoCopy,
  498. // Labels: , // FIXME missing from model ?
  499. // DriverConfig: , // FIXME missing from model ?
  500. }
  501. }
  502. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  503. if tmpfs == nil {
  504. return nil
  505. }
  506. return &mount.TmpfsOptions{
  507. SizeBytes: tmpfs.Size,
  508. // Mode: , // FIXME missing from model ?
  509. }
  510. }
  511. func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode, containerName string) *network.NetworkingConfig {
  512. config := map[string]*network.EndpointSettings{}
  513. net := string(networkMode)
  514. config[net] = &network.EndpointSettings{
  515. Aliases: append(getAliases(s, s.Networks[net]), containerName),
  516. }
  517. return &network.NetworkingConfig{
  518. EndpointsConfig: config,
  519. }
  520. }
  521. func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
  522. aliases := []string{s.Name}
  523. if c != nil {
  524. aliases = append(aliases, c.Aliases...)
  525. }
  526. return aliases
  527. }
  528. func getNetworkMode(p *types.Project, service types.ServiceConfig) container.NetworkMode {
  529. mode := service.NetworkMode
  530. if mode == "" {
  531. if len(p.Networks) > 0 {
  532. for name := range getNetworksForService(service) {
  533. return container.NetworkMode(p.Networks[name].Name)
  534. }
  535. }
  536. return container.NetworkMode("none")
  537. }
  538. // FIXME incomplete implementation
  539. if strings.HasPrefix(mode, "service:") {
  540. panic("Not yet implemented")
  541. }
  542. if strings.HasPrefix(mode, "container:") {
  543. panic("Not yet implemented")
  544. }
  545. return container.NetworkMode(mode)
  546. }
  547. func getNetworksForService(s types.ServiceConfig) map[string]*types.ServiceNetworkConfig {
  548. if len(s.Networks) > 0 {
  549. return s.Networks
  550. }
  551. return map[string]*types.ServiceNetworkConfig{"default": nil}
  552. }
  553. func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
  554. _, err := s.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
  555. if err != nil {
  556. if errdefs.IsNotFound(err) {
  557. if n.External.External {
  558. return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  559. }
  560. createOpts := moby.NetworkCreate{
  561. // TODO NameSpace Labels
  562. Labels: n.Labels,
  563. Driver: n.Driver,
  564. Options: n.DriverOpts,
  565. Internal: n.Internal,
  566. Attachable: n.Attachable,
  567. }
  568. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  569. createOpts.IPAM = &network.IPAM{}
  570. }
  571. if n.Ipam.Driver != "" {
  572. createOpts.IPAM.Driver = n.Ipam.Driver
  573. }
  574. for _, ipamConfig := range n.Ipam.Config {
  575. config := network.IPAMConfig{
  576. Subnet: ipamConfig.Subnet,
  577. }
  578. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  579. }
  580. networkEventName := fmt.Sprintf("Network %q", n.Name)
  581. w := progress.ContextWriter(ctx)
  582. w.Event(progress.CreatingEvent(networkEventName))
  583. if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
  584. w.Event(progress.ErrorEvent(networkEventName))
  585. return errors.Wrapf(err, "failed to create network %s", n.Name)
  586. }
  587. w.Event(progress.CreatedEvent(networkEventName))
  588. return nil
  589. }
  590. return err
  591. }
  592. return nil
  593. }
  594. func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string, networkName string) error {
  595. w := progress.ContextWriter(ctx)
  596. eventName := fmt.Sprintf("Network %q", networkName)
  597. w.Event(progress.RemovingEvent(eventName))
  598. if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
  599. w.Event(progress.ErrorEvent(eventName))
  600. return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID))
  601. }
  602. w.Event(progress.RemovedEvent(eventName))
  603. return nil
  604. }
  605. func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error {
  606. // TODO could identify volume by label vs name
  607. _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
  608. if err != nil {
  609. if !errdefs.IsNotFound(err) {
  610. return err
  611. }
  612. eventName := fmt.Sprintf("Volume %q", volume.Name)
  613. w := progress.ContextWriter(ctx)
  614. w.Event(progress.CreatingEvent(eventName))
  615. _, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
  616. Labels: volume.Labels,
  617. Name: volume.Name,
  618. Driver: volume.Driver,
  619. DriverOpts: volume.DriverOpts,
  620. })
  621. if err != nil {
  622. w.Event(progress.ErrorEvent(eventName))
  623. return err
  624. }
  625. w.Event(progress.CreatedEvent(eventName))
  626. }
  627. return nil
  628. }