terraspace-bundler 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +72 -24
  4. data/lib/terraspace_bundler.rb +1 -0
  5. data/lib/terraspace_bundler/cli/base.rb +9 -1
  6. data/lib/terraspace_bundler/cli/bundle.rb +26 -12
  7. data/lib/terraspace_bundler/cli/help/bundle/info.md +8 -0
  8. data/lib/terraspace_bundler/cli/help/bundle/install.md +1 -1
  9. data/lib/terraspace_bundler/cli/help/bundle/list.md +9 -0
  10. data/lib/terraspace_bundler/cli/help/bundle/update.md +6 -0
  11. data/lib/terraspace_bundler/cli/{clean.rb → purge_cache.rb} +3 -3
  12. data/lib/terraspace_bundler/cli/{install.rb → runner.rb} +1 -1
  13. data/lib/terraspace_bundler/config.rb +15 -2
  14. data/lib/terraspace_bundler/core.rb +9 -4
  15. data/lib/terraspace_bundler/dsl.rb +6 -3
  16. data/lib/terraspace_bundler/dsl/concern.rb +7 -0
  17. data/lib/terraspace_bundler/dsl/syntax.rb +8 -0
  18. data/lib/terraspace_bundler/exporter.rb +47 -0
  19. data/lib/terraspace_bundler/info.rb +25 -0
  20. data/lib/terraspace_bundler/installer.rb +15 -33
  21. data/lib/terraspace_bundler/list.rb +22 -0
  22. data/lib/terraspace_bundler/lockfile.rb +70 -0
  23. data/lib/terraspace_bundler/lockfile/version_comparer.rb +43 -0
  24. data/lib/terraspace_bundler/lockfile/yamler.rb +37 -0
  25. data/lib/terraspace_bundler/logger.rb +0 -20
  26. data/lib/terraspace_bundler/logger/formatter.rb +7 -0
  27. data/lib/terraspace_bundler/mod.rb +31 -63
  28. data/lib/terraspace_bundler/mod/downloader.rb +55 -0
  29. data/lib/terraspace_bundler/mod/{tmp_paths.rb → path_concern.rb} +1 -1
  30. data/lib/terraspace_bundler/mod/props_builder.rb +57 -0
  31. data/lib/terraspace_bundler/mod/props_extension.rb +15 -0
  32. data/lib/terraspace_bundler/mod/registry.rb +8 -6
  33. data/lib/terraspace_bundler/syncer.rb +62 -4
  34. data/lib/terraspace_bundler/terrafile.rb +39 -0
  35. data/lib/terraspace_bundler/util/git.rb +31 -0
  36. data/lib/terraspace_bundler/{logging.rb → util/logging.rb} +1 -1
  37. data/lib/terraspace_bundler/version.rb +1 -1
  38. data/spec/fixtures/Terrafile +2 -2
  39. data/spec/terraform_bundler/runner_spec.rb +17 -0
  40. metadata +24 -17
  41. data/lib/terraspace_bundler/cli/update.rb +0 -7
  42. data/lib/terraspace_bundler/helper/git.rb +0 -21
  43. data/lib/terraspace_bundler/mod/export.rb +0 -23
  44. data/lib/terraspace_bundler/mod/locked.rb +0 -48
  45. data/lib/terraspace_bundler/mod/sync.rb +0 -53
  46. data/lib/terraspace_bundler/setup.rb +0 -20
  47. data/lib/terraspace_bundler/updater.rb +0 -49
  48. data/lib/terraspace_bundler/updater/lockfile.rb +0 -48
  49. data/lib/terraspace_bundler/util/registry.rb +0 -24
  50. data/spec/terraform_bundler/installer_spec.rb +0 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 631c34ce3db2daed84f18ba29202a80f774f4b109667f7b45782e8650e78a546
4
- data.tar.gz: 86eecdcc7707f0b69dc1119f8998d26db27f379c6e7e8ca40fb57bdcd06f7fc2
3
+ metadata.gz: 500271ff2f8e5799a55febd3ec25261bcf4126bff456b83360ae0e2a96bb6357
4
+ data.tar.gz: ee45f81805b893d5287fdea6777c9b28ac39936273806c3d97275f6c314a8cd1
5
5
  SHA512:
6
- metadata.gz: 66db99a20990496644bac73075aa9e5cb8d5e998cfdfce7b15efc65ea497d8d01bfd21502e91ea914965b7400f8e278d4ce41994a5e7c0fcbba401841f9be083
7
- data.tar.gz: 8f6d8295f23d9521440584ee6ed84b1d7568900211a89b4bcfd67e5b5a5238c2b629f9d161dd540770c705b8eebc05f9a7ecbe70618399e4c070abcaff6a7e04
6
+ metadata.gz: 94af4d341ed3e43445bf2eb0eae1e3383fe064a6df708c449658b8eb5a958163a225be2ddae121356573ae7e099e87669c5cb9c54808423b9db685faf27d7837
7
+ data.tar.gz: d68d1825f89b91ac4d9ad7525a4b4b95cc709ce1e2f3c10d83aa6ad5c6b27a89b3c6ae1ad9bc1ee3179b08345ac73fc551d1467769f702cc43d69b6f3b38b577
@@ -3,5 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.2.0]
7
+ - #1 Rework implementation. Better Terrafile syncing
8
+ - clearer implementation: Terrafile and Lockfile classes build standard mods object.
9
+ - add commands: info, list
10
+ - sync of Terrafile with vendor/modules
11
+ - rename command: terraspace bundle purge_cache
12
+ - rename option to relative_root for modules within a subfolder within a repo
13
+ - prune exported modules by default
14
+ - export_prune option
15
+ - Allow configuring the logger formatter
16
+ - validate org is set if used
17
+ - mod export_to option
18
+
6
19
  ## [0.1.0]
7
20
  - Initial release.
data/README.md CHANGED
@@ -1,12 +1,6 @@
1
1
  # terraspace-bundler
2
2
 
3
- Bundles terraform modules based on a `Terrafile` to the `vendor/modules` folder.
4
-
5
- ## Installation
6
-
7
- To install:
8
-
9
- gem install terraspace-bundler
3
+ Bundles terraform modules based on a `Terrafile` to the `vendor/modules` folder. Used by the [Terraspace Terraform Framework](https://terraspace.cloud/).
10
4
 
11
5
  ## Usage
12
6
 
@@ -26,56 +20,110 @@ mod "rds", source: "boltopspro/terraform-aws-rds", version: "v0.1.0"
26
20
  mod "sg", source: "terraform-aws-modules/security-group/aws", version: "3.10.0"
27
21
  ```
28
22
 
29
- Running `terraspace bundle` creates the `Terrafile.lock` file, which locks the downloaded versions.
23
+ ## Install
24
+
25
+ Running `terraspace bundle` creates the `Terrafile.lock` file, which locks the versions.
30
26
 
31
27
  terraspace bundle
32
28
 
33
29
  ## Updating
34
30
 
35
- To update all the locked versions in `Terrafile.lock` run `terraspace bundle update`. You can also simply delete the `Terrafile.lock` file.
31
+ To update all the locked versions in `Terrafile.lock` run `terraspace bundle update`. You can also delete the `Terrafile.lock` file first.
36
32
 
37
33
  terraspace bundle update
38
34
 
39
- You can selectively update only one module:
35
+ You can selectively update multiple modules:
40
36
 
41
- terraspace bundle update MOD
37
+ terraspace bundle update MODS
38
+ terraspace bundle update mod1 mod2
42
39
 
43
- ## Removing vendor/modules
40
+ ## List & Info
44
41
 
45
- The `terraspace bundle` commands will only update the modules it knows about in `Terrafile`. To clear out old modules, delete them from `vendor/modules`. You can clean them all out like so:
42
+ terraspace bundle list
43
+ terraspace bundle info mod1
46
44
 
47
- rm -rf vendor/modules
48
- terraspace bundle update
45
+ ## DSL Methods
46
+
47
+ Some of these are methods that apply globally at the Terrafile level. Some of these are options that apply the at the mod method level.
49
48
 
50
- ## base url
49
+ ### Terrafile level
50
+
51
+ Name | Description | Default
52
+ --- | --- | ---
53
+ base_clone_url | Base clone url to use | git@gihtub.com:
54
+ export_path | Where the modules get exported to saved to. | vendor/modules
55
+ export_purge | Where or not to clean up all existing exported modules folder first. | true
56
+
57
+ ### Mod Method level
58
+
59
+ Name | Description | Default
60
+ --- | --- | ---
61
+ subfolder | The subfolder where the module lives within the repo. | nil
62
+ export_to | Overrides the export_path Terrafile level option. With this one-off option, other modules in this folder will not be purged. | nil
63
+
64
+ Examples of some of the options are below:
65
+
66
+ ### base url
51
67
 
52
68
  The base url used for clone is `git@github.com:`. You can change it with `base_url`, example:
53
69
 
54
70
  ```ruby
55
- org "boltops-tools" # set default org
56
71
  base_url "https://github.com/" # default base url for git clone
57
-
58
- mod "s3", source: "terraform-aws-s3", version: "master"
72
+ # ...
59
73
  ```
60
74
 
61
- ## export_path
75
+ ### export_path
62
76
 
63
77
  The default export path is `vendor/modules`, you can change it:
64
78
 
65
79
  ```ruby
66
- org "boltops-tools" # set default org
67
80
  export_path "app/modules"
81
+ # ...
82
+ ```
83
+
84
+ ## prune
68
85
 
69
- mod "s3", source: "terraform-aws-s3", version: "master"
86
+ The `terraspace bundle` commands removes all files in the export_path, `vendor/modules`, by default. You can disable this behavior with:
87
+
88
+ ```ruby
89
+ export_purge false
90
+ # ...
70
91
  ```
71
92
 
93
+ ### subfolder
94
+
95
+ The default export path is `vendor/modules`, you can change it:
96
+
97
+ ```ruby
98
+ # ...
99
+ mod "s3", source: "terraform-aws-s3", subfolder: "path/to/module/s3"
100
+ ```
101
+
102
+ ## Config Options
103
+
104
+ You can also configure some behavior with `TB.config`. IE: `TB.config.terrafile = "/path/to/Terrafile"`
105
+
106
+ Name | Description | Default
107
+ --- | --- | ---
108
+ base_clone_url | Base git url to use for cloning | git@github.com:
109
+ export_path | Where to export the modules to. Can also be set with the env var TB_EXPORT_PATH | vendor/modules
110
+ export_purge | Whether or not to prune all the modules | true
111
+ logger | Logger instance
112
+ terrafile | The Terrafile path. Can also be set with the env TB_TERRAFILE || "Terrafile"
113
+
114
+ ## Installation
115
+
116
+ To install:
117
+
118
+ gem install terraspace-bundler
119
+
72
120
  ## Notes
73
121
 
74
- * Simple implementation for [Terraspace](https://terraspace.cloud/) use.
122
+ * This is a simple implementation for [Terraspace](https://terraspace.cloud/) use.
75
123
  * Handles updating the `Terrafile.lock` based on the `Terrafile`
76
124
  * Others running the `terraspace bundle install` will install the exact same module versions based the `Terrafile.lock`.
77
125
  * To update `Terraform.lock` run `terraspace bundle update`.
78
- * The repos are downloaded to `/tmp/terraspace-bundler` area as a cache. Delete the cache by running `terraspace bundle clean`.
126
+ * The repos are downloaded to `/tmp/terraspace-bundler` area as a cache. Delete the cache by running `terraspace bundle purge_cache`.
79
127
 
80
128
  ## Contributing
81
129
 
@@ -17,6 +17,7 @@ DslEvaluator.backtrace_reject = "lib/terraspace_bundler"
17
17
 
18
18
  module TerraspaceBundler
19
19
  class Error < StandardError; end
20
+ class GitError < Error; end
20
21
  extend Core
21
22
  end
22
23
 
@@ -1,9 +1,17 @@
1
1
  class TerraspaceBundler::CLI
2
2
  class Base
3
- include TB::Logging
3
+ include TB::Util::Logging
4
4
 
5
5
  def initialize(options={})
6
6
  @options = options
7
+ set_config!
8
+ end
9
+
10
+ def set_config!
11
+ return unless @options[:terrafile]
12
+ TB.config.terrafile = @options[:terrafile]
13
+ TB.config.lockfile = "#{@options[:terrafile]}.lock"
14
+ TB.config
7
15
  end
8
16
  end
9
17
  end
@@ -1,27 +1,41 @@
1
1
  class TerraspaceBundler::CLI
2
2
  class Bundle < TerraspaceBundler::Command
3
3
  terrafile_option = Proc.new {
4
- option :terrafile, default: "Terrafile", desc: "Terrafile to use"
4
+ option :terrafile, default: ENV['TB_TERRAFILE'] || "Terrafile", desc: "Terrafile to use"
5
5
  }
6
6
 
7
- desc "install", "install"
8
- long_desc Help.text("bundle/install")
7
+ desc "list", "List bundled modules included by Terrafile."
8
+ long_desc Help.text("bundle/list")
9
9
  terrafile_option.call
10
- def install
11
- Install.new(options).run
10
+ def list
11
+ TB::List.new(options).run
12
+ end
13
+
14
+ desc "info MOD", "Provide info about a bundled module."
15
+ long_desc Help.text("bundle/info")
16
+ terrafile_option.call
17
+ def info(mod)
18
+ TB::Info.new(options.merge(mod: mod)).run
12
19
  end
13
20
 
14
- desc "update", "update"
21
+ desc "install", "Install modules from the Terrafile."
15
22
  long_desc Help.text("bundle/install")
16
23
  terrafile_option.call
17
- def update(mod=nil)
18
- Update.new(options.merge(mod: mod)).run
24
+ def install
25
+ Runner.new(options).run
26
+ end
27
+
28
+ desc "purge_cache", "Purge cache."
29
+ long_desc Help.text("bundle/purge_cache")
30
+ def purge_cache
31
+ PurgeCache.new(options).run
19
32
  end
20
33
 
21
- desc "clean", "clean"
22
- long_desc Help.text("bundle/clean")
23
- def clean
24
- Clean.new(options).run
34
+ desc "update [MOD]", "Update bundled modules."
35
+ long_desc Help.text("bundle/update")
36
+ terrafile_option.call
37
+ def update(*mods)
38
+ Runner.new(options.merge(mods: mods, mode: "update")).run
25
39
  end
26
40
  end
27
41
  end
@@ -0,0 +1,8 @@
1
+ ## Examples
2
+
3
+ $ terraspace bundle info random-pet
4
+ random-pet:
5
+ sha: bad676a426f5d3ddb48e674bebd6fd52f063a8b4
6
+ source: org/terraform-random-pet
7
+ type: git
8
+ url: git@github.com:org/terraform-random-pet
@@ -1,3 +1,3 @@
1
1
  ## Examples
2
2
 
3
- terraspace-bundler bundle install
3
+ terraspace bundle install
@@ -0,0 +1,9 @@
1
+ $ terraspace bundle list
2
+ Modules included by Terrafile.lock
3
+
4
+ instance
5
+ random-pet
6
+ vpc
7
+
8
+ Use `terraspace bundle info` to print more detailed information about a module
9
+ $
@@ -0,0 +1,6 @@
1
+ ## Examples
2
+
3
+ $ terraspace bundle update gke
4
+ Bundling with Terrafile...
5
+ Modules saved to vendor/modules
6
+ $
@@ -1,7 +1,7 @@
1
1
  class TerraspaceBundler::CLI
2
- class Clean < Base
3
- include TB::Mod::TmpPaths
4
- include TB::Logging
2
+ class PurgeCache < Base
3
+ include TB::Mod::PathConcern
4
+ include TB::Util::Logging
5
5
 
6
6
  def run
7
7
  FileUtils.rm_rf(tmp_root)
@@ -1,5 +1,5 @@
1
1
  class TerraspaceBundler::CLI
2
- class Install < Base
2
+ class Runner < Base
3
3
  def run
4
4
  TB::Installer.new(@options).run
5
5
  end
@@ -5,11 +5,24 @@ module TerraspaceBundler
5
5
 
6
6
  def config
7
7
  config = ActiveSupport::OrderedOptions.new
8
- config.export_path = "vendor/modules"
9
- config.terrafile = "Terrafile"
8
+ config.base_clone_url = "git@github.com:"
9
+ config.export_path = ENV['TB_EXPORT_PATH'] || "vendor/modules"
10
+ config.export_purge = ENV['TB_EXPORT_PRUNE'] == '0' ? false : true
10
11
  config.lockfile = "#{config.terrafile}.lock"
12
+ config.logger = new_logger
13
+ config.terrafile = ENV['TB_TERRAFILE'] || "Terrafile"
11
14
  config
12
15
  end
13
16
  memoize :config
17
+
18
+ # Note: When using terraspace, Terraspace uses its own logger
19
+ # So these settings dont affect: terraspace bundle
20
+ # Instead, set the log level in the terraspace project: config/app.rb
21
+ def new_logger
22
+ logger = Logger.new(ENV['TB_LOG_PATH'] || $stderr)
23
+ logger.level = ENV['TB_LOG_LEVEL'] || :info
24
+ logger.formatter = Logger::Formatter.new
25
+ logger
26
+ end
14
27
  end
15
28
  end
@@ -4,10 +4,7 @@ module TerraspaceBundler
4
4
 
5
5
  @@logger = nil
6
6
  def logger
7
- return @@logger if @@logger
8
- @@logger = Logger.new($stdout)
9
- @@logger.level = ENV['TB_LOG_LEVEL'] || 'info'
10
- @@logger
7
+ config.logger
11
8
  end
12
9
 
13
10
  def logger=(v)
@@ -17,5 +14,13 @@ module TerraspaceBundler
17
14
  def config
18
15
  Config.instance.config
19
16
  end
17
+
18
+ # DSL is evaluated once lazily when it get used
19
+ def dsl
20
+ dsl = Dsl.new
21
+ dsl.run
22
+ dsl
23
+ end
24
+ memoize :dsl
20
25
  end
21
26
  end
@@ -3,16 +3,19 @@ module TerraspaceBundler
3
3
  include DslEvaluator
4
4
  include Syntax
5
5
 
6
- class_attribute :meta
7
- self.meta = {global: {}, mods: []}
6
+ class_attribute :meta, default: {global: {}, mods: []}
8
7
 
9
8
  def run
10
9
  evaluate_file(TB.config.terrafile)
11
- self.class.meta
10
+ self
12
11
  end
13
12
 
14
13
  def meta
15
14
  self.class.meta
16
15
  end
16
+
17
+ def global
18
+ meta[:global]
19
+ end
17
20
  end
18
21
  end
@@ -0,0 +1,7 @@
1
+ class TerraspaceBundler::Dsl
2
+ module Concern
3
+ def dsl
4
+ TB.dsl
5
+ end
6
+ end
7
+ end
@@ -5,10 +5,18 @@ class TerraspaceBundler::Dsl
5
5
  end
6
6
  alias_method :user, :org
7
7
 
8
+ def base_clone_url(value)
9
+ TB.config.base_clone_url = value
10
+ end
11
+
8
12
  def export_path(path)
9
13
  global[:export_path] = path
10
14
  end
11
15
 
16
+ def export_purge(value)
17
+ TB.config.export_purge = value
18
+ end
19
+
12
20
  def mod(*args, **options)
13
21
  meta[:mods] << {args: args, options: options}
14
22
  end
@@ -0,0 +1,47 @@
1
+ module TerraspaceBundler
2
+ class Exporter
3
+ include TB::Mod::PathConcern
4
+ include TB::Util::Logging
5
+
6
+ def initialize(options={})
7
+ @options = options
8
+ end
9
+
10
+ def run
11
+ purge
12
+ lockfile.mods.each do |mod|
13
+ export(mod)
14
+ end
15
+ end
16
+
17
+ def export(mod)
18
+ downloader = Mod::Downloader.new(mod)
19
+ downloader.switch_version(mod.sha)
20
+
21
+ stage_path = stage_path(mod.full_repo)
22
+ stage_path = "#{stage_path}/#{mod.subfolder}" if mod.subfolder
23
+ mod_path = mod_path(mod)
24
+ FileUtils.rm_rf(mod_path)
25
+ FileUtils.mkdir_p(File.dirname(mod_path))
26
+ FileUtils.cp_r(stage_path, mod_path)
27
+ FileUtils.rm_rf("#{mod_path}/.git")
28
+ logger.debug "Exported: #{mod_path}"
29
+ end
30
+
31
+ def mod_path(mod)
32
+ name = mod.name
33
+ export_to = mod.export_to || TB.config.export_path
34
+ "#{export_to}/#{name}"
35
+ end
36
+
37
+ private
38
+ def purge
39
+ return unless TB.config.export_purge
40
+ FileUtils.rm_rf(TB.config.export_path)
41
+ end
42
+
43
+ def lockfile
44
+ Lockfile.instance
45
+ end
46
+ end
47
+ end