Skip to content

Instantly share code, notes, and snippets.

@relistan
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relistan/81e7b2f6c0847ae2fa8b to your computer and use it in GitHub Desktop.
Save relistan/81e7b2f6c0847ae2fa8b to your computer and use it in GitHub Desktop.
Centurion with etcd Proof-of-Concept
namespace :environment do
require 'etcd'
def hash_value_for(item)
if item.is_a?(Array)
return item.inject({}) { |memo, x| memo.merge(hash_value_for(x)) }
end
if item.directory?
return { File.basename(item.key) => hash_value_for(item.children) }
end
return { File.basename(item.key) => item.value }
end
def etcd_client
@etcd_client ||= Etcd.client(host: '127.0.0.1', port: '4001')
end
def etcd_configuration_for(service)
# TODO look at current_environment and talk to the right etcd server
env = hash_value_for(etcd_client.get("/services/#{service}/", recursive: true))
raise "Bad configuration!" if env.empty?
env[service]['ports'].each do |(container_port, to_host_port)|
host_port to_host_port, container_port: container_port
end
env[service]['hosts'].each do |(hostname, state)|
host hostname
end
end
task :common do
set :image, 'fooservice'
end
desc 'Staging environment'
task :staging => :common do
set_current_environment(:staging)
etcd_configuration_for('fooservice')
end
end

Services Layout

The following code run from inside a Ruby script that has loaded the etcd gem would create the necessary keys to deploy using the above code.

client.set('/services/fooservice/ports/3021', value: 9292)
client.set('/services/fooservice/env/TESTING_URL', value: 'http//foo.example.com/asdf')

client.set('/services/fooservice/hosts/yourco-staging-foo-1.example.com', value: 'up')
client.set('/services/fooservice/hosts/yourco-staging-foo-2.example.com', value: 'up')
client.set('/services/fooservice/hosts/yourco-staging-foo-3.example.com', value: 'up')

That assigns port 3021 on the host to 9292 in the container, "http://foo.example.com/asdf" to the TESTING_URL environment variable and specifies three hosts to deploy to.

Currently host volume mounts don't fit well into this schema as the key doesn't work well.This needs to be figured out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment