Class: VagrantPlugins::Skytap::Command::Up

Inherits:
CommandUp::Command
  • Object
show all
Defined in:
lib/vagrant-skytap/command/up.rb

Instance Method Summary collapse

Instance Method Details

#bring_up_machines(machines, options = {}) ⇒ Array

Custom handling for Skytap environments. Creating and running happens in multiple phases:

  • Compose Skytap environment from groups of VMs, or add groups of machines to an existing environment, using optimal number of API calls.

  • Customize the VMs.

  • Run the VMs. If parallelized this happens with a single API call.

Parameters:

  • machines (Array)

    The [Vagrant::Machine] objects to bring up

  • options (Hash) (defaults to: {})

Returns:

  • (Array)

    The [Vagrant::Machine] objects



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/vagrant-skytap/command/up.rb', line 146

def bring_up_machines(machines, options={})
  return [] unless machines.present?

  # Invoke once for the entire set of machines.
  result = machines.first.action(:create, options.merge(machines: machines))

  # Lets us eliminate some redundant API calls
  cached_objects = {
    api_client:     result[:api_client],
    environment:    result[:environment],
    machines:       machines,
    initial_states: machines.inject({}) {|acc, m| acc[m.id] = m.state.id ; acc },
  }

  machines.each do |machine|
    machine.action(:update_hardware, options.merge(cached_objects))
  end

  machines.each do |machine|
    @env.ui.info(I18n.t(
      "vagrant.commands.up.upping",
      name: machine.name,
      provider: machine.provider_name))
    machine.action(:run_vm, options.merge(cached_objects))
  end
end

#executeObject



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vagrant-skytap/command/up.rb', line 38

def execute
  options = {}
  options[:destroy_on_error] = true
  options[:parallel] = true
  options[:provision_ignore_sentinel] = false

  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant up [options] [name]"
    o.separator ""
    o.separator "Options:"
    o.separator ""

    build_start_options(o, options)

    o.on("--[no-]destroy-on-error",
         "Destroy machine if any fatal error happens (default to true)") do |destroy|
      options[:destroy_on_error] = destroy
    end

    o.on("--[no-]parallel",
         "Enable or disable parallelism if provider supports it") do |parallel|
      options[:parallel] = parallel
    end

    o.on("--provider PROVIDER", String,
         "Back the machine with a specific provider") do |provider|
      options[:provider] = provider
    end
  end

  # Parse the options
  argv = parse_options(opts)
  return if !argv

  # Validate the provisioners
  if method(:validate_provisioner_flags!).arity == 1
    validate_provisioner_flags!(options)
  else
    # Second argument was added in Vagrant 1.8.0
    # https://github.com/mitchellh/vagrant/pull/5981
    validate_provisioner_flags!(options, argv)
  end

  # Go over each VM and bring it up
  @logger.debug("'Up' each target VM...")

  machines = []
  names = argv
  if names.empty?
    autostart = false
    @env.vagrantfile.machine_names_and_options.each do |n, o|
      autostart = true if o.key?(:autostart)
      o[:autostart] = true if !o.key?(:autostart)
      names << n.to_s if o[:autostart]
    end

    # If we have an autostart key but no names, it means that
    # all machines are autostart: false and we don't start anything.
    names = nil if autostart && names.empty?
  end

  # Collect the machines first so we know what their provider
  # is, and decide whether to call the Vagrant core implementation
  # of this command.
  with_target_vms(names, provider: options[:provider]) {|machine| machines << machine}
  unless machines.first.provider_name == :skytap
    @logger.debug("Calling default 'Up' implementation.")
    return super
  end

  @logger.debug("Executing Skytap 'Up' implementation.")
  bring_up_machines(machines, options)

  if machines.empty?
    @env.ui.info(I18n.t("vagrant.up_no_machines"))
    return 0
  end

  # Output the post-up messages that we have, if any
  machines.each do |m|
    next if !m.config.vm.post_up_message
    next if m.config.vm.post_up_message == ""

    # Add a newline to separate things.
    @env.ui.info("", prefix: false)

    m.ui.success(I18n.t(
      "vagrant.post_up_message",
      name: m.name.to_s,
      message: m.config.vm.post_up_message))
  end

  # Success, exit status 0
  0
end