Class: Lono::Seed::Base

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices, Blueprint::Root, Conventions
Defined in:
lib/lono/seed/base.rb

Direct Known Subclasses

Configs

Instance Method Summary collapse

Methods included from Conventions

#template_param_convention

Methods included from AwsServices

#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts

Methods included from AwsServices::Util

#find_stack, #rollback_complete?, #stack_exists?, #testing_update?

Methods included from Blueprint::Root

#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root

Constructor Details

#initialize(blueprint, options) ⇒ Base

Returns a new instance of Base.



20
21
22
23
# File 'lib/lono/seed/base.rb', line 20

def initialize(blueprint, options)
  @blueprint, @options = blueprint, options
  @template, @param = template_param_convention(options)
end

Instance Method Details

#check_dsl_type!Object



59
60
61
62
63
64
65
66
# File 'lib/lono/seed/base.rb', line 59

def check_dsl_type!
  dsl_type = template_type == 'dsl'
  unless dsl_type
    puts "Detected template_type: #{template_type}"
    puts "lono seed only supports dsl template types currently."
    exit 1
  end
end

#create_param_file(app_template_path) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lono/seed/base.rb', line 86

def create_param_file(app_template_path)
  parameters = parameters(app_template_path)

  lines = []
  required = required(parameters)
  lines << "# Required parameters:" unless required.empty?
  required.each do |name, data|
    example = description_example(data["Description"])
    lines << "#{name}=#{example}"
  end
  optional = optional(parameters)
  lines << "# Optional parameters:" unless optional.empty?
  optional.each do |name, data|
    value = default_value(data)
    lines << "# #{name}=#{value}"
  end

  if lines.empty?
    puts "Template has no parameters."
    return
  end

  content = lines.join("\n") + "\n"
  dest_path = "configs/#{@blueprint}/params/#{Lono.env}.txt" # only support environment level parameters for now
  write(dest_path, content)
  puts "Starter params created:    #{dest_path}"
end

#create_paramsObject

Always create params files



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lono/seed/base.rb', line 34

def create_params
  return unless params
  # Only supporting the main blueprint for now
  path = "#{Lono.config.templates_path}/#{@blueprint}.rb"
  if File.exist?(path)
    create_param_file(path)
  end

  # TODO: detect and write multiple templates to different paths
  # with_each_template do |path|
  #   create_param_file(path)
  # end
end

#create_variablesObject



52
53
54
55
56
57
# File 'lib/lono/seed/base.rb', line 52

def create_variables
  return unless variables
  dest_path = "configs/#{@blueprint}/variables/#{Lono.env}.rb"
  write(dest_path, variables)
  puts "Starter variables created: #{dest_path}"
end

#default_value(data) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/lono/seed/base.rb', line 127

def default_value(data)
  value = data["Default"]
  if value.blank?
    description_example(data["Description"])
  else
    value
  end
end

#description_example(description) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/lono/seed/base.rb', line 119

def description_example(description)
  default = '...'
  return default unless description
  md = description.match(/(Example|IE): (.*)/)
  return default unless md
  md[2]
end

#finishObject



78
# File 'lib/lono/seed/base.rb', line 78

def finish; end

#optional(parameters) ⇒ Object



147
148
149
# File 'lib/lono/seed/base.rb', line 147

def optional(parameters)
  parameters.select { |logical_id, p| p["Default"] }
end

#parameters(app_template_path) ⇒ Object



136
137
138
139
140
# File 'lib/lono/seed/base.rb', line 136

def parameters(app_template_path)
  builder = Lono::Template::Dsl::Builder.new(app_template_path, @blueprint, quiet: false)
  template = builder.template
  template["Parameters"] || []
end

#paramsObject



48
49
50
# File 'lib/lono/seed/base.rb', line 48

def params
  true
end

#required(parameters) ⇒ Object



143
144
145
# File 'lib/lono/seed/base.rb', line 143

def required(parameters)
  parameters.reject { |logical_id, p| p["Default"] }
end

#runObject



25
26
27
28
29
30
31
# File 'lib/lono/seed/base.rb', line 25

def run
  check_dsl_type!
  setup
  create_params
  create_variables
  finish
end

#setupObject



77
# File 'lib/lono/seed/base.rb', line 77

def setup; end

#template_typeObject



68
69
70
71
72
73
74
75
# File 'lib/lono/seed/base.rb', line 68

def template_type
  blueprint_root = find_blueprint_root(@blueprint)
  meta_config = "#{blueprint_root}/.meta/config.yml"
  return false unless File.exist?(meta_config)

  meta = YAML.load_file(meta_config)
  meta['template_type']
end

#variablesObject

Meant to be overriden by subclass Return String with contents of variables file.



82
83
84
# File 'lib/lono/seed/base.rb', line 82

def variables
  false
end

#write(path, content) ⇒ Object



114
115
116
117
# File 'lib/lono/seed/base.rb', line 114

def write(path, content)
  FileUtils.mkdir_p(File.dirname(path))
  IO.write(path, content)
end