Class: Reina::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/reina/controller.rb

Constant Summary collapse

APP_COOLDOWN =

seconds

7

Instance Method Summary collapse

Constructor Details

#initialize(params, strict = false) ⇒ Controller

Returns a new instance of Controller.



5
6
7
8
9
10
11
12
13
14
# File 'lib/reina/controller.rb', line 5

def initialize(params, strict = false)
  @params = params
  @strict = strict

  abort 'Please provide $PLATFORM_API' if CONFIG[:platform_api].blank?
  abort 'Given PR number should be greater than 0' if issue_number <= 0

  oversize = apps.select { |app| app.app_name.length >= 30 }.first
  abort "#{oversize.app_name} is too long" if oversize.present?
end

Instance Method Details

#appsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/reina/controller.rb', line 72

def apps
  return @_apps if @_apps.present?

  # strict is when we only take in consideration the apps
  # that are in both `params` and `APPS`
  _apps = if strict
    app_names = branches.keys
    APPS.select { |name, _| app_names.include?(name) }
  else
    APPS
  end

  @_apps = _apps.map do |name, project|
    branch = branches[name.to_s].presence || 'master'.freeze
    App.new(heroku, name, project, issue_number, branch)
  end
end

#create_netrcObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/reina/controller.rb', line 16

def create_netrc
  return if ENV['GITHUB_AUTH'].blank?

  `git config --global user.name "#{ENV['GITHUB_NAME']}"`
  `git config --global user.email "#{ENV['GITHUB_EMAIL']}"`

  return if File.exists?('.netrc')

  File.write(
    '.netrc',
    "machine git.heroku.com login #{ENV['GITHUB_EMAIL']} password #{ENV['HEROKU_API_KEY']}"
  )
end

#delete_existing_apps!Object



56
57
58
59
60
61
# File 'lib/reina/controller.rb', line 56

def delete_existing_apps!
  existing_apps.each do |app|
    puts "Deleting #{app}"
    heroku.app.delete(app)
  end
end

#deploy_non_parallel_apps!Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reina/controller.rb', line 43

def deploy_non_parallel_apps!
  apps.reject(&:parallel?).each do |app|
    begin
      deploy!(app)
    rescue Git::GitExecuteError => e
      puts "#{app.name}: #{e.message}"
    rescue Exception => e
      msg = e.respond_to?(:response) ? e.response.body : e.message
      puts "#{app.name}: #{msg}"
    end
  end
end

#deploy_parallel_apps!Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reina/controller.rb', line 30

def deploy_parallel_apps!
  Parallel.each(apps.select(&:parallel?)) do |app|
    begin
      deploy!(app)
    rescue Git::GitExecuteError => e
      puts "#{app.name}: #{e.message}"
    rescue Exception => e
      msg = e.respond_to?(:response) ? e.response.body : e.message
      puts "#{app.name}: #{msg}"
    end
  end
end

#existing_appsObject



63
64
65
66
# File 'lib/reina/controller.rb', line 63

def existing_apps
  # apps in common between heroku's list and ours
  @_existing_apps ||= heroku.app.list.map { |a| a['name'] } & apps.map(&:app_name)
end

#heroku?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/reina/controller.rb', line 68

def heroku?
  ENV['DYNO'].present?
end