itamae-mitsurin 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +7 -0
  5. data/LICENSE.txt +26 -0
  6. data/README.md +44 -0
  7. data/Rakefile +55 -0
  8. data/bin/itamae +5 -0
  9. data/bin/itamae-mitsurin +5 -0
  10. data/itamae-mitsurin.gemspec +35 -0
  11. data/lib/itamae.rb +18 -0
  12. data/lib/itamae/backend.rb +293 -0
  13. data/lib/itamae/cli.rb +86 -0
  14. data/lib/itamae/definition.rb +40 -0
  15. data/lib/itamae/ext.rb +1 -0
  16. data/lib/itamae/ext/specinfra.rb +39 -0
  17. data/lib/itamae/generators.rb +20 -0
  18. data/lib/itamae/generators/cookbook.rb +22 -0
  19. data/lib/itamae/generators/project.rb +22 -0
  20. data/lib/itamae/generators/role.rb +22 -0
  21. data/lib/itamae/generators/templates/cookbook/default.rb +0 -0
  22. data/lib/itamae/generators/templates/cookbook/files/.keep +0 -0
  23. data/lib/itamae/generators/templates/cookbook/templates/.keep +0 -0
  24. data/lib/itamae/generators/templates/project/Gemfile +4 -0
  25. data/lib/itamae/generators/templates/project/cookbooks/.keep +0 -0
  26. data/lib/itamae/generators/templates/project/roles/.keep +0 -0
  27. data/lib/itamae/generators/templates/role/default.rb +0 -0
  28. data/lib/itamae/generators/templates/role/files/.keep +0 -0
  29. data/lib/itamae/generators/templates/role/templates/.keep +0 -0
  30. data/lib/itamae/handler.rb +21 -0
  31. data/lib/itamae/handler/base.rb +40 -0
  32. data/lib/itamae/handler/debug.rb +10 -0
  33. data/lib/itamae/handler/fluentd.rb +44 -0
  34. data/lib/itamae/handler/json.rb +22 -0
  35. data/lib/itamae/handler_proxy.rb +38 -0
  36. data/lib/itamae/logger.rb +124 -0
  37. data/lib/itamae/mitsurin.rb +13 -0
  38. data/lib/itamae/mitsurin/cli.rb +56 -0
  39. data/lib/itamae/mitsurin/creators.rb +19 -0
  40. data/lib/itamae/mitsurin/creators/cookbook.rb +24 -0
  41. data/lib/itamae/mitsurin/creators/project.rb +24 -0
  42. data/lib/itamae/mitsurin/creators/templates/project/.rspec +2 -0
  43. data/lib/itamae/mitsurin/creators/templates/project/Gemfile +3 -0
  44. data/lib/itamae/mitsurin/creators/templates/project/Rakefile +2 -0
  45. data/lib/itamae/mitsurin/creators/templates/project/environments/.keep +0 -0
  46. data/lib/itamae/mitsurin/creators/templates/project/environments/sample.json +7 -0
  47. data/lib/itamae/mitsurin/creators/templates/project/nodes/.keep +0 -0
  48. data/lib/itamae/mitsurin/creators/templates/project/nodes/sample01.json +8 -0
  49. data/lib/itamae/mitsurin/creators/templates/project/roles/.keep +0 -0
  50. data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/attributes/.keep +0 -0
  51. data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/files/.keep +0 -0
  52. data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/recipes/default.rb +0 -0
  53. data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/spec/default_spec.rb +2 -0
  54. data/lib/itamae/mitsurin/creators/templates/project/site-cookbooks/_base/_base/templates/.keep +0 -0
  55. data/lib/itamae/mitsurin/creators/templates/project/spec/spec_helper.rb +32 -0
  56. data/lib/itamae/mitsurin/creators/templates/project/tmp-nodes/.keep +0 -0
  57. data/lib/itamae/mitsurin/creators/templates/site-cookbooks/attributes/.keep +0 -0
  58. data/lib/itamae/mitsurin/creators/templates/site-cookbooks/files/.keep +0 -0
  59. data/lib/itamae/mitsurin/creators/templates/site-cookbooks/recipes/default.rb +0 -0
  60. data/lib/itamae/mitsurin/creators/templates/site-cookbooks/spec/.keep +0 -0
  61. data/lib/itamae/mitsurin/creators/templates/site-cookbooks/templates/.keep +0 -0
  62. data/lib/itamae/mitsurin/itamae_task.rb +199 -0
  63. data/lib/itamae/mitsurin/serverspec_task.rb +110 -0
  64. data/lib/itamae/mitsurin/version.rb +5 -0
  65. data/lib/itamae/mitsurin/version.txt +1 -0
  66. data/lib/itamae/node.rb +74 -0
  67. data/lib/itamae/notification.rb +46 -0
  68. data/lib/itamae/recipe.rb +171 -0
  69. data/lib/itamae/recipe_children.rb +86 -0
  70. data/lib/itamae/resource.rb +73 -0
  71. data/lib/itamae/resource/aws_ebs_volume.rb +84 -0
  72. data/lib/itamae/resource/base.rb +374 -0
  73. data/lib/itamae/resource/directory.rb +63 -0
  74. data/lib/itamae/resource/execute.rb +26 -0
  75. data/lib/itamae/resource/file.rb +176 -0
  76. data/lib/itamae/resource/gem_package.rb +81 -0
  77. data/lib/itamae/resource/git.rb +94 -0
  78. data/lib/itamae/resource/group.rb +42 -0
  79. data/lib/itamae/resource/http_request.rb +71 -0
  80. data/lib/itamae/resource/link.rb +33 -0
  81. data/lib/itamae/resource/local_ruby_block.rb +15 -0
  82. data/lib/itamae/resource/package.rb +44 -0
  83. data/lib/itamae/resource/remote_directory.rb +84 -0
  84. data/lib/itamae/resource/remote_file.rb +54 -0
  85. data/lib/itamae/resource/service.rb +69 -0
  86. data/lib/itamae/resource/template.rb +53 -0
  87. data/lib/itamae/resource/user.rb +93 -0
  88. data/lib/itamae/runner.rb +124 -0
  89. data/spec/integration/Vagrantfile +35 -0
  90. data/spec/integration/default_spec.rb +226 -0
  91. data/spec/integration/recipes/default.rb +423 -0
  92. data/spec/integration/recipes/default2.rb +6 -0
  93. data/spec/integration/recipes/define/default.rb +6 -0
  94. data/spec/integration/recipes/define/files/remote_file_in_definition +1 -0
  95. data/spec/integration/recipes/dry_run.rb +6 -0
  96. data/spec/integration/recipes/files/remote_file_auto +1 -0
  97. data/spec/integration/recipes/hello.erb +6 -0
  98. data/spec/integration/recipes/hello.txt +1 -0
  99. data/spec/integration/recipes/included.rb +9 -0
  100. data/spec/integration/recipes/node.json +3 -0
  101. data/spec/integration/recipes/redefine.rb +20 -0
  102. data/spec/integration/recipes/templates/template_auto.erb +6 -0
  103. data/spec/integration/spec_helper.rb +42 -0
  104. data/spec/unit/lib/itamae/backend_spec.rb +95 -0
  105. data/spec/unit/lib/itamae/handler/base_spec.rb +34 -0
  106. data/spec/unit/lib/itamae/handler/fluentd_spec.rb +19 -0
  107. data/spec/unit/lib/itamae/handler_proxy_spec.rb +38 -0
  108. data/spec/unit/lib/itamae/handler_spec.rb +11 -0
  109. data/spec/unit/lib/itamae/node_spec.rb +14 -0
  110. data/spec/unit/lib/itamae/recipe_spec.rb +6 -0
  111. data/spec/unit/lib/itamae/resource/base_spec.rb +127 -0
  112. data/spec/unit/lib/itamae/resource_spec.rb +23 -0
  113. data/spec/unit/lib/itamae/runner_spec.rb +32 -0
  114. data/spec/unit/spec_helper.rb +23 -0
  115. metadata +393 -0
@@ -0,0 +1,71 @@
1
+ require 'itamae'
2
+ require 'uri'
3
+ require 'net/https'
4
+
5
+ module Itamae
6
+ module Resource
7
+ class HttpRequest < File
8
+ RedirectLimitExceeded = Class.new(StandardError)
9
+
10
+ define_attribute :action, default: :get
11
+ define_attribute :headers, type: Hash, default: {}
12
+ define_attribute :message, type: String, default: ""
13
+ define_attribute :redirect_limit, type: Integer, default: 10
14
+ define_attribute :url, type: String, required: true
15
+
16
+ def pre_action
17
+ uri = URI.parse(attributes.url)
18
+ response = nil
19
+ redirects_followed = 0
20
+
21
+ loop do
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.use_ssl = true if uri.scheme == "https"
24
+
25
+ case attributes.action
26
+ when :delete, :get, :options
27
+ response = http.method(attributes.action).call(uri.request_uri, attributes.headers)
28
+ when :post, :put
29
+ response = http.method(attributes.action).call(uri.request_uri, attributes.message, attributes.headers)
30
+ end
31
+
32
+ if response.kind_of?(Net::HTTPRedirection)
33
+ if redirects_followed < attributes.redirect_limit
34
+ uri = URI.parse(response["location"])
35
+ redirects_followed += 1
36
+ Itamae.logger.debug "Following redirect #{redirects_followed}/#{attributes.redirect_limit}"
37
+ else
38
+ raise RedirectLimitExceeded
39
+ end
40
+ else
41
+ break
42
+ end
43
+ end
44
+
45
+ attributes.content = response.body
46
+
47
+ super
48
+ end
49
+
50
+ def action_delete(options)
51
+ action_create(options)
52
+ end
53
+
54
+ def action_get(options)
55
+ action_create(options)
56
+ end
57
+
58
+ def action_options(options)
59
+ action_create(options)
60
+ end
61
+
62
+ def action_post(options)
63
+ action_create(options)
64
+ end
65
+
66
+ def action_put(options)
67
+ action_create(options)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class Link < Base
6
+ define_attribute :action, default: :create
7
+ define_attribute :link, type: String, default_name: true
8
+ define_attribute :to, type: String, required: true
9
+ define_attribute :force, type: [TrueClass, FalseClass], default: false
10
+
11
+ def pre_action
12
+ case @current_action
13
+ when :create
14
+ attributes.exist = true
15
+ end
16
+ end
17
+
18
+ def set_current_attributes
19
+ current.exist = run_specinfra(:check_file_is_link, attributes.link)
20
+
21
+ if current.exist
22
+ current.to = run_specinfra(:get_file_link_target, attributes.link).stdout.strip
23
+ end
24
+ end
25
+
26
+ def action_create(options)
27
+ unless run_specinfra(:check_file_is_linked_to, attributes.link, attributes.to)
28
+ run_specinfra(:link_file_to, attributes.link, attributes.to, force: attributes.force)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
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'
2
+
3
+ module Itamae
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'
2
+
3
+ module Itamae
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
+ Itamae.logger.debug "directory content will not change"
48
+ else
49
+ Itamae.logger.info "diff:"
50
+ diff.stdout.each_line do |line|
51
+ Itamae.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'
2
+
3
+ module Itamae
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
+ Itamae.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'
2
+
3
+ module Itamae
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'
2
+ require 'erb'
3
+ require 'tempfile'
4
+
5
+ module Itamae
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'
2
+
3
+ module Itamae
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