Class: DPL::Provider::Scalingo

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/scalingo.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, options) ⇒ Scalingo

Returns a new instance of Scalingo.



24
25
26
27
28
29
30
31
32
# File 'lib/dpl/provider/scalingo.rb', line 24

def initialize(context, options)
  super
  @options = options
  @remote = options[:remote] || 'scalingo-dpl'
  @branch = options[:branch] || 'master'
  @region = options[:region] || 'agora-fr1'
  @timeout = options[:timeout] || '60'
  @debug = !options[:debug].nil?
end

Instance Method Details

#check_authObject



34
35
36
37
38
39
40
# File 'lib/dpl/provider/scalingo.rb', line 34

def check_auth
  if token
    scalingo("login --api-token #{token}")
  elsif username && @options[:password]
    scalingo('login', [], "echo -e \"#{username}\n#{@options[:password]}\"")
  end
end

#install_deploy_dependenciesObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dpl/provider/scalingo.rb', line 9

def install_deploy_dependencies
  download_url = 'https://cli-dl.scalingo.io/release/scalingo_latest_linux_amd64.tar.gz'
  command = 'curl'
  command = "#{command} --silent" if !@debug
  command = "#{command} --remote-name --location #{download_url}"
  tar_options = 'v' if @debug
  tar_options = "#{tar_options}zxf"
  command = "#{command} && tar -#{tar_options} scalingo_latest_linux_amd64.tar.gz" \
            ' && mv scalingo_*_linux_amd64/scalingo .' \
            ' && rm scalingo_latest_linux_amd64.tar.gz' \
            ' && rm -r scalingo_*_linux_amd64'

  error "Couldn't install Scalingo CLI." if !context.shell command
end

#push_appObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dpl/provider/scalingo.rb', line 54

def push_app
  install_deploy_dependencies

  if @options[:app]
    if !scalingo("--app #{@options[:app]} git-setup --remote #{@remote}")
      error 'Failed to add the Git remote.'
    end
  end

  if !context.shell "git push #{@remote} #{@branch} --force"
    error "Couldn't push your app."
  end
end

#remove_keyObject



48
49
50
51
52
# File 'lib/dpl/provider/scalingo.rb', line 48

def remove_key
  if !scalingo('keys-remove dpl_tmp_key')
    error "Couldn't remove SSH key."
  end
end

#scalingo(command, env = [], input = '') ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dpl/provider/scalingo.rb', line 76

def scalingo(command, env = [], input = '')
  env << "SCALINGO_REGION=#{@region}" if !@region.empty?

  if @debug
    env << 'DEBUG=1'
  else
    command += ' > /dev/null'
  end
  command = "#{env.join(' ')} timeout #{@timeout} ./scalingo #{command}"
  command = "#{input} | #{command}" if input != ''

  puts "Execute #{command}" if @debug

  context.shell command
end

#setup_key(file, _type = nil) ⇒ Object



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

def setup_key(file, _type = nil)
  if !scalingo("keys-add dpl_tmp_key #{file}")
    error "Couldn't add SSH key."
  end
end

#tokenObject



72
73
74
# File 'lib/dpl/provider/scalingo.rb', line 72

def token
  @options[:api_key] || @options[:api_token]
end

#usernameObject



68
69
70
# File 'lib/dpl/provider/scalingo.rb', line 68

def username
  @options[:username] || @options[:user]
end