Class: Reina::App

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

Constant Summary collapse

DEFAULT_REGION =
'eu'.freeze
DEFAULT_STAGE =
'staging'.freeze
DEFAULT_APP_NAME_PREFIX =
CONFIG[:app_name_prefix].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(heroku, name, project, issue_number, branch) ⇒ App

Returns a new instance of App.


9
10
11
12
13
14
15
# File 'lib/reina/app.rb', line 9

def initialize(heroku, name, project, issue_number, branch)
  @heroku       = heroku
  @name         = name.to_s
  @project      = project
  @issue_number = issue_number
  @branch       = branch
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch


7
8
9
# File 'lib/reina/app.rb', line 7

def branch
  @branch
end

#gObject (readonly)

Returns the value of attribute g


7
8
9
# File 'lib/reina/app.rb', line 7

def g
  @g
end

#herokuObject (readonly)

Returns the value of attribute heroku


7
8
9
# File 'lib/reina/app.rb', line 7

def heroku
  @heroku
end

#issue_numberObject (readonly)

Returns the value of attribute issue_number


7
8
9
# File 'lib/reina/app.rb', line 7

def issue_number
  @issue_number
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/reina/app.rb', line 7

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project


7
8
9
# File 'lib/reina/app.rb', line 7

def project
  @project
end

Instance Method Details

#add_buildpacksObject


52
53
54
55
56
57
58
59
60
# File 'lib/reina/app.rb', line 52

def add_buildpacks
  buildpacks = project.fetch(:buildpacks, []).map do |buildpack|
    { 'buildpack' => buildpack }
  end + app_json.fetch('buildpacks', []).map do |buildpack|
    { 'buildpack' => buildpack['url'] }
  end

  heroku.buildpack_installation.update(app_name, 'updates' => buildpacks.uniq)
end

#add_to_pipelineObject


117
118
119
120
121
122
123
124
125
126
# File 'lib/reina/app.rb', line 117

def add_to_pipeline
  return if project[:pipeline].blank?

  pipeline_id = heroku.pipeline.info(project[:pipeline])['id']
  heroku.pipeline_coupling.create(
    'app'      => app_name,
    'pipeline' => pipeline_id,
    'stage'    => DEFAULT_STAGE
  )
end

#app_jsonObject


141
142
143
144
145
146
147
148
# File 'lib/reina/app.rb', line 141

def app_json
  return @app_json if @app_json

  f = File.join(name, 'app.json')
  return {} unless File.exists?(f)

  @app_json = JSON.parse(File.read(f))
end

#app_nameObject


154
155
156
# File 'lib/reina/app.rb', line 154

def app_name
  app_name_for(name)
end

#create_appObject


32
33
34
35
36
37
# File 'lib/reina/app.rb', line 32

def create_app
  heroku.app.create(
    'name'   => app_name,
    'region' => project.fetch(:region, DEFAULT_REGION)
  )
end

#deployObject


137
138
139
# File 'lib/reina/app.rb', line 137

def deploy
  g.push(remote_name, 'master')
end

#domain_nameObject


150
151
152
# File 'lib/reina/app.rb', line 150

def domain_name
  domain_name_for(app_name)
end

#execute_postdeploy_scriptsObject


128
129
130
131
132
133
134
135
# File 'lib/reina/app.rb', line 128

def execute_postdeploy_scripts
  script = app_json.dig('scripts', 'postdeploy')
  return if script.blank?

  return if heroku? && ENV['HEROKU_API_KEY'].blank?

  `heroku run #{script} --app #{app_name}`
end

#fetch_repositoryObject


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

def fetch_repository
  if Dir.exists?(name)
    @g = Git.open(name)
  else
    @g = Git.clone(github_url, name)
  end

  g.pull('origin', branch)
  g.checkout(g.branch(branch))

  unless g.remotes.map(&:name).include?(remote_name)
    g.add_remote(remote_name, remote_url)
  end
end

#install_addonsObject


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/reina/app.rb', line 39

def install_addons
  addons = project.fetch(:addons, []) + app_json.fetch('addons', [])
  addons.each do |addon|
    if addon.is_a?(Hash) && addon.has_key?('options')
      addon['config'] = addon.extract!('options')
    else
      addon = { 'plan' => addon }
    end

    heroku.addon.create(app_name, addon)
  end
end

#parallel?Boolean

Returns:

  • (Boolean)

162
163
164
# File 'lib/reina/app.rb', line 162

def parallel?
  project[:parallel] != false
end

#remote_nameObject


158
159
160
# File 'lib/reina/app.rb', line 158

def remote_name
  "heroku-#{app_name}"
end

#set_env_varsObject


62
63
64
65
66
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
96
97
98
99
100
101
102
103
104
# File 'lib/reina/app.rb', line 62

def set_env_vars
  config_vars = project.fetch(:config_vars, {})
  except = config_vars[:except]

  if config_vars.has_key?(:from)
    copy = config_vars.fetch(:copy, [])
    config_vars = heroku.config_var.info_for_app(config_vars[:from])

    vars_cache = {}
    copy.each do |h|
      unless h[:from].include?('#')
        s = config_vars[h[:from]]
        s << h[:append] if h[:append].present?
        config_vars[h[:to]] = s
        next
      end

      source, var = h[:from].split('#')
      source_app_name = app_name_for(source)

      if var == 'url'.freeze
        config_vars[h[:to]] = "https://#{domain_name_for(source_app_name)}"
      else
        vars_cache[source_app_name] ||= heroku.config_var.info_for_app(source_app_name)
        config_vars[h[:to]] = vars_cache[source_app_name][var]
      end
    end
  end

  config_vars.except!(*except) if except.present?

  config_vars['APP_NAME']        = app_name
  config_vars['HEROKU_APP_NAME'] = app_name
  config_vars['DOMAIN_NAME']     = domain_name
  config_vars['COOKIE_DOMAIN']   = '.herokuapp.com'.freeze

  app_json.fetch('env', {}).each do |key, hash|
    next if hash['value'].blank? || config_vars[key].present?
    config_vars[key] = hash['value']
  end

  heroku.config_var.update(app_name, config_vars)
end

#setup_dynosObject


106
107
108
109
110
111
112
113
114
115
# File 'lib/reina/app.rb', line 106

def setup_dynos
  formation = app_json.fetch('formation', {})
  return if formation.blank?

  formation.each do |k, h|
    h['size'] = 'free'

    heroku.formation.update(app_name, k, h)
  end
end