systemdy 0.3.1 → 0.4.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
  SHA256:
3
- metadata.gz: 0d100ae2959d85fb8084a5d6c23113dd70a417f3f5512b8ef9664f7e49ec4b70
4
- data.tar.gz: 8d7e95b1a81f21778a56ba809b621cce17d73c509222da63939744645595eba6
3
+ metadata.gz: 1544343d2a581add0354d499edbd139097f169be6dfc17e5ef4c310af3f27121
4
+ data.tar.gz: b6e96d3e254f09b8f3f53b7e3ead30f6d4290eb7a28832743552cd0dbd98dbe7
5
5
  SHA512:
6
- metadata.gz: 8d900cabf4fea970eade41af5121ae5c3b2bad2defb72486d88f64c5622b9cb7673e43fb027bd496f41c0c50def879dfe40dddf96b7da8732b872697b41bc5e4
7
- data.tar.gz: 0507f8e49ce30ff6affc084b52f6b4388bb136f2272d6c6d234b2d5191cad404eec16f356a19b7a5010d45a98922c0119d057cd8177d6859f30ad41ab12464c8
6
+ metadata.gz: ada7db214a2832e363ae2b0c4b9a64049adfd1e2a1652e490676da479c8889758343f7501d0b5f4de1733ce7cc17b3772a3bb4362ddac20eb8e6b1c3e4b2845d
7
+ data.tar.gz: 649e589fee12a0fdf97cf03c9a7ad5697e59191f08471bec85e2ad6e3e83fa41aafbc27e1b7590a37d5c89150c0ba489fc9b20c310570f4b9fbaf49697a2681f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ ## [0.4.0] - 2022-11-22
2
+
3
+ ### Added
4
+
5
+ - Systemdy::Utility::Formatter return_an_array_from class method for convert a complex string into array with custom splitter
6
+ - Systemdy::Service port method for return service port if available
7
+ - Systemdy::Service protocol method for return service protocol if available
8
+
9
+ ### Changed
10
+
11
+ - Logic for Systemd::Utility::Formatter return_an_array_from_system_command class method
12
+
13
+ ## [0.3.2] - 2022-10-21
14
+
15
+ ### Fixed
16
+
17
+ - documentation_uri in gemspec
18
+
19
+ ## [0.3.1] - 2022-10-21
20
+
21
+ ### Changed
22
+
23
+ - Documentation syntax for Systemdy::Utility::Formatter.remove_newline_from_system_command class method
24
+ - documentation_uri in gemspec
25
+
1
26
  ## [0.3.0] - 2022-10-21
2
27
 
3
28
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- systemdy (0.2.1)
4
+ systemdy (0.3.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -31,7 +31,9 @@ If your goal is to develop software to quickly manage systemd services or journa
31
31
  * [Check if the provided service is enabled](#check-if-the-provided-service-is-enabled)
32
32
  * [Check if the provided service is active](#check-if-the-provided-service-is-active)
33
33
  * [Check the current status of the provided service](#check-the-current-status-of-the-provided-service)
34
- * [Display all the properties for the provided service](#check-the-current-status-of-the-provided-service)
34
+ * [Display all the properties for the provided service](#display-all-the-properties-for-the-provided-service)
35
+ * [Get service port](#get-service-port)
36
+ * [Get service protocol](#get-service-protocol)
35
37
  * [Start the service](#start-the-service)
36
38
  * [Restart the service](#restart-the-service)
37
39
  * [Stop the service](#stop-the-service)
@@ -70,9 +72,11 @@ And then execute:
70
72
  Or install it yourself as:
71
73
 
72
74
  $ gem install systemdy
75
+
73
76
  ## Dependencies
74
77
 
75
78
  The only dependecy you need is [systemd](http://www.freedesktop.org/wiki/Software/systemdy/) installed on your system (specifically libsystemd or the older libsystemd-journal) in order to use the gem. Currently the gem support systemd 249 or higher.
79
+
76
80
  ## Usage
77
81
 
78
82
  After installing the gem, the first step is to require it:
@@ -80,6 +84,7 @@ After installing the gem, the first step is to require it:
80
84
  ```ruby
81
85
  require 'systemdy'
82
86
  ```
87
+
83
88
  ## Manage services
84
89
 
85
90
  The first goal of this gem is to manage a systemd's service with minimal effort.
@@ -148,9 +153,11 @@ Once executed, this method return an hash with all the essential service's infor
148
153
  "InactiveExitTimestamp"=>"Thu 2022-09-29 17:13:07 CEST"
149
154
  }
150
155
  ```
156
+
151
157
  ### Display all the properties for the provided service
152
158
 
153
159
  For check all the properties of the service
160
+
154
161
  ```ruby
155
162
  my_postgresql_service.properties
156
163
  ```
@@ -170,6 +177,44 @@ To extract the value of a specific property
170
177
  my_postgresql_service.properties["MemorySwapMax"]
171
178
  ```
172
179
 
180
+ ### Get service port
181
+
182
+ For retrieve a service port
183
+
184
+ ```ruby
185
+ my_postgresql_service.port
186
+ ```
187
+ Once executed, this method return the provided service port:
188
+
189
+ ```ruby
190
+ "5432"
191
+ ```
192
+
193
+ if the provided service has no available port, this method return a message like this:
194
+
195
+ ```ruby
196
+ "postgresql.service has no port available"
197
+ ```
198
+
199
+ ### Get service protocol
200
+
201
+ For retrieve a service protocol
202
+
203
+ ```ruby
204
+ my_postgresql_service.protocol
205
+ ```
206
+ Once executed, this method return the provided service port:
207
+
208
+ ```ruby
209
+ "tcp"
210
+ ```
211
+
212
+ if the provided service has no available protocol, this method return a message like this:
213
+
214
+ ```ruby
215
+ "postgresql.service has no protocol available"
216
+ ```
217
+
173
218
  ### Start the service
174
219
 
175
220
  For start the service
@@ -218,12 +263,14 @@ For mask the service
218
263
  ```ruby
219
264
  my_postgresql_service.mask
220
265
  ```
266
+
221
267
  ### Unmask the service
222
268
 
223
269
  For unmask the service
224
270
  ```ruby
225
271
  my_postgresql_service.unmask
226
272
  ```
273
+
227
274
  ## Bonus tip: execute systemctl commands without prompt the password
228
275
 
229
276
  The methods **start**, **restart**, **stop**, **enable**, **disable**, **reload**, **mask** and **unmask** detect non-root users and automatically execute the methods with sudo.
@@ -266,6 +313,7 @@ Type 'exit' on your terminal as follows:
266
313
  root@my-machine:~# exit
267
314
  ```
268
315
  Exit from root session and you are ready to go! :sunglasses:
316
+
269
317
  ## Manage Journalctl logs
270
318
 
271
319
  The second goal of this gem is to provide a set of useful methods for retrieving journalctl log data.
@@ -535,6 +583,7 @@ If the passed arguments not match anything the method return an array with a mes
535
583
  ```ruby
536
584
  ["-- No entries --"]
537
585
  ```
586
+
538
587
  ## Contributing
539
588
 
540
589
  We :heart: pull requests from everyone.
@@ -629,9 +678,9 @@ The gem is available as open source under the terms of the [MIT License](https:/
629
678
 
630
679
  ## Useful links and resources
631
680
 
632
- - An interesting article on [how to run sudo commands without password](https://www.linuxshelltips.com/run-sudo-commands-without-password/)
633
- - The official Systemd documentation for [time specification](https://www.freedesktop.org/software/systemd/man/systemd.time.html)
681
+ - An interesting article on [how to run sudo commands without password](https://www.linuxshelltips.com/run-sudo-commands-without-password/)
682
+ - The official Systemd documentation for [time specification](https://www.freedesktop.org/software/systemd/man/systemd.time.html)
634
683
 
635
684
  ## Acknowledgements
636
685
 
637
- - Thanks to [@colstrom](https://github.com/colstrom) and his [systemized](https://github.com/colstrom/systemized) for the great inspiration
686
+ - Thanks to [@colstrom](https://github.com/colstrom) and his [systemized](https://github.com/colstrom/systemized) for the great inspiration
@@ -7,17 +7,23 @@ module Systemdy
7
7
  # extend Forwardable standard's library module for delegate a specified method to a designated object
8
8
  extend Forwardable
9
9
 
10
+ # service look up information command
11
+ INFO_LOOKUP_COMMAND = "getent services"
12
+
13
+ # list of essential information on a provided service
14
+ LIST_OF_ESSENTIAL_INFO_LOOKUP = %w( port protocol )
15
+
10
16
  # list of supported actions on a provided service
11
- LIST_OF_ACTIONS = %w( start restart stop enable disable reload mask unmask )
17
+ LIST_OF_ACTIONS = %w( start restart stop enable disable reload mask unmask )
12
18
 
13
19
  # list of supported statuses on a provided service
14
- LIST_OF_STATUSES = %w( enabled active )
20
+ LIST_OF_STATUSES = %w( enabled active )
15
21
 
16
22
  # list of status properties on a provided service when status command is called
17
- LIST_OF_STATUS_PROPERTIES = %w( Id Description ExecMainPID LoadState ActiveState FragmentPath
18
- ActiveEnterTimestamp InactiveEnterTimestamp ActiveExitTimestamp
19
- InactiveExitTimestamp
20
- )
23
+ LIST_OF_STATUS_PROPERTIES = %w( Id Description ExecMainPID LoadState ActiveState FragmentPath
24
+ ActiveEnterTimestamp InactiveEnterTimestamp ActiveExitTimestamp
25
+ InactiveExitTimestamp
26
+ )
21
27
 
22
28
  attr_reader :command, :name
23
29
 
@@ -34,6 +40,8 @@ module Systemdy
34
40
 
35
41
  # delegate return_an_array_from_system_command method to Systemdy::Utility::Formatter class contained in Systemdy/utility/formatter.rb
36
42
  def_delegator Systemdy::Utility::Formatter, :return_an_array_from_system_command
43
+ # delegate return_an_array_from method to Systemdy::Utility::Formatter class contained in Systemdy/utility/formatter.rb
44
+ def_delegator Systemdy::Utility::Formatter, :return_an_array_from
37
45
  # delegate remove_newline_from_system_command to Systemdy::Utility::Formatter class contained in Systemdy/utility/formatter.rb
38
46
  def_delegator Systemdy::Utility::Formatter, :remove_newline_from_system_command
39
47
  # delegate render_message method to Systemdy::Utility::MessageDisplayer class contained in Systemdy/utility/message_displayer.rb
@@ -51,6 +59,30 @@ module Systemdy
51
59
  def exist?
52
60
  check_if_a_service_exist(name) # class method contained in Systemdy/utility/validator.rb
53
61
  end
62
+
63
+ # create dynamically methods based on LIST_OF_ESSENTIAL_INFO_LOOKUP constant
64
+ # @!method port
65
+ # return the available +port+ of the service
66
+ # @example get the service port
67
+ # my_postgresql_service.port #=> "5432"
68
+ # @note This method is generated with use of metaprogramming techniques
69
+ # @todo This method return an error message when there are no port available
70
+ #
71
+ # @!method protocol
72
+ # return the available +protocol+ of the service
73
+ # @example restart a service
74
+ # my_postgresql_service.protocol #=> "tcp"
75
+ # @note This method is generated with use of metaprogramming techniques
76
+ # @todo This method return an error message when there are no protocol available
77
+ #
78
+ LIST_OF_ESSENTIAL_INFO_LOOKUP.each_with_index do |info, index|
79
+ define_method info do
80
+ return default_error_message() unless exist?
81
+ essential_info = return_an_array_from(`#{INFO_LOOKUP_COMMAND} #{name}`, argument_splitter: ' ')
82
+ return info_lookup_error_message(info) if essential_info.nil?
83
+ return_an_array_from(essential_info[1], argument_splitter: '/')[index]
84
+ end
85
+ end
54
86
 
55
87
  # create dynamically methods based on LIST_OF_ACTIONS constant
56
88
  # @!method start
@@ -182,11 +214,25 @@ module Systemdy
182
214
  render_message("Unit #{name}.service could not be found.") # class method contained in Systemdy/utility/message_displayer.rb
183
215
  end
184
216
 
217
+ # method to show a message when a service has no port or protocols available
218
+ # @return [String] the info lookup error message
219
+ # @example call port method on a service that has no port available
220
+ # a_service_that_has_no_port_available #=> "a_service_that_has_no_port_available.service has no port available"
221
+ def info_lookup_error_message(info)
222
+ render_message("#{name}.service has no #{info} available") # class method contained in Systemdy/utility/message_displayer.rb
223
+ end
224
+
185
225
  # @!method return_an_array_from_system_command
186
226
  # @param system_call [String] system call to convert to an array
187
227
  # @return [Array] an array-based list of the values ​​returned by making a system call
188
228
  # @note check out more about this method in Systemdy/utility/formatter.rb
189
229
  #
230
+ # @!method return_an_array_from
231
+ # @param string_to_parse [String] string to convert to an array
232
+ # @param argument_splitter [String] character for split string to an array
233
+ # @param remove_blank_elements [Boolean] remove blank elements or not
234
+ # @return [Array] an array-based list of the values ​​returned by argument_splitter
235
+ #
190
236
  # @!method filter_by_keys
191
237
  # @param hash [Hash] the hash to filter
192
238
  # @param list_of_keys [Array] the list of keys to extract from the hash
@@ -195,6 +241,6 @@ module Systemdy
195
241
  #
196
242
 
197
243
  # make the methods below as private
198
- private :render_message, :default_error_message, :return_an_array_from_system_command, :filter_by_keys
244
+ private :render_message, :default_error_message, :info_lookup_error_message, :return_an_array_from_system_command, :return_an_array_from, :filter_by_keys
199
245
  end
200
246
  end
@@ -13,7 +13,21 @@ module Systemdy
13
13
  def self.return_an_array_from_system_command(system_call)
14
14
  system_call_without_new_line = remove_newline_from_system_command(system_call) # remove \n from system call returned value
15
15
  return system_call_without_new_line if !$?.success? # return system error message if the process has non-zero exit status
16
- system_call_without_new_line.split(/\n/) # convert values returned by `` system call to an array-based list
16
+ return_an_array_from(system_call_without_new_line) # convert values returned by `` system call to an array-based list based on argument_splitter value
17
+ end
18
+
19
+ # method for convert a string into an array without blank elements
20
+ #
21
+ # @param string_to_parse [String] string to convert to an array
22
+ # @param argument_splitter [String] character for split string to an array
23
+ # @param remove_blank_elements [Boolean] remove blank elements or not
24
+ # @return [Array] an array-based list of the values ​​returned by argument_splitter
25
+ # @example convert a complex string into an array
26
+ # list_of_info = Systemdy::Utility::Formatter.return_an_array_from("5432/tcp info", argument_splitter: '/', remove_blank_elements: false) #=> ["5432", "tcp", "", "", "info"]
27
+ # @example convert a complex string into an array without blank elements
28
+ # list_of_info_without_blank_elements = Systemdy::Utility::Formatter.return_an_array_from("5432/tcp info", argument_splitter: '/') #=> ["5432", "tcp", "info"]
29
+ def self.return_an_array_from(string_to_parse, argument_splitter: '\n', remove_blank_elements: true)
30
+ remove_blank_elements ? string_to_parse.split(/#{argument_splitter}/).reject(&:empty?) : string_to_parse.split(/#{argument_splitter}/)
17
31
  end
18
32
 
19
33
  # method for remove +\n+ characters from system calls
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Systemdy
4
4
  # the current version of the gem
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: systemdy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - magic4dev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-21 00:00:00.000000000 Z
11
+ date: 2022-11-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: magic4dev@gmail.com
@@ -40,7 +40,7 @@ licenses:
40
40
  metadata:
41
41
  homepage_uri: https://github.com/magic4dev/systemdy
42
42
  source_code_uri: https://github.com/magic4dev/systemdy
43
- documentation_uri: https://www.rubydoc.info/gems/systemdy
43
+ documentation_uri: https://www.rubydoc.info/github/magic4dev/systemdy
44
44
  changelog_uri: https://github.com/magic4dev/systemdy/blob/master/CHANGELOG.md
45
45
  code_of_conduct_uri: https://github.com/magic4dev/systemdy/blob/master/CODE_OF_CONDUCT.md
46
46
  post_install_message: