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,63 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class Directory < Base
6
+ define_attribute :action, default: :create
7
+ define_attribute :path, type: String, default_name: true
8
+ define_attribute :mode, type: String
9
+ define_attribute :owner, type: String
10
+ define_attribute :group, type: String
11
+
12
+ def pre_action
13
+ case @current_action
14
+ when :create
15
+ attributes.exist = true
16
+ when :delete
17
+ attributes.exist = false
18
+ end
19
+ end
20
+
21
+ def show_differences
22
+ current.mode = current.mode.rjust(4, '0') if current.mode
23
+ attributes.mode = attributes.mode.rjust(4, '0') if attributes.mode
24
+
25
+ super
26
+ end
27
+
28
+ def set_current_attributes
29
+ exist = run_specinfra(:check_file_is_directory, attributes.path)
30
+ current.exist = exist
31
+
32
+ if exist
33
+ current.mode = run_specinfra(:get_file_mode, attributes.path).stdout.chomp
34
+ current.owner = run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
35
+ current.group = run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
36
+ else
37
+ current.mode = nil
38
+ current.owner = nil
39
+ current.group = nil
40
+ end
41
+ end
42
+
43
+ def action_create(options)
44
+ if !run_specinfra(:check_file_is_directory, attributes.path)
45
+ run_specinfra(:create_file_as_directory, attributes.path)
46
+ end
47
+ if attributes.mode
48
+ run_specinfra(:change_file_mode, attributes.path, attributes.mode)
49
+ end
50
+ if attributes.owner || attributes.group
51
+ run_specinfra(:change_file_owner, attributes.path, attributes.owner, attributes.group)
52
+ end
53
+ end
54
+
55
+ def action_delete(options)
56
+ if run_specinfra(:check_file_is_directory, attributes.path)
57
+ run_specinfra(:remove_file, attributes.path)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
@@ -0,0 +1,26 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class Execute < Base
6
+ define_attribute :action, default: :run
7
+ define_attribute :command, type: String, default_name: true
8
+
9
+ def pre_action
10
+ case @current_action
11
+ when :run
12
+ attributes.executed = true
13
+ end
14
+ end
15
+
16
+ def set_current_attributes
17
+ current.executed = false
18
+ end
19
+
20
+ def action_run(options)
21
+ run_command(attributes.command)
22
+ updated!
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,176 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class File < Base
6
+ define_attribute :action, default: :create
7
+ define_attribute :path, type: String, default_name: true
8
+ define_attribute :content, type: String, default: nil
9
+ define_attribute :mode, type: String
10
+ define_attribute :owner, type: String
11
+ define_attribute :group, type: String
12
+ define_attribute :block, type: Proc, default: proc {}
13
+
14
+ def pre_action
15
+ case @current_action
16
+ when :create
17
+ attributes.exist = true
18
+ when :delete
19
+ attributes.exist = false
20
+ when :edit
21
+ attributes.exist = true
22
+
23
+ unless runner.dry_run?
24
+ content = backend.receive_file(attributes.path)
25
+ attributes.block.call(content)
26
+ attributes.content = content
27
+ end
28
+ end
29
+
30
+ send_tempfile
31
+ end
32
+
33
+ def set_current_attributes
34
+ current.exist = run_specinfra(:check_file_is_file, attributes.path)
35
+
36
+ if current.exist
37
+ current.mode = run_specinfra(:get_file_mode, attributes.path).stdout.chomp
38
+ current.owner = run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
39
+ current.group = run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
40
+ else
41
+ current.mode = nil
42
+ current.owner = nil
43
+ current.group = nil
44
+ end
45
+ end
46
+
47
+ def show_differences
48
+ current.mode = current.mode.rjust(4, '0') if current.mode
49
+ attributes.mode = attributes.mode.rjust(4, '0') if attributes.mode
50
+
51
+ super
52
+
53
+ if @temppath
54
+ compare_file
55
+ end
56
+ end
57
+
58
+ def action_create(options)
59
+ if !current.exist && !@temppath
60
+ run_command(["touch", attributes.path])
61
+ end
62
+
63
+ change_target = @temppath || attributes.path
64
+
65
+ if attributes.mode
66
+ run_specinfra(:change_file_mode, change_target, attributes.mode)
67
+ end
68
+ if attributes.owner || attributes.group
69
+ run_specinfra(:change_file_owner, change_target, attributes.owner, attributes.group)
70
+ end
71
+
72
+ if @temppath
73
+ if run_specinfra(:check_file_is_file, attributes.path)
74
+ unless check_command(["diff", "-q", @temppath, attributes.path])
75
+ # the file is modified
76
+ updated!
77
+ end
78
+ else
79
+ # new file
80
+ updated!
81
+ end
82
+
83
+ if updated?
84
+ run_specinfra(:move_file, @temppath, attributes.path)
85
+ end
86
+ end
87
+ end
88
+
89
+ def action_delete(options)
90
+ if run_specinfra(:check_file_is_file, attributes.path)
91
+ run_specinfra(:remove_file, attributes.path)
92
+ end
93
+ end
94
+
95
+ def action_edit(options)
96
+ if attributes.mode
97
+ run_specinfra(:change_file_mode, @temppath, attributes.mode)
98
+ else
99
+ run_command(['chmod', '--reference', attributes.path, @temppath])
100
+ end
101
+
102
+ if attributes.owner || attributes.group
103
+ run_specinfra(:change_file_owner, @temppath, attributes.owner, attributes.group)
104
+ else
105
+ run_command(['chown', '--reference', attributes.path, @temppath])
106
+ end
107
+
108
+ unless check_command(["diff", "-q", @temppath, attributes.path])
109
+ # the file is modified
110
+ updated!
111
+ end
112
+
113
+ run_specinfra(:move_file, @temppath, attributes.path)
114
+ end
115
+
116
+ private
117
+
118
+ def compare_file
119
+ compare_to = if current.exist
120
+ attributes.path
121
+ else
122
+ '/dev/null'
123
+ end
124
+
125
+ diff = run_command(["diff", "-u", compare_to, @temppath], error: false)
126
+ if diff.exit_status == 0
127
+ # no change
128
+ Itamae.logger.debug "file content will not change"
129
+ else
130
+ Itamae.logger.info "diff:"
131
+ diff.stdout.each_line do |line|
132
+ color = if line.start_with?('+')
133
+ :green
134
+ elsif line.start_with?('-')
135
+ :red
136
+ else
137
+ :clear
138
+ end
139
+ Itamae.logger.color(color) do
140
+ Itamae.logger.info line.chomp
141
+ end
142
+ end
143
+ runner.handler.event(:file_content_changed, diff: diff.stdout)
144
+ end
145
+ end
146
+
147
+ # will be overridden
148
+ def content_file
149
+ nil
150
+ end
151
+
152
+ def send_tempfile
153
+ if !attributes.content && !content_file
154
+ @temppath = nil
155
+ return
156
+ end
157
+
158
+ begin
159
+ src = if content_file
160
+ content_file
161
+ else
162
+ f = Tempfile.open('itamae')
163
+ f.write(attributes.content)
164
+ f.close
165
+ f.path
166
+ end
167
+
168
+ @temppath = ::File.join(runner.tmpdir, Time.now.to_f.to_s)
169
+ backend.send_file(src, @temppath)
170
+ ensure
171
+ f.unlink if f
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,81 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class GemPackage < Base
6
+ define_attribute :action, default: :install
7
+ define_attribute :package_name, type: String, default_name: true
8
+ define_attribute :gem_binary, type: [String, Array], default: 'gem'
9
+ define_attribute :options, type: [String, Array], default: []
10
+ define_attribute :version, type: String
11
+ define_attribute :source, type: String
12
+
13
+ def pre_action
14
+ case @current_action
15
+ when :install
16
+ attributes.installed = true
17
+ end
18
+ end
19
+
20
+ def set_current_attributes
21
+ installed = installed_gems.find {|g| g[:name] == attributes.package_name }
22
+ current.installed = !!installed
23
+
24
+ if current.installed
25
+ versions = installed[:versions]
26
+ if versions.include?(attributes.version)
27
+ current.version = attributes.version
28
+ else
29
+ current.version = versions.first
30
+ end
31
+ end
32
+ end
33
+
34
+ def action_install(action_options)
35
+ if current.installed
36
+ if attributes.version && current.version != attributes.version
37
+ install!
38
+ updated!
39
+ end
40
+ else
41
+ install!
42
+ updated!
43
+ end
44
+ end
45
+
46
+ def action_upgrade(action_options)
47
+ return if current.installed && attributes.version && current.version == attributes.version
48
+
49
+ install!
50
+ updated!
51
+ end
52
+
53
+ def installed_gems
54
+ gems = []
55
+ run_command([*Array(attributes.gem_binary), 'list', '-l']).stdout.each_line do |line|
56
+ if /\A([^ ]+) \(([^\)]+)\)\z/ =~ line.strip
57
+ name = $1
58
+ versions = $2.split(', ')
59
+ gems << {name: name, versions: versions}
60
+ end
61
+ end
62
+ gems
63
+ rescue Backend::CommandExecutionError
64
+ []
65
+ end
66
+
67
+ def install!
68
+ cmd = [*Array(attributes.gem_binary), 'install', *Array(attributes.options)]
69
+ if attributes.version
70
+ cmd << '-v' << attributes.version
71
+ end
72
+ if attributes.source
73
+ cmd << '--source' << attributes.source
74
+ end
75
+ cmd << attributes.package_name
76
+
77
+ run_command(cmd)
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,94 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class Git < Base
6
+ DEPLOY_BRANCH = "deploy"
7
+
8
+ define_attribute :action, default: :sync
9
+ define_attribute :destination, type: String, default_name: true
10
+ define_attribute :repository, type: String, required: true
11
+ define_attribute :revision, type: String
12
+ define_attribute :recursive, type: [TrueClass, FalseClass], default: false
13
+
14
+ def pre_action
15
+ case @current_action
16
+ when :sync
17
+ attributes.exist = true
18
+ end
19
+ end
20
+
21
+ def set_current_attributes
22
+ current.exist = run_specinfra(:check_file_is_directory, attributes.destination)
23
+ end
24
+
25
+ def action_sync(options)
26
+ ensure_git_available
27
+
28
+ new_repository = false
29
+
30
+ if check_empty_dir
31
+ cmd = ['git', 'clone']
32
+ cmd << '--recursive' if attributes.recursive
33
+ cmd << attributes.repository << attributes.destination
34
+ run_command(cmd)
35
+ new_repository = true
36
+ end
37
+
38
+ target = if attributes.revision
39
+ get_revision(attributes.revision)
40
+ else
41
+ fetch_origin!
42
+ run_command_in_repo("git ls-remote origin HEAD | cut -f1").stdout.strip
43
+ end
44
+
45
+ if new_repository || target != get_revision('HEAD')
46
+ updated!
47
+
48
+ deploy_old_created = false
49
+ if current_branch == DEPLOY_BRANCH
50
+ run_command_in_repo("git branch -m deploy-old")
51
+ deploy_old_created = true
52
+ end
53
+
54
+ fetch_origin!
55
+ run_command_in_repo(["git", "checkout", target, "-b", DEPLOY_BRANCH])
56
+
57
+ if deploy_old_created
58
+ run_command_in_repo("git branch -d deploy-old")
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+ def ensure_git_available
65
+ unless run_command("which git", error: false).exit_status == 0
66
+ raise "`git` command is not available. Please install git."
67
+ end
68
+ end
69
+
70
+ def check_empty_dir
71
+ run_command("test -z \"$(ls -A #{shell_escape(attributes.destination)})\"", error: false).success?
72
+ end
73
+
74
+ def run_command_in_repo(*args)
75
+ run_command(*args, cwd: attributes.destination)
76
+ end
77
+
78
+ def current_branch
79
+ run_command_in_repo("git rev-parse --abbrev-ref HEAD").stdout.strip
80
+ end
81
+
82
+ def get_revision(branch)
83
+ run_command_in_repo("git rev-list #{shell_escape(branch)} | head -n1").stdout.strip
84
+ end
85
+
86
+ def fetch_origin!
87
+ return if @origin_fetched
88
+ @origin_fetched = true
89
+ run_command_in_repo(['git', 'fetch', 'origin'])
90
+ end
91
+ end
92
+ end
93
+ end
94
+
@@ -0,0 +1,42 @@
1
+ require 'itamae'
2
+
3
+ module Itamae
4
+ module Resource
5
+ class Group < Base
6
+ define_attribute :action, default: :create
7
+ define_attribute :groupname, type: String, default_name: true
8
+ define_attribute :gid, type: Integer
9
+
10
+ def set_current_attributes
11
+ current.exist = exist?
12
+
13
+ if current.exist
14
+ current.gid = run_specinfra(:get_group_gid, attributes.groupname).stdout.strip.to_i
15
+ end
16
+ end
17
+
18
+ def action_create(options)
19
+ if run_specinfra(:check_group_exists, attributes.groupname)
20
+ if attributes.gid && attributes.gid != current.gid
21
+ run_specinfra(:update_group_gid, attributes.groupname, attributes.gid)
22
+ updated!
23
+ end
24
+ else
25
+ options = {
26
+ gid: attributes.gid,
27
+ }
28
+
29
+ run_specinfra(:add_group, attributes.groupname, options)
30
+
31
+ updated!
32
+ end
33
+ end
34
+
35
+ private
36
+ def exist?
37
+ run_specinfra(:check_group_exists, attributes.groupname)
38
+ end
39
+ end
40
+ end
41
+ end
42
+