serverkit 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c63fd1655ae7424f508d32cdaefa5c0e83090117
4
- data.tar.gz: b4c5a55c88bb2e2031dd3321f26443c32bdf79a6
3
+ metadata.gz: 6bc4285bf5a80516d782219e3e1c39e2293bd149
4
+ data.tar.gz: e5aa09260d3f3312d59387e4b6d5be6931194a08
5
5
  SHA512:
6
- metadata.gz: 66c44fa0c113ccbde06013a4eab9d63ec42175572e8c6dd3848e764531d404d665f8668a5367df74d8186a80522e7dac8a13e3072a36cb0d633947ac8e55f6c1
7
- data.tar.gz: 28ae2520dc1e0805e070cff7cc4ebb7d5f4e5725d5ec27f7e4948314a38ead1442a16f7079ff0f4758296c7a5d14a129e5203815c3e963c53c0f5ef2c2ee8fc4
6
+ metadata.gz: 4a8adc794c2315262a5bf14713fef1d36d069fe0c7776b64402de888a66bc364b22ee601205f0c13ff5144dd5561cf900959055faba6a8b5d5fb4a7afe658ef9
7
+ data.tar.gz: ed16eac1e8ba241522920ec7fd5137063588e1e78fd3993e1ffa9c802f2e3111f7663a3a0d3fb3be14140cbc1bcca406dd337f7fcd0673dfd47342095b1f35c2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.4.4
2
+ - Add template resource
3
+ - Prevent abstract class from being used as resource type
4
+
1
5
  ## 0.4.3
2
6
  - Add directory resource
3
7
  - Add file resource
data/README.md CHANGED
@@ -194,17 +194,18 @@ By default, all types of resource can or must have the following attributes:
194
194
  ### Type
195
195
  A resource must have a valid type attribute like:
196
196
 
197
- - command
198
- - directory
199
- - file
200
- - git
201
- - nothing
202
- - package
203
- - recipe
204
- - remote_file
205
- - service
206
- - symlink
207
- - user
197
+ - [command](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/command.rb)
198
+ - [directory](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/directory.rb)
199
+ - [file](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/file.rb)
200
+ - [git](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/git.rb)
201
+ - [nothing](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/nothing.rb)
202
+ - [package](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/package.rb)
203
+ - [recipe](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/recipe.rb)
204
+ - [remote_file](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/remote_file.rb)
205
+ - [service](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/service.rb)
206
+ - [symlink](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/symlink.rb)
207
+ - [template](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/template.rb)
208
+ - [user](https://github.com/r7kamura/serverkit/blob/master/lib/serverkit/resources/user.rb)
208
209
 
209
210
  ### Example
210
211
  An example package resource that has type and name attributes.
data/example/recipe.yml CHANGED
@@ -13,3 +13,6 @@ resources:
13
13
  - type: directory
14
14
  path: /Users/test
15
15
  owner: test
16
+ - type: template
17
+ source: example/template.erb
18
+ destination: /tmp/template_test
@@ -0,0 +1 @@
1
+ <%= message %>
@@ -0,0 +1 @@
1
+ message: test
@@ -86,8 +86,6 @@ module Serverkit
86
86
  case
87
87
  when has_executable_path?
88
88
  load_data_from_executable
89
- when has_erb_path?
90
- load_data_from_erb
91
89
  when has_yaml_path?
92
90
  load_data_from_yaml
93
91
  else
@@ -10,6 +10,7 @@ require "serverkit/resources/recipe"
10
10
  require "serverkit/resources/remote_file"
11
11
  require "serverkit/resources/service"
12
12
  require "serverkit/resources/symlink"
13
+ require "serverkit/resources/template"
13
14
  require "serverkit/resources/unknown"
14
15
  require "serverkit/resources/user"
15
16
 
@@ -29,8 +30,16 @@ module Serverkit
29
30
 
30
31
  private
31
32
 
33
+ # @return [Array<Class>]
34
+ def available_resource_classes
35
+ Resources.constants.select do |constant_name|
36
+ constant = Resources.const_get(constant_name)
37
+ constant < Resources::Base && !constant.abstract_class?
38
+ end
39
+ end
40
+
32
41
  def has_known_type?
33
- Resources.constants.map(&:to_s).include?(resource_class_name)
42
+ available_resource_classes.map(&:to_s).include?(resource_class_name)
34
43
  end
35
44
 
36
45
  # @return [Class]
@@ -9,6 +9,12 @@ module Serverkit
9
9
  module Resources
10
10
  class Base
11
11
  class << self
12
+ attr_writer :abstract_class
13
+
14
+ def abstract_class?
15
+ !!@abstract_class
16
+ end
17
+
12
18
  # @note DSL method to define attribute with its validations
13
19
  def attribute(name, options = {})
14
20
  default = options.delete(:default)
@@ -41,6 +47,8 @@ module Serverkit
41
47
  to: :backend,
42
48
  )
43
49
 
50
+ self.abstract_class = true
51
+
44
52
  # @param [Serverkit::Recipe] recipe
45
53
  # @param [Hash] attributes
46
54
  def initialize(recipe, attributes)
@@ -1,68 +1,25 @@
1
- require "serverkit/resources/base"
1
+ require "serverkit/resources/entry"
2
2
 
3
3
  module Serverkit
4
4
  module Resources
5
- class Directory < Base
6
- attribute :group, type: String
7
- attribute :mode, type: [Integer, String]
8
- attribute :owner, type: String
5
+ class Directory < Entry
9
6
  attribute :path, required: true, type: String
10
7
 
11
- # @note Override
12
- def apply
13
- create_directory unless has_correct_directory?
14
- update_group unless has_correct_group?
15
- update_mode unless has_correct_mode?
16
- update_owner unless has_correct_owner?
17
- end
18
-
19
- # @note Override
20
- def check
21
- has_correct_directory? && has_correct_group? && has_correct_mode? && has_correct_owner?
22
- end
23
-
24
8
  private
25
9
 
26
- def create_directory
27
- run_command_from_identifier(:create_file_as_directory, path)
28
- end
29
-
30
- def has_correct_directory?
31
- check_command_from_identifier(:check_file_is_directory, path)
32
- end
33
-
34
- def has_correct_group?
35
- group.nil? || check_command_from_identifier(:check_file_is_grouped, path, group)
36
- end
37
-
38
- def has_correct_mode?
39
- mode.nil? || check_command_from_identifier(:check_file_has_mode, path, mode_in_octal_notation)
40
- end
41
-
42
- def has_correct_owner?
43
- owner.nil? || check_command_from_identifier(:check_file_is_owned_by, path, owner)
44
- end
45
-
46
- # @return [String]
47
- # @example "755" # for 0755
48
- def mode_in_octal_notation
49
- if mode.is_a?(Integer)
50
- mode.to_s(8)
51
- else
52
- mode
53
- end
54
- end
55
-
56
- def update_group
57
- run_command_from_identifier(:change_file_group, path, group)
10
+ # @note Override
11
+ def destination
12
+ path
58
13
  end
59
14
 
60
- def update_mode
61
- run_command_from_identifier(:change_file_mode, path, mode_in_octal_notation)
15
+ # @note Override
16
+ def has_correct_entry?
17
+ check_command_from_identifier(:check_file_is_directory, destination)
62
18
  end
63
19
 
64
- def update_owner
65
- run_command_from_identifier(:change_file_owner, path, owner)
20
+ # @note Override
21
+ def update_entry
22
+ run_command_from_identifier(:create_file_as_directory, destination)
66
23
  end
67
24
  end
68
25
  end
@@ -0,0 +1,107 @@
1
+ require "digest"
2
+ require "serverkit/resources/base"
3
+ require "tempfile"
4
+
5
+ module Serverkit
6
+ module Resources
7
+ # Abstract class for file and directory
8
+ class Entry < Base
9
+ attribute :group, type: String
10
+ attribute :mode, type: [Integer, String]
11
+ attribute :owner, type: String
12
+
13
+ self.abstract_class = true
14
+
15
+ # @note Override
16
+ def apply
17
+ update_entry unless has_correct_entry?
18
+ update_group unless has_correct_group?
19
+ update_mode unless has_correct_mode?
20
+ update_owner unless has_correct_owner?
21
+ end
22
+
23
+ # @note Override
24
+ def check
25
+ has_correct_entry? && has_correct_group? && has_correct_mode? && has_correct_owner?
26
+ end
27
+
28
+ private
29
+
30
+ # @note Override me
31
+ # @return [String] Path to the entry on remote side
32
+ def destination
33
+ raise NotImplementedError
34
+ end
35
+
36
+ def has_correct_content?
37
+ content.nil? || remote_file_sha256sum == local_content_sha256sum
38
+ end
39
+
40
+ # @note Override me
41
+ def has_correct_entry?
42
+ raise NotImplementedError
43
+ end
44
+
45
+ def has_correct_group?
46
+ group.nil? || check_command_from_identifier(:check_file_is_grouped, destination, group)
47
+ end
48
+
49
+ def has_correct_mode?
50
+ mode.nil? || check_command_from_identifier(:check_file_has_mode, destination, mode_in_octal_notation)
51
+ end
52
+
53
+ def has_correct_owner?
54
+ owner.nil? || check_command_from_identifier(:check_file_is_owned_by, destination, owner)
55
+ end
56
+
57
+ def has_remote_file?
58
+ check_command_from_identifier(:check_file_is_file, destination)
59
+ end
60
+
61
+ # @return [String]
62
+ def local_content_sha256sum
63
+ ::Digest::SHA256.hexdigest(content)
64
+ end
65
+
66
+ # @return [String]
67
+ # @example "755" # for 0755
68
+ def mode_in_octal_notation
69
+ if mode.is_a?(Integer)
70
+ mode.to_s(8)
71
+ else
72
+ mode
73
+ end
74
+ end
75
+
76
+ # @return [String]
77
+ def remote_file_sha256sum
78
+ run_command_from_identifier(:get_file_sha256sum, destination).stdout.rstrip
79
+ end
80
+
81
+ def send_content_to_destination
82
+ ::Tempfile.open("") do |file|
83
+ file.write(content || "")
84
+ file.close
85
+ backend.send_file(file.path, destination)
86
+ end
87
+ end
88
+
89
+ # @note Override me
90
+ def update_entry
91
+ raise NotImplementedError
92
+ end
93
+
94
+ def update_group
95
+ run_command_from_identifier(:change_file_group, destination, group)
96
+ end
97
+
98
+ def update_mode
99
+ run_command_from_identifier(:change_file_mode, destination, mode_in_octal_notation)
100
+ end
101
+
102
+ def update_owner
103
+ run_command_from_identifier(:change_file_owner, destination, owner)
104
+ end
105
+ end
106
+ end
107
+ end
@@ -1,93 +1,27 @@
1
- require "digest"
2
- require "serverkit/resources/base"
1
+ require "serverkit/resources/entry"
3
2
  require "tempfile"
4
3
 
5
4
  module Serverkit
6
5
  module Resources
7
- class File < Base
6
+ class File < Entry
8
7
  attribute :content, type: String
9
- attribute :group, type: String
10
- attribute :mode, type: [Integer, String]
11
- attribute :owner, type: String
12
8
  attribute :path, required: true, type: String
13
9
 
14
- # @note Override
15
- def apply
16
- update_file unless has_correct_file?
17
- update_group unless has_correct_group?
18
- update_mode unless has_correct_mode?
19
- update_owner unless has_correct_owner?
20
- end
21
-
22
- # @note Override
23
- def check
24
- has_correct_file? && has_correct_group? && has_correct_mode? && has_correct_owner?
25
- end
26
-
27
10
  private
28
11
 
29
- # @return [String]
30
- def content_sha256sum
31
- ::Digest::SHA256.hexdigest(content)
32
- end
33
-
34
- # @return [String]
35
- def file_sha256sum
36
- run_command_from_identifier(:get_file_sha256sum, path).stdout.rstrip
37
- end
38
-
39
- def has_file?
40
- check_command_from_identifier(:check_file_is_file, path)
41
- end
42
-
43
- def has_correct_content?
44
- content.nil? || file_sha256sum == content_sha256sum
45
- end
46
-
47
- def has_correct_file?
48
- has_file? && has_correct_content?
49
- end
50
-
51
- def has_correct_group?
52
- group.nil? || check_command_from_identifier(:check_file_is_grouped, path, group)
53
- end
54
-
55
- def has_correct_mode?
56
- mode.nil? || check_command_from_identifier(:check_file_has_mode, path, mode_in_octal_notation)
57
- end
58
-
59
- def has_correct_owner?
60
- owner.nil? || check_command_from_identifier(:check_file_is_owned_by, path, owner)
61
- end
62
-
63
- # @return [String]
64
- # @example "755" # for 0755
65
- def mode_in_octal_notation
66
- if mode.is_a?(Integer)
67
- mode.to_s(8)
68
- else
69
- mode
70
- end
71
- end
72
-
73
- def update_file
74
- ::Tempfile.open("") do |file|
75
- file.write(content || "")
76
- file.close
77
- backend.send_file(file.path, path)
78
- end
79
- end
80
-
81
- def update_group
82
- run_command_from_identifier(:change_file_group, path, group)
12
+ # @note Override
13
+ def destination
14
+ path
83
15
  end
84
16
 
85
- def update_mode
86
- run_command_from_identifier(:change_file_mode, path, mode_in_octal_notation)
17
+ # @note Override
18
+ def has_correct_entry?
19
+ has_remote_file? && has_correct_content?
87
20
  end
88
21
 
89
- def update_owner
90
- run_command_from_identifier(:change_file_owner, path, owner)
22
+ # @note Override
23
+ def update_entry
24
+ send_content_to_destination
91
25
  end
92
26
  end
93
27
  end
@@ -1,67 +1,24 @@
1
- require "digest"
2
- require "serverkit/resources/base"
1
+ require "serverkit/resources/entry"
3
2
 
4
3
  module Serverkit
5
4
  module Resources
6
- class RemoteFile < Base
5
+ class RemoteFile < Entry
7
6
  attribute :destination, required: true, type: String
8
- attribute :group, type: String
9
- attribute :owner, type: String
10
7
  attribute :source, readable: true, required: true, type: String
11
8
 
12
- # @note Override
13
- def apply
14
- send_file_from_source_to_destination if file_sendable?
15
- change_group unless has_valid_group?
16
- change_owner unless has_valid_owner?
17
- end
18
-
19
- # @note Override
20
- def check
21
- has_file? && has_same_content? && has_valid_group? && has_valid_owner?
22
- end
23
-
24
9
  private
25
10
 
26
- def change_group
27
- run_command_from_identifier(:change_file_group, destination, group)
28
- end
29
-
30
- def change_owner
31
- run_command_from_identifier(:change_file_owner, destination, owner)
32
- end
33
-
34
- def file_sendable?
35
- !has_file? || !has_same_content?
36
- end
37
-
38
- def has_file?
39
- check_command_from_identifier(:check_file_is_file, destination)
40
- end
41
-
42
- def has_same_content?
43
- remote_file_sha256sum == local_file_sha256sum
11
+ def content
12
+ @content ||= ::File.read(source)
44
13
  end
45
14
 
46
- def has_valid_group?
47
- group.nil? || check_command_from_identifier(:check_file_is_grouped, destination, group)
48
- end
49
-
50
- def has_valid_owner?
51
- owner.nil? || check_command_from_identifier(:check_file_is_owned_by, destination, owner)
52
- end
53
-
54
- # @return [String]
55
- def local_file_sha256sum
56
- ::Digest::SHA256.hexdigest(::File.read(source))
57
- end
58
-
59
- # @return [String]
60
- def remote_file_sha256sum
61
- run_command_from_identifier(:get_file_sha256sum, destination).stdout.rstrip
15
+ # @note Override
16
+ def has_correct_entry?
17
+ has_remote_file? && has_correct_content?
62
18
  end
63
19
 
64
- def send_file_from_source_to_destination
20
+ # @note Override
21
+ def update_entry
65
22
  send_file(source, destination)
66
23
  end
67
24
  end
@@ -0,0 +1,40 @@
1
+ require "erb"
2
+ require "serverkit/loaders/variables_loader"
3
+ require "serverkit/resources/entry"
4
+ require "tempfile"
5
+
6
+ module Serverkit
7
+ module Resources
8
+ class Template < RemoteFile
9
+ DEFAULT_VARIABLES_DATA = {}
10
+
11
+ private
12
+
13
+ # @note Override
14
+ def content
15
+ @content ||= ::ERB.new(template_content, nil, "-").result(variables.to_mash.binding)
16
+ end
17
+
18
+ # @return [String] ERB content
19
+ def template_content
20
+ @template ||= ::File.read(source)
21
+ end
22
+
23
+ # @note Override
24
+ def update_entry
25
+ send_content_to_destination
26
+ end
27
+
28
+ # @return [Serverkit::Variables]
29
+ def variables
30
+ @variables ||= begin
31
+ if recipe.variables_path
32
+ Loaders::VariablesLoader.new(recipe.variables_path).load
33
+ else
34
+ Variables.new(DEFAULT_VARIABLES_DATA.dup)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Serverkit
2
- VERSION = "0.4.3"
2
+ VERSION = "0.4.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-19 00:00:00.000000000 Z
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -212,6 +212,8 @@ files:
212
212
  - Vagrantfile
213
213
  - bin/serverkit
214
214
  - example/recipe.yml
215
+ - example/template.erb
216
+ - example/variables.yml
215
217
  - lib/readable_validator.rb
216
218
  - lib/required_validator.rb
217
219
  - lib/serverkit.rb
@@ -243,6 +245,7 @@ files:
243
245
  - lib/serverkit/resources/base.rb
244
246
  - lib/serverkit/resources/command.rb
245
247
  - lib/serverkit/resources/directory.rb
248
+ - lib/serverkit/resources/entry.rb
246
249
  - lib/serverkit/resources/file.rb
247
250
  - lib/serverkit/resources/git.rb
248
251
  - lib/serverkit/resources/missing.rb
@@ -252,6 +255,7 @@ files:
252
255
  - lib/serverkit/resources/remote_file.rb
253
256
  - lib/serverkit/resources/service.rb
254
257
  - lib/serverkit/resources/symlink.rb
258
+ - lib/serverkit/resources/template.rb
255
259
  - lib/serverkit/resources/unknown.rb
256
260
  - lib/serverkit/resources/user.rb
257
261
  - lib/serverkit/variables.rb