Class: DPL::Provider

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/dpl/provider.rb,
lib/dpl/provider/s3.rb,
lib/dpl/provider/gae.rb,
lib/dpl/provider/gcs.rb,
lib/dpl/provider/npm.rb,
lib/dpl/provider/deis.rb,
lib/dpl/provider/pypi.rb,
lib/dpl/provider/atlas.rb,
lib/dpl/provider/surge.rb,
lib/dpl/provider/appfog.rb,
lib/dpl/provider/heroku.rb,
lib/dpl/provider/lambda.rb,
lib/dpl/provider/script.rb,
lib/dpl/provider/bintray.rb,
lib/dpl/provider/boxfuse.rb,
lib/dpl/provider/cloud66.rb,
lib/dpl/provider/divshot.rb,
lib/dpl/provider/hackage.rb,
lib/dpl/provider/modulus.rb,
lib/dpl/provider/anynines.rb,
lib/dpl/provider/catalyze.rb,
lib/dpl/provider/exoscale.rb,
lib/dpl/provider/firebase.rb,
lib/dpl/provider/releases.rb,
lib/dpl/provider/rubygems.rb,
lib/dpl/provider/scalingo.rb,
lib/dpl/provider/launchpad.rb,
lib/dpl/provider/nodejitsu.rb,
lib/dpl/provider/openshift.rb,
lib/dpl/provider/ops_works.rb,
lib/dpl/provider/testfairy.rb,
lib/dpl/provider/transifex.rb,
lib/dpl/provider/bitballoon.rb,
lib/dpl/provider/heroku/api.rb,
lib/dpl/provider/heroku/git.rb,
lib/dpl/provider/cloud_files.rb,
lib/dpl/provider/code_deploy.rb,
lib/dpl/provider/engine_yard.rb,
lib/dpl/provider/heroku/anvil.rb,
lib/dpl/provider/packagecloud.rb,
lib/dpl/provider/puppet_forge.rb,
lib/dpl/provider/azure_webapps.rb,
lib/dpl/provider/cloud_foundry.rb,
lib/dpl/provider/heroku/generic.rb,
lib/dpl/provider/heroku/git_ssh.rb,
lib/dpl/provider/chef_supermarket.rb,
lib/dpl/provider/elastic_beanstalk.rb,
lib/dpl/provider/heroku/git_deploy_key.rb

Defined Under Namespace

Modules: Heroku Classes: Anynines, Appfog, Atlas, AzureWebApps, Bintray, BitBalloon, Boxfuse, Catalyze, ChefSupermarket, Cloud66, CloudFiles, CloudFoundry, CodeDeploy, Deis, Divshot, ElasticBeanstalk, EngineYard, ExoScale, Firebase, GAE, GCS, Hackage, Lambda, Launchpad, Modulus, NPM, Nodejitsu, Openshift, OpsWorks, Packagecloud, PuppetForge, PyPI, Releases, RubyGems, S3, Scalingo, Script, Surge, TestFairy, Transifex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options) ⇒ Provider

Returns a new instance of Provider.



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

def initialize(context, options)
  @context, @options = context, options
  context.env['GIT_HTTP_USER_AGENT'] = user_agent(git: `git --version`[/[\d\.]+/])
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.apt_get(name, command = name) ⇒ Object



86
87
88
# File 'lib/dpl/provider.rb', line 86

def self.apt_get(name, command = name)
  context.shell("sudo apt-get -qq install #{name}", retry: true) if `which #{command}`.chop.empty?
end

.contextObject



78
79
80
# File 'lib/dpl/provider.rb', line 78

def self.context
  self
end

.experimental(name) ⇒ Object



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

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

.new(context, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dpl/provider.rb', line 51

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-z0-9]/, '')
    raise Error, 'could not find provider %p' % options[:provider] unless name = constants.detect { |c| c.to_s.downcase == name }
    provider = const_get(name).new(context, options)
    provider.install_deploy_dependencies if provider.respond_to?(:install_deploy_dependencies)
    provider
  end
end

.npm_g(name, command = name) ⇒ Object



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

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

.pip(name, command = name, version = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dpl/provider.rb', line 90

def self.pip(name, command = name, version = nil)
  if version
    puts "pip install --user #{name}==#{version}"
    context.shell("pip uninstall --user -y #{name}") unless `which #{command}`.chop.empty?
    context.shell("pip install --user #{name}==#{version}", retry: true)
  else
    puts "pip install --user #{name}"
    context.shell("pip install --user #{name}", retry: true) if `which #{command}`.chop.empty?
  end
  context.shell("export PATH=$PATH:$HOME/.local/bin")
end

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



67
68
69
70
71
72
73
74
75
76
# File 'lib/dpl/provider.rb', line 67

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 --no-ri --no-rdoc #{'--pre' if options[:pre]}" % [name, version], retry: true)
  Gem.clear_paths
ensure
  require load
end

.shell(command, options = {}) ⇒ Object



82
83
84
# File 'lib/dpl/provider.rb', line 82

def self.shell(command, options = {})
  system(command)
end

Instance Method Details

#check_appObject



187
188
# File 'lib/dpl/provider.rb', line 187

def check_app
end

#cleanupObject



168
169
170
171
172
173
174
175
176
# File 'lib/dpl/provider.rb', line 168

def cleanup
  return if options[:skip_cleanup]
  context.shell "mv .dpl ~/dpl"
  log "Cleaning up git repository with `git stash --all`. " \
    "If you need build artifacts for deployment, set `deploy.skip_cleanup: true`. " \
    "See https://docs.travis-ci.com/user/deployment/#Uploading-Files."
  context.shell "git stash --all"
  context.shell "mv ~/dpl .dpl"
end

#commit_msgObject



164
165
166
# File 'lib/dpl/provider.rb', line 164

def commit_msg
  @commit_msg ||= %x{git log #{sha} -n 1 --pretty=%B}.strip
end

#create_key(file) ⇒ Object



190
191
192
# File 'lib/dpl/provider.rb', line 190

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

#default_text_charsetObject



220
221
222
# File 'lib/dpl/provider.rb', line 220

def default_text_charset
  options[:default_text_charset].downcase
end

#default_text_charset?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/dpl/provider.rb', line 216

def default_text_charset?
  options[:default_text_charset]
end

#deployObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dpl/provider.rb', line 126

def deploy
  setup_git_credentials
  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
  if needs_key?
    remove_key rescue nil
  end
  uncleanup
end

#detect_encoding?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/dpl/provider.rb', line 212

def detect_encoding?
  options[:detect_encoding]
end

#encoding_for(path) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/dpl/provider.rb', line 224

def encoding_for(path)
  file_cmd_output = `file '#{path}'`
  case file_cmd_output
  when /gzip compressed/
    'gzip'
  when /compress'd/
    'compress'
  when /text/
    'text'
  when /data/
    # Shrugs?
  end
end

#error(message) ⇒ Object

Raises:



250
251
252
# File 'lib/dpl/provider.rb', line 250

def error(message)
  raise Error, message
end

#log(message) ⇒ Object



238
239
240
# File 'lib/dpl/provider.rb', line 238

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

#needs_key?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/dpl/provider.rb', line 183

def needs_key?
  true
end

#option(name, *alternatives) ⇒ Object



120
121
122
123
124
# File 'lib/dpl/provider.rb', line 120

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

#run(command) ⇒ Object



246
247
248
# File 'lib/dpl/provider.rb', line 246

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

#setup_git_credentialsObject



194
195
196
197
# File 'lib/dpl/provider.rb', line 194

def setup_git_credentials
  context.shell "git config user.email >/dev/null 2>/dev/null || git config user.email `whoami`@localhost"
  context.shell "git config user.name >/dev/null 2>/dev/null || git config user.name `whoami`@localhost"
end

#setup_git_ssh(path, key_path) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dpl/provider.rb', line 199

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)
  context.env['GIT_SSH'] = path
end

#shaObject



160
161
162
# File 'lib/dpl/provider.rb', line 160

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

#uncleanupObject



178
179
180
181
# File 'lib/dpl/provider.rb', line 178

def uncleanup
  return if options[:skip_cleanup]
  context.shell "git stash pop"
end

#user_agent(*strings) ⇒ Object



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

def user_agent(*strings)
  strings.unshift "dpl/#{DPL::VERSION}"
  strings.unshift "travis/0.1.0" if context.env['TRAVIS']
  strings = strings.flat_map { |e| Hash === e ? e.map { |k,v| "#{k}/#{v}" } : e }
  strings.join(" ").gsub(/\s+/, " ").strip
end

#warn(message) ⇒ Object



242
243
244
# File 'lib/dpl/provider.rb', line 242

def warn(message)
  log "\e[31;1m#{message}\e[0m"
end