dreamcat4-moonshadow 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/LICENSE +165 -0
  2. data/app_generators/moonshine/moonshine_generator.rb +154 -0
  3. data/app_generators/moonshine/templates/Capfile +3 -0
  4. data/app_generators/moonshine/templates/rails/deploy.rb +3 -0
  5. data/app_generators/moonshine/templates/rails/gems.yml +1 -0
  6. data/app_generators/moonshine/templates/rails/manifest.rb +55 -0
  7. data/app_generators/moonshine/templates/rails/moonshine.rake +83 -0
  8. data/app_generators/moonshine/templates/rails/moonshine.yml +43 -0
  9. data/app_generators/moonshine/templates/readme.templates +5 -0
  10. data/app_generators/moonshine_plugin/USAGE +8 -0
  11. data/app_generators/moonshine_plugin/moonshine_plugin_generator.rb +39 -0
  12. data/app_generators/moonshine_plugin/templates/README.rdoc +14 -0
  13. data/app_generators/moonshine_plugin/templates/init.rb +3 -0
  14. data/app_generators/moonshine_plugin/templates/plugin.rb +19 -0
  15. data/app_generators/moonshine_plugin/templates/spec.rb +24 -0
  16. data/app_generators/moonshine_plugin/templates/spec_helper.rb +8 -0
  17. data/bin/moonshine +17 -0
  18. data/bin/moonshine_plugin +17 -0
  19. data/lib/moonshine.rb +7 -0
  20. data/lib/moonshine/bootstrap/bootstrap.mri.sh +21 -0
  21. data/lib/moonshine/bootstrap/bootstrap.ree.sh +34 -0
  22. data/lib/moonshine/capistrano.rb +242 -0
  23. data/lib/moonshine/manifest.rb +151 -0
  24. data/lib/moonshine/manifest/rails.rb +54 -0
  25. data/lib/moonshine/manifest/rails/apache.rb +99 -0
  26. data/lib/moonshine/manifest/rails/apt_gems.yml +32 -0
  27. data/lib/moonshine/manifest/rails/mysql.rb +79 -0
  28. data/lib/moonshine/manifest/rails/os.rb +115 -0
  29. data/lib/moonshine/manifest/rails/passenger.rb +93 -0
  30. data/lib/moonshine/manifest/rails/postgresql.rb +83 -0
  31. data/lib/moonshine/manifest/rails/rails.rb +237 -0
  32. data/lib/moonshine/manifest/rails/sqlite3.rb +8 -0
  33. data/lib/moonshine/manifest/rails/templates/innodb.cnf.erb +6 -0
  34. data/lib/moonshine/manifest/rails/templates/logrotate.conf.erb +15 -0
  35. data/lib/moonshine/manifest/rails/templates/moonshine.cnf.erb +63 -0
  36. data/lib/moonshine/manifest/rails/templates/passenger.conf.erb +106 -0
  37. data/lib/moonshine/manifest/rails/templates/passenger.vhost.erb +273 -0
  38. data/lib/moonshine/manifest/rails/templates/pg_hba.conf.erb +83 -0
  39. data/lib/moonshine/manifest/rails/templates/postgresql.conf.erb +493 -0
  40. data/lib/moonshine/manifest/rails/templates/unattended_upgrades.erb +18 -0
  41. data/lib/moonshine_setup_manifest.rb +39 -0
  42. metadata +135 -0
@@ -0,0 +1,93 @@
1
+ module Moonshine::Manifest::Rails::Passenger
2
+ # Install the passenger gem
3
+ def passenger_gem
4
+ configure(:passenger => {})
5
+ package "passenger", :ensure => (configuration[:passenger][:version] || :latest), :provider => :gem
6
+ end
7
+
8
+ # Build, install, and enable the passenger apache module. Please see the
9
+ # <tt>passenger.conf.erb</tt> template for passenger configuration options.
10
+ def passenger_apache_module
11
+ # Install Apache2 developer library
12
+ package "apache2-threaded-dev", :ensure => :installed
13
+
14
+ file "/usr/local/src", :ensure => :directory
15
+
16
+ exec "symlink_passenger",
17
+ :command => 'ln -nfs `passenger-config --root` /usr/local/src/passenger',
18
+ :unless => 'ls -al /usr/local/src/passenger | grep `passenger-config --root`',
19
+ :require => [
20
+ package("passenger"),
21
+ file("/usr/local/src")
22
+ ]
23
+
24
+ # Build Passenger from source
25
+ exec "build_passenger",
26
+ :cwd => configuration[:passenger][:path],
27
+ :command => '/usr/bin/ruby -S rake clean apache2',
28
+ :unless => "ls `passenger-config --root`/ext/apache2/mod_passenger.so",
29
+ :require => [
30
+ package("passenger"),
31
+ package("apache2-mpm-worker"),
32
+ package("apache2-threaded-dev"),
33
+ exec('symlink_passenger')
34
+ ]
35
+
36
+ load_template = "LoadModule passenger_module #{configuration[:passenger][:path]}/ext/apache2/mod_passenger.so"
37
+
38
+ file '/etc/apache2/mods-available/passenger.load',
39
+ :ensure => :present,
40
+ :content => load_template,
41
+ :require => [exec("build_passenger")],
42
+ :notify => service("apache2"),
43
+ :alias => "passenger_load"
44
+
45
+ file '/etc/apache2/mods-available/passenger.conf',
46
+ :ensure => :present,
47
+ :content => template(File.join(File.dirname(__FILE__), 'templates', 'passenger.conf.erb')),
48
+ :require => [exec("build_passenger")],
49
+ :notify => service("apache2"),
50
+ :alias => "passenger_conf"
51
+
52
+ a2enmod 'passenger', :require => [exec("build_passenger"), file("passenger_conf"), file("passenger_load")]
53
+ end
54
+
55
+ # Creates and enables a vhost configuration named after your application.
56
+ # Also ensures that the <tt>000-default</tt> vhost is disabled.
57
+ def passenger_site
58
+ file "/etc/apache2/sites-available/#{configuration[:application]}",
59
+ :ensure => :present,
60
+ :content => template(File.join(File.dirname(__FILE__), 'templates', 'passenger.vhost.erb')),
61
+ :notify => service("apache2"),
62
+ :alias => "passenger_vhost",
63
+ :require => exec("a2enmod passenger")
64
+
65
+ a2dissite '000-default', :require => file("passenger_vhost")
66
+ a2ensite configuration[:application], :require => file("passenger_vhost")
67
+ end
68
+
69
+ def passenger_configure_gem_path
70
+ configure(:passenger => {})
71
+ return configuration[:passenger][:path] if configuration[:passenger][:path]
72
+ version = begin
73
+ configuration[:passenger][:version] || Gem::SourceIndex.from_installed_gems.find_name("passenger").last.version.to_s
74
+ rescue
75
+ `gem install passenger --no-ri --no-rdoc`
76
+ `passenger-config --version`.chomp
77
+ end
78
+ configure(:passenger => { :path => "#{Gem.dir}/gems/passenger-#{version}" })
79
+ end
80
+
81
+ private
82
+
83
+ def passenger_config_boolean(key)
84
+ if key.nil?
85
+ nil
86
+ elsif key == 'Off' || (!!key) == false
87
+ 'Off'
88
+ else
89
+ 'On'
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,83 @@
1
+ # To use PostgreSQL, add the following recipes calls to your manifest:
2
+ #
3
+ # recipe :postgresql_server, :postgresql_gem, :postgresql_user, :postgresql_database
4
+ #
5
+ module Moonshine::Manifest::Rails::Postgresql
6
+
7
+ # Installs <tt>postgresql</tt> from apt and enables the <tt>postgresql</tt>
8
+ # service.
9
+ def postgresql_server
10
+ package 'postgresql', :ensure => :installed
11
+ package 'postgresql-client', :ensure => :installed
12
+ package 'postgresql-contrib', :ensure => :installed
13
+ package 'libpq-dev', :ensure => :installed
14
+ service 'postgresql-8.3',
15
+ :ensure => :running,
16
+ :hasstatus => true,
17
+ :require => [
18
+ package('postgresql'),
19
+ package('postgres'),
20
+ package('pg')
21
+ ]
22
+ #ensure the postgresql key is present on the configuration hash
23
+ configure(:postgresql => {})
24
+ file '/etc/postgresql/8.3/main/pg_hba.conf',
25
+ :ensure => :present,
26
+ :content => template(File.join(File.dirname(__FILE__), 'templates', 'pg_hba.conf.erb')),
27
+ :require => package('postgresql'),
28
+ :mode => '600',
29
+ :owner => 'postgres',
30
+ :group => 'postgres',
31
+ :notify => service('postgresql-8.3')
32
+ file '/etc/postgresql/8.3/main/postgresql.conf',
33
+ :ensure => :present,
34
+ :content => template(File.join(File.dirname(__FILE__), 'templates', 'postgresql.conf.erb')),
35
+ :require => package('postgresql'),
36
+ :mode => '600',
37
+ :owner => 'postgres',
38
+ :group => 'postgres',
39
+ :notify => service('postgresql-8.3')
40
+ end
41
+
42
+ # Install the <tt>pg</tt> rubygem and dependencies
43
+ def postgresql_gem
44
+ gem 'pg'
45
+ gem 'postgres'
46
+ end
47
+
48
+ # Grant the database user specified in the current <tt>database_environment</tt>
49
+ # permisson to access the database with the supplied password
50
+ def postgresql_user
51
+ psql "CREATE USER #{database_environment[:username]} WITH PASSWORD '#{database_environment[:password]}'",
52
+ :alias => "postgresql_user",
53
+ :unless => psql_query('\\\\du') + "| grep #{database_environment[:username]}",
54
+ :require => service('postgresql-8.3')
55
+ end
56
+
57
+ # Create the database from the current <tt>database_environment</tt>
58
+ def postgresql_database
59
+ exec "postgresql_database",
60
+ :command => "/usr/bin/createdb -O #{database_environment[:username]} #{database_environment[:database]}",
61
+ :unless => "/usr/bin/psql -l | grep #{database_environment[:database]}",
62
+ :user => 'postgres',
63
+ :require => exec('postgresql_user'),
64
+ :before => exec('rake tasks'),
65
+ :notify => exec('rails_bootstrap')
66
+ end
67
+
68
+ private
69
+
70
+ def psql(query, options = {})
71
+ name = "psql #{query}"
72
+ hash = {
73
+ :command => psql_query(query),
74
+ :user => 'postgres'
75
+ }.merge(options)
76
+ exec(name,hash)
77
+ end
78
+
79
+ def psql_query(sql)
80
+ "/usr/bin/psql -c \"#{sql}\""
81
+ end
82
+
83
+ end
@@ -0,0 +1,237 @@
1
+ module Moonshine::Manifest::Rails::Rails
2
+
3
+ # Attempt to bootstrap your application. Calls <tt>rake moonshine:bootstrap</tt>
4
+ # which runs:
5
+ #
6
+ # rake db:schema:load (if db/schema.rb exists)
7
+ # rake db:migrate (if db/migrate exists)
8
+ #
9
+ # We then run a task to load bootstrap fixtures from <tt>db/bootstrap</tt>
10
+ # (if it exists). These fixtures may be created with the included
11
+ # <tt>moonshine:db:bootstrap:dump</tt> rake task.
12
+ #
13
+ # rake moonshine:db:bootstrap
14
+ #
15
+ # We then run the following task:
16
+ #
17
+ # rake moonshine:app:bootstrap
18
+ #
19
+ # The <tt>moonshine:app:bootstrap</tt> task does nothing by default. If
20
+ # you'd like to have your application preform any logic on it's first deploy,
21
+ # overwrite this task in your <tt>Rakefile</tt>:
22
+ #
23
+ # namespace :moonshine do
24
+ # namespace :app do
25
+ # desc "Overwrite this task in your app if you have any bootstrap tasks that need to be run"
26
+ # task :bootstrap do
27
+ # #
28
+ # end
29
+ # end
30
+ # end
31
+ #
32
+ # All of this assumes one things. That your application can run 'rake
33
+ # environment' with an empty database. Please ensure your application can do
34
+ # so!
35
+ def rails_bootstrap
36
+ rake 'moonshine:bootstrap',
37
+ :alias => 'rails_bootstrap',
38
+ :refreshonly => true,
39
+ :before => exec('rake db:migrate')
40
+ end
41
+
42
+ # Runs Rails migrations. These are run on each deploy to ensure consistency!
43
+ # No more 500s when you forget to <tt>cap deploy:migrations</tt>
44
+ def rails_migrations
45
+ rake 'db:migrate'
46
+ end
47
+
48
+ # Rotates the logs for this rails app
49
+ def rails_logrotate
50
+ configure(:rails_logrotate => {})
51
+ logrotate("#{configuration[:deploy_to]}/shared/log/*.log", {
52
+ :options => configuration[:rails_logrotate][:options] || %w(daily missingok compress delaycompress sharedscripts),
53
+ :postrotate => configuration[:rails_logrotate][:postrotate] || "touch #{configuration[:deploy_to]}/current/tmp/restart.txt"
54
+ })
55
+ file "/etc/logrotate.d/#{configuration[:deploy_to].gsub('/','')}sharedlog.conf", :ensure => :absent
56
+ end
57
+
58
+ # This task ensures Rake is installed and that <tt>rake environment</tt>
59
+ # executes without error in your <tt>rails_root</tt>.
60
+ def rails_rake_environment
61
+ package 'rake', :provider => :gem, :ensure => :installed
62
+ file '/var/log/moonshine_rake.log',
63
+ :ensure => :present,
64
+ :owner => configuration[:user],
65
+ :group => configuration[:group] || configuration[:user],
66
+ :mode => '775',
67
+ :content => ' '
68
+ exec 'rake tasks',
69
+ :command => 'rake environment >> /var/log/moonshine_rake.log 2>&1',
70
+ :user => configuration[:user],
71
+ :cwd => rails_root,
72
+ :environment => "RAILS_ENV=#{ENV['RAILS_ENV']}",
73
+ :require => [
74
+ exec('rails_gems'),
75
+ package('rake'),
76
+ file('/var/log/moonshine_rake.log')
77
+ ]
78
+ end
79
+
80
+ # Automatically install all gems needed specified in the array at
81
+ # <tt>configuration[:gems]</tt>. This loads gems from
82
+ # <tt>config/gems.yml</tt>, which can be generated from by running
83
+ # <tt>rake moonshine:gems</tt> locally.
84
+ def rails_gems
85
+ gemrc = {
86
+ :verbose => true,
87
+ :gem => "--no-ri --no-rdoc",
88
+ :update_sources => true,
89
+ :sources => [
90
+ 'http://gems.rubyforge.org',
91
+ 'http://gems.github.com',
92
+ ]
93
+ }
94
+ gemrc.merge!(configuration[:rubygems]) if configuration[:rubygems]
95
+ file '/etc/gemrc',
96
+ :ensure => :present,
97
+ :mode => '744',
98
+ :owner => 'root',
99
+ :group => 'root',
100
+ :content => gemrc.to_yaml
101
+ #stub for dependencies
102
+ exec 'rails_gems', :command => 'true'
103
+ return unless configuration[:gems]
104
+ configuration[:gems].each do |gem|
105
+ gem.delete(:source) if gem[:source] && gem[:source] =~ /gems.github.com/
106
+ gem(gem[:name], {
107
+ :version => gem[:version],
108
+ :source => gem[:source]
109
+ })
110
+ end
111
+ end
112
+
113
+ # Essentially replicates the deploy:setup command from capistrano, but sets
114
+ # up permissions correctly.
115
+ def rails_directories
116
+ deploy_to_array = configuration[:deploy_to].split('/').split('/')
117
+ deploy_to_array.each_with_index do |dir, index|
118
+ next if index == 0 || index >= (deploy_to_array.size-1)
119
+ file '/'+deploy_to_array[1..index].join('/'), :ensure => :directory
120
+ end
121
+ dirs = [
122
+ "#{configuration[:deploy_to]}",
123
+ "#{configuration[:deploy_to]}/shared",
124
+ "#{configuration[:deploy_to]}/releases"
125
+ ]
126
+ if configuration[:shared_children].is_a?(Array)
127
+ shared_dirs = configuration[:shared_children].map { |d| "#{configuration[:deploy_to]}/shared/#{d}" }
128
+ dirs += shared_dirs
129
+ end
130
+ if configuration[:app_symlinks].is_a?(Array)
131
+ dirs += ["#{configuration[:deploy_to]}/shared/public"]
132
+ symlink_dirs = configuration[:app_symlinks].map { |d| "#{configuration[:deploy_to]}/shared/public/#{d}" }
133
+ dirs += symlink_dirs
134
+ end
135
+ dirs.each do |dir|
136
+ file dir,
137
+ :ensure => :directory,
138
+ :owner => configuration[:user],
139
+ :group => configuration[:group] || configuration[:user],
140
+ :mode => '775'
141
+ end
142
+ end
143
+
144
+ private
145
+ # Creates package("#{name}") with <tt>:provider</tt> set to <tt>:gem</tt>.
146
+ # The given <tt>options[:version]</tt> requirement is tweaked to ensure
147
+ # gems aren't reinstalled on each run. <tt>options[:source]</tt> does what
148
+ # you'd expect, as well.
149
+ #
150
+ # === Gem Package Dependencies
151
+ #
152
+ # System dependencies are loaded if any exist for the gem or any of it's
153
+ # gem dependencies in <tt>apt_gems.yml</tt>. For example, calling
154
+ # <tt>gem('webrat')</tt> knows to install <tt>libxml2-dev</tt> and
155
+ # <tt>libxslt1-dev</tt> because those are defined as dependencies for
156
+ # <tt>nokogiri</tt>.
157
+ #
158
+ # To define system dependencies not include in moonshine:
159
+ #
160
+ # class UrManifest < ShadowPuppet::Manifest
161
+ # configure(:apt_gems => {
162
+ # :fakegem => [
163
+ # 'package1',
164
+ # 'package2
165
+ # ]
166
+ # })
167
+ # end
168
+ #
169
+ # If you were then to require the installation of <tt>fakegem</tt> <strong>
170
+ # or any gem that depends on <tt>fakegem</tt></strong>, <tt>package1</tt>
171
+ # and <tt>package2</tt> would be installed first via apt.
172
+ def gem(name, options = {})
173
+ hash = {
174
+ :provider => :gem,
175
+ :before => exec('rails_gems'),
176
+ :require => file('/etc/gemrc')
177
+ }
178
+ hash.merge!(:source => options[:source]) if options[:source]
179
+ #fixup the version required
180
+ exact_dep = Gem::Dependency.new(name, options[:version] || '>0')
181
+ matches = Gem.source_index.search(exact_dep)
182
+ installed_spec = matches.first
183
+ if installed_spec
184
+ #it's already loaded, let's just specify that we want it installed
185
+ hash.merge!(:ensure => :installed)
186
+ else
187
+ if options[:version]
188
+ #if it's not installed and version specified, we require that version
189
+ hash.merge!(:ensure => options[:version])
190
+ else
191
+ #otherwise we don't care
192
+ hash.merge!(:ensure => :installed)
193
+ end
194
+ hash = append_system_dependecies(exact_dep, hash)
195
+ end
196
+ hash.delete(:version)
197
+ package(name, hash)
198
+ end
199
+
200
+ def append_system_dependecies(exact_dep, hash) #:nodoc:
201
+ #fixup the requires key to be an array
202
+ if hash[:require] && !hash[:require].is_a?(Array)
203
+ hash[:require] = [hash[:require]]
204
+ end
205
+ hash[:require] = [] unless hash[:require]
206
+ # load this gems' dependencies. we don't create packages for em, we just
207
+ # check them against the system dependency map
208
+ specs = Gem::SpecFetcher.fetcher.fetch exact_dep
209
+ spec = specs.first.first
210
+ deps = spec.dependencies
211
+ deps << exact_dep
212
+ deps.each do |dep|
213
+ (configuration[:apt_gems][dep.name.to_sym] || []).each do |apt|
214
+ package apt, :ensure => :installed
215
+ hash[:require] << package(apt)
216
+ end
217
+ end
218
+ hash.delete(:require) if hash[:require] == []
219
+ hash
220
+ rescue
221
+ hash
222
+ end
223
+
224
+ # Creates exec("rake #name") that runs in <tt>rails root</tt> of the rails
225
+ # app, with RAILS_ENV properly set
226
+ def rake(name, options = {})
227
+ exec("rake #{name}", {
228
+ :command => "rake #{name} >> /var/log/moonshine_rake.log 2>&1",
229
+ :user => configuration[:user],
230
+ :cwd => rails_root,
231
+ :environment => "RAILS_ENV=#{ENV['RAILS_ENV']}",
232
+ :require => exec('rake tasks')
233
+ }.merge(options)
234
+ )
235
+ end
236
+
237
+ end
@@ -0,0 +1,8 @@
1
+ module Moonshine::Manifest::Rails::Sqlite3
2
+
3
+ # Install the sqlite3 gem and it's dependencies
4
+ def sqlite3
5
+ gem 'sqlite3-ruby', :before => exec('rails_gems')
6
+ end
7
+
8
+ end
@@ -0,0 +1,6 @@
1
+ # set the log_file_size before mysqld is started to ensure logs are created
2
+ # correctly the first time
3
+ [mysqld]
4
+ innodb_log_buffer_size = <%= configuration[:mysql][:innodb_log_buffer_size] || '64M' %>
5
+ innodb_log_file_size = <%= configuration[:mysql][:innodb_log_file_size] || '80M' %>
6
+ innodb_log_files_in_group = <%= configuration[:mysql][:innodb_log_files_in_group] || '3' %>
@@ -0,0 +1,15 @@
1
+ <%= log_or_glob %> {
2
+ <% options[:options].each do |o| %>
3
+ <%= o %>
4
+ <% end %>
5
+ <% unless options[:postrotate].nil? %>
6
+ postrotate
7
+ <%= options[:postrotate] %>
8
+ endscript
9
+ <% end %>
10
+ <% unless options[:prerotate].nil? %>
11
+ prerotate
12
+ <%= options[:prerotate] %>
13
+ endscript
14
+ <% end %>
15
+ }