Class: Lono::Seed::Base

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

Direct Known Subclasses

Configs

Instance Method Summary collapse

Methods included from ServiceRole

#create_service_linked_role

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



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

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

Instance Method Details

#check_dsl_type!Object



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

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



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
87
# File 'lib/lono/seed/base.rb', line 62

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



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

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



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

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



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

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



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

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

#finishObject



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

def finish; end

#optional(parameters) ⇒ Object



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

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

#parameters(app_template_path) ⇒ Object



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

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

#paramsObject



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

def params
  true
end

#required(parameters) ⇒ Object



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

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

#runObject



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

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



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

def setup; end

#template_typeObject



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

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.



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

def variables
  false
end

#write(path, content) ⇒ Object



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

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