Project

General

Profile

Bug #14198 ยป test.rb

betabandido (Victor Jimenez), 12/18/2017 01:29 PM

 
1
require 'open3'
2
require 'net/http'
3
require 'zip'
4

    
5
Zip.on_exists_proc = true
6

    
7
ROOT_PATH = File.dirname(File.expand_path(__FILE__)).freeze
8
ENV['PATH'] = "#{ROOT_PATH};#{ENV['PATH']}"
9

    
10
def download_terraform
11
  uri = URI('https://releases.hashicorp.com/terraform/0.11.1/terraform_0.11.1_windows_amd64.zip')
12

    
13
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
14
    request = Net::HTTP::Get.new(uri)
15

    
16
    http.request(request) do |response|
17
      open('terraform.zip', 'wb') do |io|
18
        response.read_body do |chunk|
19
          io.write(chunk)
20
        end
21
      end
22
    end
23
  end
24

    
25
  Zip::File.open('terraform.zip') do |zip_file|
26
    zip_file.each do |entry|
27
      puts "Extracting #{entry.name}"
28
      entry.extract(entry.name)
29
    end
30
  end
31
end
32

    
33
download_terraform
34

    
35
def exec(cmd)
36
  Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
37
    stdout_thread = Thread.new do
38
      IO.copy_stream(stdout, STDOUT)
39
    end
40

    
41
    stderr_thread = Thread.new do
42
      IO.copy_stream(stderr, STDERR)
43
    end
44

    
45
    stdin_thread = Thread.new do
46
      IO.copy_stream(STDIN, stdin)
47
    end
48

    
49
    puts "Return code: #{thread.value}"
50

    
51
    stdin_thread.join
52
    stdout_thread.join
53
    stderr_thread.join
54
  end
55
end
56

    
57
exec('terraform destroy')