Class: VagrantPlugins::VCloud::Action::BuildVApp
- Inherits:
-
Object
- Object
- VagrantPlugins::VCloud::Action::BuildVApp
- Defined in:
- lib/vagrant-vcloud/action/build_vapp.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ BuildVApp
constructor
A new instance of BuildVApp.
Constructor Details
#initialize(app, env) ⇒ BuildVApp
Returns a new instance of BuildVApp.
9 10 11 12 |
# File 'lib/vagrant-vcloud/action/build_vapp.rb', line 9 def initialize(app, env) @app = app @logger = Log4r::Logger.new("vagrant_vcloud::action::build_vapp") end |
Instance Method Details
#call(env) ⇒ Object
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 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 133 134 135 136 137 138 139 140 141 142 143 144 145 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/vagrant-vcloud/action/build_vapp.rb', line 14 def call(env) # FIXME: we need to find a way to clean things up when a SIGINT get # called... see env[:interrupted] in the vagrant code cfg = env[:machine].provider_config cnx = cfg.vcloud_cnx.driver vmName = env[:machine].name if cfg.ip_dns.nil? dnsAddress1 = "8.8.8.8" dnsAddress2 = "8.8.4.4" else dnsAddress1 = cfg.ip_dns.shift dnsAddress2 = cfg.ip_dns.shift end if !cfg.ip_subnet.nil? @logger.debug("Input address: #{cfg.ip_subnet}") begin cidr = NetAddr::CIDR.create(cfg.ip_subnet) rescue NetAddr::ValidationError raise Errors::InvalidSubnet, :message => cfg.ip_subnet end if cidr.bits > 30 @logger.debug("Subnet too small!") raise Errors::SubnetTooSmall, :message => cfg.ip_subnet end rangeAddresses = cidr.range(0) @logger.debug("Range: #{rangeAddresses}") rangeAddresses.shift # Delete the "network" address from the range. gatewayIp = rangeAddresses.shift # Retrieve the first usable IP, to be used as a gateway. rangeAddresses.reverse! # Reverse the array in place. rangeAddresses.shift # Delete the "broadcast" address from the range. rangeAddresses.reverse! # Reverse back the array. @logger.debug("Gateway IP: #{gatewayIp.to_s}") @logger.debug("Netmask: #{cidr.wildcard_mask}") @logger.debug("IP Pool: #{rangeAddresses.first}-#{rangeAddresses.last}") @logger.debug("DNS1: #{dnsAddress1} DNS2: #{dnsAddress2}") = { :name => "Vagrant-vApp-Net", :gateway => gatewayIp.to_s, :netmask => cidr.wildcard_mask, :start_address => rangeAddresses.first, :end_address => rangeAddresses.last, :fence_mode => "natRouted", :ip_allocation_mode => "POOL", :parent_network => cfg.vdc_network_id, :enable_firewall => "false", :dns1 => dnsAddress1, :dns2 => dnsAddress2 } else @logger.debug("DNS1: #{dnsAddress1} DNS2: #{dnsAddress2}") # No IP subnet specified, reverting to defaults = { :name => "Vagrant-vApp-Net", :gateway => "10.1.1.1", :netmask => "255.255.255.0", :start_address => "10.1.1.2", :end_address => "10.1.1.254", :fence_mode => "natRouted", :ip_allocation_mode => "POOL", :parent_network => cfg.vdc_network_id, :enable_firewall => "false", :dns1 => dnsAddress1, :dns2 => dnsAddress2 } end if env[:machine].get_vapp_id.nil? env[:ui].info("Building vApp...") compose = cnx.compose_vapp_from_vm( cfg.vdc_id, "Vagrant-#{Etc.getlogin}-#{Socket.gethostname.downcase}-#{SecureRandom.hex(4)}", "vApp created by #{Etc.getlogin} running on #{Socket.gethostname.downcase} using vagrant-vcloud on #{Time.now.strftime("%B %d, %Y")}", { vmName => cfg.catalog_item[:vms_hash][env[:machine].box.name.to_s][:id] }, ) @logger.debug("Launched Compose vApp") # Wait for the task to finish. wait = cnx.wait_task_completion(compose[:task_id]) if !wait[:errormsg].nil? raise Errors::ComposeVAppError, :message => wait[:errormsg] end # Fetch thenewly created vApp ID vAppId = compose[:vapp_id] # putting the vApp Id in a globally reachable var and file. env[:machine].vappid = vAppId # Fetching new vApp object to check stuff. newVApp = cnx.get_vapp(vAppId) # FIXME: Add a lot of error handling for each step here ! if newVApp env[:ui].success("vApp #{newVApp[:name]} successfully created.") # Add the vm id as machine.id newVMProperties = newVApp[:vms_hash].fetch(vmName) env[:machine].id = newVMProperties[:id] ### SET GUEST CONFIG @logger.info("Setting Guest Customization on ID: [#{vmName}] of vApp [#{newVApp[:name]}]") setCustom = cnx.set_vm_guest_customization(newVMProperties[:id], vmName, { :enabled => true, :admin_passwd_enabled => false }) cnx.wait_task_completion(setCustom) else env[:ui].error("vApp #{newVApp[:name]} creation failed!") raise # FIXME: error handling missing. end else env[:ui].info("Adding VM to existing vApp...") recompose = cnx.recompose_vapp_from_vm( env[:machine].get_vapp_id, { vmName => cfg.catalog_item[:vms_hash][env[:machine].box.name.to_s][:id] }, ) @logger.info("Waiting for the add to complete ...") # Wait for the task to finish. wait = cnx.wait_task_completion(recompose[:task_id]) newVApp = cnx.get_vapp(env[:machine].get_vapp_id) # FIXME: Add a lot of error handling for each step here ! if newVApp newVMProperties = newVApp[:vms_hash].fetch(vmName) env[:machine].id = newVMProperties[:id] ### SET GUEST CONFIG @logger.info("Setting Guest Customization on ID: [#{newVMProperties[:id]}] of vApp [#{newVApp[:name]}]") setCustom = cnx.set_vm_guest_customization(newVMProperties[:id], vmName, { :enabled => true, :admin_passwd_enabled => false }) cnx.wait_task_completion(setCustom) else env[:ui].error("VM #{vmName} add to #{newVApp[:name]} failed!") raise end end @app.call env end |