avm-tools 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/avm.rb +1 -0
- data/lib/avm/data/instance/files_unit.rb +39 -0
- data/lib/avm/git/commit/deploy.rb +62 -10
- data/lib/avm/instances/base.rb +10 -0
- data/lib/avm/patches/object/template.rb +24 -0
- data/lib/avm/result.rb +6 -0
- data/lib/avm/stereotypes/eac_wordpress_base0/deploy.rb +148 -0
- data/lib/avm/stereotypes/eac_wordpress_base0/instance.rb +12 -2
- data/lib/avm/templates.rb +51 -0
- data/lib/avm/templates/directory.rb +96 -0
- data/lib/avm/templates/file.rb +101 -0
- data/lib/avm/tools/runner/eac_wordpress_base0.rb +1 -0
- data/lib/avm/tools/runner/eac_wordpress_base0/deploy.rb +41 -0
- data/lib/avm/tools/runner/git/deploy.rb +24 -7
- data/lib/avm/tools/version.rb +1 -1
- metadata +11 -5
- data/lib/avm/stereotypes/eac_wordpress_base0/uploads_data_unit.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d542c3ce6d969a69098250163e506bff7c11293a2c03bf9afd3c443e3e8bf93f
|
4
|
+
data.tar.gz: c9ffe435c0fd1c2f42a66e90bd2b06a27b35307f355047d6c0cd976426a9ffbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 700db3976b2b5f57269ade79a8d43b667819f82e551f6124879b4999f087f770c641313bae39551224349a4d3487e6d4835db60fb9eba556af4048aaace7cb41
|
7
|
+
data.tar.gz: 29baf3afeeef3eee66956dde7fcedf8a4de2d641e2635fd8a99c9992f13e02b2d1b3e114833f131d1e107a9615ce1b718a254f61ee2599f3534dcf54bb2f04d4
|
data/lib/avm.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/data/instance/unit'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Data
|
7
|
+
module Instance
|
8
|
+
class FilesUnit < ::Avm::Data::Instance::Unit
|
9
|
+
EXTENSION = '.tar.gz'
|
10
|
+
|
11
|
+
attr_reader :fs_path_subpath
|
12
|
+
|
13
|
+
def initialize(instance, fs_path_subpath)
|
14
|
+
super(instance)
|
15
|
+
@fs_path_subpath = fs_path_subpath
|
16
|
+
end
|
17
|
+
|
18
|
+
before_load :clear_files
|
19
|
+
|
20
|
+
def files_path
|
21
|
+
::File.join(instance.read_entry(:fs_path), fs_path_subpath)
|
22
|
+
end
|
23
|
+
|
24
|
+
def dump_command
|
25
|
+
instance.host_env.command('tar', '-czf', '-', '-C', files_path, '.')
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_command
|
29
|
+
instance.host_env.command('tar', '-xzf', '-', '-C', files_path)
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear_files
|
33
|
+
infom "Removing all files under #{files_path}..."
|
34
|
+
instance.host_env.command('find', files_path, '-mindepth', 1, '-delete').execute!
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -4,6 +4,7 @@ require 'addressable'
|
|
4
4
|
require 'eac_ruby_utils/simple_cache'
|
5
5
|
require 'eac_ruby_utils/require_sub'
|
6
6
|
::EacRubyUtils.require_sub(__FILE__)
|
7
|
+
require 'avm/templates/directory'
|
7
8
|
|
8
9
|
module Avm
|
9
10
|
module Git
|
@@ -37,21 +38,60 @@ module Avm
|
|
37
38
|
class Deploy
|
38
39
|
include ::EacRubyUtils::SimpleCache
|
39
40
|
|
40
|
-
attr_reader :commit, :target_env, :target_path
|
41
|
+
attr_reader :commit, :target_env, :target_path, :appended_directories, :variables_source
|
41
42
|
|
42
43
|
def initialize(commit, target_env, target_path)
|
43
44
|
@commit = commit
|
44
45
|
@target_env = target_env
|
45
46
|
@target_path = target_path
|
46
|
-
|
47
|
+
@appended_directories = []
|
48
|
+
@variables_source = nil
|
47
49
|
end
|
48
50
|
|
49
|
-
|
51
|
+
def append_directory(directory)
|
52
|
+
@appended_directories << directory
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def append_directories(directories)
|
57
|
+
directories.each { |directory| append_directory(directory) }
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def variables_source_set(source)
|
62
|
+
@variables_source = source
|
63
|
+
self
|
64
|
+
end
|
50
65
|
|
51
66
|
def run
|
52
|
-
|
53
|
-
|
54
|
-
|
67
|
+
on_build_dir do
|
68
|
+
copy_git_content
|
69
|
+
appended_directories.each { |directory| copy_appended_directory(directory) }
|
70
|
+
mkdir_target
|
71
|
+
clear_content
|
72
|
+
send_untar_package
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
attr_reader :build_dir
|
79
|
+
|
80
|
+
def on_build_dir
|
81
|
+
@build_dir = ::Dir.mktmpdir
|
82
|
+
yield
|
83
|
+
ensure
|
84
|
+
::FileUtils.rm_rf(@build_dir)
|
85
|
+
end
|
86
|
+
|
87
|
+
def copy_git_content
|
88
|
+
git_archive_command.pipe(untar_git_archive_command).execute!
|
89
|
+
end
|
90
|
+
|
91
|
+
def copy_appended_directory(directory)
|
92
|
+
raise 'Variables source not set' if variables_source.blank?
|
93
|
+
|
94
|
+
::Avm::Templates::Directory.new(directory).apply(variables_source, build_dir)
|
55
95
|
end
|
56
96
|
|
57
97
|
def mkdir_target
|
@@ -60,20 +100,32 @@ module Avm
|
|
60
100
|
|
61
101
|
def clear_content
|
62
102
|
target_env.command(
|
63
|
-
'find', target_path, '-mindepth', 1, '-exec', 'rm', '-rf', '{}', ';'
|
103
|
+
'find', target_path, '-mindepth', '1', '-maxdepth', '1', '-exec', 'rm', '-rf', '{}', ';'
|
64
104
|
).execute!
|
65
105
|
end
|
66
106
|
|
67
107
|
def send_untar_package
|
68
|
-
|
108
|
+
tar_build_command.pipe(untar_build_command).execute!
|
69
109
|
end
|
70
110
|
|
71
111
|
def git_archive_command
|
72
112
|
commit.git.command('archive', '--format=tar', commit.sha1)
|
73
113
|
end
|
74
114
|
|
75
|
-
def
|
76
|
-
|
115
|
+
def untar_git_archive_command
|
116
|
+
source_env.command('tar', '-xf', '-', '-C', build_dir)
|
117
|
+
end
|
118
|
+
|
119
|
+
def tar_build_command
|
120
|
+
source_env.command('tar', '-czO', '-C', build_dir, '.')
|
121
|
+
end
|
122
|
+
|
123
|
+
def untar_build_command
|
124
|
+
target_env.command('tar', '-xzf', '-', '-C', target_path)
|
125
|
+
end
|
126
|
+
|
127
|
+
def source_env
|
128
|
+
::EacRubyUtils::Envs.local
|
77
129
|
end
|
78
130
|
end
|
79
131
|
end
|
data/lib/avm/instances/base.rb
CHANGED
@@ -39,6 +39,10 @@ module Avm
|
|
39
39
|
"#{application.id}_#{suffix}"
|
40
40
|
end
|
41
41
|
|
42
|
+
def to_s
|
43
|
+
id
|
44
|
+
end
|
45
|
+
|
42
46
|
def host_env_uncached
|
43
47
|
if read_entry('host') == 'localhost'
|
44
48
|
::EacRubyUtils::Envs.local
|
@@ -46,6 +50,12 @@ module Avm
|
|
46
50
|
::EacRubyUtils::Envs.ssh("#{read_entry('user')}@#{read_entry('host')}")
|
47
51
|
end
|
48
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def source_instance_uncached
|
57
|
+
::Avm::Instances::Base.by_id(read_entry(:source_instance_id))
|
58
|
+
end
|
49
59
|
end
|
50
60
|
end
|
51
61
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/string/inflections'
|
4
|
+
require 'avm/templates'
|
5
|
+
|
6
|
+
class Object
|
7
|
+
class << self
|
8
|
+
def template
|
9
|
+
@template ||= ::Avm::Templates.template(name.underscore)
|
10
|
+
end
|
11
|
+
|
12
|
+
def template_path
|
13
|
+
@template_path ||= ::Avm::Templates.template_path(name.underscore)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def template
|
18
|
+
self.class.template
|
19
|
+
end
|
20
|
+
|
21
|
+
def template_path
|
22
|
+
self.class.template_path
|
23
|
+
end
|
24
|
+
end
|
data/lib/avm/result.rb
CHANGED
@@ -0,0 +1,148 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
require 'eac_launcher/git/base'
|
6
|
+
require 'avm/git/commit'
|
7
|
+
require 'avm/patches/object/template'
|
8
|
+
|
9
|
+
module Avm
|
10
|
+
module Stereotypes
|
11
|
+
module EacWordpressBase0
|
12
|
+
class Deploy
|
13
|
+
enable_console_speaker
|
14
|
+
enable_simple_cache
|
15
|
+
|
16
|
+
attr_reader :instance, :git_reference
|
17
|
+
|
18
|
+
DEFAULT_REMOTE_NAME = 'origin'
|
19
|
+
|
20
|
+
def initialize(instance, git_reference)
|
21
|
+
@instance = instance
|
22
|
+
@git_reference = git_reference
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
start_banner
|
27
|
+
git_deploy
|
28
|
+
setup_files_units
|
29
|
+
assert_instance_branch
|
30
|
+
::Avm::Result.success('Deployed')
|
31
|
+
rescue ::Avm::Result::Error => e
|
32
|
+
e.to_result
|
33
|
+
end
|
34
|
+
|
35
|
+
def start_banner
|
36
|
+
infov 'Instance', instance
|
37
|
+
infov 'Git reference (User)', git_reference.if_present('- BLANK -')
|
38
|
+
infov 'Git remote name', git_remote_name
|
39
|
+
infov 'Git reference (Found)', git_reference_found
|
40
|
+
infov 'Git commit SHA1', commit_sha1
|
41
|
+
end
|
42
|
+
|
43
|
+
def git_deploy
|
44
|
+
infom 'Deploying source code and appended content...'
|
45
|
+
::Avm::Git::Commit.new(git, commit_sha1).deploy_to_env_path(
|
46
|
+
instance.host_env,
|
47
|
+
instance.read_entry(:fs_path)
|
48
|
+
).append_directory(template_path).variables_source_set(instance).run
|
49
|
+
end
|
50
|
+
|
51
|
+
def setup_files_units
|
52
|
+
::Avm::Stereotypes::EacWordpressBase0::Instance::FILES_UNITS
|
53
|
+
.each do |data_key, fs_path_subpath|
|
54
|
+
FilesUnit.new(self, data_key, fs_path_subpath).run
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def assert_instance_branch
|
59
|
+
infom 'Setting instance branch...'
|
60
|
+
git.execute!('push', git_remote_name, "#{commit_sha1}:refs/heads/#{instance.id}", '-f')
|
61
|
+
end
|
62
|
+
|
63
|
+
def commit_sha1_uncached
|
64
|
+
git_fetch
|
65
|
+
r = git.rev_parse(git_reference_found)
|
66
|
+
return r if r
|
67
|
+
|
68
|
+
raise ::Avm::Result::Error, "No commit SHA1 found for \"#{git_reference_found}\""
|
69
|
+
end
|
70
|
+
|
71
|
+
def git_reference_found_uncached
|
72
|
+
%w[git_reference instance_branch master_branch].map { |b| send(b) }.find(&:present?) ||
|
73
|
+
raise(
|
74
|
+
::Avm::Result::Error,
|
75
|
+
'No git reference found (Searched for option, instance and master)'
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
def git_uncached
|
80
|
+
::EacLauncher::Git::Base.new(git_repository_path)
|
81
|
+
end
|
82
|
+
|
83
|
+
def instance_branch
|
84
|
+
remote_branch(instance.id)
|
85
|
+
end
|
86
|
+
|
87
|
+
def master_branch
|
88
|
+
remote_branch('master')
|
89
|
+
end
|
90
|
+
|
91
|
+
def git_remote_name
|
92
|
+
DEFAULT_REMOTE_NAME
|
93
|
+
end
|
94
|
+
|
95
|
+
def git_remote_hashs_uncached
|
96
|
+
git.remote_hashs(git_remote_name)
|
97
|
+
end
|
98
|
+
|
99
|
+
def git_fetch_uncached
|
100
|
+
infom "Fetching remote \"#{git_remote_name}\" from \"#{git_repository_path}\"..."
|
101
|
+
git.fetch(git_remote_name)
|
102
|
+
end
|
103
|
+
|
104
|
+
def git_repository_path
|
105
|
+
instance.source_instance.read_entry(:fs_path)
|
106
|
+
end
|
107
|
+
|
108
|
+
def remote_branch(name)
|
109
|
+
git_remote_hashs.key?("refs/heads/#{name}") ? "#{git_remote_name}/#{name}" : nil
|
110
|
+
end
|
111
|
+
|
112
|
+
class FilesUnit < ::SimpleDelegator
|
113
|
+
attr_reader :data_key, :fs_path_subpath
|
114
|
+
|
115
|
+
def initialize(deploy, data_key, fs_path_subpath)
|
116
|
+
super(deploy)
|
117
|
+
@data_key = data_key
|
118
|
+
@fs_path_subpath = fs_path_subpath
|
119
|
+
end
|
120
|
+
|
121
|
+
def run
|
122
|
+
assert_source_directory
|
123
|
+
link_source_target
|
124
|
+
end
|
125
|
+
|
126
|
+
def assert_source_directory
|
127
|
+
infom "Asserting \"#{data_key}\" source directory..."
|
128
|
+
instance.host_env.command('mkdir', '-p', source_path).execute!
|
129
|
+
end
|
130
|
+
|
131
|
+
def source_path
|
132
|
+
::File.join(instance.read_entry(:data_fs_path), data_key.to_s)
|
133
|
+
end
|
134
|
+
|
135
|
+
def target_path
|
136
|
+
::File.join(instance.read_entry(:fs_path), fs_path_subpath.to_s)
|
137
|
+
end
|
138
|
+
|
139
|
+
def link_source_target
|
140
|
+
infom "Linking \"#{data_key}\" directory..."
|
141
|
+
instance.host_env.command('rm', '-rf', target_path).execute!
|
142
|
+
instance.host_env.command('ln', '-s', source_path, target_path).execute!
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'avm/instances/base'
|
4
4
|
require 'avm/stereotypes/postgresql/instance_with'
|
5
|
+
require 'avm/data/instance/files_unit'
|
5
6
|
require 'avm/data/instance/package'
|
6
|
-
require 'avm/stereotypes/eac_wordpress_base0/uploads_data_unit'
|
7
7
|
|
8
8
|
module Avm
|
9
9
|
module Stereotypes
|
@@ -11,6 +11,8 @@ module Avm
|
|
11
11
|
class Instance < ::Avm::Instances::Base
|
12
12
|
include ::Avm::Stereotypes::Postgresql::InstanceWith
|
13
13
|
|
14
|
+
FILES_UNITS = { uploads: 'wp-content/uploads', themes: 'wp-content/themes' }.freeze
|
15
|
+
|
14
16
|
def data_dump(argv = [])
|
15
17
|
run_subcommand(::Avm::Tools::Runner::EacWordpressBase0::Data::Dump, argv)
|
16
18
|
end
|
@@ -22,7 +24,7 @@ module Avm
|
|
22
24
|
|
23
25
|
def data_package
|
24
26
|
@data_package ||= ::Avm::Data::Instance::Package.new(
|
25
|
-
self, units: { database: database_unit
|
27
|
+
self, units: { database: database_unit }.merge(files_units)
|
26
28
|
)
|
27
29
|
end
|
28
30
|
|
@@ -37,6 +39,14 @@ module Avm
|
|
37
39
|
SQL
|
38
40
|
end
|
39
41
|
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def files_units
|
46
|
+
FILES_UNITS.map do |data_key, fs_path_subpath|
|
47
|
+
[data_key, ::Avm::Data::Instance::FilesUnit.new(self, fs_path_subpath)]
|
48
|
+
end.to_h
|
49
|
+
end
|
40
50
|
end
|
41
51
|
end
|
42
52
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/require_sub'
|
4
|
+
::EacRubyUtils.require_sub(__FILE__)
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Templates
|
8
|
+
class << self
|
9
|
+
def template(subpath, required = true)
|
10
|
+
path = template_path
|
11
|
+
if path.blank?
|
12
|
+
return nil unless required
|
13
|
+
|
14
|
+
raise "Template not found for subpath \"#{subpath}\" (Included paths: #{included_paths})"
|
15
|
+
end
|
16
|
+
return ::Avm::Templates::File.new(path) if ::File.file?(path)
|
17
|
+
return ::Avm::Templates::Directory.new(path) if ::File.directory?(path)
|
18
|
+
|
19
|
+
raise 'Invalid branching'
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return The absolute path of template if found, +nil+ otherwise.
|
23
|
+
def template_path(subpath)
|
24
|
+
included_paths.each do |included_path|
|
25
|
+
r = search_template_in_included_path(included_path, subpath)
|
26
|
+
return r if r
|
27
|
+
end
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def included_paths
|
32
|
+
@included_paths ||= ::Set.new([::File.expand_path('../../template', __dir__)])
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def search_template_in_included_path(included_path, subpath)
|
38
|
+
path = ::File.join(included_path, subpath)
|
39
|
+
dir = ::File.dirname(path)
|
40
|
+
Dir.entries(dir).each do |entry|
|
41
|
+
next if %w[. ..].include?(entry)
|
42
|
+
return path if template_basename(entry) == ::File.basename(subpath)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def template_basename(entry)
|
47
|
+
entry.gsub(/(?:\.[a-z0-9]+)+\z/i, '')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/templates/file'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Templates
|
7
|
+
class Directory
|
8
|
+
TEMPLATE_EXTNAME = '.template'
|
9
|
+
|
10
|
+
attr_reader :path
|
11
|
+
|
12
|
+
def initialize(path)
|
13
|
+
@path = path
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply(variables_source, directory)
|
17
|
+
TemplateNode.new(self, '.', directory, variables_source).apply
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def apply_fs_object(source_relative, target)
|
23
|
+
if ::File.directory?(source_absolute(source_relative))
|
24
|
+
apply_directory(source_relative, target)
|
25
|
+
elsif ::File.file?(source_absolute(source_relative))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def source_absolute(source_relative)
|
30
|
+
::File.expand_path(source_relative, path)
|
31
|
+
end
|
32
|
+
|
33
|
+
class TemplateNode
|
34
|
+
attr_reader :source_directory, :source_relative, :target_root_directory, :variables_source
|
35
|
+
|
36
|
+
def initialize(source_directory, source_relative, target_root_directory, variables_source)
|
37
|
+
@source_directory = source_directory
|
38
|
+
@source_relative = source_relative
|
39
|
+
@target_root_directory = target_root_directory
|
40
|
+
@variables_source = variables_source
|
41
|
+
end
|
42
|
+
|
43
|
+
def apply
|
44
|
+
if file?
|
45
|
+
apply_file
|
46
|
+
elsif directory?
|
47
|
+
apply_directory
|
48
|
+
else
|
49
|
+
raise "Unknown filesystem type: #{source_absolute}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def apply_directory
|
56
|
+
::FileUtils.mkdir_p(target_absolute)
|
57
|
+
Dir.entries(source_absolute).each do |entry|
|
58
|
+
child(entry).apply unless %w[. ..].include?(entry)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def apply_file
|
63
|
+
if ::File.extname(source_absolute) == TEMPLATE_EXTNAME
|
64
|
+
::Avm::Templates::File.new(source_absolute).apply_to_file(
|
65
|
+
variables_source, target_absolute
|
66
|
+
)
|
67
|
+
else
|
68
|
+
::FileUtils.cp(source_absolute, target_absolute)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def child(entry)
|
73
|
+
TemplateNode.new(source_directory, ::File.join(source_relative, entry),
|
74
|
+
target_root_directory, variables_source)
|
75
|
+
end
|
76
|
+
|
77
|
+
def file?
|
78
|
+
::File.file?(source_absolute)
|
79
|
+
end
|
80
|
+
|
81
|
+
def directory?
|
82
|
+
::File.directory?(source_absolute)
|
83
|
+
end
|
84
|
+
|
85
|
+
def source_absolute
|
86
|
+
::File.expand_path(source_relative, source_directory.path)
|
87
|
+
end
|
88
|
+
|
89
|
+
def target_absolute
|
90
|
+
::File.expand_path(source_relative, target_root_directory)
|
91
|
+
.gsub(/#{::Regexp.quote(TEMPLATE_EXTNAME)}\z/, '')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
4
|
+
require 'eac_ruby_utils/simple_cache'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Templates
|
8
|
+
class File
|
9
|
+
include ::EacRubyUtils::SimpleCache
|
10
|
+
|
11
|
+
VARIABLE_DELIMITER = ::Regexp.quote('%%')
|
12
|
+
VARIABLE_PATTERN = /#{VARIABLE_DELIMITER}([a-z_][a-z0-9_]*)#{VARIABLE_DELIMITER}/i.freeze
|
13
|
+
|
14
|
+
attr_reader :path
|
15
|
+
|
16
|
+
def initialize(path)
|
17
|
+
@path = path
|
18
|
+
end
|
19
|
+
|
20
|
+
# +variables_provider+ A [Hash] or object which responds to +read_entry(entry_name)+.
|
21
|
+
def apply(variables_source)
|
22
|
+
variables_provider = build_variables_provider(variables_source)
|
23
|
+
variables.inject(content) do |a, e|
|
24
|
+
a.gsub(variable_pattern(e), variables_provider.variable_value(e).to_s)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def apply_to_file(variables_source, output_file_path)
|
29
|
+
::File.write(output_file_path, apply(variables_source))
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def variables_uncached
|
35
|
+
content.scan(VARIABLE_PATTERN).map(&:first).map do |name|
|
36
|
+
sanitize_variable_name(name)
|
37
|
+
end.to_set
|
38
|
+
end
|
39
|
+
|
40
|
+
def content_uncached
|
41
|
+
::File.read(path)
|
42
|
+
end
|
43
|
+
|
44
|
+
def sanitize_variable_name(variable_name)
|
45
|
+
variable_name.to_s.downcase
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_variables_provider(variables_source)
|
49
|
+
return HashVariablesProvider.new(variables_source) if variables_source.is_a?(::Hash)
|
50
|
+
return EntriesReaderVariablesProvider.new(variables_source) if
|
51
|
+
variables_source.respond_to?(:read_entry)
|
52
|
+
|
53
|
+
raise "Variables provider not found for #{variables_source}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def variable_pattern(name)
|
57
|
+
/#{VARIABLE_DELIMITER}#{::Regexp.quote(name)}#{VARIABLE_DELIMITER}/i
|
58
|
+
end
|
59
|
+
|
60
|
+
class BaseVariablesProvider
|
61
|
+
attr_reader :source
|
62
|
+
|
63
|
+
def initialize(source)
|
64
|
+
@source = source
|
65
|
+
end
|
66
|
+
|
67
|
+
def variable_value(name)
|
68
|
+
return variable_fetch(name) if variable_exist?(name)
|
69
|
+
|
70
|
+
raise VariableNotFoundError, "Variable \"#{name}\" not found in #{source}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class HashVariablesProvider < BaseVariablesProvider
|
75
|
+
def initialize(source)
|
76
|
+
super(source.with_indifferent_access)
|
77
|
+
end
|
78
|
+
|
79
|
+
def variable_exist?(name)
|
80
|
+
source.key?(name)
|
81
|
+
end
|
82
|
+
|
83
|
+
def variable_fetch(name)
|
84
|
+
source.fetch(name)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class EntriesReaderVariablesProvider < BaseVariablesProvider
|
89
|
+
def variable_exist?(_name)
|
90
|
+
true
|
91
|
+
end
|
92
|
+
|
93
|
+
def variable_fetch(name)
|
94
|
+
source.read_entry(name)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class VariableNotFoundError < StandardError; end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -4,6 +4,7 @@ require 'eac_ruby_utils/console/docopt_runner'
|
|
4
4
|
require 'eac_ruby_utils/simple_cache'
|
5
5
|
require 'avm/stereotypes/eac_wordpress_base0/instance'
|
6
6
|
require 'avm/tools/runner/eac_wordpress_base0/data'
|
7
|
+
require 'avm/tools/runner/eac_wordpress_base0/deploy'
|
7
8
|
|
8
9
|
module Avm
|
9
10
|
module Tools
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/console/docopt_runner'
|
4
|
+
require 'eac_ruby_utils/console/speaker'
|
5
|
+
require 'avm/stereotypes/eac_wordpress_base0/deploy'
|
6
|
+
|
7
|
+
module Avm
|
8
|
+
module Tools
|
9
|
+
class Runner < ::EacRubyUtils::Console::DocoptRunner
|
10
|
+
class EacWordpressBase0 < ::EacRubyUtils::Console::DocoptRunner
|
11
|
+
class Deploy < ::EacRubyUtils::Console::DocoptRunner
|
12
|
+
include ::EacRubyUtils::Console::Speaker
|
13
|
+
|
14
|
+
DOC = <<~DOCOPT
|
15
|
+
Deploy for EacWordpressBase0 instance.
|
16
|
+
|
17
|
+
Usage:
|
18
|
+
__PROGRAM__ [options]
|
19
|
+
__PROGRAM__ -h | --help
|
20
|
+
|
21
|
+
Options:
|
22
|
+
-h --help Show this screen.
|
23
|
+
-r --reference=<git-reference> Git reference to deploy.
|
24
|
+
DOCOPT
|
25
|
+
|
26
|
+
def run
|
27
|
+
result = ::Avm::Stereotypes::EacWordpressBase0::Deploy.new(
|
28
|
+
context(:instance),
|
29
|
+
options.fetch('--reference')
|
30
|
+
).run
|
31
|
+
if result.error?
|
32
|
+
fatal_error result.to_s
|
33
|
+
else
|
34
|
+
infov 'Result', result.label
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -17,13 +17,14 @@ module Avm
|
|
17
17
|
DOC = <<~DOCOPT
|
18
18
|
Deploy a Git revision to a location (Local or remote).
|
19
19
|
|
20
|
-
|
21
|
-
__PROGRAM__ [options] <target-url>
|
22
|
-
|
20
|
+
Usage:
|
21
|
+
__PROGRAM__ [options] <target-url> [<append-directories>...]
|
22
|
+
__PROGRAM__ -h | --help
|
23
23
|
|
24
24
|
Options:
|
25
|
-
-h --help
|
26
|
-
-r --reference=<reference>
|
25
|
+
-h --help Mostra esta ajuda.
|
26
|
+
-r --reference=<reference> Reference [default: HEAD].
|
27
|
+
-i --instance=<instance-id> Read entries from instance with id=<instance-id>.
|
27
28
|
DOCOPT
|
28
29
|
|
29
30
|
def run
|
@@ -38,6 +39,7 @@ module Avm
|
|
38
39
|
def input_banner
|
39
40
|
infov 'Repository', git
|
40
41
|
infov 'Reference', reference
|
42
|
+
infov 'Instance ID', instance_id.if_present('- BLANK -')
|
41
43
|
end
|
42
44
|
|
43
45
|
def validate
|
@@ -63,8 +65,23 @@ module Avm
|
|
63
65
|
end
|
64
66
|
|
65
67
|
def deploy
|
66
|
-
|
67
|
-
|
68
|
+
::Avm::Git::Commit.new(git, reference_sha1)
|
69
|
+
.deploy_to_url(options.fetch('<target-url>'))
|
70
|
+
.append_directories(options.fetch('<append-directories>'))
|
71
|
+
.variables_source_set(variables_source)
|
72
|
+
.run
|
73
|
+
end
|
74
|
+
|
75
|
+
def variables_source
|
76
|
+
instance || ::Avm.configs
|
77
|
+
end
|
78
|
+
|
79
|
+
def instance_uncached
|
80
|
+
instance_id.if_present { |v| ::Avm::Instances::Base.by_id(v) }
|
81
|
+
end
|
82
|
+
|
83
|
+
def instance_id
|
84
|
+
options.fetch('--instance')
|
68
85
|
end
|
69
86
|
end
|
70
87
|
end
|
data/lib/avm/tools/version.rb
CHANGED
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.
|
4
|
+
version: 0.15.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-10-
|
11
|
+
date: 2019-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aranha-parsers
|
@@ -76,14 +76,14 @@ dependencies:
|
|
76
76
|
requirements:
|
77
77
|
- - "~>"
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '0.
|
79
|
+
version: '0.14'
|
80
80
|
type: :runtime
|
81
81
|
prerelease: false
|
82
82
|
version_requirements: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
84
|
- - "~>"
|
85
85
|
- !ruby/object:Gem::Version
|
86
|
-
version: '0.
|
86
|
+
version: '0.14'
|
87
87
|
- !ruby/object:Gem::Dependency
|
88
88
|
name: filesize
|
89
89
|
requirement: !ruby/object:Gem::Requirement
|
@@ -173,6 +173,7 @@ files:
|
|
173
173
|
- lib/avm/configs.rb
|
174
174
|
- lib/avm/data.rb
|
175
175
|
- lib/avm/data/instance.rb
|
176
|
+
- lib/avm/data/instance/files_unit.rb
|
176
177
|
- lib/avm/data/instance/package.rb
|
177
178
|
- lib/avm/data/instance/unit.rb
|
178
179
|
- lib/avm/data/package.rb
|
@@ -203,14 +204,18 @@ files:
|
|
203
204
|
- lib/avm/instances/entries.rb
|
204
205
|
- lib/avm/patches.rb
|
205
206
|
- lib/avm/patches/eac_launcher_git_base.rb
|
207
|
+
- lib/avm/patches/object/template.rb
|
206
208
|
- lib/avm/result.rb
|
207
209
|
- lib/avm/stereotypes.rb
|
210
|
+
- lib/avm/stereotypes/eac_wordpress_base0/deploy.rb
|
208
211
|
- lib/avm/stereotypes/eac_wordpress_base0/instance.rb
|
209
|
-
- lib/avm/stereotypes/eac_wordpress_base0/uploads_data_unit.rb
|
210
212
|
- lib/avm/stereotypes/postgresql.rb
|
211
213
|
- lib/avm/stereotypes/postgresql/instance.rb
|
212
214
|
- lib/avm/stereotypes/postgresql/instance/data_unit.rb
|
213
215
|
- lib/avm/stereotypes/postgresql/instance_with.rb
|
216
|
+
- lib/avm/templates.rb
|
217
|
+
- lib/avm/templates/directory.rb
|
218
|
+
- lib/avm/templates/file.rb
|
214
219
|
- lib/avm/tools.rb
|
215
220
|
- lib/avm/tools/git.rb
|
216
221
|
- lib/avm/tools/runner.rb
|
@@ -218,6 +223,7 @@ files:
|
|
218
223
|
- lib/avm/tools/runner/eac_wordpress_base0/data.rb
|
219
224
|
- lib/avm/tools/runner/eac_wordpress_base0/data/dump.rb
|
220
225
|
- lib/avm/tools/runner/eac_wordpress_base0/data/load.rb
|
226
|
+
- lib/avm/tools/runner/eac_wordpress_base0/deploy.rb
|
221
227
|
- lib/avm/tools/runner/files.rb
|
222
228
|
- lib/avm/tools/runner/files/rotate.rb
|
223
229
|
- lib/avm/tools/runner/git.rb
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitar'
|
4
|
-
require 'zlib'
|
5
|
-
require 'avm/data/instance/unit'
|
6
|
-
|
7
|
-
module Avm
|
8
|
-
module Stereotypes
|
9
|
-
module EacWordpressBase0
|
10
|
-
class Instance < ::Avm::Instances::Base
|
11
|
-
class UploadsDataUnit < ::Avm::Data::Instance::Unit
|
12
|
-
EXTENSION = '.tar.gz'
|
13
|
-
|
14
|
-
before_load :clear_uploads
|
15
|
-
|
16
|
-
def uploads_path
|
17
|
-
::File.join(instance.read_entry(:fs_path), 'wp-content', 'uploads')
|
18
|
-
end
|
19
|
-
|
20
|
-
def dump_command
|
21
|
-
instance.host_env.command('tar', '-czf', '-', '-C', uploads_path, '.')
|
22
|
-
end
|
23
|
-
|
24
|
-
def load_command
|
25
|
-
instance.host_env.command('tar', '-xzf', '-', '-C', uploads_path)
|
26
|
-
end
|
27
|
-
|
28
|
-
def clear_uploads
|
29
|
-
infom "Removing all files under #{uploads_path}..."
|
30
|
-
instance.host_env.command('find', uploads_path, '-mindepth', 1, '-delete').execute!
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|