Class: Baker

Inherits:
Object
  • Object
show all
Defined in:
lib/cli.rb,
lib/baker.rb

Defined Under Namespace

Classes: CLI, NotCookbookProject

Constant Summary collapse

Version =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Baker

Returns a new instance of Baker.



24
25
26
27
28
29
# File 'lib/baker.rb', line 24

def initialize(options)
  @user   = nil
  @host   = options[:host] || raise("need to set host")
  @root   = Dir.pwd
  @logger = Logger.new(File.exist?("#{@root}/log") ? "#{@root}/log/baker.log" : "#{@root}/baker.log")
end

Class Method Details

.run(options) ⇒ Object



19
20
21
22
# File 'lib/baker.rb', line 19

def self.run(options)
  @baker = Baker.new(options)
  @baker.run
end

.setup(options) ⇒ Object



15
16
17
18
# File 'lib/baker.rb', line 15

def self.setup(options)
  @baker = Baker.new(options)
  @baker.setup
end

Instance Method Details

#runObject



45
46
47
48
49
50
51
52
53
# File 'lib/baker.rb', line 45

def run
  validate_cookbook_project
  log "*** start running chef recipes on #{@host}"
  Net::SSH.start(@host, @user) do |ssh|
    upload_recipes(ssh)
    run_chef(ssh)
  end
  log "*** done running chef recipes, check /var/log/baker.chef.log on #{@host}"
end

#run_chef(ssh) ⇒ Object



83
84
85
86
87
88
# File 'lib/baker.rb', line 83

def run_chef(ssh)
  log "*** running chef recipes on #{@host}..."
  chef_cmd = "chef-solo -c /tmp/baker/recipes/config/solo.rb -j /tmp/baker/recipes/config/node.json > /var/log/baker.chef.log 2>&1"
  log "CHEF_CMD : #{chef_cmd}"
  remote_cmd(ssh, chef_cmd)
end

#setupObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/baker.rb', line 31

def setup
  log "*** setting up chef"
  Net::SFTP.start(@host, @user) do |sftp|
    sftp.upload!(
      File.expand_path("../../scripts/baker_setup.sh", __FILE__), 
      "/tmp/baker_setup.sh"
    )
  end
  Net::SSH.start(@host, @user) do |ssh|
    remote_cmd(ssh, "bash -ex /tmp/baker_setup.sh >> /var/log/baker.setup.log 2>&1;") # 
  end
  log "*** done setting up chef, check /var/log/baker.setup.log on #{@host} for possible errors."
end

#upload_recipes(ssh) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/baker.rb', line 61

def upload_recipes(ssh)
  configs = %w{config/node.json config/solo.rb}
  if configs.find{|x| !File.exist?(x) }
    raise "Need to create #{configs.join(', ')} files, it's required for chef to run."
  end

  log "*** uploading chef recipes to #{@host}..."
  @recipes_path = "/tmp/baker/recipes"
  @tarball = "#{File.dirname(@recipes_path)}/recipes.tgz"
  # create tarball
  local_cmd("tar czf /tmp/recipes.tgz config cookbooks")
  # upload to /tmp/baker/recipes
  remote_cmd(ssh, "if [ -d '#{@recipes_path}' ] ; then rm -rf #{@recipes_path}; fi") # cleanup from before
  remote_cmd(ssh, "if [ ! -d '#{@recipes_path}' ] ; then mkdir -p #{@recipes_path}; fi")
  local_cmd("scp /tmp/recipes.tgz #{@host}:#{@tarball}")
  # not using -C flag changes /root folder owner!!! and screws up ssh access
  remote_cmd(ssh, "tar -zxf #{@tarball} -C #{@recipes_path}")
  # # cleanup both remote and local
  remote_cmd(ssh, "rm -f /tmp/baker/recipes.tgz")
  local_cmd("rm -f /tmp/recipes.tgz")
end

#validate_cookbook_projectObject



55
56
57
58
59
# File 'lib/baker.rb', line 55

def validate_cookbook_project
  if !File.exist?('cookbooks')
    raise NotCookbookProject.new("not in chef cookbooks project, @root is #{@root}")
  end
end