kanamei-keystone 0.0.17 → 0.0.18

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.
@@ -11,7 +11,7 @@ autoload :Moji , 'moji'
11
11
 
12
12
  module Keystone
13
13
 
14
- VERSION = '0.0.17'
14
+ VERSION = '0.0.18'
15
15
 
16
16
  autoload :StringUtil , 'keystone/string_util'
17
17
  autoload :Batch , 'keystone/batch'
@@ -1,4 +1,15 @@
1
1
  module Keystone
2
+
3
+ #
4
+ # ログレベルに関してのローカルルール(ログ確認に関して)
5
+ # debug 対応不要
6
+ # info 対応不要
7
+ # warn 複数個で営業時間内対応
8
+ # notice 1つで営業時間内対応
9
+ # error 1つで営業時間内対応
10
+ # alert 複数個で即時対応
11
+ # critical 1つで即時対応
12
+ #
2
13
  module Base
3
14
  def log(log_type,message, is_base_info = true)
4
15
  if is_base_info
@@ -8,10 +19,6 @@ module Keystone
8
19
  end
9
20
  end
10
21
 
11
- def info(message)
12
- log(:INFO,message)
13
- end
14
-
15
22
  def error(message)
16
23
  if message.is_a? Exception
17
24
  log(:ERROR,"#{message.message}")
@@ -23,10 +30,26 @@ module Keystone
23
30
  end
24
31
  end
25
32
 
33
+ def info(message)
34
+ log(:INFO,message)
35
+ end
36
+
26
37
  def warn(message)
27
38
  log(:WARN,message)
28
39
  end
29
40
 
41
+ def notice(message)
42
+ log(:NOTICE,message)
43
+ end
44
+
45
+ def alert(message)
46
+ log(:ALERT,message)
47
+ end
48
+
49
+ def critical(message)
50
+ log(:CRIT,message)
51
+ end
52
+
30
53
  def debug(message)
31
54
  if $DEBUG
32
55
  log(:DEBUG,message)
@@ -1,4 +1,3 @@
1
-
2
1
  require 'digest/md5'
3
2
  require "optparse"
4
3
 
@@ -197,7 +196,7 @@ module Keystone::Batch
197
196
  ===== environment =====
198
197
  #{host.dump}
199
198
  BODY
200
- Keystone::Mail::Send.send(mail_from,mail_to,title,body,smtp_addr,smtp_port)
199
+ Keystone::Mail::Send.send(mail_from,mail_to,title,body,{:smtp_addr=>smtp_addr,:smtp_port=>smtp_port})
201
200
  else
202
201
  warn "ERROR_MAIL_TO not defined.if you want error mail automatically,set this value."
203
202
  end
@@ -4,6 +4,7 @@ class Array
4
4
  # active_supportに同様のメソッドが無いかどうかの確認
5
5
  #
6
6
  def split_by(num)
7
+ return [] if self.size < 1
7
8
  ret = [[]]
8
9
  counter = 0
9
10
  self.each do |val|
@@ -1,4 +1,8 @@
1
1
  class Dir
2
+
3
+
4
+ # TODO
5
+ # replace FileUtils.mkdir_p
2
6
  def self.mkdir_r(dirPath)
3
7
  folders = dirPath.split('/')
4
8
  folderToCreate = ''
@@ -1,6 +1,8 @@
1
- module Keystone
2
- module Mail
3
- autoload :Send , 'keystone/mail/send'
4
- autoload :Receive , 'keystone/mail/receive'
5
- end
1
+ module Keystone::Mail
2
+ autoload :Message, 'keystone/mail/message'
3
+ autoload :MessageIso2022jp, 'keystone/mail/message_iso2022jp'
4
+ autoload :MessageSjis, 'keystone/mail/message_sjis'
5
+ autoload :Receive, 'keystone/mail/receive'
6
+ autoload :MessageFactory, 'keystone/mail/message_factory'
7
+ autoload :Send, 'keystone/mail/send'
6
8
  end
@@ -1,76 +1,83 @@
1
- #
2
- # 普通なことをしたいならTMailを使いましょう
3
- #
4
-
5
1
  require 'net/smtp'
6
- require 'kconv'
2
+
3
+ if __FILE__ ==$0
4
+ $: << '../..'
5
+ require 'keystone'
6
+ end
7
7
 
8
8
  module Keystone
9
9
  module Mail
10
10
  class Send
11
- def initialize(smtp_addr="127.0.0.1",smtp_port=25)
12
- @smtp_addr,@smtp_port=smtp_addr,smtp_port
13
- end
14
-
15
- def self.send(from,to,title,body,smtp_addr="127.0.0.1",smtp_port=25)
16
- from_addr,from_addr_4_header = create_addr_and_header(from)
17
- title.gsub!(/(\r|\n)/,"")
18
-
19
- subject_header = ""
20
- title.split(//).split_by(10).each do |subject|
21
- st = subject.join('')
22
- st = Kconv.tojis(st)
23
- st = st.split(//,1).pack('m'); st = st.chomp
24
- subject_header += " =?ISO-2022-JP?B?#{st}?=\n"
25
- end
26
-
27
- if to.class.to_s == 'Array'
28
- tomany = to.join(',')
29
- else
30
- tomany = to.to_s;
31
- end
11
+ attr_accessor :message, :smtp_addr, :smtp_port, :retry_cnt
32
12
 
33
- head = <<HEAD
34
- Subject: #{subject_header}To: #{tomany}
35
- From: #{from_addr_4_header}
36
- Mime-Version: 1.0
37
- Content-Transfer-Encoding: 7bit
38
- Content-Type: Text/Plain; charset=iso-2022-jp
39
- HEAD
13
+ #
14
+ # 普通なことをしたいならTMailを使いましょう
15
+ # class Send
16
+ def initialize(message=nil,opt={})
17
+ @smtp_addr, @smtp_port, @retry_cnt = "127.0.0.1", 25, 0
18
+ @message = message
19
+ @message = Keystone::Mail::MessageFactory.create(opt) if @message == nil
20
+ debug "@message=#{@message}"
21
+ set_option(opt)
22
+ end
40
23
 
41
- src = head + "\n" + Kconv.tojis(body)
42
- # puts src
43
- m = Net::SMTPSession.new(smtp_addr, smtp_port)
44
- m.start()
45
- m.sendmail(src, from_addr, to)
46
- m.finish
24
+ #
25
+ # from could be String or Array
26
+ # opt
27
+ # :smtp_addr
28
+ # :smtp_port
29
+ # :retry
30
+ # :mail_from_text
31
+ # :encoding
32
+ #
33
+ def self.send(from, to, subject, body, opt={})
34
+ queue = self.new(nil,opt)
35
+ queue.message.mail_to = to
36
+ queue.message.mail_from = from
37
+ queue.message.subject = subject
38
+ queue.message.body = body
39
+ queue.send
47
40
  end
48
41
 
49
- def send(from,to,title,body)
50
- self.class.send(from,to,title,body,@smtp_addr,@smtp_port)
51
- end
52
-
53
- private
54
-
55
- def self.create_addr_and_header(addr_data)
56
- addr = ''
57
- addr_4_header = ''
58
- case addr_data.class.to_s
59
- when 'String'
60
- addr = addr_data.dup
61
- addr_4_header = addr.dup
62
- when 'Array'
63
- addr = addr_data[1].dup
64
- addr_4_header = addr_data[0].dup
65
- addr_4_header = Kconv.tojis(addr_4_header)
66
- addr_4_header = addr_4_header.split(//,1).pack('m'); addr_4_header = addr_4_header.chomp
67
- addr_4_header = "=?ISO-2022-JP?B?#{addr_4_header}?=<#{addr}>"
68
- else
69
- # TODO exception
70
- raise 'addr_data must be String or Array'
42
+ def send
43
+ src = @message.to_src
44
+ debug src
45
+ try_cnt = 0
46
+ begin
47
+ m = Net::SMTPSession.new(@smtp_addr, @smtp_port)
48
+ m.start()
49
+ m.sendmail(src ,@message.mail_from ,@message.mail_to)
50
+ m.finish
51
+ rescue => e
52
+ debug "try_cnt:#{try_cnt}"
53
+ try_cnt += 1
54
+ sleep 1
55
+ retry if @retry_cnt >= try_cnt
56
+ raise e
71
57
  end
72
- return addr,addr_4_header
58
+ end
59
+
60
+ def set_option(opt)
61
+ @smtp_addr = opt[:smtp_addr] if opt.key?(:smtp_addr)
62
+ @smtp_port = opt[:smtp_port] if opt.key?(:smtp_port)
63
+ @retry_cnt = Integer(opt[:retry_cnt]) if opt.key?(:retry_cnt)
73
64
  end
74
65
  end
75
66
  end
76
- end
67
+ end
68
+
69
+ if __FILE__ ==$0
70
+ require 'rubygems'
71
+ require 'pit'
72
+
73
+ config = Pit.get("keystone_test")
74
+
75
+ Keystone::Mail::Send.send(
76
+ config["mailfrom01"],
77
+ config["mailto01"],
78
+ "件名かんさいーーーーでんきほーーーあんky−−−かいいい−−かいいい−−かいいいいいいいい終わり",
79
+ "body日本語",
80
+ {:mail_from_text=>"メール送信者名ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー終わり",:retry_cnt=>3,:encoding=>:sjis}
81
+ )
82
+ end
83
+
@@ -1,6 +1,18 @@
1
1
  module Keystone
2
2
  module Os
3
3
  class Osx < Darwin
4
+ def ip_address
5
+ ifconfig = `/sbin/ifconfig`
6
+ ips = []
7
+
8
+ # TODO mac
9
+ ifconfig.gsub(/inet (\d+\.\d+\.\d+\.\d+) netmask/){|ip|
10
+ if $1 != '127.0.0.1'
11
+ ips << $1
12
+ end
13
+ }
14
+ return ips
15
+ end
4
16
  end
5
17
  end
6
18
  end
@@ -14,19 +14,28 @@ module Keystone
14
14
  return ips
15
15
  end
16
16
 
17
+ def bin_or_usrbin(cmd,option="")
18
+ cmd_option = "#{cmd} #{option}"
19
+ begin;return `/bin/#{cmd_option}` if File.exists?("/bin/#{cmd}");rescue;end
20
+ begin;return `/usr/bin/#{cmd_option}` if File.exists?("/usr/bin/#{cmd}");rescue;end
21
+ begin;return `/sbin/#{cmd_option}` if File.exists?("/sbin/#{cmd}");rescue;end
22
+ begin;return `/usr/sbin/#{cmd_option}`.chomp;rescue;end
23
+ end
24
+
17
25
  def hostname
18
- `/bin/hostname`.chomp
26
+ bin_or_usrbin("hostname")
19
27
  end
20
28
 
21
29
  def disk
22
- `/bin/df -h`.chomp
30
+ bin_or_usrbin("df","-h")
23
31
  end
24
32
 
25
33
  def process_list
26
- `/bin/ps -ef`.chomp
34
+ bin_or_usrbin("ps","-aux")
27
35
  end
28
36
 
29
37
  def netstat
38
+ bin_or_usrbin("netstat","-an")
30
39
  `/usr/sbin/netstat -an`.chomp
31
40
  end
32
41
  end
@@ -29,7 +29,7 @@ module Keystone
29
29
  return new_path
30
30
  end
31
31
 
32
- def self.reverse_mail(mail)
32
+ def self.reverse_mail_address(mail)
33
33
  if mail.index('@') == nil
34
34
  return mail
35
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanamei-keystone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - kanamei
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-08 00:00:00 -07:00
12
+ date: 2009-05-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15