Class: Rebi::Application

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#colorize, #colorize_prefix, #error, #error_label, #h1, #h2, #h3, #h4, #hstatus, #log

Constructor Details

#initialize(app, client = Rebi.eb) ⇒ Application

Returns a new instance of Application.



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

def initialize app, client=Rebi.eb
  @app = app
  @app_name = app.application_name
  @client = client
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#app_nameObject (readonly)

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#s3_clientObject (readonly)

Returns the value of attribute s3_client.



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

def s3_client
  @s3_client
end

Class Method Details

.clientObject



156
157
158
# File 'lib/rebi/application.rb', line 156

def self.client
  Rebi.eb
end

.create_application(app_name, description = nil) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/rebi/application.rb', line 165

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



160
161
162
163
# File 'lib/rebi/application.rb', line 160

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



173
174
175
176
177
178
179
180
# File 'lib/rebi/application.rb', line 173

def self.get_or_create_application app_name
  raise "App name cannot be nil" if app_name.blank?
  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

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



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

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
  begin
    env.deploy opts
  rescue Interrupt
    log("Interrupt")
  end
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
        error(e.message)
        opts[:trace] && e.backtrace.each do |m|
          error(m)
        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



152
153
154
# File 'lib/rebi/application.rb', line 152

def get_environment stage_name, env_name
  Rebi::Environment.new stage_name, env_name, client
end

#log_labelObject



19
20
21
# File 'lib/rebi/application.rb', line 19

def log_label
  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 = get_environment stage_name, env_name
  env.check_created!
  log("--------- CURRENT STATUS -------------")
  log("id: #{env.id}")
  log("Status: #{env.status}")
  log("Health: #{env.health}")
  log("--------------------------------------")
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

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


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

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|
    log h1("#{stg.camelize}")
    envs.each do |kname, env_name|
      env = get_environment stg, kname
      log "\t[#{kname.camelize}] #{env.name} #{h2(env.status)} #{hstatus(env.health)}"
    end
  end

  if others.present?
    log h1("Others")
    others.each do |e|
      log "\t- #{e}"
    end
  end
end

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



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

def ssh_interaction stage_name, env_name, opts={}
  env_name = Rebi.config.stage(stage_name).keys.first unless env_name
  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|
    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

  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
    log("Environment in updating")
    raise e
  end
end