desc 'Deploying application with rsync' task :sync do # Load the config file from config/sync.yml sync_config = YAML::load(File.open("#{RAILS_ROOT}/config/rsync.yml")) or fail "You should create config/sync.yml file in your RAILS_ROOT with target servers parameters" # Get the target server's parameters target_name = ENV['to'] || sync_config['default_target'] or fail "You should specify the sync target as default_target in your sync config or as 'to' argument on the command line" fail "Invalid target name" unless sync_config[target_name] target = sync_config[target_name] # and check that all of them are in place ['host', 'user', 'dir'].each { |param| fail "Missing '#{param}' parameter for the target server '#{target_name}'" unless target[param] } # ensure that dir has a trailing slash target['dir'] = target['dir'] + '/' unless target['dir']=~/\/$/ # specify ssh port if it was set in the server's config ssh_params = target['port'] ? " -p#{target['port']} " : '' # actually do the sync only if the 'go' arguments was given to the rake, dry run in other case rsync_params = (ENV.key? 'go') ? '' : " --dry-run " # build the command itself cmd = "rsync --progress #{rsync_params} -azC --force --delete --exclude-from=#{RAILS_ROOT}/config/rsync_exclude.txt -e 'ssh #{ssh_params}' #{RAILS_ROOT} #{target['user']}@#{target['host']}:#{target['dir']}" sh cmd end