Class: KnifeSharp::SharpRollback

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

Instance Method Summary collapse

Instance Method Details

#list_rollback_pointsObject



94
95
96
97
98
99
100
101
# File 'lib/chef/knife/sharp-rollback.rb', line 94

def list_rollback_points()
  ui.msg("Available rollback points :")
  Dir.glob(File.join(@cfg["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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chef/knife/sharp-rollback.rb', line 103

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(@cfg["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



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

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



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
# File 'lib/chef/knife/sharp-rollback.rb', line 51

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


  # Sharp config
  cfg_files = [ "/etc/sharp-config.yml", "~/.chef/sharp-config.yml" ]
  loaded = false
  cfg_files.each do |cfg_file|
    begin
      @cfg = YAML::load_file(File.expand_path(cfg_file))
      loaded = true
    rescue Exception => e
      ui.error "Error on loading config : #{e.inspect}" if config[:verbosity] > 0
    end
  end
  unless loaded == true
    ui.error "config could not be loaded ! Tried the following files : #{cfg_files.join(", ")}"
    exit 1
  end

  # Knife config
  if Chef::Knife.chef_config_dir && File.exists?(File.join(Chef::Knife.chef_config_dir, "knife.rb"))
    Chef::Config.from_file(File.join(Chef::Knife.chef_config_dir, "knife.rb"))
  else
    ui.error "Cannot find knife.rb config file"
    exit 1
  end

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

#show_rollback_point(identifier) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/knife/sharp-rollback.rb', line 127

def show_rollback_point(identifier)
  begin
    fp = File.open(File.join(@cfg["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