Class: KnifeSharp::SharpRollback

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

Instance Method Summary collapse

Methods included from Common

included

Instance Method Details

#list_rollback_pointsObject



70
71
72
73
74
75
76
77
# File 'lib/chef/knife/sharp-rollback.rb', line 70

def list_rollback_points()
  ui.msg("Available rollback points :")
  Dir.glob(File.join(sharp_config["rollback"]["destination"], "*.json")).each do |f|
    ts = File.basename(f, ".json")
    ui.msg("  * #{ts} (#{Time.at(ts.to_i).to_s})")
  end
  exit 0
end

#rollback_to(identifier) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/knife/sharp-rollback.rb', line 79

def rollback_to(identifier)
  show_rollback_point(identifier)
  answer = ui.ask_question("Continue rollback ? Y/(N) ", :default => "N").upcase
  if answer != "Y"
    ui.msg("Aborting !")
    exit 0
  end

  begin
    fp = File.open(File.join(sharp_config["rollback"]["destination"],"#{identifier}.json"),"r")
    infos = JSON.load(fp)
  rescue
    ui.error("could not load rollback point #{identifier}")
    exit 1
  end

  env = Chef::Environment.load(infos["environment"])
  infos["cookbook_versions"].each do |cb, version|
      env.cookbook_versions[cb] = version
      ui.msg("Setting #{cb} to version #{version}")
  end
  env.save
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef/knife/sharp-rollback.rb', line 32

def run()
  setup()

  if config[:list]
    list_rollback_points()
    exit 0
  end

  if config[:show]
    identifier = name_args
    show_rollback_point(identifier)
    exit 0
  end

  if config[:to]
    identifier = name_args
    rollback_to(identifier)
    exit 0
  end
end

#setupObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/knife/sharp-rollback.rb', line 53

def setup()
  actions = 0
  [:to, :list, :show].each do |action|
    if config[action]
      actions+=1
    end
  end

  if actions > 1
    ui.error("please specify only one action")
    exit 1
  end

  chefcfg = Chef::Config
  @cb_path = chefcfg.cookbook_path.is_a?(Array) ? chefcfg.cookbook_path.first : chefcfg.cookbook_path
end

#show_rollback_point(identifier) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/knife/sharp-rollback.rb', line 103

def show_rollback_point(identifier)
  begin
    fp = File.open(File.join(sharp_config["rollback"]["destination"],"#{identifier}.json"),"r")
  rescue
    ui.error("could not load rollback point #{identifier}")
    exit 1
  end
  infos = JSON.load(fp)
  ui.msg("Rollback point has the following informations :")
  ui.msg("  environment : #{infos["environment"]}")
  ui.msg("  cookbooks versions :")
  infos["cookbook_versions"].each do |cb, version|
    ui.msg("   * #{cb} => #{version}")
  end
end