hennk-hennk-ec2onrails 0.9.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/CHANGELOG +0 -0
  2. data/COPYING +339 -0
  3. data/Manifest +148 -0
  4. data/README.textile +214 -0
  5. data/Rakefile +36 -0
  6. data/TODO +5 -0
  7. data/ec2onrails.gemspec +44 -0
  8. data/lib/ec2onrails/capistrano_utils.rb +33 -0
  9. data/lib/ec2onrails/recipes.rb +460 -0
  10. data/lib/ec2onrails/version.rb +32 -0
  11. data/lib/ec2onrails.rb +20 -0
  12. data/server/build-ec2onrails.sh +44 -0
  13. data/server/files/etc/aliases +6 -0
  14. data/server/files/etc/aliases.db +0 -0
  15. data/server/files/etc/apache2/apache2.conf +295 -0
  16. data/server/files/etc/apache2/conf.d/app.proxy_cluster.conf +7 -0
  17. data/server/files/etc/apache2/conf.d/app.proxy_frontend.conf +10 -0
  18. data/server/files/etc/apache2/mods-available/proxy.conf +18 -0
  19. data/server/files/etc/apache2/sites-available/app.common +58 -0
  20. data/server/files/etc/apache2/sites-available/app.custom +0 -0
  21. data/server/files/etc/apache2/sites-available/default +17 -0
  22. data/server/files/etc/apache2/sites-available/default-ssl +18 -0
  23. data/server/files/etc/cron.d/backup_app_db_to_s3 +6 -0
  24. data/server/files/etc/cron.daily/app +5 -0
  25. data/server/files/etc/cron.daily/logrotate_post +27 -0
  26. data/server/files/etc/cron.hourly/app +5 -0
  27. data/server/files/etc/cron.monthly/app +5 -0
  28. data/server/files/etc/cron.weekly/app +5 -0
  29. data/server/files/etc/denyhosts.conf +628 -0
  30. data/server/files/etc/dpkg/dpkg.cfg +13 -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/set_roles +3 -0
  39. data/server/files/etc/logrotate.d/apache2 +13 -0
  40. data/server/files/etc/logrotate.d/mysql-server +23 -0
  41. data/server/files/etc/logrotate.d/rails +7 -0
  42. data/server/files/etc/memcached.conf +47 -0
  43. data/server/files/etc/mongrel_cluster/app.yml +9 -0
  44. data/server/files/etc/monit/app.monitrc +71 -0
  45. data/server/files/etc/monit/db_primary.monitrc +10 -0
  46. data/server/files/etc/monit/memcache.monitrc +8 -0
  47. data/server/files/etc/monit/monitrc +13 -0
  48. data/server/files/etc/monit/system.monitrc +15 -0
  49. data/server/files/etc/monit/web.monitrc +10 -0
  50. data/server/files/etc/motd.tail +13 -0
  51. data/server/files/etc/mysql/my.cnf +149 -0
  52. data/server/files/etc/postfix/main.cf +4 -0
  53. data/server/files/etc/rc0.d/K10mongrel +91 -0
  54. data/server/files/etc/rc1.d/K10mongrel +91 -0
  55. data/server/files/etc/rc2.d/S90mongrel +91 -0
  56. data/server/files/etc/rc3.d/S90mongrel +91 -0
  57. data/server/files/etc/rc4.d/S90mongrel +91 -0
  58. data/server/files/etc/rc5.d/S90mongrel +91 -0
  59. data/server/files/etc/rc6.d/K10mongrel +91 -0
  60. data/server/files/etc/rcS.d/S91ec2-first-startup +36 -0
  61. data/server/files/etc/rcS.d/S92ec2-every-startup +29 -0
  62. data/server/files/etc/rcS.d/S99set_roles +3 -0
  63. data/server/files/etc/ssh/sshd_config +94 -0
  64. data/server/files/etc/sudoers +24 -0
  65. data/server/files/etc/syslog.conf +69 -0
  66. data/server/files/usr/local/ec2onrails/COPYING +339 -0
  67. data/server/files/usr/local/ec2onrails/bin/archive_file.rb +44 -0
  68. data/server/files/usr/local/ec2onrails/bin/backup_app_db.rb +83 -0
  69. data/server/files/usr/local/ec2onrails/bin/backup_files.rb +51 -0
  70. data/server/files/usr/local/ec2onrails/bin/mongrel_start +8 -0
  71. data/server/files/usr/local/ec2onrails/bin/mongrel_stop +8 -0
  72. data/server/files/usr/local/ec2onrails/bin/rails_env +35 -0
  73. data/server/files/usr/local/ec2onrails/bin/rebundle.sh +70 -0
  74. data/server/files/usr/local/ec2onrails/bin/restore_app_db.rb +58 -0
  75. data/server/files/usr/local/ec2onrails/bin/restore_files.rb +59 -0
  76. data/server/files/usr/local/ec2onrails/bin/set_rails_env +40 -0
  77. data/server/files/usr/local/ec2onrails/bin/set_roles.rb +156 -0
  78. data/server/files/usr/local/ec2onrails/config +30 -0
  79. data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +101 -0
  80. data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +126 -0
  81. data/server/files/usr/local/ec2onrails/lib/utils.rb +16 -0
  82. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/get-hostname.sh +25 -0
  83. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs.sh +39 -0
  84. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key.sh +49 -0
  85. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc.sh +30 -0
  86. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir.sh +24 -0
  87. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup_credentials.sh +33 -0
  88. data/server/rakefile.rb +245 -0
  89. data/setup.rb +1585 -0
  90. data/test/autobench.conf +60 -0
  91. data/test/spec/lib/s3_helper_spec.rb +134 -0
  92. data/test/spec/lib/s3_old.yml +3 -0
  93. data/test/spec/test_files/test1 +0 -0
  94. data/test/spec/test_files/test2 +0 -0
  95. data/test/test_app/Capfile +3 -0
  96. data/test/test_app/README +182 -0
  97. data/test/test_app/Rakefile +10 -0
  98. data/test/test_app/app/controllers/application.rb +7 -0
  99. data/test/test_app/app/controllers/db_fast_controller.rb +6 -0
  100. data/test/test_app/app/controllers/fast_controller.rb +5 -0
  101. data/test/test_app/app/controllers/slow_controller.rb +6 -0
  102. data/test/test_app/app/controllers/very_slow_controller.rb +6 -0
  103. data/test/test_app/app/helpers/application_helper.rb +3 -0
  104. data/test/test_app/app/helpers/db_fast_helper.rb +2 -0
  105. data/test/test_app/app/helpers/fast_helper.rb +2 -0
  106. data/test/test_app/app/helpers/slow_helper.rb +2 -0
  107. data/test/test_app/app/helpers/very_slow_helper.rb +2 -0
  108. data/test/test_app/config/boot.rb +109 -0
  109. data/test/test_app/config/database.yml +36 -0
  110. data/test/test_app/config/deploy.rb +21 -0
  111. data/test/test_app/config/environment.rb +60 -0
  112. data/test/test_app/config/environments/development.rb +21 -0
  113. data/test/test_app/config/environments/production.rb +18 -0
  114. data/test/test_app/config/environments/test.rb +19 -0
  115. data/test/test_app/config/routes.rb +27 -0
  116. data/test/test_app/db/schema.rb +7 -0
  117. data/test/test_app/doc/README_FOR_APP +2 -0
  118. data/test/test_app/public/404.html +30 -0
  119. data/test/test_app/public/500.html +30 -0
  120. data/test/test_app/public/dispatch.cgi +10 -0
  121. data/test/test_app/public/dispatch.fcgi +24 -0
  122. data/test/test_app/public/dispatch.rb +10 -0
  123. data/test/test_app/public/favicon.ico +0 -0
  124. data/test/test_app/public/images/rails.png +0 -0
  125. data/test/test_app/public/javascripts/application.js +2 -0
  126. data/test/test_app/public/javascripts/controls.js +963 -0
  127. data/test/test_app/public/javascripts/dragdrop.js +972 -0
  128. data/test/test_app/public/javascripts/effects.js +1120 -0
  129. data/test/test_app/public/javascripts/prototype.js +4225 -0
  130. data/test/test_app/public/robots.txt +1 -0
  131. data/test/test_app/script/about +3 -0
  132. data/test/test_app/script/breakpointer +3 -0
  133. data/test/test_app/script/console +3 -0
  134. data/test/test_app/script/destroy +3 -0
  135. data/test/test_app/script/generate +3 -0
  136. data/test/test_app/script/performance/benchmarker +3 -0
  137. data/test/test_app/script/performance/profiler +3 -0
  138. data/test/test_app/script/performance/request +3 -0
  139. data/test/test_app/script/plugin +3 -0
  140. data/test/test_app/script/process/inspector +3 -0
  141. data/test/test_app/script/process/reaper +3 -0
  142. data/test/test_app/script/process/spawner +3 -0
  143. data/test/test_app/script/runner +3 -0
  144. data/test/test_app/script/server +3 -0
  145. data/test/test_app/test/functional/db_fast_controller_test.rb +18 -0
  146. data/test/test_app/test/functional/fast_controller_test.rb +18 -0
  147. data/test/test_app/test/functional/slow_controller_test.rb +18 -0
  148. data/test/test_app/test/functional/very_slow_controller_test.rb +18 -0
  149. data/test/test_app/test/test_helper.rb +28 -0
  150. metadata +263 -0
@@ -0,0 +1,126 @@
1
+ # This file is part of EC2 on Rails.
2
+ # http://rubyforge.org/projects/ec2onrails/
3
+ #
4
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
5
+ #
6
+ # EC2 on Rails is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # EC2 on Rails is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'rubygems'
20
+ require 'aws/s3'
21
+ require 'yaml'
22
+ require 'erb'
23
+ require 'fileutils'
24
+ require "#{File.dirname(__FILE__)}/utils"
25
+
26
+ module Ec2onrails
27
+ class S3Helper
28
+
29
+ DEFAULT_CONFIG_FILE = "/mnt/app/current/config/s3.yml"
30
+
31
+ # make attributes available for specs
32
+ attr_accessor :bucket
33
+ attr_accessor :dir
34
+ attr_accessor :config_file
35
+ attr_accessor :rails_env
36
+ attr_accessor :aws_access_key
37
+ attr_accessor :aws_secret_access_key
38
+ attr_accessor :bucket
39
+
40
+ def initialize(bucket, dir, config_file = DEFAULT_CONFIG_FILE, rails_env = Utils.rails_env)
41
+ @dir = dir
42
+ @config_file = config_file
43
+ @rails_env = rails_env
44
+ load_s3_config
45
+ @bucket = bucket || "#{@bucket_base_name}-#{Ec2onrails::Utils.hostname}"
46
+ AWS::S3::Base.establish_connection!(:access_key_id => @aws_access_key, :secret_access_key => @aws_secret_access_key, :use_ssl => true)
47
+ end
48
+
49
+ def load_s3_config
50
+ if File.exists?(@config_file)
51
+ s3_config = YAML::load(ERB.new(File.read(@config_file)).result)
52
+
53
+ # try to load the section for the current RAILS_ENV
54
+ section = s3_config[@rails_env]
55
+ if section.nil?
56
+ # fall back to keys at the root of the tree
57
+ section = s3_config
58
+ end
59
+
60
+ @aws_access_key = section['aws_access_key']
61
+ @aws_secret_access_key = section['aws_secret_access_key']
62
+ @bucket_base_name = section['bucket_base_name']
63
+ else
64
+ if !File.exists?('/mnt/aws-config/config')
65
+ raise "Can't find either #{@config_file} or /mnt/aws-config/config"
66
+ end
67
+ @aws_access_key = get_bash_config('AWS_ACCESS_KEY_ID')
68
+ @aws_secret_access_key = get_bash_config('AWS_SECRET_ACCESS_KEY')
69
+ @bucket_base_name = get_bash_config('BUCKET_BASE_NAME')
70
+ end
71
+ end
72
+
73
+ def create_bucket
74
+ retries = 0
75
+ begin
76
+ AWS::S3::Bucket.find(@bucket)
77
+ rescue AWS::S3::NoSuchBucket
78
+ AWS::S3::Bucket.create(@bucket)
79
+ sleep 1 # If we try to use the bucket too quickly sometimes it's not found
80
+ retry if (retries += 1) < 15
81
+ end
82
+ end
83
+
84
+ def store_file(file)
85
+ create_bucket
86
+ AWS::S3::S3Object.store(s3_key(file), open(file), @bucket)
87
+ end
88
+
89
+ def retrieve_file(file)
90
+ key = s3_key(file)
91
+ AWS::S3::S3Object.find(key, @bucket)
92
+ open(file, 'w') do |f|
93
+ AWS::S3::S3Object.stream(key, @bucket) do |chunk|
94
+ f.write chunk
95
+ end
96
+ end
97
+ end
98
+
99
+ def list_keys(filename_prefix)
100
+ prefix = @dir ? "#{@dir}/#{filename_prefix}" : filename_prefix
101
+ AWS::S3::Bucket.objects(@bucket, :prefix => prefix).collect{|obj| obj.key}
102
+ end
103
+
104
+ def retrieve_files(filename_prefix, local_dir)
105
+ list_keys(filename_prefix).each do |k|
106
+ file = "#{local_dir}/#{File.basename(k)}"
107
+ retrieve_file(file)
108
+ end
109
+ end
110
+
111
+ def delete_files(filename_prefix)
112
+ list_keys(filename_prefix).each do |k|
113
+ AWS::S3::S3Object.delete(k, @bucket)
114
+ end
115
+ end
116
+
117
+ def s3_key(file)
118
+ @dir ? "#{@dir}/#{File.basename(file)}" : File.basename(file)
119
+ end
120
+
121
+ # load an env value from the shared config file
122
+ def get_bash_config(name)
123
+ `bash -c 'source /mnt/aws-config/config; echo $#{name}'`.strip
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,16 @@
1
+ module Ec2onrails
2
+ module Utils
3
+ def self.run(command)
4
+ result = system command
5
+ raise("error: #{$?}") unless result
6
+ end
7
+
8
+ def self.rails_env
9
+ `/usr/local/ec2onrails/bin/rails_env`.strip
10
+ end
11
+
12
+ def self.hostname
13
+ `hostname -s`.strip
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ #!/bin/sh
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
+ # Set the hostname to this instance's public hostname
22
+
23
+ hostname `curl http://169.254.169.254/latest/meta-data/public-hostname`
24
+ sudo monit reload
25
+
@@ -0,0 +1,39 @@
1
+ #!/bin/sh
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
+ make_dir() {
22
+ mkdir -p $1
23
+ if [ $2 ] ; then
24
+ chown -R $2 $1
25
+ fi
26
+ }
27
+
28
+ make_dir /mnt/app app:app
29
+
30
+ make_dir /mnt/log
31
+ make_dir /mnt/log/apache2 www-data:www-data
32
+ make_dir /mnt/log/fsck
33
+ make_dir /mnt/log/mysql mysql:mysql
34
+
35
+ make_dir /mnt/mysql_data mysql:mysql
36
+ make_dir /mnt/mysql_data/tmp mysql:mysql
37
+
38
+ make_dir /mnt/tmp
39
+ chmod 777 /mnt/tmp
@@ -0,0 +1,49 @@
1
+ #!/bin/sh
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
+ # Generate a new self-signed cert and key for https
23
+
24
+ echo "Generating default self-signed SSL cert and key..."
25
+
26
+ export RANDFILE=/tmp/randfile
27
+
28
+ cd /tmp
29
+ openssl genrsa -out server.key 1024
30
+ openssl req -new -key server.key -out server.csr <<END
31
+ CA
32
+ .
33
+ .
34
+ .
35
+ .
36
+ .
37
+ .
38
+ .
39
+ .
40
+
41
+ END
42
+ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
43
+
44
+ mkdir -p /etc/ec2onrails/ssl/cert
45
+ mkdir -p -m 700 /etc/ec2onrails/ssl/private
46
+ mv server.key /etc/ec2onrails/ssl/private/ec2onrails-default.key
47
+ mv server.crt /etc/ec2onrails/ssl/cert/ec2onrails-default.crt
48
+ rm $RANDFILE
49
+ rm server.csr
@@ -0,0 +1,30 @@
1
+ #!/bin/sh
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
+ chmod 777 /tmp
23
+
24
+ cp /etc/hosts /etc/hosts.original
25
+
26
+ #make sure the monitrc files have the correct permissions set
27
+ chmod 700 /etc/monit/*
28
+
29
+ # Rebuild postfix db
30
+ newaliases
@@ -0,0 +1,24 @@
1
+ #!/bin/sh
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
+ mkdir -p /mnt/mysql_data/tmp
23
+ chown -R mysql:mysql /mnt/mysql_data/
24
+ cp -rp /var/lib/mysql/mysql /mnt/mysql_data/
@@ -0,0 +1,33 @@
1
+ #!/bin/sh
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 file helps spread the root key so we can log in/deploy using the
22
+ # app user instead of root, but with the same ec2 key
23
+
24
+ #make sure we have the credentials
25
+ /etc/init.d/ec2-get-credentials
26
+
27
+ mkdir -p -m 700 /home/app/.ssh
28
+ cp /root/.ssh/authorized_keys /home/app/.ssh
29
+ chown -R app:app /home/app/.ssh
30
+
31
+ mkdir -p -m 700 /home/admin/.ssh
32
+ cp /root/.ssh/authorized_keys /home/admin/.ssh
33
+ chown -R admin:admin /home/admin/.ssh
@@ -0,0 +1,245 @@
1
+ # This file is part of EC2 on Rails.
2
+ # http://rubyforge.org/projects/ec2onrails/
3
+ #
4
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
5
+ #
6
+ # EC2 on Rails is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # EC2 on Rails is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+
20
+ # This script is meant to be run by build-ec2onrails.sh, which is run by
21
+ # Eric Hammond's Ubuntu build script: http://alestic.com/
22
+ # e.g.:
23
+ # bash /mnt/ec2ubuntu-build-ami --script /mnt/ec2onrails/server/build-ec2onrails.sh ...
24
+
25
+
26
+
27
+ require "rake/clean"
28
+ require 'yaml'
29
+ require 'erb'
30
+ require "#{File.dirname(__FILE__)}/../gem/lib/ec2onrails/version"
31
+
32
+ if `whoami`.strip != 'root'
33
+ raise "Sorry, this buildfile must be run as root."
34
+ end
35
+
36
+ @packages = %w(
37
+ adduser
38
+ apache2
39
+ aptitude
40
+ bison
41
+ ca-certificates
42
+ cron
43
+ curl
44
+ flex
45
+ gcc
46
+ git-core
47
+ irb
48
+ less
49
+ libdbm-ruby
50
+ libgdbm-ruby
51
+ libmysql-ruby
52
+ libopenssl-ruby
53
+ libreadline-ruby
54
+ libruby
55
+ libssl-dev
56
+ libyaml-ruby
57
+ libzlib-ruby
58
+ logrotate
59
+ make
60
+ mailx
61
+ memcached
62
+ mysql-client
63
+ mysql-server
64
+ nano
65
+ openssh-server
66
+ postfix
67
+ rdoc
68
+ ri
69
+ rsync
70
+ ruby
71
+ ruby1.8-dev
72
+ subversion
73
+ unzip
74
+ vim
75
+ wget
76
+ xfsprogs
77
+ )
78
+
79
+ # HACK: some packages just fail with apt-get but work fine
80
+ # with aptitude. These generally are virtual packages
81
+ @aptitude_packages = %w(
82
+ libmysqlclient-dev
83
+ )
84
+
85
+ # NOTE: the amazon-ec2 gem is now at github, maintained by
86
+ # grempe-amazon-ec2. Will move back to regular amazon-ec2
87
+ # gem if/when he cuts a new release with volume and snapshot
88
+ # support included
89
+ @rubygems = [
90
+ "grempe-amazon-ec2",
91
+ "aws-s3",
92
+ "memcache-client",
93
+ "mongrel",
94
+ "mongrel_cluster",
95
+ "optiflag",
96
+ "rails -v 2.2.2",
97
+ "rails -v 2.0.2",
98
+ "rake",
99
+ "archive-tar-minitar"
100
+ ]
101
+
102
+ @build_root = "/mnt/build"
103
+ @fs_dir = "#{@build_root}/ubuntu"
104
+
105
+ @version = [Ec2onrails::VERSION::MAJOR, Ec2onrails::VERSION::MINOR, Ec2onrails::VERSION::TINY].join('.')
106
+
107
+ task :default => :configure
108
+
109
+ desc "Removes all build files"
110
+ task :clean_all do |t|
111
+ rm_rf @build_root
112
+ end
113
+
114
+ desc "Use apt-get to install required packages inside the image's filesystem"
115
+ task :install_packages do |t|
116
+ unless_completed(t) do
117
+ ENV['DEBIAN_FRONTEND'] = 'noninteractive'
118
+ ENV['LANG'] = ''
119
+ run_chroot "apt-get install -y #{@packages.join(' ')}"
120
+ run_chroot "apt-get clean"
121
+
122
+ #lets run the aptitude-only packages
123
+ run_chroot "aptitude install -y #{@aptitude_packages.join(' ')}"
124
+ run_chroot "aptitude clean"
125
+ end
126
+ end
127
+
128
+ desc "Install required ruby gems inside the image's filesystem"
129
+ task :install_gems => [:install_packages] do |t|
130
+ unless_completed(t) do
131
+ run_chroot "sh -c 'cd /tmp && wget -q http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz && tar zxf rubygems-1.3.2.tgz'"
132
+ run_chroot "sh -c 'cd /tmp/rubygems-1.3.2 && ruby setup.rb'"
133
+ run_chroot "ln -sf /usr/bin/gem1.8 /usr/bin/gem"
134
+ run_chroot "gem update --system --no-rdoc --no-ri"
135
+ run_chroot "gem update --no-rdoc --no-ri"
136
+ run_chroot "gem sources -a http://gems.github.com"
137
+ @rubygems.each do |gem|
138
+ run_chroot "gem install #{gem} --no-rdoc --no-ri"
139
+ end
140
+ end
141
+ end
142
+
143
+ desc "Compile and install monit"
144
+ task :install_monit => [:install_packages] do |t|
145
+ unless_completed(t) do
146
+ run_chroot "sh -c 'cd /tmp && wget -q http://www.tildeslash.com/monit/dist/monit-4.10.1.tar.gz'"
147
+ run_chroot "sh -c 'cd /tmp && tar xzvf monit-4.10.1.tar.gz'"
148
+ run_chroot "sh -c 'cd /tmp/monit-4.10.1 && ./configure --sysconfdir=/etc/monit/ --localstatedir=/var/run && make && make install'"
149
+ end
150
+ end
151
+
152
+ desc "Configure the image"
153
+ task :configure => [:install_gems, :install_monit] do |t|
154
+ unless_completed(t) do
155
+ sh("cp -r files/* #{@fs_dir}")
156
+ sh("find #{@fs_dir} -type d -name .svn | xargs rm -rf")
157
+
158
+ replace("#{@fs_dir}/etc/motd.tail", /!!VERSION!!/, "Version #{@version}")
159
+
160
+ run_chroot "a2enmod deflate"
161
+ run_chroot "a2enmod proxy_balancer"
162
+ run_chroot "a2enmod proxy_http"
163
+ run_chroot "a2enmod rewrite"
164
+
165
+ run_chroot "/usr/sbin/adduser --gecos ',,,' --disabled-password app"
166
+ run_chroot "/usr/sbin/adduser --gecos ',,,' --disabled-password admin"
167
+ run_chroot "/usr/sbin/adduser admin adm"
168
+ run_chroot "/usr/sbin/addgroup sudoers"
169
+
170
+ run "echo '. /usr/local/ec2onrails/config' >> #{@fs_dir}/root/.bashrc"
171
+ run "echo '. /usr/local/ec2onrails/config' >> #{@fs_dir}/home/app/.bashrc"
172
+ run "echo '. /usr/local/ec2onrails/config' >> #{@fs_dir}/home/admin/.bashrc"
173
+
174
+ %w(apache2 mysql auth.log daemon.log kern.log mail.err mail.info mail.log mail.warn syslog user.log).each do |f|
175
+ rm_rf "#{@fs_dir}/var/log/#{f}"
176
+ run_chroot "ln -sf /mnt/log/#{f} /var/log/#{f}"
177
+ end
178
+
179
+ run "touch #{@fs_dir}/ec2onrails-first-boot"
180
+
181
+ # TODO find out the most correct solution here, there seems to be a bug in
182
+ # both feisty and gutsy where the dhcp daemon runs as dhcp but the dir
183
+ # that it tries to write to is owned by root and not writable by others.
184
+ run_chroot "chown -R dhcp /var/lib/dhcp3"
185
+ end
186
+ end
187
+
188
+ desc "This task is for deploying the contents of /files to a running server image to test config file changes without rebuilding."
189
+ task :deploy_files do |t|
190
+ raise "need 'key' and 'host' env vars defined" unless ENV['key'] && ENV['host']
191
+ run "rsync -rlvzcC --rsh='ssh -l root -i #{ENV['key']}' files/ #{ENV['host']}:/"
192
+ end
193
+
194
+ ##################
195
+
196
+ # Execute a given block and touch a stampfile. The block won't be run if the stampfile exists.
197
+ def unless_completed(task, &proc)
198
+ stampfile = "#{@build_root}/#{task.name}.completed"
199
+ unless File.exists?(stampfile)
200
+ yield
201
+ touch stampfile
202
+ end
203
+ end
204
+
205
+ def run_chroot(command, ignore_error = false)
206
+ run "chroot '#{@fs_dir}' #{command}", ignore_error
207
+ end
208
+
209
+ def run(command, ignore_error = false)
210
+ puts "*** #{command}"
211
+ result = system command
212
+ raise("error: #{$?}") unless result || ignore_error
213
+ end
214
+
215
+ # def mount(type, mount_point)
216
+ # unless mounted?(mount_point)
217
+ # puts
218
+ # puts "********** Mounting #{type} on #{mount_point}..."
219
+ # puts
220
+ # run "mount -t #{type} none #{mount_point}"
221
+ # end
222
+ # end
223
+ #
224
+ # def mounted?(mount_point)
225
+ # mount_point_regex = mount_point.gsub(/\//, "\\/")
226
+ # `mount`.select {|line| line.match(/#{mount_point_regex}/) }.any?
227
+ # end
228
+
229
+ def replace_line(file, newline, linenum)
230
+ contents = File.open(file, 'r').readlines
231
+ contents[linenum - 1] = newline
232
+ File.open(file, 'w') do |f|
233
+ contents.each {|line| f << line}
234
+ end
235
+ end
236
+
237
+ def replace(file, pattern, text)
238
+ contents = File.open(file, 'r').readlines
239
+ contents.each do |line|
240
+ line.gsub!(pattern, text)
241
+ end
242
+ File.open(file, 'w') do |f|
243
+ contents.each {|line| f << line}
244
+ end
245
+ end