Class: DPL::Provider::Scalingo

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

Instance Attribute Summary

Attributes inherited from DPL::Provider

#context, #options

Instance Method Summary collapse

Methods inherited from DPL::Provider

apt_get, #check_app, #cleanup, #commit_msg, context, #create_key, #default_text_charset, #default_text_charset?, #deploy, deprecated, #detect_encoding?, #encoding_for, #error, experimental, #log, #needs_key?, new, npm_g, #option, pip, requires, #run, #setup_git_credentials, #setup_git_ssh, #sha, shell, #uncleanup, #user_agent, #warn

Constructor Details

#initialize(context, options) ⇒ Scalingo

Returns a new instance of Scalingo.



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

def initialize(context, options)
  super
  @options = options
  @remote = options[:remote] || "scalingo"
  @branch = options[:branch] || "master"
end

Instance Method Details

#check_authObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dpl/provider/scalingo.rb', line 27

def check_auth
  if @options[:api_key]
    unless context.shell "mkdir -p ~/.config/scalingo"
      error "Couldn't create authentication file."
    end
    url = URI.parse('http://api.scalingo.com/v1/users/self')
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Get.new(url.request_uri)
    request.basic_auth("", @options[:api_key])
    request["Accept"] = "application/json"
    request["Content-type"] = "application/json"
    response = http.request(request)
    data = {}
    if File.exist?("#{Dir.home}/.config/scalingo/auth")
      data = JSON.parse(File.read("#{Dir.home}/.config/scalingo/auth"))
    end
    begin
      user = JSON.parse(response.body)
    rescue
      error "Invalid API token."
    end
    data["auth_config_data"] = {}
    data["auth_config_data"]["api.scalingo.com"] = {}
    data["auth_config_data"]["api.scalingo.com"]["id"] = user["user"]["id"]
    data["auth_config_data"]["api.scalingo.com"]["last_name"] = user["user"]["last_name"]
    data["auth_config_data"]["api.scalingo.com"]["username"] = user["user"]["username"]
    data["auth_config_data"]["api.scalingo.com"]["email"] = user["user"]["email"]
    data["auth_config_data"]["api.scalingo.com"]["first_name"] = user["user"]["first_name"]
    data["auth_config_data"]["api.scalingo.com"]["auth_token"] = @options[:api_key]
    data["last_update"] = DateTime.now
    f = File.open("#{Dir.home}/.config/scalingo/auth", "w+") {
      |f| f.write(data.to_json)
    }
  elsif @options[:username] && @options[:password]
    context.shell "echo -e \"#{@options[:username]}\n#{@options[:password]}\" | timeout 2 ./scalingo login 2> /dev/null > /dev/null"
  end
  if !logged_in
    error "Couldn't connect to Scalingo API."
  end
end

#install_deploy_dependenciesObject



10
11
12
13
14
# File 'lib/dpl/provider/scalingo.rb', line 10

def install_deploy_dependencies
  unless context.shell "curl -OL https://cli-dl.scalingo.io/release/scalingo_latest_linux_amd64.tar.gz && tar -zxvf 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."
  end
end

#logged_inObject



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

def logged_in
  context.shell "DISABLE_INTERACTIVE=true ./scalingo login 2> /dev/null > /dev/null"
end

#push_appObject



86
87
88
89
90
91
92
93
# File 'lib/dpl/provider/scalingo.rb', line 86

def push_app
  if @options[:app]
    context.shell "git remote add #{@remote} [email protected]:#{@options[:app]}.git 2> /dev/null > /dev/null"
  end
  unless context.shell "git push #{@remote} #{@branch} -f"
    error "Couldn't push your app."
  end
end

#remove_keyObject



77
78
79
80
81
82
83
84
# File 'lib/dpl/provider/scalingo.rb', line 77

def remove_key
  if !logged_in
    error "Couldn't connect to Scalingo API."
  end
  unless context.shell "./scalingo keys-remove dpl_tmp_key"
    error "Couldn't remove ssh key."
  end
end

#setup_key(file, type = nil) ⇒ Object



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

def setup_key(file, type = nil)
  if !logged_in
    error "Couldn't connect to Scalingo API."
  end
  unless context.shell "./scalingo keys-add dpl_tmp_key #{file}"
    error "Couldn't add ssh key."
  end
end