chicken_soup 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ ######################################################################
2
+ # MYSQL TASKS #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ require 'chicken_soup/capabilities/shared/db'
6
+
7
+ namespace :db do
8
+ desc <<-DESC
9
+ Creates the MySQL database and user for the application.
10
+
11
+ * Creates a script that is uploaded to the user's home directory.
12
+ * The script is executed as `root` and as such, the user will be prompted
13
+ for `root`'s password.
14
+ * The DB and user name are equivalent to the application_underscored
15
+ variable.
16
+ * The DB user will be granted all privileges on the DB.
17
+ DESC
18
+ task :create do
19
+ create_script = <<-CREATESCRIPT
20
+ create database #{application_underscored} character set utf8;
21
+ create user '#{application_underscored}'@'localhost' identified by '#{db_app_password}';
22
+ grant all on #{application_underscored}.* to #{application_underscored}@localhost;
23
+ CREATESCRIPT
24
+
25
+ put create_script, "#{user_home}/create_#{application_underscored}_db_script"
26
+ run %Q{#{sudo} bash -c "mysql --user=root --password=#{db_root_password} < #{user_home}/create_#{application_underscored}_db_script"}
27
+ run "rm #{user_home}/create_#{application_underscored}_db_script"
28
+ end
29
+
30
+ desc <<-DESC
31
+ Drops the MySQL database and user for the application.
32
+
33
+ * Creates a script that is uploaded to the user's home directory.
34
+ * The script is executed as `root` and as such, the user will be prompted
35
+ for `root`'s password.
36
+ DESC
37
+ task :drop do
38
+ drop_script = <<-DROPSCRIPT
39
+ drop user #{application_underscored}@localhost;
40
+ drop database #{application_underscored};
41
+ DROPSCRIPT
42
+
43
+ put drop_script, "#{user_home}/drop_#{application_underscored}_db_script"
44
+ run %Q{#{sudo} bash -c "mysql --user=root --password=#{db_root_password} < #{user_home}/drop_#{application_underscored}_db_script"}
45
+ run "rm #{user_home}/drop_#{application_underscored}_db_script"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,31 @@
1
+ ######################################################################
2
+ # PASSENGER TASKS #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ namespace :deploy do
6
+ desc <<-DESC
7
+ There is no way to start the application via Passenger.
8
+
9
+ This task does nothing.
10
+ DESC
11
+ task :start do ; end
12
+
13
+ desc <<-DESC
14
+ There is no way to stop the application via Passenger.
15
+
16
+ This task does nothing.
17
+ DESC
18
+ task :stop do ; end
19
+
20
+ desc <<-DESC
21
+ Starts/Restarts the application.
22
+
23
+ Passenger knows when you'd like to reset by looking at a file in the `tmp`
24
+ directory called `restart.txt`. If the Last Access time for `restart.txt`
25
+ changes, Passenger restarts the app.
26
+ DESC
27
+ task :restart, :except => { :no_release => true } do
28
+ run "touch #{File.join(current_path,'tmp','restart.txt')}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ ######################################################################
2
+ # POSTGRES TASKS #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ require 'chicken_soup/capabilities/shared/db'
6
+ end
@@ -0,0 +1,42 @@
1
+ ######################################################################
2
+ # RVM TASKS #
3
+ ######################################################################
4
+ def run_with_rvm(ruby_env_string, command)
5
+ run("rvm use #{ruby_env_string} && #{command}")
6
+ end
7
+
8
+ ######################################################################
9
+ # RVM ENVIRONMENT CHECKS #
10
+ ######################################################################
11
+ Capistrano::Configuration.instance(:must_exist).load do
12
+ namespace :environment do
13
+ namespace :check do
14
+ desc <<-DESC
15
+ [internal] Checks to see if all necessary RVM variables have been set up.
16
+ DESC
17
+ task :rvm do
18
+ required_variables = [
19
+ :ruby_version,
20
+ :ruby_gemset,
21
+ :rvm_ruby_string,
22
+ ]
23
+
24
+ verify_variables(required_variables)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ ######################################################################
31
+ # DEFAULT RVM ENVIRONMENT SETUP #
32
+ ######################################################################
33
+ Capistrano::Configuration.instance(:must_exist).load do
34
+ namespace :environment do
35
+ namespace :defaults do
36
+ _cset :ruby_version, ENV["rvm_ruby_string"]
37
+ _cset :ruby_gemset, ENV["GEM_HOME"].split('@')[1]
38
+
39
+ _cset(:rvm_ruby_string) {ruby_gemset ? "#{ruby_version}@#{ruby_gemset}" : ruby_version}
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,58 @@
1
+ ######################################################################
2
+ # COMMON DB TASKS #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ _cset(:db_root_password) {Capistrano::CLI.password_prompt("Root Password For DB: ")}
6
+ _cset(:db_app_password) {Capistrano::CLI.password_prompt("App Password For DB: ")}
7
+
8
+ run_task "db:create", :as => "manager"
9
+ run_task "db:drop", :as => "manager"
10
+
11
+ namespace :db do
12
+ desc <<-DESC
13
+ Calls the rake task `db:backup` on the server for the given environment.
14
+
15
+ * The backup file is placed in a directory called `db_backups` under the `shared`
16
+ directory by default.
17
+ * The filenames are formatted with the timestamp of the backup.
18
+ * After export, each file is zipped up using a bzip2 compression format.
19
+ DESC
20
+ task :backup do
21
+ run "if [ ! -d #{shared_path}/db_backups ]; then mkdir #{shared_path}/db_backups; fi"
22
+ run "cd #{current_path} && bundle exec rake BACKUP_DIRECTORY=#{shared_path}/db_backups RAILS_ENV=#{rails_env} db:backup"
23
+ end
24
+
25
+ desc <<-DESC
26
+ Calls the rake task `db:reset_and_seed` on the server for the given environment.
27
+
28
+ Typically, this task will drop the DB, recreate the DB, load the development
29
+ schema and then populate the DB by calling the `db:seed` task.
30
+
31
+ Warning: This task cannot be called in production mode. If you truely wish
32
+ to run this in production, you'll need to log into the server and
33
+ run the rake task manually.
34
+ DESC
35
+ task :reset_and_seed do
36
+ raise "I'm sorry Dave, but I can't let you do that. I have full control over production." if rails_env == 'production'
37
+ db.backup
38
+ run "cd #{current_path} && rake RAILS_ENV=#{rails_env} db:reset_and_seed"
39
+ end
40
+
41
+ desc <<-DESC
42
+ Calls the rake task `db:seed` on the server for the given environment.
43
+
44
+ Typically, this task will populate the DB with valid data which is necessary
45
+ for the initial production deployment. An example may be that the `STATES`
46
+ table gets populated with all the information about the States.
47
+
48
+ Warning: This task cannot be called in production mode. If you truely wish
49
+ to run this in production, you'll need to log into the server and
50
+ run the rake task manually.
51
+ DESC
52
+ task :seed do
53
+ raise "I'm sorry Dave, but I can't let you do that. I have full control over production." if rails_env == 'production'
54
+ db.backup
55
+ run "cd #{current_path} && rake RAILS_ENV=#{rails_env} db:seed"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,163 @@
1
+ ######################################################################
2
+ # COMMON *NIX TASKS #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ before "deploy:config:symlink", "deploy:config:create"
6
+
7
+ namespace :deploy do
8
+ namespace :config do
9
+ desc <<-DESC
10
+ Creates the directory where the `symlink` task will look for its configuration files.
11
+
12
+ It will first look in the user's home directory to see if there is
13
+ a file of the same name as one of the shared files. If there is, it
14
+ will move that to the shared location. If not, it will create an
15
+ empty file.
16
+ DESC
17
+ task :create do
18
+ run "if [ ! -d #{shared_path}/config ]; then mkdir -p #{shared_path}/config; fi"
19
+
20
+ global_shared_files.each do |shared_file|
21
+ local_shared_file = "#{shared_file}.#{rails_env}"
22
+
23
+ if File.exists?(local_shared_file)
24
+ top.upload(local_shared_file, "#{shared_path}/#{shared_file}", :mode => "600")
25
+ else
26
+ run "touch #{shared_path}/#{shared_file}"
27
+ end
28
+ end
29
+ end
30
+
31
+ desc <<-DESC
32
+ Symlinks sensitive configuration files which shouldn't be checked into source control.
33
+
34
+ By default, these live in the shared directory that Capistrano sets up.
35
+ DESC
36
+ task :symlink do
37
+ global_shared_files.each do |shared_file|
38
+ run "ln -nfs #{shared_path}/#{shared_file} #{latest_release}/#{shared_file}"
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ namespace :os do
45
+ namespace :users do
46
+ namespace :root do
47
+ desc <<-DESC
48
+ [internal] Switches Capistrano to use the root user for all subsequent SSH actions.
49
+
50
+ It will prompt for the root user's password the first time it's needed.
51
+ (If the Kompanee Bash environment has been installed, you will no longer
52
+ be able to log in as root.)
53
+ DESC
54
+ task :use do
55
+ set_user_to("root")
56
+ end
57
+ end
58
+
59
+ namespace :manager do
60
+ desc <<-DESC
61
+ Switches Capistrano to use the manager user for all subsequent SSH actions.
62
+
63
+ It will prompt for the manager user's password the first time it's needed.
64
+ (If public key authentication is already installed, you will not be prompted.)
65
+ DESC
66
+ task :use do
67
+ set_user_to(manager_username)
68
+ end
69
+ end
70
+
71
+ namespace :deploy do
72
+ desc <<-DESC
73
+ Switches Capistrano to use the deployment user for all subsequent SSH actions.
74
+
75
+ It will prompt for the deployment user's password the first time it's needed.
76
+ (If public key authentication is already installed, you will not be prompted.)
77
+ DESC
78
+ task :use do
79
+ set_user_to(deployment_username)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ ######################################################################
87
+ # UNIX ENVIRONMENT CHECKS #
88
+ ######################################################################
89
+ Capistrano::Configuration.instance(:must_exist).load do
90
+ namespace :environment do
91
+ namespace :check do
92
+ desc <<-DESC
93
+ [internal] Checks to see if all necessary unix environment variables have been set up.
94
+ DESC
95
+ task :unix do
96
+ required_variables = [
97
+ :user,
98
+ :deployment_username,
99
+ :manager_username,
100
+ :user_home,
101
+ :deployment_user_home,
102
+ :manager_user_home,
103
+ :deploy_dir,
104
+ :deploy_name,
105
+ :deploy_to,
106
+ :app_server_ip,
107
+ :web_server_ip,
108
+ :db_server_ip,
109
+ :web_server_name,
110
+ :app_server_name,
111
+ :db_server_name
112
+ ]
113
+
114
+ verify_variables(required_variables)
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ ######################################################################
121
+ # DEFAULT UNIX SERVER ENVIRONMENT SETUP #
122
+ ######################################################################
123
+ Capistrano::Configuration.instance(:must_exist).load do
124
+ namespace :environment do
125
+ namespace :defaults do
126
+ desc "[internal] Sets intelligent defaults for unix server deployments."
127
+ task :unix do
128
+ _cset :deployment_username, "deploy"
129
+ _cset :manager_username, "manager"
130
+ _cset :user, deployment_username
131
+
132
+ _cset :user_home, "/home/#{user}"
133
+ _cset :manager_user_home, "/home/#{manager_username}"
134
+ _cset :deployment_user_home, "/home/#{deployment_username}"
135
+ _cset :deploy_dir, "/var/www"
136
+ _cset :deploy_name, "#{application_short}.#{domain}"
137
+ set :deploy_to, "#{deploy_dir}/#{deploy_name}"
138
+
139
+ _cset :keep_releases, 15
140
+
141
+ _cset :global_shared_files, ["config/database.yml"]
142
+
143
+ _cset :app_server_ip, server_ip
144
+ _cset :web_server_ip, server_ip
145
+ _cset :db_server_ip, server_ip
146
+
147
+ _cset :app_server_name, server_name
148
+ _cset :web_server_name, server_name
149
+ _cset :db_server_name, server_name
150
+
151
+ # Evidently roles can't be assigned in a namespace :-/
152
+ set_unix_server_roles
153
+ end
154
+ end
155
+ end
156
+
157
+ desc "[internal] This task is only here because `role` cannot be used within a `namespace`"
158
+ task :set_unix_server_roles do
159
+ role :web, web_server_name, :primary => true
160
+ role :app, app_server_name, :primary => true
161
+ role :db, db_server_name, :primary => true
162
+ end
163
+ end
@@ -0,0 +1,82 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ before "deploy", "deploy:web:disable"
3
+ after "deploy", "deploy:web:enable"
4
+
5
+ before "deploy:migrate", "db:backup"
6
+
7
+ namespace :deploy do
8
+ desc <<-DESC
9
+ [internal] The list of tasks used by `deploy`, `deploy:cold`, `deploy:subzero` and `deploy:initial`
10
+ DESC
11
+ task :base do
12
+ transaction do
13
+ deploy.update
14
+ deploy.config.symlink
15
+ gems.install
16
+ deploy.migrate
17
+ deploy.cleanup
18
+ end
19
+ end
20
+
21
+ desc <<-DESC
22
+ Used when you would like to be more forceful with your deployment.
23
+
24
+ It will forceably disable the site (from the web server layer), run a standard
25
+ deployment and re-enable the site.
26
+ DESC
27
+ task :cold do
28
+ transaction do
29
+ website.disable
30
+ deploy.base
31
+ website.enable
32
+ end
33
+ end
34
+
35
+ desc <<-DESC
36
+ When you just want to "FINISH HIM!", hit "Forward, Down, Forward, High Punch" \
37
+ and your website will be at your mercy.
38
+
39
+ It will forceably stop the web server, run a standard deployment, and restart \
40
+ the web server.
41
+ DESC
42
+ task :subzero do
43
+ transaction do
44
+ web_server.stop
45
+ deploy.base
46
+ web_server.start
47
+ end
48
+ end
49
+
50
+ desc <<-DESC
51
+ This task should be used only on the first deployment.
52
+
53
+ It will set up the deployment structure that Capistrano uses, create the DB, install
54
+ the website configuration files into the web server, and then run a standard "cold"
55
+ deployment.
56
+ DESC
57
+ task :initial do
58
+ transaction do
59
+ deploy.setup
60
+ db.create
61
+ website.install
62
+ deploy.cold
63
+ deploy.check
64
+ end
65
+ end
66
+
67
+ desc <<-DESC
68
+ The standard deployment task.
69
+
70
+ It will check out a new release of the code to the `releases` directory, update the
71
+ symlinks to the desired shared configuration files, install all necessary gems,
72
+ run all pending migrations, tag the git commit, perform a cleanup and restart
73
+ the application layer so that the changes will take effect.
74
+ DESC
75
+ task :default do
76
+ transaction do
77
+ deploy.base
78
+ deploy.restart
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,39 @@
1
+ ######################################################################
2
+ # ENVIRONMENT CHECKS #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ on :start, 'environment:check', :except => ['staging', 'production']
6
+
7
+ before 'environment:check', 'environment:check:common'
8
+
9
+ namespace :environment do
10
+ namespace :check do
11
+ desc "[internal] Checks for environment variables shared among all deployment types."
12
+ task :common do
13
+ abort "You need to specify staging or production when you deploy. ie 'cap staging db:backup'" unless exists?(:rails_env)
14
+ abort "You need to specify a deployment type in your application's 'deploy.rb' file. ie 'set :deployment_type, :heroku'" unless exists?(:deployment_type)
15
+
16
+ required_variables = [
17
+ :application,
18
+ :application_short
19
+ ]
20
+
21
+ verify_variables(required_variables)
22
+ end
23
+
24
+ desc "[internal] Runs checks for all of the capabilities listed."
25
+ task :capabilities do
26
+ if exists?(:capabilities)
27
+ fetch(:capabilities).each do |capability|
28
+ environment.check.send(capability.to_s) if environment.check.respond_to?(capability.to_sym)
29
+ end
30
+ end
31
+ end
32
+
33
+ desc "[internal] Checks to see if all the necessary environment variables have been set up for a proper deployment."
34
+ task :default do
35
+ environment.check.capabilities
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,53 @@
1
+ ######################################################################
2
+ # DEFAULT ENVIRONMENT SETUP #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ after 'production', 'environment:defaults:production', 'environment:defaults'
6
+ after 'staging', 'environment:defaults:staging', 'environment:defaults'
7
+
8
+ before 'environment:defaults', 'load_capabilities'
9
+
10
+ namespace :environment do
11
+ namespace :defaults do
12
+ desc "[internal] Used to set up the intelligent staging defaults we like for our projects"
13
+ task :staging do
14
+ set :rails_env, "staging"
15
+ end
16
+
17
+ desc "[internal] Used to set up the intelligent production defaults we like for our projects"
18
+ task :production do
19
+ set :rails_env, "production"
20
+ end
21
+
22
+ desc "[internal] Sets intelligent common defaults for deployments"
23
+ task :common do
24
+ _cset :use_sudo, false
25
+ _cset :default_shell, false
26
+
27
+ _cset :copy_compression, :bz2
28
+
29
+ _cset(:application_underscored) {application.gsub(/-/, "_")}
30
+ end
31
+
32
+ desc "[internal] Sets defaults for all of the capabilities listed."
33
+ task :capabilities do
34
+ if exists?(:capabilities)
35
+ fetch(:capabilities).each do |capability|
36
+ environment.defaults.send(capability.to_s) if environment.defaults.respond_to?(capability.to_sym)
37
+ end
38
+ end
39
+ end
40
+
41
+ desc <<-DESC
42
+ [internal] Installs the entire environment for the given deployment type.
43
+
44
+ Most of these values can be overridden in each application's deploy.rb file.
45
+ DESC
46
+ task :default do
47
+ defaults.common
48
+ defaults.send(deployment_type.to_s)
49
+ defaults.capabilities
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ ######################################################################
2
+ # ENVIRONMENT SETUP #
3
+ ######################################################################
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
+ require 'chicken_soup/environment/checks'
6
+ require 'chicken_soup/environment/defaults'
7
+
8
+ desc "[internal] This task is only here because `require` cannot be used within a `namespace`"
9
+ task :load_capabilities do
10
+ require "chicken_soup/capabilities/unix"
11
+ capabilities.each do |capability|
12
+ require "chicken_soup/capabilities/#{capability}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ def close_sessions
2
+ sessions.values.each { |session| session.close }
3
+ sessions.clear
4
+ end
5
+
6
+ def set_user_to(username)
7
+ close_sessions
8
+ set :user, username
9
+ set(:password) {Capistrano::CLI.password_prompt("#{username.capitalize}'s Password: ")}
10
+ set(:user_home) { user == "root" ? "/root" : "/home/#{username}" }
11
+ end
12
+
13
+ def run_task(task_name, options = {})
14
+ raise "#run_task must be passed an `:as` option so that it knows who to change the user to." unless options[:as]
15
+
16
+ original_username = exists?(:user) ? user : nil
17
+
18
+ if options[:now]
19
+ set_user_to options[:as]
20
+ find_and_execute_task(task_name)
21
+ set_user_to original_username
22
+ else
23
+ before task_name, "os:users:#{options[:as]}:use"
24
+ after task_name, "os:users:#{original_username}:use"
25
+ end
26
+ end
27
+
28
+ def verify_variables(required_variables)
29
+ required_variables.each do |expected_variable|
30
+ abort( "You have not defined '#{expected_variable}' which is necessary for deployment." ) unless exists?(expected_variable)
31
+ end
32
+ end
33
+
34
+ # Taken from the capistrano code.
35
+ def _cset(name, *args, &block)
36
+ unless exists?(name)
37
+ set(name, *args, &block)
38
+ end
39
+ end
40
+
41
+ def remote_file_exists?(file)
42
+ capture("if [ -f #{file} ]; then echo 'exists'; fi;").chomp == "exists"
43
+ end
@@ -1,3 +1,3 @@
1
1
  module ChickenSoup
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/chicken_soup.rb CHANGED
@@ -1,3 +1,7 @@
1
- module ChickenSoup
2
- # Your code goes here...
1
+ if Object.const_defined?("Capistrano")
2
+ Capistrano::Configuration.instance(:must_exist).load do
3
+ require 'chicken_soup/global'
4
+ require 'chicken_soup/environment'
5
+ require 'chicken_soup/deploy'
6
+ end
3
7
  end