Created
May 19, 2012 07:35
-
-
Save lusis/2729889 to your computer and use it in GitHub Desktop.
The stupid hacky chef-listener I used in my talk at #chefconf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'webmachine' | |
| require 'celluloid' | |
| require 'json' | |
| class ChefRunner | |
| include Celluloid | |
| def initialize(name) | |
| @to_run = 0 | |
| @running = false | |
| `say "Yo dawg. Yer Chef daemon is starting"` | |
| end | |
| def run_chef | |
| if running? | |
| `say Chef already running` | |
| queue_run | |
| return | |
| end | |
| @running = true | |
| queue_run | |
| while @to_run > 0 | |
| `say Running chef` | |
| sleep 10 | |
| `say Chef run done` | |
| dequeue_run | |
| end | |
| if @to_run == 0 | |
| `say Queue empty. Waiting for work` | |
| end | |
| end | |
| def running? | |
| @running ||= false | |
| end | |
| def queue_run | |
| `say Queueing Chef run` | |
| @to_run += 1 | |
| `say Current queue size is: #{@to_run.to_s}` | |
| end | |
| def dequeue_run | |
| `say De-Queueing Chef run` | |
| @to_run = @to_run - 1 | |
| `say Current queue size is: #{@to_run.to_s}` | |
| end | |
| end | |
| class ChefResource < Webmachine::Resource | |
| def content_types_provided | |
| [["application/json", :to_json]] | |
| end | |
| def content_types_accepted | |
| [["application/json", :from_json]] | |
| end | |
| def to_json | |
| '{"foo":"bar"}' | |
| end | |
| def from_json | |
| JSON.parse(request.body.to_s) | |
| end | |
| def allowed_methods | |
| %w{POST GET} | |
| end | |
| def allow_missing_post? | |
| true | |
| end | |
| def process_post | |
| puts from_json | |
| Celluloid::Actor[:chef_runner].run_chef! | |
| true | |
| end | |
| end | |
| Webmachine::Configuration.new do |config| | |
| config.port = 8080 | |
| end | |
| Webmachine.application.dispatcher.add_route([], ChefResource) | |
| ChefRunner.supervise_as :chef_runner, "ChefRunner" | |
| Webmachine.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment