knife-sharp 0.4.5 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
+ require 'knife-sharp/common'
2
+
1
3
  module KnifeSharp
2
4
  class SharpRollback < Chef::Knife
5
+ include KnifeSharp::Common
3
6
 
4
7
  banner "knife sharp rollback [--list] [--to <identifier>] [--show <identifier>]"
5
8
 
@@ -22,8 +25,8 @@ module KnifeSharp
22
25
  :default => false
23
26
 
24
27
  deps do
28
+ require 'chef/environment'
25
29
  require 'chef/cookbook/metadata'
26
- require 'chef/cookbook_loader'
27
30
  end
28
31
 
29
32
  def run()
@@ -60,39 +63,13 @@ module KnifeSharp
60
63
  exit 1
61
64
  end
62
65
 
63
-
64
- # Sharp config
65
- cfg_files = [ "/etc/sharp-config.yml", "~/.chef/sharp-config.yml" ]
66
- loaded = false
67
- cfg_files.each do |cfg_file|
68
- begin
69
- @cfg = YAML::load_file(File.expand_path(cfg_file))
70
- loaded = true
71
- rescue Exception => e
72
- ui.error "Error on loading config : #{e.inspect}" if config[:verbosity] > 0
73
- end
74
- end
75
- unless loaded == true
76
- ui.error "config could not be loaded ! Tried the following files : #{cfg_files.join(", ")}"
77
- exit 1
78
- end
79
-
80
- # Knife config
81
- if Chef::Knife.chef_config_dir && File.exists?(File.join(Chef::Knife.chef_config_dir, "knife.rb"))
82
- Chef::Config.from_file(File.join(Chef::Knife.chef_config_dir, "knife.rb"))
83
- else
84
- ui.error "Cannot find knife.rb config file"
85
- exit 1
86
- end
87
-
88
66
  chefcfg = Chef::Config
89
67
  @cb_path = chefcfg.cookbook_path.is_a?(Array) ? chefcfg.cookbook_path.first : chefcfg.cookbook_path
90
- @loader = Chef::CookbookLoader.new(@cb_path)
91
68
  end
92
69
 
93
70
  def list_rollback_points()
94
71
  ui.msg("Available rollback points :")
95
- Dir.glob(File.join(@cfg["rollback"]["destination"], "*.json")).each do |f|
72
+ Dir.glob(File.join(sharp_config["rollback"]["destination"], "*.json")).each do |f|
96
73
  ts = File.basename(f, ".json")
97
74
  ui.msg(" * #{ts} (#{Time.at(ts.to_i).to_s})")
98
75
  end
@@ -108,7 +85,7 @@ module KnifeSharp
108
85
  end
109
86
 
110
87
  begin
111
- fp = File.open(File.join(@cfg["rollback"]["destination"],"#{identifier}.json"),"r")
88
+ fp = File.open(File.join(sharp_config["rollback"]["destination"],"#{identifier}.json"),"r")
112
89
  infos = JSON.load(fp)
113
90
  rescue
114
91
  ui.error("could not load rollback point #{identifier}")
@@ -125,7 +102,7 @@ module KnifeSharp
125
102
 
126
103
  def show_rollback_point(identifier)
127
104
  begin
128
- fp = File.open(File.join(@cfg["rollback"]["destination"],"#{identifier}.json"),"r")
105
+ fp = File.open(File.join(sharp_config["rollback"]["destination"],"#{identifier}.json"),"r")
129
106
  rescue
130
107
  ui.error("could not load rollback point #{identifier}")
131
108
  exit 1
data/lib/knife-sharp.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module KnifeSharp
2
- VERSION = "0.4.5"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -0,0 +1,113 @@
1
+ require 'logger'
2
+
3
+ module KnifeSharp
4
+ module Common
5
+ module ClassMethods; end
6
+
7
+ module InstanceMethods
8
+ def sharp_config
9
+ return @sharp_config unless @sharp_config.nil?
10
+
11
+ config_file = File.expand_path("~/.chef/sharp-config.yml")
12
+
13
+ begin
14
+ @sharp_config = YAML::load_file(config_file)
15
+ rescue Exception => e
16
+ ui.error "Failed to load config file #{config_file}: #{e.message}"
17
+ exit 1
18
+ end
19
+
20
+ @sharp_config
21
+ end
22
+
23
+ def ensure_correct_branch_provided!
24
+ # Checking current branch
25
+ given_branch = @name_args.first
26
+ current_branch = Grit::Repo.new(sharp_config["global"]["git_cookbook_path"]).head.name
27
+
28
+ if given_branch != current_branch then
29
+ ui.error "Git repo is actually on branch #{current_branch} but you want to align using #{given_branch}. Checkout to the desired one."
30
+ exit 1
31
+ end
32
+ end
33
+
34
+ def ensure_branch_and_environment_provided!
35
+ if @name_args.size != 2
36
+ show_usage
37
+ exit 1
38
+ end
39
+ end
40
+
41
+ def environment
42
+ @environment ||= @name_args.last
43
+ end
44
+
45
+ def cookbook_path
46
+ @cookbook_path ||= [Chef::Config.send(:cookbook_path)].flatten.first
47
+ end
48
+
49
+ def data_bag_path
50
+ @data_bag_path ||= [Chef::Config.send(:data_bag_path)].flatten.first
51
+ end
52
+
53
+ def role_path
54
+ @role_path ||= [Chef::Config.send(:role_path)].flatten.first
55
+ end
56
+
57
+ def logger
58
+ return @logger unless @logger.nil?
59
+
60
+ begin
61
+ log_file = sharp_config["logging"]["destination"] || "~/.chef/sharp.log"
62
+ @logger = Logger.new(File.expand_path(log_file))
63
+ rescue Exception => e
64
+ ui.error "Unable to set up logger (#{e.inspect})."
65
+ exit 1
66
+ end
67
+
68
+ @logger
69
+ end
70
+
71
+ def chef_server
72
+ @chef_server ||= SharpServer.new.current_server
73
+ end
74
+
75
+ def log_action(message)
76
+ # log file if enabled
77
+ log_message = message
78
+ log_message += " on server #{chef_server}" if chef_server
79
+ logger.info(log_message) if sharp_config["logging"]["enabled"]
80
+
81
+ # any defined notification method (currently, only hubot, defined below)
82
+ if sharp_config["notification"]
83
+ sharp_config["notification"].each do |carrier, data|
84
+ skipped = Array.new
85
+ skipped = data["skip"] if data["skip"]
86
+
87
+ if data["enabled"] and !skipped.include?(chef_server)
88
+ send(carrier, message, data)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ def hubot(message, config={})
95
+ begin
96
+ require "net/http"
97
+ require "uri"
98
+ uri = URI.parse("#{config["url"]}/#{config["channel"]}")
99
+ notif = "chef: #{message} by #{config["username"]}"
100
+ Net::HTTP.post_form(uri, { "message" => notif })
101
+ rescue
102
+ ui.error "Unable to notify via hubot."
103
+ end
104
+ end
105
+ end
106
+
107
+ def self.included(receiver)
108
+ receiver.extend(ClassMethods)
109
+ receiver.send(:include, InstanceMethods)
110
+ end
111
+ end
112
+ end
113
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-sharp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Szalay
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-23 00:00:00.000000000 Z
12
+ date: 2015-04-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
@@ -17,28 +17,28 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 10.14.0
20
+ version: '11'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 10.14.0
27
+ version: '11'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: grit
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 2.5.0
34
+ version: '2.5'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 2.5.0
41
+ version: '2.5'
42
42
  description: Sharpen your knife
43
43
  email:
44
44
  - nico@rottenbytes.info
@@ -47,14 +47,23 @@ executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - Gemfile.lock
50
53
  - README.md
54
+ - Rakefile
51
55
  - ext/sharp-config.yml
56
+ - knife-sharp.gemspec
52
57
  - lib/chef/knife/sharp-align.rb
53
58
  - lib/chef/knife/sharp-backup.rb
59
+ - lib/chef/knife/sharp-cookbook-align.rb
60
+ - lib/chef/knife/sharp-data_bag-align.rb
54
61
  - lib/chef/knife/sharp-history.rb
62
+ - lib/chef/knife/sharp-role-align.rb
55
63
  - lib/chef/knife/sharp-rollback.rb
56
64
  - lib/chef/knife/sharp-server.rb
57
65
  - lib/knife-sharp.rb
66
+ - lib/knife-sharp/common.rb
58
67
  homepage: https://github.com/Fotolia/knife-sharp
59
68
  licenses:
60
69
  - 3-BSD
@@ -75,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
84
  version: '0'
76
85
  requirements: []
77
86
  rubyforge_project:
78
- rubygems_version: 2.2.2
87
+ rubygems_version: 2.4.5
79
88
  signing_key:
80
89
  specification_version: 4
81
90
  summary: Knife sharp plugin