rake-remote_chef 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "example/chef-repo/cookbooks"]
2
+ path = example/chef-repo/cookbooks
3
+ url = https://github.com/opscode/cookbooks
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rake-remote_task-chef-solo.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 NEC Soft, Ltd.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Rake::RemoteChef
2
+
3
+ Rake::RemoteChef can run chef-solo on remote host using local Rake.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rake-remote_chef'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rake-remote_chef
18
+
19
+ ## Usage
20
+
21
+ $ rake chef:bootstrap # first time only. (Install chef-solo on remote host.)
22
+ $ rake chef:solo
23
+
24
+ ## Setup
25
+
26
+ Create a config file, usually in 'config/chef.rb'
27
+
28
+ set :application, "SetupServers"
29
+ set :user, 'ubuntu'
30
+
31
+ # Setup roles for Hosts
32
+ # 'role' means Rake::RemoteTask role. (not Chef.)
33
+ role :role1, "#{user}@host1.example.com"
34
+ role :role2, "#{user}@host2.example.com"
35
+
36
+ # Setup run_list for roles
37
+ run_list :role1, ['recipe[apache]', 'recipe[monit]']
38
+ run_list :role2, ['recipe[ruby]', 'recipe[thin]', 'recipe[my_app]']
39
+
40
+ Create attributes file, usually in 'config/attributes/default.yml.erb'
41
+ and 'config/attributes/#{role_name}.yml.erb'.
42
+
43
+ default.yml.erb is used from all roles for default attributes.
44
+
45
+ host: <%= target_host.split('@').last %>
46
+ env:
47
+ http_proxy: 'http://proxy.host:port'
48
+ https_proxy: 'https://proxy.host:port'
49
+ no_proxy: '192.168.*.*'
50
+
51
+ nats_server:
52
+ host: <%= all_roles[:nats_server].keys.first.split('@').last %>
53
+ port: 4222
54
+ user: nats
55
+ password: nats
56
+
57
+ 'config/attributes/#{role_name}.yml.erb' contains role specific attributes.
58
+
59
+ router:
60
+ index: <%= index_of_role(:router) %>
61
+ apache:
62
+ dir: '/etc/apache2'
63
+ log_dir: '/var/log/apache2'
64
+
65
+ Add the folloing to your Rakefile:
66
+
67
+ require 'rake/remote_chef'
68
+ Rake::RemoteChef.load
69
+
70
+ ### Set up _Chef Repository_
71
+
72
+ usually in './chef-repo' directory.
73
+
74
+ chef-repo/
75
+ cookbooks/
76
+ data_bags/
77
+ roles/
78
+
79
+ chef-repo directory contains cookbooks, data_bags and roles direcotry.
80
+
81
+ ### Bootstrap for chef-solo on remote host
82
+
83
+ Run `rake chef:bootstrap` install chef to your remote host.
84
+
85
+ ### Execute chef-solo
86
+
87
+ Run `rake chef:solo` execute chef-solo on your remote host.
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/example/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gem "rake-remote_chef"
data/example/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ $: << File.dirname(__FILE__) + '/lib'
2
+
3
+ require 'rake/remote_task_ex'
4
+ require 'rake/remote_chef'
5
+ Rake::RemoteChef.load
@@ -0,0 +1,2 @@
1
+ mysql:
2
+ allow_remote_root: false
@@ -0,0 +1,3 @@
1
+ java:
2
+ jdk_version: 6
3
+ java_home: '/usr/lib/jvm/default-java'
@@ -0,0 +1,3 @@
1
+ apache:
2
+ dir: '/etc/apache2'
3
+ contact: 'webmaster@example.com'
@@ -0,0 +1,10 @@
1
+
2
+ set :application, "SetupServers"
3
+ set :user, 'ubuntu'
4
+
5
+ role :web, "#{user}@host2.example.com"
6
+ role :db, "#{user}@host1.example.com"
7
+
8
+ run_list :default, [ 'recipe[java]' ]
9
+ run_list :web, [ 'recipe[apache]' ]
10
+ run_list :db, [ 'recipe[mysql]' ]
@@ -0,0 +1,22 @@
1
+ require 'rake/remote_task'
2
+
3
+ class Rake::RemoteTask
4
+ alias run_org run
5
+ def run command
6
+ options = []
7
+ options << "export http_proxy=#{http_proxy}" if defined?(http_proxy) && http_proxy
8
+ options << "export https_proxy=#{https_proxy}" if defined?(https_proxy) && https_proxy
9
+ options << command
10
+ run_org options.join(' && ')
11
+ end
12
+
13
+ alias sudo_org sudo
14
+ def sudo command
15
+ proxies = []
16
+ proxies << "http_proxy=#{http_proxy}" if defined?(http_proxy) && http_proxy
17
+ proxies << "https_proxy=#{https_proxy}" if defined?(https_proxy) && https_proxy
18
+
19
+ command = ['env', proxies, command].flatten.compact.join(" ") if proxies.size > 0
20
+ sudo_org command
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+
2
+ set :ruby_version, '1.9.3-p194'
3
+
4
+ namespace :chef do
5
+ namespace :bootstrap do
6
+ remote_task :update_package do
7
+ # run [
8
+ sudo 'apt-get -y update'#,
9
+ sudo 'apt-get -y -qq -s -o Debug::NoLocking=true upgrade'
10
+ # ].join(' && ')
11
+ end
12
+
13
+ remote_task :install_ruby_essential => :update_package do
14
+ sudo 'apt-get -y install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool'
15
+ end
16
+
17
+ remote_task :install_ruby_build => :install_ruby_essential do
18
+ run 'rm -rf ruby-build && git clone https://github.com/sstephenson/ruby-build.git'#,
19
+ run 'cd ruby-build && sudo ./install.sh'
20
+ sudo "mkdir -p #{ruby_path}"
21
+ end
22
+
23
+ remote_task :install_ruby19 => :install_ruby_build do
24
+ sudo "ruby-build #{ruby_version} #{ruby_path}"
25
+ end
26
+ end
27
+
28
+ desc 'Install chef.'
29
+ remote_task :bootstrap => :'chef:bootstrap:install_ruby19' do
30
+ sudo "#{ruby_path}/bin/gem install chef --no-ri --no-rdoc"
31
+ end
32
+ end
@@ -0,0 +1,6 @@
1
+ file_cache_path '<%= remote_file_cache_path %>'
2
+ cookbook_path '<%= remote_chef_repo_path %>/cookbooks'
3
+ role_path '<%= remote_chef_repo_path %>/roles'
4
+ data_bag_path '<%= remote_chef_repo_path %>/data_bags'
5
+ <% if defined?(http_proxy) %>http_proxy '<%= http_proxy %>'<% end %>
6
+ <% if defined?(https_proxy) %>https_proxy '<%= https_proxy %>'<% end %>
@@ -0,0 +1,107 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ require 'temp_dir'
4
+ require 'fileutils'
5
+ require 'json'
6
+ require 'deep_merge'
7
+
8
+ set :attributes_dir, './config/attributes'
9
+ set :solo_rb_template, File.dirname(__FILE__) + '/core/solo.rb.erb'
10
+
11
+ set :remote_chef_repo_path, '/tmp/chef-repo'
12
+ set :remote_file_cache_path, '/tmp/chef-cache'
13
+ set :remote_blob_dir, '/tmp/blob'
14
+
15
+ set :local_chef_repo, './chef-repo'
16
+ set :local_temp_dir, TempDir.create
17
+ set :local_blob_dir, './blob/'
18
+
19
+
20
+ class Rake::RemoteChef::Core
21
+
22
+ class AttributesTemplate
23
+ attr_reader :target_host
24
+
25
+ def initialize(target_host)
26
+ @target_host = target_host
27
+ end
28
+
29
+ def roles
30
+ roles = []
31
+ Rake::RemoteTask.roles.each do |k, v|
32
+ roles << k if v.keys.include?(target_host)
33
+ end
34
+ roles
35
+ end
36
+
37
+ def all_roles
38
+ Rake::RemoteTask.roles
39
+ end
40
+
41
+ def index_of_role role
42
+ Rake::RemoteTask.roles[role].keys.index(target_host)
43
+ end
44
+
45
+ def load
46
+ attributes = load_attributes_for(:default)
47
+ roles.each do |role|
48
+ attributes.deep_merge!(load_attributes_for(role) || {})
49
+ end
50
+ attributes.update('run_list' => Rake::RemoteChef.run_list_for(*roles)) unless attributes.has_key?('run_list')
51
+ attributes
52
+ end
53
+
54
+ def load_attributes_for(role)
55
+ role_file = "#{attributes_dir}/#{role.to_s}.yml.erb"
56
+ if File.file?(role_file)
57
+ open(role_file) do |io|
58
+ YAML.load(ERB.new(io.read).result(binding))
59
+ end
60
+ else
61
+ puts "attributes template not found at: #{role_file}"
62
+ {}
63
+ end
64
+ end
65
+ end
66
+
67
+ def self.create_solo_rb path
68
+ open(path, 'w') do |io|
69
+ open(solo_rb_template) do |i|
70
+ io.write ERB.new(i.read).result(binding)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+ namespace :chef do
78
+
79
+ task :create_temp_local_chef_repo do
80
+ FileUtils.cp_r(local_chef_repo, local_temp_dir, :preserve => true)
81
+ configs_dir = File.join(local_temp_dir, 'chef-repo', 'configs')
82
+ FileUtils.mkdir_p(configs_dir)
83
+ Rake::RemoteTask.all_hosts.each do |host|
84
+ open(File.join(configs_dir, "#{host}.json"), 'w') do |io|
85
+ io.write Rake::RemoteChef::Core::AttributesTemplate.new(host).load.to_json
86
+ end
87
+ end
88
+
89
+ Rake::RemoteChef::Core.create_solo_rb File.join(configs_dir, 'solo.rb')
90
+ end
91
+
92
+ remote_task :update_repository => :create_temp_local_chef_repo do
93
+ run "mkdir -p #{remote_chef_repo_path}"
94
+ rsync File.join(local_temp_dir, 'chef-repo', ''), "#{target_host}:#{remote_chef_repo_path}"
95
+
96
+ if File.directory?(local_blob_dir)
97
+ run "mkdir -p #{remote_blob_dir}"
98
+ rsync "#{local_blob_dir}", "#{target_host}:#{remote_blob_dir}"
99
+ end
100
+ end
101
+
102
+ desc 'Execute chef-solo on remote.'
103
+ remote_task :solo => :update_repository do
104
+ sudo "#{ruby_path}/bin/chef-solo -c #{remote_chef_repo_path}/configs/solo.rb -j #{remote_chef_repo_path}/configs/#{target_host}.json"
105
+ end
106
+
107
+ end
@@ -0,0 +1,5 @@
1
+ module Rake
2
+ class RemoteChef
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ require 'rake/remote_task'
2
+ require "rake/remote_chef/version"
3
+
4
+ [
5
+ ["Rake::RemoteChef", :runlist, :run_list]
6
+ ].each do |methods|
7
+ receiver = methods.shift
8
+ methods.each do |method|
9
+ eval "def #{method} *args, &block; #{receiver}.#{method}(*args, &block);end"
10
+ end
11
+ end
12
+
13
+ module Rake
14
+ class RemoteChef
15
+
16
+ def self.run_list role, runlist
17
+ @runlist ||= {}
18
+ @runlist[role] ||= []
19
+ (@runlist[role] += runlist).uniq!
20
+ end
21
+
22
+ def self.run_list_for *roles
23
+ roles.unshift(:default)
24
+ roles.map {|r| @runlist[r] }.flatten.uniq.compact
25
+ end
26
+
27
+
28
+ def self.load options = {}
29
+ options = {:config => options} if String === options
30
+ order = [:bootstrap, :core]
31
+ order += options.keys - order
32
+
33
+ recipes = {
34
+ :config => 'config/chef.rb',
35
+ :bootstrap => 'bootstrap/ubuntu',
36
+ :core => 'core'
37
+ }.merge(options)
38
+
39
+ order.each do |flavor|
40
+ recipe = recipes[flavor]
41
+ next if recipe.nil? or flavor == :config
42
+ require "rake/remote_chef/#{recipe}"
43
+ end
44
+
45
+ set :ruby_path, '/opt/chef'
46
+ set(:rsync_flags) {['-rlptDzP', '--exclude', '.git', '-e', "ssh #{ssh_flags.join(' ')}"]}
47
+
48
+ Kernel.load recipes[:config]
49
+ Kernel.load "config/chef_#{ENV['to']}.rb" if ENV['to']
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rake/remote_chef/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["yuanying"]
6
+ gem.email = ["yuanying@fraction.jp"]
7
+ gem.description = %q{Execute chef-solo with Rake::RemoteTask to remote host.}
8
+ gem.summary = %q{Execute chef-solo with Rake::RemoteTask to remote host.}
9
+ gem.homepage = "https://github.com/nec-soft/rake-remote_chef"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rake-remote_chef"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rake::RemoteChef::VERSION
17
+
18
+ gem.add_runtime_dependency "hoe"
19
+ gem.add_runtime_dependency "rake-remote_task"
20
+ gem.add_runtime_dependency "tempdir"
21
+ gem.add_runtime_dependency "json"
22
+ gem.add_runtime_dependency "deep_merge"
23
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake-remote_chef
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - yuanying
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hoe
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake-remote_task
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: tempdir
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: json
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: deep_merge
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Execute chef-solo with Rake::RemoteTask to remote host.
95
+ email:
96
+ - yuanying@fraction.jp
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .gitmodules
103
+ - Gemfile
104
+ - LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - example/Gemfile
108
+ - example/Rakefile
109
+ - example/config/attributes/db.yml.erb
110
+ - example/config/attributes/default.yml.erb
111
+ - example/config/attributes/web.yml.erb
112
+ - example/config/chef.rb
113
+ - example/lib/rake/remote_task_ex.rb
114
+ - lib/rake/remote_chef.rb
115
+ - lib/rake/remote_chef/bootstrap/ubuntu.rb
116
+ - lib/rake/remote_chef/core.rb
117
+ - lib/rake/remote_chef/core/solo.rb.erb
118
+ - lib/rake/remote_chef/version.rb
119
+ - rake-remote_chef.gemspec
120
+ homepage: https://github.com/nec-soft/rake-remote_chef
121
+ licenses: []
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.23
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Execute chef-solo with Rake::RemoteTask to remote host.
144
+ test_files: []