ro_rmt 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +91 -0
  3. data/conf/ro_rmt.rb +203 -0
  4. data/conf/rsync/exclude_patterns +4 -0
  5. data/conf/rsync/include_patterns +10 -0
  6. data/lib/ro_cell/rmt.rb +254 -0
  7. data/lib/ro_cell/rmt/dir.rb +55 -0
  8. data/lib/ro_cell/rmt/file.rb +96 -0
  9. data/lib/ro_cell/rmt/opts.rb +21 -0
  10. data/lib/ro_cell/rmt/se_mgr.rb +51 -0
  11. data/lib/ro_cell/tmp/ro.log +49 -0
  12. data/lib/ro_cmd/ci.rb +32 -0
  13. data/lib/ro_cmd/rmt_base.rb +132 -0
  14. data/lib/ro_cmd/rmt_rspec.rb +26 -0
  15. data/lib/ro_helper/dp.rb +20 -0
  16. data/lib/ro_helper/rmt/basic.rb +441 -0
  17. data/lib/ro_helper/rmt/compress.rb +31 -0
  18. data/lib/ro_helper/rmt/determinations.rb +25 -0
  19. data/lib/ro_helper/rmt/dir.rb +11 -0
  20. data/lib/ro_helper/rmt/file.rb +13 -0
  21. data/lib/ro_helper/rmt/file_dir.rb +60 -0
  22. data/lib/ro_helper/rmt/process.rb +46 -0
  23. data/lib/ro_helper/rmt/rb.rb +131 -0
  24. data/lib/ro_helper/rmt/scp.rb +117 -0
  25. data/lib/ro_helper/rmt/se_attrs.rb +28 -0
  26. data/lib/ro_rmt.rb +9 -0
  27. data/lib/ro_rmt_ro_cmds_bin.rb +4 -0
  28. data/lib/ro_rspec_helper/rmt_rspec_helper.rb +82 -0
  29. data/lib/ro_script/kill_port.rb +49 -0
  30. data/lib/ro_script/pgrep.rb +44 -0
  31. data/lib/ro_script/pkill.rb +51 -0
  32. data/lib/ro_script/try.rb +0 -0
  33. data/lib/tts/nginx.conf.erb +148 -0
  34. data/spec/fixtures/ro_bin_helper/Gemfile +2 -0
  35. data/spec/fixtures/ro_bin_helper/gem1/lib/ro_cmd/cmd1.rb +0 -0
  36. data/spec/fixtures/ro_bin_helper/gem1/lib/ro_cmd/cmd2.rb +0 -0
  37. data/spec/fixtures/ro_bin_helper/gem2/lib/ro_cmd/cmd1.rb +0 -0
  38. data/spec/fixtures/ro_bin_helper/gem2/lib/ro_cmd/cmd2.rb +0 -0
  39. data/spec/fixtures/scp/log/scp_rspec_err.log +216 -0
  40. data/spec/fixtures/scp/tmp/ro.log +110 -0
  41. data/spec/fixtures/scp/tmp/ro_cell__pl__sv__st_event.log +7 -0
  42. data/spec/fixtures/scp/tmp/roroco/at/localhost/home/roroco/try/try_ro_rmt.rb +1 -0
  43. data/spec/fixtures/se_mgr/log/se_mgr_rspec_err.log +207 -0
  44. data/spec/fixtures/se_mgr/tmp/ro.log +1194 -0
  45. data/spec/fixtures/se_mgr/tmp/ro_cell__pl__sv__st_event.log +7 -0
  46. data/spec/helpers/articles_helper_spec.rb +15 -0
  47. data/spec/lib/ro_cell/rmt/dir_spec.rb +22 -0
  48. data/spec/lib/ro_cell/rmt/file_spec.rb +32 -0
  49. data/spec/lib/ro_cell/rmt/se_mgr_spec.rb +33 -0
  50. data/spec/lib/ro_cell/rmt_spec.rb +113 -0
  51. data/spec/lib/ro_dp/cap_spec.rb +34 -0
  52. data/spec/lib/ro_dp/rbenv_spec.rb +10 -0
  53. data/spec/lib/ro_dp/system_spec.rb +31 -0
  54. data/spec/lib/ro_helper/rmt/basic_spec.rb +41 -0
  55. data/spec/lib/ro_helper/rmt/cf_spec.rb +17 -0
  56. data/spec/lib/ro_helper/rmt/determinations_spec.rb +11 -0
  57. data/spec/lib/ro_helper/rmt/dir_spec.rb +15 -0
  58. data/spec/lib/ro_helper/rmt/file_dir_helper_spec.rb +11 -0
  59. data/spec/lib/ro_helper/rmt/file_spec.rb +23 -0
  60. data/spec/lib/ro_helper/rmt/process_spec.rb +29 -0
  61. data/spec/lib/ro_helper/rmt/rb_spec.rb +13 -0
  62. data/spec/lib/ro_helper/rmt/scp_spec.rb +21 -0
  63. data/spec/lib/ro_script/pgrep_spec.rb +18 -0
  64. data/spec/lib/ro_sv_try/1.rb +0 -0
  65. data/spec/lib/ro_sv_try/2.rb +0 -0
  66. data/spec/lib/ro_sv_try/3.rb +0 -0
  67. data/spec/lib/ro_sv_try/tmp/rspec_guard_result +9 -0
  68. data/spec/rails_helper.rb +1 -0
  69. data/spec/spec_helper.rb +35 -0
  70. data/spec/wio_spork.rb +10 -0
  71. metadata +177 -0
@@ -0,0 +1,20 @@
1
+ require File.expand_path("../../../conf/ro_rmt", __FILE__)
2
+ require 'ro_helper/redis'
3
+
4
+ module ::RoHelper
5
+ module Dp
6
+ include Redis
7
+ #@example ro_stage "dp st", *args
8
+
9
+ def run_ro_with_stage(*args, &blk)
10
+ ensure_that(has_meth(:ivk_cmds), or_raise_err: NoMethodError) do
11
+ stage = record('cur_deploy_stage') || "staging"
12
+ implicit_ivk_cmds(*args, '-s', stage)
13
+ end
14
+ end
15
+
16
+ def stage(*args, &blk)
17
+ run_ro_with_stage(*args, &blk)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,441 @@
1
+ require File.expand_path("../../../../conf/ro_rmt", __FILE__)
2
+ require 'ro_err'
3
+ require 'ro_cell/trace'
4
+ require 'net/ssh'
5
+ require 'net/scp'
6
+ require 'ro_helper/thr'
7
+ require 'ro_core_ext/string'
8
+ require 'ro_cell/rmt/se_mgr'
9
+
10
+ require_relative 'scp'
11
+ require_relative 'rb'
12
+
13
+ require 'ro_cell/rmt/opts'
14
+
15
+ module RoHelper
16
+ module Rmt
17
+ module Basic
18
+ include ::RoHelper::Rmt::SeAttrs
19
+ include ::RoHelper::Thr
20
+ include Rb
21
+ include Scp
22
+
23
+ attr_accessor :os
24
+
25
+ # @param [String] _user_at_host such as 'root@localhost'
26
+
27
+ def initialize(_user_at_host=nil, *args, &blk)
28
+ set_opts(*args, &blk)
29
+ # @user_at_host = _user_at_host
30
+ _user_at_host.match(%r{([^\@]+)\@(.*)}) do |m|
31
+ @user = m[1]
32
+ @host = m[2]
33
+ end
34
+
35
+ @user_at_host = _user_at_host
36
+
37
+ begin
38
+ @se ||= Net::SSH.start(host, user, keys: keys, auth_methods: ['publickey'])
39
+ # @se_backup = se.clone
40
+ rescue Net::SSH::Disconnect => e
41
+ raise ::RoErr::Rmt::Discnn, <<-CONTENT
42
+ err:#{e}
43
+ cmd: ssh -Nvvv #{user_at_host}
44
+ CONTENT
45
+ rescue Net::SSH::AuthenticationFailed => e
46
+ raise ::RoErr::Rmt::AuthFailed, <<-CONTENT
47
+ run "ssh -vvv #{user_at_host}" fail at #{Pj.user}@localhost, it's probably caused by #{RoRmt.home(".ssh/id_rsa")} doesn't exist or wrong, please chk which is wrong
48
+
49
+ solution:
50
+ run "ssh -vvv #{user_at_host}" on #{Pj.user}@localhost
51
+ if you really don't know which is wrong, please rm #{RoRmt.home(".ssh/id_rsa")}, and run `ssh-keygen -t rsa` again
52
+ CONTENT
53
+ end
54
+
55
+ register_on_mgr
56
+
57
+ if se.nil?
58
+ raise ::RoErr::Nil, "se is nil, please set it"
59
+ else
60
+ se
61
+ end
62
+ end
63
+
64
+
65
+ # def reset_se(*args, &blk)
66
+ # se.close
67
+ # @se = se_backup.clone
68
+ # end
69
+
70
+ def keys
71
+ [Pj.home(".ssh/id_rsa"), "/home/roroco/.ssh/id_rsa"]
72
+ end
73
+
74
+ def handle_auth_failed(*args, &blk)
75
+ begin
76
+ Out.notify("auth #{user_at_host} failed, try `ssh -vvv #{user_at_host}` again, see console for what err is raised ", icon: "failed")
77
+ Net::SSH.start(host, user, keys: [RoRmt.home(".ssh/id_rsa")], auth_methods: ['publickey'], verbose: :dbg)
78
+ rescue Net::SSH::AuthenticationFailed => e
79
+ raise e
80
+ end
81
+ end
82
+
83
+ def register_on_mgr(*args, &blk)
84
+ last_meth = ro_trace.last.not(file: __FILE__)
85
+ se.instance_variable_set(:"@inited_at", last_meth.location)
86
+ se.instance_variable_set(:"@host", host)
87
+ se.instance_variable_set(:"@user", user)
88
+ se.instance_variable_set(:"@user_at_host", user_at_host)
89
+
90
+ se.class_eval do
91
+ attr_accessor :inited_at, :host, :user
92
+
93
+ def user_at_host
94
+ "#{user}@#{host}"
95
+ end
96
+ end
97
+
98
+ se_mgr.register se
99
+ end
100
+
101
+ def se_mgr(*args, &blk)
102
+ ::RoCell::Rmt::SeMgr
103
+ end
104
+
105
+ def out_rmt_stdout(ctn, *args, &blk)
106
+ if ctn
107
+ ctn.split("\n").each do |line|
108
+ final_line = "#{' '*4}#{line}"
109
+ Out.out final_line
110
+ end
111
+ end
112
+ end
113
+
114
+ def exe(*args, &blk)
115
+ r = capture(*args, &blk)
116
+ r
117
+ end
118
+
119
+ attr_accessor :within_dir
120
+
121
+ def within(dir, *args, &blk)
122
+ tmply_set :within_dir, dir do
123
+ yield self
124
+ end
125
+ end
126
+
127
+ def opts_tmply_set(*args, &blk)
128
+ ::RoCell::Rmt::Opts.tmply_set *args, &blk
129
+ end
130
+
131
+ def capture(*args, &blk)
132
+ args_clone, opts = args.clone.ro_opts
133
+
134
+ cmd = args.join(" ")
135
+ msg = opts[:status] || cmd
136
+
137
+ if msg.len > 100
138
+ msg = "\n#{msg}"
139
+ end
140
+
141
+ Out.status("run", "'#{msg.strip}' on #{user_at_host}")
142
+
143
+ out = nil
144
+ clr = caller.clone
145
+ stderr_lines = []
146
+
147
+ ch = se.open_channel do |ch|
148
+ ch.exec(cmd) do |ch, success|
149
+ raise "could not execute command: #{command.inspect}" unless success
150
+
151
+ ch.on_data do |ch, data|
152
+ unless ::RoCell::Rmt::Opts.shutup
153
+ puts data
154
+ end
155
+ out = data
156
+ end
157
+
158
+ ch.on_extended_data do |ch, type, data|
159
+ if data.len > 100
160
+ err_msg = "\nerr_msg:\n#{data}"
161
+ else
162
+ err_msg = "err_msg:#{data}"
163
+ end
164
+
165
+ stderr_lines << data
166
+ end
167
+ end
168
+ end
169
+
170
+ ch.wait
171
+ ch.close
172
+
173
+ unless ::RoCell::Rmt::Opts.ignore_err or stderr_lines.empty?
174
+ if stderr_lines.any? do |l|
175
+ l.match(%r{\tfrom})
176
+ end
177
+ bt = []
178
+ err_msg_arr = []
179
+ stderr_lines.each do |l|
180
+ if l.match(%r{\tfrom})
181
+ l.split("\n").each do |_l|
182
+ bt << _l.gsub(%r{^\tfrom\s+}, "")
183
+ end
184
+ else
185
+ err_msg_arr << l
186
+ end
187
+ end
188
+
189
+ e = RoErr::RmtErr.new(Out.uncolor(err_msg_arr.join), backtrace: bt.map(&:strip).hits)
190
+ else
191
+ e = RoErr::RmtErr.new(Out.uncolor(stderr_lines.join("\n")), backtrace: clr)
192
+ end
193
+
194
+ raise e
195
+ end
196
+
197
+ out
198
+ end
199
+
200
+ def listened?(*args, &blk)
201
+ rb3 <<-CONTENT
202
+ require 'socket'
203
+ begin
204
+ TCPServer.new('localhost', '23302')
205
+ puts false
206
+ rescue Errno::EADDRINUSE => e
207
+ puts true
208
+ end
209
+ CONTENT
210
+ end
211
+
212
+ def vagrant?
213
+ user == 'vagrant'
214
+ end
215
+
216
+ def mkdir(*args, &blk)
217
+ create_dir(*args, &blk)
218
+ end
219
+
220
+ def bash(*args, &blk)
221
+ if within_dir
222
+ exe(*["cd #{within_dir};", args].hits, &blk)
223
+ else
224
+ exe(*args, &blk)
225
+ end
226
+ end
227
+
228
+ def build_err(e, rescued_err)
229
+ err = lambda do
230
+ if e.inspect.match(%r{SSHKit})
231
+ msg = <<-CONTENT
232
+
233
+ #{rescued_err.message}
234
+ since
235
+ \"#{e.inspect}\"
236
+ SSHKit.conf.output_verbosity:#{SSHKit.cf.output_verbosity}
237
+ CONTENT
238
+
239
+ bt = rescued_err.backtrace
240
+ e.set_backtrace(bt)
241
+ e.class_eval do
242
+ define_method(:message) do |*args, &blk|
243
+ msg
244
+ end
245
+ end
246
+ e
247
+ elsif e.inspect.match(%r{Net\:\:SSH})
248
+ msg = <<-CONTENT
249
+ #{rescued_err.message}
250
+ since
251
+ \"#{e.inspect}\"
252
+ SSHKit::Backend::Netssh.conf.ssh_options:#{SSHKit::Backend::Netssh.cf.ssh_options}
253
+ CONTENT
254
+ bt = rescued_err.backtrace
255
+ e.set_backtrace(bt)
256
+ e.class_eval do
257
+ define_method(:message) do |*args, &blk|
258
+ msg
259
+ end
260
+ end
261
+ e
262
+ else
263
+ e.set_backtrace(rescued_err.backtrace)
264
+ e
265
+ end
266
+ end.call
267
+ end
268
+
269
+ def bash_shutup(*args, &blk)
270
+ ::RoCell::Rmt::Opts.tmply_set :shutup do
271
+ capture(*args, &blk)
272
+ end
273
+ end
274
+
275
+ def cmd(*args, &blk)
276
+ if within_dir
277
+ args = ["cd #{within_dir}", *args].flatten
278
+ bash *args, &blk
279
+ else
280
+ bash *args, &blk
281
+ end
282
+ end
283
+
284
+ def cd_bash(*args, &blk)
285
+ first_arg = args.shift
286
+ new_first_arg = "cd #{within_dir}; #{first_arg}"
287
+ bash(*[new_first_arg, args].flatten, &blk)
288
+ end
289
+
290
+ def cmd_force(*args, &blk)
291
+ begin
292
+ cmd(*args, &blk)
293
+ rescue SSHKit::Runner::ExecuteError => e
294
+ end
295
+ end
296
+
297
+ def cmdf(*args, &blk)
298
+ cmd_force(*args, &blk)
299
+ end
300
+
301
+ alias_method :cmdf, :cmd_force
302
+
303
+ def capture_shutup(*args, &blk)
304
+ capture(*args, &blk)
305
+ end
306
+
307
+ def cmd_recued_err(args, bt)
308
+ args = args.flatten.select do |arg|
309
+ !arg.blank?
310
+ end
311
+
312
+ ori_cmd = args.join("; ")
313
+ cmd = _format(ori_cmd)
314
+ msg = "run `#{ori_cmd}` fail"
315
+ rescued_err = RoErr::DeployErr.new(msg, bt)
316
+ return cmd, rescued_err
317
+ end
318
+
319
+ def pj_bash(cmd)
320
+ cmd "cd #{::RoRmt.rmt_rt} && #{cmd}"
321
+ end
322
+
323
+ def format(*cmds)
324
+ r = _format(cmds)
325
+ out_run_cmd(r)
326
+ r
327
+ end
328
+
329
+ def out_run_cmd(r)
330
+ Out.status("running", "#{r} on #{stage || ::RoRmt.cur_role}")
331
+ end
332
+
333
+ def _format(*cmds)
334
+ cmds_flatten_join = cmds.flatten.join("; ")
335
+ "#{prefix} #{cmds_flatten_join}"
336
+ cmds_flatten_join
337
+ end
338
+
339
+ def prefix
340
+ @prefix ||= "PATH=~/.rbenv/shims:~/.rbenv/bin:$PATH &&"
341
+ end
342
+
343
+ def rmt_output
344
+ @remote_output ||= "/tmp/ro_rmt.log"
345
+ end
346
+
347
+ def sudo(*cmds)
348
+ cmd "sudo #{cmds.flatten.join(" ")}"
349
+ end
350
+
351
+ def bashes_in_details(*cmds)
352
+ result = nil
353
+ on stage do
354
+ cmds.each do |cmd|
355
+ exe cmd
356
+ end
357
+ end
358
+ result
359
+ end
360
+
361
+ def bundle_exec(*args, &blk)
362
+ begin
363
+ r = bash "ro_stage=prod bundle exec #{args.join(" ")}"
364
+ if r.match(%r{Could not locate Gemfile})
365
+ raise ::RoErr::RmtErr, "#{r.strip} on #{user_at_host}"
366
+ end
367
+ r
368
+ rescue ::RoErr::RmtErr => e
369
+ loc = ro_trace.last_location_not(file: __FILE__)
370
+ raise ::RoErr::RmtErr, e.to_s, (e.backtrace + [loc])
371
+ end
372
+ end
373
+
374
+ # @param [String] to to dir in rmt, such as root@rmt
375
+
376
+ def rsync(*args, &blk)
377
+ files, opts = args.ro_opts
378
+ parents = files.map do |f|
379
+ RoFile.parent(f).gsub(Pj.home, home)
380
+ end
381
+
382
+ ms_parents_str = "mkdir -p #{parents.join(" ")}"
383
+
384
+ cmd = "rsync --rsync-path='#{ms_parents_str}; rsync' -avzr --force #{files.join(" ")} -e 'ssh -p #{port} -i #{Pj.home(".vagrant.d/insecure_private_key")}' #{user_at_host}:#{opts[:to].gsub(Pj.home, home)} --include-from=#{RoRmt.conf("rsync/include_patterns")} --exclude-from=#{RoRmt.conf "rsync/exclude_patterns"}"
385
+ Bash.bash cmd
386
+ end
387
+
388
+ def rake(*args, &blk)
389
+ bundle_exec "rake", *args
390
+ end
391
+
392
+ def rk(*args, &blk)
393
+ rake(*args, &blk)
394
+ end
395
+
396
+ def bxro(bash, *args, &blk)
397
+ bundle_exec "ro #{bash}", *args
398
+ end
399
+
400
+ def ro(*args, &blk)
401
+ bxro(*args, &blk)
402
+ end
403
+
404
+ def home(*args, &blk)
405
+ _home = lambda do
406
+ if user == 'root'
407
+ "/root"
408
+ else
409
+ "/home/#{user}"
410
+ end
411
+ end.call
412
+
413
+ RoFile.join _home, *args, &blk
414
+ end
415
+
416
+ def format_path(path)
417
+ r = path.gsub(%r{^\~}, home)
418
+ r = r.gsub(%r{\/{2,}}, "\/")
419
+ r
420
+ end
421
+
422
+ def boolean?(r)
423
+ if r.is_a?(String)
424
+ r.match(%r{^[\n\s]*(true|false)[\n\s]*$})
425
+ elsif r == true or r == false
426
+ r
427
+ else
428
+ false
429
+ end
430
+ end
431
+
432
+ def eval_result_file
433
+ @rb_eval_result_file ||= "/tmp/rb_eval_result.yml"
434
+ end
435
+
436
+ def capture_eval(*args, &blk)
437
+ capture(create_lc_script(*args, &blk))
438
+ end
439
+ end
440
+ end
441
+ end