systemd_service_check 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14d00810a361024a249ae6211e3805e1383badf662431804174abfc4b86e842e
4
- data.tar.gz: d68c217324a1a19a507e97cfa1c790f3e2ad18981d2496dba23a4be437220f30
3
+ metadata.gz: bcc52f7b0040ae6d343dc5f258d468e2b9a105352d44a73406f2615d3325eff3
4
+ data.tar.gz: a1e5b4b490e994a11f03707f5ebda9bf17cb5895f49c9d9808e5b9358168c6a8
5
5
  SHA512:
6
- metadata.gz: 5c0f87e7ee79969bad99bf4f77f8be010217b0e75c9fc6b7a93541eaec69b6ed1bd5de78d9ebfc0c248add2f421151730fee9519c83b15869679733cef730995
7
- data.tar.gz: c215f24bb497793765217e66e58df8c711d7218a40584ebce380cf0983f43289b3b36a5cb75b2b44d893feefc906a9cfedd576d295dee4c468f405e0dcea6fd4
6
+ metadata.gz: f576d59a803b1a3875f83dd018752dab4911b2dec4ba04a470374338e8f42513de9a06171bd990605c30a0bbde791c9705bafe6fad1fa2bf4773f46cb66ff14b
7
+ data.tar.gz: bece1d59dc14f99ab8ce9c1fada8e7a9b2daf605b35816a5d8af264c60c5d595e474a6c988fa259f2f79fd953ba34c74e01ec25854d46f5434bb31526941ffa8
@@ -3,6 +3,7 @@ inherit_from: .rubocop_todo.yml
3
3
  AllCops:
4
4
  TargetRubyVersion: 2.5
5
5
  Exclude:
6
+ - systemd_service_check.gemspec
6
7
  - Vagrantfile
7
8
  - vendor/**/*
8
9
  - lib/table_print-patch-pr70/*
@@ -2,4 +2,7 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.5.0
5
+ - 2.4.3
6
+ - 2.3.6
7
+ cache: bundler
5
8
  before_install: gem install bundler -v 1.16.1
@@ -4,9 +4,17 @@ Please confirm details from the following.
4
4
 
5
5
  https://github.com/k-ta-yamada/systemd_service_check/releases
6
6
 
7
+ ## v1.1.0
8
+
9
+ 2018-02-08 +0900
10
+
11
+ - issues: https://github.com/k-ta-yamada/systemd_service_check/milestone/2?closed=1
12
+ - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v1.1.0
13
+ - commits: https://github.com/k-ta-yamada/systemd_service_check/compare/v1.0.0...v1.1.0
14
+
7
15
  ## v1.0.0
8
16
 
9
- 2018-01-28 JST
17
+ 2018-01-28 +0900
10
18
 
11
19
  - issues: https://github.com/k-ta-yamada/systemd_service_check/milestone/1?closed=1
12
20
  - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v1.0.0
@@ -14,14 +22,14 @@ https://github.com/k-ta-yamada/systemd_service_check/releases
14
22
 
15
23
  ## v0.0.0
16
24
 
17
- 2018-01-25 JST
25
+ 2018-01-25 +0900
18
26
 
19
27
  - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v0.0.0
20
28
  - commits: https://github.com/k-ta-yamada/systemd_service_check/compare/v0.0.0-alpha...v0.0.0
21
29
 
22
30
  ## v0.0.0-alpha
23
31
 
24
- 2018-01-23 JST
32
+ 2018-01-23 +0900
25
33
 
26
34
  - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v0.0.0-alpha
27
35
  - commits: https://github.com/k-ta-yamada/systemd_service_check/compare/4e4adbd...v0.0.0-alpha
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/systemd_service_check.svg)](https://badge.fury.io/rb/systemd_service_check)
4
4
  [![Build Status](https://travis-ci.org/k-ta-yamada/systemd_service_check.svg?branch=master)](https://travis-ci.org/k-ta-yamada/systemd_service_check)
5
+ [![Inline docs](http://inch-ci.org/github/k-ta-yamada/systemd_service_check.svg?branch=master&style=shields)](http://inch-ci.org/github/k-ta-yamada/systemd_service_check)
5
6
 
6
7
  This gem provide `ssc` command to check multiple `systemd service`
7
8
  etc on multiple remote servers using net-ssh.
@@ -41,7 +42,10 @@ usage as CLI: `ssc check -y ./path/to/setting.yml`
41
42
  ```rb
42
43
  require 'systemd_service_check'
43
44
 
44
- ssc = SystemdServiceCheck::Base.new
45
+ envs = ['dev', 'stg']
46
+ yaml = './your_settings.yml'
47
+ role = 'app'
48
+ ssc = SystemdServiceCheck::Base.new(envs, yaml, role)
45
49
 
46
50
  puts ssc.to_json
47
51
  ```
@@ -51,43 +55,100 @@ puts ssc.to_json
51
55
  ```sh
52
56
  $ ssc help
53
57
  Commands:
54
- ssc check ENV [ENV...] options # check target ENV Servers.
55
- ssc help [COMMAND] # Describe available commands or one specific command
56
- ssc version # return SystemdServiceCheck::VERSION
58
+ ssc check ENV [ENV...] [options] # check target ENV Servers.
59
+ ssc help [COMMAND] # Describe available commands or one specific command
60
+ ssc version # return SystemdServiceCheck::VERSION
57
61
  ```
58
62
 
59
63
  ```sh
60
64
  $ ssc help check
61
65
  Usage:
62
- ssc check ENV [ENV...] options
66
+ ssc check [ENV [ENV...]] [options]
63
67
 
64
68
  Options:
65
- -f, [--format=table] # [t]able, [j]son, [a]wesome_print
66
- # Default: table
67
- -y, [--yaml=./systemd_service_check.sample.yml] # setting yaml file
68
- # Default: ./systemd_service_check.sample.yml
69
+ -y, [--yaml=YAML] # setting yaml file
70
+ # Default: ./systemd_service_check.sample.yml
71
+ -r, [--role=ROLE] # Target the specified role
72
+ -f, [--format=FORMAT] # [t]able, [j]son, [a]wesome_print
73
+ # Default: table
69
74
 
70
75
  check target ENV Servers.
76
+ If `ENV` is omitted, the value of the first env of servers described in yaml is used.
77
+ If `all` is specified for `ENV`, all ENVs are targeted.
71
78
  ```
72
79
 
73
80
  ```sh
74
81
  $ ssc check
75
- ENV | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
76
- ----|---------------|----------|------|--------------------------------|------------|--------------|-----------|-----------------|--------
77
- dev | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
78
- dev | 192.168.1.101 | centos7 | root | firewalld.service | loaded | inactive | dead | disabled | dbus
79
- dev | 192.168.1.101 | centos7 | root | rsyslog.service | loaded | active | running | enabled | notify
80
- dev | 192.168.1.101 | centos7 | root | network.service | loaded | active | exited | bad | forking
81
- dev | 192.168.1.101 | centos7 | root | systemd-tmpfiles-clean.timer | loaded | active | waiting | static | n/a
82
- dev | 192.168.1.101 | centos7 | root | dummy_long_name_size_30_______ | not-found | inactive | dead | n/a | n/a
82
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
83
+ ----|------|---------------|----------|------|--------------------------------|------------|--------------|-----------|-----------------|--------
84
+ dev | | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
85
+ dev | | 192.168.1.101 | centos7 | root | firewalld.service | loaded | inactive | dead | disabled | dbus
86
+ dev | | 192.168.1.101 | centos7 | root | rsyslog.service | loaded | active | running | enabled | notify
87
+ dev | | 192.168.1.101 | centos7 | root | network.service | loaded | active | exited | bad | forking
88
+ dev | | 192.168.1.101 | centos7 | root | systemd-tmpfiles-clean.timer | loaded | active | waiting | static |
89
+ dev | | 192.168.1.101 | centos7 | root | dummy_long_name_size_30_______ | not-found | inactive | dead | |
90
+
91
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
92
+ ----|------|---------------|----------|------|---------------|------------|--------------|-----------|-----------------|-------
93
+ dev | ap | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
94
+ dev | ap | 192.168.1.101 | centos7 | root | nginx.service | not-found | inactive | dead | |
95
+
96
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
97
+ ----|------|---------------|----------|------|--------------------|------------|--------------|-----------|-----------------|-------
98
+ dev | db | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
99
+ dev | db | 192.168.1.101 | centos7 | root | postgresql.service | not-found | inactive | dead | |
100
+ dev | db | 192.168.1.101 | centos7 | root | pgpool.service | not-found | inactive | dead | |
101
+ ```
102
+
103
+ #### role
104
+
105
+ ```sh
106
+ $ ssc check --role ap
107
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
108
+ ----|------|---------------|----------|------|---------------|------------|--------------|-----------|-----------------|-------
109
+ dev | ap | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
110
+ dev | ap | 192.168.1.101 | centos7 | root | nginx.service | not-found | inactive | dead | |
111
+ ```
112
+
113
+ ```sh
114
+ $ ssc check --role db
115
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
116
+ ----|------|---------------|----------|------|--------------------|------------|--------------|-----------|-----------------|-------
117
+ dev | db | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
118
+ dev | db | 192.168.1.101 | centos7 | root | postgresql.service | not-found | inactive | dead | |
119
+ dev | db | 192.168.1.101 | centos7 | root | pgpool.service | not-found | inactive | dead | |
83
120
  ```
84
121
 
122
+ ```sh
123
+ $ ssc check all --role db
124
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
125
+ ----|------|---------------|----------|------|--------------------|------------|--------------|-----------|-----------------|-------
126
+ dev | db | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
127
+ dev | db | 192.168.1.101 | centos7 | root | postgresql.service | not-found | inactive | dead | |
128
+ dev | db | 192.168.1.101 | centos7 | root | pgpool.service | not-found | inactive | dead | |
129
+
130
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
131
+ ----|------|---------------|----------|------|--------------------|------------|--------------|-----------|-----------------|-------
132
+ stg | db | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
133
+ stg | db | 192.168.1.101 | centos7 | root | postgresql.service | not-found | inactive | dead | |
134
+ stg | db | 192.168.1.101 | centos7 | root | pgpool.service | not-found | inactive | dead | |
135
+
136
+ ENV | ROLE | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
137
+ ----|------|---------------|----------|------|--------------------|------------|--------------|-----------|-----------------|-------
138
+ prd | db | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
139
+ prd | db | 192.168.1.101 | centos7 | root | postgresql.service | not-found | inactive | dead | |
140
+ prd | db | 192.168.1.101 | centos7 | root | pgpool.service | not-found | inactive | dead | |
141
+ ```
142
+
143
+ #### formatting sample
144
+
85
145
  ```sh
86
146
  $ ssc check --format=json | jq
87
147
  [
88
148
  {
89
149
  "server": {
90
150
  "env": "dev",
151
+ "role": null,
91
152
  "ip": "192.168.1.101",
92
153
  "user": "root",
93
154
  "options": {
@@ -95,16 +156,13 @@ $ ssc check --format=json | jq
95
156
  "keys": [
96
157
  ""
97
158
  ],
98
- "logger": "#<Logger:0x00007fa52293b0b0>",
99
- "password_prompt": "#<Net::SSH::Prompt:0x00007fa52293aa70>",
159
+ "logger": "#<Logger:0x00007fba5bb2d730>",
160
+ "password_prompt": "#<Net::SSH::Prompt:0x00007fba5bb2d5a0>",
100
161
  "user": "root"
101
162
  },
102
163
  "services": [
103
164
  "sshd.service",
104
- "firewalld.service",
105
- "rsyslog.service",
106
- "network.service",
107
- "systemd-tmpfiles-clean.timer",
165
+ ...,
108
166
  "dummy_long_name_size_30_______"
109
167
  ],
110
168
  "hostname": "centos7"
@@ -118,45 +176,57 @@ $ ssc check --format=json | jq
118
176
  "unit_file_state": "enabled",
119
177
  "type": "notify"
120
178
  },
179
+ ...,
121
180
  {
122
- "service_name": "firewalld.service",
123
- "load_state": "loaded",
181
+ "service_name": "dummy_long_name_size_30_______",
182
+ "load_state": "not-found",
124
183
  "active_state": "inactive",
125
184
  "sub_state": "dead",
126
- "unit_file_state": "disabled",
127
- "type": "dbus"
185
+ "unit_file_state": null,
186
+ "type": null
187
+ }
188
+ ]
189
+ },
190
+ ...,
191
+ {
192
+ "server": {
193
+ "env": "dev",
194
+ "role": "db",
195
+ "ip": "192.168.1.101",
196
+ "user": "root",
197
+ "options": {
198
+ "password": "vagrant",
199
+ "keys": [
200
+ ""
201
+ ],
202
+ "logger": "#<Logger:0x00007fba5bad2c90>",
203
+ "password_prompt": "#<Net::SSH::Prompt:0x00007fba5bb2d5a0>",
204
+ "user": "root"
128
205
  },
206
+ "services": [
207
+ "sshd.service",
208
+ "postgresql.service",
209
+ "pgpool.service"
210
+ ],
211
+ "hostname": "centos7"
212
+ },
213
+ "services": [
129
214
  {
130
- "service_name": "rsyslog.service",
215
+ "service_name": "sshd.service",
131
216
  "load_state": "loaded",
132
217
  "active_state": "active",
133
218
  "sub_state": "running",
134
219
  "unit_file_state": "enabled",
135
220
  "type": "notify"
136
221
  },
222
+ ...,
137
223
  {
138
- "service_name": "network.service",
139
- "load_state": "loaded",
140
- "active_state": "active",
141
- "sub_state": "exited",
142
- "unit_file_state": "bad",
143
- "type": "forking"
144
- },
145
- {
146
- "service_name": "systemd-tmpfiles-clean.timer",
147
- "load_state": "loaded",
148
- "active_state": "active",
149
- "sub_state": "waiting",
150
- "unit_file_state": "static",
151
- "type": "n/a"
152
- },
153
- {
154
- "service_name": "dummy_long_name_size_30_______",
224
+ "service_name": "pgpool.service",
155
225
  "load_state": "not-found",
156
226
  "active_state": "inactive",
157
227
  "sub_state": "dead",
158
- "unit_file_state": "n/a",
159
- "type": "n/a"
228
+ "unit_file_state": null,
229
+ "type": null
160
230
  }
161
231
  ]
162
232
  }
@@ -166,8 +236,8 @@ $ ssc check --format=json | jq
166
236
  ```sh
167
237
  $ ssc check --format=awesome_print
168
238
  [
169
- [0] #<Struct:SystemdServiceCheck::Base::Result:0x7fabeca9ad40
170
- server = #<Struct:SystemdServiceCheck::Base::Server:0x7fabec9c6ea0
239
+ [0] #<Struct:SystemdServiceCheck::SSH::Result:0x7fb39db78678
240
+ server = #<Struct:SystemdServiceCheck::Base::Server:0x7fb39dac6ec8
171
241
  env = "dev",
172
242
  hostname = "centos7",
173
243
  ip = "192.168.1.101",
@@ -176,22 +246,20 @@ $ ssc check --format=awesome_print
176
246
  :keys => [
177
247
  [0] ""
178
248
  ],
179
- :logger => #<Logger:0x00007fabec9c6478 @level=4, @progname=nil, @default_formatter=#<Logger::Formatter:0x00007fabec9c63d8 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00007fabec9c6360 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x00007fabec9c6248>>>,
180
- :password_prompt => #<Net::SSH::Prompt:0x00007fabec9c61d0>,
249
+ :logger => #<Logger:0x00007fb39dac5d98 @level=4, @progname=nil, @default_formatter=#<Logger::Formatter:0x00007fb39dac5cf8 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00007fb39dac5ca8 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x00007fb39dac5c30>>>,
250
+ :password_prompt => #<Net::SSH::Prompt:0x00007fb39dac5bb8>,
181
251
  :user => "root"
182
252
  },
253
+ role = nil,
183
254
  services = [
184
255
  [0] "sshd.service",
185
- [1] "firewalld.service",
186
- [2] "rsyslog.service",
187
- [3] "network.service",
188
- [4] "systemd-tmpfiles-clean.timer",
256
+ ...,
189
257
  [5] "dummy_long_name_size_30_______"
190
258
  ],
191
259
  user = "root"
192
260
  >,
193
261
  services = [
194
- [0] #<Struct:SystemdServiceCheck::Base::Service:0x7fabed259798
262
+ [0] #<Struct:SystemdServiceCheck::SSH::Service:0x7fb39da6bc08
195
263
  active_state = "active",
196
264
  load_state = "loaded",
197
265
  service_name = "sshd.service",
@@ -199,45 +267,57 @@ $ ssc check --format=awesome_print
199
267
  type = "notify",
200
268
  unit_file_state = "enabled"
201
269
  >,
202
- [1] #<Struct:SystemdServiceCheck::Base::Service:0x7fabecabaeb0
270
+ ...,
271
+ [5] #<Struct:SystemdServiceCheck::SSH::Service:0x7fb39db78bc8
203
272
  active_state = "inactive",
204
- load_state = "loaded",
205
- service_name = "firewalld.service",
273
+ load_state = "not-found",
274
+ service_name = "dummy_long_name_size_30_______",
206
275
  sub_state = "dead",
207
- type = "dbus",
208
- unit_file_state = "disabled"
209
- >,
210
- [2] #<Struct:SystemdServiceCheck::Base::Service:0x7fabecab7648
276
+ type = nil,
277
+ unit_file_state = nil
278
+ >
279
+ ]
280
+ >,
281
+ ...,
282
+ [2] #<Struct:SystemdServiceCheck::SSH::Result:0x7fb39e044cd8
283
+ server = #<Struct:SystemdServiceCheck::Base::Server:0x7fb39dac6888
284
+ env = "dev",
285
+ hostname = "centos7",
286
+ ip = "192.168.1.101",
287
+ options = {
288
+ :password => "vagrant",
289
+ :keys => [
290
+ [0] ""
291
+ ],
292
+ :logger => #<Logger:0x00007fb39e9c9858 @level=4, @progname=nil, @default_formatter=#<Logger::Formatter:0x00007fb39e9c9808 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00007fb39e9c9790 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x00007fb39e9c96f0>>>,
293
+ :password_prompt => #<Net::SSH::Prompt:0x00007fb39dac5bb8>,
294
+ :user => "root"
295
+ },
296
+ role = "db",
297
+ services = [
298
+ [0] "sshd.service",
299
+ [1] "postgresql.service",
300
+ [2] "pgpool.service"
301
+ ],
302
+ user = "root"
303
+ >,
304
+ services = [
305
+ [0] #<Struct:SystemdServiceCheck::SSH::Service:0x7fb39dbd5b98
211
306
  active_state = "active",
212
307
  load_state = "loaded",
213
- service_name = "rsyslog.service",
308
+ service_name = "sshd.service",
214
309
  sub_state = "running",
215
310
  type = "notify",
216
311
  unit_file_state = "enabled"
217
312
  >,
218
- [3] #<Struct:SystemdServiceCheck::Base::Service:0x7fabeca9e3f0
219
- active_state = "active",
220
- load_state = "loaded",
221
- service_name = "network.service",
222
- sub_state = "exited",
223
- type = "forking",
224
- unit_file_state = "bad"
225
- >,
226
- [4] #<Struct:SystemdServiceCheck::Base::Service:0x7fabed8c6db0
227
- active_state = "active",
228
- load_state = "loaded",
229
- service_name = "systemd-tmpfiles-clean.timer",
230
- sub_state = "waiting",
231
- type = "n/a",
232
- unit_file_state = "static"
233
- >,
234
- [5] #<Struct:SystemdServiceCheck::Base::Service:0x7fabeca9b1f0
313
+ ...,
314
+ [2] #<Struct:SystemdServiceCheck::SSH::Service:0x7fb39e045048
235
315
  active_state = "inactive",
236
316
  load_state = "not-found",
237
- service_name = "dummy_long_name_size_30_______",
317
+ service_name = "pgpool.service",
238
318
  sub_state = "dead",
239
- type = "n/a",
240
- unit_file_state = "n/a"
319
+ type = nil,
320
+ unit_file_state = nil
241
321
  >
242
322
  ]
243
323
  >
@@ -1,6 +1,52 @@
1
+ require 'net/ssh'
2
+ require 'thor'
3
+ require 'awesome_print'
4
+ require 'table_print'
5
+ require 'yaml'
6
+ require 'json'
7
+ require 'pry' # forDebug
8
+
1
9
  require "systemd_service_check/version"
2
- require "systemd_service_check/base"
10
+ require 'systemd_service_check/utils'
11
+ require 'systemd_service_check/ssh'
12
+
13
+ module SystemdServiceCheck
14
+ # Base
15
+ class Base
16
+ include Utils
17
+ include SSH
18
+
19
+ attr_reader :envs, :role, :servers, :target_envs, :target_servers, :results
20
+
21
+ # @param envs [Arrat<String>]
22
+ # @param yaml [String]
23
+ # @param role [String]
24
+ def initialize(envs, yaml, role = nil)
25
+ raise Utils::InvalidOptionError, "Argument `yaml` must not be blank." if blank? yaml
26
+
27
+ @envs = envs || []
28
+ @role = role.nil? || role.empty? ? nil : role
29
+ @servers = servers_from(yaml)
30
+ @target_envs = configure_target_envs(envs, @servers)
31
+ @target_servers = configure_target_servers(@servers, @target_envs, @role)
32
+ @results = []
33
+ end
34
+
35
+ def run
36
+ @results = @target_servers.map { |server| ssh(server) }
37
+ end
38
+
39
+ def to_json
40
+ @results.map do |result|
41
+ { server: result.server.to_h,
42
+ services: result.services.map(&:to_h) }
43
+ end.to_json
44
+ end
45
+
46
+ private
3
47
 
4
- # module SystemdServiceCheck
5
- # # # Your code goes here...
6
- # end
48
+ def blank?(yaml)
49
+ yaml.nil? || yaml.empty?
50
+ end
51
+ end
52
+ end
@@ -1,8 +1,4 @@
1
1
  require 'systemd_service_check'
2
- require 'thor'
3
- require 'awesome_print'
4
- require 'table_print'
5
- require 'pry' # forDebug
6
2
 
7
3
  # ###############################################################################
8
4
  # MEMO: This patch is a copy of https://github.com/arches/table_print/pull/70
@@ -13,33 +9,43 @@ require 'table_print-patch-pr70/formatter'
13
9
  module SystemdServiceCheck
14
10
  # CLI
15
11
  class CLI < Thor
16
- class InvalidFormatOption < StandardError; end
12
+ class InvalidFormatOptionError < StandardError; end
17
13
 
18
- option :format,
19
- type: :string,
20
- aliases: '-f',
21
- desc: "[t]able, [j]son, [a]wesome_print",
22
- banner: 'table',
23
- default: 'table'
24
14
  option :yaml,
25
15
  type: :string,
26
16
  aliases: '-y',
27
17
  desc: 'setting yaml file',
28
- banner: './systemd_service_check.sample.yml',
29
18
  default: './systemd_service_check.sample.yml'
19
+ option :role,
20
+ type: :string,
21
+ aliases: '-r',
22
+ desc: 'Target the specified role'
23
+ option :format,
24
+ type: :string,
25
+ aliases: '-f',
26
+ desc: '[t]able, [j]son, [a]wesome_print',
27
+ default: 'table'
30
28
 
31
- desc "check ENV [ENV...] options", "check target ENV Servers."
32
- def check(*env)
33
- raise InvalidFormatOption unless format_option_validate
34
- @ssc = Base.new(env, options[:yaml])
29
+ description = <<~DESCRIPTION
30
+ check target ENV Servers.
31
+ If `ENV` is omitted, the value of the first env of servers described in yaml is used.
32
+ If `all` is specified for `ENV`, all ENVs are targeted.
33
+ DESCRIPTION
34
+ desc 'check [ENV [ENV...]] [options]', description
35
+ def check(*envs) # rubocop:disable Metrics/AbcSize:
36
+ raise InvalidFormatOptionError unless format_option_validate
37
+
38
+ @ssc = Base.new(envs, options[:yaml], options[:role])
39
+ @ssc.run
35
40
  disp
36
- rescue InvalidFormatOption => e
41
+ rescue InvalidFormatOptionError => e
37
42
  puts "<#{e}>",
38
43
  " [#{options[:format]}] is invalid value."
39
44
  puts " #{e.backtrace_locations.first}"
40
45
  end
41
46
 
42
- desc "version", "return SystemdServiceCheck::VERSION"
47
+ desc 'version',
48
+ 'return SystemdServiceCheck::VERSION'
43
49
  def version
44
50
  puts VERSION
45
51
  end
@@ -66,8 +72,11 @@ module SystemdServiceCheck
66
72
  ap @ssc.results
67
73
  end
68
74
 
69
- COLS = %i[env ip hostname user service_name load_state active_state sub_state unit_file_state type].freeze
70
- def disp_table # rubocop:disable Metrics/AbcSize
75
+ # rubocop:disable Metrics/LineLength
76
+ COLS = %i[env role ip hostname user service_name].concat(Utils::PROPERTY_TO_SNAKE_SYM).freeze
77
+ # rubocop:enable Metrics/LineLength
78
+
79
+ def disp_table # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
71
80
  service_name_width =
72
81
  @ssc.results.map(&:services).flatten.map(&:service_name).map(&:size).max
73
82
 
@@ -80,10 +89,12 @@ module SystemdServiceCheck
80
89
  end
81
90
 
82
91
  # ref: https://github.com/erikhuda/thor/blob/master/lib/thor/shell.rb#L14
92
+ # rubocop:disable Style/GuardClause
83
93
  if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ && !ENV["ANSICON"]
84
94
  puts "-- For colors on windows, please setup `ANSICON`.",
85
95
  "-- ANSICON: https://github.com/adoxa/ansicon"
86
96
  end
97
+ # rubocop:enable Style/GuardClause
87
98
  end
88
99
 
89
100
  def decorate_ansi_color(services)
@@ -0,0 +1,44 @@
1
+ # SystemdServiceCheck
2
+ module SystemdServiceCheck
3
+ # SSH
4
+ module SSH
5
+ # @param server [Utils::Server]
6
+ # @return [Utils::Result]
7
+ def ssh(server)
8
+ services = []
9
+ Net::SSH.start(*server.conn_info) do |ssh|
10
+ server[:hostname] = hostname(ssh)
11
+ services =
12
+ server[:services].map { |service_name| systemctl_show(ssh, service_name) }
13
+ # TODO: store in Result object
14
+ # puts ssh.exec!("systemctl list-timers")
15
+ end
16
+ Utils::Result.new(server, services)
17
+ end
18
+
19
+ # @param ssh [Net::SSH::Connection::Session]
20
+ # @return [String]
21
+ def hostname(ssh)
22
+ ssh.exec!('hostname').chomp
23
+ end
24
+
25
+ # @param ssh [Net::SSH::Connection::Session]
26
+ # @param service_name [String]
27
+ # @return [Utils::Service]
28
+ def systemctl_show(ssh, service_name)
29
+ prop = ssh.exec!("systemctl show #{service_name} -p #{Utils::PROPERTY.join(",")}")
30
+ Utils::Service.new(service_name, *split_property(prop))
31
+ end
32
+
33
+ private
34
+
35
+ def split_property(property)
36
+ ret = property.split("\n")
37
+ .map { |v| v.split("=") }
38
+ .map { |(v1, v2)| [Thor::Util.snake_case(v1).to_sym, v2] }
39
+ .each_with_object({}) { |(k, v), memo| memo[k] = v }
40
+
41
+ Utils::PROPERTY_TO_SNAKE_SYM.map { |k| ret[k] }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,56 @@
1
+ module SystemdServiceCheck
2
+ # Included in: SystemdServiceCheck::Base
3
+ module Utils
4
+ # TODO: systemctl show -p
5
+ PROPERTY = %w[LoadState ActiveState SubState UnitFileState Type].freeze
6
+ PROPERTY_TO_SNAKE_SYM = PROPERTY.map { |v| Thor::Util.snake_case(v).to_sym }
7
+
8
+ class InvalidOptionError < StandardError; end
9
+
10
+ Server = Struct.new(:env, :role, :ip, :user, :options, :services, :hostname) do
11
+ # @return [Array<String, String, Hash>] for Net::SSH.start(*conn_info)
12
+ # @see SSH
13
+ # @see Net::SSH
14
+ def conn_info
15
+ [ip, user, options]
16
+ end
17
+ end
18
+ Service = Struct.new(:service_name, *PROPERTY_TO_SNAKE_SYM)
19
+ Result = Struct.new(:server, :services)
20
+
21
+ # @param yaml_filename [String] YAML filename with the description of the setting.
22
+ # @return [Array<Utils::Server>]
23
+ def servers_from(yaml_filename) # rubocop:disable Metrics/AbcSize
24
+ yaml = JSON.parse(YAML.load_file(yaml_filename).to_json, symbolize_names: true)
25
+
26
+ yaml[:servers].map do |s|
27
+ raise InvalidOptionError, "ENV: #{s[:env]}" if [s[:password], s[:key]].all?(&:nil?) # rubocop:disable Metrics/LineLength
28
+ options = { password: s[:password], keys: [s[:key] || ""] }
29
+ Server.new(s[:env], s[:role], s[:ip], s[:user], options, s[:services])
30
+ end
31
+ end
32
+
33
+ # @param argv [Array<String>] Array of ENVs specified with the `ssc check` command.
34
+ # @param servers [Array<Utils::Server>] Server settings.
35
+ # @return [Array<String>] Environment list to be processed.
36
+ def configure_target_envs(argv, servers)
37
+ all_envs = servers.map { |s| s[:env] }.uniq
38
+ target_envs = all_envs & argv
39
+
40
+ # If there is no argument.
41
+ target_envs = all_envs.first if target_envs.empty? && argv.empty?
42
+ # If there is only one argument and it is `all`.
43
+ target_envs = all_envs if argv.size == 1 && argv.first == 'all'
44
+
45
+ Array(target_envs)
46
+ end
47
+
48
+ # @param servers [Array<Utils::Server>]
49
+ # @param target_envs [Array<String>] Environment list to be processed.
50
+ # @return [Array<Utils::Server>]
51
+ def configure_target_servers(servers, target_envs, role)
52
+ servers.select { |s| target_envs.include?(s[:env]) }
53
+ .select { |s| role.nil? || role.empty? ? true : s[:role] == role }
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module SystemdServiceCheck
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "1.1.0".freeze
3
3
  end
@@ -9,12 +9,10 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["k-ta-yamada"]
10
10
  spec.email = ["key.luvless@gmail.com"]
11
11
 
12
- spec.summary =
13
- 'This gem provide `ssc` command to check multiple `systemd service` \
14
- etc on multiple remote servers using net-ssh.'
15
- spec.description =
16
- 'This gem provide `ssc` command to check multiple `systemd service` \
17
- etc on multiple remote servers using net-ssh.'
12
+ spec.required_ruby_version = '>= 2.3.0'
13
+
14
+ spec.summary = 'This gem provide `ssc` command to check multiple `systemd service` etc on multiple remote servers using net-ssh.'
15
+ spec.description = 'This gem provide `ssc` command to check multiple `systemd service` etc on multiple remote servers using net-ssh.'
18
16
  spec.homepage = "https://github.com/k-ta-yamada/systemd_service_check"
19
17
  spec.license = "MIT"
20
18
 
@@ -40,6 +38,8 @@ Gem::Specification.new do |spec|
40
38
  spec.add_development_dependency "minitest", "~> 5.0"
41
39
  spec.add_development_dependency "rake", "~> 10.0"
42
40
 
41
+ spec.add_development_dependency 'capture_stdout'
42
+ spec.add_development_dependency 'minitest-reporters'
43
43
  spec.add_development_dependency 'pry-doc'
44
44
  spec.add_development_dependency 'pry-theme'
45
45
  spec.add_development_dependency 'rubocop'
@@ -6,27 +6,63 @@ services: &default_services
6
6
  - network.service
7
7
  - systemd-tmpfiles-clean.timer
8
8
  - dummy_long_name_size_30_______
9
+ ap_services: &ap_services
10
+ - sshd.service
11
+ - nginx.service
12
+ db_services: &db_services
13
+ - sshd.service
14
+ - postgresql.service
15
+ - pgpool.service
9
16
 
10
17
  # SERVERS TO BE CHECKED [Array of Hash]
11
18
  servers:
12
- # only password
13
19
  - env: dev
20
+ # role: ap
14
21
  ip: 192.168.1.101
15
22
  user: root
23
+ # only password
16
24
  password: vagrant
17
25
  # key: ./.vagrant/machines/centos7/virtualbox/private_key
18
26
  services: *default_services
19
- # only private key
20
- - env: stg
27
+ - env: dev
28
+ role: ap # This value is specified by the `--role` option
21
29
  ip: 192.168.1.101
22
30
  user: root
31
+ # only private key
23
32
  # password: vagrant
24
33
  key: ./.vagrant/machines/centos7/virtualbox/private_key
25
- services: *default_services
26
- # password and private key (private key will be used preferentially)
27
- - env: prd
34
+ services: *ap_services
35
+ - env: dev
36
+ role: db
28
37
  ip: 192.168.1.101
29
38
  user: root
39
+ # password and private key (private key will be used preferentially)
30
40
  password: invalid-dummy-password
31
41
  key: ./.vagrant/machines/centos7/virtualbox/private_key
32
- services: *default_services
42
+ services: *db_services
43
+
44
+ - env: stg
45
+ role: ap
46
+ ip: 192.168.1.101
47
+ user: root
48
+ password: vagrant
49
+ services: *ap_services
50
+ - env: stg
51
+ role: db
52
+ ip: 192.168.1.101
53
+ user: root
54
+ password: vagrant
55
+ services: *db_services
56
+
57
+ - env: prd
58
+ role: ap
59
+ ip: 192.168.1.101
60
+ user: root
61
+ password: vagrant
62
+ services: *ap_services
63
+ - env: prd
64
+ role: db
65
+ ip: 192.168.1.101
66
+ user: root
67
+ password: vagrant
68
+ services: *db_services
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: systemd_service_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - k-ta-yamada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-28 00:00:00.000000000 Z
11
+ date: 2018-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: capture_stdout
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: pry-doc
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -164,9 +192,8 @@ dependencies:
164
192
  - - ">="
165
193
  - !ruby/object:Gem::Version
166
194
  version: '0'
167
- description: |-
168
- This gem provide `ssc` command to check multiple `systemd service` \
169
- etc on multiple remote servers using net-ssh.
195
+ description: This gem provide `ssc` command to check multiple `systemd service` etc
196
+ on multiple remote servers using net-ssh.
170
197
  email:
171
198
  - key.luvless@gmail.com
172
199
  executables:
@@ -178,6 +205,7 @@ files:
178
205
  - ".rubocop.yml"
179
206
  - ".rubocop_todo.yml"
180
207
  - ".travis.yml"
208
+ - CHANGELOG.md
181
209
  - CODE_OF_CONDUCT.md
182
210
  - Gemfile
183
211
  - LICENSE.txt
@@ -188,9 +216,9 @@ files:
188
216
  - bin/setup
189
217
  - bin/ssc
190
218
  - lib/systemd_service_check.rb
191
- - lib/systemd_service_check/CHANGELOG.md
192
- - lib/systemd_service_check/base.rb
193
219
  - lib/systemd_service_check/cli.rb
220
+ - lib/systemd_service_check/ssh.rb
221
+ - lib/systemd_service_check/utils.rb
194
222
  - lib/systemd_service_check/version.rb
195
223
  - lib/table_print-patch-pr70/column.rb
196
224
  - lib/table_print-patch-pr70/formatter.rb
@@ -208,7 +236,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
208
236
  requirements:
209
237
  - - ">="
210
238
  - !ruby/object:Gem::Version
211
- version: '0'
239
+ version: 2.3.0
212
240
  required_rubygems_version: !ruby/object:Gem::Requirement
213
241
  requirements:
214
242
  - - ">="
@@ -219,6 +247,6 @@ rubyforge_project:
219
247
  rubygems_version: 2.7.3
220
248
  signing_key:
221
249
  specification_version: 4
222
- summary: This gem provide `ssc` command to check multiple `systemd service` \ etc
223
- on multiple remote servers using net-ssh.
250
+ summary: This gem provide `ssc` command to check multiple `systemd service` etc on
251
+ multiple remote servers using net-ssh.
224
252
  test_files: []
@@ -1,110 +0,0 @@
1
- require 'net/ssh'
2
- require 'thor'
3
- require 'yaml'
4
- require 'json'
5
- require 'pry' # forDebug
6
-
7
- # SystemdServiceCheck
8
- module SystemdServiceCheck
9
- # Base
10
- class Base
11
- class InvalidOption < StandardError; end
12
-
13
- Server = Struct.new(:env, :ip, :user, :options, :services, :hostname)
14
- Service = Struct.new(:service_name, :load_state, :active_state, :sub_state, :unit_file_state, :type)
15
- Result = Struct.new(:server, :services)
16
-
17
- # TODO: systemctl show -p
18
- STATES = %w[LoadState ActiveState SubState UnitFileState Type].freeze
19
- # TODO: setting by yaml
20
- SHOW_GREP = /env/i
21
-
22
- attr_reader :argv, :env_names, :servers, :target_env, :target_servers, :results
23
-
24
- def initialize(argv, yaml)
25
- @argv = argv || []
26
- @env_names = []
27
- @servers = []
28
- @target_env = []
29
- @target_servers = []
30
- @results = []
31
-
32
- raise InvalidOption if yaml.nil? || yaml.empty?
33
- load_settings(yaml)
34
- configure_target_servers
35
- run
36
- rescue InvalidOption => e
37
- puts "<#{e}>",
38
- " Argument yaml must not be nil or empty.",
39
- " yaml.class: [#{yaml.class}]"
40
- puts " #{e.backtrace_locations.first}"
41
- end
42
-
43
- def run
44
- @results = @target_servers.map { |server| ssh(server) }
45
- end
46
-
47
- def to_json
48
- @results.map do |result|
49
- { server: result.server.to_h,
50
- services: result.services.map(&:to_h) }
51
- end.to_json
52
- end
53
-
54
- private
55
-
56
- def load_settings(file) # rubocop:disable Metrics/AbcSize
57
- yaml = JSON.parse(YAML.load_file(File.expand_path(file)).to_json,
58
- symbolize_names: true)
59
- @env_names = yaml[:servers].map { |s| s[:env] }.uniq
60
- @servers = yaml[:servers].map do |s|
61
- raise InvalidOption, "ENV: #{s[:env]}" if [s[:password], s[:key]].all?(nil)
62
- options = { password: s[:password], keys: [s[:key] || ""] }
63
- Server.new(s[:env], s[:ip], s[:user], options, s[:services])
64
- end
65
- end
66
-
67
- def configure_target_servers
68
- configure_target_env
69
- @target_servers = @servers.select { |s| @target_env.include?(s[:env]) }
70
- end
71
-
72
- def configure_target_env
73
- @target_env = @env_names & @argv
74
-
75
- # If there is no argument.
76
- @target_env = @env_names.first if @target_env.empty? && @argv.empty?
77
-
78
- # If there is only one argument and it is `all`.
79
- @target_env = @env_names if @argv.size == 1 && argv.first == 'all'
80
- end
81
-
82
- def ssh(server)
83
- services = []
84
- Net::SSH.start(server[:ip], server[:user], server[:options]) do |ssh|
85
- server[:hostname] = ssh.exec!('hostname').chop
86
- services = server[:services].map { |service_name| systemd(ssh, service_name) }
87
- # TODO: store in Result object
88
- # puts ssh.exec!("systemctl list-timers")
89
- end
90
- Result.new(server, services)
91
- end
92
-
93
- def systemd(ssh, service_name)
94
- states = ssh.exec!("systemctl show #{service_name} -p #{STATES.join(",")}")
95
- .split("\n")
96
- .map { |v| v.split("=") }
97
- .map { |v| [Thor::Util.snake_case(v[0]).to_sym, v[1]] }
98
- .each_with_object({}) { |(k, v), memo| memo[k] = v }
99
-
100
- Service.new(
101
- service_name,
102
- states[:load_state],
103
- states[:active_state],
104
- states[:sub_state],
105
- states[:unit_file_state] || "n/a",
106
- states[:type] || "n/a"
107
- )
108
- end
109
- end
110
- end