ssp 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require 'chef/config'
2
2
  require 'chef/search/query'
3
3
  require 'chef/node'
4
+ require 'chef/data_bag'
4
5
 
5
6
  class SSP::App::Chef < Thor
6
7
  namespace :chef
@@ -9,19 +10,37 @@ class SSP::App::Chef < Thor
9
10
  :desc => "Location of the knife configuration file",
10
11
  :default => File.join(ENV['HOME'], '.chef', 'knife.rb')
11
12
 
13
+ class_option :ssh_user, :aliases => "-u",
14
+ :desc => "The ssh username",
15
+ :default => "root"
16
+ class_option :ssh_password, :aliases => "-p",
17
+ :desc => "The ssh password"
12
18
 
13
19
  desc "client [NODES...]", "Runs chef-client on multiple nodes"
14
- method_option :ssh_user, :aliases => "-u",
15
- :desc => "The ssh username",
16
- :default => "root"
17
- method_option :ssh_password, :aliases => "-p",
18
- :desc => "The ssh password"
19
20
  def client(*nodes)
20
21
  chef_config
21
22
  command = options[:ssh_user] == "root" ? "chef-client" : "sudo chef-client"
22
23
  ssh_run nodes.join(" "), command
23
24
  end
24
25
 
26
+ desc "deploy APP", "Runs chef-client with deployment enabled for the application on the right node"
27
+ method_options %w( production -P ) => false, %w( migrate -m ) => true,
28
+ %w( rollback -r ) => false, %w( force -f ) => false
29
+ def deploy(app)
30
+ if options[:ssh_user] == "root"
31
+ raise Thor::Error, "You cannot deploy directly with the root user, use the -u option to specify your user"
32
+ end
33
+ chef_config
34
+ node = "#{options[:production] ? "#{app}-app-01" : "sage"}.sspti.me"
35
+ command = "sudo chef-client -V -l debug -L /tmp/#{app}_deploy_chef_run.#{Time.now.strftime("%Y%m%d%H%M")}.log | grep '\\[[^]]*\\] [A-Z]*:' | grep -v DEBUG:"
36
+
37
+ env = options[:production] ? "production" : "staging"
38
+ deploy_action = options[:rollback] ? "rollback" : options[:force] ? "force_deploy" : "deploy"
39
+ with_updated_bag(app, env, :deploy_action => deploy_action, :migrate => options[:migrate]) do
40
+ ssh_run(node, command)
41
+ end
42
+ end
43
+
25
44
  private
26
45
  def ssh_run(host, command, user = options[:ssh_user], pass = options[:ssh_password], manual = !host.index(':'))
27
46
  ssh = ::Chef::Knife::Ssh.new
@@ -52,4 +71,26 @@ class SSP::App::Chef < Thor
52
71
  end
53
72
  ::Chef::Config
54
73
  end
74
+
75
+ def with_updated_bag(app, env, attributes={})
76
+ bag = Chef::DataBagItem.new
77
+ bag.data_bag("apps")
78
+ bag.raw_data = Chef::DataBagItem.load("apps", app)
79
+
80
+ original_values = {}
81
+ attributes.each do |key, value|
82
+ key = key.to_s
83
+ bag.raw_data[key] ||= {}
84
+ original_values[key] = bag.raw_data[key].dup
85
+ bag.raw_data[key][env] = value
86
+ end
87
+ bag.save
88
+
89
+ yield
90
+
91
+ attributes.each do |key, value|
92
+ bag.raw_data[key.to_s] = original_values[key.to_s]
93
+ end
94
+ bag.save
95
+ end
55
96
  end
@@ -1,3 +1,3 @@
1
1
  module SSP
2
- VERSION = "0.5"
2
+ VERSION = "0.6"
3
3
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
8
- version: "0.5"
7
+ - 6
8
+ version: "0.6"
9
9
  platform: ruby
10
10
  authors:
11
11
  - "L\xC3\xA1szl\xC3\xB3 B\xC3\xA1csi"
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-10-14 00:00:00 +02:00
16
+ date: 2010-10-21 00:00:00 +02:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency