serverkit 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10080848f2805e6c3836073587e6be22407c01e6
4
- data.tar.gz: 0867ac71a5aa865f8448aa12afbb513ed7c99db8
3
+ metadata.gz: 40d5fe026687af52454bb4f3507ee76df5c24892
4
+ data.tar.gz: f5ff7864e5a727ee33b68bc66624b9bc338d1a6d
5
5
  SHA512:
6
- metadata.gz: d69a3bf329a1cad28e178f10a3767aac3c2b2b03dbb6b431d71827e8883e9628e0543ca4e7633dc9e80a32a9c206cfad4f8d5466e480f68036a70bb04b5ac649
7
- data.tar.gz: 353146a60c1c4fe93439b205bba7750073459ffdcdd891ecfaa2f165bfddacab863692d2eb041d667c5caa22c18ee7b925ea9cb2f78f82db5a5ef3af1571d93a
6
+ metadata.gz: 2fe05e953601daa2a7dbaa05151d4142e09f8ef588af30e9e600efe3dd179f45843dff9aae5f6c7dd69e0a09b0eb65c950b748f67d2c27ca5971c3453ca1b8cd
7
+ data.tar.gz: 198e7ee4550b9aa16ff52a57056073612be2dcd00cd85cad0e0545b62881591343356312e84b2abe0def220b7311b893dd8cf3279ac3e619a5e731a5263d4df2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.0.5
2
+ - Add `recipe` resource type
3
+ - Improve validation error message
4
+
1
5
  ## 0.0.4
2
6
  - Support ERB recipe and variables injection
3
7
 
data/README.md CHANGED
@@ -35,6 +35,7 @@ serverkit apply --recipe=recipe.json.erb --variables=variables.json
35
35
  serverkit apply --recipe=recipe.json.erb --variables=variables.json.erb
36
36
  serverkit apply --recipe=recipe.json.erb --variables=variables.yml
37
37
  serverkit apply --recipe=recipe.json.erb --variables=variables.yml.erb
38
+ serverkit apply --recipe=recipe.json.erb --variables=variables/
38
39
  serverkit apply --recipe=recipe.yml
39
40
  serverkit apply --recipe=recipe.yml.erb
40
41
  serverkit apply --recipe=recipes/
@@ -82,9 +83,10 @@ A resource must have a type property. Currently the following types are availabl
82
83
 
83
84
  - file
84
85
  - git
85
- - homebrew_cask
86
86
  - homebrew
87
+ - homebrew_cask
87
88
  - package
89
+ - recipe
88
90
  - service
89
91
  - symlink
90
92
 
@@ -1,16 +1,4 @@
1
1
  resources:
2
- - id: install_mysql
3
- type: homebrew
4
- name: mysql
5
- - id: install_redis
6
- type: homebrew
7
- name: redis
8
- - id: install_licecap
9
- type: homebrew_cask
10
- name: licecap
11
- - id: install_alfred
12
- type: homebrew_cask
13
- name: alfred
14
2
  - id: clone_dotfiles
15
3
  type: git
16
4
  repository: git@github.com:<%= variables["dotfiles_github_repository"] %>.git
@@ -0,0 +1,7 @@
1
+ resources:
2
+ - id: install_mysql
3
+ type: homebrew
4
+ name: mysql
5
+ - id: install_redis
6
+ type: homebrew
7
+ name: redis
@@ -0,0 +1,7 @@
1
+ resources:
2
+ - id: install_licecap
3
+ type: homebrew_cask
4
+ name: licecap
5
+ - id: install_alfred
6
+ type: homebrew_cask
7
+ name: alfred
@@ -1,36 +1,19 @@
1
1
  {
2
2
  "resources": [
3
3
  {
4
- "id": "install_mysql",
5
- "type": "homebrew",
6
- "name": "mysql"
4
+ "id": "install_homebrew_packages",
5
+ "type": "recipe",
6
+ "path": "example/recipes/homebrew.yml"
7
7
  },
8
8
  {
9
- "id": "install_redis",
10
- "type": "homebrew",
11
- "name": "redis"
9
+ "id": "install_homebrew_cask_packages",
10
+ "type": "recipe",
11
+ "path": "example/recipes/homebrew_cask.yml"
12
12
  },
13
13
  {
14
- "id": "install_licecap",
15
- "type": "homebrew_cask",
16
- "name": "licecap"
17
- },
18
- {
19
- "id": "install_alfred",
20
- "type": "homebrew_cask",
21
- "name": "alfred"
22
- },
23
- {
24
- "id": "clone_dotfiles",
25
- "type": "git",
26
- "repository": "git@github.com:r7kamura/dotfiles.git",
27
- "path": "/Users/r7kamura/src/github.com/r7kamura/dotfiles"
28
- },
29
- {
30
- "id": "symlink_zshrc",
31
- "type": "symlink",
32
- "source": "/Users/r7kamura/.zshrc",
33
- "destination": "/Users/r7kamura/src/github.com/r7kamura/dotfiles/linked/.zshrc"
14
+ "id": "install_dotfiles",
15
+ "type": "recipe",
16
+ "path": "example/recipes/dotfiles.yml.erb"
34
17
  }
35
18
  ]
36
- }
19
+ }
@@ -1,21 +1,10 @@
1
1
  resources:
2
- - id: install_mysql
3
- type: homebrew
4
- name: mysql
5
- - id: install_redis
6
- type: homebrew
7
- name: redis
8
- - id: install_licecap
9
- type: homebrew_cask
10
- name: licecap
11
- - id: install_alfred
12
- type: homebrew_cask
13
- name: alfred
14
- - id: clone_dotfiles
15
- type: git
16
- repository: git@github.com:r7kamura/dotfiles.git
17
- path: /Users/r7kamura/src/github.com/r7kamura/dotfiles
18
- - id: symlink_zshrc
19
- type: symlink
20
- source: /Users/r7kamura/.zshrc
21
- destination: /Users/r7kamura/src/github.com/r7kamura/dotfiles/linked/.zshrc
2
+ - id: install_homebrew_packages
3
+ type: recipe
4
+ path: example/recipes/homebrew.yml
5
+ - id: install_homebrew_cask_packages
6
+ type: recipe
7
+ path: example/recipes/homebrew_cask.yml
8
+ - id: install_dotfiles
9
+ type: recipe
10
+ path: example/recipes/dotfiles.yml.erb
@@ -0,0 +1,20 @@
1
+ require "serverkit/errors/base"
2
+
3
+ module Serverkit
4
+ module Errors
5
+ class AttributeValidationError < Base
6
+ # @param [Serverkit::Resources::Base] resource
7
+ # @param [String] validation_error_message
8
+ def initialize(resource, attribute_name, validation_error_message)
9
+ @attribute_name = attribute_name
10
+ @resource = resource
11
+ @validation_error_message = validation_error_message
12
+ end
13
+
14
+ # @return [String]
15
+ def to_s
16
+ "#{@attribute_name} attribute #{@validation_error_message} in #{@resource.type} resource"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -2,7 +2,7 @@ require "serverkit/errors/base"
2
2
 
3
3
  module Serverkit
4
4
  module Errors
5
- class InvalidRecipeType < Base
5
+ class InvalidRecipeTypeError < Base
6
6
  # @param [Class] type
7
7
  def initialize(type)
8
8
  @type = type
@@ -2,7 +2,7 @@ require "serverkit/errors/base"
2
2
 
3
3
  module Serverkit
4
4
  module Errors
5
- class InvalidResourcesType < Base
5
+ class InvalidResourcesTypeError < Base
6
6
  # @param [Class] type
7
7
  def initialize(type)
8
8
  @type = type
@@ -0,0 +1,17 @@
1
+ require "serverkit/errors/base"
2
+
3
+ module Serverkit
4
+ module Errors
5
+ class UnknownResourceTypeError < Base
6
+ # @param [String] type
7
+ def initialize(type)
8
+ @type = type
9
+ end
10
+
11
+ # @return [String]
12
+ def to_s
13
+ "Unknown `#{@type}` resource type"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -15,7 +15,6 @@ module Serverkit
15
15
  end
16
16
 
17
17
  # @todo Rescue Error::ENOENT error
18
- # @return [Serverkit::Recipe]
19
18
  def load
20
19
  case
21
20
  when has_directory_path?
@@ -23,7 +22,7 @@ module Serverkit
23
22
  when has_erb_path?
24
23
  load_from_erb
25
24
  else
26
- loaded_class.new(load_data)
25
+ load_from_data
27
26
  end
28
27
  end
29
28
 
@@ -34,6 +33,16 @@ module Serverkit
34
33
  TOPLEVEL_BINDING
35
34
  end
36
35
 
36
+ # @note For override
37
+ def create_empty_loadable
38
+ loaded_class.new({})
39
+ end
40
+
41
+ # @return [String]
42
+ def execute
43
+ `#{pathname}`
44
+ end
45
+
37
46
  # @return [String]
38
47
  def expand_erb
39
48
  ERB.new(pathname.read).result(binding_for_erb)
@@ -54,11 +63,6 @@ module Serverkit
54
63
  end
55
64
  end
56
65
 
57
- # @return [String]
58
- def execute
59
- `#{pathname}`
60
- end
61
-
62
66
  def has_directory_path?
63
67
  pathname.directory?
64
68
  end
@@ -89,17 +93,19 @@ module Serverkit
89
93
  end
90
94
  end
91
95
 
92
- # @return [Serverkit::Recipe]
96
+ def load_from_data
97
+ loaded_class.new(load_data)
98
+ end
99
+
93
100
  def load_from_directory
94
- loads_from_directory.inject(loaded_class.new, :merge)
101
+ loads_from_directory.inject(create_empty_loadable, :merge)
95
102
  end
96
103
 
97
- # @return [Serverkit::Recipe]
98
104
  def load_from_erb
99
105
  self.class.new(expanded_erb_tempfile.path).load
100
106
  end
101
107
 
102
- # @return [Array<Serverkit::Recipe>]
108
+ # @return [Array]
103
109
  def loads_from_directory
104
110
  Dir.glob(pathname.join("*")).sort.flat_map do |path|
105
111
  self.class.new(path).load
@@ -21,10 +21,20 @@ module Serverkit
21
21
  ErbBindingContext.new(variables_data).binding
22
22
  end
23
23
 
24
+ # @note Override to pass @variables_path
25
+ def create_empty_loadable
26
+ loaded_class.new({}, @variables_path)
27
+ end
28
+
24
29
  def has_variables_path?
25
30
  !@variables_path.nil?
26
31
  end
27
32
 
33
+ # @note Override to pass @variables_path
34
+ def load_from_data
35
+ loaded_class.new(load_data, @variables_path)
36
+ end
37
+
28
38
  # @return [Serverkit::Variables]
29
39
  def load_variables
30
40
  Loaders::VariablesLoader.new(@variables_path).load
@@ -1,21 +1,17 @@
1
1
  require "active_support/core_ext/hash/deep_merge"
2
- require "active_support/core_ext/string/inflections"
3
- require "serverkit/errors/invalid_recipe_type"
4
- require "serverkit/errors/invalid_resources_type"
5
- require "serverkit/resources/file"
6
- require "serverkit/resources/git"
7
- require "serverkit/resources/homebrew"
8
- require "serverkit/resources/homebrew_cask"
9
- require "serverkit/resources/service"
10
- require "serverkit/resources/symlink"
2
+ require "serverkit/errors/invalid_recipe_type_error"
3
+ require "serverkit/errors/invalid_resources_type_error"
4
+ require "serverkit/resource_builder"
11
5
 
12
6
  module Serverkit
13
7
  class Recipe
14
- attr_reader :recipe_data
8
+ attr_reader :recipe_data, :variables_path
15
9
 
16
10
  # @param [Hash] recipe_data
17
- def initialize(recipe_data = {})
11
+ # @param [String, nil] variables_path Used for recipe resource to render ERB template
12
+ def initialize(recipe_data, variables_path = nil)
18
13
  @recipe_data = recipe_data
14
+ @variables_path = variables_path
19
15
  end
20
16
 
21
17
  # @return [Array<Serverkit::Errors::Base>]
@@ -32,11 +28,6 @@ module Serverkit
32
28
  end
33
29
  end
34
30
 
35
- # @return [Array<String>]
36
- def hosts
37
- @recipe_data["hosts"]
38
- end
39
-
40
31
  # @param [Serverkit::Recipe] recipe
41
32
  # @return [Serverkit::Recipe]
42
33
  def merge(recipe)
@@ -51,11 +42,11 @@ module Serverkit
51
42
  )
52
43
  end
53
44
 
45
+ # @note recipe resource will be expanded into resources defined in its recipe
54
46
  # @return [Array<Serverkit::Resources::Base>]
55
- # @todo Delegate to resource builder
56
47
  def resources
57
- @resources ||= resources_property.map do |attributes|
58
- Resources.const_get(attributes["type"].camelize, false).new(attributes)
48
+ @resources ||= resources_property.flat_map do |attributes|
49
+ ResourceBuilder.new(self, attributes).build.to_a
59
50
  end
60
51
  end
61
52
 
@@ -67,20 +58,17 @@ module Serverkit
67
58
 
68
59
  # @return [Array<Serverkit::Errors::Base>]
69
60
  def errors_for_invalid_typed_recipe_data
70
- [Errors::InvalidRecipeType.new(@recipe_data.class)]
61
+ [Errors::InvalidRecipeTypeError.new(@recipe_data.class)]
71
62
  end
72
63
 
73
64
  # @return [Array<Serverkit::Errors::Base>]
74
65
  def errors_for_invalid_typed_resources_property
75
- [Errors::InvalidResourcesType.new(resources_property.class)]
66
+ [Errors::InvalidResourcesTypeError.new(resources_property.class)]
76
67
  end
77
68
 
78
- # @return [Array<Serverkit::Errors::Base>]
69
+ # @return [Array<Serverkit::Errors::AttributeValidationError>]
79
70
  def errors_in_resources
80
- resources.flat_map do |resource|
81
- resource.validate
82
- resource.errors.to_a
83
- end
71
+ resources.flat_map(&:all_errors)
84
72
  end
85
73
 
86
74
  def has_valid_typed_resources_property?
@@ -0,0 +1,51 @@
1
+ require "active_support/core_ext/string/inflections"
2
+ require "serverkit/resources/file"
3
+ require "serverkit/resources/git"
4
+ require "serverkit/resources/homebrew_cask"
5
+ require "serverkit/resources/homebrew"
6
+ require "serverkit/resources/recipe"
7
+ require "serverkit/resources/service"
8
+ require "serverkit/resources/symlink"
9
+ require "serverkit/resources/unknown"
10
+
11
+ module Serverkit
12
+ class ResourceBuilder
13
+ # @param [Serverkit::Recipe] recipe
14
+ # @param [Hash] attributes
15
+ def initialize(recipe, attributes)
16
+ @attributes = attributes
17
+ @recipe = recipe
18
+ end
19
+
20
+ # @return [Serverkit::Resources::Base]
21
+ def build
22
+ resource_class.new(@recipe, @attributes)
23
+ end
24
+
25
+ private
26
+
27
+ def has_known_type?
28
+ Resources.constants.map(&:to_s).include?(resource_class_name)
29
+ end
30
+
31
+ # @return [Class]
32
+ def resource_class
33
+ if has_known_type?
34
+ Resources.const_get(resource_class_name, false)
35
+ else
36
+ Resources::Unknown
37
+ end
38
+ end
39
+
40
+ # @return [String] (e.g. "File", "Symlink")
41
+ def resource_class_name
42
+ type.camelize
43
+ end
44
+
45
+ # @note Expected to return String in normal case
46
+ # @return [Object] (e.g. "file", "symlink")
47
+ def type
48
+ @attributes["type"]
49
+ end
50
+ end
51
+ end
@@ -1,6 +1,7 @@
1
1
  require "active_model"
2
2
  require "readable_validator"
3
3
  require "required_validator"
4
+ require "serverkit/errors/attribute_validation_error"
4
5
  require "type_validator"
5
6
 
6
7
  module Serverkit
@@ -21,15 +22,44 @@ module Serverkit
21
22
 
22
23
  attr_accessor :backend
23
24
 
25
+ attr_reader :recipe
26
+
24
27
  attribute :id, required: true, type: String
25
28
 
29
+ # @param [Serverkit::Recipe] recipe
26
30
  # @param [Hash] attributes
27
- def initialize(attributes)
31
+ def initialize(recipe, attributes)
28
32
  @attributes = attributes
33
+ @recipe = recipe
34
+ end
35
+
36
+ # @note For override use
37
+ # @return [Array<Serverkit::Errors::Base>]
38
+ def all_errors
39
+ attribute_validation_errors
40
+ end
41
+
42
+ # @note recipe resource will override to replace itself with multiple resources
43
+ # @return [Array<Serverkit::Resources::Base>]
44
+ def to_a
45
+ [self]
46
+ end
47
+
48
+ # @return [String]
49
+ def type
50
+ @attributes["type"]
29
51
  end
30
52
 
31
53
  private
32
54
 
55
+ # @return [Array<Serverkit::Errors::AttributeValidationError>]
56
+ def attribute_validation_errors
57
+ validate
58
+ errors.map do |attribute_name, message|
59
+ Serverkit::Errors::AttributeValidationError.new(self, attribute_name, message)
60
+ end
61
+ end
62
+
33
63
  # @return [true, false]
34
64
  def check_command(*args)
35
65
  run_command(*args).success?
@@ -0,0 +1,21 @@
1
+ require "serverkit/resources/base"
2
+
3
+ module Serverkit
4
+ module Resources
5
+ class Recipe < Base
6
+ attribute :path, readable: true, required: true, type: String
7
+
8
+ # @note Override
9
+ def to_a
10
+ loaded_recipe.resources
11
+ end
12
+
13
+ private
14
+
15
+ # @return [Serverkit::Recipe] Recipe loaded from given path
16
+ def loaded_recipe
17
+ @loaded_recipe ||= Loaders::RecipeLoader.new(path, variables_path: recipe.variables_path).load
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ require "serverkit/errors/unknown_resource_type_error"
2
+ require "serverkit/resources/base"
3
+
4
+ module Serverkit
5
+ module Resources
6
+ class Unknown < Base
7
+ # @return [Array<Serverkit::Errors::UnknownResourceTypeError>]
8
+ def all_errors
9
+ [Errors::UnknownResourceTypeError.new(type)]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -5,7 +5,7 @@ module Serverkit
5
5
  attr_reader :variables_data
6
6
 
7
7
  # @param [Hash] variables_data
8
- def initialize(variables_data = {})
8
+ def initialize(variables_data)
9
9
  @variables_data = variables_data
10
10
  end
11
11
 
@@ -1,3 +1,3 @@
1
1
  module Serverkit
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
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.0.4
4
+ version: 0.0.5
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-02 00:00:00.000000000 Z
11
+ date: 2015-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -154,10 +154,12 @@ files:
154
154
  - README.md
155
155
  - Rakefile
156
156
  - bin/serverkit
157
+ - example/recipes/dotfiles.yml.erb
158
+ - example/recipes/homebrew.yml
159
+ - example/recipes/homebrew_cask.yml
157
160
  - example/recipes/recipe.json
158
161
  - example/recipes/recipe.rb
159
162
  - example/recipes/recipe.yml
160
- - example/recipes/recipe.yml.erb
161
163
  - example/variables/variables.yml
162
164
  - lib/readable_validator.rb
163
165
  - lib/required_validator.rb
@@ -167,21 +169,26 @@ files:
167
169
  - lib/serverkit/actions/check.rb
168
170
  - lib/serverkit/actions/validate.rb
169
171
  - lib/serverkit/command.rb
172
+ - lib/serverkit/errors/attribute_validation_error.rb
170
173
  - lib/serverkit/errors/base.rb
171
- - lib/serverkit/errors/invalid_recipe_type.rb
172
- - lib/serverkit/errors/invalid_resources_type.rb
174
+ - lib/serverkit/errors/invalid_recipe_type_error.rb
175
+ - lib/serverkit/errors/invalid_resources_type_error.rb
176
+ - lib/serverkit/errors/unknown_resource_type_error.rb
173
177
  - lib/serverkit/loaders/base_loader.rb
174
178
  - lib/serverkit/loaders/recipe_loader.rb
175
179
  - lib/serverkit/loaders/variables_loader.rb
176
180
  - lib/serverkit/recipe.rb
181
+ - lib/serverkit/resource_builder.rb
177
182
  - lib/serverkit/resources/base.rb
178
183
  - lib/serverkit/resources/file.rb
179
184
  - lib/serverkit/resources/git.rb
180
185
  - lib/serverkit/resources/homebrew.rb
181
186
  - lib/serverkit/resources/homebrew_cask.rb
182
187
  - lib/serverkit/resources/package.rb
188
+ - lib/serverkit/resources/recipe.rb
183
189
  - lib/serverkit/resources/service.rb
184
190
  - lib/serverkit/resources/symlink.rb
191
+ - lib/serverkit/resources/unknown.rb
185
192
  - lib/serverkit/variables.rb
186
193
  - lib/serverkit/version.rb
187
194
  - lib/type_validator.rb