flydata 0.0.3.8 → 0.0.4.0

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.3.8
1
+ 0.0.4.0
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.3.8"
8
+ s.version = "0.0.4.0"
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 = "2013-01-10"
12
+ s.date = "2013-02-13"
13
13
  s.description = "FlyData Command Line Interface"
14
14
  s.email = "sysadmin@flydata.co"
15
15
  s.executables = ["flydata"]
@@ -10,6 +10,10 @@ module Flydata
10
10
  # currently one user has one data port
11
11
  self.list.first
12
12
  end
13
+ def redshift_table_list
14
+ url_path = "#{@url_path}/redshift_table_list"
15
+ @client.get(url_path)
16
+ end
13
17
  end
14
18
  end
15
19
  end
data/lib/flydata/cli.rb CHANGED
@@ -19,7 +19,10 @@ module Flydata
19
19
  #raise e
20
20
  puts "! #{e.to_s}"
21
21
  puts
22
+ puts
23
+ puts '-' * 64
22
24
  print_usage
25
+ puts '-' * 64
23
26
  end
24
27
  end
25
28
 
@@ -43,7 +46,6 @@ please setup flydata again by following commands.
43
46
 
44
47
  You can check the logs of sender(flydata) process.
45
48
  Log path: ~/.flydata/flydata.log
46
-
47
49
  EOM
48
50
  end
49
51
  end
@@ -22,9 +22,14 @@ module Flydata
22
22
 
23
23
  # print console
24
24
  def newline; puts end
25
- def ask_yes_no(message)
25
+ def separator(str="=")
26
+ puts str * 64
27
+ end
28
+ def ask_yes_no(message, default_yes=true)
29
+ suffix = default_yes ? "(Y/n)" : "(y/n)"
26
30
  loop do
27
- ans = ask("#{message} (yes/no): ")
31
+ ans = ask("#{message} #{suffix}: ")
32
+ return true if default_yes and ans == ''
28
33
  if ans.size > 0
29
34
  case ans[0].downcase
30
35
  when 'y'; return true
@@ -35,6 +40,18 @@ module Flydata
35
40
  newline
36
41
  end
37
42
  end
43
+ def choose_one(message, prompt, menu_list, value_list=[])
44
+ choice = nil
45
+ value_list = menu_list unless menu_list.size == value_list.size
46
+ say(message) if message
47
+ choose do |menu|
48
+ menu.index = :number
49
+ menu.index_suffix = ") "
50
+ menu.prompt = prompt ? prompt : ">> "
51
+ menu.choices(*menu_list) {|item| choice = value_list[menu_list.index(item)]}
52
+ end
53
+ choice
54
+ end
38
55
  end
39
56
  end
40
57
  end
@@ -15,9 +15,94 @@ module Flydata
15
15
  end
16
16
 
17
17
  def run
18
- #login
19
18
  Flydata::Command::Login.new.run unless flydata.credentials.authenticated?
19
+ ret = _run
20
+ Flydata::Command::Sender.new.restart if ret
21
+ end
22
+
23
+ private
24
+ def _run
25
+ ret = nil
26
+ #ask redshift
27
+ case ask_data_entry_type
28
+ when :redshift
29
+ ret = start_redshift_mode
30
+ else
31
+ ret = start_s3_backup_mode
32
+ end
33
+ ret
34
+ end
35
+ def ask_data_entry_type
36
+ choose_one('Please select a setup type', nil,
37
+ ["Redshift and S3 Backup", "S3 Buckup only"],
38
+ [:redshift, :s3backup])
39
+ end
40
+
41
+ #### redshift buckup mode
42
+ def start_redshift_mode
43
+ newline
44
+ shown = show_registered_entries('RedshiftFileDataEntry') do |de|
45
+ say(" - #{de['display_name']}: \t#{de['log_path']} -> #{de['redshift_table_name']}")
46
+ end
47
+ newline
48
+ ask_adding_new_redshift
49
+ end
50
+
51
+ def ask_adding_new_redshift
52
+ puts "Start to register a new entry."
53
+ newline
54
+ # select table on redshift
55
+ puts "[Select a Table Name on Redshift]"
56
+ table_name = ask_redshift_table_name
57
+ return unless table_name
58
+ newline
59
+ # enter the log path
60
+ puts "[Enter a Local Log Path]"
61
+ puts "Enter the absolute path of your local log for uploading to '#{table_name}' table on Redshift."
62
+ log_path = ask_log_path("Log path (tab:completion, return:cancel) >> ")
63
+ return unless log_path and not log_path.empty?
64
+ newline
65
+ # select the log type
66
+ puts "[Select a Log Format]"
67
+ log_file_type = choose_log_file_type(log_path)
68
+ # confirm and save
69
+ newline
70
+ puts "[Confirm]"
71
+ separator
72
+ puts " table(redshift) -> #{table_name}"
73
+ puts " local log path -> #{log_path}"
74
+ puts " log file format -> #{log_file_type}"
75
+ separator
76
+ return unless ask_yes_no("Register a new entry?", true)
77
+ create_redshift_log_entry(log_path, log_file_type, table_name)
78
+ end
79
+
80
+ def choose_log_file_type(target_path)
81
+ choose_one("Please select your log file format of (#{target_path})", nil,
82
+ %w(csv tsv json))
83
+ end
84
+
85
+ def ask_redshift_table_name
86
+ ret = flydata.data_port.redshift_table_list
87
+ if ret['table_list'].size < 1
88
+ raise "Could not find tables on Redshift. Please create tables on your Redshift cluster."
89
+ end
90
+ choose_one("Please select the table name on Redshift to store your local data.", nil, ret['table_list'])
91
+ end
92
+
93
+ def create_redshift_log_entry(log_path, log_file_type, redshift_table_name)
94
+ data_port = flydata.data_port.get
95
+ flydata.data_entry.create(
96
+ data_port_id: data_port['id'],
97
+ log_path: log_path,
98
+ log_file_type: log_file_type,
99
+ redshift_table_name: redshift_table_name)
100
+ say("Process added successfuly!")
101
+ return true
102
+ end
20
103
 
104
+ #### s3 buckup mode
105
+ def start_s3_backup_mode
21
106
  # choose entries
22
107
  unless (shown = show_registered_entries)
23
108
  list = build_recommended_entries
@@ -32,25 +117,30 @@ module Flydata
32
117
  newline
33
118
  end while more_entry?
34
119
  end
35
-
36
- # start client process
37
- Flydata::Command::Sender.new.restart
120
+ return true
38
121
  end
39
122
 
40
- private
41
- def show_registered_entries
123
+ def show_registered_entries(type='FileDataEntry', &block)
42
124
  data_entries = retrieve_data_entries
43
125
  @last_fetched_entries = data_entries
126
+ data_entries = data_entries.select {|de| de['type'] == type}
44
127
  if data_entries and data_entries.size > 0
45
- puts('Registered entries. ')
128
+ puts('Registered entries on FlyData. ')
129
+ separator
46
130
  data_entries.each { |data_entry|
47
- say(" - #{data_entry['display_name']}\t#{data_entry['log_path']}")
131
+ if block_given?
132
+ yield data_entry
133
+ else
134
+ say(" - #{data_entry['display_name']}\t#{data_entry['log_path']}")
135
+ end
48
136
  }
137
+ separator
49
138
  true
50
139
  else
51
140
  false
52
141
  end
53
142
  end
143
+
54
144
  def choose_log_path_from_examples
55
145
  candidates = (`ls #{LOG_PATH_EXAMPLES.join(' ')} 2>/dev/null`).split(/\s+/)
56
146
  candidates = candidates.find_all{|path| File.readable?(path)}
@@ -71,22 +161,12 @@ module Flydata
71
161
  newline
72
162
  choice
73
163
  end
74
- def ask_log_path
75
- path = nil
76
- loop do
77
- path = Readline.readline("Enter the absolute path of your log (return to cancel): ")
78
- return if path.empty?
79
- if not (FileTest.file?(path) and FileTest.readable?(path))
80
- say(" ! #{path} is not a readable file!")
81
- elsif @last_fetched_entries and @last_fetched_entries.any?{|e| e['log_path'] == path}
82
- say(" ! #{path} has been registered already.")
83
- else
84
- break
85
- end
86
- newline
87
- end
164
+
165
+ def ask_and_create_data_entry
166
+ path = ask_log_path
88
167
  create_log_entry(path)
89
168
  end
169
+
90
170
  def build_recommended_entries
91
171
  path_options=[]
92
172
  Dir['/var/log*/**/*log'].each{|f| if (FileTest.file?(f) and
@@ -96,6 +176,7 @@ module Flydata
96
176
  then path_options << f end}
97
177
  path_options
98
178
  end
179
+
99
180
  def print_recommended(list, options=false)
100
181
  newline
101
182
  puts " Recommended list:"
@@ -107,12 +188,14 @@ module Flydata
107
188
  end
108
189
  newline
109
190
  end
191
+
110
192
  def register_all(list)
111
193
  list.each{ |item|
112
194
  create_log_entry(item)
113
195
  }
114
196
  list.reject!{|x| x}
115
197
  end
198
+
116
199
  def choose_log_selection(list)
117
200
  path = nil
118
201
  list = build_recommended_entries unless list
@@ -120,14 +203,14 @@ module Flydata
120
203
 
121
204
  loop do
122
205
  if path_options.empty?
123
- ask_log_path
206
+ ask_and_create_data_entry
124
207
  return
125
208
  end
126
209
  print_recommended(path_options, true)
127
210
  choice = Readline.readline("Here are some common logs, enter the number next to the logs to add the log. Input nothing to cancel.")
128
211
  return if choice.empty?
129
212
  if choice.to_i==path_options.length+1
130
- ask_log_path
213
+ ask_and_create_data_entry
131
214
  return
132
215
  elsif (path_options[choice.to_i-1] != nil )
133
216
  path = path_options[choice.to_i-1]
@@ -139,6 +222,7 @@ module Flydata
139
222
  end
140
223
  create_log_entry(path)
141
224
  end
225
+
142
226
  def ask_log_deletion(path)
143
227
  unless File.writable?(path)
144
228
  say("Skip log deletion setting...")
@@ -153,14 +237,34 @@ module Flydata
153
237
  say("For more details - http://docs.hapyrus.com/faq/how-log-deletion-works/")
154
238
  ask_yes_no("Set auto log deletion mode?")
155
239
  end
240
+
156
241
  def create_log_entry(path, log_deletion=false)
157
242
  data_port = flydata.data_port.get
158
243
  flydata.data_entry.create(data_port_id: data_port['id'], log_path: path, log_deletion: log_deletion)
159
244
  say("Process added successfuly!") if flydata.response.code == 200
160
245
  end
246
+
161
247
  def more_entry?
162
248
  ask_yes_no("Do you want to add another log path?")
163
249
  end
250
+
251
+ def ask_log_path(message = nil)
252
+ path = nil
253
+ message = "Enter the absolute path of your log (return to cancel): " unless message
254
+ loop do
255
+ path = Readline.readline(message)
256
+ return if path.empty?
257
+ if not (FileTest.file?(path) and FileTest.readable?(path))
258
+ say(" ! #{path} is not a readable file!")
259
+ elsif @last_fetched_entries and @last_fetched_entries.any?{|e| e['log_path'] == path}
260
+ say(" ! #{path} has been registered already.")
261
+ else
262
+ break
263
+ end
264
+ newline
265
+ end
266
+ path
267
+ end
164
268
  end
165
269
  end
166
270
  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.3.8
4
+ version: 0.0.4.0
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: 2013-01-10 00:00:00.000000000 Z
12
+ date: 2013-02-13 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: 4102998954962470010
278
+ hash: -2366646516811496353
279
279
  required_rubygems_version: !ruby/object:Gem::Requirement
280
280
  none: false
281
281
  requirements: