lono 4.2.7 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. checksums.yaml +4 -4
  2. data/.cody/README.md +6 -0
  3. data/.cody/acceptance.sh +30 -0
  4. data/.cody/buildspec.yml +21 -0
  5. data/.cody/demo.rb +38 -0
  6. data/.cody/project.rb +12 -0
  7. data/.cody/role.rb +1 -0
  8. data/.gitignore +2 -0
  9. data/.gitmodules +6 -3
  10. data/.travis.yml +7 -0
  11. data/CHANGELOG.md +10 -0
  12. data/Gemfile +0 -1
  13. data/LICENSE.txt +1 -22
  14. data/README.md +46 -55
  15. data/lib/lono.rb +20 -27
  16. data/lib/lono/app_file.rb +5 -0
  17. data/lib/lono/app_file/base.rb +19 -0
  18. data/lib/lono/app_file/build.rb +78 -0
  19. data/lib/lono/app_file/registry.rb +14 -0
  20. data/lib/lono/app_file/registry/item.rb +46 -0
  21. data/lib/lono/app_file/upload.rb +39 -0
  22. data/lib/lono/autoloader.rb +22 -0
  23. data/lib/lono/aws_services.rb +46 -0
  24. data/lib/lono/aws_services/util.rb +49 -0
  25. data/lib/lono/blueprint.rb +113 -0
  26. data/lib/lono/blueprint/find.rb +90 -0
  27. data/lib/lono/blueprint/helper.rb +18 -0
  28. data/lib/lono/blueprint/info.rb +10 -0
  29. data/lib/lono/blueprint/list.rb +14 -0
  30. data/lib/lono/blueprint/root.rb +43 -0
  31. data/lib/lono/cfn.rb +31 -19
  32. data/lib/lono/cfn/aws_service.rb +16 -0
  33. data/lib/lono/cfn/base.rb +244 -261
  34. data/lib/lono/cfn/create.rb +36 -32
  35. data/lib/lono/cfn/current.rb +1 -1
  36. data/lib/lono/cfn/delete.rb +2 -2
  37. data/lib/lono/cfn/deploy.rb +11 -0
  38. data/lib/lono/cfn/diff.rb +1 -1
  39. data/lib/lono/cfn/preview.rb +3 -3
  40. data/lib/lono/cfn/rollback.rb +26 -0
  41. data/lib/lono/cfn/status.rb +2 -203
  42. data/lib/lono/cfn/suffix.rb +67 -0
  43. data/lib/lono/cfn/update.rb +61 -53
  44. data/lib/lono/cli.rb +42 -23
  45. data/lib/lono/completer.rb +0 -2
  46. data/lib/lono/configure.rb +37 -0
  47. data/lib/lono/configure/aws_services.rb +18 -0
  48. data/lib/lono/configure/base.rb +94 -0
  49. data/lib/lono/configure/helpers.rb +128 -0
  50. data/lib/lono/conventions.rb +11 -0
  51. data/lib/lono/core.rb +42 -12
  52. data/lib/lono/core/config.rb +5 -4
  53. data/lib/lono/default/settings.yml +0 -11
  54. data/lib/lono/file_uploader.rb +9 -4
  55. data/lib/lono/help.rb +1 -2
  56. data/lib/lono/help/blueprint.md +46 -0
  57. data/lib/lono/help/cfn.md +5 -4
  58. data/lib/lono/help/cfn/create.md +14 -9
  59. data/lib/lono/help/cfn/deploy.md +92 -0
  60. data/lib/lono/help/cfn/diff.md +0 -1
  61. data/lib/lono/help/cfn/update.md +16 -15
  62. data/lib/lono/help/completion.md +3 -3
  63. data/lib/lono/help/generate.md +0 -1
  64. data/lib/lono/help/new.md +40 -34
  65. data/lib/lono/help/param.md +1 -1
  66. data/lib/lono/help/param/generate.md +1 -1
  67. data/lib/lono/help/template.md +2 -2
  68. data/lib/lono/help/xgraph.md +1 -1
  69. data/lib/lono/inspector.rb +1 -1
  70. data/lib/lono/inspector/base.rb +26 -10
  71. data/lib/lono/inspector/graph.rb +7 -3
  72. data/lib/lono/inspector/summary.rb +15 -3
  73. data/lib/lono/md5.rb +46 -0
  74. data/lib/lono/new.rb +40 -28
  75. data/lib/lono/new/helper.rb +2 -3
  76. data/lib/lono/param.rb +12 -11
  77. data/lib/lono/param/generator.rb +96 -42
  78. data/lib/lono/project_checker.rb +27 -8
  79. data/lib/lono/s3.rb +23 -0
  80. data/lib/lono/s3/bucket.rb +123 -0
  81. data/lib/lono/script.rb +4 -8
  82. data/lib/lono/script/base.rb +7 -2
  83. data/lib/lono/script/build.rb +7 -8
  84. data/lib/lono/script/upload.rb +4 -20
  85. data/lib/lono/sequence.rb +19 -16
  86. data/lib/lono/setting.rb +19 -27
  87. data/lib/lono/template.rb +22 -26
  88. data/lib/lono/template/base.rb +13 -0
  89. data/lib/lono/template/context.rb +4 -56
  90. data/lib/lono/template/context/loader.rb +70 -0
  91. data/lib/lono/template/dsl.rb +15 -151
  92. data/lib/lono/template/dsl/builder.rb +60 -0
  93. data/lib/lono/template/dsl/builder/base.rb +14 -0
  94. data/lib/lono/template/dsl/builder/condition.rb +26 -0
  95. data/lib/lono/template/dsl/builder/fn.rb +114 -0
  96. data/lib/lono/template/dsl/builder/helper.rb +64 -0
  97. data/lib/lono/template/dsl/builder/mapping.rb +24 -0
  98. data/lib/lono/template/dsl/builder/output.rb +37 -0
  99. data/lib/lono/template/dsl/builder/parameter.rb +39 -0
  100. data/lib/lono/template/dsl/builder/resource.rb +38 -0
  101. data/lib/lono/template/dsl/builder/section.rb +12 -0
  102. data/lib/lono/template/dsl/builder/syntax.rb +58 -0
  103. data/lib/lono/template/erb.rb +82 -0
  104. data/lib/lono/template/evaluate.rb +39 -0
  105. data/lib/lono/template/generator.rb +29 -0
  106. data/lib/lono/template/helper.rb +7 -29
  107. data/lib/lono/template/post_processor.rb +69 -0
  108. data/lib/lono/template/template.rb +4 -9
  109. data/lib/lono/template/upload.rb +103 -133
  110. data/lib/lono/template/util.rb +48 -0
  111. data/lib/lono/upgrade.rb +5 -3
  112. data/lib/lono/upgrade/upgrade5.rb +55 -0
  113. data/lib/lono/user_data.rb +4 -4
  114. data/lib/lono/version.rb +1 -1
  115. data/lib/templates/blueprint/%blueprint_name%.gemspec.tt +44 -0
  116. data/lib/templates/blueprint/.gitignore +14 -0
  117. data/lib/templates/blueprint/.lono/config.yml.tt +3 -0
  118. data/lib/templates/blueprint/.meta/config.yml.tt +3 -0
  119. data/lib/templates/blueprint/CHANGELOG.md +7 -0
  120. data/lib/templates/blueprint/Gemfile +4 -0
  121. data/lib/templates/blueprint/README.md +37 -0
  122. data/lib/templates/blueprint/Rakefile +6 -0
  123. data/lib/templates/blueprint/setup/configs.rb +54 -0
  124. data/lib/templates/blueprint_configs/configs/%blueprint_name%/params/base.txt +2 -0
  125. data/lib/{starter_projects/skeleton/app/definitions/base.rb → templates/blueprint_configs/configs/%blueprint_name%/params/development.txt} +0 -0
  126. data/lib/templates/blueprint_configs/configs/%blueprint_name%/variables/base.rb +2 -0
  127. data/lib/templates/blueprint_configs/configs/%blueprint_name%/variables/development.rb +0 -0
  128. data/lib/templates/blueprint_types/dsl/app/templates/%blueprint_name%.rb +37 -0
  129. data/lib/templates/blueprint_types/dsl/app/user_data/bootstrap.sh +2 -0
  130. data/lib/templates/blueprint_types/erb/app/definitions/base.rb.tt +1 -0
  131. data/lib/templates/blueprint_types/erb/app/templates/%blueprint_name%.yml +8 -0
  132. data/lib/{starter_projects/autoscaling → templates/skeleton}/.gitignore +1 -0
  133. data/lib/templates/skeleton/Gemfile +3 -0
  134. data/lib/{starter_projects/autoscaling → templates/skeleton}/Guardfile +2 -2
  135. data/lib/templates/skeleton/README.md +58 -0
  136. data/lib/templates/skeleton/configs/settings.yml +17 -0
  137. data/lib/templates/upgrade5/blueprints/main/.lono/config.yml +3 -0
  138. data/lib/templates/upgrade5/blueprints/main/.meta/config.yml +3 -0
  139. data/lono.gemspec +12 -8
  140. data/vendor/cfn-status/CHANGELOG.md +10 -0
  141. data/vendor/cfn-status/Gemfile +4 -0
  142. data/vendor/cfn-status/LICENSE.txt +21 -0
  143. data/vendor/cfn-status/README.md +56 -0
  144. data/vendor/cfn-status/Rakefile +6 -0
  145. data/vendor/cfn-status/bin/console +14 -0
  146. data/vendor/cfn-status/bin/setup +8 -0
  147. data/vendor/cfn-status/cfn-status.gemspec +30 -0
  148. data/vendor/cfn-status/lib/cfn-status.rb +1 -0
  149. data/vendor/cfn-status/lib/cfn/aws_service.rb +51 -0
  150. data/vendor/cfn-status/lib/cfn/status.rb +219 -0
  151. data/vendor/cfn-status/lib/cfn/status/version.rb +5 -0
  152. data/vendor/cfn-status/spec/cfn/status_spec.rb +81 -0
  153. data/vendor/cfn-status/spec/fixtures/cfn/stack-events-complete.json +1080 -0
  154. data/vendor/cfn-status/spec/fixtures/cfn/stack-events-in-progress.json +1080 -0
  155. data/vendor/cfn-status/spec/fixtures/cfn/stack-events-update-rollback-complete.json +1086 -0
  156. data/vendor/cfn-status/spec/spec_helper.rb +14 -0
  157. data/vendor/cfn_camelizer/CHANGELOG.md +20 -0
  158. data/vendor/cfn_camelizer/Gemfile +4 -0
  159. data/vendor/cfn_camelizer/LICENSE.txt +21 -0
  160. data/vendor/cfn_camelizer/README.md +40 -0
  161. data/vendor/cfn_camelizer/Rakefile +6 -0
  162. data/vendor/cfn_camelizer/bin/console +14 -0
  163. data/vendor/cfn_camelizer/bin/setup +8 -0
  164. data/vendor/cfn_camelizer/cfn_camelizer.gemspec +32 -0
  165. data/vendor/cfn_camelizer/lib/camelizer.yml +37 -0
  166. data/vendor/cfn_camelizer/lib/cfn_camelizer.rb +94 -0
  167. data/vendor/cfn_camelizer/lib/cfn_camelizer/version.rb +3 -0
  168. data/vendor/cfn_camelizer/spec/cfn_camelizer_spec.rb +86 -0
  169. data/vendor/cfn_camelizer/spec/spec_helper.rb +14 -0
  170. metadata +189 -62
  171. data/.circleci/bin/commit_docs.sh +0 -26
  172. data/.circleci/config.yml +0 -72
  173. data/bin/release +0 -9
  174. data/lib/lono/help/import.md +0 -54
  175. data/lib/lono/importer.rb +0 -134
  176. data/lib/lono/new/message.rb +0 -35
  177. data/lib/starter_projects/autoscaling/Gemfile +0 -3
  178. data/lib/starter_projects/autoscaling/README.md +0 -118
  179. data/lib/starter_projects/autoscaling/app/definitions/base.rb +0 -2
  180. data/lib/starter_projects/autoscaling/app/templates/autoscaling.yml +0 -682
  181. data/lib/starter_projects/autoscaling/config/params/base/autoscaling.txt +0 -6
  182. data/lib/starter_projects/autoscaling/config/settings.yml +0 -33
  183. data/lib/starter_projects/ec2/.gitignore +0 -2
  184. data/lib/starter_projects/ec2/Gemfile +0 -3
  185. data/lib/starter_projects/ec2/Guardfile +0 -12
  186. data/lib/starter_projects/ec2/README.md +0 -86
  187. data/lib/starter_projects/ec2/app/definitions/base.rb +0 -2
  188. data/lib/starter_projects/ec2/app/definitions/development.rb +0 -1
  189. data/lib/starter_projects/ec2/app/definitions/production.rb +0 -1
  190. data/lib/starter_projects/ec2/app/helpers/my_custom_helper.rb +0 -17
  191. data/lib/starter_projects/ec2/app/partials/user_data/bootstrap.sh +0 -4
  192. data/lib/starter_projects/ec2/app/templates/example.yml +0 -430
  193. data/lib/starter_projects/ec2/config/params/base/example.txt +0 -2
  194. data/lib/starter_projects/ec2/config/params/development/example.txt +0 -3
  195. data/lib/starter_projects/ec2/config/params/production/example.txt +0 -2
  196. data/lib/starter_projects/ec2/config/settings.yml +0 -33
  197. data/lib/starter_projects/ec2/config/variables/base.rb +0 -3
  198. data/lib/starter_projects/ec2/config/variables/development.rb +0 -2
  199. data/lib/starter_projects/ec2/config/variables/production.rb +0 -2
  200. data/lib/starter_projects/ec2/welcome.txt +0 -8
  201. data/lib/starter_projects/skeleton/.gitignore +0 -2
  202. data/lib/starter_projects/skeleton/Gemfile +0 -3
  203. data/lib/starter_projects/skeleton/Guardfile +0 -12
  204. data/lib/starter_projects/skeleton/README.md +0 -53
  205. data/lib/starter_projects/skeleton/config/settings.yml +0 -33
  206. data/lib/starter_projects/skeleton/welcome.txt +0 -7
  207. data/vendor/plissken/Gemfile +0 -14
  208. data/vendor/plissken/LICENSE.txt +0 -20
  209. data/vendor/plissken/README.md +0 -46
  210. data/vendor/plissken/Rakefile +0 -56
  211. data/vendor/plissken/VERSION +0 -1
  212. data/vendor/plissken/lib/plissken.rb +0 -1
  213. data/vendor/plissken/lib/plissken/ext/hash/to_snake_keys.rb +0 -45
  214. data/vendor/plissken/plissken.gemspec +0 -61
  215. data/vendor/plissken/spec/lib/to_snake_keys_spec.rb +0 -177
  216. data/vendor/plissken/spec/spec_helper.rb +0 -90
  217. data/vendor/plissken/test/helper.rb +0 -20
  218. data/vendor/plissken/test/plissken/ext/hash/to_snake_keys_test.rb +0 -184
  219. data/vendor/plissken/test/test_plissken.rb +0 -2
@@ -1,8 +1,9 @@
1
- require "aws-sdk-s3"
2
1
  require "filesize"
3
2
 
4
3
  class Lono::Script
5
4
  class Upload < Base
5
+ include Lono::AwsServices
6
+
6
7
  def run
7
8
  Lono::ProjectChecker.check
8
9
  return unless scripts_built?
@@ -33,19 +34,12 @@ class Lono::Script
33
34
  "#{dest_folder}/#{File.basename(tarball_path)}"
34
35
  end
35
36
 
36
- # Example:
37
- # s3_folder: s3://infra-bucket/cloudformation
38
- # bucket_name: infra-bucket
39
37
  def bucket_name
40
- s3_folder.sub('s3://','').split('/').first
38
+ Lono::S3::Bucket.name
41
39
  end
42
40
 
43
- # Removes s3://bucket-name and adds Lono.env. Example:
44
- # s3_folder: s3://infra-bucket/cloudformation
45
- # bucket_name: cloudformation/development/scripts
46
41
  def dest_folder
47
- folder = s3_folder.sub('s3://','').split('/')[1..-1].join('/')
48
- "#{folder}/#{Lono.env}/scripts"
42
+ "#{Lono.env}/scripts"
49
43
  end
50
44
 
51
45
  # Scripts are only built if the app/scripts folder is non empty
@@ -57,16 +51,6 @@ class Lono::Script
57
51
  IO.read(SCRIPTS_INFO_PATH).strip
58
52
  end
59
53
 
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
54
  # http://stackoverflow.com/questions/4175733/convert-duration-to-hoursminutesseconds-or-similar-in-rails-3-or-ruby
71
55
  def pretty_time(total_seconds)
72
56
  minutes = (total_seconds / 60) % 60
@@ -6,27 +6,30 @@ require 'bundler'
6
6
  class Lono::Sequence < Thor::Group
7
7
  include Thor::Actions
8
8
 
9
- def self.template_name
10
- ENV['TEMPLATE'] || 'skeleton'
11
- end
12
-
13
9
  def self.source_root
14
- starter_projects = File.expand_path("../starter_projects", File.dirname(__FILE__))
15
- template_folder = "#{starter_projects}/#{template_name}"
16
- unless File.exist?(template_folder)
17
- templates = Dir.glob("#{starter_projects}/*")
18
- .select { |f| File.directory?(f) }
19
- .map { |f| " #{File.basename(f)}" }
20
- .sort
21
- puts "The TEMPLATE=#{ENV['TEMPLATE']} you specified does not exist.".color(:red)
22
- puts "The available templates are:\n#{templates.join("\n")}"
23
- exit
24
- end
25
- template_folder
10
+ File.expand_path("../templates/skeleton", File.dirname(__FILE__))
26
11
  end
27
12
 
28
13
  private
29
14
  def git_installed?
30
15
  system("type git > /dev/null")
31
16
  end
17
+
18
+ def run_git?
19
+ options[:git] && git_installed?
20
+ end
21
+
22
+ def run_git_init
23
+ return unless run_git?
24
+ puts "=> Initialize git repo"
25
+ run("git init")
26
+ end
27
+
28
+ def run_git_commit
29
+ return unless run_git?
30
+
31
+ puts "=> Commit git repo"
32
+ run("git add .")
33
+ run("git commit -m 'first commit'")
34
+ end
32
35
  end
@@ -12,10 +12,7 @@ module Lono
12
12
  def data
13
13
  return @@data if @@data
14
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?".color(:red)
17
- exit 1
18
- end
15
+ project_settings_path = lookup_project_settings_path
19
16
 
20
17
  # project based settings files
21
18
  project = load_file(project_settings_path)
@@ -31,26 +28,25 @@ module Lono
31
28
  @@data = all_envs[Lono.env] || all_envs["base"] || {}
32
29
  end
33
30
 
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)
31
+ def lookup_project_settings_path
32
+ standalone_path = "#{Lono.root}/config/settings.yml"
33
+ multimode_path = "#{Lono.root}/configs/settings.yml"
34
+ parent_multimode_path = "#{Lono.root}/../../configs/settings.yml"
35
+
36
+ settings_path = if File.exist?(standalone_path)
37
+ standalone_path
38
+ elsif File.exist?(multimode_path)
39
+ multimode_path
40
+ elsif File.exist?(parent_multimode_path)
41
+ parent_multimode_path
42
+ end
43
+
44
+ if @check_lono_project && !settings_path
45
+ puts "ERROR: No lono settings file found. Are you sure you are in a project with lono setup?".color(:red)
46
+ exit 1
47
+ end
51
48
 
52
- # If reach here then the s3_folder is a Hash
53
- s3_folder[ENV['AWS_PROFILE']] || s3_folder["default"]
49
+ settings_path
54
50
  end
55
51
 
56
52
  private
@@ -75,9 +71,5 @@ module Lono
75
71
  end
76
72
  all_envs
77
73
  end
78
-
79
- def project_settings_path
80
- "#{Lono.root}/config/settings.yml"
81
- end
82
74
  end
83
75
  end
@@ -1,33 +1,29 @@
1
1
  require "thor"
2
2
  require_relative "command"
3
3
 
4
- class Lono::Template < Lono::Command
5
- autoload :Context, 'lono/template/context'
6
- autoload :Helper, 'lono/template/helper'
7
- autoload :Bashify, 'lono/template/bashify'
8
- autoload :DSL, 'lono/template/dsl'
9
- autoload :Template, 'lono/template/template'
10
- autoload :Upload, 'lono/template/upload'
11
- autoload :AwsService, 'lono/template/aws_service'
4
+ module Lono
5
+ class Template < Lono::Command
6
+ class_option :quiet, type: :boolean, desc: "silence the output"
7
+ class_option :noop, type: :boolean, desc: "noop mode, do nothing destructive"
12
8
 
13
- class_option :quiet, type: :boolean, desc: "silence the output"
14
- class_option :noop, type: :boolean, desc: "noop mode, do nothing destructive"
9
+ desc "generate", "Generate the CloudFormation templates"
10
+ long_desc Lono::Help.text("template/generate")
11
+ option :clean, type: :boolean, desc: "remove all output files before generating"
12
+ def generate(blueprint=nil)
13
+ Blueprint::Find.one_or_all(blueprint).each do |b|
14
+ Generator.new(b, options).run
15
+ end
16
+ end
15
17
 
16
- desc "generate", "Generate the CloudFormation templates"
17
- long_desc Lono::Help.text("template/generate")
18
- option :clean, type: :boolean, desc: "remove all output files before generating"
19
- def generate
20
- DSL.new(options.clone).run
21
- end
22
-
23
- desc "upload", "Uploads templates to configured s3 folder"
24
- def upload
25
- Upload.new(options.clone).run
26
- end
18
+ desc "upload", "Uploads templates to configured s3 folder"
19
+ def upload(blueprint)
20
+ Upload.new(blueprint, options.clone).run
21
+ end
27
22
 
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")
30
- def bashify(path)
31
- Bashify.new(path: path).run
23
+ desc "bashify URL-OR-PATH", "Convert the UserData section of an existing CloudFormation Template to a starter bash script that is compatiable with lono"
24
+ long_desc Lono::Help.text("template/bashify")
25
+ def bashify(path)
26
+ Bashify.new(path: path).run
27
+ end
32
28
  end
33
- end
29
+ end
@@ -0,0 +1,13 @@
1
+ class Lono::Template
2
+ class Base
3
+ include Lono::Blueprint::Root
4
+
5
+ def initialize(blueprint, options={})
6
+ @blueprint, @options = blueprint, options
7
+ @template = @options[:template] || @blueprint
8
+ Lono::ProjectChecker.check
9
+ set_blueprint_root(@blueprint)
10
+ Lono::ProjectChecker.empty_templates
11
+ end
12
+ end
13
+ end
@@ -1,11 +1,11 @@
1
- # Encapsulates helper methods and instance variables to be rendered in the ERB
2
- # templates.
1
+ # Encapsulates helper methods and instance variables to be rendered in the ERB templates.
3
2
  class Lono::Template
4
3
  class Context
5
4
  include Lono::Template::Helper
5
+ include Loader
6
6
 
7
- def initialize(options={})
8
- @options = options
7
+ def initialize(blueprint, options={})
8
+ @blueprint, @options = blueprint, options
9
9
  load_variables
10
10
  load_project_helpers
11
11
  end
@@ -17,57 +17,5 @@ class Lono::Template
17
17
  instance_variable_set('@' + key.to_s, value)
18
18
  end
19
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
20
  end
73
21
  end
@@ -0,0 +1,70 @@
1
+ class Lono::Template::Context
2
+ module Loader
3
+ private
4
+ # Variables in base.rb are overridden by their environment specific variables
5
+ # file. Example, file LONO_ENV=development:
6
+ #
7
+ # config/variables/base.rb
8
+ # config/variables/development.rb - will override any variables in base.rb
9
+ #
10
+ def load_variables
11
+ load_variables_file(blueprint_path("base"))
12
+ load_variables_file(blueprint_path(Lono.env))
13
+ load_variables_file(project_path("base"))
14
+ load_variables_file(project_path(Lono.env))
15
+ end
16
+
17
+ def blueprint_path(name)
18
+ "#{Lono.blueprint_root}/config/variables/#{name}.rb"
19
+ end
20
+
21
+ def project_path(name)
22
+ "#{Lono.root}/configs/#{@blueprint}/variables/#{name}.rb"
23
+ end
24
+
25
+ # Load the variables defined in config/variables/* to make available in the
26
+ # template blocks in config/templates/*.
27
+ #
28
+ # Example:
29
+ #
30
+ # `config/variables/base.rb`:
31
+ # @foo = 123
32
+ #
33
+ # `app/definitions/base.rb`:
34
+ # template "mytemplate.yml" do
35
+ # source "mytemplate.yml.erb"
36
+ # variables(foo: @foo)
37
+ # end
38
+ #
39
+ # NOTE: Only able to make instance variables avaialble with instance_eval,
40
+ # wasnt able to make local variables available.
41
+ def load_variables_file(path)
42
+ # if File.exist?(path)
43
+ # puts "context.rb loading #{path}"
44
+ # else
45
+ # puts "context.rb file doesnt exist #{path}"
46
+ # end
47
+
48
+ return unless File.exist?(path)
49
+
50
+ instance_eval(IO.read(path), path)
51
+ end
52
+
53
+ # Load custom helper methods from project
54
+ def load_project_helpers
55
+ Dir.glob("#{Lono.config.helpers_path}/**/*_helper.rb").each do |path|
56
+ filename = path.sub(%r{.*/},'').sub('.rb','')
57
+ module_name = filename.classify
58
+
59
+ # Prepend a period so require works LONO_ROOT is set to a relative path
60
+ # without a period.
61
+ #
62
+ # Example: LONO_ROOT=tmp/lono_project
63
+ first_char = path[0..0]
64
+ path = "./#{path}" unless %w[. /].include?(first_char)
65
+ require path
66
+ self.class.send :include, module_name.constantize
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,157 +1,21 @@
1
- class Lono::Template::DSL
2
- def initialize(options={})
3
- @options = options
4
- Lono::ProjectChecker.check
5
- Lono::ProjectChecker.empty_templates
6
- @templates = []
7
- @results = {}
8
- end
9
-
10
- def run(options={})
11
- evaluate_templates
12
- build_templates
13
- write_output
14
- end
15
-
16
- # Instance eval's the template declarations in app/definitions in this order:
17
- #
18
- # app/definitions/base.rb
19
- # app/definitions/base - all files in folder
20
- # app/definitions/[Lono.env].rb
21
- # app/definitions/[Lono.env] - all files in folder
22
- #
23
- # So Lono.env specific template declarations override base template declarations.
24
- def evaluate_templates
25
- evaluate_template("base")
26
- evaluate_folder("base")
27
- evaluate_template(Lono.env)
28
- evaluate_folder(Lono.env)
29
- end
30
-
31
- def evaluate_template(name)
32
- path = "#{Lono.config.definitions_path}/#{name}.rb"
33
- evaluate_template_path(path)
34
- end
35
-
36
- def evaluate_template_path(path)
37
- return unless File.exist?(path)
38
-
39
- begin
40
- instance_eval(File.read(path), path)
41
- rescue Exception => e
42
- template_evaluation_error(e)
43
- puts "\nFull error:"
44
- raise
45
- end
46
- end
47
-
48
- def evaluate_folder(folder)
49
- paths = Dir.glob("#{Lono.config.definitions_path}/#{folder}/**/*")
50
- paths.select{ |e| File.file?(e) }.each do |path|
51
- evaluate_template_path(path)
52
- end
53
- end
54
-
55
- # Prints out a user friendly task_definition error message
56
- def template_evaluation_error(e)
57
- error_info = e.backtrace.first
58
- path, line_no, _ = error_info.split(':')
59
- line_no = line_no.to_i
60
- puts "Error evaluating #{path}:".color(:red)
61
- puts e.message
62
- puts "Here's the line in #{path} with the error:\n\n"
63
-
64
- contents = IO.read(path)
65
- content_lines = contents.split("\n")
66
- context = 5 # lines of context
67
- top, bottom = [line_no-context-1, 0].max, line_no+context-1
68
- spacing = content_lines.size.to_s.size
69
- content_lines[top..bottom].each_with_index do |line_content, index|
70
- line_number = top+index+1
71
- if line_number == line_no
72
- printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
73
- else
74
- printf("%#{spacing}d %s\n", line_number, line_content)
1
+ class Lono::Template
2
+ class Dsl < Base
3
+ attr_reader :results
4
+ def initialize(blueprint, options={})
5
+ super
6
+ end
7
+
8
+ def run
9
+ puts "Generating CloudFormation templates for blueprint #{@blueprint.color(:green)}:" unless @options[:quiet]
10
+ paths = Dir.glob("#{Lono.config.templates_path}/**/*.rb")
11
+ paths.select{ |e| File.file?(e) }.each do |path|
12
+ build_template(path)
75
13
  end
76
14
  end
77
- end
78
-
79
- def template(name, &block)
80
- @templates << {name: name, block: block}
81
- end
82
15
 
83
- def build_templates
84
- @templates.each do |t|
85
- @results[t[:name]] = Lono::Template::Template.new(t[:name], t[:block], @options).build
16
+ def build_template(path)
17
+ builder = Builder.new(path, @blueprint, @options)
18
+ builder.build
86
19
  end
87
20
  end
88
-
89
- def write_output
90
- output_path = "#{Lono.config.output_path}/templates"
91
- FileUtils.rm_rf(output_path) if @options[:clean]
92
- FileUtils.mkdir_p(output_path)
93
- puts "Generating CloudFormation templates:" unless @options[:quiet]
94
- @results.each do |name,text|
95
- path = "#{output_path}/#{name}".sub(/^\.\//,'') # strip leading '.'
96
- path += ".yml"
97
- puts " #{path}" unless @options[:quiet]
98
- ensure_parent_dir(path)
99
- text = commented(text)
100
- IO.write(path, text) # write file first so validate method is simpler
101
- validate(path)
102
- end
103
- end
104
-
105
- def validate(path)
106
- text = IO.read(path)
107
- begin
108
- YAML.load(text)
109
- rescue Psych::SyntaxError => e
110
- handle_yaml_syntax_error(e, path)
111
- end
112
- end
113
-
114
- def handle_yaml_syntax_error(e, path)
115
- io = StringIO.new
116
- io.puts "Invalid yaml. Output written to debugging: #{path}".color(:red)
117
- io.puts "ERROR: #{e.message}".color(:red)
118
-
119
- # Grab line info. Example error:
120
- # ERROR: (<unknown>): could not find expected ':' while scanning a simple key at line 2 column 1
121
- md = e.message.match(/at line (\d+) column (\d+)/)
122
- line = md[1].to_i
123
-
124
- lines = IO.read(path).split("\n")
125
- context = 5 # lines of context
126
- top, bottom = [line-context-1, 0].max, line+context-1
127
- spacing = lines.size.to_s.size
128
- lines[top..bottom].each_with_index do |line_content, index|
129
- line_number = top+index+1
130
- if line_number == line
131
- io.printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
132
- else
133
- io.printf("%#{spacing}d %s\n", line_number, line_content)
134
- end
135
- end
136
-
137
- if ENV['TEST']
138
- io.string
139
- else
140
- puts io.string
141
- exit 1
142
- end
143
- end
144
-
145
- def commented(text)
146
- comment =<<~EOS
147
- # This file was generated with lono. Do not edit directly, the changes will be lost.
148
- # More info: http://lono.cloud
149
- EOS
150
- "#{comment}#{text}"
151
- end
152
-
153
- def ensure_parent_dir(path)
154
- dir = File.dirname(path)
155
- FileUtils.mkdir_p(dir) unless File.exist?(dir)
156
- end
157
21
  end