itamae-mitsurin 0.4 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -1
  4. data/Rakefile +17 -15
  5. data/bin/itamae +2 -3
  6. data/bin/manaita +5 -0
  7. data/itamae-mitsurin.gemspec +4 -4
  8. data/lib/itamae-mitsurin.rb +17 -0
  9. data/lib/{itamae → itamae-mitsurin}/backend.rb +13 -13
  10. data/lib/{itamae → itamae-mitsurin}/cli.rb +5 -5
  11. data/lib/{itamae → itamae-mitsurin}/definition.rb +2 -2
  12. data/lib/itamae-mitsurin/ext.rb +1 -0
  13. data/lib/{itamae → itamae-mitsurin}/ext/specinfra.rb +0 -0
  14. data/lib/{itamae → itamae-mitsurin}/handler.rb +3 -3
  15. data/lib/{itamae → itamae-mitsurin}/handler/base.rb +1 -1
  16. data/lib/{itamae → itamae-mitsurin}/handler/debug.rb +2 -2
  17. data/lib/{itamae → itamae-mitsurin}/handler/fluentd.rb +2 -2
  18. data/lib/{itamae → itamae-mitsurin}/handler/json.rb +1 -1
  19. data/lib/{itamae → itamae-mitsurin}/handler_proxy.rb +1 -1
  20. data/lib/{itamae → itamae-mitsurin}/logger.rb +5 -5
  21. data/lib/itamae-mitsurin/mitsurin.rb +14 -0
  22. data/lib/{itamae → itamae-mitsurin}/mitsurin/cli.rb +5 -5
  23. data/lib/{itamae → itamae-mitsurin}/mitsurin/creators.rb +3 -3
  24. data/lib/itamae-mitsurin/mitsurin/creators/cookbook.rb +24 -0
  25. data/lib/itamae-mitsurin/mitsurin/creators/project.rb +24 -0
  26. data/lib/{itamae → itamae-mitsurin}/mitsurin/creators/templates/project/.rspec +0 -0
  27. data/lib/{itamae → itamae-mitsurin}/mitsurin/creators/templates/project/Gemfile +0 -1
  28. data/lib/itamae-mitsurin/mitsurin/creators/templates/project/Rakefile +2 -0
  29. data/lib/{itamae/generators/templates/cookbook/files → itamae-mitsurin/mitsurin/creators/templates/project/environments}/.keep +0 -0
  30. data/lib/{itamae → itamae-mitsurin}/mitsurin/creators/templates/project/environments/sample.json +0 -0
  31. data/lib/{itamae/generators/templates/cookbook/templates → itamae-mitsurin/mitsurin/creators/templates/project/keys}/.keep +0 -0
  32. data/lib/{itamae/generators/templates/project/cookbooks → itamae-mitsurin/mitsurin/creators/templates/project/nodes}/.keep +0 -0
  33. data/lib/itamae-mitsurin/mitsurin/creators/templates/project/nodes/sample01.json +10 -0
  34. data/lib/{itamae/generators → itamae-mitsurin/mitsurin/creators}/templates/project/roles/.keep +0 -0
  35. data/lib/{itamae/generators/templates/role/files → itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/attributes}/.keep +0 -0
  36. data/lib/itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/default.rb +1 -0
  37. data/lib/{itamae/generators/templates/role/templates → itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/directories}/.keep +0 -0
  38. data/lib/{itamae/mitsurin/creators/templates/project/environments → itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/files}/.keep +0 -0
  39. data/lib/{itamae/mitsurin/creators/templates/project/keys → itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/templates}/.keep +0 -0
  40. data/lib/{itamae → itamae-mitsurin}/mitsurin/creators/templates/project/site-cookbooks/_base/_base/spec/default_spec.rb +0 -0
  41. data/lib/{itamae → itamae-mitsurin}/mitsurin/creators/templates/project/spec/spec_helper.rb +0 -0
  42. data/lib/{itamae/mitsurin/creators/templates/project/nodes → itamae-mitsurin/mitsurin/creators/templates/project/tmp-nodes}/.keep +0 -0
  43. data/lib/{itamae/mitsurin/creators/templates/project/roles → itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/attributes}/.keep +0 -0
  44. data/lib/{itamae/generators/templates/cookbook → itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/recipes}/default.rb +0 -0
  45. data/lib/{itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/attributes → itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/recipes/directories}/.keep +0 -0
  46. data/lib/{itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base → itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/recipes}/files/.keep +0 -0
  47. data/lib/{itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base → itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/recipes}/templates/.keep +0 -0
  48. data/lib/{itamae/generators/templates/role/default.rb → itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/spec/default_spec.rb} +0 -0
  49. data/lib/itamae-mitsurin/mitsurin/itamae_task.rb +197 -0
  50. data/lib/itamae-mitsurin/mitsurin/itamae_with_git_task.rb +213 -0
  51. data/lib/itamae-mitsurin/mitsurin/serverspec_task.rb +123 -0
  52. data/lib/{itamae → itamae-mitsurin}/node.rb +3 -3
  53. data/lib/{itamae → itamae-mitsurin}/notification.rb +3 -3
  54. data/lib/{itamae → itamae-mitsurin}/recipe.rb +6 -6
  55. data/lib/{itamae → itamae-mitsurin}/recipe_children.rb +2 -2
  56. data/lib/{itamae → itamae-mitsurin}/resource.rb +23 -23
  57. data/lib/{itamae → itamae-mitsurin}/resource/aws_ebs_volume.rb +3 -3
  58. data/lib/{itamae → itamae-mitsurin}/resource/base.rb +23 -23
  59. data/lib/{itamae → itamae-mitsurin}/resource/directory.rb +2 -2
  60. data/lib/{itamae → itamae-mitsurin}/resource/execute.rb +2 -2
  61. data/lib/itamae-mitsurin/resource/file.rb +180 -0
  62. data/lib/{itamae → itamae-mitsurin}/resource/gem_package.rb +2 -2
  63. data/lib/{itamae → itamae-mitsurin}/resource/git.rb +2 -2
  64. data/lib/{itamae → itamae-mitsurin}/resource/group.rb +2 -2
  65. data/lib/{itamae → itamae-mitsurin}/resource/http_request.rb +3 -3
  66. data/lib/{itamae → itamae-mitsurin}/resource/link.rb +2 -2
  67. data/lib/{itamae → itamae-mitsurin}/resource/local_ruby_block.rb +2 -2
  68. data/lib/{itamae → itamae-mitsurin}/resource/package.rb +2 -2
  69. data/lib/{itamae → itamae-mitsurin}/resource/remote_directory.rb +5 -5
  70. data/lib/{itamae → itamae-mitsurin}/resource/remote_file.rb +3 -3
  71. data/lib/{itamae → itamae-mitsurin}/resource/service.rb +2 -2
  72. data/lib/{itamae → itamae-mitsurin}/resource/template.rb +2 -2
  73. data/lib/{itamae → itamae-mitsurin}/resource/user.rb +2 -2
  74. data/lib/{itamae → itamae-mitsurin}/runner.rb +8 -8
  75. data/lib/{itamae/mitsurin → itamae-mitsurin}/version.rb +1 -3
  76. data/lib/itamae-mitsurin/version.txt +1 -0
  77. data/test/test_itamae-mitsurin.rb +18 -0
  78. data/test/test_itamae-mitsurin/ext/test_specinfra.rb +39 -0
  79. data/test/test_itamae-mitsurin/handler/test_base.rb +40 -0
  80. data/test/test_itamae-mitsurin/handler/test_debug.rb +10 -0
  81. data/test/test_itamae-mitsurin/handler/test_fluentd.rb +44 -0
  82. data/test/test_itamae-mitsurin/handler/test_json.rb +22 -0
  83. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/.rspec +2 -0
  84. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/Gemfile +3 -0
  85. data/{lib/itamae → test/test_itamae-mitsurin}/mitsurin/creators/templates/project/Rakefile +0 -0
  86. data/{lib/itamae/mitsurin/creators/templates/project/tmp-nodes → test/test_itamae-mitsurin/mitsurin/creators/templates/project/environments}/.keep +0 -0
  87. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/environments/sample.json +7 -0
  88. data/{lib/itamae/mitsurin/creators/templates/site-cookbooks/attributes → test/test_itamae-mitsurin/mitsurin/creators/templates/project/keys}/.keep +0 -0
  89. data/{lib/itamae/mitsurin/creators/templates/site-cookbooks/files → test/test_itamae-mitsurin/mitsurin/creators/templates/project/nodes}/.keep +0 -0
  90. data/{lib/itamae → test/test_itamae-mitsurin}/mitsurin/creators/templates/project/nodes/sample01.json +0 -0
  91. data/{lib/itamae/mitsurin/creators/templates/site-cookbooks/spec → test/test_itamae-mitsurin/mitsurin/creators/templates/project/roles}/.keep +0 -0
  92. data/{lib/itamae/mitsurin/creators/templates/site-cookbooks/templates → test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/attributes}/.keep +0 -0
  93. data/{lib/itamae/mitsurin/creators/templates/site-cookbooks/recipes/default.rb → test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/files/.keep} +0 -0
  94. data/{lib/itamae → test/test_itamae-mitsurin}/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/default.rb +0 -0
  95. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/spec/default_spec.rb +1 -0
  96. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/site-cookbooks/_base/_base/templates/.keep +0 -0
  97. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/spec/spec_helper.rb +33 -0
  98. data/test/test_itamae-mitsurin/mitsurin/creators/templates/project/tmp-nodes/.keep +0 -0
  99. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/attributes/.keep +0 -0
  100. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/files/.keep +0 -0
  101. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/recipes/default.rb +0 -0
  102. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/spec/.keep +0 -0
  103. data/test/test_itamae-mitsurin/mitsurin/creators/templates/site-cookbooks/templates/.keep +0 -0
  104. data/{lib/itamae/mitsurin/creators/cookbook.rb → test/test_itamae-mitsurin/mitsurin/creators/test_cookbook.rb} +0 -0
  105. data/{lib/itamae/mitsurin/creators/project.rb → test/test_itamae-mitsurin/mitsurin/creators/test_project.rb} +0 -0
  106. data/test/test_itamae-mitsurin/mitsurin/test_cli.rb +56 -0
  107. data/test/test_itamae-mitsurin/mitsurin/test_creators.rb +19 -0
  108. data/{lib/itamae/mitsurin/itamae_task.rb → test/test_itamae-mitsurin/mitsurin/test_itamae_task.rb} +0 -1
  109. data/{lib/itamae/mitsurin/itamae_with_git_task.rb → test/test_itamae-mitsurin/mitsurin/test_itamae_with_git_task.rb} +0 -1
  110. data/{lib/itamae/mitsurin/serverspec_task.rb → test/test_itamae-mitsurin/mitsurin/test_serverspec_task.rb} +1 -1
  111. data/test/test_itamae-mitsurin/resource/test_aws_ebs_volume.rb +84 -0
  112. data/test/test_itamae-mitsurin/resource/test_base.rb +374 -0
  113. data/test/test_itamae-mitsurin/resource/test_directory.rb +63 -0
  114. data/test/test_itamae-mitsurin/resource/test_execute.rb +26 -0
  115. data/{lib/itamae/resource/file.rb → test/test_itamae-mitsurin/resource/test_file.rb} +6 -6
  116. data/test/test_itamae-mitsurin/resource/test_gem_package.rb +81 -0
  117. data/test/test_itamae-mitsurin/resource/test_git.rb +94 -0
  118. data/test/test_itamae-mitsurin/resource/test_group.rb +42 -0
  119. data/test/test_itamae-mitsurin/resource/test_http_request.rb +71 -0
  120. data/test/test_itamae-mitsurin/resource/test_link.rb +33 -0
  121. data/test/test_itamae-mitsurin/resource/test_local_ruby_block.rb +15 -0
  122. data/test/test_itamae-mitsurin/resource/test_package.rb +44 -0
  123. data/test/test_itamae-mitsurin/resource/test_remote_directory.rb +84 -0
  124. data/test/test_itamae-mitsurin/resource/test_remote_file.rb +54 -0
  125. data/test/test_itamae-mitsurin/resource/test_service.rb +69 -0
  126. data/test/test_itamae-mitsurin/resource/test_template.rb +53 -0
  127. data/test/test_itamae-mitsurin/resource/test_user.rb +93 -0
  128. data/test/test_itamae-mitsurin/test_backend.rb +297 -0
  129. data/test/test_itamae-mitsurin/test_cli.rb +88 -0
  130. data/test/test_itamae-mitsurin/test_definition.rb +40 -0
  131. data/test/test_itamae-mitsurin/test_ext.rb +1 -0
  132. data/test/test_itamae-mitsurin/test_handler.rb +21 -0
  133. data/test/test_itamae-mitsurin/test_handler_proxy.rb +38 -0
  134. data/test/test_itamae-mitsurin/test_logger.rb +124 -0
  135. data/test/test_itamae-mitsurin/test_mitsurin.rb +14 -0
  136. data/test/test_itamae-mitsurin/test_node.rb +74 -0
  137. data/test/test_itamae-mitsurin/test_notification.rb +46 -0
  138. data/test/test_itamae-mitsurin/test_recipe.rb +171 -0
  139. data/test/test_itamae-mitsurin/test_recipe_children.rb +86 -0
  140. data/test/test_itamae-mitsurin/test_resource.rb +73 -0
  141. data/test/test_itamae-mitsurin/test_runner.rb +124 -0
  142. data/test/test_itamae-mitsurin/test_version.rb +3 -0
  143. data/{lib/itamae/mitsurin → test/test_itamae-mitsurin}/version.txt +0 -0
  144. data/test_project/.rspec +2 -0
  145. data/test_project/Gemfile +4 -0
  146. data/test_project/Rakefile +3 -0
  147. data/test_project/environments/.keep +0 -0
  148. data/test_project/keys/.keep +0 -0
  149. data/test_project/nodes/.keep +0 -0
  150. data/test_project/nodes/test.json +9 -0
  151. data/test_project/roles/.keep +0 -0
  152. data/test_project/roles/test.json +14 -0
  153. data/test_project/site-cookbooks/_base/_base/attributes/.keep +0 -0
  154. data/test_project/site-cookbooks/_base/_base/files/.keep +0 -0
  155. data/test_project/site-cookbooks/_base/_base/recipes/default.rb +1 -0
  156. data/test_project/site-cookbooks/_base/_base/spec/default_spec.rb +1 -0
  157. data/test_project/site-cookbooks/_base/_base/templates/.keep +0 -0
  158. data/test_project/site-cookbooks/a_test/directory/attributes/.keep +0 -0
  159. data/test_project/site-cookbooks/a_test/directory/files/.keep +0 -0
  160. data/test_project/site-cookbooks/a_test/directory/recipes/default.rb +8 -0
  161. data/test_project/site-cookbooks/a_test/directory/spec/.keep +0 -0
  162. data/test_project/site-cookbooks/a_test/directory/spec/default_spec.rb +4 -0
  163. data/test_project/site-cookbooks/a_test/directory/templates/.keep +0 -0
  164. data/test_project/site-cookbooks/a_test/package/attributes/.keep +0 -0
  165. data/test_project/site-cookbooks/a_test/package/files/.keep +0 -0
  166. data/test_project/site-cookbooks/a_test/package/recipes/default.rb +2 -0
  167. data/test_project/site-cookbooks/a_test/package/spec/.keep +0 -0
  168. data/test_project/site-cookbooks/a_test/package/spec/default_spec.rb +4 -0
  169. data/test_project/site-cookbooks/a_test/package/templates/.keep +0 -0
  170. data/test_project/site-cookbooks/a_test/service/attributes/.keep +0 -0
  171. data/test_project/site-cookbooks/a_test/service/files/.keep +0 -0
  172. data/test_project/site-cookbooks/a_test/service/recipes/default.rb +5 -0
  173. data/test_project/site-cookbooks/a_test/service/spec/.keep +0 -0
  174. data/test_project/site-cookbooks/a_test/service/spec/default_spec.rb +6 -0
  175. data/test_project/site-cookbooks/a_test/service/templates/.keep +0 -0
  176. data/test_project/site-cookbooks/b_test/git/attributes/.keep +0 -0
  177. data/test_project/site-cookbooks/b_test/git/files/.keep +0 -0
  178. data/test_project/site-cookbooks/b_test/git/recipes/default.rb +5 -0
  179. data/test_project/site-cookbooks/b_test/git/spec/.keep +0 -0
  180. data/test_project/site-cookbooks/b_test/git/spec/default_spec.rb +5 -0
  181. data/test_project/site-cookbooks/b_test/git/templates/.keep +0 -0
  182. data/test_project/site-cookbooks/b_test/link/attributes/.keep +0 -0
  183. data/test_project/site-cookbooks/b_test/link/files/.keep +0 -0
  184. data/test_project/site-cookbooks/b_test/link/recipes/default.rb +5 -0
  185. data/test_project/site-cookbooks/b_test/link/spec/.keep +0 -0
  186. data/test_project/site-cookbooks/b_test/link/spec/default_spec.rb +5 -0
  187. data/test_project/site-cookbooks/b_test/link/templates/.keep +0 -0
  188. data/test_project/site-cookbooks/b_test/remote_file/attributes/.keep +0 -0
  189. data/test_project/site-cookbooks/b_test/remote_file/files/.keep +0 -0
  190. data/test_project/site-cookbooks/b_test/remote_file/files/remote_file.sh +1 -0
  191. data/test_project/site-cookbooks/b_test/remote_file/recipes/default.rb +9 -0
  192. data/test_project/site-cookbooks/b_test/remote_file/spec/.keep +0 -0
  193. data/test_project/site-cookbooks/b_test/remote_file/spec/default_spec.rb +7 -0
  194. data/test_project/site-cookbooks/b_test/remote_file/templates/.keep +0 -0
  195. data/test_project/site-cookbooks/b_test/template/attributes/.keep +0 -0
  196. data/test_project/site-cookbooks/b_test/template/files/.keep +0 -0
  197. data/test_project/site-cookbooks/b_test/template/recipes/default.rb +5 -0
  198. data/test_project/site-cookbooks/b_test/template/spec/.keep +0 -0
  199. data/test_project/site-cookbooks/b_test/template/spec/default_spec.rb +7 -0
  200. data/test_project/site-cookbooks/b_test/template/templates/.keep +0 -0
  201. data/test_project/site-cookbooks/b_test/template/templates/template.erb +2 -0
  202. data/test_project/site-cookbooks/c_test/execute/attributes/.keep +0 -0
  203. data/test_project/site-cookbooks/c_test/execute/files/.keep +0 -0
  204. data/test_project/site-cookbooks/c_test/execute/recipes/default.rb +6 -0
  205. data/test_project/site-cookbooks/c_test/execute/spec/.keep +0 -0
  206. data/test_project/site-cookbooks/c_test/execute/spec/default_spec.rb +6 -0
  207. data/test_project/site-cookbooks/c_test/execute/templates/.keep +0 -0
  208. data/test_project/site-cookbooks/c_test/remote_directory/attributes/.keep +0 -0
  209. data/test_project/site-cookbooks/c_test/remote_directory/files/.keep +0 -0
  210. data/test_project/site-cookbooks/c_test/remote_directory/recipes/default.rb +10 -0
  211. data/test_project/site-cookbooks/c_test/remote_directory/spec/.keep +0 -0
  212. data/test_project/site-cookbooks/c_test/remote_directory/spec/default_spec.rb +20 -0
  213. data/test_project/site-cookbooks/c_test/remote_directory/templates/.keep +0 -0
  214. data/test_project/site-cookbooks/c_test/remote_directory/templates/remote_dir/a.txt +0 -0
  215. data/test_project/site-cookbooks/c_test/remote_directory/templates/remote_dir/b.txt +0 -0
  216. data/test_project/site-cookbooks/c_test/remote_directory/templates/remote_dir/c.txt +0 -0
  217. data/test_project/site-cookbooks/d_spec/air/attributes/.keep +0 -0
  218. data/test_project/site-cookbooks/d_spec/air/files/.keep +0 -0
  219. data/test_project/site-cookbooks/d_spec/air/recipes/default.rb +0 -0
  220. data/test_project/site-cookbooks/d_spec/air/spec/.keep +0 -0
  221. data/test_project/site-cookbooks/d_spec/air/spec/default_spec.rb +0 -0
  222. data/test_project/site-cookbooks/d_spec/air/templates/.keep +0 -0
  223. data/test_project/spec/spec_helper.rb +33 -0
  224. data/test_project/tmp-nodes/.keep +0 -0
  225. data/test_project/tmp-nodes/test.json +12 -0
  226. metadata +337 -138
  227. data/bin/itamae-mitsurin +0 -5
  228. data/lib/itamae.rb +0 -18
  229. data/lib/itamae/ext.rb +0 -1
  230. data/lib/itamae/generators.rb +0 -20
  231. data/lib/itamae/generators/cookbook.rb +0 -22
  232. data/lib/itamae/generators/project.rb +0 -22
  233. data/lib/itamae/generators/role.rb +0 -22
  234. data/lib/itamae/generators/templates/project/Gemfile +0 -4
  235. data/lib/itamae/mitsurin.rb +0 -14
@@ -0,0 +1,15 @@
1
+ require 'itamae-mitsurin'
2
+
3
+ module ItamaeMitsurin
4
+ module Resource
5
+ class LocalRubyBlock < Base
6
+ define_attribute :action, default: :run
7
+ define_attribute :block, type: Proc
8
+
9
+ def action_run(options)
10
+ attributes.block.call
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,44 @@
1
+ require 'itamae-mitsurin'
2
+
3
+ module ItamaeMitsurin
4
+ module Resource
5
+ class Package < Base
6
+ define_attribute :action, default: :install
7
+ define_attribute :name, type: String, default_name: true
8
+ define_attribute :version, type: String
9
+ define_attribute :options, type: String
10
+
11
+ def pre_action
12
+ case @current_action
13
+ when :install
14
+ attributes.installed = true
15
+ when :remove
16
+ attributes.installed = false
17
+ end
18
+ end
19
+
20
+ def set_current_attributes
21
+ current.installed = run_specinfra(:check_package_is_installed, attributes.name)
22
+
23
+ if current.installed
24
+ current.version = run_specinfra(:get_package_version, attributes.name).stdout.strip
25
+ end
26
+ end
27
+
28
+ def action_install(action_options)
29
+ unless run_specinfra(:check_package_is_installed, attributes.name, attributes.version)
30
+ run_specinfra(:install_package, attributes.name, attributes.version, attributes.options)
31
+ updated!
32
+ end
33
+ end
34
+
35
+ def action_remove(action_options)
36
+ if run_specinfra(:check_package_is_installed, attributes.name, nil)
37
+ run_specinfra(:remove_package, attributes.name, attributes.options)
38
+ updated!
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,84 @@
1
+ require 'itamae-mitsurin'
2
+
3
+ module ItamaeMitsurin
4
+ module Resource
5
+ class RemoteDirectory < Base
6
+ define_attribute :action, default: :create
7
+ define_attribute :path, type: String, default_name: true
8
+ define_attribute :source, type: String, required: true
9
+ define_attribute :mode, type: String
10
+ define_attribute :owner, type: String
11
+ define_attribute :group, type: String
12
+
13
+ def pre_action
14
+ directory = ::File.expand_path(attributes.source, ::File.dirname(@recipe.path))
15
+ src = ::File.expand_path(directory, ::File.dirname(@recipe.path))
16
+
17
+ @temppath = ::File.join(runner.tmpdir, Time.now.to_f.to_s)
18
+ backend.send_directory(src, @temppath)
19
+
20
+ case @current_action
21
+ when :create
22
+ attributes.exist = true
23
+ end
24
+ end
25
+
26
+ def set_current_attributes
27
+ current.exist = run_specinfra(:check_file_is_directory, attributes.path)
28
+
29
+ if current.exist
30
+ current.mode = run_specinfra(:get_file_mode, attributes.path).stdout.chomp
31
+ current.owner = run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
32
+ current.group = run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
33
+ else
34
+ current.mode = nil
35
+ current.owner = nil
36
+ current.group = nil
37
+ end
38
+ end
39
+
40
+ def show_differences
41
+ super
42
+
43
+ if current.exist
44
+ diff = run_command(["diff", "-u", attributes.path, @temppath], error: false)
45
+ if diff.exit_status == 0
46
+ # no change
47
+ ItamaeMitsurin.logger.debug "directory content will not change"
48
+ else
49
+ ItamaeMitsurin.logger.info "diff:"
50
+ diff.stdout.each_line do |line|
51
+ ItamaeMitsurin.logger.info "#{line.strip}"
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def action_create(options)
58
+ if attributes.mode
59
+ run_specinfra(:change_file_mode, @temppath, attributes.mode)
60
+ end
61
+ if attributes.owner || attributes.group
62
+ run_specinfra(:change_file_owner, @temppath, attributes.owner, attributes.group)
63
+ end
64
+
65
+ if run_specinfra(:check_file_is_file, attributes.path)
66
+ unless check_command(["diff", "-q", @temppath, attributes.path])
67
+ updated!
68
+ end
69
+ else
70
+ updated!
71
+ end
72
+
73
+ run_specinfra(:remove_file, attributes.path)
74
+ run_specinfra(:move_file, @temppath, attributes.path)
75
+ end
76
+
77
+ def action_delete(options)
78
+ if run_specinfra(:check_file_is_directory, attributes.path)
79
+ run_specinfra(:remove_file, attributes.path)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,54 @@
1
+ require 'itamae-mitsurin'
2
+
3
+ module ItamaeMitsurin
4
+ module Resource
5
+ class RemoteFile < File
6
+ SourceNotFoundError = Class.new(StandardError)
7
+
8
+ define_attribute :source, type: [String, Symbol], default: :auto
9
+
10
+ private
11
+
12
+ def content_file
13
+ source_file
14
+ end
15
+
16
+ def source_file
17
+ @source_file ||= find_source_file
18
+ end
19
+
20
+ def find_source_file
21
+ if attributes.source == :auto
22
+ dirs = attributes.path.split(::File::SEPARATOR)
23
+ dirs.shift if dirs.first == ""
24
+
25
+ searched_paths = []
26
+ dirs.size.times do |i|
27
+ source_file_exts.each do |ext|
28
+ path = ::File.join(@recipe.dir, source_file_dir, "#{dirs[i..-1].join("/")}#{ext}")
29
+ if ::File.exist?(path)
30
+ ItamaeMitsurin.logger.debug "#{path} is used as a source file."
31
+ return path
32
+ else
33
+ searched_paths << path
34
+ end
35
+ end
36
+ end
37
+
38
+ raise SourceNotFoundError, "source file is not found (searched paths: #{searched_paths.join(', ')})"
39
+ else
40
+ ::File.expand_path(attributes.source, @recipe.dir)
41
+ end
42
+ end
43
+
44
+ def source_file_dir
45
+ "files"
46
+ end
47
+
48
+ def source_file_exts
49
+ [""]
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,69 @@
1
+ require 'itamae-mitsurin'
2
+
3
+ module ItamaeMitsurin
4
+ module Resource
5
+ class Service < Base
6
+ define_attribute :action, default: :nothing
7
+ define_attribute :name, type: String, default_name: true
8
+ define_attribute :provider, type: Symbol, default: nil
9
+
10
+ def initialize(*args)
11
+ super
12
+ @under = attributes.provider ? "_under_#{attributes.provider}" : ""
13
+ end
14
+
15
+ def pre_action
16
+ case @current_action
17
+ when :start, :restart
18
+ attributes.running = true
19
+ when :stop
20
+ attributes.running = false
21
+ when :enable
22
+ attributes.enabled = true
23
+ when :disable
24
+ attributes.enabled = false
25
+ end
26
+ end
27
+
28
+ def set_current_attributes
29
+ current.running = run_specinfra(:"check_service_is_running#{@under}", attributes.name)
30
+ current.enabled = run_specinfra(:"check_service_is_enabled#{@under}", attributes.name)
31
+ end
32
+
33
+ def action_start(options)
34
+ unless current.running
35
+ run_specinfra(:"start_service#{@under}", attributes.name)
36
+ end
37
+ end
38
+
39
+ def action_stop(options)
40
+ if current.running
41
+ run_specinfra(:"stop_service#{@under}", attributes.name)
42
+ end
43
+ end
44
+
45
+ def action_restart(options)
46
+ run_specinfra(:"restart_service#{@under}", attributes.name)
47
+ end
48
+
49
+ def action_reload(options)
50
+ if current.running
51
+ run_specinfra(:"reload_service#{@under}", attributes.name)
52
+ end
53
+ end
54
+
55
+ def action_enable(options)
56
+ unless current.enabled
57
+ run_specinfra(:"enable_service#{@under}", attributes.name)
58
+ end
59
+ end
60
+
61
+ def action_disable(options)
62
+ if current.enabled
63
+ run_specinfra(:"disable_service#{@under}", attributes.name)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
@@ -0,0 +1,53 @@
1
+ require 'itamae-mitsurin'
2
+ require 'erb'
3
+ require 'tempfile'
4
+
5
+ module ItamaeMitsurin
6
+ module Resource
7
+ class Template < RemoteFile
8
+ define_attribute :variables, type: Hash, default: {}
9
+
10
+ def pre_action
11
+ attributes.content = RenderContext.new(self).render_file(source_file)
12
+
13
+ super
14
+ end
15
+
16
+ private
17
+
18
+ def content_file
19
+ nil
20
+ end
21
+
22
+ def source_file_dir
23
+ "templates"
24
+ end
25
+
26
+ def source_file_exts
27
+ [".erb", ""]
28
+ end
29
+
30
+ class RenderContext
31
+ def initialize(resource)
32
+ @resource = resource
33
+
34
+ @resource.attributes.variables.each_pair do |key, value|
35
+ instance_variable_set("@#{key}".to_sym, value)
36
+ end
37
+ end
38
+
39
+ def render_file(src)
40
+ template = ::File.read(src)
41
+ ERB.new(template, nil, '-').tap do |erb|
42
+ erb.filename = src
43
+ end.result(binding)
44
+ end
45
+
46
+ def node
47
+ @resource.recipe.runner.node
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,93 @@
1
+ require 'itamae-mitsurin'
2
+
3
+ module ItamaeMitsurin
4
+ module Resource
5
+ class User < Base
6
+ define_attribute :action, default: :create
7
+ define_attribute :username, type: String, default_name: true
8
+ define_attribute :gid, type: [Integer, String]
9
+ define_attribute :home, type: String
10
+ define_attribute :password, type: String
11
+ define_attribute :system_user, type: [TrueClass, FalseClass]
12
+ define_attribute :uid, type: Integer
13
+ define_attribute :shell, type: String
14
+ define_attribute :create_home, type: [TrueClass, FalseClass], default: false
15
+
16
+ def pre_action
17
+ case @current_action
18
+ when :create
19
+ attributes.exist = true
20
+ end
21
+ end
22
+
23
+ def set_current_attributes
24
+ current.exist = exist?
25
+
26
+ if current.exist
27
+ current.uid = run_specinfra(:get_user_uid, attributes.username).stdout.strip.to_i
28
+ current.gid = run_specinfra(:get_user_gid, attributes.username).stdout.strip.to_i
29
+ current.home = run_specinfra(:get_user_home_directory, attributes.username).stdout.strip
30
+ current.shell = run_specinfra(:get_user_login_shell, attributes.username).stdout.strip
31
+ current.password = current_password
32
+ end
33
+ end
34
+
35
+ def action_create(options)
36
+ if run_specinfra(:check_user_exists, attributes.username)
37
+ if attributes.uid && attributes.uid != current.uid
38
+ run_specinfra(:update_user_uid, attributes.username, attributes.uid)
39
+ updated!
40
+ end
41
+
42
+ if attributes.gid && attributes.gid != current.gid
43
+ run_specinfra(:update_user_gid, attributes.username, attributes.gid)
44
+ updated!
45
+ end
46
+
47
+ if attributes.password && attributes.password != current.password
48
+ run_specinfra(:update_user_encrypted_password, attributes.username, attributes.password)
49
+ updated!
50
+ end
51
+
52
+ if attributes.home && attributes.home != current.home
53
+ run_specinfra(:update_user_home_directory, attributes.username, attributes.home)
54
+ updated!
55
+ end
56
+
57
+ if attributes.shell && attributes.shell != current.shell
58
+ run_specinfra(:update_user_login_shell, attributes.username, attributes.shell)
59
+ updated!
60
+ end
61
+ else
62
+ options = {
63
+ gid: attributes.gid,
64
+ home_directory: attributes.home,
65
+ password: attributes.password,
66
+ system_user: attributes.system_user,
67
+ uid: attributes.uid,
68
+ shell: attributes.shell,
69
+ create_home: attributes.create_home,
70
+ }
71
+
72
+ run_specinfra(:add_user, attributes.username, options)
73
+
74
+ updated!
75
+ end
76
+ end
77
+
78
+ private
79
+ def exist?
80
+ run_specinfra(:check_user_exists, attributes.username)
81
+ end
82
+
83
+ def current_password
84
+ result = run_specinfra(:get_user_encrypted_password, attributes.username)
85
+ if result.success?
86
+ result.stdout.strip
87
+ else
88
+ nil
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,297 @@
1
+ require 'specinfra/core'
2
+ require 'singleton'
3
+ require 'io/console'
4
+ require 'net/ssh'
5
+
6
+ Specinfra::Configuration.error_on_missing_backend_type = true
7
+
8
+ module Specinfra
9
+ module Configuration
10
+ def self.sudo_password
11
+ return ENV['SUDO_PASSWORD'] if ENV['SUDO_PASSWORD']
12
+ return @sudo_password if @sudo_password
13
+
14
+ # TODO: Fix this dirty hack
15
+ return nil unless caller.any? {|call| call.include?('channel_data') }
16
+
17
+ print "sudo password: "
18
+ @sudo_password = STDIN.noecho(&:gets).strip
19
+ print "\n"
20
+ @sudo_password
21
+ end
22
+ end
23
+ end
24
+
25
+ module TestItamaeMitsurin
26
+ module TestBackend
27
+ UnknownBackendTypeError = Class.new(StandardError)
28
+ CommandExecutionError = Class.new(StandardError)
29
+ SourceNotExistError = Class.new(StandardError)
30
+
31
+ class << self
32
+ def create(type, opts = {})
33
+ self.const_get(type.capitalize).new(opts)
34
+ end
35
+ end
36
+
37
+ class Base < Test::Unit::TestCase
38
+ attr_reader :executed_commands
39
+
40
+ def initialize(options)
41
+ @options = options
42
+ @backend = create_specinfra_backend
43
+ @executed_commands = []
44
+ end
45
+
46
+ def run_command(commands, options = {})
47
+ options = {error: true}.merge(options)
48
+
49
+ command = build_command(commands, options)
50
+ TestItamaeMitsurin.logger.debug "Executing `#{command}`..."
51
+
52
+ result = nil
53
+
54
+ TestItamaeMitsurin.logger.with_indent do
55
+ reset_output_handler
56
+
57
+ result = run_command_with_profiling(command)
58
+
59
+ flush_output_handler_buffer
60
+
61
+ if result.exit_status == 0 || !options[:error]
62
+ method = :debug
63
+ message = "exited with #{result.exit_status}"
64
+ else
65
+ method = :error
66
+ message = "Command `#{command}` failed. (exit status: #{result.exit_status})"
67
+
68
+ unless TestItamaeMitsurin.logger.level == ::Logger::DEBUG
69
+ result.stdout.each_line do |l|
70
+ log_output_line("stdout", l, :error)
71
+ end
72
+ result.stderr.each_line do |l|
73
+ log_output_line("stderr", l, :error)
74
+ end
75
+ end
76
+ end
77
+
78
+ TestItamaeMitsurin.logger.public_send(method, message)
79
+ end
80
+
81
+ if options[:error] && result.exit_status != 0
82
+ raise CommandExecutionError
83
+ end
84
+
85
+ result
86
+ end
87
+
88
+ def get_command(*args)
89
+ @backend.command.get(*args)
90
+ end
91
+
92
+ def receive_file(src, dst = nil)
93
+ if dst
94
+ TestItamaeMitsurin.logger.debug "Receiving a file from '#{src}' to '#{dst}'..."
95
+ else
96
+ TestItamaeMitsurin.logger.debug "Receiving a file from '#{src}'..."
97
+ end
98
+ @backend.receive_file(src, dst)
99
+ end
100
+
101
+ def send_file(src, dst)
102
+ TestItamaeMitsurin.logger.debug "Sending a file from '#{src}' to '#{dst}'..."
103
+ unless ::File.exist?(src)
104
+ raise SourceNotExistError, "The file '#{src}' doesn't exist."
105
+ end
106
+ unless ::File.file?(src)
107
+ raise SourceNotExistError, "'#{src}' is not a file."
108
+ end
109
+ @backend.send_file(src, dst)
110
+ end
111
+
112
+ def send_directory(src, dst)
113
+ TestItamaeMitsurin.logger.debug "Sending a directory from '#{src}' to '#{dst}'..."
114
+ unless ::File.exist?(src)
115
+ raise SourceNotExistError, "The directory '#{src}' doesn't exist."
116
+ end
117
+ unless ::File.directory?(src)
118
+ raise SourceNotExistError, "'#{src}' is not a directory."
119
+ end
120
+ @backend.send_directory(src, dst)
121
+ end
122
+
123
+ def host_inventory
124
+ @backend.host_inventory
125
+ end
126
+
127
+ def finalize
128
+ # pass
129
+ end
130
+
131
+ private
132
+
133
+ def create_specinfra_backend
134
+ raise NotImplementedError
135
+ end
136
+
137
+ def reset_output_handler
138
+ @buf = {}
139
+ %w!stdout stderr!.each do |output_name|
140
+ @buf[output_name] = ""
141
+ handler = lambda do |str|
142
+ lines = str.split(/\r?\n/, -1)
143
+ @buf[output_name] += lines.pop
144
+ unless lines.empty?
145
+ lines[0] = @buf[output_name] + lines[0]
146
+ @buf[output_name] = ""
147
+ lines.each do |l|
148
+ log_output_line(output_name, l)
149
+ end
150
+ end
151
+ end
152
+ @backend.public_send("#{output_name}_handler=", handler)
153
+ end
154
+ end
155
+
156
+ def flush_output_handler_buffer
157
+ @buf.each do |output_name, line|
158
+ next if line.empty?
159
+ log_output_line(output_name, line)
160
+ end
161
+ end
162
+
163
+ def log_output_line(output_name, line, severity = :debug)
164
+ line = line.gsub(/[[:cntrl:]]/, '')
165
+ TestItamaeMitsurin.logger.public_send(severity, "#{output_name} | #{line}")
166
+ end
167
+
168
+ def build_command(commands, options)
169
+ if commands.is_a?(Array)
170
+ command = commands.map do |cmd|
171
+ cmd.shellescape
172
+ end.join(' ')
173
+ else
174
+ command = commands
175
+ end
176
+
177
+ cwd = options[:cwd]
178
+ if cwd
179
+ command = "cd #{cwd.shellescape} && #{command}"
180
+ end
181
+
182
+ user = options[:user]
183
+ if user
184
+ command = "cd ~#{user.shellescape} ; #{command}"
185
+ command = "sudo -H -u #{user.shellescape} -- #{shell.shellescape} -c #{command.shellescape}"
186
+ end
187
+
188
+ command
189
+ end
190
+
191
+ def shell
192
+ @options[:shell] || '/bin/sh'
193
+ end
194
+
195
+ def run_command_with_profiling(command)
196
+ start_at = Time.now
197
+ result = @backend.run_command(command)
198
+ duration = Time.now.to_f - start_at.to_f
199
+
200
+ @executed_commands << {command: command, duration: duration}
201
+
202
+ result
203
+ end
204
+ end
205
+
206
+ class Local < Base
207
+ private
208
+ def create_specinfra_backend
209
+ Specinfra::Backend::Exec.new(
210
+ shell: @options[:shell],
211
+ )
212
+ end
213
+ end
214
+
215
+ class Ssh < Base
216
+ private
217
+ def create_specinfra_backend
218
+ Specinfra::Backend::Ssh.new(
219
+ request_pty: true,
220
+ host: ssh_options[:host_name],
221
+ disable_sudo: disable_sudo?,
222
+ ssh_options: ssh_options,
223
+ shell: @options[:shell],
224
+ )
225
+ end
226
+
227
+ def ssh_options
228
+ opts = {}
229
+
230
+ opts[:host_name] = @options[:host]
231
+
232
+ # from ssh-config
233
+ opts.merge!(Net::SSH::Config.for(@options[:host]))
234
+ opts[:user] = @options[:user] || opts[:user] || Etc.getlogin
235
+ opts[:keys] = [@options[:key]] if @options[:key]
236
+ opts[:port] = @options[:port] if @options[:port]
237
+
238
+ if @options[:vagrant]
239
+ config = Tempfile.new('', Dir.tmpdir)
240
+ hostname = opts[:host_name] || 'default'
241
+ vagrant_cmd = "vagrant ssh-config #{hostname} > #{config.path}"
242
+ if defined?(Bundler)
243
+ Bundler.with_clean_env do
244
+ `#{vagrant_cmd}`
245
+ end
246
+ else
247
+ `#{vagrant_cmd}`
248
+ end
249
+ opts.merge!(Net::SSH::Config.for(hostname, [config.path]))
250
+ end
251
+
252
+ if @options[:ask_password]
253
+ print "password: "
254
+ unless ENV['SSH_PASSWORD'].nil?
255
+ password = ENV['SSH_PASSWORD']
256
+ else
257
+ password = STDIN.noecho(&:gets).strip
258
+ end
259
+ print "\n"
260
+ opts.merge!(password: password)
261
+ end
262
+
263
+ opts
264
+ end
265
+
266
+ def disable_sudo?
267
+ !@options[:sudo]
268
+ end
269
+ end
270
+
271
+ class Docker < Base
272
+ def finalize
273
+ image = @backend.commit_container
274
+ TestItamaeMitsurin.logger.info "Image created: #{image.id}"
275
+ end
276
+
277
+ private
278
+ def create_specinfra_backend
279
+ begin
280
+ require 'docker'
281
+ rescue LoadError
282
+ TestItamaeMitsurin.logger.fatal "To use docker backend, please install 'docker-api' gem"
283
+ end
284
+
285
+ # TODO: Move to Specinfra?
286
+ Excon.defaults[:ssl_verify_peer] = @options[:tls_verify_peer]
287
+ ::Docker.logger = TestItamaeMitsurin.logger
288
+
289
+ Specinfra::Backend::Docker.new(
290
+ docker_image: @options[:image],
291
+ docker_container: @options[:container],
292
+ shell: @options[:shell],
293
+ )
294
+ end
295
+ end
296
+ end
297
+ end