lono 3.5.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +15 -4
  3. data/.rspec +1 -0
  4. data/CHANGELOG.md +15 -1
  5. data/Gemfile +3 -3
  6. data/Guardfile +17 -8
  7. data/{LICENSE → LICENSE.txt} +1 -1
  8. data/README.md +20 -12
  9. data/Rakefile +1 -2
  10. data/{bin → exe}/lono +1 -0
  11. data/lib/lono.rb +12 -9
  12. data/lib/lono/cfn.rb +7 -9
  13. data/lib/lono/cfn/{aws_services.rb → aws_service.rb} +1 -1
  14. data/lib/lono/cfn/base.rb +41 -38
  15. data/lib/lono/cfn/create.rb +6 -2
  16. data/lib/lono/cfn/delete.rb +2 -2
  17. data/lib/lono/cfn/diff.rb +1 -1
  18. data/lib/lono/cfn/preview.rb +26 -15
  19. data/lib/lono/cfn/update.rb +11 -9
  20. data/lib/lono/cfn/util.rb +3 -3
  21. data/lib/lono/clean.rb +1 -1
  22. data/lib/lono/cli.rb +71 -39
  23. data/lib/lono/command.rb +42 -18
  24. data/lib/lono/completer.rb +162 -0
  25. data/lib/lono/completer/script.rb +6 -0
  26. data/lib/lono/completer/script.sh +10 -0
  27. data/lib/lono/completion.rb +15 -0
  28. data/lib/lono/core.rb +23 -9
  29. data/lib/lono/core/config.rb +20 -0
  30. data/lib/lono/default/settings.yml +33 -13
  31. data/lib/lono/help.rb +6 -79
  32. data/lib/lono/help/cfn.md +6 -0
  33. data/lib/lono/help/cfn/create.md +22 -0
  34. data/lib/lono/help/cfn/delete.md +5 -0
  35. data/lib/lono/help/cfn/diff.md +5 -0
  36. data/lib/lono/help/cfn/download.md +5 -0
  37. data/lib/lono/help/cfn/preview.md +11 -0
  38. data/lib/lono/help/cfn/update.md +21 -0
  39. data/lib/lono/help/completion.md +22 -0
  40. data/lib/lono/help/completion_script.md +3 -0
  41. data/lib/lono/help/generate.md +7 -0
  42. data/lib/lono/help/hello.md +5 -0
  43. data/lib/lono/help/import.md +7 -0
  44. data/lib/lono/help/inspect.md +4 -0
  45. data/lib/lono/help/inspect/depends.md +3 -0
  46. data/lib/lono/help/inspect/summary.md +3 -0
  47. data/lib/lono/help/new.md +8 -0
  48. data/lib/lono/help/param.md +3 -0
  49. data/lib/lono/{param/help.rb → help/param/generate.md} +1 -9
  50. data/lib/lono/help/script/build.md +5 -0
  51. data/lib/lono/help/script/upload.md +8 -0
  52. data/lib/lono/help/template.md +4 -0
  53. data/lib/lono/help/template/bashify.md +4 -0
  54. data/lib/lono/help/template/generate.md +7 -0
  55. data/lib/lono/help/user_data.md +3 -0
  56. data/lib/lono/importer.rb +43 -20
  57. data/lib/lono/inspector.rb +2 -19
  58. data/lib/lono/inspector/base.rb +2 -2
  59. data/lib/lono/inspector/{depends.rb → graph.rb} +3 -3
  60. data/lib/lono/inspector/summary.rb +1 -1
  61. data/lib/lono/new.rb +79 -26
  62. data/lib/lono/new/helper.rb +16 -0
  63. data/lib/lono/new/message.rb +35 -0
  64. data/lib/lono/param.rb +1 -2
  65. data/lib/lono/param/generator.rb +34 -86
  66. data/lib/lono/project_checker.rb +35 -40
  67. data/lib/lono/script.rb +19 -0
  68. data/lib/lono/script/base.rb +9 -0
  69. data/lib/lono/script/build.rb +73 -0
  70. data/lib/lono/script/upload.rb +81 -0
  71. data/lib/lono/sequence.rb +33 -0
  72. data/lib/lono/setting.rb +83 -0
  73. data/lib/lono/template.rb +8 -9
  74. data/lib/lono/template/{aws_services.rb → aws_service.rb} +1 -1
  75. data/lib/lono/template/context.rb +73 -0
  76. data/lib/lono/template/dsl.rb +63 -64
  77. data/lib/lono/template/helper.rb +201 -0
  78. data/lib/lono/template/template.rb +29 -221
  79. data/lib/lono/template/upload.rb +41 -33
  80. data/lib/lono/upgrade4.rb +175 -0
  81. data/lib/lono/user_data.rb +31 -0
  82. data/lib/lono/version.rb +1 -1
  83. data/lib/starter_projects/autoscaling/.gitignore +1 -0
  84. data/lib/starter_projects/{json_project → autoscaling}/Gemfile +0 -0
  85. data/lib/starter_projects/{yaml_project → autoscaling}/Guardfile +0 -0
  86. data/lib/starter_projects/autoscaling/README.md +118 -0
  87. data/lib/starter_projects/autoscaling/app/definitions/base.rb +2 -0
  88. data/lib/starter_projects/autoscaling/app/templates/autoscaling.yml +682 -0
  89. data/lib/starter_projects/autoscaling/config/params/base/autoscaling.txt +6 -0
  90. data/lib/starter_projects/autoscaling/config/settings.yml +33 -0
  91. data/lib/starter_projects/ec2/.gitignore +1 -0
  92. data/lib/starter_projects/{yaml_project → ec2}/Gemfile +0 -0
  93. data/lib/starter_projects/{json_project → ec2}/Guardfile +1 -1
  94. data/lib/starter_projects/ec2/README.md +86 -0
  95. data/lib/starter_projects/ec2/app/definitions/base.rb +2 -0
  96. data/lib/starter_projects/ec2/app/definitions/development.rb +1 -0
  97. data/lib/starter_projects/ec2/app/definitions/production.rb +1 -0
  98. data/lib/starter_projects/{yaml_project → ec2/app}/helpers/my_custom_helper.rb +0 -0
  99. data/lib/starter_projects/{json_project/templates/user_data/app.sh → ec2/app/partials/user_data/bootstrap.sh} +1 -2
  100. data/lib/starter_projects/{yaml_project → ec2/app}/templates/example.yml +0 -0
  101. data/lib/starter_projects/{json_project/params/base/api-web.txt → ec2/config/params/base/example.txt} +0 -0
  102. data/lib/starter_projects/ec2/config/params/development/example.txt +3 -0
  103. data/lib/starter_projects/ec2/config/params/production/example.txt +2 -0
  104. data/lib/starter_projects/ec2/config/settings.yml +33 -0
  105. data/lib/starter_projects/ec2/config/variables/base.rb +3 -0
  106. data/lib/starter_projects/ec2/config/variables/development.rb +2 -0
  107. data/lib/starter_projects/ec2/config/variables/production.rb +2 -0
  108. data/lib/starter_projects/ec2/welcome.txt +8 -0
  109. data/lib/starter_projects/skeleton/.gitignore +1 -0
  110. data/lib/starter_projects/skeleton/Gemfile +3 -0
  111. data/lib/starter_projects/skeleton/Guardfile +12 -0
  112. data/lib/starter_projects/skeleton/README.md +53 -0
  113. data/{spec/fixtures/my_project/templates/.gitkeep → lib/starter_projects/skeleton/app/definitions/base.rb} +0 -0
  114. data/lib/starter_projects/skeleton/config/settings.yml +33 -0
  115. data/lib/starter_projects/skeleton/welcome.txt +7 -0
  116. data/lono.gemspec +12 -10
  117. data/spec/fixtures/lono_project/.gitignore +1 -0
  118. data/spec/fixtures/lono_project/Gemfile +3 -0
  119. data/spec/fixtures/lono_project/Guardfile +12 -0
  120. data/spec/fixtures/lono_project/app/definitions/base.rb +10 -0
  121. data/spec/fixtures/lono_project/app/definitions/base/more.rb +7 -0
  122. data/spec/fixtures/lono_project/app/definitions/development.rb +1 -0
  123. data/spec/fixtures/lono_project/app/definitions/production.rb +1 -0
  124. data/spec/fixtures/lono_project/app/helpers/custom_helper.rb +5 -0
  125. data/spec/fixtures/lono_project/app/partials/security_group.yml +10 -0
  126. data/{lib/starter_projects/yaml_project/templates/partial → spec/fixtures/lono_project/app/partials}/user_data/bootstrap.sh +8 -2
  127. data/spec/fixtures/lono_project/app/templates/example.yml +50 -0
  128. data/{lib/starter_projects/yaml_project/params/base/api-web-prod.txt → spec/fixtures/lono_project/config/params/base/example.txt} +1 -0
  129. data/spec/fixtures/lono_project/config/params/development/example.txt +1 -0
  130. data/spec/fixtures/lono_project/config/params/production/example.txt +1 -0
  131. data/spec/fixtures/lono_project/config/settings.yml +31 -0
  132. data/spec/fixtures/lono_project/config/variables/base.rb +3 -0
  133. data/spec/fixtures/lono_project/config/variables/development.rb +1 -0
  134. data/spec/fixtures/lono_project/config/variables/production.rb +1 -0
  135. data/spec/fixtures/params/envonly/params/{prod → development}/network.txt +0 -0
  136. data/spec/fixtures/params/overlay/params/{prod → development}/network.txt +0 -0
  137. data/spec/fixtures/raw_templates/aws-waf-security-automations.template +2 -2
  138. data/spec/lib/lono/cfn_spec.rb +6 -9
  139. data/spec/lib/lono/cli_spec.rb +44 -0
  140. data/spec/lib/lono/completion_spec.rb +17 -0
  141. data/spec/lib/lono/inspect_spec.rb +6 -15
  142. data/spec/lib/lono/param/generator_spec.rb +45 -26
  143. data/spec/lib/lono/param_spec.rb +1 -3
  144. data/spec/lib/lono/setting_spec.rb +47 -0
  145. data/spec/lib/lono/template/dsl_spec.rb +33 -157
  146. data/spec/lib/lono/template_spec.rb +4 -16
  147. data/spec/spec_helper.rb +45 -14
  148. metadata +168 -82
  149. data/.coveralls.yml +0 -1
  150. data/lib/lono/cfn/help.rb +0 -103
  151. data/lib/lono/current_region.rb +0 -42
  152. data/lib/lono/inspector/help.rb +0 -21
  153. data/lib/lono/settings.rb +0 -45
  154. data/lib/lono/template/help.rb +0 -25
  155. data/lib/lono/template/helpers.rb +0 -136
  156. data/lib/starter_projects/json_project/.gitignore +0 -1
  157. data/lib/starter_projects/json_project/config/templates/base/blog.rb +0 -20
  158. data/lib/starter_projects/json_project/config/templates/base/stacks.rb +0 -58
  159. data/lib/starter_projects/json_project/templates/db.json +0 -212
  160. data/lib/starter_projects/json_project/templates/partial/host_record.json +0 -28
  161. data/lib/starter_projects/json_project/templates/partial/server.json +0 -45
  162. data/lib/starter_projects/json_project/templates/user_data/db.sh +0 -39
  163. data/lib/starter_projects/json_project/templates/user_data/db2.sh +0 -2
  164. data/lib/starter_projects/json_project/templates/user_data/ruby_script.rb +0 -5
  165. data/lib/starter_projects/json_project/templates/web.json +0 -386
  166. data/lib/starter_projects/yaml_project/.gitignore +0 -1
  167. data/lib/starter_projects/yaml_project/config/templates/base/blog.rb +0 -20
  168. data/lib/starter_projects/yaml_project/config/templates/base/stacks.rb +0 -56
  169. data/lib/starter_projects/yaml_project/config/templates/prod/stacks.rb +0 -1
  170. data/lib/starter_projects/yaml_project/config/templates/stag/stacks.rb +0 -1
  171. data/lib/starter_projects/yaml_project/config/variables/base/variables.rb +0 -4
  172. data/lib/starter_projects/yaml_project/config/variables/prod/variables.rb +0 -1
  173. data/lib/starter_projects/yaml_project/config/variables/stag/variables.rb +0 -1
  174. data/lib/starter_projects/yaml_project/params/base/example.txt +0 -2
  175. data/lib/starter_projects/yaml_project/params/prod/example.txt +0 -1
  176. data/lib/starter_projects/yaml_project/params/stag/example.txt +0 -1
  177. data/lib/starter_projects/yaml_project/templates/db.yml +0 -148
  178. data/lib/starter_projects/yaml_project/templates/partial/host_record.yml +0 -14
  179. data/lib/starter_projects/yaml_project/templates/partial/server.yml +0 -59
  180. data/lib/starter_projects/yaml_project/templates/web.yml +0 -206
  181. data/spec/fixtures/my_project/config/templates/base/stacks.rb +0 -3
  182. data/spec/fixtures/my_project/params/my-stack.txt +0 -3
  183. data/spec/fixtures/my_project/templates/my-stack.yml +0 -0
  184. data/spec/lib/lono/new_spec.rb +0 -59
  185. data/spec/lib/lono/template/template_spec.rb +0 -104
  186. data/spec/lib/lono_spec.rb +0 -27
@@ -1,46 +1,41 @@
1
- class Lono::ProjectChecker
2
- # Checks to see command is running in a lono project.
3
- # If not, provide a friendly message and exit.
4
- def self.check
5
- new.check
6
- end
7
-
8
- def check
9
- config_folder_exist
10
- templates_folder_exist
11
- empty_folders
12
- end
1
+ # frozen_string_literal: true
2
+ module Lono
3
+ # Checks to see command is running in a lono project.
4
+ # If not, provide a friendly message and possibly exit.
5
+ class ProjectChecker
6
+ class << self
7
+ def check
8
+ check_lono_project
9
+ end
13
10
 
14
- def config_folder_exist
15
- unless File.exist?("#{Lono.root}/config")
16
- puts "The config folder does not exist in this project. Are you sure this is a lono project?"
17
- quit
18
- end
19
- end
11
+ def check_lono_project
12
+ paths = %w[
13
+ config/settings.yml
14
+ app/definitions
15
+ app/templates
16
+ ]
17
+ paths.each do |path|
18
+ unless File.exist?("#{Lono.root}/#{path}")
19
+ puts "ERROR: The #{path} does not exist in this project. Are you sure you are in lono project?".colorize(:red)
20
+ quit 1
21
+ end
22
+ end
23
+ end
20
24
 
21
- def templates_folder_exist
22
- unless File.exist?("#{Lono.root}/templates")
23
- puts "The templates folder does not exist in this project. Are you sure this is a lono project?"
24
- quit
25
- end
26
- end
27
-
28
- def empty_folders
29
- if Dir["#{Lono.root}/config/**/*.rb"].empty?
30
- puts "The config folder does not contain any lono template definitions."
31
- quit
32
- end
33
- if Dir["#{Lono.root}/templates/**/*"].empty?
34
- puts "The templates folder does not contain any lono template definitions."
35
- quit
36
- end
37
- end
25
+ # Dont exit for this one. It's okay. But show a warning.
26
+ def empty_templates
27
+ if Dir["#{Lono.config.templates_path}/**/*"].empty?
28
+ puts "INFO: The app/templates folder does not contain any lono template definitions.".colorize(:yellow)
29
+ end
30
+ end
38
31
 
39
- def quit
40
- if ENV['TEST'] == '1'
41
- raise("Not in lono project")
42
- else
43
- exit 1
32
+ def quit(signal)
33
+ if ENV['TEST'] == '1'
34
+ signal == 0 || raise("Not in lono project")
35
+ else
36
+ exit(signal)
37
+ end
38
+ end
44
39
  end
45
40
  end
46
41
  end
@@ -0,0 +1,19 @@
1
+ module Lono
2
+ class Script < Command
3
+ autoload :Base, "lono/script/base"
4
+ autoload :Build, "lono/script/build"
5
+ autoload :Upload, "lono/script/upload"
6
+
7
+ desc "build", "Builds output/scripts/scripts-md5sum.tgz from app/script folder"
8
+ long_desc Help.text("script/build")
9
+ def build
10
+ Build.new(options).run
11
+ end
12
+
13
+ desc "upload", "Uploads output/scripts/scripts-md5sum.tgz to s3"
14
+ long_desc Help.text("script/upload")
15
+ def upload
16
+ Upload.new(options).run
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ class Lono::Script
2
+ class Base
3
+ SCRIPTS_INFO_PATH = "#{Lono.config.output_path}/data/scripts_info.txt"
4
+
5
+ def initialize(options = {})
6
+ @options = options
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,73 @@
1
+ require "digest"
2
+ require "fileutils"
3
+
4
+ class Lono::Script
5
+ class Build < Base
6
+ # Only avaialble after script has been built.
7
+ def self.scripts_name
8
+ new.scripts_name
9
+ end
10
+
11
+ def run
12
+ Lono::ProjectChecker.check
13
+ reset
14
+ if Dir["#{Lono.root}/app/scripts/*"].empty?
15
+ puts "No detected app/scripts"
16
+ return
17
+ else
18
+ puts "Detected app/scripts"
19
+ end
20
+
21
+ puts "Tarballing app/scripts folder to scripts.tgz"
22
+ tarball_path = create_tarball
23
+ save_scripts_info(tarball_path)
24
+ puts "Tarball created at #{tarball_path}"
25
+ end
26
+
27
+ # Only avaialble after script has been built.
28
+ def scripts_name
29
+ IO.read(SCRIPTS_INFO_PATH).strip
30
+ end
31
+
32
+ def reset
33
+ FileUtils.rm_f(SCRIPTS_INFO_PATH)
34
+ end
35
+
36
+ def create_tarball
37
+ # https://apple.stackexchange.com/questions/14980/why-are-dot-underscore-files-created-and-how-can-i-avoid-them
38
+ sh "cd app && dot_clean ." if system("type dot_clean > /dev/null")
39
+
40
+ # https://serverfault.com/questions/110208/different-md5sums-for-same-tar-contents
41
+ # Using tar czf directly results in a new m5sum each time because the gzip
42
+ # timestamp is included. So using: tar -c ... | gzip -n
43
+ sh "cd app && tar -c scripts | gzip -n > scripts.tgz" # temporary app/scripts.tgz file
44
+
45
+ rename_with_md5!
46
+ end
47
+
48
+ # Apppend a md5 to file after it's been created and moves it to
49
+ # output/scripts/scripts-[MD5].tgz
50
+ def rename_with_md5!
51
+ md5_path = "output/scripts/scripts-#{md5sum}.tgz"
52
+ FileUtils.mkdir_p(File.dirname(md5_path))
53
+ FileUtils.mv("app/scripts.tgz", md5_path)
54
+ md5_path
55
+ end
56
+
57
+ def save_scripts_info(scripts_name)
58
+ FileUtils.mkdir_p(File.dirname(SCRIPTS_INFO_PATH))
59
+ IO.write(SCRIPTS_INFO_PATH, scripts_name)
60
+ end
61
+
62
+ # cache this because the file will get removed
63
+ def md5sum
64
+ @md5sum ||= Digest::MD5.file("app/scripts.tgz").to_s[0..7]
65
+ end
66
+
67
+ def sh(command)
68
+ puts "=> #{command}"
69
+ system command
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,81 @@
1
+ require "aws-sdk-s3"
2
+ require "filesize"
3
+
4
+ class Lono::Script
5
+ class Upload < Base
6
+ def run
7
+ Lono::ProjectChecker.check
8
+ return unless scripts_built?
9
+
10
+ upload(tarball_path)
11
+ puts "Uploaded #{File.basename(s3_dest)} to s3}"
12
+ end
13
+
14
+ def upload(tarball_path)
15
+ puts "Uploading scripts.tgz (#{filesize}) to #{s3_dest}"
16
+ obj = s3_resource.bucket(bucket_name).object(key)
17
+ start_time = Time.now
18
+ obj.upload_file(tarball_path)
19
+ time_took = pretty_time(Time.now-start_time).colorize(:green)
20
+ puts "Time to upload code to s3: #{time_took}"
21
+ end
22
+
23
+ def filesize
24
+ Filesize.from(File.size(tarball_path).to_s + " B").pretty
25
+ end
26
+
27
+ def s3_dest
28
+ "s3://#{bucket_name}/#{key}"
29
+ end
30
+
31
+ def key
32
+ # Example key: cloudformation/development/scripts/scripts-md5
33
+ "#{dest_folder}/#{File.basename(tarball_path)}"
34
+ end
35
+
36
+ # Example:
37
+ # s3_folder: s3://infra-bucket/cloudformation
38
+ # bucket_name: infra-bucket
39
+ def bucket_name
40
+ s3_folder.sub('s3://','').split('/').first
41
+ end
42
+
43
+ # Removes s3://bucket-name and adds Lono.env. Example:
44
+ # s3_folder: s3://infra-bucket/cloudformation
45
+ # bucket_name: cloudformation/development/scripts
46
+ def dest_folder
47
+ folder = s3_folder.sub('s3://','').split('/')[1..-1].join('/')
48
+ "#{folder}/#{Lono.env}/scripts"
49
+ end
50
+
51
+ # Scripts are only built if the app/scripts folder is non empty
52
+ def scripts_built?
53
+ File.exist?(SCRIPTS_INFO_PATH) && !tarball_path.empty?
54
+ end
55
+
56
+ def tarball_path
57
+ IO.read(SCRIPTS_INFO_PATH).strip
58
+ end
59
+
60
+ # s3_folder example:
61
+ def s3_folder
62
+ setting = Lono::Setting.new
63
+ setting.s3_folder
64
+ end
65
+
66
+ def s3_resource
67
+ @s3_resource ||= Aws::S3::Resource.new
68
+ end
69
+
70
+ # http://stackoverflow.com/questions/4175733/convert-duration-to-hoursminutesseconds-or-similar-in-rails-3-or-ruby
71
+ def pretty_time(total_seconds)
72
+ minutes = (total_seconds / 60) % 60
73
+ seconds = total_seconds % 60
74
+ if total_seconds < 60
75
+ "#{seconds.to_i}s"
76
+ else
77
+ "#{minutes.to_i}m #{seconds.to_i}s"
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,33 @@
1
+ require 'fileutils'
2
+ require 'colorize'
3
+ require 'active_support/core_ext/string'
4
+ require 'thor'
5
+ require 'bundler'
6
+
7
+ class Lono::Sequence < Thor::Group
8
+ include Thor::Actions
9
+
10
+ def self.template_name
11
+ ENV['TEMPLATE'] || 'skeleton'
12
+ end
13
+
14
+ def self.source_root
15
+ starter_projects = File.expand_path("../starter_projects", File.dirname(__FILE__))
16
+ template_folder = "#{starter_projects}/#{template_name}"
17
+ unless File.exist?(template_folder)
18
+ templates = Dir.glob("#{starter_projects}/*")
19
+ .select { |f| File.directory?(f) }
20
+ .map { |f| " #{File.basename(f)}" }
21
+ .sort
22
+ puts "The TEMPLATE=#{ENV['TEMPLATE']} you specified does not exist.".colorize(:red)
23
+ puts "The available templates are:\n#{templates.join("\n")}"
24
+ exit
25
+ end
26
+ template_folder
27
+ end
28
+
29
+ private
30
+ def git_installed?
31
+ system("type git > /dev/null")
32
+ end
33
+ end
@@ -0,0 +1,83 @@
1
+ require 'yaml'
2
+
3
+ module Lono
4
+ class Setting
5
+ def initialize(check_lono_project=true)
6
+ @check_lono_project = check_lono_project
7
+ end
8
+
9
+ # data contains the settings.yml config. The order or precedence for settings
10
+ # is the project lono/settings.yml and then the ~/.lono/settings.yml.
11
+ @@data = nil
12
+ def data
13
+ return @@data if @@data
14
+
15
+ if @check_lono_project && !File.exist?(project_settings_path)
16
+ puts "ERROR: No settings file at #{project_settings_path}. Are you sure you are in a project with lono setup?".colorize(:red)
17
+ exit 1
18
+ end
19
+
20
+ # project based settings files
21
+ project = load_file(project_settings_path)
22
+
23
+ user_file = "#{ENV['HOME']}/.lono/settings.yml"
24
+ user = load_file(user_file)
25
+
26
+ default_file = File.expand_path("../default/settings.yml", __FILE__)
27
+ default = load_file(default_file)
28
+
29
+ all_envs = default.deep_merge(user.deep_merge(project))
30
+ all_envs = merge_base(all_envs)
31
+ @@data = all_envs[Lono.env] || all_envs["base"] || {}
32
+ end
33
+
34
+ # Special helper method to support multiple formats for s3_folder setting.
35
+ # Format 1: Simple String
36
+ #
37
+ # development:
38
+ # s3_folder: mybucket/path/to/folder
39
+ #
40
+ # Format 2: Hash
41
+ #
42
+ # development:
43
+ # s3_folder:
44
+ # default: mybucket/path/to/folder
45
+ # dev_profile1: mybucket/path/to/folder
46
+ # dev_profile1: another-bucket/storage/path
47
+ #
48
+ def s3_folder
49
+ s3_folder = data['s3_folder']
50
+ return s3_folder if s3_folder.nil? or s3_folder.is_a?(String)
51
+
52
+ # If reach here then the s3_folder is a Hash
53
+ s3_folder[ENV['AWS_PROFILE']] || s3_folder["default"]
54
+ end
55
+
56
+ private
57
+ def load_file(path)
58
+ return Hash.new({}) unless File.exist?(path)
59
+
60
+ content = RenderMePretty.result(path)
61
+ data = YAML.load(content)
62
+ # If key is is accidentally set to nil it screws up the merge_base later.
63
+ # So ensure that all keys with nil value are set to {}
64
+ data.each do |lono_env, _setting|
65
+ data[lono_env] ||= {}
66
+ end
67
+ data
68
+ end
69
+
70
+ # automatically add base settings to the rest of the environments
71
+ def merge_base(all_envs)
72
+ base = all_envs["base"]
73
+ all_envs.each do |lono_env, env_settings|
74
+ all_envs[lono_env] = base.merge(env_settings) unless lono_env == "base"
75
+ end
76
+ all_envs
77
+ end
78
+
79
+ def project_settings_path
80
+ "#{Lono.root}/config/settings.yml"
81
+ end
82
+ end
83
+ end
data/lib/lono/template.rb CHANGED
@@ -2,21 +2,20 @@ require "thor"
2
2
  require_relative "command"
3
3
 
4
4
  class Lono::Template < Lono::Command
5
- autoload :Help, 'lono/template/help'
6
- autoload :Helpers, 'lono/template/helpers'
5
+ autoload :Context, 'lono/template/context'
6
+ autoload :Helper, 'lono/template/helper'
7
7
  autoload :Bashify, 'lono/template/bashify'
8
8
  autoload :DSL, 'lono/template/dsl'
9
9
  autoload :Template, 'lono/template/template'
10
10
  autoload :Upload, 'lono/template/upload'
11
- autoload :AwsServices, 'lono/template/aws_services'
11
+ autoload :AwsService, 'lono/template/aws_service'
12
12
 
13
- class_option :quiet, type: :boolean, aliases: "-q", desc: "silence the output"
13
+ class_option :quiet, type: :boolean, desc: "silence the output"
14
14
  class_option :noop, type: :boolean, desc: "noop mode, do nothing destructive"
15
15
 
16
16
  desc "generate", "Generate the CloudFormation templates"
17
- Help.generate
18
- option :clean, type: :boolean, aliases: "-c", desc: "remove all output files before generating"
19
- option :pretty, type: :boolean, default: true, desc: "json pretty the output. only applies with json format"
17
+ long_desc Lono::Help.text("template/generate")
18
+ option :clean, type: :boolean, desc: "remove all output files before generating"
20
19
  def generate
21
20
  DSL.new(options.clone).run
22
21
  end
@@ -26,8 +25,8 @@ class Lono::Template < Lono::Command
26
25
  Upload.new(options.clone).run
27
26
  end
28
27
 
29
- desc "bashify [URL-OR-PATH]", "Convert the UserData section of an existing CloudFormation Template to a starter bash script that is compatiable with lono"
30
- Help.bashify
28
+ desc "bashify URL-OR-PATH", "Convert the UserData section of an existing CloudFormation Template to a starter bash script that is compatiable with lono"
29
+ long_desc Lono::Help.text("template/bashify")
31
30
  def bashify(path)
32
31
  Bashify.new(path: path).run
33
32
  end
@@ -1,6 +1,6 @@
1
1
  require "aws-sdk-s3"
2
2
 
3
- module Lono::Template::AwsServices
3
+ module Lono::Template::AwsService
4
4
  def s3
5
5
  return @s3 if @s3
6
6
 
@@ -0,0 +1,73 @@
1
+ # Encapsulates helper methods and instance variables to be rendered in the ERB
2
+ # templates.
3
+ class Lono::Template
4
+ class Context
5
+ include Lono::Template::Helper
6
+
7
+ def initialize(options={})
8
+ @options = options
9
+ load_variables
10
+ load_project_helpers
11
+ end
12
+
13
+ # Take a hash and makes them instance variables in the current scope.
14
+ # Use this in custom helper methods to make variables accessible to ERB templates.
15
+ def instance_variables!(variables)
16
+ variables.each do |key, value|
17
+ instance_variable_set('@' + key.to_s, value)
18
+ end
19
+ end
20
+
21
+ private
22
+ # Variables in base.rb are overridden by their environment specific variables
23
+ # file. Example, file LONO_ENV=development:
24
+ #
25
+ # config/variables/base.rb
26
+ # config/variables/development.rb - will override any variables in base.rb
27
+ #
28
+ def load_variables
29
+ load_variables_file("base")
30
+ load_variables_file(Lono.env)
31
+ end
32
+
33
+ # Load custom helper methods from project
34
+ def load_project_helpers
35
+ Dir.glob("#{Lono.config.helpers_path}/**/*_helper.rb").each do |path|
36
+ filename = path.sub(%r{.*/},'').sub('.rb','')
37
+ module_name = filename.classify
38
+
39
+ # Prepend a period so require works LONO_ROOT is set to a relative path
40
+ # without a period.
41
+ #
42
+ # Example: LONO_ROOT=tmp/lono_project
43
+ first_char = path[0..0]
44
+ path = "./#{path}" unless %w[. /].include?(first_char)
45
+ require path
46
+ self.class.send :include, module_name.constantize
47
+ end
48
+ end
49
+
50
+ # Load the variables defined in config/variables/* to make available in the
51
+ # template blocks in config/templates/*.
52
+ #
53
+ # Example:
54
+ #
55
+ # `config/variables/base.rb`:
56
+ # @foo = 123
57
+ #
58
+ # `app/definitions/base.rb`:
59
+ # template "mytemplate.yml" do
60
+ # source "mytemplate.yml.erb"
61
+ # variables(foo: @foo)
62
+ # end
63
+ #
64
+ # NOTE: Only able to make instance variables avaialble with instance_eval,
65
+ # wasnt able to make local variables available.
66
+ def load_variables_file(name)
67
+ path = "#{Lono.config.variables_path}/#{name}.rb"
68
+ return unless File.exist?(path)
69
+
70
+ instance_eval(IO.read(path))
71
+ end
72
+ end
73
+ end