diff --git a/config/deploy.rb b/config/deploy.rb index 3c25d81..8bd2fb6 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -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" } ]) diff --git a/lib/capistrano/substitute_strings.rb b/lib/capistrano/substitute_strings.rb new file mode 100644 index 0000000..d4b2443 --- /dev/null +++ b/lib/capistrano/substitute_strings.rb @@ -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 diff --git a/lib/capistrano/tasks/setup_config.cap b/lib/capistrano/tasks/setup_config.cap index 6985c8f..81ce88d 100644 --- a/lib/capistrano/tasks/setup_config.cap +++ b/lib/capistrano/tasks/setup_config.cap @@ -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