capobvious 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in Capobvious.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1 @@
1
+ require 'capistrano/ext/capobvious'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ VERSION = "0.0.1"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "capobvious"
7
+ s.version = VERSION
8
+ s.authors = ["Dmitry Gruzd"]
9
+ s.email = ["donotsendhere@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Cap recipes}
12
+ s.description = %q{Capfile that we use every day}
13
+
14
+ s.rubyforge_project = "capobvious"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,299 @@
1
+ $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
2
+ require "rvm/capistrano"
3
+
4
+ Capistrano::Configuration.instance.load do
5
+ rvmrc = "rvm use #{rvm_ruby_string}"
6
+ set :rvm_type, :user
7
+
8
+ default_run_options[:pty] = true
9
+ ssh_options[:forward_agent] = true
10
+
11
+ unless exists?(:rails_env)
12
+ set :rails_env, "production"
13
+ end
14
+ unless exists?(:dbconf)
15
+ set :dbconf, "database.yml"
16
+ end
17
+
18
+ config = YAML::load(File.open("config/#{dbconf}"))
19
+ adapter = config[rails_env]["adapter"]
20
+ database = config[rails_env]["database"]
21
+ db_username = config[rails_env]["username"]
22
+ db_password = config[rails_env]["password"]
23
+
24
+ set :local_folder_path, "tmp/backup"
25
+ set :timestamp, Time.new.to_i.to_s
26
+ set :db_file_name, "#{database}-#{timestamp}.sql"
27
+ set :db_archive_ext, "7z"
28
+
29
+
30
+ after "deploy:symlink", "auto:run"
31
+ after "deploy:setup", "db:create", "nginx:conf"
32
+ namespace :auto do
33
+ task :run do
34
+ if exists?(:assets) && fetch(:assets) == true
35
+ assets.precompile
36
+ end
37
+ create.files
38
+ if exists?(:sphinx) && fetch(:sphinx) == true
39
+ sphinx.symlink
40
+ end
41
+ bundle.install
42
+ if exists?(:auto_migrate) && fetch(:auto_migrate) == true
43
+ deploy.migrate
44
+ end
45
+ end
46
+ end
47
+ namespace :create do
48
+ task :files do
49
+ create.rvmrc
50
+ if fetch(:dbconf) != 'database.yml'
51
+ create.database_yml
52
+ end
53
+ end
54
+ task :database_yml do
55
+ run "ln -sfv #{current_path}/config/#{dbconf} #{current_path}/config/database.yml"
56
+ end
57
+ desc "Create .rvmrc & files"
58
+ task :rvmrc do
59
+ put rvmrc, "#{current_path}/.rvmrc"
60
+ end
61
+ end
62
+
63
+
64
+ namespace :db do
65
+ task :create do
66
+ run "echo \"create user #{db_username} with password '#{db_password}';\" | #{sudo} -u postgres psql"
67
+ run "echo \"create database #{database} owner #{db_username};\" | #{sudo} -u postgres psql"
68
+ end
69
+ task :seed do
70
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} db:seed"
71
+ end
72
+ task :reset do
73
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} db:reset"
74
+ end
75
+ task :import do
76
+ file_name = "#{db_file_name}.#{db_archive_ext}"
77
+ file_path = "#{local_folder_path}/#{file_name}"
78
+ system "cd #{local_folder_path} && 7z x #{file_name}"
79
+ system "echo \"drop database #{database}\" | sudo -u postgres psql"
80
+ system "echo \"create database #{database} owner #{db_username};\" | sudo -u postgres psql"
81
+ system "sudo -u postgres psql #{database} < #{local_folder_path}/#{db_file_name}"
82
+ system "rm -v #{local_folder_path}/#{db_file_name}"
83
+ end
84
+ task :pg_import do
85
+ backup.db
86
+ db.import
87
+ end
88
+ end
89
+
90
+ #==== PRIKHA-START ====
91
+ desc "Run custom task usage: cap run_rake_task TASK=patch:project_category"
92
+ task :run_rake_task do
93
+ if ENV.has_key?('TASK')
94
+ p "running rake task: #{ENV['TASK']}"
95
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} #{ENV['TASK']}"
96
+ else
97
+ puts 'Please specify correct task: cap run_rake_task TASK= some_task'
98
+ end
99
+ end
100
+ #==== PRIKHA-END ====
101
+
102
+ namespace :backup do
103
+ desc "Backup a database"
104
+ task :db do
105
+ file_name = fetch(:db_file_name)
106
+ archive_ext = fetch(:db_archive_ext)
107
+ dump_file_path = "#{shared_path}/backup/#{file_name}"
108
+ output_file = "#{dump_file_path}.#{archive_ext}"
109
+ require 'yaml'
110
+ run "mkdir -p #{shared_path}/backup"
111
+ if adapter == "postgresql"
112
+ run "pg_dump -U #{db_username} #{database} > #{dump_file_path}"
113
+ run "cd #{shared_path} && 7z a #{output_file} #{dump_file_path} && rm #{dump_file_path}"
114
+ else
115
+ puts "Cannot backup, adapter #{adapter} is not implemented for backup yet"
116
+ end
117
+ system "mkdir -p #{local_folder_path}"
118
+ download(output_file, "#{local_folder_path}/#{file_name}.#{archive_ext}")
119
+ end
120
+ desc "Backup public/system folder"
121
+ task :sys do
122
+ file_name = "#{application}-system-#{timestamp}.7z"
123
+ file_path = "#{shared_path}/backup/#{file_name}"
124
+ run "7z a #{file_path} #{shared_path}/system"
125
+ download(file_path, "#{local_folder_path}/#{file_name}")
126
+ end
127
+ desc "Clean backup folder"
128
+ task :clean do
129
+ run "rm -rfv #{shared_path}/backup/*"
130
+ end
131
+ end
132
+ if exists?(:backup_db) && fetch(:backup_db) == true
133
+ before "deploy:update", "backup:db"
134
+ end
135
+ if exists?(:backup_sys) && fetch(:backup_sys) == true
136
+ before "deploy:update", "backup:sys"
137
+ end
138
+
139
+ namespace :assets do
140
+ desc "Assets precompile"
141
+ task :precompile do
142
+ if exists?(:assets) && fetch(:assets) == true
143
+ system("bundle exec rake assets:precompile && cd public && tar czf assets.tar.gz assets/")
144
+ upload("public/assets.tar.gz","#{current_path}/public/assets.tar.gz")
145
+ system("rm public/assets.tar.gz && rm -rf tmp/assets && mv public/assets tmp/assets")
146
+ run("cd #{current_path}/public && rm -rf assets/ && tar xzf assets.tar.gz && rm assets.tar.gz")
147
+ else
148
+ puts "assets is disabled in config/deploy.rb to enable add line set :assets, true"
149
+ end
150
+ end
151
+ end
152
+
153
+ namespace :nginx do
154
+ task :restart do
155
+ run "#{sudo} /etc/init.d/nginx restart"
156
+ end
157
+ task :reload do
158
+ run "#{sudo} /etc/init.d/nginx reload"
159
+ end
160
+ task :start do
161
+ run "#{sudo} /etc/init.d/nginx start"
162
+ end
163
+ task :stop do
164
+ run "#{sudo} /etc/init.d/nginx stop"
165
+ end
166
+ desc "Add app nginx conf to server"
167
+ task :conf do
168
+ default_nginx_template = <<-EOF
169
+ server {
170
+ listen 80;
171
+ server_name #{server_name};
172
+ root #{current_path}/public;
173
+ location / {
174
+ try_files $uri @unicorn;
175
+ }
176
+ location @unicorn {
177
+ proxy_set_header Client-Ip $remote_addr;
178
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
179
+ proxy_set_header Host $host;
180
+ proxy_pass http://unix:#{shared_path}/pids/unicorn.sock;
181
+ }
182
+ }
183
+ EOF
184
+ if exists?(:server_redirect)
185
+ server_redirect = fetch(:server_redirect)#.split(" ")
186
+ redirect_template = <<-RED
187
+ server {
188
+ server_name #{server_redirect};
189
+ rewrite ^(.*)$ http://#{server_name}$1 permanent;
190
+ }
191
+ RED
192
+ default_nginx_template += redirect_template
193
+ end
194
+
195
+ puts default_nginx_template
196
+
197
+ if exists?(:server_name)
198
+ #location = fetch(:template_dir, "config") + '/nginx.conf.erb'
199
+ #template = File.file?(location) ? File.read(location) : default_nginx_template
200
+ config = ERB.new(default_nginx_template)
201
+ # puts config.result
202
+ put config.result(binding), "#{shared_path}/nginx.conf"
203
+ run "#{sudo} ln -sfv #{shared_path}/nginx.conf /etc/nginx/sites-enabled/#{application}"
204
+ else
205
+ puts "Aborting because :server_name is not setted in deploy.rb"
206
+ end
207
+ end
208
+ desc "Del nginx config"
209
+ task :delconf do
210
+ run "#{sudo} rm -v /etc/nginx/sites-enabled/#{application}"
211
+ end
212
+ end
213
+ after "nginx:conf", "nginx:reload"
214
+ after "nginx:delconf", "nginx:reload"
215
+
216
+
217
+ namespace :bundle do
218
+ desc "Run bundle install"
219
+ task :install do
220
+ without = ['development','test','production']-[rails_env]
221
+ run "cd #{current_path} && bundle install --without #{without.join(" ")}"
222
+ end
223
+ end
224
+
225
+ namespace :log do
226
+ desc "tail -f production.log"
227
+ task :tail do
228
+ stream("tail -f -n 0 #{current_path}/log/production.log")
229
+ end
230
+ end
231
+
232
+
233
+
234
+
235
+ namespace :install do
236
+ desc "Install apt-nyaa"
237
+ task :aptnyaa do
238
+ run "#{sudo} apt-get --assume-yes install wget > /dev/null 2>&1 && cd /usr/bin/ && #{sudo} wget -Nq https://raw.github.com/nyaa/UbuntuScript/master/apt-nyaa && #{sudo} chmod +x apt-nyaa"
239
+ end
240
+ end
241
+
242
+
243
+ namespace :sphinx do
244
+ desc "Rebuild indexes"
245
+ task :rebuild, :roles => :app, :except => {:no_release => true} do
246
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} ts:rebuild"
247
+ end
248
+ desc "Sphinx start"
249
+ task :start, :roles => :app, :except => {:no_release => true} do
250
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} ts:start"
251
+ end
252
+ desc "Sphinx stop"
253
+ task :stop, :roles => :app, :except => {:no_release => true} do
254
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} ts:stop"
255
+ end
256
+ desc "Sphinx configure"
257
+ task :stop, :roles => :app, :except => {:no_release => true} do
258
+ run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} ts:conf"
259
+ end
260
+ desc "Re-establish symlinks"
261
+ task :symlink do
262
+ if exists?(:sphinx) && fetch(:sphinx) == true
263
+ run "mkdir -pv #{shared_path}/sphinx"
264
+ run "rm -rf #{release_path}/db/sphinx && ln -sfv #{shared_path}/sphinx #{release_path}/db/sphinx"
265
+ run "ln -sfv #{shared_path}/sphinx/#{rails_env}.sphinx.conf #{release_path}/config/#{rails_env}.sphinx.conf"
266
+ else
267
+ puts "sphinx is disabled in config/deploy.rb to enable add line set :sphinx, true"
268
+ end
269
+ end
270
+ end
271
+
272
+
273
+
274
+ set :unicorn_conf, "#{current_path}/config/unicorn.rb"
275
+ set :unicorn_pid, "#{shared_path}/pids/unicorn.pid"
276
+ namespace :unicorn do
277
+ desc "start unicorn"
278
+ task :start do
279
+ run "cd #{current_path} && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
280
+ end
281
+ desc "stop unicorn"
282
+ #task :stop, :roles => :app, :except => {:no_release => true} do
283
+ task :stop do
284
+ run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
285
+ end
286
+ desc "restart unicorn"
287
+ task :restart do
288
+ puts "Restarting unicorn"
289
+ unicorn.stop
290
+ unicorn.start
291
+ end
292
+ end
293
+
294
+ namespace :deploy do
295
+ task :restart do
296
+ unicorn.restart
297
+ end
298
+ end
299
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capobvious
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dmitry Gruzd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Capfile that we use every day
15
+ email:
16
+ - donotsendhere@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README
24
+ - Rakefile
25
+ - capobvious.gemspec
26
+ - lib/capistrano/ext/capobvious.rb
27
+ homepage: ''
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project: capobvious
47
+ rubygems_version: 1.8.15
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Cap recipes
51
+ test_files: []