Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: luxflux/cloudbalancer
base: f83decf767
...
head fork: luxflux/cloudbalancer
compare: 031b83174e
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
1  .rspec
@@ -0,0 +1 @@
+--color
View
52 .rvmrc
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+
+# This is an RVM Project .rvmrc file, used to automatically load the ruby
+# development environment upon cd'ing into the directory
+
+# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
+# Only full ruby name is supported here, for short names use:
+# echo "rvm use 1.9.3" > .rvmrc
+environment_id="ruby-1.9.3-head@cloudbalancer"
+
+# Uncomment the following lines if you want to verify rvm version per project
+# rvmrc_rvm_version="1.11.2" # 1.10.1 seams as a safe start
+# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
+# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
+# return 1
+# }
+
+# First we attempt to load the desired environment directly from the environment
+# file. This is very fast and efficient compared to running through the entire
+# CLI and selector. If you want feedback on which environment was used then
+# insert the word 'use' after --create as this triggers verbose mode.
+if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
+then
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
+ if [[ $- == *i* ]] # check for interactive shells
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
+ fi
+else
+ # If the environment file has not yet been created, use the RVM CLI to select.
+ rvm --create use "$environment_id" || {
+ echo "Failed to create RVM environment '${environment_id}'."
+ return 1
+ }
+fi
+
+# If you use bundler, this might be useful to you:
+# if [[ -s Gemfile ]] && {
+# ! builtin command -v bundle >/dev/null ||
+# builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
+# }
+# then
+# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
+# gem install bundler
+# fi
+# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
+# then
+# bundle install | grep -vE '^Using|Your bundle is complete'
+# fi
View
30 Gemfile.lock
@@ -0,0 +1,30 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ amq-client (0.9.2)
+ amq-protocol (>= 0.9.0)
+ eventmachine
+ amq-protocol (0.9.0)
+ amqp (0.9.4)
+ amq-client (~> 0.9.2)
+ amq-protocol (>= 0.9.0)
+ eventmachine
+ diff-lcs (1.1.3)
+ eventmachine (0.12.10)
+ json (1.6.6)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ amqp
+ json
+ rspec
View
2  lib/cloud_balancer.rb
@@ -1,3 +1,5 @@
module CloudBalancer
+ autoload :Server, 'cloud_balancer/server'
+
end
View
69 lib/cloud_balancer/server.rb
@@ -1,4 +1,73 @@
module CloudBalancer
class Server
+
+ class UnknownMessageType < RuntimeError; end
+ class MessageTypeNotSpecified < RuntimeError; end
+ class UnknownService < RuntimeError; end
+
+ attr_reader :services
+
+ def initialize()
+ # TODO: load services from config
+ @services = { :www => { :nodes => [] } }
+ # TODO: load password from config
+ @config = { :cluster_password => "testpass" }
+ end
+
+ # Starting point for a new message
+ #
+ # @param [OpenStruct] metadata The metadata of this message
+ # @param [String] payload The message content
+ #
+ def handle_message(metadata, payload)
+ message = parse_payload(metadata.content_type, payload)
+ if metadata.type
+ case metadata.type.to_sym
+ when :register
+ register_node(message[:service], message[:node], message[:password])
+ else
+ raise UnknownMessageType, "I don't know how to handle message of type #{metadata.type} (Content: #{message})."
+ end
+ else
+ raise MessageTypeNotSpecified, "I don't know how to handle this message (#{message}), no message type specified."
+ end
+ end
+
+
+ private
+
+ # Parse the received payload
+ #
+ # @param [String] content_type The content type of payload
+ # @param [String] payload The payload to parse
+ #
+ # @return [Hash] The parsed content of payload
+ #
+ def parse_payload(content_type, payload)
+ JSON.parse(payload, :symbolize_names => true)
+ end
+
+ # Register a node to a service
+ #
+ # @param [Symbol,String] service The service where the node should be assigned
+ # @param [String] node The node which should be assigned
+ # @param [String] password The password to join this cluster
+ #
+ # @return [Boolean] The status whether it worked out
+ #
+ def register_node(service, node, password)
+ service = service.to_sym
+ if password == @config[:cluster_password]
+ raise UnknownService, "I cannot add a node to service '#{service}' as I don't know this service" unless @services.has_key?(service)
+ unless @services[service][:nodes].find { |registered_node| registered_node[:name] == node }
+ @services[service][:nodes] << { :name => node, :weight => 0 }
+ else
+ # puts "Node is already registered"
+ end
+ else
+ # puts "Password was wrong"
+ end
+ end
+
end
end
View
37 spec/cloud_balancer/server_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe CloudBalancer::Server do
+
+ context "Communication to the nodes" do
+
+ it "connects to a message queue server on start" do
+ pending "Dunno how to test this, yet"
+ end
+
+ context "announcement" do
+
+ subject do
+ CloudBalancer::Server.new
+ end
+
+ let(:metadata) do
+ o = OpenStruct.new
+
+ o.content_type = "application/json"
+ o.type = "register"
+ o
+ end
+
+ let(:register_payload) do
+ { "queue" => "testnode1", "password" => "testpass", "service" => "www" }.to_json
+ end
+
+ it "adds the node to the setup" do
+ expect { subject.handle_message(metadata, register_payload) }.to change { subject.services[:www][:nodes].length }.from(0).to(1)
+ end
+
+ end
+
+ end
+
+end
View
10 spec/spec_helper.rb
@@ -0,0 +1,10 @@
+
+require 'rubygems'
+require 'bundler'
+
+Bundler.require
+
+$: << 'lib'
+
+require 'cloud_balancer'
+require 'ostruct'

No commit comments for this range

Something went wrong with that request. Please try again.