serverkit 0.4.8 → 0.4.9

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: 8940c786379f55b05d9416d30a0b80488db1bbc4
4
- data.tar.gz: c59b253fec99e0a5b4bdccfe30fa205fe5d25c50
3
+ metadata.gz: 8b8c984c3c718fe947082f6f4d09ae7a22ff289e
4
+ data.tar.gz: a3e1fdf09683e2a563be79a689558245de424532
5
5
  SHA512:
6
- metadata.gz: 7121acccc709d1e5dff3d4d69bcfb43eb405f1e4408548e4f73a3b28907b963ec1d5659076891c3f8687fc516570952aa9d7c8f2ac7d5ad9e72ff8de65736144
7
- data.tar.gz: b3a2456a7c2b784330900408b0667da7a048e832d89dd2b422727d1b54aaa4671989fa10a34eb2e43a3f2bd353c49accbc2043e36e42c742d02055c59be6580b
6
+ metadata.gz: 9ec23d869a2e19605833366e7fb211f5aeb1d60342350e8173c5160a9055329e2f8437d1ea334902a9bef239ebf7cd02a3466543e688b37de4f32b42b796494a
7
+ data.tar.gz: 2e8a2d2e52e428de9cab8071e3aa74a7be137bac4216559b3faff5156328f862cba039c295cda6d0e037a68f0aee0caf64895d0604414b4b9d1dca578c41a803
data/.rubocop.yml CHANGED
@@ -1,6 +1,12 @@
1
+ Lint/AssignmentInCondition:
2
+ Enabled: false
3
+
1
4
  Lint/HandleExceptions:
2
5
  Enabled: false
3
6
 
7
+ Lint/ShadowingOuterLocalVariable:
8
+ Enabled: false
9
+
4
10
  Lint/UnderscorePrefixedVariableName:
5
11
  Enabled: false
6
12
 
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.4.9
2
+ - Add insert_after, insert_before, and pattern attributes on line resource
3
+ - Rescue YAML parse error on recipe loading phase
4
+
1
5
  ## 0.4.8
2
6
  - Add line resource
3
7
 
@@ -46,7 +46,7 @@ module Serverkit
46
46
  else
47
47
  raise Errors::UnknownActionNameError, action_name
48
48
  end
49
- rescue Errors::Base, Slop::MissingArgumentError, Slop::MissingOptionError => exception
49
+ rescue Errors::Base, Psych::SyntaxError, Slop::MissingArgumentError, Slop::MissingOptionError => exception
50
50
  abort "Error: #{exception}"
51
51
  end
52
52
 
@@ -134,6 +134,17 @@ module Serverkit
134
134
  end
135
135
  end
136
136
 
137
+ # Create a file on remote side
138
+ # @param [String] content
139
+ # @param [String] path
140
+ def create_remote_file(content: nil, path: nil)
141
+ ::Tempfile.open("") do |file|
142
+ file.write(content)
143
+ file.close
144
+ backend.send_file(file.path, path)
145
+ end
146
+ end
147
+
137
148
  # @note For override
138
149
  # @return [String]
139
150
  def default_id
@@ -168,6 +179,19 @@ module Serverkit
168
179
  send(required_attribute_name)
169
180
  end
170
181
  end
182
+
183
+ # Update remote file content on given path with given content
184
+ # @param [String] content
185
+ # @param [String] path
186
+ def update_remote_file_content(content: nil, path: nil)
187
+ group = run_command_from_identifier(:get_file_owner_group, path).stdout.rstrip
188
+ mode = run_command_from_identifier(:get_file_mode, path).stdout.rstrip
189
+ owner = run_command_from_identifier(:get_file_owner_user, path).stdout.rstrip
190
+ 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)
194
+ end
171
195
  end
172
196
  end
173
197
  end
@@ -11,13 +11,19 @@ module Serverkit
11
11
  DEFAULT_STATE = "present"
12
12
 
13
13
  attribute :path, required: true, type: String
14
+ attribute :insert_after, type: String
15
+ attribute :insert_before, type: String
14
16
  attribute :line, required: true, type: String
17
+ attribute :pattern, type: String
15
18
  attribute :state, default: DEFAULT_STATE, inclusion: %w[absent present], type: String
16
19
 
17
20
  # @note Override
18
21
  def apply
19
22
  if has_correct_file?
20
- send_applied_remote_file_content_to_path
23
+ update_remote_file_content(
24
+ content: applied_remote_file_content,
25
+ path: path,
26
+ )
21
27
  end
22
28
  end
23
29
 
@@ -28,41 +34,135 @@ module Serverkit
28
34
 
29
35
  private
30
36
 
37
+ def absent?
38
+ state == "absent"
39
+ end
40
+
31
41
  # @return [String]
32
42
  def applied_remote_file_content
33
- if present?
34
- remote_file_content << line_with_break
43
+ case
44
+ when absent?
45
+ content.delete(line).to_s
46
+ when insert_after
47
+ content.insert_after(Regexp.new(insert_after), line).to_s
48
+ when insert_before
49
+ content.insert_before(Regexp.new(insert_before), line).to_s
35
50
  else
36
- (remote_file_content.each_line.to_a - [line_with_break]).join
51
+ content.append(line).to_s
37
52
  end
38
53
  end
39
54
 
55
+ # @return [Serverkit::Resources::Line::Content]
56
+ def content
57
+ Content.new(get_remote_file_content)
58
+ end
59
+
60
+ # @return [String]
61
+ def get_remote_file_content
62
+ run_command_from_identifier(:get_file_content, path).stdout
63
+ end
64
+
40
65
  def has_correct_file?
41
66
  check_command_from_identifier(:check_file_is_file, path)
42
67
  end
43
68
 
44
69
  def has_correct_line?
45
- !(present? ^ remote_file_content.each_line.include?(line_with_break))
70
+ case
71
+ when present? && !has_matched_line?
72
+ false
73
+ when !present? && has_matched_line?
74
+ false
75
+ else
76
+ true
77
+ end
46
78
  end
47
79
 
48
- def line_with_break
49
- "#{line}\n"
80
+ def has_matched_line?
81
+ if pattern
82
+ content.match(Regexp.new(pattern))
83
+ else
84
+ content.match(line)
85
+ end
50
86
  end
51
87
 
52
88
  def present?
53
89
  state == "present"
54
90
  end
55
91
 
56
- # @return [String]
57
- def remote_file_content
58
- run_command_from_identifier(:get_file_content, path).stdout
59
- end
92
+ # Wrapper class to easily manage lines in remote file content.
93
+ class Content
94
+ # @param [String] raw
95
+ def initialize(raw)
96
+ @raw = raw
97
+ end
98
+
99
+ # @param [String] line
100
+ # @return [Serverkit::Resources::Line::Content]
101
+ def append(line)
102
+ self.class.new([*lines, line, ""].join("\n"))
103
+ end
104
+
105
+ # @param [String] line
106
+ # @return [Serverkit::Resources::Line::Content]
107
+ def delete(line)
108
+ self.class.new(@raw.gsub(/^#{line}$/, ""))
109
+ end
110
+
111
+ # Insert the line after the last matched line or EOF
112
+ # @param [Regexp] regexp
113
+ # @param [String] line
114
+ # @return [Serverkit::Resources::Line::Content]
115
+ def insert_after(regexp, line)
116
+ if index = lines.rindex { |line| line =~ regexp }
117
+ insert(index + 1, line)
118
+ else
119
+ append(line)
120
+ end
121
+ end
122
+
123
+ # Insert the line before the last matched line or BOF
124
+ # @param [Regexp] regexp
125
+ # @param [String] line
126
+ # @return [Serverkit::Resources::Line::Content]
127
+ def insert_before(regexp, line)
128
+ if index = lines.rindex { |line| line =~ regexp }
129
+ insert(index, line)
130
+ else
131
+ prepend(line)
132
+ end
133
+ end
134
+
135
+ # @param [Regexp, String] pattern
136
+ # @return [false, true] True if any line matches given pattern
137
+ def match(pattern)
138
+ lines.lazy.grep(pattern).any?
139
+ end
140
+
141
+ # @note Override
142
+ def to_s
143
+ @raw.dup
144
+ end
145
+
146
+ private
147
+
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
+ # @param [Integer] index
156
+ # @param [String] line
157
+ # @return [Serverkit::Resources::Line::Content]
158
+ def insert(index, line)
159
+ self.class.new([*lines.dup.insert(index, line), ""].join("\n"))
160
+ end
60
161
 
61
- def send_applied_remote_file_content_to_path
62
- ::Tempfile.open("") do |file|
63
- file.write(applied_remote_file_content)
64
- file.close
65
- backend.send_file(file.path, path)
162
+ # @param [String] line
163
+ # @return [Serverkit::Resources::Line::Content]
164
+ def prepend(line)
165
+ self.class.new("#{line}\n#{@raw}")
66
166
  end
67
167
  end
68
168
  end
@@ -1,3 +1,3 @@
1
1
  module Serverkit
2
- VERSION = "0.4.8"
2
+ VERSION = "0.4.9"
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.8
4
+ version: 0.4.9
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-21 00:00:00.000000000 Z
11
+ date: 2015-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel