avm-tools 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: ada053a628dbec161f4e877cc0bd72884102354b6c8621a108c3ef0461f06acf
4
- data.tar.gz: 5b5d4b4c53ce36d2856dbb7364048ef6fac3fa92109b80dc51a6d48b705e0a2d
3
+ metadata.gz: c972632849ad1e0d395436ad4cde63a1b2a43b0805a8e1f7bd36dbc1b40c15b5
4
+ data.tar.gz: 6156864dc1c49ca5e16112420ff78e15a16c59e48246068247bb44646083d6bc
5
5
  SHA512:
6
- metadata.gz: a20e940d7df2f44ed70eef41d49cca74ec0ea42b1ac787906c6c0d779ae3485f7b89f1d49e2dcfbbfae980a085ccd21027f8b828b6aa6f642876fc1046a90c56
7
- data.tar.gz: 71236b4e428442adee98c040b3c91fb17d349c5cdc3f26a8167da91c2624f77c4cb63f8582d23ed9a7c0abb6214b77f253a68f31ef3153df29cce970308d36d5
6
+ metadata.gz: b3ab6bd6d4e2111ebd6cdf99654fbbe92c3725d529d19bd6a860c4a80136cab8d96e50339f32e3490738d2159c4364e7524ed6136351234e2529c00c1c055996
7
+ data.tar.gz: 7c5bcf61a66dd32bf686ca428c1c7e5197c5f06acdce5415c7a2d840e658f3c7d0f10c56d8c40b15996b7f758b7ad8f805fa979365ea86d4805ed44831d51eb0
data/lib/avm.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Avm
4
+ require 'avm/configs'
5
+ require 'avm/instances'
6
+ require 'avm/stereotypes'
4
7
  require 'avm/tools'
5
8
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs'
4
+
5
+ module Avm
6
+ class << self
7
+ def configs
8
+ @configs ||= ::EacRubyUtils::Console::Configs.new('avm-tools')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Avm
4
+ module Instances
5
+ require 'avm/instances/application'
6
+ require 'avm/instances/entries'
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/instances/entries'
4
+
5
+ module Avm
6
+ module Instances
7
+ class Application
8
+ include ::Avm::Instances::Entries
9
+
10
+ attr_reader :id
11
+
12
+ def initialize(id)
13
+ @id = id.to_s
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/simple_cache'
4
+ require 'avm/instances/entries'
5
+
6
+ module Avm
7
+ module Instances
8
+ class Base
9
+ include ::EacRubyUtils::SimpleCache
10
+ include ::Avm::Instances::Entries
11
+
12
+ ID_PATTERN = /\A([a-z]+(?:\-[a-z]+)*)_(.+)\z/.freeze
13
+
14
+ class << self
15
+ def by_id(id)
16
+ application_id, suffix = parse_id(id)
17
+ require 'avm/instances/application'
18
+ new(::Avm::Instances::Application.new(application_id), suffix)
19
+ end
20
+
21
+ private
22
+
23
+ def parse_id(id)
24
+ m = ID_PATTERN.match(id)
25
+ return [m[1], m[2]] if m
26
+
27
+ raise "ID Pattern no matched: \"#{id}\""
28
+ end
29
+ end
30
+
31
+ attr_reader :application, :suffix
32
+
33
+ def initialize(application, suffix)
34
+ @application = application
35
+ @suffix = suffix.to_s
36
+ end
37
+
38
+ def id
39
+ "#{application.id}_#{suffix}"
40
+ end
41
+
42
+ def host_env_uncached
43
+ if read_entry('host') == 'localhost'
44
+ ::EacRubyUtils::Envs.local
45
+ else
46
+ ::EacRubyUtils::Envs.ssh("#{read_entry('user')}@#{read_entry('host')}")
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/object/asserts'
4
+ require 'avm/configs'
5
+
6
+ module Avm
7
+ module Instances
8
+ module Entries
9
+ def path_prefix
10
+ @path_prefix ||= [id].freeze
11
+ end
12
+
13
+ def read_entry(entry_suffix, options = {})
14
+ ::Avm.configs.read_entry(full_entry_path(entry_suffix), options)
15
+ end
16
+
17
+ def full_entry_path(entry_suffix)
18
+ unless entry_suffix.is_a?(::Array)
19
+ entry_suffix = ::EacRubyUtils::PathsHash.parse_entry_key(entry_suffix.to_s)
20
+ end
21
+ (path_prefix + entry_suffix).join('.')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Avm
4
+ module Stereotypes
5
+ Dir["#{File.dirname(__FILE__)}/#{::File.basename(__FILE__, '.*')}/*.rb"].each do |path|
6
+ require path
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/instances/base'
4
+ require 'avm/stereotypes/postgresql/instance_with'
5
+
6
+ module Avm
7
+ module Stereotypes
8
+ module EacWordpressBase0
9
+ class Instance < ::Avm::Instances::Base
10
+ include ::Avm::Stereotypes::Postgresql::InstanceWith
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Avm
4
+ module Stereotypes
5
+ module Postgresql
6
+ Dir["#{File.dirname(__FILE__)}/#{::File.basename(__FILE__, '.*')}/*.rb"].each do |path|
7
+ require path
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Avm
4
+ module Stereotypes
5
+ module Postgresql
6
+ class Instance
7
+ def initialize(env, connection_params)
8
+ @env = env
9
+ @connection_params = connection_params.with_indifferent_access
10
+ end
11
+
12
+ def dump_command
13
+ env.command('pg_dump', '--no-privileges', '--clean', '--no-owner', *common_command_args)
14
+ .envvar('PGPASSWORD', password)
15
+ end
16
+
17
+ def dump_gzip_command
18
+ dump_command.append(['@ESC_|', 'gzip', '-9', '-c'])
19
+ end
20
+
21
+ def psql_command
22
+ env.command("@ESC_PGPASSWORD=#{password}", 'psql', *common_command_args)
23
+ end
24
+
25
+ def psql_command_command(sql)
26
+ psql_command.append(['--quiet', '--tuples-only', '--command', sql])
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :env, :connection_params
32
+
33
+ def common_command_args
34
+ ['--host', host, '--username', user, '--port', port, name]
35
+ end
36
+
37
+ def host
38
+ connection_params[:host] || '127.0.0.1'
39
+ end
40
+
41
+ def port
42
+ connection_params[:port] || '5432'
43
+ end
44
+
45
+ def user
46
+ connection_params.fetch(:user)
47
+ end
48
+
49
+ def password
50
+ connection_params.fetch(:password)
51
+ end
52
+
53
+ def name
54
+ connection_params.fetch(:name)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/stereotypes/postgresql/instance'
4
+
5
+ module Avm
6
+ module Stereotypes
7
+ module Postgresql
8
+ module InstanceWith
9
+ def pg
10
+ @pg ||= ::Avm::Stereotypes::Postgresql::Instance.new(
11
+ host_env, user: read_entry('database.user'), password: read_entry('database.password'),
12
+ name: read_entry('database.name')
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/docopt_runner'
4
+ require 'eac_ruby_utils/simple_cache'
5
+ require 'avm/stereotypes/eac_wordpress_base0/instance'
6
+ require 'avm/tools/runner/eac_wordpress_base0/data'
7
+
8
+ module Avm
9
+ module Tools
10
+ class Runner < ::EacRubyUtils::Console::DocoptRunner
11
+ class EacWordpressBase0 < ::EacRubyUtils::Console::DocoptRunner
12
+ include ::EacRubyUtils::SimpleCache
13
+
14
+ DOC = <<~DOCOPT
15
+ Utilities for EacWordpressBase0 instances.
16
+
17
+ Usage:
18
+ __PROGRAM__ [options] <instance_id> __SUBCOMMANDS__
19
+ __PROGRAM__ -h | --help
20
+
21
+ Options:
22
+ -h --help Show this screen.
23
+ DOCOPT
24
+
25
+ private
26
+
27
+ def instance_uncached
28
+ ::Avm::Stereotypes::EacWordpressBase0::Instance.by_id(options['<instance_id>'])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/docopt_runner'
4
+ require 'avm/tools/runner/eac_wordpress_base0/data/dump'
5
+
6
+ module Avm
7
+ module Tools
8
+ class Runner < ::EacRubyUtils::Console::DocoptRunner
9
+ class EacWordpressBase0 < ::EacRubyUtils::Console::DocoptRunner
10
+ class Data < ::EacRubyUtils::Console::DocoptRunner
11
+ DOC = <<~DOCOPT
12
+ Data utilities for EacWordpressBase0 instances.
13
+
14
+ Usage:
15
+ __PROGRAM__ __SUBCOMMANDS__
16
+ __PROGRAM__ -h | --help
17
+
18
+ Options:
19
+ -h --help Show this screen.
20
+ DOCOPT
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/numeric/time'
4
+ require 'eac_ruby_utils/console/speaker'
5
+ require 'eac_ruby_utils/simple_cache'
6
+
7
+ module Avm
8
+ module Tools
9
+ class Runner < ::EacRubyUtils::Console::DocoptRunner
10
+ class EacWordpressBase0 < ::EacRubyUtils::Console::DocoptRunner
11
+ class Data < ::EacRubyUtils::Console::DocoptRunner
12
+ class Dump < ::EacRubyUtils::Console::DocoptRunner
13
+ include ::EacRubyUtils::SimpleCache
14
+ include ::EacRubyUtils::Console::Speaker
15
+
16
+ DUMP_EXPIRE_TIME = 1.days
17
+ DEFAULT_DUMP_PATH_ENTRY_SUFFIX = 'data.default_dump_path'
18
+ NO_DUMP_MESSAGE = 'Dump "%s" already exist and rewrite options was no setted nor ' \
19
+ 'dump was expired.'
20
+
21
+ DOC = <<~DOCUMENT
22
+ Dump utility for EacRailsBase instance.
23
+
24
+ Usage:
25
+ __PROGRAM__ [options]
26
+
27
+ Options:
28
+ -h --help Show this screen.
29
+ --rewrite Forces dump overwrite.
30
+ --dump-path=<dump_path> Set DUMP_PATH variable.
31
+ DOCUMENT
32
+
33
+ def run
34
+ if run_dump?
35
+ run_dump
36
+ else
37
+ warn(format(NO_DUMP_MESSAGE, dump_path))
38
+ end
39
+ success("Dump path: \"#{dump_path}\"")
40
+ end
41
+
42
+ private
43
+
44
+ def dump_path
45
+ options.fetch('--dump-path') || default_dump_path
46
+ end
47
+
48
+ def default_dump_path
49
+ context(:instance).read_entry(DEFAULT_DUMP_PATH_ENTRY_SUFFIX)
50
+ end
51
+
52
+ def dump_exist?
53
+ ::File.exist?(dump_path)
54
+ end
55
+
56
+ def dump_time_uncached
57
+ dump_exist? ? ::Time.now - ::File.mtime(dump_path) : nil
58
+ end
59
+
60
+ def dump_expired?
61
+ return false unless dump_time.present?
62
+
63
+ dump_time >= DUMP_EXPIRE_TIME
64
+ end
65
+
66
+ def run_dump
67
+ download_dump
68
+ rotate_dump
69
+ move_download_to_final_dest
70
+ end
71
+
72
+ def download_dump
73
+ infom 'Downloading dump...'
74
+ dump_command.system!(output_file: download_path)
75
+ fatal_error "File \"#{download_path}\" not saved" unless ::File.exist?(download_path)
76
+ fatal_error "File \"#{download_path}\" is empty" if ::File.zero?(download_path)
77
+ end
78
+
79
+ def move_download_to_final_dest
80
+ ::FileUtils.mv(download_path, dump_path)
81
+ end
82
+
83
+ def rotate_dump
84
+ return unless dump_exist?
85
+
86
+ info "Rotating \"#{dump_path}\"..."
87
+ ::Avm::Files::Rotate.new(dump_path).run
88
+ end
89
+
90
+ def download_path_uncached
91
+ f = ::Tempfile.new('eac_rails_base0_data_dump')
92
+ path = f.path
93
+ f.close
94
+ f.unlink
95
+ path
96
+ end
97
+
98
+ def run_dump?
99
+ !dump_exist? || options.fetch('--rewrite') || dump_expired?
100
+ end
101
+
102
+ def dump_command
103
+ context(:instance).pg.dump_gzip_command
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Avm
4
4
  module Tools
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avm-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-05 00:00:00.000000000 Z
11
+ date: 2019-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eac_launcher
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.9'
33
+ version: '0.10'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.9'
40
+ version: '0.10'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -96,12 +96,25 @@ files:
96
96
  - Gemfile
97
97
  - exe/avm
98
98
  - lib/avm.rb
99
+ - lib/avm/configs.rb
99
100
  - lib/avm/files.rb
100
101
  - lib/avm/files/rotate.rb
102
+ - lib/avm/instances.rb
103
+ - lib/avm/instances/application.rb
104
+ - lib/avm/instances/base.rb
105
+ - lib/avm/instances/entries.rb
106
+ - lib/avm/stereotypes.rb
107
+ - lib/avm/stereotypes/eac_wordpress_base0/instance.rb
108
+ - lib/avm/stereotypes/postgresql.rb
109
+ - lib/avm/stereotypes/postgresql/instance.rb
110
+ - lib/avm/stereotypes/postgresql/instance_with.rb
101
111
  - lib/avm/tools.rb
102
112
  - lib/avm/tools/git.rb
103
113
  - lib/avm/tools/git/complete_issue.rb
104
114
  - lib/avm/tools/runner.rb
115
+ - lib/avm/tools/runner/eac_wordpress_base0.rb
116
+ - lib/avm/tools/runner/eac_wordpress_base0/data.rb
117
+ - lib/avm/tools/runner/eac_wordpress_base0/data/dump.rb
105
118
  - lib/avm/tools/runner/files.rb
106
119
  - lib/avm/tools/runner/files/rotate.rb
107
120
  - lib/avm/tools/runner/git.rb