chef-zero 15.0.17 → 15.0.21

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.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +36 -31
  3. data/LICENSE +201 -201
  4. data/Rakefile +73 -68
  5. data/bin/chef-zero +111 -111
  6. data/chef-zero.gemspec +34 -33
  7. data/lib/chef_zero/chef_data/acl_path.rb +140 -140
  8. data/lib/chef_zero/chef_data/cookbook_data.rb +237 -237
  9. data/lib/chef_zero/chef_data/data_normalizer.rb +276 -276
  10. data/lib/chef_zero/chef_data/default_creator.rb +476 -476
  11. data/lib/chef_zero/data_store/data_already_exists_error.rb +29 -29
  12. data/lib/chef_zero/data_store/data_error.rb +32 -32
  13. data/lib/chef_zero/data_store/data_not_found_error.rb +29 -29
  14. data/lib/chef_zero/data_store/default_facade.rb +143 -147
  15. data/lib/chef_zero/data_store/interface_v1.rb +67 -67
  16. data/lib/chef_zero/data_store/interface_v2.rb +18 -18
  17. data/lib/chef_zero/data_store/memory_store.rb +33 -33
  18. data/lib/chef_zero/data_store/memory_store_v2.rb +159 -159
  19. data/lib/chef_zero/data_store/raw_file_store.rb +143 -143
  20. data/lib/chef_zero/data_store/v1_to_v2_adapter.rb +150 -150
  21. data/lib/chef_zero/data_store/v2_to_v1_adapter.rb +105 -105
  22. data/lib/chef_zero/dist.rb +9 -9
  23. data/lib/chef_zero/endpoints/acl_endpoint.rb +39 -39
  24. data/lib/chef_zero/endpoints/acls_endpoint.rb +41 -41
  25. data/lib/chef_zero/endpoints/actor_default_key_endpoint.rb +78 -78
  26. data/lib/chef_zero/endpoints/actor_endpoint.rb +184 -184
  27. data/lib/chef_zero/endpoints/actor_key_endpoint.rb +62 -62
  28. data/lib/chef_zero/endpoints/actor_keys_endpoint.rb +129 -129
  29. data/lib/chef_zero/endpoints/actors_endpoint.rb +104 -104
  30. data/lib/chef_zero/endpoints/authenticate_user_endpoint.rb +32 -32
  31. data/lib/chef_zero/endpoints/container_endpoint.rb +22 -22
  32. data/lib/chef_zero/endpoints/containers_endpoint.rb +25 -25
  33. data/lib/chef_zero/endpoints/controls_endpoint.rb +16 -16
  34. data/lib/chef_zero/endpoints/cookbook_artifact_endpoint.rb +24 -24
  35. data/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb +68 -68
  36. data/lib/chef_zero/endpoints/cookbook_artifacts_endpoint.rb +34 -34
  37. data/lib/chef_zero/endpoints/cookbook_endpoint.rb +39 -39
  38. data/lib/chef_zero/endpoints/cookbook_version_endpoint.rb +136 -136
  39. data/lib/chef_zero/endpoints/cookbooks_base.rb +80 -80
  40. data/lib/chef_zero/endpoints/cookbooks_endpoint.rb +19 -19
  41. data/lib/chef_zero/endpoints/data_bag_endpoint.rb +45 -45
  42. data/lib/chef_zero/endpoints/data_bag_item_endpoint.rb +25 -25
  43. data/lib/chef_zero/endpoints/data_bags_endpoint.rb +23 -23
  44. data/lib/chef_zero/endpoints/dummy_endpoint.rb +29 -29
  45. data/lib/chef_zero/endpoints/environment_cookbook_endpoint.rb +24 -24
  46. data/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb +126 -126
  47. data/lib/chef_zero/endpoints/environment_cookbooks_endpoint.rb +22 -22
  48. data/lib/chef_zero/endpoints/environment_endpoint.rb +33 -33
  49. data/lib/chef_zero/endpoints/environment_nodes_endpoint.rb +23 -23
  50. data/lib/chef_zero/endpoints/environment_recipes_endpoint.rb +22 -22
  51. data/lib/chef_zero/endpoints/environment_role_endpoint.rb +36 -36
  52. data/lib/chef_zero/endpoints/file_store_file_endpoint.rb +22 -22
  53. data/lib/chef_zero/endpoints/group_endpoint.rb +20 -20
  54. data/lib/chef_zero/endpoints/groups_endpoint.rb +13 -13
  55. data/lib/chef_zero/endpoints/license_endpoint.rb +25 -25
  56. data/lib/chef_zero/endpoints/node_endpoint.rb +34 -34
  57. data/lib/chef_zero/endpoints/node_identifiers_endpoint.rb +22 -22
  58. data/lib/chef_zero/endpoints/nodes_endpoint.rb +34 -34
  59. data/lib/chef_zero/endpoints/not_found_endpoint.rb +11 -11
  60. data/lib/chef_zero/endpoints/organization_association_request_endpoint.rb +22 -22
  61. data/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb +30 -30
  62. data/lib/chef_zero/endpoints/organization_authenticate_user_endpoint.rb +26 -26
  63. data/lib/chef_zero/endpoints/organization_endpoint.rb +47 -47
  64. data/lib/chef_zero/endpoints/organization_user_base.rb +15 -15
  65. data/lib/chef_zero/endpoints/organization_user_default_key_endpoint.rb +16 -16
  66. data/lib/chef_zero/endpoints/organization_user_endpoint.rb +26 -26
  67. data/lib/chef_zero/endpoints/organization_user_key_endpoint.rb +17 -17
  68. data/lib/chef_zero/endpoints/organization_user_keys_endpoint.rb +17 -17
  69. data/lib/chef_zero/endpoints/organization_users_endpoint.rb +43 -43
  70. data/lib/chef_zero/endpoints/organization_validator_key_endpoint.rb +20 -20
  71. data/lib/chef_zero/endpoints/organizations_endpoint.rb +61 -61
  72. data/lib/chef_zero/endpoints/policies_endpoint.rb +26 -26
  73. data/lib/chef_zero/endpoints/policy_endpoint.rb +24 -24
  74. data/lib/chef_zero/endpoints/policy_group_endpoint.rb +46 -46
  75. data/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb +83 -83
  76. data/lib/chef_zero/endpoints/policy_groups_endpoint.rb +38 -38
  77. data/lib/chef_zero/endpoints/policy_revision_endpoint.rb +66 -66
  78. data/lib/chef_zero/endpoints/policy_revisions_endpoint.rb +15 -15
  79. data/lib/chef_zero/endpoints/principal_endpoint.rb +55 -55
  80. data/lib/chef_zero/endpoints/rest_list_endpoint.rb +42 -42
  81. data/lib/chef_zero/endpoints/rest_object_endpoint.rb +78 -78
  82. data/lib/chef_zero/endpoints/role_endpoint.rb +16 -16
  83. data/lib/chef_zero/endpoints/role_environments_endpoint.rb +14 -14
  84. data/lib/chef_zero/endpoints/sandbox_endpoint.rb +27 -27
  85. data/lib/chef_zero/endpoints/sandboxes_endpoint.rb +51 -51
  86. data/lib/chef_zero/endpoints/search_endpoint.rb +208 -208
  87. data/lib/chef_zero/endpoints/searches_endpoint.rb +18 -18
  88. data/lib/chef_zero/endpoints/server_api_version_endpoint.rb +14 -14
  89. data/lib/chef_zero/endpoints/system_recovery_endpoint.rb +30 -30
  90. data/lib/chef_zero/endpoints/universe_endpoint.rb +15 -15
  91. data/lib/chef_zero/endpoints/user_association_request_endpoint.rb +41 -41
  92. data/lib/chef_zero/endpoints/user_association_requests_count_endpoint.rb +19 -19
  93. data/lib/chef_zero/endpoints/user_association_requests_endpoint.rb +19 -19
  94. data/lib/chef_zero/endpoints/user_organizations_endpoint.rb +22 -22
  95. data/lib/chef_zero/endpoints/version_endpoint.rb +13 -13
  96. data/lib/chef_zero/log.rb +7 -7
  97. data/lib/chef_zero/rest_base.rb +332 -332
  98. data/lib/chef_zero/rest_error_response.rb +11 -11
  99. data/lib/chef_zero/rest_request.rb +84 -88
  100. data/lib/chef_zero/rest_router.rb +72 -72
  101. data/lib/chef_zero/rspec.rb +355 -355
  102. data/lib/chef_zero/server.rb +730 -730
  103. data/lib/chef_zero/socketless_server_map.rb +92 -93
  104. data/lib/chef_zero/solr/query/binary_operator.rb +52 -52
  105. data/lib/chef_zero/solr/query/phrase.rb +23 -23
  106. data/lib/chef_zero/solr/query/range_query.rb +46 -46
  107. data/lib/chef_zero/solr/query/regexpable_query.rb +30 -30
  108. data/lib/chef_zero/solr/query/subquery.rb +37 -37
  109. data/lib/chef_zero/solr/query/term.rb +45 -45
  110. data/lib/chef_zero/solr/query/unary_operator.rb +41 -41
  111. data/lib/chef_zero/solr/solr_doc.rb +53 -53
  112. data/lib/chef_zero/solr/solr_parser.rb +208 -208
  113. data/lib/chef_zero/version.rb +3 -3
  114. data/lib/chef_zero.rb +10 -10
  115. data/spec/run_oc_pedant.rb +226 -226
  116. data/spec/search_spec.rb +36 -36
  117. data/spec/server_spec.rb +96 -96
  118. data/spec/socketless_server_map_spec.rb +74 -74
  119. data/spec/support/oc_pedant.rb +149 -149
  120. data/spec/support/secrets.json +6 -6
  121. data/spec/support/stickywicket.pem +27 -27
  122. metadata +35 -18
@@ -1,33 +1,33 @@
1
- #
2
- # Author:: John Keiser (<jkeiser@opscode.com>)
3
- # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # https://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require_relative "v2_to_v1_adapter"
20
- require_relative "memory_store_v2"
21
- require_relative "default_facade"
22
-
23
- module ChefZero
24
- module DataStore
25
- class MemoryStore < ChefZero::DataStore::V2ToV1Adapter
26
- def initialize
27
- super
28
- @real_store = ChefZero::DataStore::DefaultFacade.new(ChefZero::DataStore::MemoryStoreV2.new, "chef", true)
29
- clear
30
- end
31
- end
32
- end
33
- end
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require_relative "v2_to_v1_adapter"
20
+ require_relative "memory_store_v2"
21
+ require_relative "default_facade"
22
+
23
+ module ChefZero
24
+ module DataStore
25
+ class MemoryStore < ChefZero::DataStore::V2ToV1Adapter
26
+ def initialize
27
+ super
28
+ @real_store = ChefZero::DataStore::DefaultFacade.new(ChefZero::DataStore::MemoryStoreV2.new, "chef", true)
29
+ clear
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,159 +1,159 @@
1
- #
2
- # Author:: John Keiser (<jkeiser@opscode.com>)
3
- # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # https://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require_relative "data_already_exists_error"
20
- require_relative "data_not_found_error"
21
- require_relative "interface_v2"
22
-
23
- module ChefZero
24
- module DataStore
25
- class MemoryStoreV2 < ChefZero::DataStore::InterfaceV2
26
- def initialize
27
- clear
28
- end
29
-
30
- attr_reader :data
31
-
32
- def clear
33
- @data = {}
34
- end
35
-
36
- def create_dir(path, name, *options)
37
- parent = _get(path, options.include?(:recursive))
38
-
39
- if parent.key?(name)
40
- unless options.include?(:recursive)
41
- raise DataAlreadyExistsError.new(path + [name])
42
- end
43
- else
44
- parent[name] = {}
45
- end
46
- end
47
-
48
- def create(path, name, data, *options)
49
- unless data.is_a?(String)
50
- raise "set only works with strings (given data: #{data.inspect})"
51
- end
52
-
53
- parent = _get(path, options.include?(:create_dir))
54
-
55
- if parent.key?(name)
56
- raise DataAlreadyExistsError.new(path + [name])
57
- end
58
-
59
- parent[name] = data
60
- end
61
-
62
- def get(path, request = nil)
63
- value = _get(path)
64
- if value.is_a?(Hash)
65
- raise "get() called on directory #{path.join("/")}"
66
- end
67
-
68
- value
69
- end
70
-
71
- def set(path, data, *options)
72
- unless data.is_a?(String)
73
- raise "set only works with strings: #{path} = #{data.inspect}"
74
- end
75
-
76
- # Get the parent
77
- parent = _get(path[0..-2], options.include?(:create_dir))
78
-
79
- if !options.include?(:create) && !parent[path[-1]]
80
- raise DataNotFoundError.new(path)
81
- end
82
-
83
- parent[path[-1]] = data
84
- end
85
-
86
- def delete(path)
87
- parent = _get(path[0, path.length - 1])
88
- unless parent.key?(path[-1])
89
- raise DataNotFoundError.new(path)
90
- end
91
- unless parent[path[-1]].is_a?(String)
92
- raise "delete only works with strings: #{path}"
93
- end
94
-
95
- parent.delete(path[-1])
96
- end
97
-
98
- def delete_dir(path, *options)
99
- parent = _get(path[0, path.length - 1])
100
- unless parent.key?(path[-1])
101
- raise DataNotFoundError.new(path)
102
- end
103
- unless parent[path[-1]].is_a?(Hash)
104
- raise "delete_dir only works with directories: #{path}"
105
- end
106
-
107
- parent.delete(path[-1])
108
- end
109
-
110
- def list(path)
111
- dir = _get(path)
112
- unless dir.is_a? Hash
113
- raise "list only works with directories (#{path} = #{dir.class})"
114
- end
115
-
116
- dir.keys.sort
117
- end
118
-
119
- def exists?(path, options = {})
120
- value = _get(path)
121
- if value.is_a?(Hash) && !options[:allow_dirs]
122
- raise "exists? does not work with directories (#{path} = #{value.class})"
123
- end
124
-
125
- true
126
- rescue DataNotFoundError
127
- false
128
- end
129
-
130
- def exists_dir?(path)
131
- dir = _get(path)
132
- unless dir.is_a? Hash
133
- raise "exists_dir? only works with directories (#{path} = #{dir.class})"
134
- end
135
-
136
- true
137
- rescue DataNotFoundError
138
- false
139
- end
140
-
141
- private
142
-
143
- def _get(path, create_dir = false)
144
- value = @data
145
- path.each_with_index do |path_part, index|
146
- unless value.key?(path_part)
147
- if create_dir
148
- value[path_part] = {}
149
- else
150
- raise DataNotFoundError.new(path[0, index + 1])
151
- end
152
- end
153
- value = value[path_part]
154
- end
155
- value
156
- end
157
- end
158
- end
159
- end
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require_relative "data_already_exists_error"
20
+ require_relative "data_not_found_error"
21
+ require_relative "interface_v2"
22
+
23
+ module ChefZero
24
+ module DataStore
25
+ class MemoryStoreV2 < ChefZero::DataStore::InterfaceV2
26
+ def initialize
27
+ clear
28
+ end
29
+
30
+ attr_reader :data
31
+
32
+ def clear
33
+ @data = {}
34
+ end
35
+
36
+ def create_dir(path, name, *options)
37
+ parent = _get(path, options.include?(:recursive))
38
+
39
+ if parent.key?(name)
40
+ unless options.include?(:recursive)
41
+ raise DataAlreadyExistsError.new(path + [name])
42
+ end
43
+ else
44
+ parent[name] = {}
45
+ end
46
+ end
47
+
48
+ def create(path, name, data, *options)
49
+ unless data.is_a?(String)
50
+ raise "set only works with strings (given data: #{data.inspect})"
51
+ end
52
+
53
+ parent = _get(path, options.include?(:create_dir))
54
+
55
+ if parent.key?(name)
56
+ raise DataAlreadyExistsError.new(path + [name])
57
+ end
58
+
59
+ parent[name] = data
60
+ end
61
+
62
+ def get(path, request = nil)
63
+ value = _get(path)
64
+ if value.is_a?(Hash)
65
+ raise "get() called on directory #{path.join("/")}"
66
+ end
67
+
68
+ value
69
+ end
70
+
71
+ def set(path, data, *options)
72
+ unless data.is_a?(String)
73
+ raise "set only works with strings: #{path} = #{data.inspect}"
74
+ end
75
+
76
+ # Get the parent
77
+ parent = _get(path[0..-2], options.include?(:create_dir))
78
+
79
+ if !options.include?(:create) && !parent[path[-1]]
80
+ raise DataNotFoundError.new(path)
81
+ end
82
+
83
+ parent[path[-1]] = data
84
+ end
85
+
86
+ def delete(path)
87
+ parent = _get(path[0, path.length - 1])
88
+ unless parent.key?(path[-1])
89
+ raise DataNotFoundError.new(path)
90
+ end
91
+ unless parent[path[-1]].is_a?(String)
92
+ raise "delete only works with strings: #{path}"
93
+ end
94
+
95
+ parent.delete(path[-1])
96
+ end
97
+
98
+ def delete_dir(path, *options)
99
+ parent = _get(path[0, path.length - 1])
100
+ unless parent.key?(path[-1])
101
+ raise DataNotFoundError.new(path)
102
+ end
103
+ unless parent[path[-1]].is_a?(Hash)
104
+ raise "delete_dir only works with directories: #{path}"
105
+ end
106
+
107
+ parent.delete(path[-1])
108
+ end
109
+
110
+ def list(path)
111
+ dir = _get(path)
112
+ unless dir.is_a? Hash
113
+ raise "list only works with directories (#{path} = #{dir.class})"
114
+ end
115
+
116
+ dir.keys.sort
117
+ end
118
+
119
+ def exists?(path, options = {})
120
+ value = _get(path)
121
+ if value.is_a?(Hash) && !options[:allow_dirs]
122
+ raise "exists? does not work with directories (#{path} = #{value.class})"
123
+ end
124
+
125
+ true
126
+ rescue DataNotFoundError
127
+ false
128
+ end
129
+
130
+ def exists_dir?(path)
131
+ dir = _get(path)
132
+ unless dir.is_a? Hash
133
+ raise "exists_dir? only works with directories (#{path} = #{dir.class})"
134
+ end
135
+
136
+ true
137
+ rescue DataNotFoundError
138
+ false
139
+ end
140
+
141
+ private
142
+
143
+ def _get(path, create_dir = false)
144
+ value = @data
145
+ path.each_with_index do |path_part, index|
146
+ unless value.key?(path_part)
147
+ if create_dir
148
+ value[path_part] = {}
149
+ else
150
+ raise DataNotFoundError.new(path[0, index + 1])
151
+ end
152
+ end
153
+ value = value[path_part]
154
+ end
155
+ value
156
+ end
157
+ end
158
+ end
159
+ end
@@ -1,143 +1,143 @@
1
- #
2
- # Author:: John Keiser (<jkeiser@opscode.com>)
3
- # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # https://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require_relative "data_already_exists_error"
20
- require_relative "data_not_found_error"
21
- require_relative "interface_v2"
22
- require "fileutils" unless defined?(FileUtils)
23
-
24
- module ChefZero
25
- module DataStore
26
- class RawFileStore < ChefZero::DataStore::InterfaceV2
27
- def initialize(root, destructible = false)
28
- @root = root
29
- @destructible = destructible
30
- end
31
-
32
- attr_reader :root
33
- attr_reader :destructible
34
-
35
- def path_to(path, name = nil)
36
- if name
37
- File.join(root, *path, name)
38
- else
39
- File.join(root, *path)
40
- end
41
- end
42
-
43
- def clear
44
- if destructible
45
- Dir.entries(root).each do |entry|
46
- next if entry == "." || entry == ".."
47
-
48
- FileUtils.rm_rf(Path.join(root, entry))
49
- end
50
- end
51
- end
52
-
53
- def create_dir(path, name, *options)
54
- real_path = path_to(path, name)
55
- if options.include?(:recursive)
56
- FileUtils.mkdir_p(real_path)
57
- else
58
- begin
59
- Dir.mkdir(File.join(path, name))
60
- rescue Errno::ENOENT
61
- raise DataNotFoundError.new(path)
62
- rescue Errno::EEXIST
63
- raise DataAlreadyExistsError.new(path + [name])
64
- end
65
- end
66
- end
67
-
68
- def create(path, name, data, *options)
69
- if options.include?(:create_dir)
70
- FileUtils.mkdir_p(path_to(path))
71
- end
72
- begin
73
- File.open(path_to(path, name), File::WRONLY | File::CREAT | File::EXCL | File::BINARY, internal_encoding: nil) do |file|
74
- file.write data
75
- end
76
- rescue Errno::ENOENT
77
- raise DataNotFoundError.new(path)
78
- rescue Errno::EEXIST
79
- raise DataAlreadyExistsError.new(path + [name])
80
- end
81
- end
82
-
83
- def get(path, request = nil)
84
- IO.read(path_to(path))
85
- rescue Errno::ENOENT
86
- raise DataNotFoundError.new(path)
87
- end
88
-
89
- def set(path, data, *options)
90
- if options.include?(:create_dir)
91
- FileUtils.mkdir_p(path_to(path[0..-2]))
92
- end
93
- begin
94
- mode = File::WRONLY | File::TRUNC | File::BINARY
95
- if options.include?(:create)
96
- mode |= File::CREAT
97
- end
98
- File.open(path_to(path), mode, internal_encoding: nil) do |file|
99
- file.write data
100
- end
101
- rescue Errno::ENOENT
102
- raise DataNotFoundError.new(path)
103
- end
104
- end
105
-
106
- def delete(path)
107
- File.delete(path_to(path))
108
- rescue Errno::ENOENT
109
- raise DataNotFoundError.new(path)
110
- end
111
-
112
- def delete_dir(path, *options)
113
- if options.include?(:recursive)
114
- unless File.exist?(path_to(path))
115
- raise DataNotFoundError.new(path)
116
- end
117
-
118
- FileUtils.rm_rf(path_to(path))
119
- else
120
- begin
121
- Dir.rmdir(path_to(path))
122
- rescue Errno::ENOENT
123
- raise DataNotFoundError.new(path)
124
- end
125
- end
126
- end
127
-
128
- def list(path)
129
- Dir.entries(path_to(path)).select { |entry| entry != "." && entry != ".." }.to_a
130
- rescue Errno::ENOENT
131
- raise DataNotFoundError.new(path)
132
- end
133
-
134
- def exists?(path, options = {})
135
- File.exist?(path_to(path))
136
- end
137
-
138
- def exists_dir?(path)
139
- File.exist?(path_to(path))
140
- end
141
- end
142
- end
143
- end
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require_relative "data_already_exists_error"
20
+ require_relative "data_not_found_error"
21
+ require_relative "interface_v2"
22
+ require "fileutils" unless defined?(FileUtils)
23
+
24
+ module ChefZero
25
+ module DataStore
26
+ class RawFileStore < ChefZero::DataStore::InterfaceV2
27
+ def initialize(root, destructible = false)
28
+ @root = root
29
+ @destructible = destructible
30
+ end
31
+
32
+ attr_reader :root
33
+ attr_reader :destructible
34
+
35
+ def path_to(path, name = nil)
36
+ if name
37
+ File.join(root, *path, name)
38
+ else
39
+ File.join(root, *path)
40
+ end
41
+ end
42
+
43
+ def clear
44
+ if destructible
45
+ Dir.entries(root).each do |entry|
46
+ next if entry == "." || entry == ".."
47
+
48
+ FileUtils.rm_rf(Path.join(root, entry))
49
+ end
50
+ end
51
+ end
52
+
53
+ def create_dir(path, name, *options)
54
+ real_path = path_to(path, name)
55
+ if options.include?(:recursive)
56
+ FileUtils.mkdir_p(real_path)
57
+ else
58
+ begin
59
+ Dir.mkdir(File.join(path, name))
60
+ rescue Errno::ENOENT
61
+ raise DataNotFoundError.new(path)
62
+ rescue Errno::EEXIST
63
+ raise DataAlreadyExistsError.new(path + [name])
64
+ end
65
+ end
66
+ end
67
+
68
+ def create(path, name, data, *options)
69
+ if options.include?(:create_dir)
70
+ FileUtils.mkdir_p(path_to(path))
71
+ end
72
+ begin
73
+ File.open(path_to(path, name), File::WRONLY | File::CREAT | File::EXCL | File::BINARY, internal_encoding: nil) do |file|
74
+ file.write data
75
+ end
76
+ rescue Errno::ENOENT
77
+ raise DataNotFoundError.new(path)
78
+ rescue Errno::EEXIST
79
+ raise DataAlreadyExistsError.new(path + [name])
80
+ end
81
+ end
82
+
83
+ def get(path, request = nil)
84
+ IO.read(path_to(path))
85
+ rescue Errno::ENOENT
86
+ raise DataNotFoundError.new(path)
87
+ end
88
+
89
+ def set(path, data, *options)
90
+ if options.include?(:create_dir)
91
+ FileUtils.mkdir_p(path_to(path[0..-2]))
92
+ end
93
+ begin
94
+ mode = File::WRONLY | File::TRUNC | File::BINARY
95
+ if options.include?(:create)
96
+ mode |= File::CREAT
97
+ end
98
+ File.open(path_to(path), mode, internal_encoding: nil) do |file|
99
+ file.write data
100
+ end
101
+ rescue Errno::ENOENT
102
+ raise DataNotFoundError.new(path)
103
+ end
104
+ end
105
+
106
+ def delete(path)
107
+ File.delete(path_to(path))
108
+ rescue Errno::ENOENT
109
+ raise DataNotFoundError.new(path)
110
+ end
111
+
112
+ def delete_dir(path, *options)
113
+ if options.include?(:recursive)
114
+ unless File.exist?(path_to(path))
115
+ raise DataNotFoundError.new(path)
116
+ end
117
+
118
+ FileUtils.rm_rf(path_to(path))
119
+ else
120
+ begin
121
+ Dir.rmdir(path_to(path))
122
+ rescue Errno::ENOENT
123
+ raise DataNotFoundError.new(path)
124
+ end
125
+ end
126
+ end
127
+
128
+ def list(path)
129
+ Dir.entries(path_to(path)).select { |entry| entry != "." && entry != ".." }.to_a
130
+ rescue Errno::ENOENT
131
+ raise DataNotFoundError.new(path)
132
+ end
133
+
134
+ def exists?(path, options = {})
135
+ File.exist?(path_to(path))
136
+ end
137
+
138
+ def exists_dir?(path)
139
+ File.exist?(path_to(path))
140
+ end
141
+ end
142
+ end
143
+ end