Class: AppEventsController

Inherits:
BaseController show all
Defined in:
app/controllers/app_events_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::API_VERSION, BaseController::SUPPORTED_API_VERSIONS

Instance Method Summary collapse

Methods inherited from BaseController

#show

Methods included from UserActionLogger

#get_action_logger, #log_action

Instance Method Details

#createObject

POST /domains//applications//events



6
7
8
9
10
11
12
13
14
15
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
61
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
107
108
109
110
111
112
113
114
# File 'app/controllers/app_events_controller.rb', line 6

def create
  domain_id = params[:domain_id]
  id = params[:application_id]
  event = params[:event]
  server_alias = params[:alias]

  domain = Domain.get(@cloud_user, domain_id)
  return render_error(:not_found, "Domain #{domain_id} not found", 127,
                      "APPLICATION_EVENT") if !domain || !domain.hasAccess?(@cloud_user)

  @domain_name = domain.namespace
  application = get_application(id)
  return render_error(:not_found, "Application '#{id}' not found", 101,
                      "APPLICATION_EVENT") unless application
  
  @application_name = application.name
  @application_uuid = application.uuid
  return render_error(:unprocessable_entity, "Alias must be specified for adding or removing application alias.", 126,
                      "APPLICATION_EVENT", "event") if ['add-alias', 'remove-alias'].include?(event) && !server_alias
  return render_error(:unprocessable_entity, "Reached gear limit of #{@cloud_user.max_gears}", 104,
                      "APPLICATION_EVENT") if (event == 'scale-up') && (@cloud_user.consumed_gears >= @cloud_user.max_gears)

  msg = "Added #{event} to application #{id}"
  begin
    case event
      when "start"
        application.start
        msg = "Application #{id} has started"
      when "stop"
        application.stop
        msg = "Application #{id} has stopped"
      when "force-stop"
        application.force_stop
        msg = "Application #{id} has forcefully stopped"
      when "restart"
        application.restart
        msg = "Application #{id} has restarted"
      when "expose-port"
        application.expose_port
        msg = "Application #{id} has exposed port"
      when "conceal-port"
        application.conceal_port
        msg = "Application #{id} has concealed port"
      when "show-port"
        r = application.show_port
        msg = "Application #{id} called show port"
        msg += ": #{r.resultIO.string.chomp}" if !r.resultIO.string.empty?
      when "add-alias"
        applications = Application.find_all(@cloud_user)
        applications.each do |app|
          app.aliases.each do |a|
            if a.casecmp(server_alias) == 0
              return render_error(:unprocessable_entity, "Alias already in use.", 140,
                 "APPLICATION_EVENT", "event")
            end 
          end unless app.aliases.nil?
        end
        r = application.add_alias(server_alias)
        msg = "Application #{id} has added alias"
        msg += ": #{r.resultIO.string.chomp}" if !r.resultIO.string.empty?
      when "remove-alias"
        r = application.remove_alias(server_alias)
        msg = "Application #{id} has removed alias"
        msg += ": #{r.resultIO.string.chomp}" if !r.resultIO.string.empty?
      when "scale-up"
        application.scaleup
        msg = "Application #{id} has scaled up"
      when "scale-down"
        application.scaledown
        msg = "Application #{id} has scaled down"
      when 'tidy'
        r = application.tidy
        msg = "Application #{id} called tidy"
        msg += ": #{r.resultIO.string.chomp}" if !r.resultIO.string.empty?
      when 'reload'
        r = application.reload
        msg = "Application #{id} called reload"
        msg += ": #{r.resultIO.string.chomp}" if !r.resultIO.string.empty?
      when "thread-dump"
        r = application.threaddump
        msg = !r.errorIO.string.empty? ? r.errorIO.string.chomp : r.resultIO.string.chomp
        #TODO: We need to reconsider how we are reporting messages to the client
        success_msg = "Application event '#{event}' successful" unless msg.include?("not supported")
        message = Message.new(:result, msg, 0)
        application = get_application(id)
        if $requested_api_version >= 1.2
          app = RestApplication12.new(application, get_url, nolinks)
        else
          app = RestApplication10.new(application, get_url, nolinks)
        end
        render_success(:ok, "application", app, "#{event.sub('-', '_').upcase}_APPLICATION",
	   success_msg, true, nil, [message])
 return
     else
        return render_error(:unprocessable_entity, "Invalid application event '#{event}' specified",
                            126, "APPLICATION_EVENT", "event")
      end
  rescue Exception => e
    return render_exception(e, "#{event.sub('-', '_').upcase}_APPLICATION")
  end
  application = get_application(id)
  if $requested_api_version >= 1.2
    app = RestApplication12.new(application, get_url, nolinks)
  else
    app = RestApplication10.new(application, get_url, nolinks)
  end
  render_success(:ok, "application", app, "#{event.sub('-', '_').upcase}_APPLICATION",
                 "Application event '#{event}' successful", true)
end