Class: EY::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/ey/deployment.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Deployment

Returns a new instance of Deployment.



29
30
31
# File 'lib/ey/deployment.rb', line 29

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



33
34
35
# File 'lib/ey/deployment.rb', line 33

def env
  @env
end

Class Method Details

.appObject



13
14
15
# File 'lib/ey/deployment.rb', line 13

def self.app
  @app ||= instance.build_app
end

.db_migrateObject



21
22
23
# File 'lib/ey/deployment.rb', line 21

def self.db_migrate
  instance.db_migrate
end

.envObject



17
18
19
# File 'lib/ey/deployment.rb', line 17

def self.env
  instance.env
end

.instanceObject



25
26
27
# File 'lib/ey/deployment.rb', line 25

def self.instance
  @instance || raise(Error, "call setup(rack_env) first")
end

.setup(env) ⇒ Object



8
9
10
11
# File 'lib/ey/deployment.rb', line 8

def self.setup(env)
  @instance = new(env)
  @instance.setup
end

Instance Method Details

#build_appObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ey/deployment.rb', line 49

def build_app
  instance = self

  Rack::Builder.app {

    if instance.hoptoad_key
      use Rack::Hoptoad, instance.hoptoad_key
    end

    map "/ey/sha1" do
      run lambda { [200, {"Content-Type" => "text/plain"}, [instance.sha1]] }
    end

    if instance.resque_web
      map "/ey/resque" do
        run Resque::Server
      end
    end

    map "/" do
      run instance.app
    end
  }
end

#db_adapterObject



98
99
100
# File 'lib/ey/deployment.rb', line 98

def db_adapter
  "postgres"
end

#db_configObject



112
113
114
115
116
# File 'lib/ey/deployment.rb', line 112

def db_config
  @db_config ||= YAML.load_file(db_file.to_s).
    fetch(@env).
    values_at("username", "password", "host", "database")
end

#db_fileObject



124
125
126
# File 'lib/ey/deployment.rb', line 124

def db_file
  @db_file ||= Pathname("config/database.yml")
end

#db_migrateObject



90
91
92
# File 'lib/ey/deployment.rb', line 90

def db_migrate
  DataMapper.auto_migrate!
end

#hoptoad_keyObject



106
107
# File 'lib/ey/deployment.rb', line 106

def hoptoad_key
end

#resque_configObject



118
119
120
121
122
# File 'lib/ey/deployment.rb', line 118

def resque_config
  @resque_config ||= YAML.load_file(resque_file.to_s).
    fetch(@env).
    values_at("redis_host", "redis_port")
end

#resque_fileObject



128
129
130
# File 'lib/ey/deployment.rb', line 128

def resque_file
  @resque_file ||= Pathname("config/resque.yml")
end

#resque_webObject



109
110
# File 'lib/ey/deployment.rb', line 109

def resque_web
end

#setupObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ey/deployment.rb', line 35

def setup
  unless valid_envs.include?(@env)
    raise Error, "invalid env: #{@env.inspect}"
  end

  if db_file.exist?
    setup_db
  end

  if resque_file.exist?
    setup_resque
  end
end

#setup_dbObject



74
75
76
# File 'lib/ey/deployment.rb', line 74

def setup_db
  DataMapper.setup(:default, ("#{db_adapter}://%s:%s@%s/%s" % db_config))
end

#setup_resqueObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ey/deployment.rb', line 78

def setup_resque
  Resque.redis = ("%s:%s" % resque_config)

  if hoptoad_key
    Resque::Failure::Hoptoad.configure do |c|
      c.api_key            = hoptoad_key
      c.secure             = true
      c.server_environment = @env
    end
  end
end

#sha1Object



102
103
104
# File 'lib/ey/deployment.rb', line 102

def sha1
  `git rev-parse HEAD`.chomp
end

#valid_envsObject



94
95
96
# File 'lib/ey/deployment.rb', line 94

def valid_envs
  %w[staging production]
end