serverkit 0.4.9 → 0.5.0

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: 8b8c984c3c718fe947082f6f4d09ae7a22ff289e
4
- data.tar.gz: a3e1fdf09683e2a563be79a689558245de424532
3
+ metadata.gz: 8a305e729177502f64c869b2629dd30acfad21ae
4
+ data.tar.gz: 712936e178cf102fd9725046027f9cb7e96d7fca
5
5
  SHA512:
6
- metadata.gz: 9ec23d869a2e19605833366e7fb211f5aeb1d60342350e8173c5160a9055329e2f8437d1ea334902a9bef239ebf7cd02a3466543e688b37de4f32b42b796494a
7
- data.tar.gz: 2e8a2d2e52e428de9cab8071e3aa74a7be137bac4216559b3faff5156328f862cba039c295cda6d0e037a68f0aee0caf64895d0604414b4b9d1dca578c41a803
6
+ metadata.gz: c1618d46d882bec5251d762b954341f326fb94aaba997bff66e3358306b5da5392c7a4d9714bd45d6a4bd93bf7fbc3b255a132995822954b2cbb5e7d40f2a1d2
7
+ data.tar.gz: b27a2c2ee0062256bcfea32e61658e0a5a8eb912e01d30bb0573165f0ef69afdc76c58ffd7192a51c9c3d6617b2eb5fca1518ec964c3b35684fa9baf9c5c9d56
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.5.0
2
+ - Add validation_script attribute on line resource
3
+
1
4
  ## 0.4.9
2
5
  - Add insert_after, insert_before, and pattern attributes on line resource
3
6
  - Rescue YAML parse error on recipe loading phase
data/example/recipe.yml CHANGED
@@ -14,4 +14,8 @@ resources:
14
14
  mode: 0700
15
15
  - type: line
16
16
  path: /etc/sudoers
17
- line: "#includedir /etc/sudoers.d"
17
+ pattern: '%wheel +ALL=\(ALL\) ALL'
18
+ line: "%wheel ALL=(ALL) ALL"
19
+ insert_before: "^#includedir"
20
+ validation_script: visudo -cf %{path}
21
+ state: absent
@@ -0,0 +1,11 @@
1
+ require "active_model"
2
+
3
+ class RegexpValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ unless value.nil?
6
+ Regexp.new(value)
7
+ end
8
+ rescue RegexpError
9
+ record.errors.add(attribute, "is invalid regexp expression #{value.inspect}")
10
+ end
11
+ end
@@ -2,6 +2,7 @@ require "active_model"
2
2
  require "active_model/errors"
3
3
  require "active_support/core_ext/module/delegation"
4
4
  require "readable_validator"
5
+ require "regexp_validator"
5
6
  require "required_validator"
6
7
  require "serverkit/errors/attribute_validation_error"
7
8
  require "type_validator"
@@ -145,12 +146,40 @@ module Serverkit
145
146
  end
146
147
  end
147
148
 
149
+ # Create temp file on remote side with given content
150
+ # @param [String] content
151
+ # @return [String] Path to remote temp file
152
+ def create_remote_temp_file(content)
153
+ make_remote_temp_path.tap do |path|
154
+ update_remote_file_content(content: content, path: path)
155
+ end
156
+ end
157
+
148
158
  # @note For override
149
159
  # @return [String]
150
160
  def default_id
151
161
  required_values.join(" ")
152
162
  end
153
163
 
164
+ # @note GNU mktemp doesn't require -t option, but BSD mktemp does
165
+ # @return [String]
166
+ def make_remote_temp_path
167
+ run_command("mktemp -u 2>/dev/null || mktemp -u -t tmp").stdout.rstrip
168
+ end
169
+
170
+ # @note "metadata" meens a set of group, mode, and owner
171
+ # @param [String] destination
172
+ # @param [String] source
173
+ def move_remote_file_keeping_destination_metadata(source, destination)
174
+ group = run_command_from_identifier(:get_file_owner_group, destination).stdout.rstrip
175
+ mode = run_command_from_identifier(:get_file_mode, destination).stdout.rstrip
176
+ owner = run_command_from_identifier(:get_file_owner_user, destination).stdout.rstrip
177
+ run_command_from_identifier(:change_file_group, source, group) unless group.empty?
178
+ run_command_from_identifier(:change_file_mode, source, mode) unless mode.empty?
179
+ run_command_from_identifier(:change_file_owner, source, owner) unless owner.empty?
180
+ run_command_from_identifier(:move_file, source, destination)
181
+ end
182
+
154
183
  # @note For override
155
184
  # @return [true, false]
156
185
  def recheck
@@ -188,9 +217,9 @@ module Serverkit
188
217
  mode = run_command_from_identifier(:get_file_mode, path).stdout.rstrip
189
218
  owner = run_command_from_identifier(:get_file_owner_user, path).stdout.rstrip
190
219
  create_remote_file(content: content, path: path)
191
- run_command_from_identifier(:change_file_group, path, group)
192
- run_command_from_identifier(:change_file_mode, path, mode)
193
- run_command_from_identifier(:change_file_owner, path, owner)
220
+ run_command_from_identifier(:change_file_group, path, group) unless group.empty?
221
+ run_command_from_identifier(:change_file_mode, path, mode) unless mode.empty?
222
+ run_command_from_identifier(:change_file_owner, path, owner) unless owner.empty?
194
223
  end
195
224
  end
196
225
  end
@@ -14,16 +14,18 @@ module Serverkit
14
14
  attribute :insert_after, type: String
15
15
  attribute :insert_before, type: String
16
16
  attribute :line, required: true, type: String
17
- attribute :pattern, type: String
17
+ attribute :pattern, regexp: true, type: String
18
18
  attribute :state, default: DEFAULT_STATE, inclusion: %w[absent present], type: String
19
+ attribute :validation_script, type: String
19
20
 
20
21
  # @note Override
21
22
  def apply
22
23
  if has_correct_file?
23
- update_remote_file_content(
24
- content: applied_remote_file_content,
25
- path: path,
26
- )
24
+ if validation_script
25
+ update_remote_file_content_with_validation
26
+ else
27
+ update_remote_file_content_without_validation
28
+ end
27
29
  end
28
30
  end
29
31
 
@@ -89,6 +91,22 @@ module Serverkit
89
91
  state == "present"
90
92
  end
91
93
 
94
+ # Create a new temp file on remote side, then validate it with given script,
95
+ # and move it to right file path if it succeeded. Note that `%{path}` in validation
96
+ # script will be replaced with the path to temp file.
97
+ def update_remote_file_content_with_validation
98
+ temp_path = create_remote_temp_file(applied_remote_file_content)
99
+ if check_command(format(validation_script, path: temp_path))
100
+ move_remote_file_keeping_destination_metadata(temp_path, path)
101
+ end
102
+ run_command_from_identifier(:remove_file, temp_path)
103
+ end
104
+
105
+ # Create or update remote file
106
+ def update_remote_file_content_without_validation
107
+ update_remote_file_content(content: applied_remote_file_content, path: path)
108
+ end
109
+
92
110
  # Wrapper class to easily manage lines in remote file content.
93
111
  class Content
94
112
  # @param [String] raw
@@ -105,7 +123,7 @@ module Serverkit
105
123
  # @param [String] line
106
124
  # @return [Serverkit::Resources::Line::Content]
107
125
  def delete(line)
108
- self.class.new(@raw.gsub(/^#{line}$/, ""))
126
+ self.class.new(@raw.gsub(/^#{Regexp.escape(line)}[\n$]/, ""))
109
127
  end
110
128
 
111
129
  # Insert the line after the last matched line or EOF
@@ -145,13 +163,6 @@ module Serverkit
145
163
 
146
164
  private
147
165
 
148
- # @return [Array<String>]
149
- def lines
150
- @lines ||= @raw.each_line.map do |line|
151
- line.gsub(/\n$/, "")
152
- end
153
- end
154
-
155
166
  # @param [Integer] index
156
167
  # @param [String] line
157
168
  # @return [Serverkit::Resources::Line::Content]
@@ -159,6 +170,13 @@ module Serverkit
159
170
  self.class.new([*lines.dup.insert(index, line), ""].join("\n"))
160
171
  end
161
172
 
173
+ # @return [Array<String>]
174
+ def lines
175
+ @lines ||= @raw.each_line.map do |line|
176
+ line.gsub(/\n$/, "")
177
+ end
178
+ end
179
+
162
180
  # @param [String] line
163
181
  # @return [Serverkit::Resources::Line::Content]
164
182
  def prepend(line)
@@ -1,3 +1,3 @@
1
1
  module Serverkit
2
- VERSION = "0.4.9"
2
+ VERSION = "0.5.0"
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.9
4
+ version: 0.5.0
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-22 00:00:00.000000000 Z
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -215,6 +215,7 @@ files:
215
215
  - example/template.erb
216
216
  - example/variables.yml
217
217
  - lib/readable_validator.rb
218
+ - lib/regexp_validator.rb
218
219
  - lib/required_validator.rb
219
220
  - lib/serverkit.rb
220
221
  - lib/serverkit/actions/apply.rb