gigpark-ec2onrails 0.9.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. data/CHANGELOG +190 -0
  2. data/COPYING +339 -0
  3. data/Manifest +160 -0
  4. data/README.textile +233 -0
  5. data/Rakefile +31 -0
  6. data/TODO +104 -0
  7. data/ec2onrails.gemspec +46 -0
  8. data/echoe_config.rb +27 -0
  9. data/examples/Capfile +3 -0
  10. data/examples/deploy.rb +103 -0
  11. data/examples/s3.yml +9 -0
  12. data/lib/ec2onrails.rb +20 -0
  13. data/lib/ec2onrails/capistrano_utils.rb +47 -0
  14. data/lib/ec2onrails/recipes.rb +146 -0
  15. data/lib/ec2onrails/recipes/db.rb +366 -0
  16. data/lib/ec2onrails/recipes/deploy.rb +26 -0
  17. data/lib/ec2onrails/recipes/server.rb +517 -0
  18. data/lib/ec2onrails/version.rb +29 -0
  19. data/server/build +74 -0
  20. data/server/files/etc/README +7 -0
  21. data/server/files/etc/aliases +5 -0
  22. data/server/files/etc/cron.d/ec2onrails +16 -0
  23. data/server/files/etc/cron.daily/app +31 -0
  24. data/server/files/etc/cron.daily/logrotate_post +25 -0
  25. data/server/files/etc/cron.hourly/app +31 -0
  26. data/server/files/etc/cron.monthly/app +31 -0
  27. data/server/files/etc/cron.weekly/app +31 -0
  28. data/server/files/etc/default/varnish +33 -0
  29. data/server/files/etc/default/varnishncsa +11 -0
  30. data/server/files/etc/denyhosts.conf +628 -0
  31. data/server/files/etc/dpkg/dpkg.cfg +13 -0
  32. data/server/files/etc/ec2onrails/rails_env +1 -0
  33. data/server/files/etc/ec2onrails/roles.yml +5 -0
  34. data/server/files/etc/environment +2 -0
  35. data/server/files/etc/event.d/god +8 -0
  36. data/server/files/etc/god/db_primary.god +16 -0
  37. data/server/files/etc/god/dkim_filter.god +17 -0
  38. data/server/files/etc/god/master.conf +40 -0
  39. data/server/files/etc/god/memcache.god +15 -0
  40. data/server/files/etc/god/notifications.god +11 -0
  41. data/server/files/etc/god/proxy.god +40 -0
  42. data/server/files/etc/god/system.god +37 -0
  43. data/server/files/etc/god/web.god +23 -0
  44. data/server/files/etc/init.d/ec2-every-startup +29 -0
  45. data/server/files/etc/init.d/ec2-first-startup +37 -0
  46. data/server/files/etc/init.d/nginx +78 -0
  47. data/server/files/etc/logrotate.d/nginx +10 -0
  48. data/server/files/etc/logrotate.d/rails +8 -0
  49. data/server/files/etc/logrotate.d/varnish +14 -0
  50. data/server/files/etc/memcached.conf +47 -0
  51. data/server/files/etc/motd.tail +13 -0
  52. data/server/files/etc/mysql/my.cnf +152 -0
  53. data/server/files/etc/nginx/conf.d/custom.conf +1 -0
  54. data/server/files/etc/nginx/nginx.conf.erb +152 -0
  55. data/server/files/etc/postfix/main.cf +4 -0
  56. data/server/files/etc/ssh/sshd_config +96 -0
  57. data/server/files/etc/sudoers +31 -0
  58. data/server/files/etc/syslog.conf +63 -0
  59. data/server/files/etc/varnish/default.vcl.erb +140 -0
  60. data/server/files/usr/local/ec2onrails/COPYING +339 -0
  61. data/server/files/usr/local/ec2onrails/bin/archive_file +44 -0
  62. data/server/files/usr/local/ec2onrails/bin/backup_app_db +160 -0
  63. data/server/files/usr/local/ec2onrails/bin/backup_dir +89 -0
  64. data/server/files/usr/local/ec2onrails/bin/ec2_meta_data +80 -0
  65. data/server/files/usr/local/ec2onrails/bin/exec_runner +76 -0
  66. data/server/files/usr/local/ec2onrails/bin/in_role +33 -0
  67. data/server/files/usr/local/ec2onrails/bin/init_services +43 -0
  68. data/server/files/usr/local/ec2onrails/bin/install_system_files +56 -0
  69. data/server/files/usr/local/ec2onrails/bin/optimize_mysql +346 -0
  70. data/server/files/usr/local/ec2onrails/bin/public-hostname +24 -0
  71. data/server/files/usr/local/ec2onrails/bin/rails_env +33 -0
  72. data/server/files/usr/local/ec2onrails/bin/rebundle +62 -0
  73. data/server/files/usr/local/ec2onrails/bin/restore_app_db +58 -0
  74. data/server/files/usr/local/ec2onrails/bin/set_rails_env +36 -0
  75. data/server/files/usr/local/ec2onrails/bin/set_roles +36 -0
  76. data/server/files/usr/local/ec2onrails/bin/uninstall_system_files +34 -0
  77. data/server/files/usr/local/ec2onrails/config +30 -0
  78. data/server/files/usr/local/ec2onrails/lib/aws_helper.rb +75 -0
  79. data/server/files/usr/local/ec2onrails/lib/god_helper.rb +84 -0
  80. data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +100 -0
  81. data/server/files/usr/local/ec2onrails/lib/roles_helper.rb +156 -0
  82. data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +116 -0
  83. data/server/files/usr/local/ec2onrails/lib/system_files_helper.rb +110 -0
  84. data/server/files/usr/local/ec2onrails/lib/system_files_manifest.rb +107 -0
  85. data/server/files/usr/local/ec2onrails/lib/utils.rb +26 -0
  86. data/server/files/usr/local/ec2onrails/lib/vendor/ini.rb +268 -0
  87. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/README +5 -0
  88. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/create-mysqld-pid-dir +26 -0
  89. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/README +5 -0
  90. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs +45 -0
  91. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key +49 -0
  92. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/get-hostname +27 -0
  93. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc +27 -0
  94. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir +24 -0
  95. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-credentials +32 -0
  96. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-file-permissions +30 -0
  97. data/server/rakefile-wrapper +26 -0
  98. data/server/rakefile.rb +267 -0
  99. data/test/autobench.conf +60 -0
  100. data/test/spec/lib/s3_helper_spec.rb +154 -0
  101. data/test/spec/lib/s3_old.yml +3 -0
  102. data/test/spec/lib/system_files_manifest_spec.rb +122 -0
  103. data/test/spec/test_files/system_files1/_manifest +13 -0
  104. data/test/spec/test_files/system_files1/test1 +0 -0
  105. data/test/spec/test_files/system_files1/test2 +0 -0
  106. data/test/spec/test_files/system_files1/testfolder/test3 +0 -0
  107. data/test/spec/test_files/system_files2/_manifest +2 -0
  108. data/test/spec/test_files/system_files2/test1 +0 -0
  109. data/test/spec/test_files/system_files2/test2 +0 -0
  110. data/test/spec/test_files/system_files2/testfolder/test3 +0 -0
  111. data/test/spec/test_files/test2 +0 -0
  112. data/test/test_app/Capfile +3 -0
  113. data/test/test_app/README +243 -0
  114. data/test/test_app/Rakefile +10 -0
  115. data/test/test_app/app/controllers/application_controller.rb +10 -0
  116. data/test/test_app/app/controllers/db_fast_controller.rb +6 -0
  117. data/test/test_app/app/controllers/fast_controller.rb +5 -0
  118. data/test/test_app/app/controllers/slow_controller.rb +6 -0
  119. data/test/test_app/app/controllers/very_slow_controller.rb +6 -0
  120. data/test/test_app/app/helpers/application_helper.rb +3 -0
  121. data/test/test_app/config/boot.rb +110 -0
  122. data/test/test_app/config/database.yml +24 -0
  123. data/test/test_app/config/deploy.rb +22 -0
  124. data/test/test_app/config/ec2onrails/config.rb +5 -0
  125. data/test/test_app/config/environment.rb +41 -0
  126. data/test/test_app/config/environments/development.rb +17 -0
  127. data/test/test_app/config/environments/production.rb +28 -0
  128. data/test/test_app/config/environments/test.rb +28 -0
  129. data/test/test_app/config/initializers/backtrace_silencers.rb +7 -0
  130. data/test/test_app/config/initializers/inflections.rb +10 -0
  131. data/test/test_app/config/initializers/mime_types.rb +5 -0
  132. data/test/test_app/config/initializers/new_rails_defaults.rb +19 -0
  133. data/test/test_app/config/initializers/session_store.rb +15 -0
  134. data/test/test_app/config/locales/en.yml +5 -0
  135. data/test/test_app/config/routes.rb +43 -0
  136. data/test/test_app/doc/README_FOR_APP +2 -0
  137. data/test/test_app/public/404.html +30 -0
  138. data/test/test_app/public/422.html +30 -0
  139. data/test/test_app/public/500.html +30 -0
  140. data/test/test_app/public/favicon.ico +0 -0
  141. data/test/test_app/public/images/rails.png +0 -0
  142. data/test/test_app/public/index.html +275 -0
  143. data/test/test_app/public/javascripts/application.js +2 -0
  144. data/test/test_app/public/javascripts/controls.js +963 -0
  145. data/test/test_app/public/javascripts/dragdrop.js +973 -0
  146. data/test/test_app/public/javascripts/effects.js +1128 -0
  147. data/test/test_app/public/javascripts/prototype.js +4320 -0
  148. data/test/test_app/public/robots.txt +5 -0
  149. data/test/test_app/script/about +4 -0
  150. data/test/test_app/script/console +3 -0
  151. data/test/test_app/script/dbconsole +3 -0
  152. data/test/test_app/script/destroy +3 -0
  153. data/test/test_app/script/generate +3 -0
  154. data/test/test_app/script/performance/benchmarker +3 -0
  155. data/test/test_app/script/performance/profiler +3 -0
  156. data/test/test_app/script/plugin +3 -0
  157. data/test/test_app/script/runner +3 -0
  158. data/test/test_app/script/server +3 -0
  159. data/test/test_app/test/performance/browsing_test.rb +9 -0
  160. data/test/test_app/test/test_helper.rb +38 -0
  161. metadata +266 -0
@@ -0,0 +1,33 @@
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
+ if ARGV[0].nil? || ARGV[0] == ""
25
+ puts "Missing rolename argument"
26
+ exit 2
27
+ end
28
+
29
+ if in_role?(ARGV[0].to_sym)
30
+ exit 0
31
+ else
32
+ exit 1
33
+ end
@@ -0,0 +1,43 @@
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
+ require "#{File.dirname(__FILE__)}/../lib/utils"
23
+
24
+ include Ec2onrails::RolesHelper
25
+
26
+
27
+ APP_ROOT = "/mnt/app/current"
28
+ RAILS_ENV = Ec2onrails::Utils.rails_env
29
+
30
+ #reload configs to pick up any new changes
31
+ # Just a thought... do we really need to load god files from inside the app? They could just be installed via install_system_files, that would be simpler
32
+ Dir.glob("/etc/god/*.god") + Dir.glob("/mnt/app/current/config/god/*.god").each do |f|
33
+ sudo "god load '#{f}'"
34
+ end
35
+
36
+
37
+ roles.keys.each do |role|
38
+ if in_role? role
39
+ start role
40
+ else
41
+ stop role
42
+ end
43
+ end
@@ -0,0 +1,56 @@
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
+
22
+ # This script installs system files that are included in the deployed Rails
23
+ # app. The files are copied from RAILS_ROOT/config/ec2onrails/system_files
24
+ # (i.e. /mnt/app/current/config/ec2onrails/system_files) to the root
25
+ # of the system, keeping their subdirectories.
26
+ #
27
+ # For example, to replace /etc/nginx/nginx.conf with your own customized
28
+ # version, you would create config/ec2onrails/system_files/nginx/nginx.conf
29
+ # in your rails app.
30
+ #
31
+ # If the files being installed are overwriting an existing system file,
32
+ # that file will be renamed to <original_name>.ec2onrails_backup
33
+ #
34
+ # An optional "manifest" file can be created that lists all the files.
35
+ #
36
+ # Before installing the files, or when given the --uninstall flag, it will
37
+ # remove the previously installed files. This only works if the previously
38
+ # installed files had a manifest.
39
+ #
40
+ # This replaces the ec2onrails:server:deploy_files Capistrano task that
41
+ # uploaded a set of files from anywhere on the deployer's computer. The
42
+ # advantage of doing it this way is that the system config files are deployed
43
+ # with the app, and kept with the app in your revision control system where
44
+ # they can be branched, etc., together with the app, then uninstalled cleanly
45
+ # when redeploying a different version of the app.
46
+
47
+ require "fileutils"
48
+ require "#{File.dirname(__FILE__)}/../lib/system_files_helper"
49
+ require "#{File.dirname(__FILE__)}/../lib/utils"
50
+
51
+
52
+ SYSTEM_FILES_SRC = "config/ec2onrails/system_files"
53
+
54
+ app_dir = ARGV[0] || "/mnt/app/current"
55
+
56
+ Ec2onrails::SystemFilesHelper.new.install_system_files(File.join(app_dir, SYSTEM_FILES_SRC))
@@ -0,0 +1,346 @@
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.collect{|r| r.first if r.last.include?('127.0.0.1')}.compact
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?(:web) ? `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?(:web) && local_roles.include?(:memcache)
59
+ mem_opt ||= 0.35 if local_roles.include?(:web) || local_roles.include?(:memcache)
60
+ mem_opt ||= 0.70 #if only db, lets use a 70% ratio
61
+
62
+ orig_free_mem = (`free -m` =~ /buffers\/cache:\s+\d+\s+(\d+)/; $1).to_i rescue 1024
63
+
64
+ #TODO: take into account memcached settings if memcached is running on this server
65
+ ruby_mem_reserved = num_ruby_instances * 180
66
+ if orig_free_mem > 4098 && (orig_free_mem * mem_opt + ruby_mem_reserved) * 1.25 < orig_free_mem
67
+ mem_opt *= 1.5
68
+ end
69
+ free_mem = (orig_free_mem * mem_opt).to_i
70
+
71
+ @mysql = Ec2onrails::MysqlHelper.new
72
+
73
+ # result = sudo("god start db_primary")
74
+ result = sudo("/etc/init.d/mysql start")
75
+ if result
76
+ puts <<-MSG
77
+ ****** WOOPS ******
78
+ mysql was not successfully started up.
79
+ Not optimizing mysql config file
80
+ MSG
81
+ exit 1
82
+ end
83
+ # sleep(10) #make sure the db has some time to start up!
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_concurrency 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_concurrency: only turn on thread concurrency if
137
+ # there are some spare 'cores' available
138
+ if avail_cores < 2
139
+ configs['mysqld'].delete('thread_concurrency')
140
+ elsif
141
+ # Try number of CPU's*2 for thread_concurrency
142
+ configs['mysqld']['thread_concurrency'] = (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 = sudo("god stop db_primary")
274
+ # not using god because it doesn't wait for the script to finish
275
+ # before returning....
276
+ # result = sudo("god stop db_primary")
277
+ result = sudo("/etc/init.d/mysql stop")
278
+ clean_stop = true
279
+ if result
280
+ config_file_loc += ".optimized"
281
+ clean_stop = false
282
+ puts <<-MSG
283
+ ****** WOOPS ******
284
+ mysql was not successfully shut down, so we dare not
285
+ update the config file (it can cause problems with the
286
+ ib_logfile cache). We have saved the new config file at
287
+ #{config_file_loc}
288
+ in case you still want to use it in place of
289
+ #{DEFAULT_CONFIG_LOC}
290
+ MSG
291
+ else
292
+ puts <<-MSG
293
+ cleanly shutdown mysql. Replacing config file:
294
+ #{config_file_loc}
295
+ The original config file can be found here:
296
+ #{DEFAULT_CONFIG_LOC}.pre_optimized
297
+
298
+ Starting mysql...
299
+ MSG
300
+ end
301
+
302
+ configs.save(config_file_loc)
303
+
304
+ config_file = File.read(DEFAULT_CONFIG_LOC)
305
+ File.open(DEFAULT_CONFIG_LOC, 'w') do |file|
306
+ file << <<-MSG
307
+ # This file is generated by '#{__FILE__}'
308
+ # Based upon the default '#{DEFAULT_CONFIG_LOC}'
309
+ # which is now saved at '#{DEFAULT_CONFIG_LOC}.pre_optimized'
310
+
311
+ # See file for comments:
312
+ # #{__FILE__}
313
+ #
314
+ #
315
+
316
+ MSG
317
+ file << config_file
318
+ end
319
+
320
+ if clean_stop
321
+ #before we can start, we need to move the old cache files...
322
+ old_logs = []
323
+ Dir.glob("/mnt/mysql_data/ib_logfile*").each do |f|
324
+ FileUtils.mv(f, f + "_old")
325
+ old_logs << "#{f}_old"
326
+ end
327
+ puts <<-MSG
328
+ Moving the old mysql ib logfiles because we might have changed the
329
+ default logfile cache size. If mysql startups up successfully,
330
+ these files can be removed:
331
+ #{old_logs.join("\n ")}
332
+ MSG
333
+
334
+
335
+ # result = sudo("god start db_primary")
336
+ #using init.d instead of god, because god doesn't wait for the command to finish...
337
+ #it fires it off and then checks it in x seconds
338
+ result = sudo("/etc/init.d/mysql start")
339
+ if result
340
+ puts <<-MSG
341
+ ****** WOOPS ******
342
+ mysql was not successfully started up.
343
+ Check syslog, as the culprit will be logged there.
344
+ MSG
345
+ end
346
+ end