Class: AktionCap::Tasks

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/aktion_cap/tasks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install_tasksObject



8
9
10
# File 'lib/aktion_cap/tasks.rb', line 8

def install_tasks
  new.install
end

Instance Method Details

#installObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/aktion_cap/tasks.rb', line 91

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{|stage| write_stage_config_deploy(stage, opts)}
  end
end

#prompts_for_capifyObject



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
  options = {}
  options[:application] = ask("Enter the application name: ") {|q| q.default = File.basename(Dir.pwd)}
  options[:scm] = ask("Enter type of version control: ") {|q| q.default = "git"}
  options[:repository] = ask("Enter the git repository to deploy from: ") do |q|
    if options[:scm] == 'git'
      q.default = `git config --local remote.origin.url`.strip
    end
  end
  options[:ssh_user] = ask("Enter the ssh username to deploy with: ") {|q| q.default = 'deployer'}
  options[:stages] = ask("Enter the deployment stages(separate with commas): ") {|q| q.default = 'production'}.split(',').map(&:to_sym)
  options[:stages].each do |stage|
    say("\nConfigure #{stage.to_s}:")
    options[stage] = {}
    options[stage][:server_hostname] = ask("  Enter the server hostname or IP: ") {|q| q.default = 'localhost'}
    options[stage][:server_port] = ask("  Enter the ssh port to connect to: ", Integer) do |q|
      if options[stage][:server_hostname] == 'localhost'
        q.default = '2222'
      else
        q.default = '22'
      end
    end
  end
  options
end

#write_capfileObject



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
# 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'\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'\nafter  'deploy',                'deploy:cleanup'\n    FILE\n  end\nend\n"

#write_stage_config_deploy(stage, opts) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aktion_cap/tasks.rb', line 79

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"