baker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.markdown +18 -0
  2. data/Rakefile +32 -0
  3. data/bin/baker +10 -0
  4. data/gemspec.rb +18 -0
  5. data/lib/baker.rb +93 -0
  6. metadata +66 -0
data/README.markdown ADDED
@@ -0,0 +1,18 @@
1
+ runchef
2
+ =======
3
+
4
+ A Simple Way to Run Chef recipes
5
+
6
+ Install
7
+ -------
8
+
9
+ <pre>
10
+ sudo gem install baker --source http://gemcutter.org --no-ri --no-rdoc
11
+ </pre>
12
+
13
+ Usage
14
+ -------
15
+
16
+ <pre>
17
+ baker <server>
18
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+ require 'spec/rake/spectask'
5
+ require 'gemspec'
6
+
7
+ desc "Generate gemspec"
8
+ task :gemspec do
9
+ File.open("#{Dir.pwd}/#{GEM_NAME}.gemspec", 'w') do |f|
10
+ f.write(GEM_SPEC.to_ruby)
11
+ end
12
+ end
13
+
14
+ desc "Install gem"
15
+ task :install do
16
+ sudo = ENV['SUDO'].nil? ? '' : 'sudo '
17
+ Rake::Task['gem'].invoke
18
+ `#{sudo} gem uninstall #{GEM_NAME} -x`
19
+ `#{sudo} gem install pkg/#{GEM_NAME}*.gem`
20
+ `rm -Rf pkg`
21
+ end
22
+
23
+ desc "Package gem"
24
+ Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
25
+ pkg.gem_spec = GEM_SPEC
26
+ end
27
+
28
+ desc "Run specs"
29
+ Spec::Rake::SpecTask.new do |t|
30
+ t.spec_opts = ["--format", "specdoc", "--colour"]
31
+ t.spec_files = FileList["spec/**/*_spec.rb"]
32
+ end
data/bin/baker ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__),'..','lib','baker')
4
+
5
+ server = ARGV[0]
6
+ if server.nil? or server == ""
7
+ puts "Usage: baker <server-name>"
8
+ exit 1
9
+ end
10
+ Baker.run(server)
data/gemspec.rb ADDED
@@ -0,0 +1,18 @@
1
+ GEM_NAME = 'baker'
2
+ GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
3
+ GEM_SPEC = Gem::Specification.new do |s|
4
+ # == CONFIGURE ==
5
+ s.author = "Tung Nguyen"
6
+ s.email = "tongueroo@gmail.com"
7
+ s.homepage = "http://github.com/tongueroo/#{GEM_NAME}"
8
+ s.summary = "A simple way to run chef recipes"
9
+ # == CONFIGURE ==
10
+ s.executables += [GEM_NAME]
11
+ s.extra_rdoc_files = [ "README.markdown" ]
12
+ s.files = GEM_FILES.to_a
13
+ s.has_rdoc = false
14
+ s.name = GEM_NAME
15
+ s.platform = Gem::Platform::RUBY
16
+ s.require_path = "lib"
17
+ s.version = "0.1.0"
18
+ end
data/lib/baker.rb ADDED
@@ -0,0 +1,93 @@
1
+ # TODO: upload config files: dna.json and solo.rb
2
+
3
+ require 'net/ssh'
4
+ require 'pp'
5
+
6
+ class Baker
7
+ def self.run(host)
8
+ new(host)
9
+ end
10
+
11
+ def initialize(host, user = nil)
12
+ log "start running chef recipes on #{host}"
13
+ @debug = true
14
+ @host = host
15
+ @user = user
16
+ Net::SSH.start(@host, @user) do |ssh|
17
+ check
18
+ upload_chef_configs(ssh)
19
+ upload_recipes(ssh)
20
+ run_chef(ssh)
21
+ end
22
+ log "done running chef recipes on #{host}"
23
+ end
24
+
25
+ def check
26
+ if !File.exist?('cookbooks')
27
+ raise "not in chef cookbooks project need to be in one"
28
+ end
29
+ end
30
+ def upload_chef_configs(ssh)
31
+ log "uploading chef configs to #{@host}..."
32
+ if !File.exist?("config/baker/dna.json") or !File.exist?("config/baker/solo.rb")
33
+ 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"
34
+ end
35
+ bash_exec("tar czf chef-config.tgz config/baker")
36
+ bash_exec("scp chef-config.tgz #{@host}:")
37
+ ssh_exec(ssh, "rm -rf chef-config && tar -zxf chef-config.tgz && mv config/baker chef-config")
38
+ ssh_exec(ssh, "rm -f chef-config.tgz")
39
+ bash_exec("rm -f chef-config.tgz")
40
+ end
41
+ def upload_recipes(ssh)
42
+ log "uploading chef recipes to #{@host}..."
43
+ @file_cache_path = "/tmp/chef-solo"
44
+ @recipes_path = "/tmp/chef-solo/recipes"
45
+ # create
46
+ bash_exec("tar czf recipes.tgz .")
47
+ # upload
48
+ bash_exec("scp recipes.tgz #{@host}:")
49
+ # move
50
+ ssh_exec(ssh, "rm -rf #{@recipes_path}")
51
+ ssh_exec(ssh, "mkdir -p #{@recipes_path}")
52
+ ssh_exec(ssh, "tar -zxf recipes.tgz -C #{@recipes_path}")
53
+ bash_exec("rm recipes.tgz")
54
+ end
55
+
56
+ def run_chef(ssh)
57
+ log "running chef recipes on #{@host}..."
58
+ chef_cmd = "chef-solo -c ~/chef-config/solo.rb -j ~/chef-config/dna.json"
59
+ log "chef_cmd : #{chef_cmd}"
60
+ ssh_exec(ssh, chef_cmd)
61
+ end
62
+
63
+ private
64
+ def log(msg)
65
+ puts msg
66
+ end
67
+ def debug(msg)
68
+ puts msg if @debug
69
+ end
70
+ def bash_exec(command)
71
+ `#{command}`
72
+ end
73
+ def ssh_exec(ssh, command)
74
+
75
+ unless ARGV.empty?
76
+ debug "Executing command: #{command}"
77
+ end
78
+
79
+ stdout = ""
80
+ ssh.exec!(command) do |channel, stream, data|
81
+ stdout << data if stream == :stdout
82
+ end
83
+ output = stdout
84
+
85
+ if output and @debug
86
+ output = output.split("\n").join("\n ")
87
+ debug " ssh output: #{output}"
88
+ end
89
+
90
+ output
91
+ end
92
+ end
93
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Tung Nguyen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-19 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: tongueroo@gmail.com
23
+ executables:
24
+ - baker
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - bin/baker
31
+ - gemspec.rb
32
+ - lib/baker.rb
33
+ - Rakefile
34
+ - README.markdown
35
+ has_rdoc: true
36
+ homepage: http://github.com/tongueroo/baker
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A simple way to run chef recipes
65
+ test_files: []
66
+