mastodon_healthchecker 0.1.0.pre → 0.1.0.pre.2

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
  SHA1:
3
- metadata.gz: cf3b0f82d8322a8b5ab8ee6ffa15f2ae69b3f74a
4
- data.tar.gz: 295ec16abde24bdf984a80d3b63374a2a33c5669
3
+ metadata.gz: 7ac4c1e11218f383ffeb7d4e4f93c2f47a103b1a
4
+ data.tar.gz: 3a07a3424670db0aa6c02d15f6269b8dc151ad4f
5
5
  SHA512:
6
- metadata.gz: 19f358c52a57860b6b9875f6db23b6942bd71f65a86e8cc617170cbb1d5e1017112e05e838bda288b030b7ea70667bc615d243532f265cdaa2c35dce5efb6777
7
- data.tar.gz: 12a936ee80f32546e1d4f9f73babc768fbf4ca9fc43d6e995333384cf918a848ced71f73f5c7560b7deb68307cbfe6d38a67f59e3ec525db2305762225a4f945
6
+ metadata.gz: '0638501d22b1a3e2a89c263cef80b0e87eb80ff2d39ecc622b994e18a7a48e855ac5786684ac7b2b9bb7c0e2b6b40784e7ecab3cbfb157f99ca9b0041c5a5848'
7
+ data.tar.gz: 8ea93661741d70a4db76207740ad131b0cc65298ace25fcffcd5d689a1e08bde2c1ed27f3fac5a6ad8fcb84c67758d35fbefda66d2fa57a2572e0bd62b74c5f2
@@ -1,20 +1,19 @@
1
+ require 'mastodon_healthchecker/dns_records'
2
+ require 'mastodon_healthchecker/instance_info'
1
3
  require 'mastodon_healthchecker/version'
2
4
 
3
5
  module MastodonHealthchecker
4
- autoload :AvailabilityInspector,
5
- 'mastodon_healthchecker/inspectors/availability'
6
- autoload :RecordExistenceInspector,
7
- 'mastodon_healthchecker/inspectors/record_existence'
6
+ Result = Struct.new('Result', :exists_record, :up, :info)
8
7
 
9
- DefaultInspectionItems = {
10
- exists_record: RecordExistenceInspector,
11
- up: AvailabilityInspector
12
- }.freeze
8
+ def self.perform(host)
9
+ records = DNSRecords.new(host)
10
+ info_v4 = InstanceInfo.fetch(host, records.v4_addresses) if records.v4_addresses.any?
11
+ info_v6 = InstanceInfo.fetch(host, records.v6_addresses) if records.v6_addresses.any?
13
12
 
14
- def self.perform(host, inspection_items: DefaultInspectionItems)
15
- inspection_items.inject({}) do |result, (key, item)|
16
- result[key] = item.call(host)
17
- result
18
- end
13
+ Result.new(
14
+ { v4: records.v4_addresses.any?, v6: records.v6_addresses.any? },
15
+ { v4: !info_v4.nil?, v6: !info_v6.nil? },
16
+ info_v4 || info_v6 || nil
17
+ )
19
18
  end
20
19
  end
@@ -0,0 +1,23 @@
1
+ require 'resolv'
2
+
3
+ module MastodonHealthchecker
4
+ class DNSRecords
5
+ def initialize(host)
6
+ @v4_addresses = []
7
+ @v6_addresses = []
8
+ Resolv::DNS.new.each_address(host) do |addr|
9
+ case addr
10
+ when Resolv::IPv4
11
+ v4_addresses << addr
12
+ when Resolv::IPv6
13
+ v6_addresses << addr
14
+ else
15
+ raise "Resolver returns an address which is neither IPv4 nor IPv6."
16
+ end
17
+ end
18
+ end
19
+
20
+ attr_reader :v4_addresses, :v6_addresses
21
+ end
22
+ end
23
+
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Copyright (C) 2017 nullkal.
5
+ #
6
+ # This program is a derived work from Resolv::Hosts of Ruby 2.4
7
+ # (https://github.com/ruby/ruby/).
8
+ #
9
+ # ----
10
+ #
11
+ # Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
12
+ #
13
+ # Redistribution and use in source and binary forms, with or without
14
+ # modification, are permitted provided that the following conditions
15
+ # are met:
16
+ # 1. Redistributions of source code must retain the above copyright
17
+ # notice, this list of conditions and the following disclaimer.
18
+ # 2. Redistributions in binary form must reproduce the above copyright
19
+ # notice, this list of conditions and the following disclaimer in the
20
+ # documentation and/or other materials provided with the distribution.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
+ # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
+ # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
+ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
+ # SUCH DAMAGE.
33
+ #
34
+
35
+ require 'resolv'
36
+
37
+ module MastodonHealthchecker
38
+ class HashHosts
39
+ def initialize(hosts)
40
+ @name2addr = hosts
41
+
42
+ @addr2name = {}
43
+ hosts.each do |name, addrs|
44
+ addrs.each do |addr|
45
+ @addr2name[addr] ||= []
46
+ @addr2name[addr] << name
47
+ end
48
+ end
49
+ end
50
+
51
+ ##
52
+ # Gets the IP address of +name+ from the hosts file.
53
+
54
+ def getaddress(name)
55
+ each_address(name) {|address| return address}
56
+ raise ResolvError.new("#{@filename} has no name: #{name}")
57
+ end
58
+
59
+ ##
60
+ # Gets all IP addresses for +name+ from the hosts file.
61
+
62
+ def getaddresses(name)
63
+ ret = []
64
+ each_address(name) {|address| ret << address}
65
+ return ret
66
+ end
67
+
68
+ ##
69
+ # Iterates over all IP addresses for +name+ retrieved from the hosts file.
70
+
71
+ def each_address(name, &proc)
72
+ if @name2addr.include?(name)
73
+ @name2addr[name].each(&proc)
74
+ end
75
+ end
76
+
77
+ ##
78
+ # Gets the hostname of +address+ from the hosts file.
79
+
80
+ def getname(address)
81
+ each_name(address) {|name| return name}
82
+ raise ResolvError.new("#{@filename} has no address: #{address}")
83
+ end
84
+
85
+ ##
86
+ # Gets all hostnames for +address+ from the hosts file.
87
+
88
+ def getnames(address)
89
+ ret = []
90
+ each_name(address) {|name| ret << name}
91
+ return ret
92
+ end
93
+
94
+ ##
95
+ # Iterates over all hostnames for +address+ retrieved from the hosts file.
96
+
97
+ def each_name(address, &proc)
98
+ @addr2name[address]&.each(&proc)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,90 @@
1
+ require 'json'
2
+ require 'mastodon_healthchecker/hash_hosts'
3
+ require 'nokogiri'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require 'faraday/encoding'
7
+ require 'resolv'
8
+ require 'resolv-replace'
9
+
10
+ module MastodonHealthchecker
11
+ class InstanceInfo
12
+
13
+ class << self
14
+ def fetch(host, addresses)
15
+ default_resolver = Resolv::DefaultResolver
16
+
17
+ hash_hosts = HashHosts.new({ "#{host}" => addresses })
18
+ Resolv::DefaultResolver.replace_resolvers([hash_hosts, Resolv::DNS.new])
19
+
20
+ result = nil
21
+ begin
22
+ info = InstanceInfo.new
23
+ info.parse_about(get(host, '/about'))
24
+ info.parse_about_more(get(host, '/about/more'))
25
+ info.parse_instance(get(host, '/api/v1/instance'))
26
+ result = info
27
+ rescue Faraday::ClientError
28
+ rescue Nokogiri::SyntaxError
29
+ rescue JSON::ParseError
30
+ end
31
+
32
+ Resolv::DefaultResolver.replace_resolvers(default_resolver)
33
+ result
34
+ end
35
+
36
+ private
37
+
38
+ def get(host, path)
39
+ conn = Faraday.new("http://#{host}") do |faraday|
40
+ faraday.use FaradayMiddleware::FollowRedirects
41
+ faraday.response :encoding
42
+ faraday.response :raise_error
43
+ faraday.adapter :net_http
44
+ end
45
+ conn.get(path)
46
+ end
47
+ end
48
+
49
+ def initialize
50
+ @version = nil
51
+
52
+ @users = nil
53
+ @statuses = nil
54
+ @connections = nil
55
+
56
+ @description = nil
57
+ @extended_description = nil
58
+ @email = nil
59
+ @opened = nil
60
+ end
61
+
62
+ def parse_about(response)
63
+ doc = Nokogiri::HTML.parse(response.body)
64
+ @opened = doc.xpath("//*[@class='closed-registrations-message']").empty?
65
+ end
66
+
67
+ def parse_about_more(response)
68
+ doc = Nokogiri::HTML.parse(response.body)
69
+
70
+ info = doc.xpath("//*[@class='main']/*[@class='information-board']/*[@class='section']/strong")
71
+ if 3 <= info.length
72
+ @users = info[0].text.tr(',', '').to_i
73
+ @statuses = info[1].text.tr(',', '').to_i
74
+ @connections = info[2].text.tr(',', '').to_i
75
+ end
76
+
77
+ @extended_description = doc.xpath("//*[@class='main']/*[@class='panel'][2]").inner_html
78
+ end
79
+
80
+ def parse_instance(response)
81
+ instance = JSON.parse(response.body)
82
+ @version = instance['version']
83
+ @description = instance['description']
84
+ @email = instance['email']
85
+ end
86
+
87
+ attr_reader :version, :users, :statuses, :connections,
88
+ :description, :extended_description, :email, :opened
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module MastodonHealthchecker
2
- VERSION = '0.1.0.pre'.freeze
2
+ VERSION = '0.1.0.pre.2'.freeze
3
3
  end
@@ -29,4 +29,9 @@ and other statuses of a mastodon instance.
29
29
  spec.add_development_dependency 'rake', '~> 10.0'
30
30
  spec.add_development_dependency 'rspec', '~> 3.0'
31
31
  spec.add_development_dependency 'rubocop', '~> 0.49.1'
32
+
33
+ spec.add_runtime_dependency 'faraday', '~> 0.9.2'
34
+ spec.add_runtime_dependency 'faraday-encoding', '~> 0.0.4'
35
+ spec.add_runtime_dependency 'faraday_middleware', '~> 0.10.0'
36
+ spec.add_runtime_dependency 'nokogiri', '~> 1.6', '>= 1.6.7.2'
32
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastodon_healthchecker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre
4
+ version: 0.1.0.pre.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nullkal
@@ -66,6 +66,68 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.49.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday-encoding
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.0.4
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.0.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: faraday_middleware
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.10.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.10.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.6'
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 1.6.7.2
121
+ type: :runtime
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '1.6'
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 1.6.7.2
69
131
  description: |
70
132
  mastodon_healthchecker provides a function which checks the connectability,
71
133
  and other statuses of a mastodon instance.
@@ -87,9 +149,9 @@ files:
87
149
  - bin/console
88
150
  - bin/setup
89
151
  - lib/mastodon_healthchecker.rb
90
- - lib/mastodon_healthchecker/inspectors/availability.rb
91
- - lib/mastodon_healthchecker/inspectors/record_existence.rb
92
- - lib/mastodon_healthchecker/results/network_inspection.rb
152
+ - lib/mastodon_healthchecker/dns_records.rb
153
+ - lib/mastodon_healthchecker/hash_hosts.rb
154
+ - lib/mastodon_healthchecker/instance_info.rb
93
155
  - lib/mastodon_healthchecker/version.rb
94
156
  - mastodon_healthchecker.gemspec
95
157
  homepage: https://github.com/nullkal/mastodon_healthchecker
@@ -1,19 +0,0 @@
1
- require 'mastodon_healthchecker/results/network_inspection.rb'
2
-
3
- module MastodonHealthchecker
4
- module AvailabilityInspector
5
- def self.call(host)
6
- NetworkInspectionResult.new(v4: inspect_v4, v6: inspect_v6)
7
- end
8
-
9
- private
10
-
11
- def self.inspect_v4
12
- # TODO: Implement here
13
- end
14
-
15
- def self.inspect_v6
16
- # TODO: Implement here
17
- end
18
- end
19
- end
@@ -1,26 +0,0 @@
1
- module MastodonHealthchecker
2
- module RecordExistenceInspector
3
- class Result
4
- def initialize(v4: false, v6: false)
5
- @v4 = v4
6
- @v6 = v6
7
- end
8
-
9
- attr_accessor :v4, :v6
10
- end
11
-
12
- def self.call(host)
13
- Result.new(v4: inspect_v4, v6: inspect_v6)
14
- end
15
-
16
- private
17
-
18
- def self.inspect_v4
19
- # TODO: Implement here
20
- end
21
-
22
- def self.inspect_v6
23
- # TODO: Implement here
24
- end
25
- end
26
- end
@@ -1,15 +0,0 @@
1
- module MastodonHealthchecker
2
- class NetworkInspectionResult
3
- def initialize(v4: false, v6: false)
4
- @v4 = v4
5
- @v6 = v6
6
- end
7
-
8
- def to_s
9
- "v4: #{v4}, v6: #{v6}"
10
- end
11
-
12
- attr_accessor :v4, :v6
13
- end
14
- end
15
-