Class: KnifeSharp::SharpEnvironmentAlign

Inherits:
Chef::Knife
  • Object
show all
Includes:
Common
Defined in:
lib/chef/knife/sharp-environment-align.rb

Instance Method Summary collapse

Methods included from Common

included

Instance Method Details

#check_environmentsObject



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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/knife/sharp-environment-align.rb', line 35

def check_environments
  not_up_to_date = Array.new
  to_update = Array.new

  unless File.exists?(environment_path)
    ui.warn("Bad environment path, skipping environment sync.")
    return to_update
  end

  ui.msg(ui.color("== Environments ==", :bold))

  local_envs = Dir.glob(File.join(environment_path, "*.json")).map {|file| File.basename(file, ".json")}
  remote_envs = Chef::Environment.list.keys

  if local_envs.empty?
    ui.warn("No local environment found, is the environment path correct ? (#{environment_path})")
    return to_update
  end

  # Create new environments on server
  (local_envs - remote_envs).each do |env|
    local_env = Chef::Environment.load_from_file(env)
    message = "* #{local_env.name} environment is local only"
    if ignore_list(:environments).include?(local_env.name)
      message += " (ignored)"
    else
      not_up_to_date << local_env
    end
    ui.msg(message)
  end

  # Compare envs common to local and remote
  (remote_envs & local_envs).each do |env|
    remote_env = Chef::Environment.load(env)
    local_env = Chef::Environment.load_from_file(env)

    diffs = relevant_env_keys.map do |method, display|
      if remote_env.send(method) != local_env.send(method)
        remote_env.send("#{method}=", local_env.send(method))
        display
      end
    end.compact

    unless diffs.empty?
      message = "* #{remote_env.name} environment is not up-to-date (#{diffs.join(", ")})"
      if ignore_list(:environments).include?(remote_env.name)
        message += " (ignored)"
      else
        not_up_to_date << remote_env
      end
      ui.msg(message)
    end
  end

  if !not_up_to_date.empty?
    all = false
    not_up_to_date.each do |env|
      answer = all ? "Y" : ui.ask_question("> Update #{env.name} environment on server ? Y/N/(A)ll/(Q)uit ", :default => "N").upcase

      if answer == "A"
        all = true
      elsif answer == "Q"
        ui.msg "* Aborting environment alignment."
        break
      end

      if all or answer == "Y"
        to_update << env
      else
        ui.msg "* Skipping #{env.name} environment"
      end
    end
  else
    ui.msg "* Environments are up-to-date."
  end

  to_update
end

#relevant_env_keysObject



125
126
127
128
129
130
131
# File 'lib/chef/knife/sharp-environment-align.rb', line 125

def relevant_env_keys
  # env sections to compare (methods)
  {
    "default_attributes" => "default attributes",
    "override_attributes" => "override attributes"
  }
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chef/knife/sharp-environment-align.rb', line 14

def run
  # Checking args
  ensure_branch_provided!

  # Checking repo branch
  ensure_correct_branch_provided!

  ui.msg(ui.color("On server #{chef_server}", :bold)) if chef_server

  to_update = check_environments

  if to_update.empty?
    ui.msg "Nothing else to do"
    exit 0
  end

  ui.confirm(ui.color("> Proceed ", :bold))

  update_environments(to_update)
end

#update_environments(env_list) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/knife/sharp-environment-align.rb', line 114

def update_environments(env_list)
  env_list.each do |env|
    begin
      env.save
      ui.msg("* Updating #{env.name} environment")
    rescue Exception => e
      ui.error("Unable to update #{env.name} environment (#{e.message})")
    end
  end
end