brighter_planet_deploy 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in brighter_planet_deploy.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "brighter_planet_deploy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "brighter_planet_deploy"
7
+ s.version = BrighterPlanet::Deploy::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Seamus Abshere", "Andy Rossmeissl"]
10
+ s.email = ["seamus@abshere.net"]
11
+ s.homepage = ""
12
+ s.summary = %q{Brighter Planet deployment system, published as the gem brighter_planet_deploy}
13
+ s.description = %q{Brighter Planet deployment system, published as the gem brighter_planet_deploy. Internal use.}
14
+
15
+ s.rubyforge_project = "brighter_planet_deploy"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'test-unit'
23
+ s.add_development_dependency 'fakeweb'
24
+ s.add_development_dependency 'fakefs'
25
+ s.add_dependency 'eat'
26
+ end
@@ -0,0 +1,36 @@
1
+ module BrighterPlanet
2
+ class Deploy
3
+ class EmissionEstimateService
4
+ include ::Singleton
5
+ include ReadsFromPublicUrl
6
+
7
+ WANTS = [
8
+ :resque_redis_url,
9
+ :incoming_queue,
10
+ :color,
11
+ :role,
12
+ :environment,
13
+ :log_dir,
14
+ :phase,
15
+ :carrier, # amazon
16
+ :ey_app, # cm1_edge_blue
17
+ ]
18
+
19
+ def endpoint
20
+ 'http://carbon.brighterplanet.com'
21
+ end
22
+
23
+ def name
24
+ 'EmissionEstimateService'
25
+ end
26
+
27
+ def method_missing(method_id, *args)
28
+ if args.length == 0 and not block_given?
29
+ from_public_url method_id
30
+ else
31
+ super
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,72 @@
1
+ require 'fileutils'
2
+ module BrighterPlanet
3
+ class Deploy
4
+ module ReadsFromLocalFilesystem
5
+ class NotLocal < ::RuntimeError;
6
+ end
7
+
8
+ def from_private_dir(k)
9
+ from_file private_brighter_planet_deploy_path(k)
10
+ end
11
+
12
+ def from_public_dir(k)
13
+ from_file public_brighter_planet_deploy_path(k)
14
+ end
15
+
16
+ def write_config(config = {})
17
+ [ :public, :private ].each do |loc|
18
+ config[loc].each do |k, v|
19
+ path = send("#{loc}_brighter_planet_deploy_path", k)
20
+ $stderr.puts "[brighter_planet_deploy] Writing #{k}=#{v} to #{path}"
21
+ ::File.open(path, 'w') { |f| f.write v.to_s }
22
+ end
23
+ end
24
+ end
25
+
26
+ def save_config
27
+ public = {}
28
+ private = {}
29
+ instance_variables.each do |k|
30
+ k1 = k.to_s.sub('@', '').to_sym
31
+ next if not_saved? k1
32
+ if v = instance_variable_get(k)
33
+ if public? k1
34
+ public[k1] = v
35
+ else
36
+ private[k1] = v
37
+ end
38
+ end
39
+ end
40
+ write_config :public => public, :private => private
41
+ end
42
+
43
+ private
44
+
45
+ def public_brighter_planet_deploy_path(k)
46
+ p = ::File.join public_dir, 'brighter_planet_deploy', k.to_s
47
+ ::FileUtils.mkdir_p ::File.dirname(p)
48
+ p
49
+ end
50
+
51
+ def private_brighter_planet_deploy_path(k)
52
+ p = ::File.join private_dir, 'brighter_planet_deploy', k.to_s
53
+ ::FileUtils.mkdir_p ::File.dirname(p)
54
+ p
55
+ end
56
+
57
+ # fills placeholders like [STATUS] by sending :status
58
+ def from_file(path)
59
+ raise NotLocal, "[brighter_planet_deploy] Can't read #{path} unless this is being run on the server" unless local?
60
+ return unless ::File.readable? path
61
+ str = ::File.readlines(path)[0].chomp
62
+ str.dup.scan(%r{\[([^\]]+)\]}) do |placeholder|
63
+ placeholder = placeholder[0]
64
+ if v = send(placeholder.downcase)
65
+ str.gsub! "[#{placeholder}]", v.to_s
66
+ end
67
+ end
68
+ str
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,9 @@
1
+ module BrighterPlanet
2
+ class Deploy
3
+ module ReadsFromPublicUrl
4
+ def from_public_url(k)
5
+ eat "#{endpoint}/brighter_planet_deploy/#{k}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,123 @@
1
+ module BrighterPlanet
2
+ class Deploy
3
+ class Server
4
+ include ReadsFromLocalFilesystem
5
+
6
+ class InvalidKey < ::ArgumentError;
7
+ end
8
+
9
+ # keys that are published
10
+ PUBLIC = [:color]
11
+ # keys that are not saved
12
+ NOT_SAVED = [:local, :status, :rails_root, :public_dir, :private_dir]
13
+
14
+ class << self
15
+ def me
16
+ new :local => true
17
+ end
18
+ end
19
+
20
+ def initialize(attrs = {})
21
+ attrs.each do |k, v|
22
+ instance_variable_set "@#{k}", v
23
+ end
24
+ end
25
+
26
+ def local?
27
+ !!@local
28
+ end
29
+
30
+ # can't be saved
31
+ attr_writer :rails_root
32
+ def rails_root
33
+ @rails_root || local_rails_root
34
+ end
35
+
36
+ # can't be saved
37
+ attr_writer :public_dir
38
+ def public_dir
39
+ @public_dir || ::File.join(rails_root, 'public')
40
+ end
41
+
42
+ # can't be saved
43
+ attr_writer :private_dir
44
+ def private_dir
45
+ @private_dir || ::File.join(rails_root, 'config')
46
+ end
47
+
48
+ # can't be saved
49
+ attr_writer :status
50
+ def status
51
+ @status || (color == service_class.color ? :active : :standby)
52
+ end
53
+
54
+ attr_writer :environment
55
+ def environment
56
+ @environment || lookup(:environment) || local_rails_environment
57
+ end
58
+
59
+ attr_writer :hostname
60
+ def hostname
61
+ @hostname || lookup(:hostname) || local_hostname
62
+ end
63
+
64
+ def service_class
65
+ case service.to_sym
66
+ when :EmissionEstimateService, :cm1
67
+ EmissionEstimateService.instance
68
+ end
69
+ end
70
+
71
+ def method_missing(method_id, *args)
72
+ if method_id.to_s.end_with?('=') and args.length == 1
73
+ instance_variable_set "@#{method_id.to_s.chomp('=')}", args[0]
74
+ elsif args.length == 0 and not block_given?
75
+ lookup method_id
76
+ else
77
+ super
78
+ end
79
+ end
80
+
81
+ def public?(k)
82
+ PUBLIC.include? k.to_sym
83
+ end
84
+
85
+ def not_saved?(k)
86
+ NOT_SAVED.include? k.to_sym
87
+ end
88
+
89
+ private
90
+
91
+ def lookup(k)
92
+ if public? k
93
+ from_public_dir k
94
+ else
95
+ from_private_dir k
96
+ end
97
+ end
98
+
99
+ def local_hostname
100
+ return unless local?
101
+ `hostname -f`.chomp
102
+ end
103
+
104
+ def local_rails_root
105
+ return unless local?
106
+ if defined?(::Rails) and ::Rails.respond_to?(:root)
107
+ ::Rails.root
108
+ elsif ::ENV.has_key? 'RAILS_ROOT'
109
+ ::ENV['RAILS_ROOT']
110
+ end
111
+ end
112
+
113
+ def local_rails_environment
114
+ return unless local?
115
+ if defined?(::Rails) and ::Rails.respond_to?(:env)
116
+ ::Rails.env
117
+ elsif ::ENV.has_key? 'RAILS_ENV'
118
+ ::ENV['RAILS_ENV']
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,5 @@
1
+ module BrighterPlanet
2
+ class Deploy
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'singleton'
2
+ require 'eat'
3
+
4
+ module BrighterPlanet
5
+ def self.deploy
6
+ Deploy.instance
7
+ end
8
+
9
+ class Deploy
10
+ include ::Singleton
11
+
12
+ autoload :Server, 'brighter_planet_deploy/server'
13
+ autoload :EmissionEstimateService, 'brighter_planet_deploy/emission_estimate_service'
14
+
15
+ # mixins
16
+ autoload :ReadsFromLocalFilesystem, 'brighter_planet_deploy/reads_from_local_filesystem'
17
+ autoload :ReadsFromPublicUrl, 'brighter_planet_deploy/reads_from_public_url'
18
+
19
+ def servers
20
+ Server
21
+ end
22
+
23
+ def emission_estimate_service
24
+ EmissionEstimateService.instance
25
+ end
26
+ end
27
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ require 'test/unit'
5
+ require 'fakefs/safe'
6
+ require 'fakeweb'
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'brighter_planet_deploy'
10
+ class Test::Unit::TestCase
11
+ end
data/test/test_cm1.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'helper'
2
+
3
+ class TestCm1 < Test::Unit::TestCase
4
+ def setup
5
+ FakeWeb.clean_registry
6
+ FakeWeb.allow_net_connect = false
7
+ {
8
+ 'http://carbon.brighterplanet.com/brighter_planet_deploy/color' => 'blue',
9
+ }.each do |url, body|
10
+ FakeWeb.register_uri :get, url, :status => ["200", "OK"], :body => body
11
+ end
12
+ FakeFS.activate!
13
+
14
+ FileUtils.mkdir_p '/data/randomeyappname/current/config/brighter_planet_deploy'
15
+ File.open('/data/randomeyappname/current/config/brighter_planet_deploy/resque_redis_url', 'w') { |f| f.write "redis://username:password@hostname.redistogo.com:9000/[STATUS]:resque" }
16
+ File.open('/data/randomeyappname/current/config/brighter_planet_deploy/phase', 'w') { |f| f.write 'edge' }
17
+ File.open('/data/randomeyappname/current/config/brighter_planet_deploy/service', 'w') { |f| f.write 'cm1' }
18
+
19
+ FileUtils.mkdir_p '/data/randomeyappname/current/public/brighter_planet_deploy'
20
+ File.open('/data/randomeyappname/current/public/brighter_planet_deploy/color', 'w') { |f| f.write 'blue' }
21
+
22
+ @me = BrighterPlanet.deploy.servers.me
23
+ @me.rails_root = '/data/randomeyappname/current'
24
+ end
25
+
26
+ def teardown
27
+ FakeWeb.clean_registry
28
+ FakeWeb.allow_net_connect = true
29
+ FakeFS.deactivate!
30
+ end
31
+
32
+ def test_001_color
33
+ assert_equal 'blue', @me.color
34
+ assert_equal 'blue', BrighterPlanet.deploy.emission_estimate_service.color
35
+ end
36
+
37
+ def test_003_service
38
+ assert_equal 'EmissionEstimateService', BrighterPlanet.deploy.emission_estimate_service.name
39
+ assert_equal 'EmissionEstimateService', @me.service_class.name
40
+ end
41
+
42
+ def test_004_status
43
+ assert_equal :active, @me.status
44
+ end
45
+
46
+ def test_005_resque_redis_url
47
+ assert @me.resque_redis_url.start_with?('redis://')
48
+ assert @me.resque_redis_url.include?('active')
49
+ end
50
+
51
+ def test_006_write_config
52
+ @me.write_config :public => { :color => '-bar-' }, :private => { :resque_redis_url => 'foo[COLOR]baz' }
53
+ assert_equal '-bar-', @me.color
54
+ assert_equal 'foo-bar-baz', @me.resque_redis_url
55
+ end
56
+
57
+ def test_007_save_config
58
+ @me.color = '-zzz-'
59
+ @me.resque_redis_url = 'fie[COLOR]bang'
60
+ @me.save_config
61
+ me2 = BrighterPlanet.deploy.servers.me
62
+ me2.rails_root = '/data/randomeyappname/current'
63
+ assert_equal '-zzz-', me2.color
64
+ assert_equal 'fie-zzz-bang', me2.resque_redis_url
65
+ end
66
+
67
+ # not sure this should be included
68
+ def test_008_phase
69
+ assert_equal 'edge', @me.phase
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brighter_planet_deploy
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
+ - Seamus Abshere
14
+ - Andy Rossmeissl
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-05-04 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: test-unit
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: fakeweb
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: fakefs
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: eat
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: Brighter Planet deployment system, published as the gem brighter_planet_deploy. Internal use.
78
+ email:
79
+ - seamus@abshere.net
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - Rakefile
90
+ - brighter_planet_deploy.gemspec
91
+ - lib/brighter_planet_deploy.rb
92
+ - lib/brighter_planet_deploy/emission_estimate_service.rb
93
+ - lib/brighter_planet_deploy/reads_from_local_filesystem.rb
94
+ - lib/brighter_planet_deploy/reads_from_public_url.rb
95
+ - lib/brighter_planet_deploy/server.rb
96
+ - lib/brighter_planet_deploy/version.rb
97
+ - test/helper.rb
98
+ - test/test_cm1.rb
99
+ homepage: ""
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ requirements: []
126
+
127
+ rubyforge_project: brighter_planet_deploy
128
+ rubygems_version: 1.7.2
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Brighter Planet deployment system, published as the gem brighter_planet_deploy
132
+ test_files:
133
+ - test/helper.rb
134
+ - test/test_cm1.rb