create.go 19 KB

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