Agiley-ec2onrails 0.9.9 → 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. data/{History.txt → CHANGELOG} +0 -0
  2. data/{COPYING.txt → COPYING} +0 -0
  3. data/Manifest +161 -0
  4. data/{website/index.txt → README.textile} +33 -5
  5. data/Rakefile +36 -4
  6. data/TODO +91 -0
  7. data/ec2onrails.gemspec +279 -0
  8. data/examples/Capfile +3 -0
  9. data/examples/deploy.rb +88 -0
  10. data/examples/s3.yml +9 -0
  11. data/lib/ec2onrails/capistrano_utils.rb +0 -11
  12. data/lib/ec2onrails/recipes.rb +165 -59
  13. data/lib/ec2onrails/version.rb +1 -1
  14. data/server/build-ec2onrails.sh +44 -0
  15. data/server/files/etc/aliases +5 -0
  16. data/server/files/etc/aliases.db +0 -0
  17. data/server/files/etc/apache2/apache2.conf +295 -0
  18. data/server/files/etc/apache2/conf.d/app.proxy_cluster.conf +7 -0
  19. data/server/files/etc/apache2/conf.d/app.proxy_frontend.conf +10 -0
  20. data/server/files/etc/apache2/mods-available/proxy.conf +18 -0
  21. data/server/files/etc/apache2/sites-available/app.common +56 -0
  22. data/server/files/etc/apache2/sites-available/app.custom +0 -0
  23. data/server/files/etc/apache2/sites-available/default +14 -0
  24. data/server/files/etc/apache2/sites-available/default-ssl +18 -0
  25. data/server/files/etc/cron.d/backup_app_db_to_s3 +6 -0
  26. data/server/files/etc/cron.daily/app +9 -0
  27. data/server/files/etc/cron.daily/logrotate_post +19 -0
  28. data/server/files/etc/cron.hourly/app +10 -0
  29. data/server/files/etc/cron.monthly/app +10 -0
  30. data/server/files/etc/cron.weekly/app +10 -0
  31. data/server/files/etc/ec2onrails/balancer_members +6 -0
  32. data/server/files/etc/ec2onrails/roles.yml +5 -0
  33. data/server/files/etc/environment +2 -0
  34. data/server/files/etc/event.d/monit +13 -0
  35. data/server/files/etc/init.d/ec2-every-startup +29 -0
  36. data/server/files/etc/init.d/ec2-first-startup +36 -0
  37. data/server/files/etc/init.d/mongrel +91 -0
  38. data/server/files/etc/init.d/nginx +78 -0
  39. data/server/files/etc/init.d/set_roles +3 -0
  40. data/server/files/etc/logrotate.d/apache2 +16 -0
  41. data/server/files/etc/logrotate.d/mongrel +11 -0
  42. data/server/files/etc/logrotate.d/nginx +11 -0
  43. data/server/files/etc/memcached.conf +47 -0
  44. data/server/files/etc/mongrel_cluster/app.yml +9 -0
  45. data/server/files/etc/monit/README +5 -0
  46. data/server/files/etc/monit/app.monitrc.erb +13 -0
  47. data/server/files/etc/monit/db_primary.monitrc.erb +10 -0
  48. data/server/files/etc/monit/memcache.monitrc +8 -0
  49. data/server/files/etc/monit/monitrc +12 -0
  50. data/server/files/etc/monit/system.monitrc +15 -0
  51. data/server/files/etc/monit/web.monitrc.erb +23 -0
  52. data/server/files/etc/motd.tail +13 -0
  53. data/server/files/etc/mysql/my.cnf +149 -0
  54. data/server/files/etc/nginx/nginx.conf +296 -0
  55. data/server/files/etc/postfix/main.cf +4 -0
  56. data/server/files/etc/rc0.d/K10mongrel +1 -0
  57. data/server/files/etc/rc1.d/K10mongrel +1 -0
  58. data/server/files/etc/rc2.d/S90mongrel +1 -0
  59. data/server/files/etc/rc3.d/S90mongrel +1 -0
  60. data/server/files/etc/rc4.d/S90mongrel +1 -0
  61. data/server/files/etc/rc5.d/S90mongrel +1 -0
  62. data/server/files/etc/rc6.d/K10mongrel +1 -0
  63. data/server/files/etc/rcS.d/S91ec2-first-startup +1 -0
  64. data/server/files/etc/rcS.d/S92ec2-every-startup +1 -0
  65. data/server/files/etc/rcS.d/S99set_roles +1 -0
  66. data/server/files/etc/ssh/sshd_config +94 -0
  67. data/server/files/etc/sudoers +1 -0
  68. data/server/files/etc/sudoers.full_access +26 -0
  69. data/server/files/etc/sudoers.restricted_access +28 -0
  70. data/server/files/etc/syslog.conf +69 -0
  71. data/server/files/usr/local/ec2onrails/COPYING +339 -0
  72. data/server/files/usr/local/ec2onrails/bin/archive_file.rb +44 -0
  73. data/server/files/usr/local/ec2onrails/bin/backup_app_db.rb +68 -0
  74. data/server/files/usr/local/ec2onrails/bin/init_services.rb +57 -0
  75. data/server/files/usr/local/ec2onrails/bin/mongrel_start +8 -0
  76. data/server/files/usr/local/ec2onrails/bin/mongrel_stop +8 -0
  77. data/server/files/usr/local/ec2onrails/bin/optimize_mysql.rb +339 -0
  78. data/server/files/usr/local/ec2onrails/bin/rails_env +35 -0
  79. data/server/files/usr/local/ec2onrails/bin/rebundle.sh +70 -0
  80. data/server/files/usr/local/ec2onrails/bin/restore_app_db.rb +58 -0
  81. data/server/files/usr/local/ec2onrails/bin/set_rails_env +40 -0
  82. data/server/files/usr/local/ec2onrails/bin/set_roles.rb +76 -0
  83. data/server/files/usr/local/ec2onrails/bin/setup_web_proxy.rb +106 -0
  84. data/server/files/usr/local/ec2onrails/config +30 -0
  85. data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +82 -0
  86. data/server/files/usr/local/ec2onrails/lib/roles_helper.rb +137 -0
  87. data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +126 -0
  88. data/server/files/usr/local/ec2onrails/lib/utils.rb +16 -0
  89. data/server/files/usr/local/ec2onrails/lib/vendor/ini.rb +268 -0
  90. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/get-hostname.sh +27 -0
  91. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/README +5 -0
  92. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs.sh +42 -0
  93. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key.sh +49 -0
  94. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc.sh +27 -0
  95. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir.sh +24 -0
  96. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-credentials.sh +29 -0
  97. data/server/rakefile.rb +222 -0
  98. data/test/autobench.conf +60 -0
  99. data/test/spec/lib/s3_helper_spec.rb +134 -0
  100. data/test/spec/lib/s3_old.yml +3 -0
  101. data/test/spec/test_files/test1 +0 -0
  102. data/test/spec/test_files/test2 +0 -0
  103. data/test/test_app/Capfile +3 -0
  104. data/test/test_app/README +182 -0
  105. data/test/test_app/Rakefile +10 -0
  106. data/test/test_app/app/controllers/application.rb +7 -0
  107. data/test/test_app/app/controllers/db_fast_controller.rb +6 -0
  108. data/test/test_app/app/controllers/fast_controller.rb +5 -0
  109. data/test/test_app/app/controllers/slow_controller.rb +6 -0
  110. data/test/test_app/app/controllers/very_slow_controller.rb +6 -0
  111. data/test/test_app/app/helpers/application_helper.rb +3 -0
  112. data/test/test_app/app/helpers/db_fast_helper.rb +2 -0
  113. data/test/test_app/app/helpers/fast_helper.rb +2 -0
  114. data/test/test_app/app/helpers/slow_helper.rb +2 -0
  115. data/test/test_app/app/helpers/very_slow_helper.rb +2 -0
  116. data/test/test_app/config/boot.rb +109 -0
  117. data/test/test_app/config/database.yml +19 -0
  118. data/test/test_app/config/deploy.rb +21 -0
  119. data/test/test_app/config/environment.rb +60 -0
  120. data/test/test_app/config/environments/development.rb +21 -0
  121. data/test/test_app/config/environments/production.rb +18 -0
  122. data/test/test_app/config/environments/test.rb +19 -0
  123. data/test/test_app/config/routes.rb +27 -0
  124. data/test/test_app/db/schema.rb +7 -0
  125. data/test/test_app/doc/README_FOR_APP +2 -0
  126. data/test/test_app/public/404.html +30 -0
  127. data/test/test_app/public/500.html +30 -0
  128. data/test/test_app/public/dispatch.cgi +10 -0
  129. data/test/test_app/public/dispatch.fcgi +24 -0
  130. data/test/test_app/public/dispatch.rb +10 -0
  131. data/test/test_app/public/favicon.ico +0 -0
  132. data/test/test_app/public/images/rails.png +0 -0
  133. data/test/test_app/public/javascripts/application.js +2 -0
  134. data/test/test_app/public/javascripts/controls.js +963 -0
  135. data/test/test_app/public/javascripts/dragdrop.js +972 -0
  136. data/test/test_app/public/javascripts/effects.js +1120 -0
  137. data/test/test_app/public/javascripts/prototype.js +4225 -0
  138. data/test/test_app/public/robots.txt +1 -0
  139. data/test/test_app/script/about +3 -0
  140. data/test/test_app/script/breakpointer +3 -0
  141. data/test/test_app/script/console +3 -0
  142. data/test/test_app/script/destroy +3 -0
  143. data/test/test_app/script/generate +3 -0
  144. data/test/test_app/script/performance/benchmarker +3 -0
  145. data/test/test_app/script/performance/profiler +3 -0
  146. data/test/test_app/script/performance/request +3 -0
  147. data/test/test_app/script/plugin +3 -0
  148. data/test/test_app/script/process/inspector +3 -0
  149. data/test/test_app/script/process/reaper +3 -0
  150. data/test/test_app/script/process/spawner +3 -0
  151. data/test/test_app/script/runner +3 -0
  152. data/test/test_app/script/server +3 -0
  153. data/test/test_app/test/functional/db_fast_controller_test.rb +18 -0
  154. data/test/test_app/test/functional/fast_controller_test.rb +18 -0
  155. data/test/test_app/test/functional/slow_controller_test.rb +18 -0
  156. data/test/test_app/test/functional/very_slow_controller_test.rb +18 -0
  157. metadata +193 -36
  158. data/Manifest.txt +0 -25
  159. data/README.txt +0 -1
  160. data/config/hoe.rb +0 -70
  161. data/config/requirements.rb +0 -17
  162. data/script/destroy +0 -14
  163. data/script/generate +0 -14
  164. data/script/txt2html +0 -74
  165. data/tasks/deployment.rake +0 -27
  166. data/tasks/environment.rake +0 -7
  167. data/tasks/website.rake +0 -17
  168. data/website/javascripts/rounded_corners_lite.inc.js +0 -285
  169. data/website/stylesheets/screen.css +0 -144
  170. data/website/template.rhtml +0 -53
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ # This script archives a file to S3
22
+
23
+ require "rubygems"
24
+ require "optiflag"
25
+ require "#{File.dirname(__FILE__)}/../lib/s3_helper"
26
+ require "#{File.dirname(__FILE__)}/../lib/utils"
27
+
28
+ include FileUtils
29
+
30
+ module CommandLineArgs extend OptiFlagSet
31
+ optional_flag "bucket"
32
+ optional_flag "dir"
33
+ optional_flag "file"
34
+ and_process!
35
+ end
36
+
37
+ # include the hostname in the bucket name so test instances don't accidentally clobber real backups
38
+ bucket = ARGV.flags.bucket
39
+ dir = ARGV.flags.dir
40
+ file = ARGV.flags.file
41
+ exit unless File.exists?(file)
42
+
43
+ @s3 = Ec2onrails::S3Helper.new(bucket, dir)
44
+ @s3.store_file(file)
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ exit unless File.stat("/etc/init.d/mysql").executable?
22
+ exit unless File.exists?("/mnt/app/current")
23
+
24
+ require "rubygems"
25
+ require "optiflag"
26
+ require "fileutils"
27
+ require "#{File.dirname(__FILE__)}/../lib/mysql_helper"
28
+ require "#{File.dirname(__FILE__)}/../lib/s3_helper"
29
+ require "#{File.dirname(__FILE__)}/../lib/utils"
30
+
31
+ module CommandLineArgs extend OptiFlagSet
32
+ optional_flag "bucket"
33
+ optional_flag "dir"
34
+ optional_switch_flag "incremental"
35
+ optional_switch_flag "reset"
36
+ and_process!
37
+ end
38
+
39
+ # include the hostname in the bucket name so test instances don't accidentally clobber real backups
40
+ bucket = ARGV.flags.bucket
41
+ dir = ARGV.flags.dir || "database"
42
+ @s3 = Ec2onrails::S3Helper.new(bucket, dir)
43
+ @mysql = Ec2onrails::MysqlHelper.new
44
+ @temp_dir = "/mnt/tmp/ec2onrails-backup-#{@s3.bucket}-#{dir.gsub(/\//, "-")}"
45
+ if File.exists?(@temp_dir)
46
+ puts "Temp dir exists (#{@temp_dir}), aborting. Is another backup process running?"
47
+ exit
48
+ end
49
+
50
+ begin
51
+ FileUtils.mkdir_p @temp_dir
52
+ if ARGV.flags.incremental
53
+ # Incremental backup
54
+ @mysql.execute_sql "flush logs"
55
+ logs = Dir.glob("/mnt/log/mysql/mysql-bin.[0-9]*").sort
56
+ logs_to_archive = logs[0..-2] # all logs except the last
57
+ logs_to_archive.each {|log| @s3.store_file log}
58
+ @mysql.execute_sql "purge master logs to '#{File.basename(logs[-1])}'"
59
+ else
60
+ # Full backup
61
+ file = "#{@temp_dir}/dump.sql.gz"
62
+ @mysql.dump(file, ARGV.flags.reset)
63
+ @s3.store_file file
64
+ @s3.delete_files("mysql-bin")
65
+ end
66
+ ensure
67
+ FileUtils.rm_rf(@temp_dir)
68
+ end
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ require "#{File.dirname(__FILE__)}/../lib/roles_helper"
22
+ include Ec2onrails::RolesHelper
23
+
24
+
25
+ # memcache role:
26
+ if in_role?(:memcache)
27
+ # increase memory size, etc if no other roles exist?
28
+ start(:memcache, "memcached")
29
+ else
30
+ stop(:memcache, "memcached")
31
+ end
32
+
33
+ # db primary role:
34
+ if in_role?(:db_primary)
35
+ # increase caches, etc if no other roles exist?
36
+ start(:db_primary, "mysql", "mysqld")
37
+ else
38
+ stop(:db_primary, "mysql", "mysqld")
39
+ end
40
+
41
+ # web role:
42
+ if in_role?(:web)
43
+ #we symlink the web_proxy we are using....
44
+ start(:web, "web_proxy")
45
+ run("/etc/init.d/web_proxy reload")
46
+ else
47
+ stop(:web, "nginx")
48
+ stop(:web, "web_proxy")
49
+ end
50
+
51
+
52
+ # app role:
53
+ if in_role?(:app)
54
+ start(:app, "mongrel", "mongrel_rails")
55
+ else
56
+ stop(:app, "mongrel", "mongrel_rails")
57
+ end
@@ -0,0 +1,8 @@
1
+ #!/bin/sh
2
+
3
+ . /etc/environment
4
+ export PATH
5
+ export RUBYLIB
6
+
7
+ mongrel_rails cluster::start -C /etc/mongrel_cluster/app.yml --clean $*
8
+
@@ -0,0 +1,8 @@
1
+ #!/bin/sh
2
+
3
+ . /etc/environment
4
+ export PATH
5
+ export RUBYLIB
6
+
7
+ mongrel_rails cluster::stop -C /etc/mongrel_cluster/app.yml $*
8
+
@@ -0,0 +1,339 @@
1
+ #!/usr/bin/ruby
2
+
3
+
4
+ ## Notes about optimizations are inline below.
5
+ # based upon recommendations listed here:
6
+ # http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server-after-installation/
7
+ #
8
+ # For best effect, call this script after every other service has been started.
9
+ #
10
+
11
+ require 'fileutils'
12
+ require "/usr/local/ec2onrails/lib/roles_helper"
13
+ require "/usr/local/ec2onrails/lib/vendor/ini"
14
+ require "/usr/local/ec2onrails/lib/mysql_helper"
15
+ require "/usr/local/ec2onrails/lib/utils"
16
+
17
+ include Ec2onrails::RolesHelper
18
+
19
+ DEFAULT_CONFIG_LOC = "/etc/mysql/my.cnf"
20
+
21
+ exit unless in_role?(:db_primary)
22
+
23
+ local_roles = roles.inject([]){|all_roles, role| all_roles << role.first if role.last.include?("127.0.0.1")}
24
+ only_db_role = local_roles.size < 2
25
+
26
+
27
+ #lets make a copy of the original...but do not overwrite if it already exists!
28
+ FileUtils.copy(DEFAULT_CONFIG_LOC, "#{DEFAULT_CONFIG_LOC}.pre_optimized") unless File.exists?("#{DEFAULT_CONFIG_LOC}.pre_optimized")
29
+
30
+
31
+ ##### ************************** COMPUTE METRICS ************************** #####
32
+ # define metrics used to modify my.cnf. These metrics are used as guidelines
33
+ # and are not meant to be exact measurements or limits.
34
+ #
35
+ # * how much free memory is available?
36
+ #
37
+ # * how many other roles is the db server taking on? Based upon the type
38
+ # and number of other roles, change the amout of memory we should dedicate
39
+ # to the db. This ratio is not the absolute ratio but is used as a guideline
40
+ #
41
+ # * how many cores does the slice have. Then, based upon the Engineyard
42
+ # recommendation that upto 4 mongrels generally take up one core, compute
43
+ # the number of estimated available cores
44
+ #
45
+ # * how many max connections are allowed, and how many database tables exist
46
+ ##### ************************ END COMPUTE METRICS ************************ #####
47
+
48
+ #default is one core
49
+ num_cores = `cat /proc/cpuinfo`.find_all{|o| o =~ /^\s*processor\s+/}.size rescue 1
50
+ #if also running app, just search for ruby because that will
51
+ #include mongrel but also daemons and other scripts
52
+ num_ruby_instances = local_roles.include?(:app) ? `ps ax | grep ruby | grep -v 'grep ruby'`.split("\n").size : 0
53
+ avail_cores = num_cores - num_ruby_instances/4
54
+ avail_cores = 0 if avail_cores.nil? || avail_cores < 0
55
+
56
+
57
+ mem_opt = nil
58
+ mem_opt ||= 0.15 if local_roles.include?(:app) && local_roles.include?(:web) && local_roles.include?(:memcache)
59
+ mem_opt ||= 0.25 if local_roles.include?(:app) && local_roles.include?(:memcache)
60
+ mem_opt ||= 0.35 if local_roles.include?(:app) || local_roles.include?(:memcache)
61
+ mem_opt ||= 0.50 if local_roles.include?(:web)
62
+ mem_opt ||= 0.70 #if only db, lets use a 70% ratio
63
+
64
+ orig_free_mem = (`free -m` =~ /buffers\/cache:\s+\d+\s+(\d+)/; $1).to_i rescue 1024
65
+
66
+ #TODO: take into account memcached settings if memcached is running on this server
67
+ ruby_mem_reserved = num_ruby_instances * 180
68
+ if orig_free_mem > 4098 && (orig_free_mem * mem_opt + ruby_mem_reserved) * 1.25 < orig_free_mem
69
+ mem_opt *= 1.5
70
+ end
71
+ free_mem = (orig_free_mem * mem_opt).to_i
72
+
73
+ @mysql = Ec2onrails::MysqlHelper.new
74
+
75
+ result = run("/etc/init.d/mysql start")
76
+ if result
77
+ puts <<-MSG
78
+ ****** WOOPS ******
79
+ mysql was not successfully started up.
80
+ Not optimizing mysql config file
81
+ MSG
82
+ exit 1
83
+ end
84
+
85
+ num_connections = 100
86
+ mysql_cmd = %{mysql -u #{@mysql.user} -e "select @@max_connections;"}
87
+ mysql_cmd += " -p'#{@mysql.password}' " unless @mysql.password.nil?
88
+ if `#{mysql_cmd}` =~ /(\d+)/imx
89
+ num_connections = $1.to_i
90
+ end
91
+
92
+ num_tables = 100
93
+ mysql_cmd = %{mysql -u #{@mysql.user} -e "SELECT count(*) TABLES from information_schema.tables where TABLE_SCHEMA = '#{@mysql.database}';"}
94
+ mysql_cmd += " -p'#{@mysql.password}' " unless @mysql.password.nil?
95
+ if `#{mysql_cmd}` =~ /(\d+)/imx
96
+ num_tables = $1.to_i
97
+ end
98
+
99
+ # the default my.cnf is already different than the default example files... it has already
100
+ # been modified for ec2. So lets use that one rather than starting from scratch again.
101
+ # default_mysql_config = if free_mem > 2048
102
+ # "my-huge.cnf"
103
+ # elsif free_mem > 768
104
+ # "my-large.cnf"
105
+ # else
106
+ # "my-medium.cnf"
107
+ # end
108
+
109
+ puts <<-MSG
110
+
111
+ Optimizing mysql based off of these stats:
112
+ * sharing server with these roles : #{local_roles.inspect}
113
+ * num cores (est avail cores) : #{num_cores} (#{avail_cores})
114
+ * avail mem (mem for db) : #{orig_free_mem} (#{free_mem})
115
+ * num database tables : #{num_tables}
116
+ * max num conns : #{num_connections}
117
+ MSG
118
+
119
+
120
+ #lets figure out which default config file to start with:
121
+ # new_config = "/etc/mysql/#{default_mysql_config}"
122
+
123
+
124
+ ##### ******************** Modifying MYSQL config file ******************** #####
125
+ configs = Ini.load(DEFAULT_CONFIG_LOC, :comment => '#')
126
+ configs['mysqld']['key_buffer_size'] ||= configs['mysqld']['key_buffer']
127
+
128
+ modifying_keys = %w(thread_concurency thread_cache_size query_cache_size table_cache
129
+ key_buffer_size innodb_flush_log_at_trx_commit
130
+ innodb_buffer_pool_size innodb_additional_mem_pool_size
131
+ innodb_log_buffer_size innodb_log_file_size)
132
+
133
+ original_values = modifying_keys.inject([]){|all, key| all << [key, configs['mysqld'][key.to_s]]}
134
+
135
+
136
+ ##### thread_concurency: only turn on thread concurrency if
137
+ # there are some spare 'cores' available
138
+ if avail_cores < 2
139
+ configs['mysqld'].delete('thread_concurency')
140
+ elsif
141
+ # Try number of CPU's*2 for thread_concurrency
142
+ configs['mysqld']['thread_concurency'] = (avail_cores) * 2
143
+ end
144
+
145
+
146
+ #### thread_cache: we don't want threads being created on a regular basis, but
147
+ # if we don't have available cores it doesn't make much sense to cache
148
+ # to many threads
149
+ configs['mysqld']['thread_cache_size'] = if avail_cores > 6
150
+ 24
151
+ elsif avail_cores > 3
152
+ 16
153
+ else
154
+ 8
155
+ end
156
+
157
+
158
+ #### query_cache_size: Important for read-heavy db loads, but it gets expensive
159
+ # to maintain if it gets too large.
160
+ configs['mysqld']['query_cache_size'] = if free_mem > 4096
161
+ only_db_role ? 512 : 384
162
+ elsif free_mem > 2048
163
+ only_db_role ? 256 : 128
164
+ elsif free_mem > 1024
165
+ only_db_role ? 128 : 64
166
+ else
167
+ 64
168
+ end
169
+ configs['mysqld']['query_cache_size'] = "#{configs['mysqld']['query_cache_size']}M"
170
+
171
+
172
+ #### table_cache: Opening tables can be expensive, so this cache helps mitigate that.
173
+ # Each connection needs its own entry in the table cache, but this is less important for innodb
174
+ # heavy database (which most rails apps are).
175
+ # based upont he observation that a cache size of 1024 is a good size for a db with a few hundred tables
176
+ configs['mysqld']['table_cache'] = (num_connections * num_tables)/10
177
+
178
+
179
+ #### key_buffer_size: Does not need to be very large because most rails
180
+ # applications do not use MyISAM, or use if very little (usually to store
181
+ # db-based sessions). Keep space available for temp tables and other
182
+ # little mysql needs
183
+ configs['mysqld']['key_buffer_size'] = if free_mem > 4096
184
+ only_db_role ? 256 : 128
185
+ elsif free_mem > 2048
186
+ only_db_role ? 128 : 64
187
+ elsif free_mem > 1024
188
+ only_db_role ? 64 : 32
189
+ else
190
+ 16
191
+ end
192
+ configs['mysqld']['key_buffer_size'] = "#{configs['mysqld']['key_buffer_size']}M"
193
+ #can use either key_buffer or key_buffer_size.
194
+ #Since we are using key_buffer_size, lets remove the other one
195
+ configs['mysqld'].delete('key_buffer')
196
+
197
+
198
+ #### innodb_flush_log_at_trx_commit: this makes INNODB *much* faster, but
199
+ # it is not 100% ACID compliant. Instead of flushing to disk for every
200
+ # commit, this flushes to the OS file cache. That means that if MySQL
201
+ # crashes, the data will be written, but if the OS crashes, 1-2
202
+ # seconds of information could be lost
203
+ configs['mysqld']['innodb_flush_log_at_trx_commit'] = 2
204
+
205
+
206
+ # * innodb_buffer_pool_size upto 70% of memory..but if sharing and small data sizes, use less (50%?)
207
+
208
+ #### innodb_buffer_pool_size: this is where we should put most of our free memory, since
209
+ # rails apps are heavy users of innodb. Need to be careful NOT to specify TOO much memory
210
+ configs['mysqld']['innodb_buffer_pool_size'] = if free_mem > 4096
211
+ free_mem - 512
212
+ elsif free_mem > 2048
213
+ free_mem - 384
214
+ elsif free_mem > 1024
215
+ free_mem - 256
216
+ elsif free_mem > 512
217
+ free_mem - 192
218
+ elsif free_mem > 256
219
+ free_mem - 128
220
+ else
221
+ free_mem - 98
222
+ end
223
+ configs['mysqld']['innodb_buffer_pool_size'] = "#{configs['mysqld']['innodb_buffer_pool_size']}M"
224
+
225
+
226
+ #### innodb_additional_mem_pool_size: This is not really needed as most OS's do a good job
227
+ # of allocating memory.
228
+ configs['mysqld']['innodb_additional_mem_pool_size'] ||= '16M'
229
+
230
+
231
+ #### innodb_log_buffer_size: This is flushed every second anyway, so 8-16M is generally ok
232
+ configs['mysqld']['innodb_log_buffer_size'] ||= '12M'
233
+
234
+
235
+ #### innodb_log_file_size: Help with heavy writes,
236
+ # BUT if it is too large recovery times can be a lot longer
237
+ configs['mysqld']['innodb_log_file_size'] = if free_mem > 4096
238
+ 512
239
+ elsif free_mem > 2048
240
+ 256
241
+ elsif free_mem > 1024
242
+ 128
243
+ else
244
+ 64
245
+ end
246
+ configs['mysqld']['innodb_log_file_size'] = "#{configs['mysqld']['innodb_log_file_size']}M"
247
+ ##### ****************** END Modifying MYSQL config file ****************** #####
248
+
249
+
250
+
251
+ new_values = modifying_keys.inject([]){|all, key| all << [key, configs['mysqld'][key.to_s]]}
252
+ msg = "\nModified these mysqld parameters:\n"
253
+ msg += <<-MSG
254
+ mysqld key: new value (original value)
255
+ -----------------------------------------------
256
+ MSG
257
+ new_values.each_with_index do |v, i|
258
+ orig_value = original_values[i].last.nil? ? 'not set' : original_values[i].last
259
+ msg += <<-MSG
260
+ * #{v.first}: #{v.last} (#{orig_value})
261
+ MSG
262
+ end
263
+ puts msg
264
+
265
+ config_file_loc = DEFAULT_CONFIG_LOC
266
+ #We need to shut down mysql BEFORE we move the new configs over...
267
+ puts "\nCleanly stopping mysql to replace its config file."
268
+
269
+ #make sure the mysql has time to startup before we shut it down again
270
+ #TODO: can we improve this?
271
+ sleep(5)
272
+
273
+ result = run("/etc/init.d/mysql stop")
274
+ clean_stop = true
275
+ if result
276
+ config_file_loc += ".optimized"
277
+ clean_stop = false
278
+ puts <<-MSG
279
+ ****** WOOPS ******
280
+ mysql was not successfully shut down, so we dare not
281
+ update the config file (it can cause problems with the
282
+ ib_logfile cache). We have saved the new config file at
283
+ #{config_file_loc}
284
+ in case you still want to use it in place of
285
+ #{DEFAULT_CONFIG_LOC}
286
+ MSG
287
+ else
288
+ puts <<-MSG
289
+ cleanly shutdown mysql. Replacing config file:
290
+ #{config_file_loc}
291
+ The original config file can be found here:
292
+ #{DEFAULT_CONFIG_LOC}.pre_optimized
293
+
294
+ Starting mysql...
295
+ MSG
296
+ end
297
+
298
+ configs.save(config_file_loc)
299
+
300
+ config_file = File.read(DEFAULT_CONFIG_LOC)
301
+ File.open(DEFAULT_CONFIG_LOC, 'w') do |file|
302
+ file << <<-MSG
303
+ # This file is generated by '#{__FILE__}'
304
+ # Based upon the default '#{DEFAULT_CONFIG_LOC}'
305
+ # which is now saved at '#{DEFAULT_CONFIG_LOC}.pre_optimized'
306
+
307
+ # See file for comments:
308
+ # #{__FILE__}
309
+ #
310
+ #
311
+
312
+ MSG
313
+ file << config_file
314
+ end
315
+
316
+ if clean_stop
317
+ #before we can start, we need to move the old cache files...
318
+ old_logs = []
319
+ Dir.glob("/mnt/mysql_data/ib_logfile*").each do |f|
320
+ FileUtils.mv(f, f + "_old")
321
+ old_logs << "#{f}_old"
322
+ end
323
+ puts <<-MSG
324
+ Moving the old mysql ib logfiles because we might have changed the
325
+ default logfile cache size. If mysql startups up successfully,
326
+ these files can be removed:
327
+ #{old_logs.join("\n ")}
328
+ MSG
329
+
330
+
331
+ result = run("/etc/init.d/mysql start")
332
+ if result
333
+ puts <<-MSG
334
+ ****** WOOPS ******
335
+ mysql was not successfully started up.
336
+ Check syslog, as the culprit will be logged there.
337
+ MSG
338
+ end
339
+ end