Class: VagrantPlugins::OneCloud::Actions::PrivateNetwork

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable, Helpers::Client
Defined in:
lib/vagrant-1cloud/actions/private_network.rb

Instance Method Summary collapse

Methods included from Helpers::Client

#client

Constructor Details

#initialize(app, env) ⇒ PrivateNetwork

Returns a new instance of PrivateNetwork.



11
12
13
14
15
16
# File 'lib/vagrant-1cloud/actions/private_network.rb', line 11

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @client = client
  @logger = Log4r::Logger.new('vagrant::onecloud::private_network')
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
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
# File 'lib/vagrant-1cloud/actions/private_network.rb', line 18

def call(env)
  # check if network name is set
  return @app.call(env) unless @machine.provider_config.private_net

  @machine.provider_config.private_net.each do |net, ip|
    # Getting private network by name
    result = @client.request('/network')
    private_network = result['body'].find { |network| network['Name'] == net.to_s }

    raise "Private network #{net} is not created" if !private_network

    # Checking if machine is already added to network
    result = @client.request("/server/#{@machine.id}")
    linked_network = result['body']['LinkedNetworks'].find { |network| network['NetworkID'] == private_network['ID'] }

    if linked_network
        env[:ui].info I18n.t('vagrant_1cloud.info.already_connected', network: net)
        next
    end

    # Adding server to specified network
    result = @client.post("/server/#{@machine.id}/Action", {
        :Type => "AddNetwork",
        :NetworkID => private_network['ID']
    })

    # Waiting for server to add to private network
    env[:ui].info I18n.t('vagrant_1cloud.info.setting_private_network')
    @client.wait_for_event(env, @machine.id, result['body']['ID'])

    # refresh droplet state with provider
    Provider.droplet(@machine, :refresh => true)

    result = @client.request("/server/#{@machine.id}")
    linked_network = result['body']['LinkedNetworks'].find { |network| network['NetworkID'] == private_network['ID'] }

    if !ip
      ip = linked_network['IP']
    end

    # override ssh username to root temporarily
    user = @machine.config.ssh.username
    @machine.config.ssh.username = 'root'

    # set private network rules
    if private_network['IsDHCP']
      @machine.communicate.execute(<<-BASH)
        ifdown -a
  
        export INTERFACE=$(ifconfig -a | grep #{linked_network['MAC']} | awk '{print $1}')
        export MATCHADDR=#{linked_network['MAC']}
        export MATCHID=$(ifconfig -a | grep #{linked_network['MAC']} | awk 'system("udevadm info /sys/class/net/" $1)' | grep P: | awk -F/ '{print $(NF-2)}')
        /lib/udev/write_net_rules
        udevadm control --reload-rules && udevadm trigger
        
        echo >> /etc/network/interfaces
        ifconfig -a | grep #{linked_network['MAC']} | awk '{print "auto " $1}' >> /etc/network/interfaces
        ifconfig -a | grep #{linked_network['MAC']} | awk '{print "iface " $1 " inet dhcp"}' >> /etc/network/interfaces
        
        ifup -a
      BASH
    else
      @machine.communicate.execute(<<-BASH)
        ifdown -a
  
        export INTERFACE=$(ifconfig -a | grep #{linked_network['MAC']} | awk '{print $1}')
        export MATCHADDR=#{linked_network['MAC']}
        export MATCHID=$(ifconfig -a | grep #{linked_network['MAC']} | awk 'system("udevadm info /sys/class/net/" $1)' | grep P: | awk -F/ '{print $(NF-2)}')
        /lib/udev/write_net_rules
        udevadm control --reload-rules && udevadm trigger
        
        echo >> /etc/network/interfaces
        ifconfig -a | grep #{linked_network['MAC']} | awk '{print "auto " $1}' >> /etc/network/interfaces
        ifconfig -a | grep #{linked_network['MAC']} | awk '{print "iface " $1 " inet static"}' >> /etc/network/interfaces
        echo "address #{ip}" >> /etc/network/interfaces
        echo "netmask #{private_network['Mask']}" >> /etc/network/interfaces
        
        ifup -a
      BASH
    end

    # reset username
    @machine.config.ssh.username = user
  end

  @app.call(env)
end