Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. # This method will pull the public ip off of the zone and create a VIP from it and forward
  2. # for ports 80 and 22
  3. def virtualize_public_ip!
  4. # TODO: Raising these errors are kind of useless. Raise exception classes so we can act on them
  5. # Paths of action: do we keep going, do we destroy a zone depending on type of exception
  6. raise ZoneError::NoPublicIp, "Cannot virtualize ip on a zone without a public ip.", caller unless public_ip = public_ips.first
  7. raise ZoneError::NoPrivateIp, "Cannot virtualize ip on a zone without a private ip.", caller unless private_ip = private_ips.first
  8. raise ZoneError::NoLoadBalancer, "Cannot find load balancer to use for virtualization.", caller unless load_balancer = LoadBalancer.find_aptana
  9. raise ZoneError::Inactive, "Cannot virtualize ip on an inactive zone.", caller unless active? # could be removed if good reason for it
  10. # raise "Cannot virtualize ip on a zone without a customer." unless customer # a bit arbitrary, but include for now
  11.  
  12. # NOTE: This likely should be put in a transaction and dealt with more cleanly in case of an error
  13. # In order to do so we would need to rollback the objects and the writing to the load balancer
  14. # if that in fact happened.
  15. # NOTE: I would love to figure out a way to elegantly remove the need for this object, but
  16. # currently it is the 'addressable' that is needed to 'utilize' an ip address
  17. vip = (customer ? customer.virtual_ips.create : VirtualIp.create)
  18.  
  19. vip.zones << self
  20. vip.ips << public_ip
  21.  
  22. [22, 80, 3306].each do |port|
  23. lb_virtual_server = vip.load_balanced_virtual_servers.create!(:name => "vs_#{name}_#{port}",
  24. :ip => public_ip,
  25. :port => port,
  26. :load_balancer => load_balancer)
  27.  
  28. lb_virtual_server.add_member_to_pool(private_ip, port)
  29. end
  30.  
  31. # This is the last step for aptana zone creation
  32. update_attribute(:creation_state, "unverified")
  33.  
  34. {:success => true, :virtual_ip => vip}
  35. end
Add Comment
Please, Sign In to add comment