Skip to content

Commit

Permalink
Adds a simple string templating system for fetch vars not yet set
Browse files Browse the repository at this point in the history
  • Loading branch information
TalkingQuickly committed Jan 3, 2014
1 parent 1b672e6 commit 649979d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 7 additions & 5 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,25 @@

# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
# init scripts etc. The full_app_name variable isn't
# available at this point so we use a custom template {{}}
# tag and then add it at run time.
set(:symlinks, [
{
source: "nginx.conf",
link: "/etc/nginx/sites-enabled/#{fetch(:full_app_name)}"
link: "/etc/nginx/sites-enabled/{{full_app_name}}"
},
{
source: "unicorn_init.sh",
link: "/etc/init.d/unicorn_#{fetch(:full_app_name)}"
link: "/etc/init.d/unicorn_{{full_app_name}}"
},
{
source: "log_rotation",
link: "/etc/logrotate.d/#{fetch(:full_app_name)}"
link: "/etc/logrotate.d/{{full_app_name}}"
},
{
source: "monit",
link: "/etc/monit/conf.d/#{fetch(:full_app_name)}.conf"
link: "/etc/monit/conf.d/{{full_app_name}}.conf"
}
])

Expand Down
12 changes: 12 additions & 0 deletions lib/capistrano/substitute_strings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# we often want to refer to variables which
# are defined in subsequent stage files. This
# let's us use the {{var}} to represent fetch(:var)
# in strings which are only evaluated at runtime.

def sub_strings(input_string)
output_string = input_string
input_string.scan(/{{(\w*)}}/).each do |var|
output_string.gsub!("{{#{var[0]}}}", fetch(var[0].to_sym))
end
output_string
end
2 changes: 1 addition & 1 deletion lib/capistrano/tasks/setup_config.cap
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace :deploy do
symlinks = fetch(:symlinks)

symlinks.each do |symlink|
sudo "ln -nfs #{shared_path}/config/#{symlink[:source]} #{symlink[:link]}"
sudo "ln -nfs #{shared_path}/config/#{symlink[:source]} #{sub_strings(symlink[:link])}"
end
end
end
Expand Down

0 comments on commit 649979d

Please sign in to comment.