copy_db_from_prod 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63d0b8b5714cc8f1d676813327671ba4da1caa8d
4
+ data.tar.gz: f9ec3a35d3dc57fd6354a61d0d75c93089b81537
5
+ SHA512:
6
+ metadata.gz: 5d00d39a1aff1e76c59ea40f71402b9a8d75548c9c7e4efc3e5ce7c5aa2b4657d5df3a8d66b8d3e4bbfc5f41d18db2b278e75b936bd8e39668a9918df97ca7eb
7
+ data.tar.gz: 21580ba4fe202ceef232837e64bd40c859fc4faeb889b466f9dc4377fb319dad838a253c4ea9eae6430eb1c5d2da8db2ba112d34f22e79ceb186bc15b1831fbd
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'copy_db_from_prod'
4
+
5
+ def echo_wrong_args
6
+ puts("Wrong args")
7
+ end
8
+
9
+ def echo_help
10
+ puts("copy_db_from_prod -h <host> -u <user> -p <path>
11
+ <user> - default 'deploy'
12
+ <path> - default '/projects/insales3', without / at the end")
13
+ end
14
+
15
+ def schema_only(params)
16
+ p = CopyDbFromProd.new(params)
17
+ p.load_schema
18
+ puts('Load schema success')
19
+ end
20
+
21
+ class Wraper
22
+ attr_accessor :shell_args, :host
23
+
24
+ def initialize(args)
25
+ @shell_args = args
26
+ shell_args.each_with_index do |e, i|
27
+ if '-h' == e
28
+ @host = shell_args[i + 1] if '-' != shell_args[i + 1][0]
29
+ elsif '-u' == e
30
+ @user = shell_args[i + 1] if '-' != shell_args[i + 1][0]
31
+ elsif '-p' == e
32
+ @path = shell_args[i + 1] if '-' != shell_args[i + 1][0]
33
+ end
34
+ end
35
+ end
36
+
37
+ def include?(one)
38
+ @exec ||= shell_args.include?(one)
39
+ shell_args.include?(one)
40
+ end
41
+
42
+ def exec?
43
+ @exec
44
+ end
45
+
46
+ def user
47
+ @user ||= 'deploy'
48
+ end
49
+
50
+ def path
51
+ @path ||= '/projects/insales3'
52
+ end
53
+ end
54
+
55
+ w = Wraper.new(ARGV)
56
+
57
+ if ARGV.empty?
58
+ echo_help
59
+ else
60
+ schema_only(w) if w.include?('--schema-only')
61
+ CopyDbFromProd.hi if w.include?('--hi')
62
+ echo_help if w.include?('--help')
63
+ echo_wrong_args if !w.exec?
64
+ end
@@ -0,0 +1,57 @@
1
+ require 'net/ssh'
2
+ require 'yaml'
3
+ require 'pg'
4
+
5
+ class CopyDbFromProd
6
+ attr_accessor :cf
7
+
8
+ def self.hi
9
+ puts "Hello world!"
10
+ end
11
+
12
+ def load_conf(args)
13
+ @cf = { host: args.host, user: args.user,
14
+ keys: [File.open("#{ENV['HOME']}/.ssh/id_rsa", 'r').read],
15
+ deploy_to: args.path }
16
+ end
17
+
18
+ def initialize(args)
19
+ load_conf(args)
20
+ end
21
+
22
+ def download_schema
23
+ Net::SSH.start(cf[:host], cf[:user], key_data: cf[:keys], keys_only: TRUE) do |ssh|
24
+ @prod_conf = YAML.load(ssh.exec!("cat #{cf[:deploy_to]}/config/database.yml"))['production']
25
+ prod_data = ssh.exec!(dump_schema)
26
+ if prod_data.include?('pg_dump') || prod_data.include?('warn')
27
+ raise "error: pg_dump dosen't work correct \n #{prod_data}"
28
+ else
29
+ return Zlib::GzipReader.new(StringIO.new(ssh.exec!(prod_data))).read
30
+ end
31
+ end
32
+ end
33
+
34
+ def dump_schema
35
+ cmd = []
36
+ { password: 'export PGPASSWORD=#{val} &&', database: 'pg_dump #{val}', host: '-h #{val}', port: '-p #{val}',
37
+ username: '-U #{val}' }.each_pair do |key, val|
38
+ cmd.push(val.sub('#{val}', @prod_conf[key.to_s].to_s)) if @prod_conf.key?(key.to_s)
39
+ end
40
+ "#{cmd.join(' ')} --schema-only --no-owner --no-privileges -Z9"
41
+ end
42
+
43
+ def load_schema
44
+ @dev_conf = YAML.load(File.open('config/database.yml', 'r').read)['development']
45
+ conn = PGconn.open(host: @dev_conf['host'],
46
+ dbname: 'postgres',
47
+ user: @dev_conf['username'],
48
+ password: @dev_conf['password'])
49
+ conn.exec("DROP DATABASE IF EXISTS #{@dev_conf['database']}")
50
+ conn.exec("CREATE DATABASE #{@dev_conf['database']}")
51
+ conn = PGconn.open(host: @dev_conf['host'],
52
+ dbname: @dev_conf['database'],
53
+ user: @dev_conf['username'],
54
+ password: @dev_conf['password'])
55
+ conn.exec(download_schema)
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: copy_db_from_prod
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - vgulaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A simple hello world gem
42
+ email: vgulaev@yandex.ru
43
+ executables:
44
+ - copy_db_from_prod
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/copy_db_from_prod
49
+ - lib/copy_db_from_prod.rb
50
+ homepage: http://rubygems.org/gems/copy_db_from_prod
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.5.1
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Hola!
74
+ test_files: []