Class: WdiTf::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/wdi_tf/main.rb

Instance Method Summary collapse

Instance Method Details

#build_workspace(files:) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/wdi_tf/main.rb', line 32

def build_workspace(files:)
    verbose("Building workspace.")
    
    files.each do |d_file, s_file|
        verbose("  symlinking #{ s_file }")
        File.symlink(s_file, d_file)
    end
end

#clean_workspaceObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/wdi_tf/main.rb', line 6

def clean_workspace
    verbose("Cleaning workspace.")
    
    Dir["*.tf"].each do |file|
        if File.symlink?(file)
            verbose("  removing #{ file }")
            File.delete(file)
        end
    end
end

#generate_vars_file(file:, vars:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wdi_tf/main.rb', line 17

def generate_vars_file(file:, vars:)
    if vars.length > 0
        verbose("Generate the #{ file } file from Terraform.proj information.")
        
        open(file, 'w') do |f|
            vars.each do |var, val|
                verbose("  #{ var } = \"#{ val }\"")
                f.puts "#{ var } = \"#{ val }\""
            end
        end
    elsif File.exists?(file)
        File.delete(file)
    end
end

#run_terraform(command:, variables:) ⇒ Object



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
# File 'lib/wdi_tf/main.rb', line 41

def run_terraform(command:, variables:)
    info("Running Terraform command.")
    
    cmd = "terraform init && terraform #{ command }"
    
    if File.exists?(variables)
        cmd = "#{ cmd } -var-file=#{ variables }"
    end
    
    if command == 'apply'
        cmd = "#{ cmd } -auto-approve"
    end
    
    if command == 'destroy'
        cmd = "#{ cmd } -force"
    end
    
    info("  #{ cmd }")
    Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|
        while line = stdout_err.gets
            puts line
        end
        
        exit_status = wait_thr.value
        
        unless exit_status.success?
            abort "FAILED !!! #{cmd}"
        end
    end
end