wdi_tf 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/exe/wdi-tf +5 -0
- data/lib/wdi_tf/cli.rb +75 -0
- data/lib/wdi_tf/funcs.rb +30 -0
- data/lib/wdi_tf/main.rb +52 -0
- data/lib/wdi_tf/project.rb +62 -0
- data/lib/wdi_tf/version.rb +4 -0
- data/lib/wdi_tf.rb +1 -0
- data/wdi_tf.gemspec +29 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb3e856034e61e9147c43c6bd641ca9a13829ac7
|
4
|
+
data.tar.gz: 9f3aa90a750926a3b11da2419ad4d9ace71307ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 80cce084c46ef46ed3ba0a59a6cd8e46e2f79667048ac1ab6c06e21f802cdc81c9ee0b978af2e4ecd571af9c3afb16b5d0bb6d2fbb6b2ab7120f7949ddc365ca
|
7
|
+
data.tar.gz: 340ed07f572b731de753d034f15ee8a949ca16aae6961d1b46ae1479527889d000d36e7e7894c929e0fc98c464cdb411d85613093d6adaaa235cee548ea3dd91
|
data/Gemfile
ADDED
data/exe/wdi-tf
ADDED
data/lib/wdi_tf/cli.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
require 'wdi_tf/main'
|
3
|
+
require 'wdi_tf/project'
|
4
|
+
require 'wdi_tf/funcs'
|
5
|
+
|
6
|
+
include WdiTf::Funcs
|
7
|
+
|
8
|
+
module WdiTf
|
9
|
+
module Cli
|
10
|
+
module_function
|
11
|
+
|
12
|
+
@@project_file = 'Terraform.tfproj'
|
13
|
+
@@variables_file = 'Terraform.tfvars'
|
14
|
+
|
15
|
+
def load_opts(args: ARGV)
|
16
|
+
Trollop.options(args) do
|
17
|
+
banner <<-EOS.gsub(/^ {20}/, '')
|
18
|
+
tf: Wrapper for Terraform
|
19
|
+
|
20
|
+
Usage:
|
21
|
+
tf [options] COMMAND
|
22
|
+
EOS
|
23
|
+
|
24
|
+
opt :debug, 'Puts the wrapper into debug mode', type: TrueClass, default: nil
|
25
|
+
opt :verbose, 'Display more information to STDOUT', type: TrueClass, default: nil
|
26
|
+
|
27
|
+
stop_on_unknown
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_cli_command(args: ARGV)
|
32
|
+
verbose("Get the Terraform command to run from the command line ARGVs.")
|
33
|
+
|
34
|
+
cmd = ARGV.shift
|
35
|
+
cmd ||= "plan"
|
36
|
+
|
37
|
+
return cmd
|
38
|
+
end
|
39
|
+
|
40
|
+
def start
|
41
|
+
opts = load_opts
|
42
|
+
WdiTf::Funcs.set_verbose = opts[:verbose]
|
43
|
+
|
44
|
+
cmd = get_cli_command
|
45
|
+
|
46
|
+
@main = WdiTf::Main.new()
|
47
|
+
@main.clean_workspace()
|
48
|
+
|
49
|
+
if not cmd == "clean"
|
50
|
+
info("Deploying the Terraform project.")
|
51
|
+
|
52
|
+
if not File.exists?(@@project_file)
|
53
|
+
error(msg: "Unable to locate your Terraform project file.", code: 1)
|
54
|
+
end
|
55
|
+
|
56
|
+
@project = WdiTf::Project.new(file: @@project_file)
|
57
|
+
|
58
|
+
source = @project.load_source_files()
|
59
|
+
variables = @project.load_vars_info()
|
60
|
+
|
61
|
+
@main.generate_vars_file(file: @@variables_file, vars: variables)
|
62
|
+
@main.build_workspace(files: source)
|
63
|
+
@main.run_terraform(command: cmd, variables: @@variables_file)
|
64
|
+
|
65
|
+
if opts[:debug]
|
66
|
+
verbose("Leaving the workspace as is; in debug mode.")
|
67
|
+
else
|
68
|
+
@main.clean_workspace()
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
info("Done!")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/lib/wdi_tf/funcs.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
module WdiTf
|
3
|
+
module Funcs
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def set_verbose=(v)
|
7
|
+
@verbose = v
|
8
|
+
end
|
9
|
+
|
10
|
+
def verbose?
|
11
|
+
@verbose
|
12
|
+
end
|
13
|
+
|
14
|
+
def verbose(msg)
|
15
|
+
if WdiTf::Funcs.verbose?
|
16
|
+
info(msg)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def error(msg:, code: 1)
|
21
|
+
puts "\033[31mERROR: #{ msg }\033[0m"
|
22
|
+
exit(code)
|
23
|
+
end
|
24
|
+
|
25
|
+
def info(msg)
|
26
|
+
color = (/^\s+/.match(msg)) ? 32 : 34
|
27
|
+
puts "\033[1;30m[#{ Time.now.strftime("%Y.%m.%d %H:%M:%S") }] \033[#{ color }m#{ msg }\033[0m"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/wdi_tf/main.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'wdi_tf/main'
|
2
|
+
|
3
|
+
module WdiTf
|
4
|
+
class Main
|
5
|
+
def clean_workspace
|
6
|
+
verbose("Cleaning workspace.")
|
7
|
+
|
8
|
+
Dir["*.tf"].each do |file|
|
9
|
+
if File.symlink?(file)
|
10
|
+
verbose(" removing #{ file }")
|
11
|
+
File.delete(file)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_vars_file(file:, vars:)
|
17
|
+
if vars.length > 0
|
18
|
+
verbose("Generate the #{ file } file from Terraform.proj information.")
|
19
|
+
|
20
|
+
open(file, 'w') do |f|
|
21
|
+
vars.each do |var, val|
|
22
|
+
verbose(" #{ var } = \"#{ val }\"")
|
23
|
+
f.puts "#{ var } = \"#{ val }\""
|
24
|
+
end
|
25
|
+
end
|
26
|
+
elsif File.exists?(file)
|
27
|
+
File.delete(file)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_workspace(files:)
|
32
|
+
verbose("Building workspace.")
|
33
|
+
|
34
|
+
files.each do |d_file, s_file|
|
35
|
+
verbose(" symlinking #{ s_file }")
|
36
|
+
File.symlink(s_file, d_file)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def run_terraform(command:, variables:)
|
41
|
+
info("Running Terraform command.")
|
42
|
+
|
43
|
+
cmd = "terraform #{ command }"
|
44
|
+
|
45
|
+
if File.exists?(variables)
|
46
|
+
cmd = "#{ cmd } -var-file=#{ variables }"
|
47
|
+
end
|
48
|
+
|
49
|
+
info(" #{ cmd }")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module WdiTf
|
4
|
+
class Project
|
5
|
+
def initialize(file:)
|
6
|
+
verbose("Loading the Terraform project file.")
|
7
|
+
@project = YAML.load_file(file)
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_source_files
|
11
|
+
files = {}
|
12
|
+
conflict = false
|
13
|
+
|
14
|
+
if @project.has_key?('source')
|
15
|
+
verbose("Load the source files from the Project file.")
|
16
|
+
|
17
|
+
@project['source'].each do |file|
|
18
|
+
if Dir.exists?(file)
|
19
|
+
Dir["#{ file }/*.tf"].each do |f|
|
20
|
+
@project['source'].push(f)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
base = File.basename(file)
|
24
|
+
|
25
|
+
if files.has_key?(base)
|
26
|
+
verbose(" conflict #{ file } (local file with same name)")
|
27
|
+
conflict = true
|
28
|
+
else
|
29
|
+
if File.extname(file) == ".tf"
|
30
|
+
verbose(" using #{ file }")
|
31
|
+
files[base] = file
|
32
|
+
else
|
33
|
+
verbose(" skipping #{ file } (not a *.tf file)")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if conflict
|
41
|
+
error(msg: "One or more files are conflicting with files already in the workspace with the same name.", code: 1)
|
42
|
+
end
|
43
|
+
|
44
|
+
return files
|
45
|
+
end
|
46
|
+
|
47
|
+
def load_vars_info
|
48
|
+
vars = {}
|
49
|
+
|
50
|
+
if @project.has_key?('variables')
|
51
|
+
verbose("Load the variables from the Project file")
|
52
|
+
|
53
|
+
@project['variables'].each do |var, val|
|
54
|
+
verbose(" #{ var } = \"#{ val }\"")
|
55
|
+
vars[var] = val
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
return vars
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/wdi_tf.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'wdi_tf/main'
|
data/wdi_tf.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wdi_tf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'wdi_tf'
|
8
|
+
spec.version = WdiTf::VERSION
|
9
|
+
spec.authors = ["Anthony Kirby"]
|
10
|
+
spec.email = 'anthony.kirby@winedirect.com'
|
11
|
+
|
12
|
+
spec.description = "Terraform wrapper"
|
13
|
+
spec.summary = spec.description
|
14
|
+
spec.homepage = 'http://rubygems.org/gems/wdi_tf'
|
15
|
+
spec.license = 'Apache-2.0'
|
16
|
+
|
17
|
+
spec.files = `git ls-files *.rb *.gemspec Gemfile exe`.split($INPUT_RECORD_SEPARATOR)
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = %w(lib)
|
22
|
+
|
23
|
+
spec.required_ruby_version = ['>= 2.2.6']
|
24
|
+
|
25
|
+
spec.add_dependency 'trollop', '~> 2.1'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.4'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wdi_tf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anthony Kirby
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: trollop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description: Terraform wrapper
|
56
|
+
email: anthony.kirby@winedirect.com
|
57
|
+
executables:
|
58
|
+
- wdi-tf
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- exe/wdi-tf
|
64
|
+
- lib/wdi_tf.rb
|
65
|
+
- lib/wdi_tf/cli.rb
|
66
|
+
- lib/wdi_tf/funcs.rb
|
67
|
+
- lib/wdi_tf/main.rb
|
68
|
+
- lib/wdi_tf/project.rb
|
69
|
+
- lib/wdi_tf/version.rb
|
70
|
+
- wdi_tf.gemspec
|
71
|
+
homepage: http://rubygems.org/gems/wdi_tf
|
72
|
+
licenses:
|
73
|
+
- Apache-2.0
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 2.2.6
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.6.12
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Terraform wrapper
|
95
|
+
test_files: []
|