flydata 0.0.1.2011102401 → 0.0.1.2011102601

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1.2011102401
1
+ 0.0.1.2011102601
data/flydata.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "flydata"
8
- s.version = "0.0.1.2011102401"
8
+ s.version = "0.0.1.2011102601"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Koichi Fujikawa"]
12
- s.date = "2012-10-24"
12
+ s.date = "2012-10-26"
13
13
  s.description = "FlyData Command Line Interface"
14
14
  s.email = "sysadmin@flydata.co"
15
15
  s.executables = ["flydata"]
data/lib/flydata/cli.rb CHANGED
@@ -31,6 +31,10 @@ module Flydata
31
31
  puts <<-EOM
32
32
  Usage: flydata COMMAND
33
33
  setup # setup initially
34
+ start # start flydata process
35
+ stop # stop flydata process
36
+ restart # restart flydata process
37
+ setlogdel # show and change log deletion settting
34
38
  EOM
35
39
  end
36
40
  end
@@ -9,7 +9,7 @@ module Flydata
9
9
  # retrieve models on servers
10
10
  def retrieve_data_entries
11
11
  data_entries = flydata.get('/data_entries')
12
- unless flydata.response.code == 200 && data_entries.size > 0
12
+ unless flydata.response.code == 200
13
13
  raise "Failed to retrieve data_entries"
14
14
  end
15
15
  data_entries
@@ -1,21 +1,38 @@
1
1
  module Flydata
2
2
  module Command
3
3
  class Login < Base
4
+ LOGIN_TRIAL_TIMES=3
4
5
  def run
5
- email = ask("FlyData Email: ")
6
- password = ask("FlyData password: ") {|q| q.echo = false}
7
- flydata.credentials = Flydata::Credentials.new(email, password)
8
-
9
- login
6
+ ret = login(LOGIN_TRIAL_TIMES)
7
+ raise "Login failed #{LOGIN_TRIAL_TIMES} times." unless ret
8
+ ret
10
9
  end
11
10
 
12
11
  private
13
- def login
12
+ def login(times=3)
13
+ 1.upto(times) do |i|
14
+ begin
15
+ return true if login_once
16
+ rescue Exception => e
17
+ puts e
18
+ end
19
+ end
20
+ false
21
+ end
22
+ def login_once
23
+ # Ask login info
24
+ email = ask("FlyData Email: ")
25
+ password = ask("FlyData password: ") {|q| q.echo = false}
26
+ flydata.credentials = Flydata::Credentials.new(email, password)
27
+ # Auth request
14
28
  flydata.post('/users/sign_in')
15
29
  if flydata.response.code == 201 # 201: Created
16
30
  say("Login succeeded!")
17
31
  flydata.credentials.authenticate!
32
+ return true
18
33
  end
34
+ say("Login failed!")
35
+ false
19
36
  end
20
37
  end
21
38
  end
@@ -16,13 +16,21 @@ module Flydata
16
16
  end
17
17
 
18
18
  say('Starting sender process.')
19
- system("fluentd -d #{FLYDATA_HOME}/flydata.pid -l #{FLYDATA_HOME}/flydata.log -c #{FLYDATA_HOME}/flydata.conf")
19
+ Dir.chdir(FLYDATA_HOME){
20
+ system("fluentd -d #{FLYDATA_HOME}/flydata.pid -l #{FLYDATA_HOME}/flydata.log -c #{FLYDATA_HOME}/flydata.conf")
21
+ }
20
22
 
21
23
  sleep 5
24
+
25
+ data_port = flydata.data_port.get
26
+ data_port_id = data_port['id']
27
+
22
28
  retry_count = 10
29
+ ready = false
23
30
  1.upto(retry_count) do |i|
24
- if client_ready?
31
+ if (ready or ready = client_ready?) and uploaded_successfully?(data_port_id)
25
32
  say('Done.')
33
+ say("Go to your Dashboard! http://#{Flydata::FLYDATA_API_HOST}")
26
34
  return true
27
35
  end
28
36
  say("Waiting client side active... (#{i}/#{retry_count})")
@@ -30,8 +38,14 @@ module Flydata
30
38
  end
31
39
 
32
40
  say('Something wrong..')
41
+ false
33
42
  end
34
43
  def stop
44
+ unless process_exist?
45
+ say("Process doesn't exist.")
46
+ return true
47
+ end
48
+
35
49
  say('Stopping sender process.')
36
50
  if system("kill `cat #{FLYDATA_HOME}/flydata.pid`")
37
51
  say('Done.')
@@ -39,10 +53,21 @@ module Flydata
39
53
  else
40
54
  say('Something wrong..')
41
55
  end
56
+ false
42
57
  end
43
58
  def restart
44
- say('Restarting sender process.')
45
- stop and start
59
+ if process_exist?
60
+ say('Restarting sender process.')
61
+ if system("kill -HUP `cat #{FLYDATA_HOME}/flydata.pid`")
62
+ say('Done.')
63
+ return true
64
+ else
65
+ say('Something wrong..')
66
+ end
67
+ else
68
+ say("Process doesn't exist.")
69
+ start
70
+ end
46
71
  end
47
72
 
48
73
  private
@@ -57,6 +82,10 @@ module Flydata
57
82
  process_count = `ps aux|grep -v grep|grep "\\.flydata/flydata\\.conf"|grep fluentd|wc -l`.to_i
58
83
  process_count > 0
59
84
  end
85
+ def uploaded_successfully?(data_port_id)
86
+ res = flydata.get("/data_ports/#{data_port_id}/tail.json")
87
+ res and res['logs'] and res['logs'].size > 0
88
+ end
60
89
  end
61
90
  end
62
91
  end
@@ -15,8 +15,13 @@ module Flydata
15
15
  private
16
16
  def choose_data_entry
17
17
  data_entries = retrieve_data_entries
18
+ if data_entries.size < 1
19
+ puts("There are no registered entries.")
20
+ puts("You need to create a data entry by flydata setup command.")
21
+ return nil
22
+ end
18
23
  choices = data_entries.map {|e|
19
- "#{e['log_path']}\t\t -- Log deletion:#{boolToOnOff e['log_deletion']}"
24
+ "#{e['name']}\t#{e['log_path']}\t -- Log deletion:#{boolToOnOff e['log_deletion']}"
20
25
  }
21
26
  choices << "Cancel"
22
27
 
@@ -18,23 +18,47 @@ module Flydata
18
18
  # login
19
19
  Flydata::Command::Login.new.run unless flydata.credentials.authenticated?
20
20
 
21
- begin
22
- path = choose_log_path_from_examples
23
- case path
24
- when OTHER; ask_log_path
25
- else; create_log_entry(path, ask_log_deletion)
26
- end
27
- newline
28
- end while more_entry?
21
+ # choose entries
22
+ unless shown = show_registered_entries and !(more_entry?)
23
+ begin
24
+ unless shown
25
+ show_registered_entries
26
+ shown = false
27
+ end
28
+ path = choose_log_path_from_examples
29
+ case path
30
+ when OTHER; ask_log_path
31
+ else; create_log_entry(path, ask_log_deletion)
32
+ end
33
+ newline
34
+ end while more_entry?
35
+ end
29
36
 
30
37
  # start client process
31
- sender = Flydata::Command::Sender.new
32
- sender.start
38
+ Flydata::Command::Sender.new.restart
33
39
  end
34
40
 
35
41
  private
42
+ def show_registered_entries
43
+ data_entries = retrieve_data_entries
44
+ @last_fetched_entries = data_entries
45
+ if data_entries and data_entries.size > 0
46
+ puts('Registered entries. ')
47
+ data_entries.each { |data_entry|
48
+ say(" - #{data_entry['name']}\t#{data_entry['log_path']}")
49
+ }
50
+ true
51
+ else
52
+ false
53
+ end
54
+ end
36
55
  def choose_log_path_from_examples
37
56
  candidates = (`ls #{LOG_PATH_EXAMPLES.join(' ')} 2>/dev/null`).split(/\s+/)
57
+ candidates = candidates.find_all{|path| File.readable?(path)}
58
+ if @last_fetched_entries
59
+ candidates = candidates - @last_fetched_entries.map{|v| v['log_path']}
60
+ end
61
+ return OTHER unless candidates.size > 0
38
62
  candidates << OTHER
39
63
  choice = nil
40
64
  say('Please select your log path for sending FlyData')
@@ -4,9 +4,12 @@ module Flydata
4
4
  attr_reader :user, :password
5
5
  def initialize(user=nil, password=nil)
6
6
  read_credentials
7
- unless @authenticated
8
- @user = user || ENV['FLYDATA_LOGIN']
9
- @password = password || ENV['FLYDATA_PASSWORD']
7
+ if user && password
8
+ @user = user
9
+ @password = password
10
+ elsif !(@authenticated)
11
+ @user = ENV['FLYDATA_LOGIN']
12
+ @password = ENV['FLYDATA_PASSWORD']
10
13
  end
11
14
  end
12
15
  def authenticate!
data/lib/flydata/cron.rb CHANGED
@@ -1,14 +1,88 @@
1
1
  module Flydata
2
2
  class Cron
3
- CRON_SETTING_SHELL=File.join(
4
- File.expand_path(File.dirname(__FILE__)),
5
- "flydata_crontab.sh")
3
+ FLYDATA_HOME=Flydata::HOME_DIR
4
+ CRON_TMP_PATH="#{FLYDATA_HOME}/cron_list.tmp"
5
+ CRON_BACKUP_PATH="#{FLYDATA_HOME}/cron_list.back"
6
+ LOG_PATH="#{FLYDATA_HOME}/flydata_cron.log"
7
+ ROUTINE_BASE_COMMAND="#{FLYDATA_HOME}/bin/flydata routine >> #{LOG_PATH} 2>&1"
8
+ if ENV['FLYDATA_API_HOST'] and ENV['FLYDATA_API_HOST'] != ""
9
+ ROUTINE_COMMAND="export FLYDATA_API_HOST=#{ENV['FLYDATA_API_HOST']} && #{ROUTINE_BASE_COMMAND}"
10
+ else
11
+ ROUTINE_COMMAND=ROUTINE_BASE_COMMAND
12
+ end
13
+
14
+ #CRON_LINE="15 4 * * * #{ROUTINE_COMMAND}"
15
+ CRON_LINE="* * * * * #{ROUTINE_COMMAND}"
16
+ REMOVE_OPTION="-r"
17
+
6
18
  def initialize
7
19
  end
8
20
  def update(option=nil)
9
21
  arg = option ? option : ''
10
- ret = system("sh " + CRON_SETTING_SHELL + " " + arg)
11
- raise "Failed to update crontab." unless ret
22
+
23
+ # Check condition
24
+ check_crontab_command
25
+ dump_cron_setting
26
+ reagistered = check_cron_setting
27
+
28
+ # Remove mode
29
+ if arg == REMOVE_OPTION
30
+ unless reagistered
31
+ puts "Skip removing. There are no flydata jobs in crontab."
32
+ else
33
+ remove_cron_setting
34
+ puts "Updated crontab to remove the deletion of your log data."
35
+ end
36
+ # Add mode
37
+ else
38
+ if reagistered
39
+ puts "Skip register. Already cron setup done."
40
+ else
41
+ add_cron_setting
42
+ puts "Updated crontab to manage the deletion of your log data."
43
+ end
44
+ end
45
+ true
46
+ end
47
+
48
+ private
49
+ def check_crontab_command
50
+ raise "Failed to update. Crontab is not installed." unless system("which crontab > /dev/null")
51
+ end
52
+ def dump_cron_setting
53
+ system("crontab -l > #{CRON_BACKUP_PATH}")
54
+ end
55
+ def check_cron_setting
56
+ already_exist = false
57
+ File.open(CRON_BACKUP_PATH, 'r') { |file|
58
+ while l = file.gets
59
+ if l =~ /#{ROUTINE_COMMAND}/
60
+ already_exist = true
61
+ break
62
+ end
63
+ end
64
+ }
65
+ already_exist
66
+ end
67
+ def add_cron_setting
68
+ FileUtils.copy(CRON_BACKUP_PATH, CRON_TMP_PATH)
69
+ File.open(CRON_TMP_PATH, "a") { |file|
70
+ file.puts(CRON_LINE)
71
+ }
72
+ ret = system("crontab #{CRON_TMP_PATH}")
73
+ raise "Failed to register flydata routine job to crontab." unless ret
74
+ end
75
+ def remove_cron_setting
76
+ File.open(CRON_TMP_PATH, "w") { |dst|
77
+ File.open(CRON_BACKUP_PATH, 'r') { |src|
78
+ while l = src.gets
79
+ dst.puts(l) unless l =~ /#{ROUTINE_COMMAND}/
80
+ end
81
+ }
82
+ }
83
+ ret = system("crontab #{CRON_TMP_PATH}")
84
+ raise "Failed to remove flydata routine job from crontab." unless ret
12
85
  end
86
+
13
87
  end
14
88
  end
@@ -28,7 +28,7 @@ module Flydata
28
28
  # initialize paths
29
29
  @flydata_path = File.join(File.dirname(@log_path),
30
30
  FLYDATA_DIR,
31
- File.basename(@log_path))
31
+ File.basename(@log_path) + ".d")
32
32
  @backup_path = File.join(@flydata_path, BACKUP_DIR)
33
33
  @trash_path = File.join(@flydata_path, TRASH_DIR)
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flydata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.2011102401
4
+ version: 0.0.1.2011102601
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-24 00:00:00.000000000 Z
12
+ date: 2012-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -275,7 +275,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
275
275
  version: '0'
276
276
  segments:
277
277
  - 0
278
- hash: -967327673415101597
278
+ hash: 2346901440603012980
279
279
  required_rubygems_version: !ruby/object:Gem::Requirement
280
280
  none: false
281
281
  requirements: