Module: WdiTf::Cli

Defined in:
lib/wdi_tf/cli.rb

Constant Summary collapse

@@project_file =
'Terraform.tfproj'
@@variables_file =
'Terraform.tfvars'
@@allowed_cmds =
['clean', 'plan', 'apply', 'destroy']

Class Method Summary collapse

Class Method Details

.get_cli_command(args: ARGV) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wdi_tf/cli.rb', line 40

def get_cli_command(args: ARGV)
    verbose("Get the Terraform command to run from the command line ARGVs.")
    
    cmd = ARGV.shift
    cmd ||= "plan"
    
    if not @@allowed_cmds.include?(cmd)
        error(msg: "Unknown command '#{ cmd }'.  Accepted commands: #{ @@allowed_cmds.join(', ') }")
    end
    
    return cmd
end

.load_opts(args: ARGV) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wdi_tf/cli.rb', line 16

def load_opts(args: ARGV)
    Trollop.options(args) do
        banner "            tf: Wrapper for Terraform\n            \n            Usage:\n              tf [options] COMMAND\n            \n            Commands:\n              clean     - cleans the workspace and returns it back to the original state.\n              plan      - runs 'terraform plan' on the workspace. (default)\n              apply     - runs 'terraform apply' on the workspace.\n              destroy   - runs 'teraform destroy' on the workspace.\n            \n            Options:\n        EOS\n        \n        opt :debug, 'Puts the wrapper into debug mode', type: TrueClass, default: nil\n        opt :verbose, 'Display more information to STDOUT', type: TrueClass, default: nil\n        \n        stop_on_unknown\n    end\nend\n".gsub(/^ {20}/, '')

.startObject



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

def start
    opts = load_opts
    WdiTf::Funcs.set_verbose = opts[:verbose]
    
    cmd = get_cli_command
    
    @main = WdiTf::Main.new()
    @main.clean_workspace()
    
    if not cmd == "clean"
        info("Deploying the Terraform project.")

        if not File.exists?(@@project_file)
            error(msg: "Unable to locate your Terraform project file.", code: 1)
        end
        
        @project = WdiTf::Project.new(file: @@project_file)
        
        source = @project.load_source_files()
        variables = @project.load_vars_info()
        
        @main.generate_vars_file(file: @@variables_file, vars: variables)
        @main.build_workspace(files: source)
        @main.run_terraform(command: cmd, variables: @@variables_file)
        
        if opts[:debug]
            verbose("Leaving the workspace as is; in debug mode.")
        else
            @main.clean_workspace()
        end
    end
    
    info("Done!")
end