themigrator 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 085e22641bbbe62e08c1e198162675d47b6db9c5
4
- data.tar.gz: 538f05195800115aa28338cc4896378bc57df811
3
+ metadata.gz: c604771ae4c4bbad1daef23d7053daee6690200c
4
+ data.tar.gz: 5511873c95b27ffcd87ab7d86d616d62b9bde47e
5
5
  SHA512:
6
- metadata.gz: ee81f2adb4f68ccb702a32931fbabb4a6030edc2732bc87e1b6ad12d54c803ee4a8f34c6e8a620d6db08fb53ade3618a67d8d7fb2080550f2fc3965518e2d0ad
7
- data.tar.gz: 1e0483923ba73473f685f91cda9a9f132fbb2df7bf67a958ede16f825d5082ee8f3cab1725d1f9a5636236477fc09fcec1898f998c15c95ad58af0aaa0a34dd5
6
+ metadata.gz: 17ded8f0c8343627df08fe6cbe770734d1bf47ba8866d29042a35d65bfe4b6a4b911e9d009022eb4caf7c1b0bf18aa7eb9639bd7b12bcd45cd413111e93667b1
7
+ data.tar.gz: a08b65371dd25033a64ac9c9e27830de346bef3c20d23a204a8585ebd02e8ec770a4f92157b3795e692b1bda143501e32b6a3eee4df68aa29cd80a638d76248d
@@ -8,7 +8,13 @@ module Themigrator
8
8
  desc "migrate", "Migrate the current project"
9
9
  def migrate
10
10
  migrator = Themigrator::Migrator.new(Dir.pwd)
11
- migrator.migrate!
11
+ pid = fork do
12
+ exit migrator.migrate! ? 0 : -1
13
+ end
14
+ Process.detach(pid)
15
+ log_file = migrator.main_log_path(Dir.pwd, migrator.run_id)
16
+ sleep 0.1
17
+ Process.exec("tail --pid=#{pid} --lines=+0 --follow #{log_file}")
12
18
  end
13
19
 
14
20
  desc "roles", "show the list of roles"
@@ -19,6 +19,10 @@ module Themigrator
19
19
  end
20
20
  end
21
21
 
22
+ def close_logger
23
+ @logger.close if @logger
24
+ end
25
+
22
26
  def logger
23
27
  @logger || ::Logger.new(STDOUT)
24
28
  end
@@ -1,11 +1,11 @@
1
-
1
+ require 'set'
2
2
 
3
3
  module Themigrator
4
4
 
5
5
  # Holds the plan of the migration
6
6
  class Migration
7
7
 
8
- ACTIONS = %w(setup pre-migrate migrate cleanup rollback)
8
+ ACTIONS = %w(setup pre-migrate migrate cleanup)
9
9
  SCRIPTS = ACTIONS | %w(rollback)
10
10
 
11
11
  attr_reader :roles, :actions
@@ -7,10 +7,12 @@ module Themigrator
7
7
  class Migrator
8
8
  include Logger
9
9
 
10
+ attr_reader :run_id
10
11
 
11
12
  def initialize(dir)
12
13
  @dir = dir
13
14
  @runned_roles = []
15
+ @run_id = Time.now.strftime("%Y-%m-%d-%H:%M:%S")
14
16
  end
15
17
 
16
18
  def migrate!
@@ -19,19 +21,18 @@ module Themigrator
19
21
 
20
22
  init_logger(main_log_path(@dir, run_id))
21
23
 
22
- log "* [starting]"
23
- catch(:rollback) do
24
- each_action do |action|
25
- log "[step #{action}]"
26
- run_action_and_wait(action)
24
+ log "start PID=#{$$}"
25
+ log "roles: #{@migration.roles.join(" ")}"
26
+ did_it_work = true
27
+ each_action do |action|
28
+ if !run_action_and_wait(action)
29
+ did_it_work = false
30
+ run_rollback_and_wait("rollback")
31
+ return -1
27
32
  end
28
- return true
29
33
  end
30
- run_rollback_and_wait
31
- end
32
-
33
- def run_id
34
- @run_id ||= Time.now.strftime("%Y-%m-%d-%H:%M:%S")
34
+ close_logger
35
+ did_it_work
35
36
  end
36
37
 
37
38
  private
@@ -50,34 +51,29 @@ module Themigrator
50
51
  @runned_roles.uniq!
51
52
  end
52
53
 
53
- def run_rollback_and_wait
54
- script_pool = ScriptPool.new
55
- each_role("rollback") do |role|
56
- next unless @runned_roles.include?(role)
57
- script_path = find_script(role, action)
58
- log_path = log_path(@dir,run_id,role, action)
59
-
60
- log("[running #{action} for #{role}]")
61
-
62
- script_pool.add_script(script_path,log_path)
63
- end
64
- script_pool.run_and_wait
65
- end
66
54
 
67
55
  def run_action_and_wait(action)
56
+ log "#{action} => start"
68
57
  script_pool = ScriptPool.new
69
58
 
70
59
  each_role(action) do |role|
60
+ next if role == "rollback" && !@runned_roles.include?(role)
61
+
71
62
  script_path = find_script(role, action)
72
63
  log_path = log_path(@dir,run_id,role, action)
73
64
 
74
- log("[running #{action} for #{role}]")
75
65
 
76
- script_pool.add_script(script_path,log_path)
66
+ log "#{action} | #{role} => start"
67
+ script_pool.add_script(script_path,log_path, role: role, action: action) do |s|
68
+ action = s.attr[:action]
69
+ role = s.attr[:role]
70
+ log "#{action} | #{role} => exited with #{s.exitcode} (#{s.run_time * 1000} ms)"
71
+ end
77
72
  end
78
73
 
79
- script_pool.run_and_wait or throw :rollback
80
-
74
+ ok = script_pool.run_and_wait
75
+ log("#{action} => #{ok ? "done" : "fail"} (#{script_pool.run_time*1000} ms)")
76
+ ok
81
77
  end
82
78
 
83
79
  def find_script(role, action)
@@ -2,22 +2,44 @@
2
2
 
3
3
  module Themigrator
4
4
  class Script
5
- def initialize(script, log_file)
5
+ attr_reader :script, :log_file, :attr
6
+
7
+ # new creates a new script but doesn't start until called run
8
+ # Arguments:
9
+ # * script : Fullpath of the file to execute
10
+ # * log_file: Fullpath of the file to save the log
11
+ # * attr: Extra attributes that could be handy (not used
12
+ # internally by Script)
13
+ # * finish_cbk: A block that will receive the following arguments
14
+ # 1 The script itself
15
+ def initialize(script, log_file, attr = {}, &finish_cbk)
16
+ require 'byebug'
17
+ @attr = attr
6
18
  @script = script
7
- @log_file = File.open(log_file, "w", 0600)
19
+ @log_file = log_file
20
+ @log_fd = File.open(@log_file, "w", 0600)
8
21
  @wd_dir = File.dirname(script)
9
22
  @pid = nil
10
23
  @thread = nil
24
+ @start_time = Time.now
25
+ @end_time = nil
26
+ @finish_cbk = finish_cbk
27
+ end
28
+
29
+ def run_time
30
+ @end_time - @start_time if @end_time
11
31
  end
12
32
 
13
33
  def run
14
34
  @thread = Thread.new do
15
35
  @pid = Process.spawn(@script,
16
- err: @log_file,
17
- out: @log_file,
36
+ err: @log_fd,
37
+ out: @log_fd,
18
38
  chdir: @wd_dir)
19
39
  pid, status = Process.wait2(@pid)
20
- if status.signaled?
40
+ @end_time = Time.now
41
+ close
42
+ return_code = if status.signaled?
21
43
  signal = Signal.list.find{|k,v|
22
44
  v == status.termsig
23
45
  }
@@ -25,10 +47,18 @@ module Themigrator
25
47
  else
26
48
  status.exitstatus
27
49
  end
50
+
51
+ Thread.new { invoke_cbks } if @finish_cbk
52
+
53
+ return_code
28
54
  end
29
55
  sleep 0
30
56
  end
31
57
 
58
+ def invoke_cbks
59
+ @finish_cbk.call(self)
60
+ end
61
+
32
62
  # Waits for the process to be over
33
63
  def wait(seconds = nil)
34
64
  @thread.join(seconds)
@@ -71,7 +101,7 @@ module Themigrator
71
101
  end
72
102
 
73
103
  def close
74
- @log_file.close
104
+ @log_fd.close
75
105
  end
76
106
 
77
107
  end
@@ -3,14 +3,15 @@ require 'themigrator/script'
3
3
 
4
4
  module Themigrator
5
5
  class ScriptPool
6
+ attr_reader :scripts, :run_time
6
7
  def initialize
7
8
  @queue = Queue.new
8
9
  @lock = Mutex.new
9
10
  @scripts = []
10
11
  end
11
12
 
12
- def add_script(script,log_file)
13
- s = Script.new(script, log_file)
13
+ def add_script(script,log_file, attr = {},&block)
14
+ s = Script.new(script, log_file, attr, &block)
14
15
  @lock.synchronize do
15
16
  @scripts << s
16
17
  end
@@ -22,10 +23,13 @@ module Themigrator
22
23
  # * true: All scripts returned 0
23
24
  # * false: Some of the scripts fail
24
25
  def run_and_wait
26
+ start = Time.now
25
27
  @lock.synchronize do
26
28
  start_scripts
27
29
  collect_result
28
30
  end
31
+ ensure
32
+ @run_time = start - Time.now
29
33
  end
30
34
 
31
35
  private
@@ -2,6 +2,11 @@ require 'curses'
2
2
 
3
3
  module Themigrator
4
4
  module UI
5
+ include Curses
6
+
7
+ def init_ui
8
+ init_screen
9
+ end
5
10
 
6
11
  def render
7
12
  puts "hello"
@@ -1,3 +1,3 @@
1
1
  module Themigrator
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: themigrator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillermo Álvarez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-25 00:00:00.000000000 Z
11
+ date: 2016-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler