icwot 0.1.1 → 0.2.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 +4 -4
- data/bin/icwot +11 -108
- data/icwot.gemspec +1 -1
- data/lib/icwot/client.rb +38 -0
- data/lib/icwot/console.rb +77 -0
- data/lib/icwot/server_client.rb +56 -0
- data/lib/icwot/version.rb +3 -1
- data/lib/icwot.rb +22 -42
- metadata +4 -2
- data/Rakefile +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87d7b6e4a3a0b5d5f5310815f6d62dcbb8a7dc68
|
4
|
+
data.tar.gz: 5e45269940885f14aa76296bad21d25bb1834ddb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8dcc6a75b177ba34fc434cce1a795b9b323fca4131ae72d65ff2fc13138cc081ab6265d67d85e69750cc30e84b488583f6077be99fbcecf422c2ce173dd407aa
|
7
|
+
data.tar.gz: d564062c6af2afd3f34256e7320b0cdc407cb5821dc886be51c19b84cf191a0ef66c5f562111ce9e7a25334103424787ed26fb99ffe4546484b909f3ca5b48cb
|
data/bin/icwot
CHANGED
@@ -6,123 +6,26 @@ require 'socket'
|
|
6
6
|
require 'timeout'
|
7
7
|
require 'rest-client'
|
8
8
|
require 'json'
|
9
|
+
require 'icwot'
|
10
|
+
require 'icwot/client'
|
11
|
+
require 'icwot/console'
|
9
12
|
|
10
|
-
|
11
|
-
# from http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open
|
12
|
-
def self.is_port_open?(ip, port)
|
13
|
-
begin
|
14
|
-
Timeout::timeout(1) do
|
15
|
-
begin
|
16
|
-
s = TCPSocket.new(ip, port)
|
17
|
-
s.close
|
18
|
-
return true
|
19
|
-
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
|
20
|
-
return false
|
21
|
-
end
|
22
|
-
end
|
23
|
-
rescue Timeout::Error
|
24
|
-
end
|
25
|
-
return false
|
26
|
-
end
|
27
|
-
|
28
|
-
class Client
|
29
|
-
attr_accessor :uri, :content_type
|
30
|
-
|
31
|
-
def initialize
|
32
|
-
@content_type = 'application/json'
|
33
|
-
end
|
34
|
-
|
35
|
-
def to_xml
|
36
|
-
"<client xmlns=\"http://jaxb.xwot.first.ch.unifr.diuf\"><uri>#{uri}</uri></client>"
|
37
|
-
end
|
38
|
-
|
39
|
-
def to_hash
|
40
|
-
{uri: uri}
|
41
|
-
end
|
13
|
+
console = Console.new
|
42
14
|
|
43
|
-
|
44
|
-
|
45
|
-
ip=Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
|
46
|
-
ip ? ip.ip_address : '127.0.0.1'
|
47
|
-
end
|
48
|
-
|
49
|
-
def to_s
|
50
|
-
case content_type
|
51
|
-
when 'application/json'
|
52
|
-
to_hash.to_json
|
53
|
-
when 'application/xml'
|
54
|
-
to_xml
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
if ARGV.empty?
|
60
|
-
puts 'you need to provide at least one argument! See icwot -h'
|
15
|
+
unless console.parse
|
16
|
+
puts console.errors
|
61
17
|
exit 0
|
62
18
|
end
|
63
|
-
produces = 'application/json'
|
64
|
-
accept = 'application/json'
|
65
|
-
port = 4567
|
66
|
-
host = ''
|
67
|
-
protocol = 'http://'
|
68
|
-
log_path = ''
|
69
|
-
ARGV.each_with_index do |a, index|
|
70
|
-
case a
|
71
|
-
when '-h'
|
72
|
-
puts 'Usage : icwot <host>
|
73
|
-
-h print help
|
74
|
-
-l the host is localhost
|
75
|
-
-c the content-type value for the header application/json by default
|
76
|
-
-a the accept value for the header text/plain by default
|
77
|
-
-p the port where to run the server
|
78
|
-
-t the protocol to use http:// by default
|
79
|
-
-l where to save the log. By default your-home-directory/log/icwot-{port}-msg.log
|
80
|
-
host is where to register for the service.
|
81
|
-
'
|
82
|
-
exit 0
|
83
|
-
when '-l'
|
84
|
-
host='localhost'
|
85
|
-
when '-c'
|
86
|
-
produces = ARGV.delete_at index + 1
|
87
|
-
when '-a'
|
88
|
-
accept = ARGV.delete_at index + 1
|
89
|
-
when '-p'
|
90
|
-
port = ARGV.delete_at(index + 1).to_i
|
91
|
-
when '-t'
|
92
|
-
unless (temp = ARGV.delete_at(index + 1)).nil?
|
93
|
-
protocol = temp
|
94
|
-
end
|
95
|
-
when '-l'
|
96
|
-
log_path = ARGV.delete_at index + 1
|
97
|
-
else
|
98
|
-
host += a
|
99
|
-
end
|
100
|
-
end
|
101
19
|
|
102
|
-
if
|
103
|
-
puts 'Host must be set !'
|
104
|
-
exit 0
|
105
|
-
end
|
106
|
-
|
107
|
-
if port == 0
|
108
|
-
puts 'The port must be bigger than 0'
|
109
|
-
exit 0
|
110
|
-
end
|
111
|
-
|
112
|
-
if is_port_open?('localhost', port)
|
20
|
+
if Icwot.is_port_open?('localhost', console.port)
|
113
21
|
puts 'Port already in use ! Try another one.'
|
114
22
|
exit 0
|
115
23
|
end
|
116
24
|
|
117
|
-
client = Client.new
|
118
|
-
client.uri = "http://#{client.get_ip}:#{port}/"
|
119
|
-
client.content_type = produces
|
25
|
+
client = Client.new(port: console.port, content_type: console.produces)
|
120
26
|
|
121
|
-
|
27
|
+
RestClient.post console.url, client.to_s, console.header
|
122
28
|
|
123
|
-
|
124
|
-
puts 'Error. Connection not establish with the server.'
|
125
|
-
exit 0
|
126
|
-
end
|
29
|
+
#TODO handle exception from RestClient
|
127
30
|
|
128
|
-
|
31
|
+
Icwot.run(console.port, console.log_path)
|
data/icwot.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'icwot/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "icwot"
|
8
|
-
spec.version = Icwot::
|
8
|
+
spec.version = Icwot::version
|
9
9
|
spec.authors = ["leo"]
|
10
10
|
spec.email = ["facenord.sud@gmail.com"]
|
11
11
|
spec.description = %q{icwot : RESTfull sinatra server providing inversion of control for logging messages from another web-server}
|
data/lib/icwot/client.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Client
|
2
|
+
attr_accessor :uri, :content_type, :port
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@content_type = options[:content_type] || 'application/json'
|
6
|
+
@uri = options[:uri]
|
7
|
+
@port = options[:port]
|
8
|
+
end
|
9
|
+
|
10
|
+
def uri
|
11
|
+
@uri.nil? ? "http://#{get_ip}:#{port}" : @uri
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
case content_type
|
16
|
+
when 'application/json'
|
17
|
+
to_hash.to_json
|
18
|
+
when 'application/xml'
|
19
|
+
to_xml
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def to_xml
|
26
|
+
"<client xmlns=\"http://jaxb.xwot.first.ch.unifr.diuf\"><uri>#{uri}</uri></client>"
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_hash
|
30
|
+
{uri: uri}
|
31
|
+
end
|
32
|
+
|
33
|
+
# inspired by http://stackoverflow.com/questions/11897269/get-own-ip-address
|
34
|
+
def get_ip
|
35
|
+
ip=Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
|
36
|
+
ip ? ip.ip_address : '127.0.0.1'
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class Console
|
2
|
+
|
3
|
+
attr_reader :produces, :accept, :port, :host, :protocol, :log_path, :errors
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@produces = 'application/json'
|
7
|
+
@accept = 'application/json'
|
8
|
+
@port = 4567
|
9
|
+
@host = ''
|
10
|
+
@protocol = 'http://'
|
11
|
+
@log_path = ''
|
12
|
+
@errors = ''
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse
|
16
|
+
if ARGV.empty?
|
17
|
+
@errors = 'you need to provide at least one argument! See icwot -h'
|
18
|
+
return false
|
19
|
+
end
|
20
|
+
ARGV.each_with_index do |a, index|
|
21
|
+
case a
|
22
|
+
when '-h'
|
23
|
+
puts 'Usage : icwot <host>
|
24
|
+
-h print help
|
25
|
+
-l the host is localhost
|
26
|
+
-c the content-type value for the header application/json by default
|
27
|
+
-a the accept value for the header text/plain by default
|
28
|
+
-p the port where to run the server
|
29
|
+
-t the protocol to use http:// by default
|
30
|
+
-l where to save the log. By default your-home-directory/log/icwot-{port}-msg.log
|
31
|
+
host is where to register for the service.
|
32
|
+
'
|
33
|
+
exit 0
|
34
|
+
when '-l'
|
35
|
+
@host='localhost'
|
36
|
+
when '-c'
|
37
|
+
@produces = ARGV.delete_at index + 1
|
38
|
+
when '-a'
|
39
|
+
@accept = ARGV.delete_at index + 1
|
40
|
+
when '-p'
|
41
|
+
@port = ARGV.delete_at(index + 1).to_i
|
42
|
+
when '-t'
|
43
|
+
unless (temp = ARGV.delete_at(index + 1)).nil?
|
44
|
+
@protocol = temp
|
45
|
+
end
|
46
|
+
when '-l'
|
47
|
+
@log_path = ARGV.delete_at index + 1
|
48
|
+
else
|
49
|
+
@host += a
|
50
|
+
end
|
51
|
+
end
|
52
|
+
validate
|
53
|
+
end
|
54
|
+
|
55
|
+
def url
|
56
|
+
protocol+host
|
57
|
+
end
|
58
|
+
|
59
|
+
def header
|
60
|
+
{accept: accept, content_type: produces}
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def validate
|
66
|
+
if @host == ''
|
67
|
+
@errors=('Host must be set !')
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
|
71
|
+
if @port == 0
|
72
|
+
@errors = 'The port must be bigger than 0'
|
73
|
+
return false
|
74
|
+
end
|
75
|
+
true
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
class ServerClient < Sinatra::Base
|
4
|
+
|
5
|
+
# Si le fichier log/msg.log n'existe pas, on le créé, sinon on l'ouvre
|
6
|
+
def self.create_or_open_log
|
7
|
+
file_name = "icwot-#{settings.port}-msg.log"
|
8
|
+
directory_path = "#{Dir.home}/log/"
|
9
|
+
Dir.mkdir(directory_path) unless File.exists?(directory_path)
|
10
|
+
file = (settings.log_path.nil? || !File.exist?(settings.log_path) ? File.new(directory_path+file_name, 'a+') : File.new(settings.log_path))
|
11
|
+
file.sync = true
|
12
|
+
file
|
13
|
+
end
|
14
|
+
|
15
|
+
configure do
|
16
|
+
set :log_path, nil
|
17
|
+
|
18
|
+
set :environment, :production
|
19
|
+
|
20
|
+
enable :logging
|
21
|
+
# We put the log level to info
|
22
|
+
set :logger_level, :info
|
23
|
+
# The logger is at the location log/msg.log
|
24
|
+
set :logger_log_file, lambda { create_or_open_log }
|
25
|
+
end
|
26
|
+
|
27
|
+
# POST sur / avec le port par défaut 4567
|
28
|
+
post '/' do
|
29
|
+
content_type 'text/plain'
|
30
|
+
# On enregistre dans un log spécial le body de la requête
|
31
|
+
msg.info request.body.read
|
32
|
+
logger.info "message saved to #{self.class.logger_log_file.path}"
|
33
|
+
# On retourne le code http 200 avec le texte 'ok'
|
34
|
+
'ok'
|
35
|
+
end
|
36
|
+
|
37
|
+
helpers do
|
38
|
+
# logger personalisé pour enregistrer les réponses du body
|
39
|
+
def msg
|
40
|
+
@logger ||= begin
|
41
|
+
@logger = ::Logger.new(self.class.logger_log_file)
|
42
|
+
@logger.level = ::Logger.const_get((self.class.logger_level || :warn).to_s.upcase)
|
43
|
+
@logger.datetime_format = "%Y-%m-%d %H:%M:%S"
|
44
|
+
@logger.formatter = proc do |serverity, time, progname, msg|
|
45
|
+
# le format est de la sorte:
|
46
|
+
#
|
47
|
+
# 2013-12-12 00:00:00
|
48
|
+
# "message body"
|
49
|
+
|
50
|
+
"#{time} :\n\"#{msg}\"\n\n"
|
51
|
+
end
|
52
|
+
@logger
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/icwot/version.rb
CHANGED
data/lib/icwot.rb
CHANGED
@@ -1,51 +1,31 @@
|
|
1
1
|
require "icwot/version"
|
2
|
-
require '
|
2
|
+
require 'icwot/server_client'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# POST sur / avec le port 4567
|
12
|
-
post '/' do
|
13
|
-
content_type 'text/plain'
|
14
|
-
# On enregistre dans un log spécial le body de la requête
|
15
|
-
msg.info request.body.read
|
16
|
-
logger.info "message saved to #{self.class.logger_log_file.path}"
|
17
|
-
# On retourne le code http 200 avec le texte 'ok'
|
18
|
-
'ok'
|
19
|
-
end
|
20
|
-
|
21
|
-
helpers do
|
22
|
-
# logger personalisé pour enregistrer les réponses du body
|
23
|
-
def msg
|
24
|
-
@logger ||= begin
|
25
|
-
@logger = ::Logger.new(self.class.logger_log_file)
|
26
|
-
@logger.level = ::Logger.const_get((self.class.logger_level || :warn).to_s.upcase)
|
27
|
-
@logger.datetime_format = "%Y-%m-%d %H:%M:%S"
|
28
|
-
@logger.formatter = proc do |serverity, time, progname, msg|
|
29
|
-
# le format et de la sorte:
|
30
|
-
#
|
31
|
-
# 2013-12-12 00:00:00
|
32
|
-
# "message body"
|
4
|
+
module Icwot
|
5
|
+
def self.run(port, log_path)
|
6
|
+
app = ServerClient.new
|
7
|
+
app.settings.port = port
|
8
|
+
app.settings.log_path = log_path
|
9
|
+
app.settings.run!
|
10
|
+
end
|
33
11
|
|
34
|
-
|
12
|
+
# determine if a port is used or not
|
13
|
+
# from http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open
|
14
|
+
def self.is_port_open?(ip, port)
|
15
|
+
begin
|
16
|
+
Timeout::timeout(1) do
|
17
|
+
begin
|
18
|
+
s = TCPSocket.new(ip, port)
|
19
|
+
s.close
|
20
|
+
return true
|
21
|
+
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
|
22
|
+
return false
|
23
|
+
end
|
35
24
|
end
|
36
|
-
|
25
|
+
rescue Timeout::Error
|
37
26
|
end
|
27
|
+
return false
|
38
28
|
end
|
39
29
|
end
|
40
30
|
|
41
|
-
private
|
42
31
|
|
43
|
-
# Si le fichier log/msg.log n'existe pas, on le créé, sinon on l'ouvre
|
44
|
-
def create_or_open_log
|
45
|
-
file_name = "icwot-#{ENV['sinatra_port']}-msg.log"
|
46
|
-
directory_path = "#{Dir.home}/log/"
|
47
|
-
Dir.mkdir(directory_path) unless File.exists?(directory_path)
|
48
|
-
file = ((log_path = ENV['sinatra_log']) == '' && !File.exist?(log_path) ? File.new(directory_path+file_name, 'a+') : File.new(ENV['sinatra_log']))
|
49
|
-
file.sync = true
|
50
|
-
file
|
51
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: icwot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- leo
|
@@ -107,10 +107,12 @@ files:
|
|
107
107
|
- Gemfile
|
108
108
|
- LICENSE.txt
|
109
109
|
- README.md
|
110
|
-
- Rakefile
|
111
110
|
- bin/icwot
|
112
111
|
- icwot.gemspec
|
113
112
|
- lib/icwot.rb
|
113
|
+
- lib/icwot/client.rb
|
114
|
+
- lib/icwot/console.rb
|
115
|
+
- lib/icwot/server_client.rb
|
114
116
|
- lib/icwot/version.rb
|
115
117
|
homepage: https://github.com/facenord-sud/icwot
|
116
118
|
licenses:
|
data/Rakefile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|