yyuu-capistrano-chef-solo 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -29,7 +29,7 @@ To setup your servers with `chef-solo`, add following in you `config/deploy.rb`.
29
29
 
30
30
  # in "config/deploy.rb"
31
31
  require 'capistrano-chef-solo'
32
- set(:chef_solo_cookbook_repository, "git@example.com:foo/bar.git")
32
+ set(:chef_solo_cookbooks_repository, "git@example.com:foo/bar.git")
33
33
 
34
34
  And then, now you can start `chef-solo` via capistrano.
35
35
 
@@ -41,8 +41,10 @@ Following options are available to manage your `chef-solo`.
41
41
  * `:chef_solo_user` - special user to invoke `chef-solo`. use `user` by default.
42
42
  * `:chef_solo_ssh_options` - special ssh options for `chef_solo_user`. use `ssh_options` by default.
43
43
  * `:chef_solo_ruby_version` - ruby version to launch `chef-solo`.
44
- * `:chef_solo_cookbook_repository` - the URL of your cookbook repository.
45
- * `:chef_solo_cookbook_revision` - the `branch` in the repository.
44
+ * `:chef_solo_cookbooks_repository` - the URL of your cookbook repository. use `repository` by default.
45
+ * `:chef_solo_cookbooks_revision` - the `branch` in the repository.
46
+ * `:chef_solo_cookbooks_subdir` - the path to the `cookbooks` directory in the repository. use `config/cookbooks` by default.
47
+ * `:chef_solo_cookbooks` - an alternative way to specify cookbooks repository. you can set multiple repositories from here.
46
48
  * `:chef_solo_attributes` - the `attributes` to apply to `chef-solo`.
47
49
  * `:chef_solo_run_list` - the `run_list` to apply to `chef-solo`.
48
50
 
@@ -1,7 +1,10 @@
1
- require "capistrano-chef-solo/version"
1
+ require 'capistrano-chef-solo/version'
2
2
  require 'capistrano-rbenv'
3
+ require 'capistrano/configuration'
4
+ require 'capistrano/recipes/deploy/scm'
3
5
  require 'json'
4
6
  require 'tmpdir'
7
+ require 'uri'
5
8
 
6
9
  module Capistrano
7
10
  module ChefSolo
@@ -55,55 +58,101 @@ module Capistrano
55
58
  }
56
59
 
57
60
  task(:update) {
58
- update_cookbook
61
+ update_cookbooks
59
62
  update_config
60
63
  update_attributes
61
64
  invoke
62
65
  }
63
66
 
64
- _cset(:chef_solo_cookbook_repository) {
65
- abort("chef_solo_cookbook_repository not set")
66
- }
67
- _cset(:chef_solo_cookbook_revision, 'HEAD')
68
- _cset(:chef_solo_cookbooks_exclude, [])
69
- task(:update_cookbook) { # TODO: refactor
70
- git = fetch(:chef_solo_git, 'git')
71
- tar = fetch(:chef_solo_tar, 'tar')
72
- copy_dir = Dir.mktmpdir()
73
- destination = File.join(copy_dir, 'cookbooks')
74
- filename = "#{destination}.tar.gz"
67
+ task(:update_cookbooks) {
68
+ tmpdir = Dir.mktmpdir()
69
+ remote_tmpdir = Dir.mktmpdir()
70
+ destination = File.join(tmpdir, 'cookbooks')
75
71
  remote_destination = File.join(chef_solo_path, 'cookbooks')
76
- remote_filename = File.join('/tmp', File.basename(filename))
77
-
78
- repository_cache = File.join(copy_dir, 'cached-copy')
79
- if fetch(:chef_solo_cookbook_subdir, nil)
80
- repository_cache_subdir = File.join(repository_cache, chef_solo_cookbook_subdir)
81
- else
82
- repository_cache_subdir = repository_cache
72
+ filename = File.join(tmpdir, 'cookbooks.tar.gz')
73
+ remote_filename = File.join(remote_tmpdir, 'cookbooks.tar.gz')
74
+ begin
75
+ bundle_cookbooks(filename, destination)
76
+ run("mkdir -p #{remote_tmpdir}")
77
+ distribute_cookbooks(filename, remote_filename, remote_destination)
78
+ ensure
79
+ run("rm -rf #{remote_tmpdir}")
80
+ run_locally("rm -rf #{tmpdir}")
83
81
  end
82
+ }
84
83
 
85
- begin
86
- checkout = []
87
- checkout << "#{git} clone -q #{chef_solo_cookbook_repository} #{repository_cache}"
88
- checkout << "cd #{repository_cache} && #{git} checkout -q -b deploy #{chef_solo_cookbook_revision}"
89
- if chef_solo_cookbooks_exclude.empty?
90
- checkout << "cp -RPp #{repository_cache_subdir} #{destination}"
84
+ _cset(:chef_solo_cookbook_repository) { abort("chef_solo_cookbook_repository not set") }
85
+ _cset(:chef_solo_cookbooks_repository) {
86
+ logger.info("WARNING: `chef_solo_cookbook_repository' has been deprecated. use `chef_solo_cookbooks_repository' instead.")
87
+ chef_solo_cookbook_repository
88
+ }
89
+ _cset(:chef_solo_cookbook_revision, 'HEAD')
90
+ _cset(:chef_solo_cookbooks_revision) {
91
+ logger.info("WARNING: `chef_solo_cookbook_revision' has been deprecated. use `chef_solo_cookbooks_revision' instead.")
92
+ chef_solo_cookbook_revision
93
+ }
94
+ _cset(:chef_solo_cookbook_subdir, '/')
95
+ _cset(:chef_solo_cookbooks_subdir) {
96
+ logger.info("WARNING: `chef_solo_cookbook_subdir' has been deprecated. use `chef_solo_cookbooks_subdir' instead.")
97
+ chef_solo_cookbook_subdir
98
+ }
99
+ _cset(:chef_solo_cookbooks_exclude, %w(.hg .git .svn))
100
+
101
+ # special variable to set multiple cookbooks repositories.
102
+ # by default, it will build from :chef_solo_cookbooks_* variables.
103
+ _cset(:chef_solo_cookbooks) {
104
+ name = File.basename(chef_solo_cookbooks_repository, File.extname(chef_solo_cookbooks_repository))
105
+ {
106
+ name => {
107
+ :repository => chef_solo_cookbooks_repository,
108
+ :revision => chef_solo_cookbooks_revision,
109
+ :cookbooks => chef_solo_cookbooks_subdir,
110
+ :cookbooks_exclude => chef_solo_cookbooks_exclude,
111
+ }
112
+ }
113
+ }
114
+
115
+ _cset(:chef_solo_configuration, configuration)
116
+ _cset(:chef_solo_repository_cache) { File.expand_path('./tmp/cookbooks-cache') }
117
+ def bundle_cookbooks(filename, destination)
118
+ dirs = [ File.dirname(filename), destination ].uniq
119
+ run_locally("mkdir -p #{dirs.join(' ')}")
120
+ chef_solo_cookbooks.each do |name, options|
121
+ configuration = Capistrano::Configuration.new()
122
+ chef_solo_configuration.variables.merge(options).each { |key, val|
123
+ configuration.set(key, val)
124
+ }
125
+ # refreshing just :source, :revision and :real_revision is enough?
126
+ configuration.set(:source) { Capistrano::Deploy::SCM.new(configuration[:scm], configuration) }
127
+ configuration.set(:revision) { configuration[:source].head }
128
+ configuration.set(:real_revision) {
129
+ configuration[:source].local.query_revision(configuration[:revision]) { |cmd|
130
+ with_env("LC_ALL", "C") { run_locally(cmd) }
131
+ }
132
+ }
133
+ repository_cache = File.join(chef_solo_repository_cache, name)
134
+ if File.exist?(repository_cache)
135
+ run_locally(configuration[:source].sync(configuration[:real_revision], repository_cache))
91
136
  else
92
- exclusions = chef_solo_cookbooks_exclude.map { |e| "--exclude=\"#{e}\"" }.join(' ')
93
- checkout << "rsync -lrpt #{exclusions} #{repository_cache_subdir} #{destination}"
137
+ run_locally(configuration[:source].checkout(configuration[:real_revision], repository_cache))
94
138
  end
95
- run_locally(checkout.join(' && '))
96
-
97
- copy = []
98
- copy << "cd #{File.dirname(destination)} && #{tar} chzf #{filename} #{File.basename(destination)}"
99
- run_locally(copy.join(' && '))
100
139
 
101
- upload(filename, remote_filename)
102
- run("cd #{File.dirname(remote_destination)} && #{tar} xzf #{remote_filename} && rm -f #{remote_filename}")
103
- ensure
104
- run_locally("rm -rf #{copy_dir}")
140
+ cookbooks = [ options.fetch(:cookbooks, '/') ].flatten.compact
141
+ execute = cookbooks.map { |c|
142
+ repository_cache_subdir = File.join(repository_cache, c)
143
+ exclusions = options.fetch(:cookbooks_exclude, []).map { |e| "--exclude=\"#{e}\"" }.join(' ')
144
+ "rsync -lrpt #{exclusions} #{repository_cache_subdir}/ #{destination}"
145
+ }
146
+ run_locally(execute.join(' && '))
105
147
  end
106
- }
148
+ run_locally("cd #{File.dirname(destination)} && tar chzf #{filename} #{File.basename(destination)}")
149
+ end
150
+
151
+ def distribute_cookbooks(filename, remote_filename, remote_destination)
152
+ upload(filename, remote_filename)
153
+ run("rm -rf #{remote_destination}")
154
+ run("cd #{File.dirname(remote_destination)} && tar xzf #{remote_filename}")
155
+ end
107
156
 
108
157
  _cset(:chef_solo_config) {
109
158
  (<<-EOS).gsub(/^\s*/, '')
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module ChefSolo
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yyuu-capistrano-chef-solo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-24 00:00:00.000000000 Z
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano