chef-failover 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in chef-failover.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "chef-failover/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "chef-failover"
7
+ s.version = Chef::Failover::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Roland Moriz"]
10
+ s.email = ["roland@moriz.de"]
11
+ s.homepage = ""
12
+ s.summary = %q{Experimental client side failover. NOT READY YET, SORRY!}
13
+ s.description = %q{Experimental client side failover. NOT READY YET, SORRY!}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'chef-failover'
3
+
4
+ chef_failover = Chef::Failover.new(:strategy => Chef::Failover::Strategy::RoundRobin)
5
+ chef_failover.add_config :config_name => 'main_chef_server'
6
+ chef_failover.add_config :config_name => 'backup_chef_server'
7
+ chef_failover.dispatch
@@ -0,0 +1,40 @@
1
+ require 'lib/chef-failover/version'
2
+ require 'lib/chef-failover/strategy/base'
3
+ require 'lib/chef-failover/strategy/round_robin'
4
+
5
+ module Chef
6
+ class Failover
7
+
8
+ attr_accessor :config
9
+ attr_accessor :status
10
+
11
+ def initialize(args = {})
12
+ @config = {}
13
+ @config[:config_directory] ||= '/etc/chef/client.d'
14
+ @config[:chef_client_log] ||= '/var/log/chef/chef.log'
15
+ @config[:configurations] = []
16
+ @config[:strategy] = args[:strategy] || Chef::Failover::Strategy::RoundRobin
17
+
18
+ @status = {}
19
+ @status[:last_run_success] = true
20
+ end
21
+
22
+ def add_config(args)
23
+ @config[:configurations] << args[:config_name]
24
+ end
25
+
26
+ def dispatch
27
+ strategy = @config[:strategy].new(self)
28
+ strategy.dispatch
29
+ end
30
+
31
+ def config_filename(name)
32
+ self.config_directory + '/' + name.to_s
33
+ end
34
+
35
+ def config_directory
36
+ @config[:config_directory]
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ require 'forwardable'
2
+
3
+ module Chef
4
+ class Failover
5
+ module Strategy
6
+ class Base
7
+ extend Forwardable
8
+ def_delegators :@failover, :status, :config, :config_directory, :config_filename
9
+
10
+ def initialize(failover)
11
+ @failover = failover
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,50 @@
1
+ module Chef
2
+ class Failover
3
+ module Strategy
4
+ class RoundRobin < Base
5
+
6
+ def dispatch
7
+ load_and_update_status
8
+ end
9
+
10
+ private
11
+
12
+ def load_and_update_status
13
+ available_client_configs
14
+ analyze_status
15
+ switch_to_next_configuration unless status[:last_run_success]
16
+ end
17
+
18
+ def available_client_configs
19
+ available = []
20
+ Dir.glob(File.join(config_directory, "*.rb")).each do |file|
21
+ available << File.basename(file, ".rb")
22
+ end
23
+
24
+ status[:available] = available
25
+ status[:configured] = available & config[:configurations]
26
+ status[:active] = File.basename(File.readlink(config_filename(:active)), ".rb") rescue nil
27
+ end
28
+
29
+ def analyze_status
30
+ last_log_output = `tail -n 1 #{config[:chef_client_log]}`
31
+
32
+ if last_log_output =~ /ERROR: Sleeping for \d+ seconds before trying again/
33
+ status[:last_run_success] = false
34
+ end
35
+ end
36
+
37
+ def switch_to_next_configuration
38
+ old_index = status[:configured].index(status[:active]) || 0
39
+ new_config = status[:configured][old_index + 1] || status[:configured].first
40
+
41
+ puts "last configuration didn't work. switching to: #{new_config}"
42
+
43
+ File.delete(config_filename(:active)) if File.symlink?(config_filename(:active))
44
+ File.symlink(config_filename("#{new_config}.rb"), config_filename(:active))
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module Chef
2
+ class Failover
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-failover
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Roland Moriz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-10 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Experimental client side failover. NOT READY YET, SORRY!
22
+ email:
23
+ - roland@moriz.de
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - chef-failover.gemspec
35
+ - example-client.rb
36
+ - lib/chef-failover.rb
37
+ - lib/chef-failover/strategy/base.rb
38
+ - lib/chef-failover/strategy/round_robin.rb
39
+ - lib/chef-failover/version.rb
40
+ homepage: ""
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.7.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Experimental client side failover. NOT READY YET, SORRY!
73
+ test_files: []
74
+