capistrano-config 0.0.3 → 0.0.4

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
@@ -40,7 +40,6 @@ Following options are available to manage your configuration files.
40
40
  * `:config_readable_files` - list of files which should be readable. empty by default.
41
41
  * `:config_writable_files` - list of files which should be writable. empty by default.
42
42
  * `:config_executable_files` - list of files which should be executable. empty by default.
43
- * `:config_remove_files` - list of files which should be removed. empty by default.
44
43
 
45
44
  ## Contributing
46
45
 
@@ -11,16 +11,10 @@ module Capistrano
11
11
  _cset(:config_path_local) { File.expand_path('.') }
12
12
  _cset(:config_template_path) { File.join(File.expand_path('.'), 'config', 'templates') }
13
13
  _cset(:config_files, [])
14
- _cset(:config_source_files) {
15
- config_files.map { |file| File.join(config_template_path, file) }
16
- }
17
- _cset(:config_temporary_files) {
18
- config_files.map { |file|
19
- f = Tempfile.new('config'); t = f.path
20
- f.close(true) # remote tempfile immediately
21
- t
22
- }
23
- }
14
+
15
+ _cset(:config_use_shared, false)
16
+ _cset(:config_shared_path) { File.join(shared_path, 'config') }
17
+
24
18
  _cset(:config_readable_mode, "ug+r")
25
19
  _cset(:config_readable_files, [])
26
20
  _cset(:config_writable_mode, "ug+rw")
@@ -29,16 +23,14 @@ module Capistrano
29
23
  _cset(:config_executable_files, [])
30
24
  _cset(:config_remove_files, [])
31
25
 
32
- desc("Update applicatin config.")
33
- task(:update, :roles => :app, :except => { :no_release => true }) {
34
- transaction {
35
- update_locally if fetch(:config_update_locally, false)
36
- update_remotely if fetch(:config_update_remotely, true)
37
- }
38
- }
39
- after 'deploy:finalize_update', 'config:update'
26
+ def tempfile(name)
27
+ f = Tempfile.new(name)
28
+ path = f.path
29
+ f.close(true) # close and remove tempfile immediately
30
+ path
31
+ end
40
32
 
41
- def _read_config(config)
33
+ def template(config)
42
34
  if File.file?(config)
43
35
  File.read(config)
44
36
  elsif File.file?("#{config}.erb")
@@ -48,62 +40,112 @@ module Capistrano
48
40
  end
49
41
  end
50
42
 
51
- def _do_update(src_tmp_tgt, options={})
52
- options = {
53
- :readable_files => [], :writable_files => [], :executable_files => [],
54
- :remove_files => [],
55
- :use_sudo => true,
56
- }.merge(options)
57
- dirs = src_tmp_tgt.map { |src, tmp, tgt| File.dirname(tgt) }.uniq
43
+ def update_one(source, target, options={})
58
44
  try_sudo = options[:use_sudo] ? sudo : ""
59
45
  execute = []
60
- execute << "mkdir -p #{dirs.join(' ')}" unless dirs.empty?
61
- src_tmp_tgt.map { |src, tmp, tgt|
62
- execute << "( diff -u #{tgt} #{tmp} || #{try_sudo} mv -f #{tmp} #{tgt} )"
63
- }
64
- execute << "#{try_sudo} chmod #{config_readable_mode} #{options[:readable_files].join(' ')}" unless options[:readable_files].empty?
65
- execute << "#{try_sudo} chmod #{config_writable_mode} #{options[:writable_files].join(' ')}" unless options[:writable_files].empty?
66
- execute << "#{try_sudo} chmod #{config_executable_mode} #{options[:executable_files].join(' ')}" unless options[:executable_files].empty?
67
- execute << "#{try_sudo} rm -f #{options[:remove_files].join(' ')}" unless options[:remove_files].empty?
46
+ dirs = [ File.dirname(source), File.dirname(target) ].uniq
47
+ execute << "#{try_sudo} mkdir -p #{dirs.map { |d| d.dump }.join(' ')}"
48
+ execute << "( #{try_sudo} diff -u #{target.dump} #{source.dump} || #{try_sudo} mv -f #{source.dump} #{target.dump} )"
49
+
50
+ execute << "#{try_sudo} chmod #{config_readable_mode} #{target.dump}" if options[:readable_files].include?(target)
51
+ execute << "#{try_sudo} chmod #{config_writable_mode} #{target.dump}" if options[:writable_files].include?(target)
52
+ execute << "#{try_sudo} chmod #{config_executable_mode} #{target.dump}" if options[:executable_files].include?(target)
53
+ execute << "#{try_sudo} rm -f #{config_remove_files.map { |f| f.dump }.join(' ')}" if options[:remove_files].include?(target)
68
54
 
69
55
  execute.join(' && ')
70
56
  end
71
57
 
72
- task(:update_locally, :roles => :app, :except => { :no_release => true }) {
58
+ def update_all(files={}, options={})
59
+ srcs = files.map { |src, dst| src }
60
+ tmps = files.map { tempfile("capistrano-config") }
61
+ dsts = files.map { |src, dst| dst }
62
+ begin
63
+ srcs.zip(tmps).each do |src, tmp|
64
+ put(template(src), tmp)
65
+ end
66
+ tmps.zip(dsts).each do |tmp, dst|
67
+ run(update_one(tmp, dst, options.merge(:use_sudo => fetch(:config_use_sudo_remotely, false))))
68
+ end
69
+ ensure
70
+ run("rm -f #{tmps.map { |f| f.dump }.join(' ')}") unless tmps.empty?
71
+ end
72
+ end
73
+
74
+ def update_all_locally(files={}, options={})
75
+ srcs = files.map { |src, dst| src }
76
+ tmps = files.map { tempfile("capistrano-config") }
77
+ dsts = files.map { |src, dst| dst }
73
78
  begin
74
- target_files = config_files.map { |f| File.join(config_path_local, f) }
75
- src_tmp_tgt = config_source_files.zip(config_temporary_files, target_files)
76
- src_tmp_tgt.each { |src, tmp, tgt|
77
- File.open(tmp, 'wb') { |fp| fp.write(_read_config(src)) } unless dry_run
78
- }
79
- run_locally(_do_update(src_tmp_tgt,
80
- :use_sudo => fetch(:config_use_sudo_locally, false),
81
- :readable_files => config_readable_files.map { |f| File.join(config_path_local, f) },
82
- :writable_files => config_writable_files.map { |f| File.join(config_path_local, f) },
83
- :executable_files => config_executable_files.map { |f| File.join(config_path_local, f) },
84
- :remove_files => config_remove_files.map { |f| File.join(config_path_local, f) }))
79
+ srcs.zip(tmps).each do |src, tmp|
80
+ File.open(tmp, 'wb') { |fp| fp.write(template(src)) } unless dry_run
81
+ end
82
+ tmps.zip(dsts).each do |tmp, dst|
83
+ run_locally(update_one(tmp, dst, options.merge(:use_sudo => fetch(:config_use_sudo_locally, false))))
84
+ end
85
85
  ensure
86
- run_locally("rm -f #{config_temporary_files.join(' ')}") unless config_temporary_files.empty?
86
+ run_locally("rm -f #{tmps.map { |f| f.dump }.join(' ')}") unless tmps.empty?
87
87
  end
88
+ end
89
+
90
+ def symlink_all(files={}, options={})
91
+ execute = []
92
+ files.each do |src, dst|
93
+ execute << "( rm -f #{dst.dump} && ln -s #{src.dump} #{dst.dump} )"
94
+ end
95
+ run(execute.join(' && '))
96
+ end
97
+
98
+ desc("Setup shared application config.")
99
+ task(:setup, :roles => :app, :except => { :no_release => true }) {
100
+ if config_use_shared
101
+ srcs = config_files.map { |f| File.join(config_template_path, f) }
102
+ dsts = config_files.map { |f| File.join(config_shared_path, f) }
103
+ update_all(srcs.zip(dsts),
104
+ :readable_files => config_readable_files.map { |f| File.join(config_shared_path, f) },
105
+ :writable_files => config_writable_files.map { |f| File.join(config_shared_path, f) },
106
+ :executable_files => config_executable_files.map { |f| File.join(config_shared_path, f) },
107
+ :remove_files => config_remove_files.map { |f| File.join(config_shared_path, f) }
108
+ )
109
+ end
110
+ }
111
+ after 'deploy:setup', 'config:setup'
112
+
113
+ desc("Update applicatin config.")
114
+ task(:update, :roles => :app, :except => { :no_release => true }) {
115
+ transaction {
116
+ update_remotely if fetch(:config_update_remotely, true)
117
+ update_locally if fetch(:config_update_locally, false)
118
+ }
88
119
  }
120
+ after 'deploy:finalize_update', 'config:update'
89
121
 
90
122
  task(:update_remotely, :roles => :app, :except => { :no_release => true }) {
91
- begin
92
- target_files = config_files.map { |f| File.join(config_path, f) }
93
- src_tmp_tgt = config_source_files.zip(config_temporary_files, target_files)
94
- src_tmp_tgt.each { |src, tmp, tgt|
95
- put(_read_config(src), tmp)
96
- }
97
- run(_do_update(src_tmp_tgt,
98
- :use_sudo => fetch(:config_use_sudo_remotely, true),
123
+ if config_use_shared
124
+ srcs = config_files.map { |f| File.join(config_shared_path, f) }
125
+ dsts = config_files.map { |f| File.join(config_path, f) }
126
+ symlink_all(srcs.zip(dsts))
127
+ else
128
+ srcs = config_files.map { |f| File.join(config_template_path, f) }
129
+ dsts = config_files.map { |f| File.join(config_path, f) }
130
+ update_all(srcs.zip(dsts),
99
131
  :readable_files => config_readable_files.map { |f| File.join(config_path, f) },
100
132
  :writable_files => config_writable_files.map { |f| File.join(config_path, f) },
101
133
  :executable_files => config_executable_files.map { |f| File.join(config_path, f) },
102
- :remove_files => config_remove_files.map { |f| File.join(config_path, f) }))
103
- ensure
104
- run("rm -f #{config_temporary_files.join(' ')}") unless config_temporary_files.empty?
134
+ :remove_files => config_remove_files.map { |f| File.join(config_path, f) }
135
+ )
105
136
  end
106
137
  }
138
+
139
+ task(:update_locally, :roles => :app, :except => { :no_release => true }) {
140
+ srcs = config_files.map { |f| File.join(config_template_path, f) }
141
+ dsts = config_files.map { |f| File.join(config_path_local, f) }
142
+ update_all_locally(srcs.zip(dsts),
143
+ :readable_files => config_readable_files.map { |f| File.join(config_path_local, f) },
144
+ :writable_files => config_writable_files.map { |f| File.join(config_path_local, f) },
145
+ :executable_files => config_executable_files.map { |f| File.join(config_path_local, f) },
146
+ :remove_files => config_remove_files.map { |f| File.join(config_path_local, f) }
147
+ )
148
+ }
107
149
  }
108
150
  }
109
151
  end
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module ConfigRecipe
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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-10-01 00:00:00.000000000 Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano