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



95
96
97
# File 'lib/ufo/upgrade3.rb', line 95

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

#mv(src, dest) ⇒ Object



116
117
118
119
# File 'lib/ufo/upgrade3.rb', line 116

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

#new_env_infoObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ufo/upgrade3.rb', line 99

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
30
31
32
33
34
# 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

  upgrade_variables
  upgrade_gitignore

  mv("ufo", ".ufo")
  puts "Upgrade complete."
  new_env_info
end

#upgrade_gitignoreObject



36
37
38
39
40
41
42
43
44
# File 'lib/ufo/upgrade3.rb', line 36

def upgrade_gitignore
  lines = IO.readlines(".gitignore")
  lines.map! do |line|
    line.sub(/[\/]?ufo/, '.ufo')
  end
  lines << [".ufo/data\n"] # new ignore rule
  text = lines.join
  IO.write(".gitignore", text)
end

#upgrade_settings(path) ⇒ Object



60
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
87
88
# File 'lib/ufo/upgrade3.rb', line 60

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
end

#upgrade_variable_path(old_ufo_env) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ufo/upgrade3.rb', line 52

def upgrade_variable_path(old_ufo_env)
  old_path = "ufo/variables/#{old_ufo_env}.rb"
  return unless File.exist?(old_path)

  ufo_env = map_env(old_ufo_env)
  mv(old_path, "ufo/variables/#{ufo_env}.rb")
end

#upgrade_variablesObject



46
47
48
49
50
# File 'lib/ufo/upgrade3.rb', line 46

def upgrade_variables
  upgrade_variable_path("dev")
  upgrade_variable_path("stag")
  upgrade_variable_path("prod")
end