Class: LegacyBrokerController

Inherits:
ApplicationController
  • Object
show all
Includes:
CartridgeHelper, LegacyBrokerHelper, UserActionLogger
Defined in:
app/controllers/legacy_broker_controller.rb

Instance Method Summary collapse

Methods included from CartridgeHelper

#get_cartridges

Methods included from UserActionLogger

#get_action_logger, #log_action

Methods included from LegacyBrokerHelper

#get_cached

Instance Method Details

#cart_list_postObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/legacy_broker_controller.rb', line 195

def cart_list_post
  cart_type = @req.cart_type                                                                                                                                                                                                                                    
  unless cart_type
    log_action('nil', 'nil', 'nil', "LEGACY_CART_LIST", true, "Cartridge type not specified", get_extra_log_args)
    @reply.resultIO << "Invalid cartridge types: #{cart_type} specified"
    @reply.exitcode = 109
    render :json => @reply, :status => :bad_request
    return
  end

  cache_key = "cart_list_#{cart_type}"                                                                                                                                         
  carts = get_cached(cache_key, :expires_in => 21600.seconds) {
    Application.get_available_cartridges(cart_type)
  }
  log_action('nil', 'nil', 'nil', "LEGACY_CART_LIST")
  @reply.data = { :carts => carts }.to_json
  render :json => @reply
end

#cartridge_postObject

Raises:

  • (OpenShift::UserException)


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'app/controllers/legacy_broker_controller.rb', line 214

def cartridge_post
  raise OpenShift::UserException.new("Invalid user", 99) if @cloud_user.nil?
  
  case @req.action
  when 'configure'    #create app and configure framework
    apps = @cloud_user.applications
    domain = @cloud_user.domains.first
    app = Application.new(@cloud_user, @req.app_name, nil, @req.node_profile, @req.cartridge, nil, false, domain)
    check_cartridge_type(@req.cartridge, "standalone")
    if (@cloud_user.consumed_gears >= @cloud_user.max_gears)
      raise OpenShift::UserException.new("#{@login} has already reached the gear limit of #{@cloud_user.max_gears}", 104)
    end
    raise OpenShift::UserException.new("The supplied application name '#{app.name}' is not allowed", 105) if OpenShift::ApplicationContainerProxy.blacklisted? app.name
    if app.valid?
      @domain_name = domain.namespace
      @application_name = app.name
      @application_uuid = app.uuid
      begin
        app.user_agent = request.headers["User-Agent"]
        Rails.logger.debug "Creating application #{app.name}"
        @reply.append app.create
        Rails.logger.debug "Configuring dependencies #{app.name}"
        @reply.append app.configure_dependencies

        app.execute_connections
        begin
          @reply.append app.create_dns
          
          case app.framework_cartridge
            when 'php'
              page = 'health_check.php'
            when 'perl'
              page = 'health_check.pl'
            else
              page = 'health'
          end
        
          @reply.data = {:health_check_path => page, :uuid => app.uuid}.to_json
        rescue Exception => e
          @reply.append app.destroy_dns
          raise
        end
      rescue Exception => e
        log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_APP", false, "Failed to create application #{app.name}: #{e.message}", get_extra_log_args)
        @reply.append app.destroy(true)
        if app.persisted?
          app.delete
        end
        @reply.resultIO = StringIO.new(e.message)
        raise
      end
      log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_APP", true, "Created application #{app.name}", get_extra_log_args)
      @reply.resultIO << "Successfully created application: #{app.name}" if @reply.resultIO.length == 0
    else
      log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_APP", true, "Invalid application: #{app.errors.first[1][:message]}", get_extra_log_args)
      @reply.resultIO << app.errors.first[1][:message]
      @reply.exitcode = app.errors.first[1][:exit_code]
      render :json => @reply, :status => :bad_request 
      return
    end
  when 'deconfigure'
    app = get_app_from_request(@cloud_user)
    @reply.append app.cleanup_and_delete
    @reply.resultIO << "Successfully destroyed application: #{app.name}"
  when 'start'
    app = get_app_from_request(@cloud_user)
    @reply.append app.start(app.framework)
  when 'stop'
    app = get_app_from_request(@cloud_user)
    @reply.append app.stop(app.framework)
  when 'restart'
    app = get_app_from_request(@cloud_user)
    @reply.append app.restart(app.framework)
  when 'force-stop'
    app = get_app_from_request(@cloud_user)
    @reply.append app.force_stop(app.framework)
  when 'reload'
    app = get_app_from_request(@cloud_user)
    @reply.append app.reload(app.framework)
  when 'status'
    app = get_app_from_request(@cloud_user)
    @reply.append app.status(app.framework)
  when 'tidy'
    app = get_app_from_request(@cloud_user)
    @reply.append app.tidy(app.framework)
  when 'add-alias'
    app = get_app_from_request(@cloud_user)
    @reply.append app.add_alias @req.server_alias
  when 'remove-alias'
    app = get_app_from_request(@cloud_user)
    @reply.append app.remove_alias @req.server_alias
  when 'threaddump'
    app = get_app_from_request(@cloud_user)
    @reply.append app.threaddump(app.framework)
  when 'expose-port'
    app = get_app_from_request(@cloud_user)
    @reply.append app.expose_port(app.framework)
  when 'conceal-port'
    app = get_app_from_request(@cloud_user)
    @reply.append app.conceal_port(app.framework)
  when 'show-port'
    app = get_app_from_request(@cloud_user)
    @reply.append app.show_port(app.framework)
  when 'system-messages'
    app = get_app_from_request(@cloud_user)
    @reply.append app.system_messages
  else
    raise OpenShift::UserException.new("Invalid action #{@req.action}", 111)
  end
  @reply.resultIO << 'Success' if @reply.resultIO.length == 0
  log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CARTRIDGE_POST", true, "Processed event #{@req.action} for application #{app.name}", get_extra_log_args)
  
  render :json => @reply
end

#domain_postObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/legacy_broker_controller.rb', line 108

def domain_post
  domain = get_domain(@cloud_user, @req.namespace)
  domain = @cloud_user.domains.first if !domain && @req.alter
  
  if (!domain or not domain.hasFullAccess?(@cloud_user)) && (@req.alter || @req.delete)
    log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", true, "Cannot alter or remove namespace #{@req.namespace}. Namespace does not exist.", get_extra_log_args)
    @reply.resultIO << "Cannot alter or remove namespace #{@req.namespace}. Namespace does not exist.\n"
    @reply.exitcode = 106
    render :json => @reply, :status => :bad_request
    return
  end

  if @req.alter
    
    Rails.logger.debug "Updating namespace for domain #{domain.uuid} from #{domain.namespace} to #{@req.namespace}"

    raise OpenShift::UserException.new("The supplied namespace '#{@req.namespace}' is not allowed", 106) if OpenShift::ApplicationContainerProxy.blacklisted? @req.namespace   
    begin
      if domain.namespace != @req.namespace
        domain.namespace = @req.namespace     
        @reply.append domain.save
        log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", true, "Updated namespace for domain #{domain.uuid} to #{@req.namespace}", get_extra_log_args)
      end
    rescue Exception => e
     log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", false, "Failed to updated namespace for domain #{domain.uuid} to #{@req.namespace}", get_extra_log_args)
     Rails.logger.error "Failed to update domain #{domain.uuid} from #{domain.namespace} to #{@req.namespace} #{e.message}"
     Rails.logger.error e.backtrace
     raise
    end

    if @req.ssh
      @cloud_user.update_ssh_key(@req.ssh, @req.key_type, @req.key_name)
      @cloud_user.save
      log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", true, "Updated SSH key '#{@req.key_name}' for domain #{domain.namespace}", get_extra_log_args)
    end
  elsif @req.delete
     if not domain.hasFullAccess?(@cloud_user)
       log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_DELETE_DOMAIN", true, "Domain #{domain.namespace} is not associated with user", get_extra_log_args)
       @reply.resultIO << "Cannot remove namespace #{@req.namespace}. This namespace is not associated with login: #{@cloud_user.}\n"
       @reply.exitcode = 106
       render :json => @reply, :status => :bad_request
       return
     end
     if not @cloud_user.applications.empty?
       @cloud_user.applications.each do |app|
         if app.domain.uuid == domain.uuid
           log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_DELETE_DOMAIN", true, "Domain #{domain.namespace} contains applications", get_extra_log_args)
           @reply.resultIO << "Cannot remove namespace #{@req.namespace}. Remove existing app(s) first: "
           @reply.resultIO << @cloud_user.applications.map{|a| a.name}.join("\n")
           @reply.exitcode = 106 
           render :json => @reply, :status => :bad_request
       return
         end
       end
     end
     @reply.append domain.delete
     log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_DELETE_DOMAIN", true, "Deleted domain #{@req.namespace}", get_extra_log_args)
     render :json => @reply
     return
  else
    raise OpenShift::UserException.new("The supplied namespace '#{@req.namespace}' is not allowed", 106) if OpenShift::ApplicationContainerProxy.blacklisted? @req.namespace
    raise OpenShift::UserException.new("Domain already exists for user. Update the domain to modify.", 158) if !@cloud_user.domains.empty?

    key = Key.new(Key::DEFAULT_SSH_KEY_NAME, @req.key_type, @req.ssh)
    if key.invalid?
       log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_DOMAIN", true, "Failed to create domain #{@req.namespace}: #{key.errors.first[1][:message]}", get_extra_log_args)
       @reply.resultIO << key.errors.first[1][:message]
       @reply.exitcode = key.errors.first[1][:exit_code]
       render :json => @reply, :status => :bad_request 
       return
    end
    @cloud_user.add_ssh_key(Key::DEFAULT_SSH_KEY_NAME, @req.ssh, @req.key_type)
    domain = Domain.new(@req.namespace, @cloud_user)
    @reply.append domain.save
    log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_DOMAIN", true, "Created domain #{@req.namespace}", get_extra_log_args)
  end

  @reply.append @cloud_user.save
  @reply.data = {
    :rhlogin    => @cloud_user.,
    :uuid       => @cloud_user.uuid,
    :rhc_domain => Rails.configuration.openshift[:domain_suffix]
  }.to_json
    
  render :json => @reply
end

#embed_cartridge_postObject

Raises:

  • (OpenShift::UserException)


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'app/controllers/legacy_broker_controller.rb', line 329

def embed_cartridge_post
  raise OpenShift::UserException.new("Invalid user", 99) if @cloud_user.nil?
  
  app = get_app_from_request(@cloud_user)    
  check_cartridge_type(@req.cartridge, "embedded")
  
  # making this check here for the specific actions, so that the error codes for other conditions are not affected
  if ['deconfigure', 'start', 'stop', 'restart', 'status', 'reload'].include?(@req.action) and ( app.embedded.nil? or not app.embedded.has_key?(@req.cartridge) )
    raise OpenShift::UserException.new("The application #{app.name} is not configured with the embedded cartridge #{@req.cartridge}.", 129) 
  end

  Rails.logger.debug "DEBUG: Performing action '#{@req.action}'"    
  case @req.action
  when 'configure'
    if app.scalable && (@cloud_user.consumed_gears >= @cloud_user.max_gears) && @req.cartridge != 'jenkins-client-1.4'  #TODO Need a proper method to let us know if cart will get its own gear
      raise OpenShift::UserException.new("#{@login} has already reached the gear limit of #{@cloud_user.max_gears}", 104)
    end
    @reply.append app.add_dependency(@req.cartridge)
  when 'deconfigure'
    @reply.append app.remove_dependency(@req.cartridge)
  when 'start'
    @reply.append app.start(@req.cartridge)      
  when 'stop'
    @reply.append app.stop(@req.cartridge)      
  when 'restart'
    @reply.append app.restart(@req.cartridge)      
  when 'status'
    @reply.append app.status(@req.cartridge)      
  when 'reload'
    @reply.append app.reload(@req.cartridge)
  else
    raise OpenShift::UserException.new("Invalid action #{@req.action}", 111)           
  end
  
  log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_EMBED_CARTRIDGE_POST", true, "Processed event #{@req.action} for cartridge #{@req.cartridge} of application #{app.name}", get_extra_log_args)
  @reply.resultIO << 'Success' if @reply.resultIO.length == 0
  render :json => @reply
end

#ssh_keys_postObject



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
105
106
# File 'app/controllers/legacy_broker_controller.rb', line 62

def ssh_keys_post
  if @cloud_user    
    case @req.action
    when "add-key"
      raise OpenShift::UserKeyException.new("Missing SSH key or key name", 119) if @req.ssh.nil? or @req.key_name.nil?
      if @cloud_user.ssh_keys
        raise OpenShift::UserKeyException.new("Key with name #{@req.key_name} already exists.  Please choose a different name", 120) if @cloud_user.ssh_keys.has_key?(@req.key_name)
      end
      @cloud_user.add_ssh_key(@req.key_name, @req.ssh, @req.key_type)
      @cloud_user.save
    when "remove-key"
      raise OpenShift::UserKeyException.new("Missing key name", 119) if @req.key_name.nil?
      @cloud_user.remove_ssh_key(@req.key_name)
      @cloud_user.save
    when "update-key"
      raise OpenShift::UserKeyException.new("Missing SSH key or key name", 119) if @req.ssh.nil? or @req.key_name.nil?
      @cloud_user.update_ssh_key(@req.ssh, @req.key_type, @req.key_name)
      @cloud_user.save
    when "list-keys"
      #FIXME: when client tools are updated
      if @cloud_user.ssh_keys.nil? || @cloud_user.ssh_keys.empty?
        @reply.data = {:keys => {}, :ssh_key => "", :ssh_type => ""}.to_json
      else
        other_keys = @cloud_user.ssh_keys.reject {|k, v| k == Key::DEFAULT_SSH_KEY_NAME }
        if @cloud_user.ssh_keys.has_key?(Key::DEFAULT_SSH_KEY_NAME)
          default_key = @cloud_user.ssh_keys[Key::DEFAULT_SSH_KEY_NAME]['key'] 
          default_key_type = @cloud_user.ssh_keys[Key::DEFAULT_SSH_KEY_NAME]['type']
        else
          default_key = default_key_type = ""            
        end
        
        @reply.data = { :keys => other_keys, 
                      :ssh_key => default_key,
                      :ssh_type => default_key_type,
                    }.to_json
      end
    else
      raise OpenShift::UserKeyException.new("Invalid action #{@req.action}", 111)
    end
    log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_SSH_KEY", true, "Successfully completed action: #{@req.action}", get_extra_log_args)
    render :json => @reply
  else
    raise OpenShift::UserException.new("Invalid user", 99)
  end
end

#user_info_postObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/legacy_broker_controller.rb', line 16

def 
  if @cloud_user
     = @cloud_user.as_json
    #FIXME: This is redundant, for now keeping it for backward compatibility
    key_info = @cloud_user.get_ssh_key
    if key_info
      ["ssh_key"] = key_info['key']
      ["ssh_type"] = key_info['type']
    else
      ["ssh_key"] = ""
      ["ssh_type"] = ""
    end
      
    ["rhlogin"] = ["login"]
    .delete("login") 
    # this is to support old version of client tools
    if @cloud_user.domains and @cloud_user.domains.length > 0
      ["namespace"] = @cloud_user.domains.first.namespace
    end
    [:rhc_domain] = Rails.configuration.openshift[:domain_suffix]
    app_info = {}
    unless @cloud_user.applications.nil?
      @cloud_user.applications.each do |app|
        app_info[app.name] = {
          "framework" => app.framework,
          "creation_time" => app.creation_time,
          "uuid" => app.uuid,
          "aliases" => app.aliases,
          "embedded" => app.embedded
        }
      end
    end
    
    log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_USER_INFO", true, "", get_extra_log_args)
    @reply.data = {:user_info => , :app_info => app_info}.to_json
    render :json => @reply
  else
    log_action(@request_id, "nil", @login, "LEGACY_USER_INFO", true, "User not found", get_extra_log_args)
    # Return a 404 to denote the user doesn't exist
    @reply.resultIO << "User does not exist"
    @reply.exitcode = 99
    
    render :json => @reply, :status => :not_found
  end
end