tfr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 836aeb50a9241a170b88db2ca99a5f7e6191ab500a8830bcf695ef06c09a3a17
4
+ data.tar.gz: ef41c0d1914efa0e886eefbd8f84c63dc3d1b643fce1e2efee222382cb48e049
5
+ SHA512:
6
+ metadata.gz: 18c5a84da9f339198893589ff3a90f28342be492963fd9fb04fb333ee06d1ebf6b9facc63268fcfd4b5f36dd80f4901bc8b82371e02e67a899b036e6f2087f06
7
+ data.tar.gz: 40b43bfb445aaba1fc23f086a331f3c4c01960b4cfe4e6cc58ace88ca4035a777a5a5650258a2214c986f8e44fbcfe65821e723c200825962429c3b45a5d0c32
data/bin/tfr ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Wrapper for Terraform
4
+ $: << File.expand_path("#{File.dirname(__FILE__)}/../lib")
5
+ require 'tfr'
6
+
7
+ Tfr::CLI.start(ARGV)
data/lib/tfr/apply.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Apply < TerraformWorkspaceGroup
3
+ desc "Apply on a given WORKSPACE"
4
+
5
+ def apply
6
+ execute("apply #{workspace_variables}")
7
+ end
8
+ end
9
+ end
data/lib/tfr/cli.rb ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Wrapper for Terraform
4
+ module Tfr
5
+ TERRAFORM_VERSION = "1.3.7"
6
+
7
+ class CLI < Thor
8
+ def self.exit_on_failure?
9
+ true
10
+ end
11
+
12
+ desc "list", "List Terraform projects"
13
+
14
+ def list(customer: "neutronium")
15
+ search_path = Pathname("#{Dir.home}/workspace/#{customer}")
16
+ results = search_path.glob("**/*.tf").collect { |f| File.dirname(f) }.compact.uniq
17
+
18
+ p results
19
+ end
20
+
21
+ # Workspaced commands
22
+ register(Tfr::Init, "init", "init", "Init terraform")
23
+ register(Tfr::Plan, "plan", "plan", "Plan terraform")
24
+ register(Tfr::Apply, "apply", "apply", "Apply terraform")
25
+ register(Tfr::Destroy, "destroy", "destroy", "Destroy terraform")
26
+ register(Tfr::Refresh, "refresh", "refresh", "Refresh terraform")
27
+
28
+ # Standard commands
29
+ register(Tfr::Validate, "validate", "validate", "Validate terraform")
30
+ register(Tfr::Format, "fmt", "format", "Format terraform")
31
+
32
+ desc "release SUBCOMMAND", "Manage module releases"
33
+ subcommand "release", Tfr::Release::Commands
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Destroy < TerraformWorkspaceGroup
3
+ desc "Destroy on a given WORKSPACE"
4
+
5
+ def plan
6
+ execute("destroy #{workspace_variables}")
7
+ end
8
+ end
9
+ end
data/lib/tfr/format.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Format < TerraformGroup
3
+ desc "Format on a given WORKSPACE"
4
+
5
+ def format
6
+ execute("fmt -recursive .")
7
+ end
8
+ end
9
+ end
data/lib/tfr/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Init < TerraformGroup
3
+ desc "Init on a given WORKSPACE"
4
+
5
+ def init
6
+ execute("init")
7
+ end
8
+ end
9
+ end
data/lib/tfr/plan.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Plan < TerraformWorkspaceGroup
3
+ desc "Plan on a given WORKSPACE"
4
+
5
+ def plan
6
+ execute("plan #{workspace_variables}")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Refresh < TerraformWorkspaceGroup
3
+ desc "Refresh on a given WORKSPACE"
4
+
5
+ def refresh
6
+ execute("refresh")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Tfr
2
+ module Release
3
+ class Commands < Thor
4
+ # Release commands
5
+ register(Tfr::Release::Major, "major", "maj", "Release new major version")
6
+ register(Tfr::Release::Minor, "minor", "min", "Release new minor version")
7
+ register(Tfr::Release::Patch, "patch", "pat", "Release new patch version")
8
+ register(Tfr::Release::Pre, "pre", "pre", "Release new pre-release version")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Tfr
2
+ module Release
3
+ class Major < Release
4
+ desc "Release new major version"
5
+
6
+ def major
7
+ current_tag = get_current_tag
8
+ puts current_tag
9
+ new_version = generate_next_tag(current_tag, :major)
10
+ puts new_version
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Tfr
2
+ module Release
3
+ class Minor < Release
4
+ desc "Release new minor version"
5
+
6
+ def minor
7
+ current_tag = get_current_tag
8
+ puts current_tag
9
+ new_version = generate_next_tag(current_tag, :minor)
10
+ puts new_version
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Tfr
2
+ module Release
3
+ class Patch < Release
4
+ desc "Release new patch version"
5
+
6
+ def patch
7
+ current_tag = get_current_tag
8
+ puts current_tag
9
+ new_version = generate_next_tag(current_tag, :patch)
10
+ puts new_version
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Tfr
2
+ module Release
3
+ class Pre < Release
4
+ desc "Release new pre version"
5
+
6
+ def pre
7
+ current_tag = get_current_tag
8
+ puts current_tag
9
+ new_version = generate_next_tag(current_tag, :pre_release)
10
+ puts new_version
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,91 @@
1
+ module Tfr
2
+ module Release
3
+ class Release < Thor::Group
4
+ no_commands {
5
+ def get_current_tag
6
+ g = Git.open(".", :log => Logger.new(STDOUT))
7
+ g.tags.collect { |tag| Semverse::Version.new(tag.name) }.max
8
+ end
9
+
10
+ def generate_next_tag(tag, field)
11
+ case field
12
+ when :major
13
+ logger.info("Major")
14
+ if tag.pre_release
15
+ logger.info("Pre")
16
+ Semverse::Version.new([
17
+ tag.major,
18
+ tag.minor,
19
+ tag.patch,
20
+ ])
21
+ else
22
+ logger.info("No-Pre")
23
+ Semverse::Version.new([
24
+ tag.major + 1,
25
+ 0,
26
+ 0,
27
+ ])
28
+ end
29
+ when :minor
30
+ logger.info("minor")
31
+ if tag.pre_release
32
+ logger.info("Pre")
33
+ Semverse::Version.new([
34
+ tag.major,
35
+ tag.minor,
36
+ tag.patch,
37
+ ])
38
+ else
39
+ logger.info("No-Pre")
40
+ Semverse::Version.new([
41
+ tag.major,
42
+ tag.minor + 1,
43
+ 0,
44
+ ])
45
+ end
46
+ when :patch
47
+ logger.info("patch")
48
+ if tag.pre_release
49
+ logger.info("Pre")
50
+ Semverse::Version.new([
51
+ tag.major,
52
+ tag.minor,
53
+ tag.patch,
54
+ ])
55
+ else
56
+ logger.info("No-Pre")
57
+ Semverse::Version.new([
58
+ tag.major,
59
+ tag.minor,
60
+ tag.patch + 1,
61
+ ])
62
+ end
63
+ when :pre_release
64
+ logger.info("pre_release")
65
+ if tag.pre_release
66
+ logger.info("Pre #{tag.pre_release}")
67
+ Semverse::Version.new([
68
+ tag.major,
69
+ tag.minor,
70
+ tag.patch,
71
+ "rc.#{tag.pre_release + 1}",
72
+ ])
73
+ else
74
+ logger.info("No-Pre")
75
+ Semverse::Version.new([
76
+ tag.major,
77
+ tag.minor,
78
+ tag.patch,
79
+ "rc.0",
80
+ ])
81
+ end
82
+ end
83
+ end
84
+
85
+ def logger
86
+ logger ||= Logger.new(STDOUT)
87
+ end
88
+ }
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,27 @@
1
+ module Tfr
2
+ class TerraformGroup < Thor::Group
3
+ no_commands {
4
+ def execute(command)
5
+ current_folder = Pathname.getwd
6
+ terraform_command = "docker run --rm -v #{Dir.home}/.aws:/root/.aws -v #{current_folder}:/data -w /data hashicorp/terraform:#{TERRAFORM_VERSION} #{command}"
7
+ logger.debug(terraform_command)
8
+ stdout, stderr, status = Open3.capture3(terraform_command)
9
+ result = status.exitstatus
10
+ if result != 0
11
+ logger.info stdout if !stdout.empty?
12
+ logger.fatal stderr
13
+ exit
14
+ else
15
+ logger.info stdout if !stdout.empty?
16
+ end
17
+ end
18
+
19
+ def execute_terraform
20
+ end
21
+
22
+ def logger
23
+ logger ||= Logger.new(STDOUT)
24
+ end
25
+ }
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module Tfr
2
+ class TerraformWorkspaceGroup < TerraformGroup
3
+ argument :workspace, :type => :string, :desc => "Terraform Workspace name"
4
+
5
+ def select_workspace
6
+ execute("workspace select #{workspace}")
7
+ end
8
+
9
+ no_commands {
10
+ def workspace_variables
11
+ "-var-file tfvars/#{workspace}.tfvars"
12
+ end
13
+ }
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Tfr
2
+ class Validate < TerraformGroup
3
+ desc "Validate on a given WORKSPACE"
4
+
5
+ def validate
6
+ execute("validate")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Tfr
2
+ # :section:
3
+ VERSION = "0.0.1"
4
+ end
data/lib/tfr.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "thor"
2
+ require "fileutils"
3
+ require "logger"
4
+ require "open3"
5
+ require "pathname"
6
+ require "thor"
7
+ require "thor/group"
8
+ require "semver"
9
+ require "git"
10
+
11
+ # Root classes
12
+ require "tfr/terraform_group.rb"
13
+ require "tfr/terraform_workspace_group.rb"
14
+ require "tfr/release/release.rb"
15
+
16
+ # Terraform commands
17
+ require "tfr/apply.rb"
18
+ require "tfr/destroy.rb"
19
+ require "tfr/format.rb"
20
+ require "tfr/init.rb"
21
+ require "tfr/plan.rb"
22
+ require "tfr/refresh.rb"
23
+ require "tfr/validate.rb"
24
+
25
+ # Release commands
26
+ require "tfr/release/major.rb"
27
+ require "tfr/release/minor.rb"
28
+ require "tfr/release/patch.rb"
29
+ require "tfr/release/pre.rb"
30
+
31
+ require "tfr/release/commands.rb"
32
+
33
+ # CLI
34
+ require "tfr/cli.rb"
35
+ require "tfr/version.rb"
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tfr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Norguet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: git
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: semver
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: open3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.2
83
+ description:
84
+ email:
85
+ executables:
86
+ - tfr
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - bin/tfr
91
+ - lib/tfr.rb
92
+ - lib/tfr/apply.rb
93
+ - lib/tfr/cli.rb
94
+ - lib/tfr/destroy.rb
95
+ - lib/tfr/format.rb
96
+ - lib/tfr/init.rb
97
+ - lib/tfr/plan.rb
98
+ - lib/tfr/refresh.rb
99
+ - lib/tfr/release/commands.rb
100
+ - lib/tfr/release/major.rb
101
+ - lib/tfr/release/minor.rb
102
+ - lib/tfr/release/patch.rb
103
+ - lib/tfr/release/pre.rb
104
+ - lib/tfr/release/release.rb
105
+ - lib/tfr/terraform_group.rb
106
+ - lib/tfr/terraform_workspace_group.rb
107
+ - lib/tfr/validate.rb
108
+ - lib/tfr/version.rb
109
+ homepage:
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubygems_version: 3.3.7
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Wrapper for Terraform
132
+ test_files: []