Class: CreateVMCommand
- Inherits:
-
BaseCommand
- Object
- Clamp::Command
- BaseCommand
- CreateVMCommand
- Defined in:
- bin/esx
Instance Method Summary collapse
- #download!(source_url, destination_file) ⇒ Object
- #execute ⇒ Object
- #report_progress(progress, total, show_parts = true) ⇒ Object
Instance Method Details
#download!(source_url, destination_file) ⇒ Object
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 |
# File 'bin/esx', line 151 def download!(source_url, destination_file) dst = File.open(destination_file, 'w') proxy_uri = URI.parse(ENV["http_proxy"] || "") uri = URI.parse(source_url) http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end res = http.start do |h| h.request_get(uri.request_uri) do |response| total = response.content_length progress = 0 segment_count = 0 response.read_body do |segment| # Report the progress out progress += segment.length segment_count += 1 # Progress reporting is limited to every 25 segments just so # we're not constantly updating if segment_count % 25 == 0 report_progress(progress, total) segment_count = 0 end # Store the segment dst.write(segment) end end end raise Exception.new("HTTP Error #{res}") if res.class != Net::HTTPOK ensure dst.close end |
#execute ⇒ Object
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 |
# File 'bin/esx', line 85 def execute begin host = ESX::Host.connect address, user, password rescue Exception => e $stderr.puts "Can't connect to the host #{address}." if debug? $stderr.puts e. end exit 1 end downloaded_file = nil df = disk_file.dup if df.strip.chomp =~ /^http/ begin downloaded_file = disk_file.dup tmpfile = "#{tmpdir}/#{Time.now.to_i}.esx" puts "Downloading file... (#{tmpfile})" download! downloaded_file, tmpfile puts df = tmpfile rescue Exception => e FileUtils.rm_f(tmpfile) $stderr.puts "Error downloading file from #{downloaded_file}." $stderr.puts e. if debug? exit 1 end end raise Exception.new("Invalid disk file") if not File.exist?(df) if not name $stderr.puts "Invalid VM name." $stderr.puts "Use --name option to specify the VM name" exit 1 end host.remote_command "mkdir /vmfs/volumes/#{datastore}/#{name}" begin host.import_disk df, "/vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk" rescue Exception => e $stderr.puts "Error uploading file to /vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk" $stderr.puts e. if debug? exit 1 end if not downloaded_file.nil? puts "Deleting tmp file #{df}" if debug? FileUtils.rm_f(df) end vm = host.create_vm :vm_name => name, :disk_file => "#{name}/#{name}.vmdk", :datastore => datastore, :disk_type => :flat, :memory => memory, :guest_id => guest_id if poweron? vm.power_on end end |
#report_progress(progress, total, show_parts = true) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'bin/esx', line 141 def report_progress(progress, total, show_parts=true) line_reset = "\r\e[0K" percent = (progress.to_f / total.to_f) * 100 line = "Progress: #{percent.to_i}%" line << " (#{progress} / #{total})" if show_parts line = "#{line_reset}#{line}" $stdout.sync = true $stdout.print line end |