Class: SSP::App::Chef

Inherits:
Thor
  • Object
show all
Defined in:
lib/ssp/application/chef.rb

Instance Method Summary collapse

Instance Method Details

#client(*nodes) ⇒ Object



20
21
22
23
24
# File 'lib/ssp/application/chef.rb', line 20

def client(*nodes)
  chef_config
  command = options[:ssh_user] == "root" ? "chef-client" : "sudo chef-client"
  ssh_run nodes.join(" "), command
end

#deploy(app) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
# File 'lib/ssp/application/chef.rb', line 33

def deploy(app)
  if options[:ssh_user] == "root"
    raise Thor::Error, "You cannot deploy directly with the root user, use the -u option to specify your user"
  end
  chef_config
  node = options[:node] || (options[:production] ? "#{app}.sspti.me" : "ginger.sspti.me")
  debug_log = "/tmp/#{app}_deploy_chef_run.#{Time.now.strftime("%Y%m%d%H%M")}.log"
  command = "sudo chef-client -V -l debug -L #{debug_log} | grep --line-buffered '\\[[^]]*\\] [A-Z]*:' | grep -v DEBUG:"

  env = options[:production] ? "production" : "staging"
  bag_override = { :migrate => options[:migrate] }
  case rev = options[:revision] || ("1" if options[:rollback])
  when *%w(1 2 3 4)
    all_releases = JSON.parse(%x{ssh #{options[:ssh_user]}@#{node} cat /srv/chef/cache/revision-deploys/#{app}}) rescue []
    all_releases.map! {|r| File.basename(r)}
    bag_override[:revision] = all_releases[-rev.to_i-1]
    raise Thor::Error, "Couldn't find revision #{options[:revision]}" unless bag_override[:revision]
  when String
    bag_override[:revision] = options[:revision]
  end
  bag_override[:deploy_action] =
    if options[:rollback]
      "rollback"
    elsif options[:force]
      "force_deploy"
    else
      "deploy"
    end

  with_updated_bag(app, env, bag_override) do |bag|
    say <<-EOS.gsub(/\s+/, ' ').strip
      #{bag_override[:deploy_action].tr('_', ' ').capitalize}ing
      #{shell.set_color(app, :green)}
      #{'to' if options[:rollback]} revision
      #{shell.set_color(bag_override[:revision] || bag['revision'][env], :yellow)}
      as #{shell.set_color(options[:ssh_user], :blue)}
      on #{shell.set_color(node, :cyan)}
    EOS

    ssh_run(node, command)
  end

ensure
  say "Debug log of Chef run written to #{node}:#{debug_log}"
end