Class: Lono::Upgrade::Upgrade4

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/upgrade/upgrade4.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Upgrade4

Returns a new instance of Upgrade4.



6
7
8
# File 'lib/lono/upgrade/upgrade4.rb', line 6

def initialize(options)
  @options = options
end

Instance Method Details

#add_mandantory_settingsObject

If config/settings.yml does not exist, use the default one.



162
163
164
165
166
167
# File 'lib/lono/upgrade/upgrade4.rb', line 162

def add_mandantory_settings
  return if File.exists?("config/settings.yml")

  default_settings = File.expand_path("default/settings.yml", File.dirname(__FILE__))
  FileUtils.cp(default_settings, "config/settings.yml")
end

#checksObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lono/upgrade/upgrade4.rb', line 95

def checks
  if File.exist?(Lono.config.definitions_path)
    puts "It looks like you already have a #{Lono.config.definitions_path} folder in your project. This is the new project structure so exiting without updating anything."
    exit
  end

  old_templates_path = "#{Lono.root}/config/templates"
  if !File.exist?(old_templates_path)
    puts "Could not find a #{old_templates_path} folder in your project. Maybe you want to run lono new to initialize a new lono project instead?"
    exit
  end
end

#env_map(lono_env) ⇒ Object

make to the longer full environment names



86
87
88
89
90
91
92
93
# File 'lib/lono/upgrade/upgrade4.rb', line 86

def env_map(lono_env)
  map = {
    "prod" => "production",
    "stag" => "staging",
    "dev" => "development",
  }
  map[lono_env] || lono_env
end

#mv(src, dest) ⇒ Object



169
170
171
172
173
# File 'lib/lono/upgrade/upgrade4.rb', line 169

def mv(src, dest)
  return unless File.exist?(src)
  puts "mv #{src} #{dest}"
  FileUtils.mv(src, dest)
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lono/upgrade/upgrade4.rb', line 10

def run
  checks
  puts "Upgrading structure of your current project to the new lono version 4 project structure"

  upgrade_settings("config/settings.yml")
  upgrade_settings("#{ENV['HOME']}/.lono/settings.yml")
  add_mandantory_settings

  FileUtils.mkdir_p("app")
  mv("helpers", "app/helpers")
  mv("params", "config/params")
  mv("config/templates", "app/definitions")
  mv("templates", "app/templates")
  mv("app/templates/partial", "app/partials")
  mv("app/partials/user_data", "app/user_data")
  update_variables
  update_stacks
  update_params

  puts "Upgrade to lono version 4 complete!"
end

#update_paramsObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/lono/upgrade/upgrade4.rb', line 74

def update_params
  Dir.glob("config/params/*").each do |path|
    next unless File.directory?(path)
    lono_env = File.basename(path)
    mapped_env = env_map(lono_env)
    if mapped_env != lono_env
      mv("config/params/#{lono_env}", "config/params/#{mapped_env}")
    end
  end
end

#update_s3_setting(data) ⇒ Object

accounts for most cases



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/lono/upgrade/upgrade4.rb', line 135

def update_s3_setting(data)
  return data unless data["s3"] && data["s3"]["path"]

  options = data["s3"]["path"]
  if options.is_a?(String)
    data.delete("s3")
    data["s3_folder"] = options
    return data # return early if String
  end

  # Reach here: dealing with a Hash
  if options.size == 1 and options["default"]
    data["s3_folder"] = options["default"]
  end

  if options.size > 1
    data["s3_folder"] = {}
    options.each do |key, value|
      data["s3_folder"][key] = value
    end
  end

  data.delete("s3")
  data
end

#update_stacksObject



32
33
34
# File 'lib/lono/upgrade/upgrade4.rb', line 32

def update_stacks
  update_structure("app/definitions")
end

#update_structure(component_path) ⇒ Object

Takes variable files in the subfolder like config/variables/production/* and combines the into a single file like config/variables/production.rb.

config/variables/base/* -> config/variables/base.rb
config/variables/development/* -> config/variables/development.rb
config/variables/production/* -> config/variables/production.rb
app/definitions/base/* -> app/definitions/base.rb
app/definitions/development/* -> app/definitions/development.rb
app/definitions/production/* -> app/definitions/production.rb


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lono/upgrade/upgrade4.rb', line 49

def update_structure(component_path)
  puts "Updating structure of #{component_path}"
  Dir.glob("#{component_path}/*").each do |path|
    next unless File.directory?(path)
    folder = File.basename(path)
    update_structure_for(component_path, folder)
  end

  # remove the old folders
  Dir.glob("#{component_path}/*").each do |path|
    next unless File.directory?(path)
    FileUtils.rm_rf(path)
  end
end

#update_structure_for(component_path, lono_env) ⇒ Object

combines the files in the lono_env subfolder into one file



65
66
67
68
69
70
71
72
# File 'lib/lono/upgrade/upgrade4.rb', line 65

def update_structure_for(component_path, lono_env)
  code = ""
  Dir.glob("#{component_path}/#{lono_env}/*.rb").each do |path|
    code << IO.read(path)
    code << "\n"
  end
  IO.write("#{component_path}/#{env_map(lono_env)}.rb", code)
end

#update_variablesObject



36
37
38
# File 'lib/lono/upgrade/upgrade4.rb', line 36

def update_variables
  update_structure("config/variables")
end

#upgrade_settings(path) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lono/upgrade/upgrade4.rb', line 108

def upgrade_settings(path)
  return unless File.exist?(path)

  data = YAML.load_file(path)
  return if data.key?("base") # already in new format

  new_structure = {}

  (data["aws_profile_lono_env_map"] || {}).each do |aws_profile, lono_env|
    new_structure[env_map(lono_env)] ||= {}
    new_structure[env_map(lono_env)]["aws_profiles"] ||= []
    new_structure[env_map(lono_env)]["aws_profiles"] << aws_profile
  end
  data.delete("aws_profile_lono_env_map")

  data = update_s3_setting(data)

  new_structure["base"] = data
  text = YAML.dump(new_structure)
  IO.write(path, text)
  puts "Upgraded settings: #{path}"
  if path.include?(ENV['HOME'])
    puts "NOTE: Your ~/.lono/settings.yml file was also upgraded to the new format. If you are using lono in other projects those will have to be upgraded also."
  end
end