Standup.script :node do self.default_params = { :rails_env => 'production', :name => 'webapp' } def run install_package 'git-core' install_gem 'bundler' unless file_exists? scripts.webapp.app_path sudo "mkdir -p #{scripts.webapp.app_path}" end sudo "chown -R ubuntu:ubuntu #{scripts.webapp.app_path}" ensure_github_access unless file_exists? "#{scripts.webapp.app_path}/.git" exec "rm -rf #{scripts.webapp.app_path}/*" exec "git clone git@github.com:#{params.github_user}/#{params.github_repo}.git #{scripts.webapp.app_path}" end bootstrap_db sudo "chown -R nobody:nogroup #{scripts.webapp.app_path}" with_processed_file script_file('webapp.conf') do |file| scripts.passenger.add_server_conf file, "#{params.name}.conf" end end def app_path "/opt/#{params.name}" end def db_name "#{params.name}_#{params.rails_env}" end protected def ensure_github_access return unless exec('ssh -o StrictHostKeyChecking=no git@github.com') =~ /Permission denied \(publickey\)/ unless file_exists? '~/.ssh/id_rsa' exec "ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -C `hostname`" end password = bright_ask("Enter GitGub password for user #{params.github_user}:", false) github_add_deploy_key params.github_user, password, params.github_repo, exec('hostname').strip, exec('cat ~/.ssh/id_rsa.pub').strip end def bootstrap_db unless scripts.postgresql.exec_sql("select * from pg_user where usename = 'webapp'") =~ /1 row/ scripts.postgresql.exec_sql "create user webapp with password 'webapp'" end unless scripts.postgresql.exec_sql("select * from pg_database where datname = '#{db_name}'") =~ /1 row/ scripts.postgresql.exec_sql "create database #{db_name} with owner webapp" in_dir scripts.webapp.app_path do sudo 'bundle install' exec "RAILS_ENV=#{params.rails_env} rake db:schema:load" exec "RAILS_ENV=#{params.rails_env} rake db:seed" end end end def github_add_deploy_key user, password, repo, title, key require 'net/http' Net::HTTP.start 'github.com' do |http| req = Net::HTTP::Post.new "/api/v2/json/repos/key/#{user}/#{repo}/add" req.form_data = {'title' => title, 'key' => key} req.basic_auth user, password response = http.request req response.is_a? Net::HTTPSuccess end end end