Class: Larrow::Runner::Manager

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
lib/larrow/runner/manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_url) ⇒ Manager

Returns a new instance of Manager.



11
12
13
14
15
# File 'lib/larrow/runner/manager.rb', line 11

def initialize target_url
  signal_trap
  self.vcs = Vcs.detect target_url
  self.app = Model::App.new vcs
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



10
11
12
# File 'lib/larrow/runner/manager.rb', line 10

def app
  @app
end

#vcsObject

Returns the value of attribute vcs.



9
10
11
# File 'lib/larrow/runner/manager.rb', line 9

def vcs
  @vcs
end

Class Method Details

.cleanupObject



101
102
103
104
105
106
107
# File 'lib/larrow/runner/manager.rb', line 101

def self.cleanup
  resource_iterator do |clazz, array|
    clazz.cleanup array
  end
  File.delete ResourcePath rescue nil
  RunLogger.title 'resource cleaned'
end

.resourceObject



94
95
96
97
98
99
# File 'lib/larrow/runner/manager.rb', line 94

def self.resource
  resource_iterator do |clazz, array|
    RunLogger.info clazz.name.split("::").last
    clazz.show array, 1
  end
end

.resource_iteratorObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/larrow/runner/manager.rb', line 109

def self.resource_iterator
  RunLogger.title "load resource from #{ResourcePath}"
  resource = YAML.load(File.read ResourcePath) rescue {}
  
  resource.each_pair do |k,array|
    case k
    when :nodes
      yield Model::Node, array
    end
  end
  RunLogger.detail "no resource on the file" if resource.empty?
end

Instance Method Details

#build_imageObject



32
33
34
35
36
37
# File 'lib/larrow/runner/manager.rb', line 32

def build_image
  handle_exception do
    app.allocate
    app.build_image
  end
end

#build_serverObject



39
40
41
42
43
44
# File 'lib/larrow/runner/manager.rb', line 39

def build_server
  handle_exception do
    app.allocate
    app.deploy
  end
end

#debug?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/larrow/runner/manager.rb', line 70

def debug?
  RunOption.key? :debug
end

#goObject



25
26
27
28
29
30
# File 'lib/larrow/runner/manager.rb', line 25

def go
  handle_exception do
    app.allocate
    app.action :all
  end
end

#handle_exceptionObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/larrow/runner/manager.rb', line 46

def handle_exception
  yield
rescue InvalidConfigFile => e
  data = eval(e.message)
  url = "https://github.com/fsword/larrow-runner/wiki/#{data[:wiki]}"
  RunLogger.err "invalid config file: #{data[:file]}"
  RunLogger.level(1).err "see: #{url}"
rescue => e
  RunOption[:keep] = true if e.is_a?(ExecutionError)
  if e.is_a?(ExecutionError) && !debug?
    data = eval(e.message)
    RunLogger.level(1).err "Execute fail: #{data[:status]}"
    RunLogger.level(1).err "-> #{data[:errmsg]}"
  else
    debug? ? binding.pry : raise(e)
  end
ensure
  if keep?
    store_resource
  else
    release
  end
end

#keep?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/larrow/runner/manager.rb', line 74

def keep?
  RunOption.key? :keep
end

#releaseObject



84
85
86
87
88
89
90
91
92
# File 'lib/larrow/runner/manager.rb', line 84

def release
  RunLogger.title 'release resource'
  begin_at = Time.new
  if app && app.node
    app.node.destroy if @state != :release
  end
  during = sprintf('%.2f', Time.new - begin_at)
  RunLogger.level(1).detail "released(#{during}s)"
end

#signal_trapObject



17
18
19
20
21
22
23
# File 'lib/larrow/runner/manager.rb', line 17

def signal_trap
  trap('INT') do
    RunLogger.title 'try to release'
    release
    ::Kernel.exit
  end
end

#store_resourceObject



78
79
80
81
82
# File 'lib/larrow/runner/manager.rb', line 78

def store_resource
  resource = app.dump
  File.write ResourcePath, YAML.dump(resource)
  RunLogger.title 'store resource'
end