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



183
184
185
# File 'lib/rebi/application.rb', line 183

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

.create_application(app_name, description = nil) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/rebi/application.rb', line 192

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



187
188
189
190
# File 'lib/rebi/application.rb', line 187

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



200
201
202
203
204
205
206
# File 'lib/rebi/application.rb', line 200

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 = get_environment stage_name, env_name
  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

#get_environment(stage_name, env_name) ⇒ Object



179
180
181
# File 'lib/rebi/application.rb', line 179

def get_environment stage_name, env_name
  Rebi::Environment.new stage_name, env_name, client
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 = get_environment stage_name, env_name
  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 = get_environment stage_name, env_name
  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


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rebi/application.rb', line 126

def print_list
  others = []
  configed = Hash.new {|h, k| h[k] = {} }
  environments.each do |e|
    if env_conf = Rebi.config.env_by_name(e.environment_name)
      configed[env_conf.stage.to_s].merge! env_conf.env_name.to_s => env_conf.name
    else
      others << e.environment_name
    end
  end

  configed.each do |stg, envs|
    Rebi.log "-------------"
    Rebi.log "#{stg.camelize}:"
    envs.each do |k, v|
      Rebi.log "\t#{k.camelize}: #{v}"
    end
  end

  if others.present?
    Rebi.log "-------------"
    Rebi.log "Others:"
    others.each do |e|
      Rebi.log "\t- #{e}"
    end
  end
end

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rebi/application.rb', line 154

def ssh_interaction stage_name, env_name, opts={}
  env = get_environment stage_name, env_name
  instance_ids = env.instance_ids
  return if instance_ids.empty?

  instance_ids.each.with_index do |i,idx|
    Rebi.log "#{idx+1}) #{i}"
  end

  instance_id = instance_ids.first

  if instance_ids.count != 1 && opts[:select]


    idx = 0
    while idx < 1 || idx > instance_ids.count
      idx = ask_for_integer "Select an instance to ssh into:"
    end
    instance_id = instance_ids[idx - 1]
  end

  Rebi.log "Preparing to ssh into [#{instance_id}]"
  env.ssh instance_id
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 = get_environment stage_name, env_name
  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