Class: Wildcloud::Keeper::Runtime
- Inherits:
-
Object
- Object
- Wildcloud::Keeper::Runtime
- Defined in:
- lib/wildcloud/keeper/runtime.rb
Instance Method Summary collapse
- #clean_system_configuration(options) ⇒ Object
- #get_instance(message) ⇒ Object
- #get_instance_name(message) ⇒ Object
- #handle(message) ⇒ Object
- #handle_build(message) ⇒ Object
- #handle_deploy(message) ⇒ Object
- #handle_quit ⇒ Object
- #handle_resources(message) ⇒ Object
- #handle_undeploy(message) ⇒ Object
-
#initialize ⇒ Runtime
constructor
A new instance of Runtime.
- #instance(id, message) ⇒ Object
- #write_system_configuration(options) ⇒ Object
Constructor Details
#initialize ⇒ Runtime
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.('~/.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() root = [:root_path] if [: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() instance = instance(get_instance_name(), ) = instance[:options] [instance, ] end |
#get_instance_name(message) ⇒ Object
109 110 111 |
# File 'lib/wildcloud/keeper/runtime.rb', line 109 def get_instance_name() ['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() Keeper.logger.debug('Runtime') { "Message received #{message.inspect}" } method = "handle_#{message['type']}".to_sym scope = self @queue << proc do scope.send(method, ) 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() handle_deploy() handle_undeploy() 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() instance, = get_instance() instance[:deployer].deploy() write_system_configuration() instance[:isolator].start() unless [:build] @transport.send({:type => :deployed, :node => Keeper.configuration['node']['name'], :id => ['id']}, :master) end end |
#handle_quit ⇒ Object
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() instance_name = get_instance_name() instance = @repository[instance_name] unless instance return end = instance[:options] [:memory] = ['memory'] [:swap] = ['swap'] [:cpus] = ['cpus'] [:cpu_share] = ['cpu_share'] instance[:isolator].resources() 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() instance, = get_instance() instance[:isolator].stop() clean_system_configuration() if [:build] build_log = File.read(File.join([:root_path], 'var', 'build.log')) end instance[:deployer].undeploy() if [:build] @transport.send({:type => :build_log, :node => Keeper.configuration['node']['name'], :id => ['id'], :content => build_log}, :master) else @transport.send({:type => :undeployed, :node => Keeper.configuration['node']['name'], :id => ['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, ) unless @repository[id] @repository[id] = {:isolator => Isolators::Lxc.new, :deployer => Deployers::Aufs.new} @repository[id][:options] = { :id => "instance_#{message['id']}", :appid => ['appid'], :base_image => ['image'], :persistent => ['persistent'], :ip_address => ['ip_address'], :memory => ['memory'], :swap => ['swap'], :cpus => ['cpus'], :cpu_share => ['cpu_share'] } if ['type'] == 'build' @repository[id][:options].merge!({ :id => "build_#{message['id']}", :persistent => true, :build => true, :repository => ['repository'], :revision => ['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() root = [:root_path] interfaces = ERB.new(File.read(File.('../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 [:build] FileUtils.mkdir_p(File.join(root, 'root', '.ssh')) FileUtils.cp(File.('~/.ssh/id_rsa'), File.join(root, 'root', '.ssh', 'id_rsa')) FileUtils.cp(File.('~/.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()) } end end |