biola_deploy 0.1.0

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.
@@ -0,0 +1,8 @@
1
+ require 'biola_deploy'
2
+ require 'rails'
3
+ require 'active_record'
4
+
5
+ module BiolaDeploy
6
+ class Engine < Rails::Engine
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module BiolaDeploy
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,3 @@
1
+ module BiolaDeploy
2
+ require 'biola_deploy/engine'
3
+ end
@@ -0,0 +1,228 @@
1
+ require 'whiskey_disk/helpers'
2
+ require 'fileutils'
3
+ require 'uri'
4
+
5
+ namespace :deploy do
6
+ desc 'Create a new user for the app'
7
+ task :create_user => :environment do
8
+ unless app_user_exists?
9
+ puts "creating user #{app_username}..."
10
+ system "sudo useradd --system --shell /bin/false --home #{app_dir} #{app_username}"
11
+ end
12
+ end
13
+
14
+ desc 'Put the logs in /var/log and sym link to ./logs'
15
+ task :prepare_logs => :environment do
16
+ puts 'preparing logs...'
17
+
18
+ # ensure the base log directory for our apps exists
19
+ system("sudo mkdir #{base_log_dir}") unless Dir.exists?(base_log_dir)
20
+
21
+ # create the app's log directory under the base directory and make the app user the owner
22
+ system "sudo mkdir #{app_log_dir}" unless Dir.exists?(app_log_dir)
23
+ chown_to_app_user app_log_dir
24
+
25
+ # symbolic link the system log directory to the app log directory
26
+ Dir.chdir(app_dir) do
27
+ system('rm -rf log') if Dir.exists?('log')
28
+ FileUtils.ln_s(app_log_dir, 'log')
29
+ end
30
+ end
31
+
32
+ desc 'Create the ./tmp directory with the correct permissions'
33
+ task :prepare_tmp => :environment do
34
+ puts 'preparing /tmp directory...'
35
+ Dir.chdir(app_dir) do
36
+ Dir.mkdir('tmp') unless Dir.exists?('tmp')
37
+ make_globally_writable 'tmp'
38
+ chown_to_app_user 'public'
39
+ end
40
+ end
41
+
42
+ desc 'Set permissions on ./public'
43
+ task :prepare_public => :environment do
44
+ Dir.chdir(app_dir) do
45
+ make_globally_writable 'public'
46
+ chown_to_app_user 'public'
47
+ end
48
+ end
49
+
50
+ desc 'Configure Nginx to serve the app'
51
+ task :setup_webserver => :environment do
52
+ File.open(webserver_app_config_file, 'w') do |file|
53
+ config = if server?
54
+ "
55
+ server {
56
+ listen 80;
57
+ listen 443 ssl;
58
+ server_name #{app_url.host};
59
+ server_name #{app_url.host.split('.').first}.#{hostname};
60
+
61
+ index index.html index.htm;
62
+
63
+ location / {
64
+ root #{app_public_dir};
65
+ passenger_set_cgi_param HTTP_X_FORWARDED_PROTO $scheme;
66
+ passenger_enabled on;
67
+ }
68
+ }
69
+ "
70
+ else
71
+ "
72
+ location #{app_url.path} {
73
+ passenger_enabled on;
74
+ passenger_base_uri #{app_url.path};
75
+ passenger_user #{app_username};
76
+ passenger_group #{app_username};
77
+ passenger_set_cgi_param HTTP_X_FORWARDED_PROTO $scheme;
78
+ }
79
+ "
80
+ end
81
+
82
+ file.write config
83
+ end
84
+
85
+ # sym link the location path
86
+ if location?
87
+ FileUtils.ln_s(File.join(app_dir, 'public'), File.join(web_root_dir, app_url.path))
88
+ end
89
+ end
90
+
91
+ desc 'Create the database'
92
+ task :create_db do
93
+ host = db_config['host'] || 'localhost'
94
+
95
+ if db_config['adapter'] =~ /mysql/
96
+ client = Mysql2::Client.new(:host => host, :username => ENV['DB_CREATE_USERNAME'], :password => ENV['DB_CREATE_PASSWORD'])
97
+ db = db_config['database']
98
+ user = db_config['username']
99
+ password = db_config['password']
100
+
101
+ client.query 'USE mysql'
102
+ client.query "CREATE DATABASE #{db}"
103
+ client.query "GRANT ALL PRIVILEGES ON #{db}.* TO '#{user}'@'%' IDENTIFIED BY '#{password}'"
104
+ end
105
+ end
106
+
107
+ desc 'Run the database migrations'
108
+ task :migrate_db => :environment do
109
+ puts 'migrating the database...'
110
+ Rake::Task['db:migrate'].invoke
111
+ end
112
+
113
+ desc 'TODO'
114
+ task :setup_solr do
115
+ # TODO
116
+ end
117
+
118
+ desc 'Reindex Solr if it exists'
119
+ task :reindex_solr do
120
+ if Rake::Task.task_defined? 'sunspot:reindex'
121
+ puts 'reindexing Solr...'
122
+ Rake::Task['sunspot:reindex'].invoke
123
+ end
124
+ end
125
+
126
+ desc 'Precompile the asset pipeline in Rails apps 3.1 and above'
127
+ task :precompile_assets do
128
+ if Rake::Task.task_defined? 'assets:precompile'
129
+ puts 'precompiling assets...'
130
+ Rake::Task['assets:precompile'].invoke
131
+ end
132
+ end
133
+
134
+ desc 'Trigger a Passenger restart'
135
+ task :restart_app => :environment do
136
+ restart_file = 'tmp/restart.txt'
137
+
138
+ puts 'restarting the app...'
139
+ Dir.chdir(app_dir) do
140
+ make_globally_writable restart_file if File.exists? restart_file
141
+ FileUtils.touch restart_file
142
+ end
143
+ end
144
+
145
+ desc 'Run all setup tasks'
146
+ task :post_setup => [:create_db, :create_user, :prepare_logs, :prepare_tmp, :prepare_public, :setup_webserver, :setup_solr]
147
+
148
+ desc 'Run all deployment tasks'
149
+ task :post_deploy => [:migrate_db, :reindex_solr, :precompile_assets, :restart_app]
150
+
151
+ private
152
+ def web_root_dir
153
+ ENV['WEB_ROOT']
154
+ end
155
+
156
+ def app_dir
157
+ Rails.root.to_s
158
+ end
159
+
160
+ def app_public_dir
161
+ File.join(app_dir, 'public')
162
+ end
163
+
164
+ def app_url
165
+ URI.parse ENV['APP_URL']
166
+ end
167
+ alias :app_uri :app_url
168
+
169
+ def url_type
170
+ if app_url.path.to_s.gsub('/', '') == ''
171
+ :server
172
+ else
173
+ :location
174
+ end
175
+ end
176
+
177
+ def server?
178
+ url_type == :server
179
+ end
180
+
181
+ def location?
182
+ url_type == :location
183
+ end
184
+
185
+ def app_name
186
+ ENV['APP_NAME']
187
+ end
188
+
189
+ def app_username
190
+ app_name
191
+ end
192
+
193
+ def hostname
194
+ system 'hostname --long'
195
+ end
196
+
197
+ def webserver_app_config_file
198
+ "/etc/nginx/sites/#{app_name}.#{url_type}.conf"
199
+ end
200
+
201
+ def base_log_dir
202
+ '/var/log/rails'
203
+ end
204
+
205
+ def app_log_dir
206
+ File.join(base_log_dir, app_name)
207
+ end
208
+
209
+ def app_user_exists?
210
+ !!system("id #{app_username}")
211
+ end
212
+
213
+ def chown_to_app_user(file)
214
+ system "sudo chown -R #{app_username}:#{app_username} #{file}"
215
+ end
216
+
217
+ def make_globally_writable(file)
218
+ permissions = File.directory?(file) ? 777 : 666
219
+ system "sudo chmod #{permissions} #{file}"
220
+ end
221
+
222
+ def db_config
223
+ return @db_config unless @db_config.nil?
224
+
225
+ database_yml_path = File.join(changes_file_root, 'config', 'database.yml')
226
+ @db_config = YAML::load(File.open(database_yml_path))[ENV['RAILS_ENV']]
227
+ end
228
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: biola_deploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Crownoble
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: whiskey_disk
16
+ requirement: &78832420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.24
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *78832420
25
+ description: Designed to automate deployment of Biola's applications to it's servers
26
+ using whiskey_disk friendly rake tasks
27
+ email: adam.crownoble@biola.edu
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/biola_deploy.rb
33
+ - lib/biola_deploy/version.rb
34
+ - lib/biola_deploy/engine.rb
35
+ - lib/tasks/deploy.rake
36
+ homepage: https://bitbucket.org/biola/biola-deploy
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.16
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Easy deployment for Biola applications
60
+ test_files: []