Class: KPM::NodesInfoController

Inherits:
EngineController show all
Includes:
ActionController::Live
Defined in:
app/controllers/kpm/nodes_info_controller.rb

Instance Method Summary collapse

Methods inherited from EngineController

#current_tenant_user, #get_layout, #options_for_klient

Instance Method Details

#indexObject



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
# File 'app/controllers/kpm/nodes_info_controller.rb', line 10

def index
  @installing = params[:i].present?

  @nodes_info = ::KillBillClient::Model::NodesInfo.nodes_info(options_for_klient)

  # For convenience, put pure OSGI bundles at the bottom
  @nodes_info.each do |node_info|
    next if node_info.plugins_info.nil?

    node_info.plugins_info.sort! do |a, b|
      if osgi_bundle?(a) && !osgi_bundle?(b)
        1
      elsif !osgi_bundle?(a) && osgi_bundle?(b)
        -1
      else
        a.plugin_name <=> b.plugin_name
      end
    end
  end

  @kb_host = params[:kb_host] || KillBillClient::API.base_uri
  @last_event_id = params[:last_event_id]

  @tenant_plugin_config = {}
  begin
    raw_tenant_config = ::KillBillClient::Model::Tenant.search_tenant_config('PLUGIN_CONFIG_', options_for_klient)
    @tenant_plugin_config = raw_tenant_config.each_with_object({}) do |e, hsh|
      plugin_name = e.key.gsub('PLUGIN_CONFIG_', '')
      hsh[plugin_name] = e.values[0]
    end
  rescue StandardError => e
    Rails.logger.warn("Unable to fetch tenant plugin config: #{e.inspect}")
  end
end

#install_pluginObject



90
91
92
93
# File 'app/controllers/kpm/nodes_info_controller.rb', line 90

def install_plugin
  trigger_node_plugin_command('INSTALL_PLUGIN')
  redirect_to nodes_info_index_path(i: 1)
end

#refreshObject



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
# File 'app/controllers/kpm/nodes_info_controller.rb', line 45

def refresh
  response.headers['Content-Type'] = 'text/event-stream'

  last_event_id_ref = Concurrent::AtomicReference.new(request.headers['Last-Event-Id'] || params[:last_event_id])
  sse = nil
  sse_client = nil
  begin
    # Kaui -> Browser
    sse = ActionController::Live::SSE.new(response.stream, retry: 300, event: 'refresh')

    # Kill Bill -> Kaui
    sse_client = ::Killbill::KPM::KPMClient.stream_osgi_logs(sse, params[:kb_host], last_event_id_ref)

    i = 0
    # We force the browser to reconnect periodically (ensures clients don't block the server shutdown sequence)
    while i < 6 # 30s
      i += 1
      # Keep the thread alive (Kill Bill should send us a heartbeat as well though)
      # Note that we set the id as the last log id, so that we can easily resume
      sse.write('heartbeat', id: last_event_id_ref.get)
      sleep 5
    end
  rescue ActionController::Live::ClientDisconnected
    # ignored
  ensure
    begin
      begin
        sse_client&.close
      rescue StandardError
        # ignored
      end
      sse&.close
    ensure
      # Clear dead DB connections
      # Very lame, but I couldn't do better... Rails will checkout a DB connection
      # whenever a new Thread is spawn and ActiveRecord::Base.clear_active_connections!
      # didn't seem to do the trick: the number of active and dead connections kept growing:
      #     connections = ActiveRecord::Base.connection_pool.instance_eval { @connections }
      #     busy = connections.count { |c| c.in_use? }
      #     dead = connections.count { |c| c.in_use? && !c.owner.alive? }
      ActiveRecord::Base.connection_pool.reap
    end
  end
end

#restart_pluginObject



138
139
140
141
# File 'app/controllers/kpm/nodes_info_controller.rb', line 138

def restart_plugin
  trigger_node_plugin_command('RESTART_PLUGIN')
  head :ok
end

#start_pluginObject



100
101
102
103
# File 'app/controllers/kpm/nodes_info_controller.rb', line 100

def start_plugin
  trigger_node_plugin_command('START_PLUGIN')
  head :ok
end

#stop_pluginObject



105
106
107
108
# File 'app/controllers/kpm/nodes_info_controller.rb', line 105

def stop_plugin
  trigger_node_plugin_command('STOP_PLUGIN')
  head :ok
end

#uninstall_pluginObject



95
96
97
98
# File 'app/controllers/kpm/nodes_info_controller.rb', line 95

def uninstall_plugin
  trigger_node_plugin_command('UNINSTALL_PLUGIN')
  head :ok
end

#upload_plugin_configObject



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
# File 'app/controllers/kpm/nodes_info_controller.rb', line 110

def upload_plugin_config
  plugin_name = params[:plugin_name]
  plugin_config = params[:plugin_config]

  if plugin_name.blank?
    flash[:error] = 'Plugin name cannot be blank'
  elsif plugin_config.blank?
    flash[:error] = 'Plugin configuration cannot be blank'
  else
    begin
      user = current_tenant_user
      ::KillBillClient::Model::Tenant.upload_tenant_plugin_config(
        plugin_name,
        plugin_config.gsub(/\r\n?/, "\n"),
        user[:username],
        nil,
        nil,
        options_for_klient
      )
      flash[:notice] = 'Plugin configuration was successfully uploaded'
    rescue StandardError => e
      flash[:error] = "Failed to upload plugin configuration: #{e.message}"
    end
  end

  redirect_to nodes_info_index_path
end