itamae-mitsurin 0.50 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +5 -5
  2. data/lib/itamae-mitsurin/cli.rb +0 -2
  3. data/lib/itamae-mitsurin/logger.rb +74 -20
  4. data/lib/itamae-mitsurin/mitsurin/base.rb +109 -0
  5. data/lib/itamae-mitsurin/mitsurin/base_task.rb +147 -0
  6. data/lib/itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/default.rb +0 -11
  7. data/lib/itamae-mitsurin/mitsurin/itamae_task.rb +44 -130
  8. data/lib/itamae-mitsurin/mitsurin/itamae_with_target_task.rb +64 -157
  9. data/lib/itamae-mitsurin/mitsurin/local_task.rb +57 -131
  10. data/lib/itamae-mitsurin/mitsurin/serverspec_task.rb +56 -81
  11. data/lib/itamae-mitsurin/mitsurin/serverspec_with_target_task.rb +80 -143
  12. data/lib/itamae-mitsurin/node.rb +0 -1
  13. data/lib/itamae-mitsurin/notification.rb +0 -1
  14. data/lib/itamae-mitsurin/recipe.rb +2 -5
  15. data/lib/itamae-mitsurin/resource.rb +1 -1
  16. data/lib/itamae-mitsurin/resource/base.rb +0 -12
  17. data/lib/itamae-mitsurin/resource/http_request.rb +23 -8
  18. data/lib/itamae-mitsurin/resource/s3_file.rb +33 -0
  19. data/lib/itamae-mitsurin/runner.rb +4 -5
  20. data/lib/itamae-mitsurin/version.txt +1 -1
  21. metadata +6 -221
  22. data/lib/itamae-mitsurin/mitsurin/itamae_with_git_task.rb +0 -159
  23. data/lib/itamae-mitsurin/mitsurin/task_base.rb +0 -100
  24. data/test/test_itamae-mitsurin.rb +0 -18
  25. data/test/test_itamae-mitsurin/ext/test_specinfra.rb +0 -39
  26. data/test/test_itamae-mitsurin/handler/test_base.rb +0 -40
  27. data/test/test_itamae-mitsurin/handler/test_debug.rb +0 -10
  28. data/test/test_itamae-mitsurin/handler/test_fluentd.rb +0 -44
  29. data/test/test_itamae-mitsurin/handler/test_json.rb +0 -22
  30. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/.rspec +0 -2
  31. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/Gemfile +0 -3
  32. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/Rakefile +0 -2
  33. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/environments/.keep +0 -0
  34. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/environments/sample.json +0 -7
  35. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/keys/.keep +0 -0
  36. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/nodes/.keep +0 -0
  37. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/nodes/sample01.json +0 -9
  38. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/roles/.keep +0 -0
  39. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/attributes/.keep +0 -0
  40. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/files/.keep +0 -0
  41. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/default.rb +0 -1
  42. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/spec/default_spec.rb +0 -1
  43. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/templates/.keep +0 -0
  44. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/spec/spec_helper.rb +0 -33
  45. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/tmp-nodes/.keep +0 -0
  46. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/attributes/.keep +0 -0
  47. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/files/.keep +0 -0
  48. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/recipes/default.rb +0 -0
  49. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/spec/.keep +0 -0
  50. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/templates/.keep +0 -0
  51. data/test/test_itamae-mitsurin/mitsurin/creators/test_cookbook.rb +0 -24
  52. data/test/test_itamae-mitsurin/mitsurin/creators/test_project.rb +0 -24
  53. data/test/test_itamae-mitsurin/mitsurin/test_cli.rb +0 -56
  54. data/test/test_itamae-mitsurin/mitsurin/test_creators.rb +0 -19
  55. data/test/test_itamae-mitsurin/mitsurin/test_itamae_task.rb +0 -197
  56. data/test/test_itamae-mitsurin/mitsurin/test_itamae_with_git_task.rb +0 -213
  57. data/test/test_itamae-mitsurin/mitsurin/test_serverspec_task.rb +0 -123
  58. data/test/test_itamae-mitsurin/resource/test_aws_ebs_volume.rb +0 -84
  59. data/test/test_itamae-mitsurin/resource/test_base.rb +0 -374
  60. data/test/test_itamae-mitsurin/resource/test_directory.rb +0 -63
  61. data/test/test_itamae-mitsurin/resource/test_execute.rb +0 -26
  62. data/test/test_itamae-mitsurin/resource/test_file.rb +0 -176
  63. data/test/test_itamae-mitsurin/resource/test_gem_package.rb +0 -81
  64. data/test/test_itamae-mitsurin/resource/test_git.rb +0 -94
  65. data/test/test_itamae-mitsurin/resource/test_group.rb +0 -42
  66. data/test/test_itamae-mitsurin/resource/test_http_request.rb +0 -71
  67. data/test/test_itamae-mitsurin/resource/test_link.rb +0 -33
  68. data/test/test_itamae-mitsurin/resource/test_local_ruby_block.rb +0 -15
  69. data/test/test_itamae-mitsurin/resource/test_package.rb +0 -44
  70. data/test/test_itamae-mitsurin/resource/test_remote_directory.rb +0 -84
  71. data/test/test_itamae-mitsurin/resource/test_remote_file.rb +0 -54
  72. data/test/test_itamae-mitsurin/resource/test_service.rb +0 -69
  73. data/test/test_itamae-mitsurin/resource/test_template.rb +0 -53
  74. data/test/test_itamae-mitsurin/resource/test_user.rb +0 -93
  75. data/test/test_itamae-mitsurin/test_backend.rb +0 -297
  76. data/test/test_itamae-mitsurin/test_cli.rb +0 -88
  77. data/test/test_itamae-mitsurin/test_definition.rb +0 -40
  78. data/test/test_itamae-mitsurin/test_ext.rb +0 -1
  79. data/test/test_itamae-mitsurin/test_handler.rb +0 -21
  80. data/test/test_itamae-mitsurin/test_handler_proxy.rb +0 -38
  81. data/test/test_itamae-mitsurin/test_logger.rb +0 -124
  82. data/test/test_itamae-mitsurin/test_mitsurin.rb +0 -14
  83. data/test/test_itamae-mitsurin/test_node.rb +0 -74
  84. data/test/test_itamae-mitsurin/test_notification.rb +0 -46
  85. data/test/test_itamae-mitsurin/test_recipe.rb +0 -171
  86. data/test/test_itamae-mitsurin/test_recipe_children.rb +0 -86
  87. data/test/test_itamae-mitsurin/test_resource.rb +0 -73
  88. data/test/test_itamae-mitsurin/test_runner.rb +0 -124
  89. data/test/test_itamae-mitsurin/test_version.rb +0 -3
  90. data/test/test_itamae-mitsurin/version.txt +0 -1
  91. data/test_project/.rspec +0 -2
  92. data/test_project/Gemfile +0 -4
  93. data/test_project/Rakefile +0 -3
  94. data/test_project/environments/.keep +0 -0
  95. data/test_project/keys/.keep +0 -0
  96. data/test_project/nodes/.keep +0 -0
  97. data/test_project/nodes/test.json +0 -9
  98. data/test_project/roles/.keep +0 -0
  99. data/test_project/roles/test.json +0 -14
  100. data/test_project/site-cookbooks/_base/_base/attributes/.keep +0 -0
  101. data/test_project/site-cookbooks/_base/_base/files/.keep +0 -0
  102. data/test_project/site-cookbooks/_base/_base/recipes/default.rb +0 -1
  103. data/test_project/site-cookbooks/_base/_base/spec/default_spec.rb +0 -1
  104. data/test_project/site-cookbooks/_base/_base/templates/.keep +0 -0
  105. data/test_project/site-cookbooks/a_test/directory/attributes/.keep +0 -0
  106. data/test_project/site-cookbooks/a_test/directory/files/.keep +0 -0
  107. data/test_project/site-cookbooks/a_test/directory/recipes/default.rb +0 -8
  108. data/test_project/site-cookbooks/a_test/directory/spec/.keep +0 -0
  109. data/test_project/site-cookbooks/a_test/directory/spec/default_spec.rb +0 -4
  110. data/test_project/site-cookbooks/a_test/directory/templates/.keep +0 -0
  111. data/test_project/site-cookbooks/a_test/package/attributes/.keep +0 -0
  112. data/test_project/site-cookbooks/a_test/package/files/.keep +0 -0
  113. data/test_project/site-cookbooks/a_test/package/recipes/default.rb +0 -2
  114. data/test_project/site-cookbooks/a_test/package/spec/.keep +0 -0
  115. data/test_project/site-cookbooks/a_test/package/spec/default_spec.rb +0 -4
  116. data/test_project/site-cookbooks/a_test/package/templates/.keep +0 -0
  117. data/test_project/site-cookbooks/a_test/service/attributes/.keep +0 -0
  118. data/test_project/site-cookbooks/a_test/service/files/.keep +0 -0
  119. data/test_project/site-cookbooks/a_test/service/recipes/default.rb +0 -5
  120. data/test_project/site-cookbooks/a_test/service/spec/.keep +0 -0
  121. data/test_project/site-cookbooks/a_test/service/spec/default_spec.rb +0 -6
  122. data/test_project/site-cookbooks/a_test/service/templates/.keep +0 -0
  123. data/test_project/site-cookbooks/b_test/git/attributes/.keep +0 -0
  124. data/test_project/site-cookbooks/b_test/git/files/.keep +0 -0
  125. data/test_project/site-cookbooks/b_test/git/recipes/default.rb +0 -5
  126. data/test_project/site-cookbooks/b_test/git/spec/.keep +0 -0
  127. data/test_project/site-cookbooks/b_test/git/spec/default_spec.rb +0 -5
  128. data/test_project/site-cookbooks/b_test/git/templates/.keep +0 -0
  129. data/test_project/site-cookbooks/b_test/link/attributes/.keep +0 -0
  130. data/test_project/site-cookbooks/b_test/link/files/.keep +0 -0
  131. data/test_project/site-cookbooks/b_test/link/recipes/default.rb +0 -5
  132. data/test_project/site-cookbooks/b_test/link/spec/.keep +0 -0
  133. data/test_project/site-cookbooks/b_test/link/spec/default_spec.rb +0 -5
  134. data/test_project/site-cookbooks/b_test/link/templates/.keep +0 -0
  135. data/test_project/site-cookbooks/b_test/remote_file/attributes/.keep +0 -0
  136. data/test_project/site-cookbooks/b_test/remote_file/files/.keep +0 -0
  137. data/test_project/site-cookbooks/b_test/remote_file/files/remote_file.sh +0 -1
  138. data/test_project/site-cookbooks/b_test/remote_file/recipes/default.rb +0 -9
  139. data/test_project/site-cookbooks/b_test/remote_file/spec/.keep +0 -0
  140. data/test_project/site-cookbooks/b_test/remote_file/spec/default_spec.rb +0 -7
  141. data/test_project/site-cookbooks/b_test/remote_file/templates/.keep +0 -0
  142. data/test_project/site-cookbooks/b_test/template/attributes/.keep +0 -0
  143. data/test_project/site-cookbooks/b_test/template/files/.keep +0 -0
  144. data/test_project/site-cookbooks/b_test/template/recipes/default.rb +0 -5
  145. data/test_project/site-cookbooks/b_test/template/spec/.keep +0 -0
  146. data/test_project/site-cookbooks/b_test/template/spec/default_spec.rb +0 -7
  147. data/test_project/site-cookbooks/b_test/template/templates/.keep +0 -0
  148. data/test_project/site-cookbooks/b_test/template/templates/template.erb +0 -2
  149. data/test_project/site-cookbooks/c_test/execute/attributes/.keep +0 -0
  150. data/test_project/site-cookbooks/c_test/execute/files/.keep +0 -0
  151. data/test_project/site-cookbooks/c_test/execute/recipes/default.rb +0 -6
  152. data/test_project/site-cookbooks/c_test/execute/spec/.keep +0 -0
  153. data/test_project/site-cookbooks/c_test/execute/spec/default_spec.rb +0 -6
  154. data/test_project/site-cookbooks/c_test/execute/templates/.keep +0 -0
  155. data/test_project/site-cookbooks/c_test/remote_directory/attributes/.keep +0 -0
  156. data/test_project/site-cookbooks/c_test/remote_directory/files/.keep +0 -0
  157. data/test_project/site-cookbooks/c_test/remote_directory/recipes/default.rb +0 -10
  158. data/test_project/site-cookbooks/c_test/remote_directory/spec/.keep +0 -0
  159. data/test_project/site-cookbooks/c_test/remote_directory/spec/default_spec.rb +0 -20
  160. data/test_project/site-cookbooks/c_test/remote_directory/templates/.keep +0 -0
  161. data/test_project/site-cookbooks/c_test/remote_directory/templates/remote_dir/a.txt +0 -0
  162. data/test_project/site-cookbooks/c_test/remote_directory/templates/remote_dir/b.txt +0 -0
  163. data/test_project/site-cookbooks/c_test/remote_directory/templates/remote_dir/c.txt +0 -0
  164. data/test_project/site-cookbooks/d_spec/air/attributes/.keep +0 -0
  165. data/test_project/site-cookbooks/d_spec/air/files/.keep +0 -0
  166. data/test_project/site-cookbooks/d_spec/air/recipes/default.rb +0 -0
  167. data/test_project/site-cookbooks/d_spec/air/spec/.keep +0 -0
  168. data/test_project/site-cookbooks/d_spec/air/spec/default_spec.rb +0 -0
  169. data/test_project/site-cookbooks/d_spec/air/templates/.keep +0 -0
  170. data/test_project/spec/spec_helper.rb +0 -33
  171. data/test_project/tmp-nodes/.keep +0 -0
  172. data/test_project/tmp-nodes/test.json +0 -12
@@ -1,185 +1,92 @@
1
- require 'itamae-mitsurin/mitsurin/task_base'
2
- include Rake::DSL if defined? Rake::DSL
1
+ require 'itamae-mitsurin/mitsurin/base_task'
3
2
 
4
3
  module ItamaeMitsurin
5
4
  module Mitsurin
6
- class ItamaeWithTargetTask
5
+ class ItamaeWithTargetTask < BaseTask
6
+ ChangeTargetError = Class.new(StandardError)
7
+
8
+ ItamaeMitsurin.logger.formatter.colored = true
9
+ task = ItamaeWithTargetTask.new
7
10
 
8
11
  namespace :itamae do
9
12
  all = []
10
- if (ARGV[0] == '-T' || ARGV[0] == '--tasks') && ARGV[1] != nil
11
- if File.exists?("nodes/#{ARGV[1]}")
12
- project_h = {:project => ARGV[1]}
13
- File.open "Project.json", 'w' do |f|
13
+
14
+ begin
15
+ project = { project: ARGV[1] }
16
+
17
+ if (ARGV[0] == '-T' || ARGV[0] == '--tasks') && !project[:project].nil?
18
+ unless Dir.exist?("nodes/#{project[:project]}")
19
+ raise ChangeTargetError, "'#{project[:project]}' project is not exist."
20
+ end
21
+
22
+ File.open 'Project.json', 'w' do |f|
14
23
  f.flock File::LOCK_EX
15
- f.puts project_h.to_json
24
+ f.puts project.to_json
16
25
  f.flock File::LOCK_UN
17
26
  end
18
- puts TaskBase.hl.color "Changed target mode '#{ARGV[1]}'", :green
19
- else
20
- raise "Change mode error '#{ARGV[1]}' is not exists"
27
+
28
+ resp = JSON.parse(File.read('Project.json'))
29
+ target = resp['project'] << '/**'
30
+
31
+ ItamaeMitsurin.logger.color(:green) do
32
+ ItamaeMitsurin.logger.info "Changed target mode '#{project[:project]}'"
33
+ end
21
34
  end
35
+ rescue => e
36
+ ItamaeMitsurin.logger.error e.inspect
37
+ exit 2
22
38
  end
23
39
 
24
- ret = JSON.parse(File.read("Project.json"))
25
- target = ret["project"] << '/**'
26
-
27
40
  Dir.glob("nodes/#{target}/*.json").each do |node_file|
28
- bname = File.basename(node_file, '.json')
29
-
30
41
  begin
31
- node_h = JSON.parse(File.read(node_file), symbolize_names: true)
32
- rescue JSON::ParserError => e
33
- puts e.class.to_s + ", " + e.backtrace[0].to_s
34
- puts "Node error, nodefile:#{node_file}, reason:#{e.message}"
35
- TaskBase.file_logger.fatal "Node error, nodefile:#{node_file}, reason:#{e.message}"
42
+ node_name = File.basename(node_file, '.json')
43
+ node = task.load_node_attributes(node_file)
44
+ node_short = node[:environments][:hostname].split('.')[0]
45
+ rescue => e
46
+ ItamaeMitsurin.logger.error e.inspect
47
+ ItamaeMitsurin.logger.info "From node file: #{node_file}"
48
+ exit 2
36
49
  end
37
50
 
38
- node_short = node_h[:environments][:hostname].split(".")[0]
39
51
  all << node_short
40
- desc "Itamae to all nodes"
41
- task :all => all
52
+ desc 'Itamae to all nodes'
53
+ task 'all' => all
42
54
 
43
- desc "Itamae to #{bname}"
44
- task node_h[:environments][:hostname].split(".")[0] do
55
+ desc "Itamae to #{node_name}"
56
+ task node_short do
45
57
  begin
46
- recipes = []
47
- TaskBase.get_roles(node_file).each do |role|
48
- recipes << TaskBase.get_recipes(role)
49
- end
50
- TaskBase.get_node_recipes(node_file).each do |recipe|
51
- recipes << recipe
52
- end
53
- rescue Exception => e
54
- puts e.class.to_s + ", " + e.backtrace[0].to_s
55
- puts "Node or role error, nodefile:#{node_file}, reason:#{e.message}"
56
- TaskBase.file_logger.fatal "Node or role error, nodefile:#{node_file}, reason:#{e.message}"
57
- else
58
- recipes.flatten!
59
- end
60
-
61
- # get env attr
62
- begin
63
- env_set = node_h[:environments][:set]
64
- raise "No environments set error" if env_set.nil?
65
- env_h = JSON.parse(File.read("environments/#{env_set}.json"), symbolize_names: true)
66
- rescue Exception => e
67
- puts e.class.to_s + ", " + e.backtrace[0].to_s
68
- puts "Node or environment error, nodefile:#{node_file}, reason:#{e.message}"
69
- TaskBase.file_logger.fatal "Node or environment error, nodefile:#{node_file}, reason:#{e.message}"
70
- end
71
-
72
- # get recipes attr
73
- recipe_attr_file = []
74
- recipes.each do |recipe_h|
75
- if recipe_h["#{recipe_h.keys.join}"] == "default"
76
- recipe_attr_file.insert 0,
77
- Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/attributes/#{recipe_h["#{recipe_h.keys.join}"]}.json")
78
- else
79
- recipe_attr_file <<
80
- Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/attributes/#{recipe_h["#{recipe_h.keys.join}"]}.json")
81
- end
82
- end
83
-
84
- recipe_attr_file.flatten!
85
-
86
- # recipes attr other=env
87
- recipe_env_h_a = []
88
- recipe_attr_file.each do |file|
89
- recipe_h = JSON.parse(File.read(file), symbolize_names: true)
90
- recipe_env_h_a << recipe_h.deep_merge(env_h)
91
- end
92
-
93
- # recipe attr other=recipes_env
94
- moto = recipe_env_h_a[0]
95
- recipe_env_h_a.each {|hash| moto.deep_merge!(hash)}
96
- recipe_env_h = moto
97
-
98
- if recipe_env_h.nil?
99
- # env attr other=node
100
- node_env_h = env_h.deep_merge(node_h)
101
- node_env_j = TaskBase.jq node_env_h
102
- TaskBase.write_json(bname) {|file| file.puts node_env_j}
103
- else
104
- # recipe_env attr other=node
105
- recipe_env_node_h = recipe_env_h.deep_merge(node_h)
106
- recipe_env_node_j = TaskBase.jq recipe_env_node_h
107
- TaskBase.write_json(bname) {|file| file.puts recipe_env_node_j}
108
- end
109
-
110
- recipes << {'_base' => 'default'}
111
- node_property = JSON.parse(File.read("tmp-nodes/#{bname}.json"), symbolize_names: true)
112
- node = node_property[:environments][:hostname]
113
- ssh_user = node_property[:environments][:ssh_user]
114
- ssh_password = node_property[:environments][:ssh_password]
115
- sudo_password = node_property[:environments][:sudo_password]
116
- ssh_port = node_property[:environments][:ssh_port]
117
- ssh_key = node_property[:environments][:ssh_key]
118
- local_ipv4 = node_property[:environments][:local_ipv4]
119
-
120
- ENV['TARGET_HOST'] = node
121
- ENV['NODE_FILE'] = node_file
122
- ENV['SSH_PASSWORD'] = ssh_password
123
- ENV['SUDO_PASSWORD'] = sudo_password
124
-
125
- command = "bundle exec itamae ssh"
126
- if local_ipv4.nil?
127
- command << " -h #{node}"
128
- else
129
- command << " -h #{local_ipv4}"
130
- end
131
- command << " -u #{ssh_user}"
132
- command << " -p #{ssh_port}"
133
- command << " -i keys/#{ssh_key}" unless ssh_key.nil?
134
- command << " -j tmp-nodes/#{bname}.json"
135
- command << " --shell=bash"
136
- command << " --ask-password" unless ssh_password.nil?
137
- command << " --dry-run" if ENV['dry-run'] == "true"
138
- command << " -l debug" if ENV['debug'] == "true"
139
- # command << " -c logs/config/itamae_with_target_task.config"
140
-
141
- # Pass to read the recipe command
142
- command_recipe = []
143
- recipes.each do |recipe_h|
144
- target_recipe = "site-cookbooks/**/#{recipe_h.keys.join}/recipes/#{recipe_h[recipe_h.keys.join]}.rb"
145
- if Dir.glob(target_recipe).empty?
146
- raise "Recipe load error, nodefile: #{node_file}, reason: Does not exist " +
147
- recipe_h.keys.join + '::' +recipe_h.values.join
148
- end
149
- Dir.glob(target_recipe).join("\s").split.each do |target|
150
- unless File.exists?(target)
151
- ex_recipe = recipe_h.to_s.gsub('=>', '::').gsub('"', '')
152
- raise "Recipe load error, nodefile:#{node_file}, reason: Does not exist #{ex_recipe}"
58
+ run_list = task.load_run_list(node_file)
59
+ environments = task.load_environments(node)
60
+ recipe_attributes_list = task.load_recipe_attributes(run_list)
61
+
62
+ merged_recipe = task.merge_attributes(recipe_attributes_list)
63
+ merged_environments = task.merge_attributes(merged_recipe, environments)
64
+ attributes = task.merge_attributes(merged_environments, node)
65
+ task.create_tmp_nodes(node_name, attributes)
66
+
67
+ command = task.create_itamae_command(node_name, attributes)
68
+ command_recipe = task.list_recipe_filepath(run_list)
69
+ command_recipe.sort_by! {|item| File.dirname(item) }
70
+ command << command_recipe.join
71
+
72
+ task.runner_display(attributes[:run_list], run_list, command)
73
+ st = system command
74
+ if st
75
+ ItamaeMitsurin.logger.color(:green) do
76
+ ItamaeMitsurin.logger.info 'itamae_with_target_task is completed.'
153
77
  end
154
- command_recipe << " #{target}"
155
- end
156
- end
157
-
158
- command_recipe.sort_by! {|item| File.dirname(item) }
159
- command << command_recipe.join
160
-
161
- puts TaskBase.hl.color(%!Run Itamae to "#{bname}"!, :red)
162
- TaskBase.file_logger.info(%!Run Itamae to "#{bname}"!)
163
- run_list_noti = []
164
- command_recipe.each { |c_recipe|
165
- unless c_recipe.split("/")[4].split(".")[0] == 'default'
166
- run_list_noti << c_recipe.split("/")[2] + "::#{c_recipe.split("/")[4].split(".")[0]}"
167
78
  else
168
- run_list_noti << c_recipe.split("/")[2]
79
+ ItamaeMitsurin.logger.error 'itamae_with_target_task is failed.'
80
+ exit 1
169
81
  end
170
- }
171
-
172
- puts TaskBase.hl.color(%!Run List to \"#{run_list_noti.uniq.join(", ")}\"!, :green)
173
- TaskBase.file_logger.info(%!Run List to \"#{run_list_noti.uniq.join(", ")}\"!)
174
- puts TaskBase.hl.color(%!#{command}!, :white)
175
- TaskBase.file_logger.debug(%!#{command}!)
176
- st = system command
177
- exit 1 unless st
178
- TaskBase.file_logger.info "itamae_with_target_task end."
82
+ rescue => e
83
+ ItamaeMitsurin.logger.error e.inspect
84
+ ItamaeMitsurin.logger.info "From node file: #{node_file}"
85
+ exit 2
86
+ end
179
87
  end
180
88
  end
181
89
  end
182
-
183
90
  end
184
91
  end
185
92
  end
@@ -1,154 +1,80 @@
1
- require 'itamae-mitsurin/mitsurin/task_base'
2
- include Rake::DSL if defined? Rake::DSL
3
-
4
- # For AWS Resources
1
+ require 'itamae-mitsurin/mitsurin/base_task'
5
2
 
6
3
  module ItamaeMitsurin
7
4
  module Mitsurin
8
- class LocalTask
5
+ class LocalTask < BaseTask
6
+ def create_itamae_command(node_name, hash)
7
+ command = 'bundle exec itamae local'
8
+ command << " -j tmp-nodes/#{node_name}.json"
9
+
10
+ hash[:environments][:shell] = ENV['shell'] if ENV['shell']
11
+ command << if hash[:environments][:shell]
12
+ " --shell=#{hash[:environments][:shell]}"
13
+ else
14
+ ' --shell=bash'
15
+ end
16
+
17
+ command << ' --dry-run' if ENV['dry-run'] == 'true'
18
+ command << ' --log-level=debug' if ENV['debug'] == 'true'
19
+ command
20
+ end
21
+
22
+ ItamaeMitsurin.logger.formatter.colored = true
23
+ task = LocalTask.new
24
+
9
25
  namespace :local do
10
- Dir.glob("nodes/**/*.json").each do |node_file|
11
- all = []
12
- bname = File.basename(node_file, '.json')
26
+ all = []
13
27
 
28
+ Dir.glob('nodes/**/*.json').each do |node_file|
14
29
  begin
15
- node_h = JSON.parse(File.read(node_file), symbolize_names: true)
16
- rescue JSON::ParserError => e
17
- puts e.class.to_s + ", " + e.backtrace[0].to_s
18
- puts "Node error, nodefile:#{node_file}, reason:#{e.message}"
19
- else
20
- all << node_h[:environments][:hostname].split(".")[0]
21
- task :all => all
30
+ node_name = File.basename(node_file, '.json')
31
+ node = task.load_node_attributes(node_file)
32
+ node_short = node[:environments][:hostname].split('.')[0]
33
+ rescue => e
34
+ ItamaeMitsurin.logger.error e.inspect
35
+ ItamaeMitsurin.logger.info "From node file: #{node_file}"
36
+ exit 2
22
37
  end
23
38
 
24
- desc "Local to #{bname}"
25
- task node_h[:environments][:hostname].split(".")[0] do
26
- begin
27
- recipes = []
28
- TaskBase.get_roles(node_file).each do |role|
29
- recipes << TaskBase.get_recipes(role)
30
- end
31
- TaskBase.get_node_recipes(node_file).each do |recipe|
32
- recipes << recipe
33
- end
34
- rescue Exception => e
35
- puts e.class.to_s + ", " + e.backtrace[0].to_s
36
- puts "Node or role error, nodefile:#{node_file}, reason:#{e.message}"
37
- else
38
- recipes.flatten!
39
- end
39
+ all << node_short
40
+ desc 'Itamae local to all nodes'
41
+ task 'all' => all
40
42
 
41
- # get env attr
43
+ desc "Itamae local to #{node_name}"
44
+ task node_short do
42
45
  begin
43
- env_set = node_h[:environments][:set]
44
- raise "No environments set error" if env_set.nil?
45
- env_h = JSON.parse(File.read("environments/#{env_set}.json"), symbolize_names: true)
46
- rescue Exception => e
47
- puts e.class.to_s + ", " + e.backtrace[0].to_s
48
- puts "Node or environment error, nodefile:#{node_file}, reason:#{e.message}"
49
- end
50
-
51
- # get recipes attr
52
- recipe_attr_file = []
53
- recipes.each do |recipe_h|
54
- if recipe_h["#{recipe_h.keys.join}"] == "default"
55
- recipe_attr_file.insert 0,
56
- Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/attributes/#{recipe_h["#{recipe_h.keys.join}"]}.json")
57
- else
58
- recipe_attr_file <<
59
- Dir.glob("site-cookbooks/**/#{recipe_h.keys.join}/attributes/#{recipe_h["#{recipe_h.keys.join}"]}.json")
60
- end
61
- end
62
-
63
- recipe_attr_file.flatten!
64
-
65
- # recipes attr other=env
66
- recipe_env_h_a = []
67
- recipe_attr_file.each do |file|
68
- recipe_h = JSON.parse(File.read(file), symbolize_names: true)
69
- recipe_env_h_a << recipe_h.deep_merge(env_h)
70
- end
71
-
72
- # recipe attr other=recipes_env
73
- moto = recipe_env_h_a[0]
74
- recipe_env_h_a.each {|hash| moto.deep_merge!(hash)}
75
- recipe_env_h = moto
76
-
77
- if recipe_env_h.nil?
78
- # env attr other=node
79
- node_env_h = env_h.deep_merge(node_h)
80
- node_env_j = TaskBase.jq node_env_h
81
- path = TaskBase.write_tmp_json(bname) {|file| file.puts node_env_j}
82
- else
83
- # recipe_env attr other=node
84
- recipe_env_node_h = recipe_env_h.deep_merge(node_h)
85
- recipe_env_node_j = TaskBase.jq recipe_env_node_h
86
- path = TaskBase.write_tmp_json(bname) {|file| file.puts recipe_env_node_j}
87
- end
46
+ run_list = task.load_run_list(node_file)
47
+ environments = task.load_environments(node)
48
+ recipe_attributes_list = task.load_recipe_attributes(run_list)
88
49
 
89
- recipes << {'_base' => 'default'}
90
- node_property = JSON.parse(File.read("#{path}/#{bname}.json"), symbolize_names: true)
91
- node = node_property[:environments][:hostname]
92
- sudo_password = node_property[:environments][:sudo_password]
50
+ merged_recipe = task.merge_attributes(recipe_attributes_list)
51
+ merged_environments = task.merge_attributes(merged_recipe, environments)
52
+ attributes = task.merge_attributes(merged_environments, node)
53
+ task.create_tmp_nodes(node_name, attributes)
93
54
 
94
- ENV['TARGET_HOST'] = node
95
- ENV['NODE_FILE'] = node_file
96
- ENV['SUDO_PASSWORD'] = sudo_password
55
+ command = task.create_itamae_command(node_name, attributes)
56
+ command_recipe = task.list_recipe_filepath(run_list)
57
+ command_recipe.sort_by! {|item| File.dirname(item) }
58
+ command << command_recipe.join
97
59
 
98
- command = "bundle exec itamae local"
99
- command << " -j #{path}/#{bname}.json"
100
- command << " --shell=bash"
101
- command << " --dry-run" if ENV['dry-run'] == "true"
102
- command << " -l debug" if ENV['debug'] == "true"
103
- command << " -c logs/config/local_task.config"
104
-
105
- # Pass to read the recipe command
106
- command_recipe = []
107
- recipes.each do |recipe_h|
108
- target_recipe = "site-cookbooks/**/#{recipe_h.keys.join}/recipes/#{recipe_h[recipe_h.keys.join]}.rb"
109
- if Dir.glob(target_recipe).empty?
110
- raise "Recipe load error, nodefile: #{node_file}, reason: Does not exist " +
111
- recipe_h.keys.join + '::' +recipe_h.values.join
112
- end
113
- Dir.glob(target_recipe).join("\s").split.each do |target|
114
- unless File.exists?(target)
115
- ex_recipe = recipe_h.to_s.gsub('=>', '::').gsub('"', '')
116
- raise "Recipe load error, nodefile:#{node_file}, reason: Does not exist #{ex_recipe}"
60
+ task.runner_display(attributes[:run_list], run_list, command)
61
+ st = system command
62
+ if st
63
+ ItamaeMitsurin.logger.color(:green) do
64
+ ItamaeMitsurin.logger.info 'local_task is completed.'
117
65
  end
118
- command_recipe << " #{target}"
119
- end
120
- end
121
-
122
- command_recipe.sort_by! {|item| File.dirname(item) }
123
- command << command_recipe.join
124
-
125
- puts TaskBase.hl.color(%!Run Itamae to "#{bname}"!, :red)
126
- run_list_noti = []
127
- command_recipe.each { |c_recipe|
128
- unless c_recipe.split("/")[4].split(".")[0] == 'default'
129
- run_list_noti << c_recipe.split("/")[2] + "::#{c_recipe.split("/")[4].split(".")[0]}"
130
66
  else
131
- run_list_noti << c_recipe.split("/")[2]
67
+ ItamaeMitsurin.logger.error 'local_task is failed.'
68
+ exit 1
132
69
  end
133
- }
134
-
135
- puts TaskBase.hl.color(%!Run List to \"#{run_list_noti.uniq.join(", ")}\"!, :green)
136
- puts TaskBase.hl.color(%!#{command}!, :white)
137
- begin
138
- st = system command
139
- rescue Exception => e
140
- puts "Command error, nodefile:#{node_file}, reason:#{e.message}"
141
- puts "#{e.backtrace}"
142
- ensure
143
- FileUtils.remove_entry_secure path
144
- exit 1 unless st
70
+ rescue => e
71
+ ItamaeMitsurin.logger.error e.inspect
72
+ ItamaeMitsurin.logger.info "From node file: #{node_file}"
73
+ exit 2
145
74
  end
146
75
  end
147
76
  end
148
- desc "local init all"
149
- task :local => 'local:all'
150
77
  end
151
-
152
78
  end
153
79
  end
154
80
  end