Class: Baker

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user = nil) ⇒ Baker

Returns a new instance of Baker.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/baker.rb', line 11

def initialize(host, user = nil)
  log "start running chef recipes on #{host}"
  @debug = true
  @host = host
  @user = user
  Net::SSH.start(@host, @user) do |ssh|
    check
    upload_chef_configs(ssh)
    upload_recipes(ssh)
    run_chef(ssh)
  end
  log "done running chef recipes on #{host}"
end

Class Method Details

.run(host) ⇒ Object



7
8
9
# File 'lib/baker.rb', line 7

def self.run(host)
  new(host)
end

Instance Method Details

#checkObject



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

def check
  if !File.exist?('cookbooks')
    raise "not in chef cookbooks project need to be in one"
  end
end

#run_chef(ssh) ⇒ Object



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

def run_chef(ssh)
  log "running chef recipes on #{@host}..."
  chef_cmd = "chef-solo -c ~/chef-config/solo.rb -j ~/chef-config/dna.json"
  log "chef_cmd : #{chef_cmd}"
  ssh_exec(ssh, chef_cmd)
end

#upload_chef_configs(ssh) ⇒ Object



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

def upload_chef_configs(ssh)
  log "uploading chef configs to #{@host}..."
  if !File.exist?("config/baker/dna.json") or !File.exist?("config/baker/solo.rb")
    raise "need to create a config/baker/dna.json and config/baker/solo.rb file, so it can be uploaded to the server that needs it"
  end
  bash_exec("tar czf chef-config.tgz config/baker")
  bash_exec("scp chef-config.tgz #{@host}:")
  ssh_exec(ssh, "rm -rf chef-config && tar -zxf chef-config.tgz && mv config/baker chef-config")
  ssh_exec(ssh, "rm -f chef-config.tgz")
  bash_exec("rm -f chef-config.tgz")
end

#upload_recipes(ssh) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/baker.rb', line 41

def upload_recipes(ssh)
  log "uploading chef recipes to #{@host}..."
  @file_cache_path = "/tmp/chef-solo"
  @recipes_path = "/tmp/chef-solo/recipes"
  # create
  bash_exec("tar czf recipes.tgz .")
  # upload
  bash_exec("scp recipes.tgz #{@host}:")
  # move
  ssh_exec(ssh, "rm -rf #{@recipes_path}")
  ssh_exec(ssh, "mkdir -p #{@recipes_path}")
  ssh_exec(ssh, "tar -zxf recipes.tgz -C #{@recipes_path}")
  bash_exec("rm recipes.tgz")
end