knife-sharp 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca5eb5868e371ab216bf8009f49fc1341b0884dd
4
- data.tar.gz: 4f2471fc8a99e9372dd459c9c0da29e7e5d9f862
3
+ metadata.gz: ddab4231fc3e426c9a71063a2a417b10b3cae947
4
+ data.tar.gz: eda6e7ed4b19017b5a386b2550b1cbaac6364a0e
5
5
  SHA512:
6
- metadata.gz: bdf6f7d68cfc0806231fedec65d4e074ea97bddbef433ee8f58ec9f180200c7337f8accf8e530919275a3d12f0f8dbb13672756faa8c482aeff8b82373f74be2
7
- data.tar.gz: 732dcc82d02814c8d7e4fba9d00b1aaa2dd2037fae214cbb5e36d1e91769369ecdad99ac512adbc96b005913bf9f9aa954b50b90cc805a9382512e282637104b
6
+ metadata.gz: 4070ebe2f93b5bbbef1fa1a28652b5988ab76d356cb6383bb20d9646dec145c13c6e8ceecdef8930e8b3581347f50ff66f3f435b7c449283e404f435e02f6a51
7
+ data.tar.gz: 53dd08f8ad6159fa06096a69f177e57d7da443df3ac607517be1ee6f495dab513eff969c573f114d72e3c351d04a77e0b1fafe55c4090876f8e101b2e1d2fe54
data/README.md CHANGED
@@ -72,7 +72,7 @@ $ knife sharp align ldap production
72
72
 
73
73
  Cookbooks, data_bags and roles are uploaded, and cookbook versions updated in given environment.
74
74
 
75
- To use all of these features, your knife.rb(s) must provide paths for cookbooks, data bags and roles (see [configuration](#Configuration))
75
+ To use all of these features, your knife.rb(s) must provide paths for cookbooks, data bags, roles and environments (see [configuration](#Configuration))
76
76
 
77
77
  ### Ignore list
78
78
 
@@ -188,6 +188,7 @@ Fully enabled Sharp needs:
188
188
  cookbook_path '/home/jamiez/chef/cookbooks'
189
189
  data_bag_path '/home/jamiez/chef/data_bags'
190
190
  role_path '/home/jamiez/chef/roles'
191
+ environment_path '/home/jamiez/chef/environments'
191
192
  ```
192
193
  in knife.rb
193
194
 
@@ -8,11 +8,14 @@ global:
8
8
  # backup dir for knife sharp backup
9
9
  backupdir: "/home/nico/.chef/sharp/backups/"
10
10
 
11
- # Currently only hubot notifications supported
11
+ # Currently only bot notifications supported
12
+ # notification operation:
13
+ # POST <url>/<channel>
14
+ # with JSON payload {"message": <message>}
12
15
  notification:
13
- hubot:
16
+ bot:
14
17
  enabled: true
15
- url: "http://your.hubot.lan:18080/hubot/chef/bump/"
18
+ url: "http://bot.lan:18080/chef/bump/"
16
19
  channel: "foo"
17
20
  username: "Nicolas"
18
21
  # don't notify if updates are done on development chef
@@ -8,13 +8,29 @@ module KnifeSharp
8
8
 
9
9
  banner "knife sharp align BRANCH ENVIRONMENT [OPTS]"
10
10
 
11
- [:cookbooks, :databags, :roles].each do |opt|
12
- option opt,
13
- :short => "-#{opt.to_s[0,1].upcase}",
14
- :long => "--#{opt}-only",
15
- :description => "sync #{opt} only",
16
- :default => false
17
- end
11
+ option :cookbooks,
12
+ :short => "-C",
13
+ :long => "--cookbooks-only",
14
+ :description => "sync cookbooks only",
15
+ :default => false
16
+
17
+ option :roles,
18
+ :short => "-R",
19
+ :long => "--roles-only",
20
+ :description => "sync roles only",
21
+ :default => false
22
+
23
+ option :databags,
24
+ :short => "-D",
25
+ :long => "--databags-only",
26
+ :description => "sync data bags only",
27
+ :default => false
28
+
29
+ option :environments,
30
+ :short => "-N",
31
+ :long => "--environments-only",
32
+ :description => "sync environments only",
33
+ :default => false
18
34
 
19
35
  option :force_align,
20
36
  :short => "-f",
@@ -36,10 +52,10 @@ module KnifeSharp
36
52
  ensure_correct_branch_provided!
37
53
 
38
54
  # check cli flags
39
- if config[:cookbooks] or config[:databags] or config[:roles]
40
- do_cookbooks, do_databags, do_roles = config[:cookbooks], config[:databags], config[:roles]
55
+ if config[:cookbooks] or config[:databags] or config[:roles] or config[:environments]
56
+ do_cookbooks, do_databags, do_roles, do_environments = config[:cookbooks], config[:databags], config[:roles], config[:environments]
41
57
  else
42
- do_cookbooks, do_databags, do_roles = true, true, true
58
+ do_cookbooks, do_databags, do_roles, do_environments = true, true, true, true
43
59
  end
44
60
 
45
61
  ui.msg(ui.color("On server #{chef_server}", :bold)) if chef_server
@@ -53,9 +69,12 @@ module KnifeSharp
53
69
  SharpRoleAlign.load_deps
54
70
  sra = SharpRoleAlign.new
55
71
  roles_to_update = do_roles ? sra.check_roles : {}
72
+ SharpEnvironmentAlign.load_deps
73
+ sea = SharpEnvironmentAlign.new
74
+ environments_to_update = do_environments ? sea.check_environments : {}
56
75
 
57
76
  # All questions asked, can we proceed ?
58
- if cookbooks_to_update.empty? and databags_to_update.empty? and roles_to_update.empty?
77
+ if cookbooks_to_update.empty? and databags_to_update.empty? and roles_to_update.empty? and environments_to_update.empty?
59
78
  ui.msg "Nothing else to do"
60
79
  exit 0
61
80
  end
@@ -64,6 +83,7 @@ module KnifeSharp
64
83
  sca.bump_cookbooks(environment, cookbooks_to_update) if do_cookbooks
65
84
  sda.update_databags(databags_to_update) if do_databags
66
85
  sra.update_roles(roles_to_update) if do_roles
86
+ sea.update_environments(environments_to_update) if do_environments
67
87
  end
68
88
  end
69
89
  end
@@ -0,0 +1,133 @@
1
+ require 'chef/knife'
2
+ require 'knife-sharp/common'
3
+
4
+ module KnifeSharp
5
+ class SharpEnvironmentAlign < Chef::Knife
6
+ include KnifeSharp::Common
7
+
8
+ banner "knife sharp environment align BRANCH [OPTS]"
9
+
10
+ deps do
11
+ require "chef/environment"
12
+ end
13
+
14
+ def run
15
+ # Checking args
16
+ ensure_branch_provided!
17
+
18
+ # Checking repo branch
19
+ ensure_correct_branch_provided!
20
+
21
+ ui.msg(ui.color("On server #{chef_server}", :bold)) if chef_server
22
+
23
+ to_update = check_environments
24
+
25
+ if to_update.empty?
26
+ ui.msg "Nothing else to do"
27
+ exit 0
28
+ end
29
+
30
+ ui.confirm(ui.color("> Proceed ", :bold))
31
+
32
+ update_environments(to_update)
33
+ end
34
+
35
+ def check_environments
36
+ not_up_to_date = Array.new
37
+ to_update = Array.new
38
+
39
+ unless File.exists?(environment_path)
40
+ ui.warn("Bad environment path, skipping environment sync.")
41
+ return to_update
42
+ end
43
+
44
+ ui.msg(ui.color("== Environments ==", :bold))
45
+
46
+ local_envs = Dir.glob(File.join(environment_path, "*.json")).map {|file| File.basename(file, ".json")}
47
+ remote_envs = Chef::Environment.list.keys
48
+
49
+ if local_envs.empty?
50
+ ui.warn("No local environment found, is the environment path correct ? (#{environment_path})")
51
+ return to_update
52
+ end
53
+
54
+ # Create new environments on server
55
+ (local_envs - remote_envs).each do |env|
56
+ local_env = Chef::Environment.load_from_file(env)
57
+ message = "* #{local_env.name} environment is local only"
58
+ if ignore_list(:environments).include(local_env.name)
59
+ message += " (ignored)"
60
+ else
61
+ not_up_to_date << local_env
62
+ end
63
+ ui.msg(message)
64
+ end
65
+
66
+ # Compare envs common to local and remote
67
+ (remote_envs & local_envs).each do |env|
68
+ remote_env = Chef::Environment.load(env)
69
+ local_env = Chef::Environment.load_from_file(env)
70
+
71
+ diffs = relevant_env_keys.map do |method, display|
72
+ if remote_env.send(method) != local_env.send(method)
73
+ remote_env.send("#{method}=", local_env.send(method))
74
+ display
75
+ end
76
+ end.compact
77
+
78
+ unless diffs.empty?
79
+ message = "* #{remote_env.name} environment is not up-to-date (#{diffs.join(", ")})"
80
+ if ignore_list(:environments).include?(remote_env.name)
81
+ message += " (ignored)"
82
+ else
83
+ not_up_to_date << remote_env
84
+ end
85
+ ui.msg(message)
86
+ end
87
+ end
88
+
89
+ if !not_up_to_date.empty?
90
+ all = false
91
+ not_up_to_date.each do |env|
92
+ answer = all ? "Y" : ui.ask_question("> Update #{env.name} environment on server ? Y/N/(A)ll/(Q)uit ", :default => "N").upcase
93
+
94
+ if answer == "A"
95
+ all = true
96
+ elsif answer == "Q"
97
+ ui.msg "* Aborting environment alignment."
98
+ break
99
+ end
100
+
101
+ if all or answer == "Y"
102
+ to_update << env
103
+ else
104
+ ui.msg "* Skipping #{env.name} environment"
105
+ end
106
+ end
107
+ else
108
+ ui.msg "* Environments are up-to-date."
109
+ end
110
+
111
+ to_update
112
+ end
113
+
114
+ def update_environments(env_list)
115
+ env_list.each do |env|
116
+ begin
117
+ env.save
118
+ ui.msg("* Updating #{env.name} environment")
119
+ rescue Exception => e
120
+ ui.error("Unable to update #{env.name} environment (#{e.message})")
121
+ end
122
+ end
123
+ end
124
+
125
+ def relevant_env_keys
126
+ # env sections to compare (methods)
127
+ {
128
+ "default_attributes" => "default attributes",
129
+ "override_attributes" => "override attributes"
130
+ }
131
+ end
132
+ end
133
+ end
@@ -1,3 +1,3 @@
1
1
  module KnifeSharp
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -38,6 +38,13 @@ module KnifeSharp
38
38
  end
39
39
  end
40
40
 
41
+ def ensure_branch_provided!
42
+ if @name_args.size != 1
43
+ show_usage
44
+ exit 1
45
+ end
46
+ end
47
+
41
48
  def environment
42
49
  @environment ||= @name_args.last
43
50
  end
@@ -54,6 +61,10 @@ module KnifeSharp
54
61
  @role_path ||= [Chef::Config.send(:role_path)].flatten.first
55
62
  end
56
63
 
64
+ def environment_path
65
+ @environment_path ||= [Chef::Config.send(:environment_path)].flatten.first
66
+ end
67
+
57
68
  def logger
58
69
  return @logger unless @logger.nil?
59
70
 
@@ -91,7 +102,7 @@ module KnifeSharp
91
102
  end
92
103
  end
93
104
 
94
- def hubot(message, config={})
105
+ def bot(message, config={})
95
106
  begin
96
107
  require "net/http"
97
108
  require "uri"
@@ -99,7 +110,15 @@ module KnifeSharp
99
110
  notif = "chef: #{message} by #{config["username"]}"
100
111
  Net::HTTP.post_form(uri, { "message" => notif })
101
112
  rescue
102
- ui.error "Unable to notify via hubot."
113
+ ui.error "Unable to notify via bot."
114
+ end
115
+ end
116
+
117
+ def ignore_list(component)
118
+ if sharp_config[chef_server] and sharp_config[chef_server]["ignore_#{component}"]
119
+ sharp_config[chef_server]["ignore_#{component}"]
120
+ else
121
+ []
103
122
  end
104
123
  end
105
124
  end
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.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Szalay
@@ -58,6 +58,7 @@ files:
58
58
  - lib/chef/knife/sharp-backup.rb
59
59
  - lib/chef/knife/sharp-cookbook-align.rb
60
60
  - lib/chef/knife/sharp-data_bag-align.rb
61
+ - lib/chef/knife/sharp-environment-align.rb
61
62
  - lib/chef/knife/sharp-history.rb
62
63
  - lib/chef/knife/sharp-role-align.rb
63
64
  - lib/chef/knife/sharp-rollback.rb