mirror of
https://github.com/OpenNebula/one.git
synced 2026-12-02 05:06:14 +09:00
B #-: Fix multiple IP assigment in Packet
This commit is contained in:
3 files changed
+56
-23
No files matched your search
@@ -100,6 +100,8 @@ require 'ipaddr'
|
||||
|
||||
IP_TYPE = %w[public_ipv4 global_ipv4]
|
||||
|
||||
DEFAULT_PRIVATE_CIDR = "172.0.0.0/8"
|
||||
|
||||
# Add ^ operator to the IPAddr class
|
||||
class IPAddr
|
||||
|
||||
@@ -141,6 +143,12 @@ begin
|
||||
# --------------------------------------------------------------------------
|
||||
# Connect to Packet and allocate a new IP
|
||||
# --------------------------------------------------------------------------
|
||||
private_cidr = data.xpath('//AR/PRIVATE_CIDR').text
|
||||
|
||||
if private_cidr.empty?
|
||||
private_cidr = DEFAULT_PRIVATE_CIDR
|
||||
end
|
||||
|
||||
packet = Packet::Client.new
|
||||
packet.auth_token = pk_token
|
||||
|
||||
@@ -165,15 +173,21 @@ begin
|
||||
|
||||
packet.create_ip(ip)
|
||||
|
||||
ipgw = IPAddr.new(ip.address) ^ 1
|
||||
cidr = IPAddr.new(private_cidr)
|
||||
ipmd5 = Digest::MD5.hexdigest("#{ip.address}").to_i(16) & 0x0FFFFFF
|
||||
eip = IPAddr.new(ipmd5, Socket::AF_INET)
|
||||
|
||||
ipvm = (eip & 0x00FFFFFF) | cidr.mask(8)
|
||||
ipgw = ipvm ^ 1
|
||||
|
||||
puts <<-EOF
|
||||
AR = [
|
||||
TYPE = "IP4",
|
||||
IP = "#{ip.address}",
|
||||
IP = "#{ipvm}",
|
||||
SIZE = "#{ip.quantity}",
|
||||
IPAM_MAD = "packet",
|
||||
GATEWAY = "#{ipgw}",
|
||||
EXTERNAL_IP = "#{ip.address}",
|
||||
NETWORK_MASK = "255.255.255.254",
|
||||
PACKET_IP_ID = "#{ip.id}",
|
||||
PROVISION_ID = "#{provision_id}"
|
||||
|
||||
@@ -64,32 +64,47 @@ class ElasticDriver < VNMMAD::VNMDriver
|
||||
|
||||
# Create route and arp proxy
|
||||
def activate
|
||||
cmds = VNMMAD::VNMNetwork::Commands.new
|
||||
provider = ElasticDriver.provider(@provider, @host)
|
||||
|
||||
attach_nic_id = @vm['TEMPLATE/NIC[ATTACH="YES"]/NIC_ID']
|
||||
|
||||
process do |nic|
|
||||
next if attach_nic_id && attach_nic_id != nic[:nic_id]
|
||||
|
||||
ip("route add #{nic[:ip]}/32 dev #{nic[:bridge]}")
|
||||
ip("neighbour add proxy #{nic[:gateway]} dev #{nic[:bridge]}")
|
||||
cmds.add :ip, "route add #{nic[:ip]}/32 dev #{nic[:bridge]}"
|
||||
cmds.add :ip, "neighbour add proxy #{nic[:gateway]} dev #{nic[:bridge]}"
|
||||
|
||||
provider.activate(cmds, nic) if provider.respond_to? :activate
|
||||
end
|
||||
|
||||
cmds.run_remote(@ssh)
|
||||
|
||||
0
|
||||
end
|
||||
|
||||
# Remove route and arp proxy
|
||||
def deactivate
|
||||
cmds = VNMMAD::VNMNetwork::Commands.new
|
||||
provider = ElasticDriver.provider(@provider, @host)
|
||||
|
||||
attach_nic_id = @vm['TEMPLATE/NIC[ATTACH="YES"]/NIC_ID']
|
||||
|
||||
process do |nic|
|
||||
next if attach_nic_id && attach_nic_id != nic[:nic_id]
|
||||
|
||||
ip("route del #{nic[:ip]}/32 dev #{nic[:bridge]} | true")
|
||||
ip("neighbour del proxy #{nic[:gateway]} dev #{nic[:bridge]} " <<
|
||||
'| true')
|
||||
cmds.add :ip, "route del #{nic[:ip]}/32 dev #{nic[:bridge]} | true"
|
||||
cmds.add :ip, "neigh del proxy #{nic[:gateway]} dev #{nic[:bridge]} | true"
|
||||
|
||||
provider.deactivate(cmds, nic) if provider.respond_to? :deactivate
|
||||
|
||||
next if nic[:conf][:keep_empty_bridge]
|
||||
|
||||
ip("link delete #{nic[:bridge]} | true")
|
||||
cmds.add :ip, "link delete #{nic[:bridge]} | true"
|
||||
end
|
||||
|
||||
cmds.run_remote(@ssh)
|
||||
|
||||
0
|
||||
end
|
||||
|
||||
@@ -159,14 +174,5 @@ class ElasticDriver < VNMMAD::VNMDriver
|
||||
|
||||
private
|
||||
|
||||
# Run ip command with given params on @ssh stream
|
||||
# @param params [String]
|
||||
# @return [String] command stdout
|
||||
def ip(params)
|
||||
commands = VNMMAD::VNMNetwork::Commands.new
|
||||
commands.add :ip, params
|
||||
commands.run_remote(@ssh)
|
||||
end
|
||||
|
||||
end
|
||||
# rubocop:enable Naming/FileName
|
||||
@@ -52,26 +52,39 @@ class PacketProvider
|
||||
@deploy_id = host['TEMPLATE/PROVISION/DEPLOY_ID']
|
||||
end
|
||||
|
||||
def assign(ip, _external, _opts = {})
|
||||
@client.assign_cidr_device("#{ip}/32", @deploy_id)
|
||||
def assign(_ip, external, _opts = {})
|
||||
@client.assign_cidr_device("#{external}/32", @deploy_id)
|
||||
0
|
||||
rescue StandardError => e
|
||||
OpenNebula.log_error("Error assiging #{ip}:#{e.message}")
|
||||
OpenNebula.log_error("Error assiging #{external}:#{e.message}")
|
||||
1
|
||||
end
|
||||
|
||||
def unassign(ip, _external)
|
||||
def unassign(_ip, external)
|
||||
dev = @client.get_device(@deploy_id)
|
||||
|
||||
ip = dev.ip_addresses.select do |i|
|
||||
i['address'] == ip &&
|
||||
i['address'] == external &&
|
||||
i['cidr'] == 32 &&
|
||||
i['address_family'] == 4
|
||||
end
|
||||
|
||||
@client.delete_ip(ip[0]['id'])
|
||||
rescue StandardError => e
|
||||
OpenNebula.log_error("Error assiging #{ip}:#{e.message}")
|
||||
OpenNebula.log_error("Error assiging #{external}:#{e.message}")
|
||||
end
|
||||
|
||||
def activate(cmds, nic)
|
||||
cmds.add :iptables, "-t nat -A POSTROUTING -s #{nic[:ip]} -j SNAT"\
|
||||
" --to-source #{nic[:external_ip]}"
|
||||
cmds.add :iptables, "-t nat -A PREROUTING -d #{nic[:external_ip]} -j DNAT"\
|
||||
" --to-destination #{nic[:ip]}"
|
||||
end
|
||||
|
||||
def deactivate(cmds, nic)
|
||||
cmds.add :iptables, "-t nat -D POSTROUTING -s #{nic[:ip]} -j SNAT"\
|
||||
" --to-source #{nic[:external_ip]}"
|
||||
cmds.add :iptables, "-t nat -D PREROUTING -d #{nic[:external_ip]} -j DNAT"\
|
||||
" --to-destination #{nic[:ip]}"
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user