Class: VagrantPlugins::OneCloud::Commands::AddNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-1cloud/commands/add_network.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject

Show description when ‘vagrant list-commands` is triggered



9
10
11
# File 'lib/vagrant-1cloud/commands/add_network.rb', line 9

def self.synopsis
  "plugin: vagrant-1cloud: adds VPS to specific private network"
end

Instance Method Details

#executeObject



13
14
15
16
17
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
# File 'lib/vagrant-1cloud/commands/add_network.rb', line 13

def execute
  options = {}

  optparse = OptionParser.new do |opts|
    opts.banner = 'Usage: vagrant add-network [vm-name] [options]'

    opts.on('-n', '--net NETNAME', 'Network name') do |net|
      options[:Net] = net
    end

    options[:IP] = nil
    opts.on('-i', '--ip [IP]', 'Private IP address') do |ip|
      options[:IP] = ip
    end

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end

  begin
    optparse.parse!
    mandatory = [:Net]
    missing = mandatory.select{ |param| options[param].nil? }
    unless missing.empty?
      raise OptionParser::MissingArgument.new(missing.join(', '))
    end
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    puts $!.to_s
    puts optparse
    exit
  end

  argv = parse_options(optparse)

  with_target_vms(argv) do |machine|
    machine.provider_config.private_net = {options[:Net] => options[:IP]}
    machine.action(:addnet)
  end

  0
end