Class: Lono::Seed::Base

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices, Blueprint::Root, Conventions, Thor::Actions, Thor::Base
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

attr_reader :options



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

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

Instance Method Details

#check_dsl_type!Object



94
95
96
97
98
99
100
101
# File 'lib/lono/seed/base.rb', line 94

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lono/seed/base.rb', line 61

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
  create_file(dest_path, content) # Thor::Action
end

#create_paramsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lono/seed/base.rb', line 42

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



88
89
90
91
92
# File 'lib/lono/seed/base.rb', line 88

def create_variables
  return unless variables
  dest_path = "configs/#{@blueprint}/variables/#{Lono.env}.rb"
  create_file(dest_path, variables) # Thor::Action
end

#default_value(data) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/lono/seed/base.rb', line 134

def default_value(data)
  value = data["Default"]
  # Dont use !blank? since there can be false optional values
  # Also dont use .empty? since value can be an Integer
  if value.nil? || value == ''
    description_example(data["Description"])
  else
    value
  end
end

#description_example(description) ⇒ Object



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

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

#finishObject



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

def finish; end

#optional(parameters) ⇒ Object



156
157
158
# File 'lib/lono/seed/base.rb', line 156

def optional(parameters)
  parameters.select { |logical_id, p| !p["Default"].nil? } # allow for false
end

#parameters(app_template_path) ⇒ Object



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

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

#paramsObject



57
58
59
# File 'lib/lono/seed/base.rb', line 57

def params
  true
end

#required(parameters) ⇒ Object



152
153
154
# File 'lib/lono/seed/base.rb', line 152

def required(parameters)
  parameters.select { |logical_id, p| p["Default"].nil? } # allow for false
end

#runObject



33
34
35
36
37
38
39
40
# File 'lib/lono/seed/base.rb', line 33

def run
  check_dsl_type!
  setup
  self.destination_root = Dir.pwd # Thor::Actions require destination_root to be set
  create_params
  create_variables
  finish
end

#setupObject



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

def setup; end

#template_typeObject



103
104
105
106
107
108
109
110
# File 'lib/lono/seed/base.rb', line 103

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.



117
118
119
# File 'lib/lono/seed/base.rb', line 117

def variables
  false
end

#write(path, content) ⇒ Object



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

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