itamae-mitsurin 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +26 -0
- data/README.md +44 -0
- data/Rakefile +55 -0
- data/bin/itamae +5 -0
- data/bin/itamae-mitsurin +5 -0
- data/itamae-mitsurin.gemspec +35 -0
- data/lib/itamae.rb +18 -0
- data/lib/itamae/backend.rb +293 -0
- data/lib/itamae/cli.rb +86 -0
- data/lib/itamae/definition.rb +40 -0
- data/lib/itamae/ext.rb +1 -0
- data/lib/itamae/ext/specinfra.rb +39 -0
- data/lib/itamae/generators.rb +20 -0
- data/lib/itamae/generators/cookbook.rb +22 -0
- data/lib/itamae/generators/project.rb +22 -0
- data/lib/itamae/generators/role.rb +22 -0
- data/lib/itamae/generators/templates/cookbook/default.rb +0 -0
- data/lib/itamae/generators/templates/cookbook/files/.keep +0 -0
- data/lib/itamae/generators/templates/cookbook/templates/.keep +0 -0
- data/lib/itamae/generators/templates/project/Gemfile +4 -0
- data/lib/itamae/generators/templates/project/cookbooks/.keep +0 -0
- data/lib/itamae/generators/templates/project/roles/.keep +0 -0
- data/lib/itamae/generators/templates/role/default.rb +0 -0
- data/lib/itamae/generators/templates/role/files/.keep +0 -0
- data/lib/itamae/generators/templates/role/templates/.keep +0 -0
- data/lib/itamae/handler.rb +21 -0
- data/lib/itamae/handler/base.rb +40 -0
- data/lib/itamae/handler/debug.rb +10 -0
- data/lib/itamae/handler/fluentd.rb +44 -0
- data/lib/itamae/handler/json.rb +22 -0
- data/lib/itamae/handler_proxy.rb +38 -0
- data/lib/itamae/logger.rb +124 -0
- data/lib/itamae/mitsurin.rb +13 -0
- data/lib/itamae/mitsurin/cli.rb +56 -0
- data/lib/itamae/mitsurin/creators.rb +19 -0
- data/lib/itamae/mitsurin/creators/cookbook.rb +24 -0
- data/lib/itamae/mitsurin/creators/project.rb +24 -0
- data/lib/itamae/mitsurin/creators/templates/project/.rspec +2 -0
- data/lib/itamae/mitsurin/creators/templates/project/Gemfile +3 -0
- data/lib/itamae/mitsurin/creators/templates/project/Rakefile +2 -0
- data/lib/itamae/mitsurin/creators/templates/project/environments/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/environments/sample.json +7 -0
- data/lib/itamae/mitsurin/creators/templates/project/nodes/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/nodes/sample01.json +8 -0
- data/lib/itamae/mitsurin/creators/templates/project/roles/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/attributes/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/files/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/default.rb +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/spec/default_spec.rb +2 -0
- data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/templates/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/project/spec/spec_helper.rb +32 -0
- data/lib/itamae/mitsurin/creators/templates/project/tmp-nodes/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/site-cookbooks/attributes/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/site-cookbooks/files/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/site-cookbooks/recipes/default.rb +0 -0
- data/lib/itamae/mitsurin/creators/templates/site-cookbooks/spec/.keep +0 -0
- data/lib/itamae/mitsurin/creators/templates/site-cookbooks/templates/.keep +0 -0
- data/lib/itamae/mitsurin/itamae_task.rb +199 -0
- data/lib/itamae/mitsurin/serverspec_task.rb +110 -0
- data/lib/itamae/mitsurin/version.rb +5 -0
- data/lib/itamae/mitsurin/version.txt +1 -0
- data/lib/itamae/node.rb +74 -0
- data/lib/itamae/notification.rb +46 -0
- data/lib/itamae/recipe.rb +171 -0
- data/lib/itamae/recipe_children.rb +86 -0
- data/lib/itamae/resource.rb +73 -0
- data/lib/itamae/resource/aws_ebs_volume.rb +84 -0
- data/lib/itamae/resource/base.rb +374 -0
- data/lib/itamae/resource/directory.rb +63 -0
- data/lib/itamae/resource/execute.rb +26 -0
- data/lib/itamae/resource/file.rb +176 -0
- data/lib/itamae/resource/gem_package.rb +81 -0
- data/lib/itamae/resource/git.rb +94 -0
- data/lib/itamae/resource/group.rb +42 -0
- data/lib/itamae/resource/http_request.rb +71 -0
- data/lib/itamae/resource/link.rb +33 -0
- data/lib/itamae/resource/local_ruby_block.rb +15 -0
- data/lib/itamae/resource/package.rb +44 -0
- data/lib/itamae/resource/remote_directory.rb +84 -0
- data/lib/itamae/resource/remote_file.rb +54 -0
- data/lib/itamae/resource/service.rb +69 -0
- data/lib/itamae/resource/template.rb +53 -0
- data/lib/itamae/resource/user.rb +93 -0
- data/lib/itamae/runner.rb +124 -0
- data/spec/integration/Vagrantfile +35 -0
- data/spec/integration/default_spec.rb +226 -0
- data/spec/integration/recipes/default.rb +423 -0
- data/spec/integration/recipes/default2.rb +6 -0
- data/spec/integration/recipes/define/default.rb +6 -0
- data/spec/integration/recipes/define/files/remote_file_in_definition +1 -0
- data/spec/integration/recipes/dry_run.rb +6 -0
- data/spec/integration/recipes/files/remote_file_auto +1 -0
- data/spec/integration/recipes/hello.erb +6 -0
- data/spec/integration/recipes/hello.txt +1 -0
- data/spec/integration/recipes/included.rb +9 -0
- data/spec/integration/recipes/node.json +3 -0
- data/spec/integration/recipes/redefine.rb +20 -0
- data/spec/integration/recipes/templates/template_auto.erb +6 -0
- data/spec/integration/spec_helper.rb +42 -0
- data/spec/unit/lib/itamae/backend_spec.rb +95 -0
- data/spec/unit/lib/itamae/handler/base_spec.rb +34 -0
- data/spec/unit/lib/itamae/handler/fluentd_spec.rb +19 -0
- data/spec/unit/lib/itamae/handler_proxy_spec.rb +38 -0
- data/spec/unit/lib/itamae/handler_spec.rb +11 -0
- data/spec/unit/lib/itamae/node_spec.rb +14 -0
- data/spec/unit/lib/itamae/recipe_spec.rb +6 -0
- data/spec/unit/lib/itamae/resource/base_spec.rb +127 -0
- data/spec/unit/lib/itamae/resource_spec.rb +23 -0
- data/spec/unit/lib/itamae/runner_spec.rb +32 -0
- data/spec/unit/spec_helper.rb +23 -0
- metadata +393 -0
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'serverspec'
|
2
|
+
require 'rake'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'json'
|
5
|
+
require 'simple_color'
|
6
|
+
include Rake::DSL if defined? Rake::DSL
|
7
|
+
|
8
|
+
module Itamae
|
9
|
+
module Mitsurin
|
10
|
+
class ServerspecTask
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
task :spec => 'spec:all'
|
15
|
+
task :default => :spec
|
16
|
+
|
17
|
+
def self.get_roles(node_file)
|
18
|
+
roles = []
|
19
|
+
JSON.parse(File.read(node_file))['run_list'].each do |role|
|
20
|
+
roles << role.gsub(/role\[(.+)\]/, '\1') if /role\[(.+)\]/ === role
|
21
|
+
end
|
22
|
+
roles
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.get_recipes(role)
|
26
|
+
recipes = []
|
27
|
+
JSON.parse(File.read("roles/#{role}.json"))['run_list'].each do |recipe|
|
28
|
+
if /recipe\[(.+)::(.+)\]/ === recipe
|
29
|
+
recipes << {recipe.gsub(/recipe\[(.+)::(.+)\]/, '\1') => recipe.gsub(/recipe\[(.+)::(.+)\]/, '\2')}
|
30
|
+
else
|
31
|
+
recipes << {recipe.gsub(/recipe\[(.+)\]/, '\1') => nil}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
recipes
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.get_node_recipes(node_file)
|
38
|
+
recipes = []
|
39
|
+
JSON.parse(File.read(node_file))['run_list'].each do |recipe|
|
40
|
+
if /recipe\[(.+)::(.+)\]/ === recipe
|
41
|
+
recipes << {recipe.gsub(/recipe\[(.+)::(.+)\]/, '\1') => recipe.gsub(/recipe\[(.+)::(.+)\]/, '\2')}
|
42
|
+
else
|
43
|
+
recipes << {recipe.gsub(/recipe\[(.+)\]/, '\1') => nil} unless /role\[(.+)\]/ === recipe
|
44
|
+
end
|
45
|
+
end
|
46
|
+
recipes
|
47
|
+
end
|
48
|
+
|
49
|
+
namespace :spec do
|
50
|
+
all = []
|
51
|
+
Dir.glob("tmp-nodes/**/*.json").each do |node_file|
|
52
|
+
|
53
|
+
file_name = File.basename(node_file, '.json')
|
54
|
+
node_attr = JSON.parse(File.read(node_file), symbolize_names: true)
|
55
|
+
desc "Run to #{file_name}"
|
56
|
+
|
57
|
+
begin
|
58
|
+
recipes = []
|
59
|
+
get_roles(node_file).each do |role|
|
60
|
+
recipes << get_recipes(role)
|
61
|
+
end
|
62
|
+
get_node_recipes(node_file).each do |recipe|
|
63
|
+
recipes << recipe
|
64
|
+
end
|
65
|
+
rescue Exception => e
|
66
|
+
puts e.class.to_s + ", " + e.backtrace[0].to_s
|
67
|
+
puts "nodefile or role error, nodefile:#{node_file} reason:#{e.message}"
|
68
|
+
exit 1
|
69
|
+
else
|
70
|
+
recipes << {'_base' => nil}
|
71
|
+
recipes.flatten!
|
72
|
+
end
|
73
|
+
|
74
|
+
node_name = node_attr[:environments][:hostname]
|
75
|
+
ssh_user = node_attr[:environments][:ssh_user]
|
76
|
+
ssh_password = node_attr[:environments][:ssh_password]
|
77
|
+
ssh_port = node_attr[:environments][:ssh_port]
|
78
|
+
ssh_key = node_attr[:environments][:ssh_key]
|
79
|
+
|
80
|
+
node_short = node_name.split(".")[0]
|
81
|
+
all << node_short
|
82
|
+
|
83
|
+
desc "Run spec to #{file_name}"
|
84
|
+
RSpec::Core::RakeTask.new(node_short.to_sym) do |t|
|
85
|
+
ENV['TARGET_HOST'] = node_name
|
86
|
+
ENV['NODE_FILE'] = node_file
|
87
|
+
ENV['SSH_PASSWORD'] = ssh_password
|
88
|
+
ENV['SSH_KEY'] = "keys/#{ssh_key}"
|
89
|
+
|
90
|
+
specs = []
|
91
|
+
spec_recips = []
|
92
|
+
recipes.each {|hash|
|
93
|
+
specs << hash.keys.join
|
94
|
+
spec_recips << hash.values.join unless hash.values.join.empty?
|
95
|
+
}
|
96
|
+
|
97
|
+
t.pattern = "site-cookbooks/**/\{#{specs.join(',')}\}/spec/\{default,#{spec_recips.join(',')}\}_spec.rb"
|
98
|
+
t.fail_on_error = true
|
99
|
+
color = SimpleColor.new
|
100
|
+
color.echos(:red, "Run Serverspec to #{node_name}")
|
101
|
+
color.echos(:green, "Run List to #{specs.uniq.sort.join(", ")}")
|
102
|
+
end
|
103
|
+
task :all => all
|
104
|
+
task :default => :all
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
0.1
|
data/lib/itamae/node.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'itamae'
|
2
|
+
require 'hashie'
|
3
|
+
require 'json'
|
4
|
+
require 'schash'
|
5
|
+
|
6
|
+
module Itamae
|
7
|
+
class Node
|
8
|
+
ValidationError = Class.new(StandardError)
|
9
|
+
|
10
|
+
attr_reader :mash
|
11
|
+
|
12
|
+
def initialize(hash, backend)
|
13
|
+
@mash = Hashie::Mash.new(hash)
|
14
|
+
@backend = backend
|
15
|
+
end
|
16
|
+
|
17
|
+
def reverse_merge(other_hash)
|
18
|
+
self.class.new(_reverse_merge(other_hash), @backend)
|
19
|
+
end
|
20
|
+
|
21
|
+
def reverse_merge!(other_hash)
|
22
|
+
@mash.replace(_reverse_merge(other_hash))
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](key)
|
26
|
+
if @mash.has_key?(key)
|
27
|
+
@mash[key]
|
28
|
+
else
|
29
|
+
fetch_inventory_value(key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate!(&block)
|
34
|
+
errors = Schash::Validator.new(&block).validate(@mash)
|
35
|
+
unless errors.empty?
|
36
|
+
errors.each do |error|
|
37
|
+
Itamae.logger.error "'#{error.position.join('->')}' #{error.message}"
|
38
|
+
end
|
39
|
+
raise ValidationError
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def _reverse_merge(other_hash)
|
46
|
+
Hashie::Mash.new(other_hash).merge(@mash)
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_missing(method, *args)
|
50
|
+
if @mash.respond_to?(method)
|
51
|
+
return @mash.public_send(method, *args)
|
52
|
+
elsif args.empty? && value = fetch_inventory_value(method)
|
53
|
+
return value
|
54
|
+
end
|
55
|
+
|
56
|
+
super
|
57
|
+
end
|
58
|
+
|
59
|
+
def respond_to?(method, priv = false)
|
60
|
+
@mash.respond_to?(method, priv) || super
|
61
|
+
end
|
62
|
+
|
63
|
+
def fetch_inventory_value(key)
|
64
|
+
value = @backend.host_inventory[key]
|
65
|
+
if value.is_a?(Hash)
|
66
|
+
value = Hashie::Mash.new(value)
|
67
|
+
end
|
68
|
+
|
69
|
+
value
|
70
|
+
rescue NotImplementedError, NameError
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'itamae'
|
2
|
+
|
3
|
+
module Itamae
|
4
|
+
class Notification < Struct.new(:defined_in_resource, :action, :target_resource_desc, :timing)
|
5
|
+
def self.create(*args)
|
6
|
+
self.new(*args).tap(&:validate!)
|
7
|
+
end
|
8
|
+
|
9
|
+
def resource
|
10
|
+
runner.children.find_resource_by_description(target_resource_desc)
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
action_resource.run(action)
|
15
|
+
end
|
16
|
+
|
17
|
+
def action_resource
|
18
|
+
resource
|
19
|
+
end
|
20
|
+
|
21
|
+
def runner
|
22
|
+
defined_in_resource.recipe.runner
|
23
|
+
end
|
24
|
+
|
25
|
+
def delayed?
|
26
|
+
[:delay, :delayed].include?(timing)
|
27
|
+
end
|
28
|
+
|
29
|
+
def immediately?
|
30
|
+
timing == :immediately
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate!
|
34
|
+
unless [:delay, :delayed, :immediately].include?(timing)
|
35
|
+
Itamae.logger.error "'#{timing}' is not valid notification timing. (Valid option is delayed or immediately)"
|
36
|
+
abort
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Subscription < Notification
|
42
|
+
def action_resource
|
43
|
+
defined_in_resource
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'itamae'
|
2
|
+
|
3
|
+
module Itamae
|
4
|
+
class Recipe
|
5
|
+
NotFoundError = Class.new(StandardError)
|
6
|
+
|
7
|
+
attr_reader :path
|
8
|
+
attr_reader :runner
|
9
|
+
attr_reader :children
|
10
|
+
attr_reader :delayed_notifications
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def find_recipe_in_gem(recipe)
|
14
|
+
plugin_name, recipe_file = recipe.split('::', 2)
|
15
|
+
recipe_file = recipe_file.gsub("::", "/") if recipe_file
|
16
|
+
|
17
|
+
gem_name = "itamae-plugin-recipe-#{plugin_name}"
|
18
|
+
begin
|
19
|
+
gem gem_name
|
20
|
+
rescue LoadError
|
21
|
+
end
|
22
|
+
spec = Gem.loaded_specs.values.find do |spec|
|
23
|
+
spec.name == gem_name
|
24
|
+
end
|
25
|
+
|
26
|
+
return nil unless spec
|
27
|
+
|
28
|
+
candidate_files = []
|
29
|
+
if recipe_file
|
30
|
+
recipe_file += '.rb' unless recipe_file.end_with?('.rb')
|
31
|
+
candidate_files << "#{plugin_name}/#{recipe_file}"
|
32
|
+
else
|
33
|
+
candidate_files << "#{plugin_name}/default.rb"
|
34
|
+
candidate_files << "#{plugin_name}.rb"
|
35
|
+
end
|
36
|
+
|
37
|
+
candidate_files.map do |file|
|
38
|
+
File.join(spec.lib_dirs_glob, 'itamae', 'plugin', 'recipe', file)
|
39
|
+
end.find do |path|
|
40
|
+
File.exist?(path)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize(runner, path)
|
46
|
+
@runner = runner
|
47
|
+
@path = path
|
48
|
+
@delayed_notifications = []
|
49
|
+
@children = RecipeChildren.new
|
50
|
+
end
|
51
|
+
|
52
|
+
def dir
|
53
|
+
::File.dirname(@path)
|
54
|
+
end
|
55
|
+
|
56
|
+
def load(vars = {})
|
57
|
+
context = EvalContext.new(self, vars)
|
58
|
+
context.instance_eval(File.read(path), path, 1)
|
59
|
+
end
|
60
|
+
|
61
|
+
def run
|
62
|
+
show_banner
|
63
|
+
|
64
|
+
@runner.handler.event(:recipe, path: @path) do
|
65
|
+
Itamae.logger.with_indent do
|
66
|
+
@children.run
|
67
|
+
run_delayed_notifications
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def run_delayed_notifications
|
75
|
+
@delayed_notifications.uniq! do |notification|
|
76
|
+
[notification.action, notification.action_resource]
|
77
|
+
end
|
78
|
+
|
79
|
+
while notification = @delayed_notifications.shift
|
80
|
+
notification.run
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def show_banner
|
85
|
+
Itamae.logger.info "Recipe: #{@path}"
|
86
|
+
end
|
87
|
+
|
88
|
+
class EvalContext
|
89
|
+
def initialize(recipe, vars)
|
90
|
+
@recipe = recipe
|
91
|
+
|
92
|
+
vars.each do |k, v|
|
93
|
+
define_singleton_method(k) { v }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def respond_to_missing?(method, include_private = false)
|
98
|
+
Resource.get_resource_class(method)
|
99
|
+
true
|
100
|
+
rescue NameError
|
101
|
+
false
|
102
|
+
end
|
103
|
+
|
104
|
+
def method_missing(*args, &block)
|
105
|
+
super unless args.size == 2
|
106
|
+
|
107
|
+
method, name = args
|
108
|
+
begin
|
109
|
+
klass = Resource.get_resource_class(method)
|
110
|
+
rescue NameError
|
111
|
+
super
|
112
|
+
end
|
113
|
+
|
114
|
+
resource = klass.new(@recipe, name, &block)
|
115
|
+
@recipe.children << resource
|
116
|
+
end
|
117
|
+
|
118
|
+
def define(name, params = {}, &block)
|
119
|
+
Resource.define_resource(name, Definition.create_class(name, params, @recipe, &block))
|
120
|
+
end
|
121
|
+
|
122
|
+
def include_recipe(target)
|
123
|
+
expanded_path = ::File.expand_path(target, File.dirname(@recipe.path))
|
124
|
+
expanded_path = ::File.join(expanded_path, 'default.rb') if ::Dir.exists?(expanded_path)
|
125
|
+
expanded_path.concat('.rb') unless expanded_path.end_with?('.rb')
|
126
|
+
candidate_paths = [expanded_path, Recipe.find_recipe_in_gem(target)].compact
|
127
|
+
path = candidate_paths.find {|path| File.exist?(path) }
|
128
|
+
|
129
|
+
unless path
|
130
|
+
raise NotFoundError, "Recipe not found. (#{target})"
|
131
|
+
end
|
132
|
+
|
133
|
+
if runner.children.find_recipe_by_path(path)
|
134
|
+
Itamae.logger.debug "Recipe, #{path}, is skipped because it is already included"
|
135
|
+
return
|
136
|
+
end
|
137
|
+
|
138
|
+
recipe = Recipe.new(runner, path)
|
139
|
+
@recipe.children << recipe
|
140
|
+
recipe.load
|
141
|
+
end
|
142
|
+
|
143
|
+
def node
|
144
|
+
runner.node
|
145
|
+
end
|
146
|
+
|
147
|
+
def runner
|
148
|
+
@recipe.runner
|
149
|
+
end
|
150
|
+
|
151
|
+
def run_command(*args)
|
152
|
+
runner.backend.run_command(*args)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
class RecipeFromDefinition < Recipe
|
157
|
+
attr_accessor :definition
|
158
|
+
|
159
|
+
def load(vars = {})
|
160
|
+
context = EvalContext.new(self, vars)
|
161
|
+
context.instance_eval(&@definition.class.definition_block)
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
|
166
|
+
def show_banner
|
167
|
+
Itamae.logger.debug "#{@definition.resource_type}[#{@definition.resource_name}]"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Itamae
|
2
|
+
class RecipeChildren < Array
|
3
|
+
NotFoundError = Class.new(StandardError)
|
4
|
+
|
5
|
+
def find_resource_by_description(desc)
|
6
|
+
# desc is like 'resource_type[name]'
|
7
|
+
resources.find do |resource|
|
8
|
+
type, name = Itamae::Resource.parse_description(desc)
|
9
|
+
resource.resource_type == type && resource.resource_name == name
|
10
|
+
end.tap do |resource|
|
11
|
+
unless resource
|
12
|
+
raise NotFoundError, "'#{desc}' resource is not found."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def subscribing(target)
|
18
|
+
resources.map do |resource|
|
19
|
+
resource.subscriptions.select do |subscription|
|
20
|
+
subscription.resource == target
|
21
|
+
end
|
22
|
+
end.flatten
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_recipe_by_path(path)
|
26
|
+
recipes.find do |recipe|
|
27
|
+
recipe.path == path
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def resources
|
32
|
+
self.map do |item|
|
33
|
+
case item
|
34
|
+
when Resource::Base
|
35
|
+
item
|
36
|
+
when Recipe
|
37
|
+
item.children.resources
|
38
|
+
end
|
39
|
+
end.flatten
|
40
|
+
end
|
41
|
+
|
42
|
+
def recipes(options = {})
|
43
|
+
options = {recursive: true}.merge(options)
|
44
|
+
|
45
|
+
self.select do |item|
|
46
|
+
item.is_a?(Recipe)
|
47
|
+
end.map do |recipe|
|
48
|
+
if options[:recursive]
|
49
|
+
[recipe] + recipe.children.recipes
|
50
|
+
else
|
51
|
+
recipe
|
52
|
+
end
|
53
|
+
end.flatten
|
54
|
+
end
|
55
|
+
|
56
|
+
def run
|
57
|
+
self.each do |resource|
|
58
|
+
resource.run
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# returns dependencies graph in DOT
|
63
|
+
def dependency_in_dot
|
64
|
+
result = ""
|
65
|
+
result << "digraph recipes {\n"
|
66
|
+
result << " rankdir=LR;\n"
|
67
|
+
result << _dependency_in_dot
|
68
|
+
result << "}"
|
69
|
+
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
def _dependency_in_dot
|
74
|
+
result = ""
|
75
|
+
|
76
|
+
recipes(recursive: false).each do |recipe|
|
77
|
+
recipe.children.recipes(recursive: false).each do |child_recipe|
|
78
|
+
result << %{ "#{recipe.path}" -> "#{child_recipe.path}";\n}
|
79
|
+
end
|
80
|
+
result << recipe.children._dependency_in_dot
|
81
|
+
end
|
82
|
+
|
83
|
+
result
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|