require 'securerandom' require 'stringio' def generate_token(n) SecureRandom.hex(n) end namespace :token do desc 'Generate random token' task :generate do on roles(fetch(:token_roles)) do info generate_token(fetch(:token_length)) end end desc 'Upload random token as a file on remote server' task :upload do on roles(fetch(:token_roles)) do token = generate_token(fetch(:token_length)) io = StringIO.new(token) upload! io, fetch(:token_target) end end end namespace :load do task :defaults do set :token_length, 64 set :token_target, -> { shared_path.join('.token') } set :token_roles, :app end end