Class: Rebi::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/rebi/application.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, client) ⇒ Application

Returns a new instance of Application.



4
5
6
7
8
9
# File 'lib/rebi/application.rb', line 4

def initialize app, client
  @app = app
  @app_name = app.application_name
  @client = client
  @s3_client = Aws::S3::Client.new
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/rebi/application.rb', line 3

def app
  @app
end

#app_nameObject (readonly)

Returns the value of attribute app_name.



3
4
5
# File 'lib/rebi/application.rb', line 3

def app_name
  @app_name
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/rebi/application.rb', line 3

def client
  @client
end

#s3_clientObject (readonly)

Returns the value of attribute s3_client.



3
4
5
# File 'lib/rebi/application.rb', line 3

def s3_client
  @s3_client
end

Class Method Details

.clientObject



126
127
128
# File 'lib/rebi/application.rb', line 126

def self.client
  Rebi.client || Aws::ElasticBeanstalk::Client.new
end

.create_application(app_name, description = nil) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/rebi/application.rb', line 135

def self.create_application app_name, description=nil
  res = client.create_application(
    application_name: app_name,
    description: description,
  )
  return Rebi::Application.new(res.application, client)
end

.get_application(app_name) ⇒ Object



130
131
132
133
# File 'lib/rebi/application.rb', line 130

def self.get_application app_name
  raise Error::ApplicationNotFound.new unless app = client.describe_applications(application_names: [app_name]).applications.first
  return Rebi::Application.new(app, client)
end

.get_or_create_application(app_name) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/rebi/application.rb', line 143

def self.get_or_create_application app_name
  begin
    return get_application app_name
  rescue Error::ApplicationNotFound
    return create_application app_name, Rebi.config.app_description
  end
end

Instance Method Details

#bucket_nameObject



11
12
13
# File 'lib/rebi/application.rb', line 11

def bucket_name
  @bucket_name ||= client.create_storage_location.s3_bucket
end

#create_app_version(env) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rebi/application.rb', line 100

def create_app_version env
  start = Time.now.utc
  source_bundle = Rebi::ZipHelper.new.gen(env.config)
  version_label = source_bundle[:label]
  key = "#{app_name}/#{version_label}.zip"
  Rebi.log("Uploading source bundle: #{version_label}.zip", env.config.name)
  s3_client.put_object(
    bucket: bucket_name,
    key: key,
    body: source_bundle[:file].read
    )
  Rebi.log("Creating app version: #{version_label}", env.config.name)
  client.create_application_version({
    application_name: app_name,
    description: source_bundle[:message],
    version_label: version_label,
    source_bundle: {
      s3_bucket: bucket_name,
      s3_key: key
      }
    })
  Rebi.log("App version was created in: #{Time.now.utc - start}s", env.config.name)
  return version_label
end

#deploy(stage_name, env_name = nil, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rebi/application.rb', line 19

def deploy stage_name, env_name=nil, opts={}
  return deploy_stage(stage_name, opts) if env_name.blank?
  env = Rebi::Environment.new stage_name, env_name, client
  app_version = create_app_version env
  begin
    req_id = env.deploy app_version, opts
    env.watch_request req_id if req_id
  rescue Rebi::Error::EnvironmentInUpdating => e
    Rebi.log("Environment in updating", env.name)
    raise e
  end
  req_id
end

#deploy_stage(stage_name, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rebi/application.rb', line 33

def deploy_stage stage_name, opts={}
  threads = []
  Rebi.config.stage(stage_name).each do |env_name, conf|
    next if conf.blank?
    threads << Thread.new do
      begin
        deploy stage_name, env_name, opts
      rescue Exception => e
        Rebi.log(e.message, "ERROR")
        e.backtrace.each do |m|
          Rebi.log(m, "ERROR")
        end
      end
    end
  end

  ThreadsWait.all_waits(*threads)
end

#environmentsObject



15
16
17
# File 'lib/rebi/application.rb', line 15

def environments
  Rebi::Environment.all app_name
end


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

def print_environment_status stage_name, env_name
  if env_name.blank?
    Rebi.config.stage(stage_name).each do |e_name, conf|
      next if conf.blank?
      print_environment_status stage_name, e_name
    end
    return
  end

  env = Rebi::Environment.new stage_name, env_name, client
  env.check_created!
  Rebi.log("--------- CURRENT STATUS -------------", env.name)
  Rebi.log("id: #{env.id}", env.name)
  Rebi.log("Status: #{env.status}", env.name)
  Rebi.log("Health: #{env.health}", env.name)
  Rebi.log("--------------------------------------", env.name)
end


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rebi/application.rb', line 52

def print_environment_variables stage_name, env_name, from_config=false
  if env_name.blank?
    Rebi.config.stage(stage_name).each do |e_name, conf|
      next if conf.blank?
      print_environment_variables stage_name, e_name, from_config
    end
    return
  end

  env = Rebi::Environment.new stage_name, env_name, client
  env_vars = from_config ? env.config.environment_variables : env.environment_variables

  Rebi.log("#{from_config ? "Config" : "Current"} environment variables", env.name)
  env_vars.each do |k,v|
    Rebi.log("#{k}=#{v}")
  end
  Rebi.log("--------------------------------------", env.name)
end

#terminate!(stage_name, env_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/rebi/application.rb', line 89

def terminate! stage_name, env_name
  env = Rebi::Environment.new stage_name, env_name, client
  begin
    req_id = env.terminate!
    ThreadsWait.all_waits(env.watch_request req_id) if req_id
  rescue Rebi::Error::EnvironmentInUpdating => e
    Rebi.log("Environment in updating", env.name)
    raise e
  end
end