Class: VagrantDockerNetworksManager::ActionUp

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-docker-networks-manager/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ ActionUp

Vagrant middleware entry point that ensures the configured Docker network exists.



16
# File 'lib/vagrant-docker-networks-manager/action.rb', line 16

def initialize(app, _env); @app = app; end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-docker-networks-manager/action.rb', line 18

def call(env)
  UiHelpers.setup_i18n!
  cfg  = env[:machine].config.docker_network
  UiHelpers.setup_locale_from_config!(cfg)
  ui   = env[:ui]

  unless Util.docker_available?
    UiHelpers.error(ui, "#{UiHelpers.e(:error)} #{I18n.t('vdnm.errors.docker_unavailable')}")
    return @app.call(env)
  end

  name = cfg.network_name
  mid  = env[:machine].id

  if Util.docker_network_exists?(name)
    if owned_by_this_machine?(name, mid)
      write_marker(env, name, cfg)
      UiHelpers.say(ui, "#{UiHelpers.e(:success)} #{I18n.t('vdnm.messages.network_exists_adopted', name: name)}")
    else
      UiHelpers.say(ui, "#{UiHelpers.e(:info)} #{I18n.t('vdnm.messages.network_exists', name: name)}")
    end
  else
    subnet_label = cfg.network_subnet || "-"

    if cfg.network_subnet && !VagrantDockerNetworksManager::Util.valid_subnet?(cfg.network_subnet)
      UiHelpers.error(ui, "#{UiHelpers.e(:error)} #{I18n.t('vdnm.errors.invalid_subnet')}")
      return @app.call(env)
    end

    if cfg.network_subnet &&
       VagrantDockerNetworksManager::Util.docker_subnet_conflicts?(
         cfg.network_subnet,
         ignore_network: name
       )
      UiHelpers.error(ui, "#{UiHelpers.e(:error)} #{I18n.t('vdnm.errors.subnet_in_use')}")
      return @app.call(env)
    end

    UiHelpers.say(ui,
                  "#{UiHelpers.e(:ongoing)} #{I18n.t('vdnm.log.create_network', name: name, subnet: subnet_label)}")
    builder = NetworkBuilder.new(cfg, machine_id: mid)
    args = builder.build_create_command_args
    if Util.sh!(*args)
      write_marker(env, name, cfg)
      UiHelpers.say(ui, "#{UiHelpers.e(:success)} #{I18n.t('vdnm.log.ok')}")
    else
      rendered = args.shelljoin
      UiHelpers.error(ui, "#{UiHelpers.e(:error)} #{I18n.t('vdnm.messages.create_failed', cmd: rendered)}")
    end
  end

  @app.call(env)
end