Class: AktionCap::Tasks
- Inherits:
-
Object
- Object
- AktionCap::Tasks
- Includes:
- Rake::DSL
- Defined in:
- lib/aktion_cap/tasks.rb
Class Method Summary collapse
Instance Method Summary collapse
- #install ⇒ Object
- #prompts_for_capify ⇒ Object
- #write_capfile ⇒ Object
- #write_config_deploy(opts) ⇒ Object
- #write_nginx(stage, opts) ⇒ Object
- #write_stage_config_deploy(stage, opts) ⇒ Object
Class Method Details
.install_tasks ⇒ Object
8 9 10 |
# File 'lib/aktion_cap/tasks.rb', line 8 def install_tasks new.install end |
Instance Method Details
#install ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/aktion_cap/tasks.rb', line 137 def install desc 'capify' task 'capify' do opts = prompts_for_capify Dir.mkdir('config/deploy') unless Dir.exists?('config/deploy') write_capfile write_config_deploy opts opts[:stages].each do |stage| write_stage_config_deploy(stage, opts) write_nginx(stage, opts) end end end |
#prompts_for_capify ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/aktion_cap/tasks.rb', line 13 def prompts_for_capify = {} [:application] = ask("Enter the application name: ") {|q| q.default = File.basename(Dir.pwd)} [:scm] = ask("Enter type of version control: ") {|q| q.default = "git"} [:repository] = ask("Enter the git repository to deploy from: ") do |q| if [:scm] == 'git' q.default = `git config --local remote.origin.url`.strip end end [:ssh_user] = ask("Enter the ssh username to deploy with: ") {|q| q.default = 'deployer'} [:stages] = ask("Enter the deployment stages(separate with commas): ") {|q| q.default = 'production'}.split(',').map(&:to_sym) [:stages].each do |stage| say("\nConfigure #{stage.to_s}:") [stage] = {} [stage][:server_hostname] = ask(" Enter the server hostname or IP: ") {|q| q.default = 'localhost'} [stage][:server_port] = ask(" Enter the ssh port to connect to: ", Integer) do |q| if [stage][:server_hostname] == 'localhost' q.default = '2222' else q.default = '22' end end end end |
#write_capfile ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/aktion_cap/tasks.rb', line 39 def write_capfile File.open('Capfile', 'w') do |file| file << "load 'deploy'\nload 'config/deploy'\n FILE\n end\nend\n" |
#write_config_deploy(opts) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/aktion_cap/tasks.rb', line 48 def write_config_deploy(opts) File.open('config/deploy.rb', 'w') do |file| file << "set :stages, %w(\#{opts[:stages].map(&:to_s).join(' ')})\n\nrequire 'capistrano/ext/multistage'\nrequire 'bundler/capistrano'\nrequire 'rvm/capistrano'\nrequire './config/boot'\nrequire 'aktion_cap/recipe/base'\nrequire 'aktion_cap/recipe/database'\nrequire 'aktion_cap/recipe/nginx'\nrequire 'aktion_cap/recipe/unicorn'\n\nssh_options[:username] = '\#{opts[:ssh_user]}'\nssh_options[:forward_agent] = true\n\nset :application, '\#{opts[:application]}'\nset :repository, '\#{opts[:repository]}'\nset :scm, :\#{opts[:scm]}\nset :deploy_via, :remote_cache\nset(:deploy_to) {\"/var/www/\\\#{application}/\\\#{stage}\"}\nset :rvm_type, :user\nset :use_sudo, false\n\nset :shared_symlinks, %w(config/database.yml)\nset :tasks_for_rake, %w(db:migrate)\n\nafter 'deploy:update_code', 'deploy:create_shared_symlinks'\nbefore 'deploy:create_symlink', 'deploy:run_rake_tasks'\nbefore 'deploy:restart', 'nginx:config'\nafter 'deploy', 'deploy:cleanup'\n FILE\n end\nend\n" |
#write_nginx(stage, opts) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/aktion_cap/tasks.rb', line 96 def write_nginx(stage, opts) File.open("config/nginx_#{stage.to_s}.conf", 'w') do |file| file << "upstream \#{opts[:application]}_\#{stage.to_s} {\n server unix:/tmp/unicorn-\#{opts[:application]}_\#{stage.to_s}.sock fail_timeout=0;\n}\n\nserver {\n listen 80;\n\n server_name opts[stage][:server_hostname];\n\n root /var/www/\#{opts[:application]}/\#{stage.to_s}/current/public;\n access_log /var/log/nginx/\#{opts[:application]}_\#{stage.to_s}-access.log;\n error_log /var/log/nginx/\#{opts[:application]}_\#{stage.to_s}-error.log;\n\n location ~ ^/assets/ {\ngzip_static on;\nexpires max;\nadd_header Cache-Control public;\n }\n\n location {\nproxy_set_header X-Real-IP $remote_addr;\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\nproxy_set_header Host $http_host;\nproxy_redirect off;\n\nif (!-f $request_filename) {\n proxy_pass http://\#{opts[:application]}_\#{stage.to_s};\n break;\n}\n }\n\n error_page 404 /404.html;\n error_page 500 502 503 504 /500.html;\n}\n FILE\n end\nend\n" |
#write_stage_config_deploy(stage, opts) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/aktion_cap/tasks.rb', line 84 def write_stage_config_deploy(stage, opts) File.open("config/deploy/#{stage.to_s}.rb", 'w') do |file| file << "set :port, \#{opts[stage][:server_port]}\nset :server_hostname, '\#{opts[stage][:server_hostname]}'\nrole :app, server_hostname\nrole :web, server_hostname\nrole :db, server_hostname, primary: true\n FILE\n end\nend\n" |