Class: Ufo::Upgrade3

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/upgrade3.rb

Constant Summary collapse

ENV_MAP =
{
  "dev" => "development",
  "prod" => "production",
  "stag" => "staging",
}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Upgrade3

Returns a new instance of Upgrade3.



6
7
8
# File 'lib/ufo/upgrade3.rb', line 6

def initialize(options)
  @options = options
end

Instance Method Details

#map_env(ufo_env) ⇒ Object



68
69
70
# File 'lib/ufo/upgrade3.rb', line 68

def map_env(ufo_env)
  ENV_MAP[ufo_env] || ufo_env
end

#mv(src, dest) ⇒ Object



89
90
91
92
# File 'lib/ufo/upgrade3.rb', line 89

def mv(src, dest)
  puts "mv #{src} #{dest}"
  FileUtils.mv(src, dest)
end

#new_env_infoObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ufo/upgrade3.rb', line 72

def new_env_info
  puts <<-EOL.colorize(:yellow)
INFO: The UFO_ENV default environment is now development.
The short env names have been mapped over to their longer names.
Examples:

  prod => production
  dev => development

To adjust the default UFO_ENV, export it in your ~/.profile. Example:

export UFO_ENV=production # the default is now development, when not set

Refer to https://github.com/tongueroo/ufo/blob/master/CHANGELOG.md for other notable changes.
EOL
end

#runObject



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

def run
  if File.exist?("#{Ufo.root}/.ufo")
    puts "It looks like you already have a .ufo folder in your project. This is the new project structure so exiting without updating anything."
    return
  end

  if !File.exist?("#{Ufo.root}/ufo")
    puts "Could not find a ufo folder in your project. Maybe you want to run ufo init to initialize a new ufo project instead?"
    return
  end

  puts "Upgrading structure of your current project to the new ufo version 3 project structure"
  upgrade_settings("ufo/settings.yml")
   = "#{ENV['HOME']}/.ufo/settings.yml"
  if File.exist?()
    upgrade_settings()
  end
  mv("ufo", ".ufo")
  puts "Upgrade complete."
end

#upgrade_settings(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ufo/upgrade3.rb', line 31

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

  new_structure = {}

  (data["aws_profile_ufo_env_map"] || {}).each do |aws_profile, ufo_env|
    ufo_env = map_env(ufo_env)
    new_structure[ufo_env] ||= {}
    new_structure[ufo_env]["aws_profiles"] ||= []
    new_structure[ufo_env]["aws_profiles"] << aws_profile
  end
  data.delete("aws_profile_ufo_env_map")

  (data["ufo_env_cluster_map"] || {}).each do |ufo_env, cluster|
    ufo_env = map_env(ufo_env)
    new_structure[ufo_env] ||= {}
    new_structure[ufo_env]["cluster"] = cluster
  end
  data.delete("ufo_env_cluster_map")

  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 ~/.ufo/settings.yml file was also upgraded to the new format. If you are using ufo in other projects those will have to be upgraded also."
  end

  new_env_info
end