j-cap-recipes 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5196ab571ff7a96196e7a0e7e7a1a740e05e2663
4
- data.tar.gz: 51f805237ce8285ebf7f1e4cd3be1d9b6affd735
3
+ metadata.gz: c956d783e3a5cc9334124c40c98d4b6d6d8f0486
4
+ data.tar.gz: ff287d34e0858d0d7c4dcbed8fcacb2dc461b763
5
5
  SHA512:
6
- metadata.gz: b8ac7e2590969083d4a2db7712e174f9a155659b25f354a81d814def2e96d78184b35e47b0f4601ba1224bf3477361b2ebae49d80407f5c48c1e180118d860af
7
- data.tar.gz: f212f9d82f1006094fc70379a52bbefcbccf521da8224f8a914fa7b2ab339666b4664615638ae4ebd2d7b78deddd3553c9233318e5fe65ca403ec3d7941efba0
6
+ metadata.gz: 916166821d2a71988cebb4cd810cc9be36361ffebf238df3f8a0e1bffb3a28e4095ae639f4d78b91de8ada42b9236bb5709480ae1e5b09bd5fed8a6bb2340023
7
+ data.tar.gz: 05e3a11711e3988095ef5f43a94da4cc3366b414e96f21914b0006b806c3536a0cc7ac1d653d0e4ef58e3f165a9228dba47d9a3f44694799f929c77d8cc7feef
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## Changelog
2
2
 
3
+ ## 0.0.17
4
+
5
+ * Added ability to define what the env to download or upload for database backups. If it differs from the rails env.
6
+ * Added task to update the version with current revision and deploy timestamp.
7
+
3
8
  ## 0.0.16
4
9
 
5
10
  * Added handy task to remote edit settings file.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- j-cap-recipes (0.0.16)
4
+ j-cap-recipes (0.0.17)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -51,7 +51,9 @@ Also you need to include rake tasks in your `Rakefile`:
51
51
  `cap production db:create`
52
52
  `cap production db:backup`
53
53
  `cap production db:dump_download`
54
+ `cap production db:dump_download[rails_env]` - Download the file that located in `<current>/db/backups/<application>_<rails_env>_latest.dump`
54
55
  `cap production db:dump_upload`
56
+ `cap production db:dump_upload[rails_env]` - Upload the file `<current>/db/backups/<application>_<rails_env>_latest.dump` to remote host
55
57
  `cap production db:restore`
56
58
 
57
59
 
@@ -107,6 +109,15 @@ There are tasks available:
107
109
  - `cap staging config:settings:get` Download the remote config file to local one
108
110
  - `cap staging config:settings:edit` Direct editing of the settings file
109
111
 
112
+ ### Update VERSION file with build number
113
+
114
+ Task `deploy:update_version` adds to end of line the `:build_number` string. You may set it to:
115
+
116
+ ```ruby
117
+ set :build_number, proc { [fetch(:current_revision), Time.now.strftime("%Y%m%d"), ].compact.join('-') }
118
+ set :version_filename, 'VERSION'
119
+ ```
120
+
110
121
  ### Git
111
122
 
112
123
  First should add `require 'j-cap-recipes/git'` to `Capfile`.
@@ -40,29 +40,34 @@ namespace :db do
40
40
  end
41
41
  end
42
42
 
43
- desc 'Downlaod to local machine the latest backup'
44
- task :dump_download do
43
+ desc 'Download to local machine the latest backup'
44
+ task :dump_download, :env_name do |task, args|
45
45
  on primary fetch(:migration_role) do
46
46
  within release_path do
47
47
  FileUtils.mkdir_p 'db/backups'
48
- bakup_file = "db/backups/#{fetch(:application)}_#{fetch(:rails_env).to_s}_latest.dump"
49
- download! "#{release_path}/#{bakup_file}", bakup_file
48
+ env_name = args[:env_name] || fetch(:rails_env).to_s
49
+ database_config_content = read_remote_database_config
50
+ database_name = JCap::Recipes::Util.database_name(env_name, database_config_content)
51
+ backup_file = "db/backups/#{database_name}_latest.dump"
52
+ download! "#{release_path}/#{backup_file}", backup_file
50
53
  end
51
54
  end
52
55
  end
53
56
 
54
- desc 'Downlaod to local machine the latest backup'
55
- task :dump_upload do
57
+ desc 'Download to local machine the latest backup'
58
+ task :dump_upload, :env_name do |task, args|
56
59
  on primary fetch(:migration_role) do
57
60
  within release_path do
58
61
  FileUtils.mkdir_p 'db/backups'
59
- bakup_file = "db/backups/#{fetch(:application)}_#{fetch(:rails_env).to_s}_latest.dump"
60
- upload! bakup_file, "#{release_path}/#{bakup_file}"
62
+ env_name = args[:env_name] || fetch(:rails_env).to_s
63
+ database_config_content = read_remote_database_config
64
+ database_name = JCap::Recipes::Util.database_name(env_name, database_config_content)
65
+ backup_file = "db/backups/#{database_name}_latest.dump"
66
+ upload! backup_file, "#{release_path}/#{backup_file}"
61
67
  end
62
68
  end
63
69
  end
64
70
 
65
-
66
71
  end
67
72
 
68
73
  remote_file 'config/database.yml' => '/tmp/database.yml', roles: :app
@@ -94,3 +99,7 @@ file '/tmp/database.yml' do |t|
94
99
  f.puts config.result(binding)
95
100
  end
96
101
  end
102
+
103
+ def read_remote_database_config(path = 'config/database.yml')
104
+ capture :cat, path
105
+ end
@@ -13,56 +13,65 @@ namespace :db do
13
13
  backup_file = File.join(backup_dir, "#{database_name}_#{datestamp}.dump")
14
14
 
15
15
  #dump the backup and zip it up
16
- dump_command = "pg_dump #{database_name} -w -F c"
17
- dump_command += " -h #{config[:hostname]}" if config[:hostname].present?
18
- dump_command += " -U #{config[:username]}" if config[:username].present?
19
- dump_command += " > #{backup_file}"
16
+ dump_command = "#{postgres_password(config)} pg_dump #{database_name} -w -F c"
17
+ dump_command += postgres_auth_options(config)
18
+ dump_command += " > #{backup_file}"
20
19
 
21
20
  sh dump_command
22
21
 
23
22
  safe_ln backup_file, File.join(backup_dir, "#{database_name}_latest.dump")
24
-
25
- #send_to_amazon backup_file
26
- #remove the file on completion so we don't clog up our app
27
- #File.delete backup_file
28
23
  end
29
24
 
30
25
  desc 'PG restore from the last backup file'
31
- task restore: [:environment, :load_config] do
26
+ task restore: ['db:create', :environment, :load_config] do
32
27
  config = ActiveRecord::Base.connection_config
33
28
  database_name = ActiveRecord::Base.connection.current_database
34
29
  backup_dir = ENV['backup-path'] || Rails.root.join('db', 'backups')
35
30
  backup_file = File.join(backup_dir, "#{database_name}_latest.dump")
36
31
 
37
- Rake::Task['db:kill_postgres_connections'].invoke
38
- Rake::Task['db:drop'].invoke
39
- Rake::Task['db:create'].invoke
32
+ execute_task!('db:kill_postgres_connections')
33
+ execute_task!('db:drop')
34
+ execute_task!('db:create')
40
35
 
41
- restore_command = "pg_restore -d #{database_name} -F c -w #{backup_file}"
42
- restore_command = postgres_command_options restore_command, config
36
+ restore_command = "#{postgres_password(config)} pg_restore -d #{database_name} -F c -w #{backup_file}"
37
+ restore_command += postgres_auth_options(config)
38
+ restore_command += ' -O -c'
43
39
  sh "#{restore_command} || echo 'done'"
44
40
  end
45
41
 
46
42
  task :kill_postgres_connections => :environment do
47
- database_name = ActiveRecord::Base.connection.current_database
48
- command = <<EOF
49
- ps xa \
50
- | grep postgres: \
51
- | grep #{database_name} \
52
- | grep -v grep \
53
- | awk '{print $1}' \
54
- | xargs kill || true
55
- EOF
56
- sh command
43
+ db_name = ActiveRecord::Base.connection.current_database
44
+ pid_column_name = if ActiveRecord::Base.connection.send(:postgresql_version) > 90200
45
+ 'pid'
46
+ else
47
+ 'procpid'
48
+ end
49
+
50
+ kill_query = <<-QUERY
51
+ SELECT pg_terminate_backend(#{pid_column_name})
52
+ FROM pg_stat_activity
53
+ WHERE datname = '#{db_name}';
54
+ QUERY
55
+
56
+ begin
57
+ ActiveRecord::Base.connection.exec_query(kill_query)
58
+ rescue ActiveRecord::StatementInvalid => ex
59
+ puts "All connections to #{db_name} were killed successfully!"
60
+ puts "Database message: #{ex.message}"
61
+ end
57
62
  end
58
63
 
59
64
  end
60
65
 
61
- def postgres_command_options(str, config)
62
- str += " -h #{config[:hostname]}" if config[:hostname].present?
63
- str += " -U #{config[:username]}" if config[:username].present?
64
- str += ' -O -c'
65
- str
66
+ def postgres_password(config)
67
+ "PGPASSWORD='#{config[:password]}'" if config[:password].present?
68
+ end
69
+
70
+ def postgres_auth_options(config)
71
+ command_options = ''
72
+ command_options += " -h #{config[:hostname]}" if config[:hostname].present?
73
+ command_options += " -U #{config[:username]}" if config[:username].present?
74
+ command_options
66
75
  end
67
76
 
68
77
  #TODO: Use setting to get S3 credentials
@@ -73,3 +82,8 @@ def send_to_amazon(file_path)
73
82
  #push the file up
74
83
  AWS::S3::S3Object.store(file_name, File.open(file_path), bucket)
75
84
  end
85
+
86
+ def execute_task!(task_name)
87
+ Rake::Task[task_name].reenable
88
+ Rake::Task[task_name].invoke
89
+ end
@@ -5,4 +5,15 @@ namespace :deploy do
5
5
  execute :touch, current_path.join('tmp/restart.txt')
6
6
  end
7
7
  end
8
+
9
+ desc 'Store the deploy date and version to project file'
10
+ task :update_version do
11
+ on release_roles(:all) do
12
+ within release_path do
13
+ build_number = fetch(:build_number, [fetch(:current_revision), release_timestamp].compact.join('-'))
14
+ version_file_name = fetch(:version_filename, 'VERSION')
15
+ execute %{sed -i.bak 's/$/@#{build_number}/' #{release_path.join(version_file_name)}}
16
+ end
17
+ end
18
+ end
8
19
  end
@@ -1,5 +1,5 @@
1
1
  namespace :files do
2
- desc 'Downlaod remote shared files to local machine.'
2
+ desc 'Download remote shared files to local machine.'
3
3
  task :download, :path do |task, args|
4
4
 
5
5
  if args[:path].nil?
@@ -11,4 +11,15 @@ namespace :git do
11
11
  (puts "Created tag `#{tag_name}`")
12
12
  end
13
13
  end
14
+
15
+ desc 'Updates repository url'
16
+ task :update_repo_url do
17
+ on release_roles :all do
18
+ within repo_path do
19
+ with fetch(:git_environmental_variables) do
20
+ execute :git, :remote, 'set-url', 'origin', fetch(:repo_url)
21
+ end
22
+ end
23
+ end
24
+ end
14
25
  end
@@ -0,0 +1,12 @@
1
+ module JCap
2
+ module Recipes
3
+ module Util
4
+ class << self
5
+ def database_name(env_name, config_contents)
6
+ db_config = YAML.load(config_contents)
7
+ db_config[env_name][:database] || db_config[env_name]['database']
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module JCap
2
2
  module Recipes
3
- VERSION = '0.0.16'
3
+ VERSION = '0.0.17'
4
4
  end
5
5
  end
data/lib/j-cap-recipes.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require_relative 'j-cap-recipes/version'
2
+ require_relative 'j-cap-recipes/util'
2
3
  require_relative 'j-cap-recipes/railtie' if defined?(Rails)
@@ -19,7 +19,7 @@ module SSHKit
19
19
  host_url = String(host.hostname)
20
20
  host_url = '%s@%s' % [host.username, host_url] if host.username
21
21
  result = 'ssh %s -t "%s"' % [host_url, command(*args).to_command]
22
- output << Command.new(result)
22
+ output << Command.new(result, host: host)
23
23
  system(result)
24
24
  end
25
25
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: j-cap-recipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Nikitochkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-05 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,6 +107,7 @@ files:
107
107
  - lib/j-cap-recipes/tasks/templates/database.yml.erb
108
108
  - lib/j-cap-recipes/tasks/unicorn.rake
109
109
  - lib/j-cap-recipes/unicorn.rb
110
+ - lib/j-cap-recipes/util.rb
110
111
  - lib/j-cap-recipes/version.rb
111
112
  - lib/sshkit/backends/ssh_command.rb
112
113
  homepage: https://github.com/jetthoughts/j-cap-recipes
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  version: '0'
130
131
  requirements: []
131
132
  rubyforge_project:
132
- rubygems_version: 2.2.1
133
+ rubygems_version: 2.4.5
133
134
  signing_key:
134
135
  specification_version: 4
135
136
  summary: Capistrano 3 recipes for nginx, monit, rails log, setup, unicorn