Class: DPL::Provider

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/dpl/provider.rb,
lib/dpl/provider/heroku.rb,
lib/dpl/provider/dot_cloud.rb,
lib/dpl/provider/nodejitsu.rb,
lib/dpl/provider/openshift.rb,
lib/dpl/provider/heroku/git.rb,
lib/dpl/provider/engine_yard.rb,
lib/dpl/provider/heroku/anvil.rb

Direct Known Subclasses

Heroku::Git

Defined Under Namespace

Modules: Heroku Classes: DotCloud, EngineYard, Nodejitsu, Openshift

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options) ⇒ Provider

Returns a new instance of Provider.



57
58
59
# File 'lib/dpl/provider.rb', line 57

def initialize(context, options)
  @context, @options = context, options
end

Instance Attribute Details

#context ⇒ Object (readonly)

Returns the value of attribute context.



55
56
57
# File 'lib/dpl/provider.rb', line 55

def context
  @context
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



55
56
57
# File 'lib/dpl/provider.rb', line 55

def options
  @options
end

Class Method Details

.context ⇒ Object



39
40
41
# File 'lib/dpl/provider.rb', line 39

def self.context
  self
end

.experimental(name) ⇒ Object



24
25
26
# File 'lib/dpl/provider.rb', line 24

def self.experimental(name)
  puts "", "!!! #{name} support is experimental !!!", ""
end

.new(context, options) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/dpl/provider.rb', line 14

def self.new(context, options)
  return super if self < Provider

  context.fold("Installing deploy dependencies") do
    name = super.option(:provider).to_s.downcase.gsub(/[^a-z]/, '')
    raise Error, 'could not find provider %p' % options[:provider] unless name = constants.detect { |c| c.to_s.downcase == name }
    const_get(name).new(context, options)
  end
end

.npm_g(name, command = name) ⇒ Object



51
52
53
# File 'lib/dpl/provider.rb', line 51

def self.npm_g(name, command = name)
  context.shell "npm install -g #{name}" if `which #{command}`.chop.empty?
end

.pip(name, command = name) ⇒ Object



47
48
49
# File 'lib/dpl/provider.rb', line 47

def self.pip(name, command = name)
  context.shell "sudo pip install #{name}" if `which #{command}`.chop.empty?
end

.requires(name, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/dpl/provider.rb', line 28

def self.requires(name, options = {})
  version = options[:version] || '> 0'
  load    = options[:load]    || name
  gem(name, version)
rescue LoadError
  context.shell("gem install %s -v %p" % [name, version])
  Gem.clear_paths
ensure
  require load
end

.shell(command) ⇒ Object



43
44
45
# File 'lib/dpl/provider.rb', line 43

def self.shell(command)
  system(command)
end

Instance Method Details

#check_app ⇒ Object



110
111
# File 'lib/dpl/provider.rb', line 110

def check_app
end

#cleanup ⇒ Object



101
102
103
104
# File 'lib/dpl/provider.rb', line 101

def cleanup
  context.shell "git reset --hard #{sha}"
  context.shell "git clean -dffqx -e .dpl"
end

#create_key(file) ⇒ Object



113
114
115
# File 'lib/dpl/provider.rb', line 113

def create_key(file)
  context.shell "ssh-keygen -t rsa -N \"\" -C #{option(:key_name)} -f #{file}"
end

#deploy ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dpl/provider.rb', line 67

def deploy
  rm_rf ".dpl"
  mkdir_p ".dpl"

  context.fold("Preparing deploy") do
    check_auth
    check_app

    if needs_key?
      create_key(".dpl/id_rsa")
      setup_key(".dpl/id_rsa.pub")
      setup_git_ssh(".dpl/git-ssh", ".dpl/id_rsa")
    end

    cleanup
  end

  context.fold("Deploying application") { push_app }

  Array(options[:run]).each do |command|
    if command == 'restart'
      context.fold("Restarting application") { restart }
    else
      context.fold("Running %p" % command) { run(command) }
    end
  end
ensure
  remove_key if needs_key?
end

#error(message) ⇒ Object

Raises:



138
139
140
# File 'lib/dpl/provider.rb', line 138

def error(message)
  raise Error, message
end

#log(message) ⇒ Object



130
131
132
# File 'lib/dpl/provider.rb', line 130

def log(message)
  $stderr.puts(message)
end

#needs_key? ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/dpl/provider.rb', line 106

def needs_key?
  true
end

#option(name, *alternatives) ⇒ Object



61
62
63
64
65
# File 'lib/dpl/provider.rb', line 61

def option(name, *alternatives)
  options.fetch(name) do
    alternatives.any? ? option(*alternatives) : raise(Error, "missing #{name}")
  end
end

#run(command) ⇒ Object



134
135
136
# File 'lib/dpl/provider.rb', line 134

def run(command)
  error "running commands not supported"
end

#setup_git_ssh(path, key_path) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dpl/provider.rb', line 117

def setup_git_ssh(path, key_path)
  key_path = File.expand_path(key_path)
  path     = File.expand_path(path)

  File.open(path, 'w') do |file|
    file.write "#!/bin/sh\n"
    file.write "exec ssh -o StrictHostKeychecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -i #{key_path} -- \"$@\"\n"
  end

  chmod(0740, path)
  ENV['GIT_SSH'] = path
end

#sha ⇒ Object



97
98
99
# File 'lib/dpl/provider.rb', line 97

def sha
  @sha ||= ENV['TRAVIS_COMMIT'] || `git rev-parse HEAD`.strip
end