Class: Wildcloud::Keeper::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/wildcloud/keeper/runtime.rb

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Returns a new instance of Runtime.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wildcloud/keeper/runtime.rb', line 31

def initialize
  @repository = {}

  Keeper.logger.info('Runtime') { 'Starting transport' }
  @transport = Transport::Amqp.new

  Keeper.logger.info('Runtime') { 'Starting thread-pool' }
  @queue = Queue.new
  @thread_pool = []

  Keeper.configuration['workers'].times do |i|
    Keeper.logger.debug('Runtime', "Starting thread ##{i}")
    Thread.new(i) do |id|
      Thread.current.abort_on_exception = false
      loop do
        Keeper.logger.debug('Runtime') { "Thread ##{id} waiting for task" }
        begin
          @queue.pop.call
        rescue Exception => exception
          Keeper.logger.fatal('Runtime') { "Exception in thread #{id}: #{exception.message}" }
          Keeper.logger.debug('Runtime') { exception }
          Keeper.logger.debug('Runtime') { exception.backtrace }
        else
          Keeper.logger.debug('Runtime') { "Thread ##{id} handled task successfully" }
        end
      end
    end
  end

  handler = self.method(:handle)
  @transport.start(&handler)
  @transport.send({:type => :sshkey, :node => Keeper.configuration['node']['name'], :key => File.read(File.expand_path('~/.ssh/id_rsa.pub')).strip}, :master)
end

Instance Method Details

#clean_system_configuration(options) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/wildcloud/keeper/runtime.rb', line 175

def clean_system_configuration(options)
  root = options[:root_path]

  if options[:build]
    FileUtils.rm_rf(File.join(root, 'root', '.ssh'))
  end
end

#get_instance(message) ⇒ Object



113
114
115
116
117
# File 'lib/wildcloud/keeper/runtime.rb', line 113

def get_instance(message)
  instance = instance(get_instance_name(message), message)
  options = instance[:options]
  [instance, options]
end

#get_instance_name(message) ⇒ Object



109
110
111
# File 'lib/wildcloud/keeper/runtime.rb', line 109

def get_instance_name(message)
  message['type'] == 'build' ? "build_#{message['id']}" : "instance_#{message['id']}"
end

#handle(message) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wildcloud/keeper/runtime.rb', line 65

def handle(message)
  Keeper.logger.debug('Runtime') { "Message received #{message.inspect}" }
  method = "handle_#{message['type']}".to_sym
  scope = self
  @queue << proc do
    scope.send(method, message)
  end
rescue Exception => exception
  Keeper.logger.fatal('Runtime') { "Exception in runtime: #{exception.message}" }
  Keeper.logger.fatal(exception)
end

#handle_build(message) ⇒ Object



77
78
79
80
# File 'lib/wildcloud/keeper/runtime.rb', line 77

def handle_build(message)
  handle_deploy(message)
  handle_undeploy(message)
end

#handle_deploy(message) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/wildcloud/keeper/runtime.rb', line 119

def handle_deploy(message)
  instance, options = get_instance(message)
  instance[:deployer].deploy(options)
  write_system_configuration(options)
  instance[:isolator].start(options)
  unless options[:build]
    @transport.send({:type => :deployed, :node => Keeper.configuration['node']['name'], :id => message['id']}, :master)
  end
end

#handle_quitObject



183
184
185
186
# File 'lib/wildcloud/keeper/runtime.rb', line 183

def handle_quit
  Keeper.logger.fatal('Runtime') { "Shutdown requested." }
  EventMachine.stop_event_loop
end

#handle_resources(message) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/wildcloud/keeper/runtime.rb', line 144

def handle_resources(message)
  instance_name = get_instance_name(message)
  instance = @repository[instance_name]
  unless instance
    return
  end
  options = instance[:options]
  options[:memory] = message['memory']
  options[:swap] = message['swap']
  options[:cpus] = message['cpus']
  options[:cpu_share] = message['cpu_share']
  instance[:isolator].resources(options)
end

#handle_undeploy(message) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/wildcloud/keeper/runtime.rb', line 129

def handle_undeploy(message)
  instance, options = get_instance(message)
  instance[:isolator].stop(options)
  clean_system_configuration(options)
  if options[:build]
    build_log = File.read(File.join(options[:root_path], 'var', 'build.log'))
  end
  instance[:deployer].undeploy(options)
  if options[:build]
    @transport.send({:type => :build_log, :node => Keeper.configuration['node']['name'], :id => message['id'], :content => build_log}, :master)
  else
    @transport.send({:type => :undeployed, :node => Keeper.configuration['node']['name'], :id => message['id']}, :master)
  end
end

#instance(id, message) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wildcloud/keeper/runtime.rb', line 82

def instance(id, message)
  unless @repository[id]
    @repository[id] = {:isolator => Isolators::Lxc.new, :deployer => Deployers::Aufs.new}
    @repository[id][:options] = {
        :id => "instance_#{message['id']}",
        :appid => message['appid'],
        :base_image => message['image'],
        :persistent => message['persistent'],
        :ip_address => message['ip_address'],
        :memory => message['memory'],
        :swap => message['swap'],
        :cpus => message['cpus'],
        :cpu_share => message['cpu_share']
    }
    if message['type'] == 'build'
      @repository[id][:options].merge!({
          :id => "build_#{message['id']}",
          :persistent => true,
          :build => true,
          :repository => message['repository'],
          :revision => message['revision']
      })
    end
  end
  @repository[id]
end

#write_system_configuration(options) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/wildcloud/keeper/runtime.rb', line 158

def write_system_configuration(options)
  root = options[:root_path]

  interfaces = ERB.new(File.read(File.expand_path('../templates/interfaces', __FILE__))).result(binding)
  interfaces_path = File.join(root, 'etc', 'network', 'interfaces')
  FileUtils.mkdir_p(File.dirname(interfaces_path))
  File.open(interfaces_path, 'w') { |file| file.write(interfaces) }

  if options[:build]
    FileUtils.mkdir_p(File.join(root, 'root', '.ssh'))
    FileUtils.cp(File.expand_path('~/.ssh/id_rsa'), File.join(root, 'root', '.ssh', 'id_rsa'))
    FileUtils.cp(File.expand_path('~/.ssh/id_rsa.pub'), File.join(root, 'root', '.ssh', 'id_rsa.pub'))

    File.open(File.join(root, 'root', 'build.yml'), 'w') { |file| file.write(YAML::dump(options)) }
  end
end