-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclusters_controller.rb
More file actions
189 lines (168 loc) · 6.21 KB
/
clusters_controller.rb
File metadata and controls
189 lines (168 loc) · 6.21 KB
1
2
3
4
5
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
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
class ClustersController < AuthenticatedUsersController
get '/clusters' do
clusters = Tendrl::Cluster.all
{ clusters: clusters }.to_json
end
before '/clusters/:cluster_id/?*?' do
@cluster = Tendrl::Cluster.find(params[:cluster_id])
unless @cluster.present?
raise Tendrl::HttpResponseErrorHandler.new(
StandardError.new,
cause: '/clusters/id',
object_id: params[:cluster_id]
)
end
end
get '/clusters/:cluster_id' do
@cluster.to_json
end
get '/clusters/:cluster_id/peers' do
peers = @cluster.gd2.get_peers.to_a
{ peers: peers }.to_json
end
get '/clusters/:cluster_id/volumes' do
volumes = @cluster.gd2.volume_list.to_a
{ volumes: volumes }.to_json
end
get '/clusters/:cluster_id/volumes/:volname' do
volume = @cluster.gd2.volume_info(params[:volname]).to_a
{ volume: volume }.to_json
end
post '/clusters/:cluster_id/volumes/:volname/start' do
@cluster.gd2.volume_start(params[:volname]).body
end
post '/clusters/:cluster_id/volumes/:volname/stop' do
@cluster.gd2.volume_stop(params[:volname]).body
end
get '/clusters/:cluster_id/volumes/:volname/bricks' do
bricks = @cluster.gd2.volume_bricks_status(params[:volname]).to_a
{ bricks: bricks }.to_json
end
post '/import' do
new_endpoint = {
'gd2_url' => parsed_body['gd2_url'],
'user' => parsed_body['user'],
'secret' => parsed_body['secret']
}
gd2 = Gd2Client.new new_endpoint
halt 404, 'Invalid endpoint' unless gd2.ping? && gd2.generate_api_methods
state = gd2.statedump.to_h
cluster = Tendrl::Cluster.new state['cluster-id']
unless cluster.data['endpoints'].include? new_endpoint
cluster.add_endpoint state['peer-id'], new_endpoint
end
cluster.add_short_name(parsed_body['short_name'])
begin
# TODO Publish webhook to GD2 as below:
# cluster.gd2.events_webhook_add(url: "http://<tendrl_server>/api/1.0/clusters/#{cluster.uuid}/event_webhook")
# TODO See below for event_webhook API.
rescue Tendrl::HttpResponseErrorHandler => e
details = e.body[:errors][:details]
raise e unless details.present? && details['errors'].first['code'].to_s == '1'
end
status 201
cluster.to_json
end
# Server sent events for subscribing to glusterd2 events
# TODO Add https://github.com/remy/polyfills/blob/master/EventSource.js polyfill to UI
# TODO Use http://walterbm.github.io/blog/2015/10/07/sinatra-and-server-sent-events/ as a reference on how to consume this in UI
connections = []
get '/clusters/:cluster_id/event_stream', provides: 'text/event-stream' do
stream(:keep_open) do |out|
connections << out
connections.reject!(&:closed?)
end
end
# This API listens to events from this cluster (to be published to GD2 as part of import)
# TODO Publish events to interested clients that have connected to the event_stream API
# TODO This API should also call prometheus alertmanager APIs if necessary
# TODO Add metric scraping jobs to prometheus server config, so that gluster-prometheus data starts appearing in our prometheus instance
post '/clusters/:cluster_id/event_webhook' do
connections.each do |conn|
conn << '{"data": "foobar"}'+"\n\n"
end
end
get '/clusters/:cluster_id/events' do
{ events: @cluster.gd2.events_list.to_a }.to_json
end
#get '/clusters/:cluster_id/nodes/:node_id/bricks' do
#bricks = @cluster.gd2.get("/endpoints")
#{ bricks: bricks }.to_json
#end
#get '/clusters/:cluster_id/notifications' do
#notifications = Tendrl::Notification.all
#NotificationPresenter.list_by_integration_id(notifications, params[:cluster_id]).to_json
#end
#get '/clusters/:cluster_id/jobs' do
#begin
#jobs = Tendrl::Job.all
#rescue Etcd::KeyNotFound
#jobs = []
#end
#{ jobs: JobPresenter.list_by_integration_id(jobs, params[:cluster_id]) }.to_json
#end
#post '/clusters/:cluster_id/unmanage' do
#Tendrl.load_node_definitions
#flow = Tendrl::Flow.new('namespace.tendrl', 'UnmanageCluster')
#body = JSON.parse(request.body.string.present? ? request.body.string : '{}')
#job = Tendrl::Job.new(
#current_user,
#flow,
#integration_id: params[:cluster_id]).create(body)
#status 202
#{ job_id: job.job_id }.to_json
#end
#post '/clusters/:cluster_id/expand' do
#Tendrl.load_node_definitions
#flow = Tendrl::Flow.new 'namespace.tendrl', 'ExpandClusterWithDetectedPeers'
#job = Tendrl::Job.new(
#current_user,
#flow,
#integration_id: params[:cluster_id]
#).create({})
#status 202
#{ job_id: job.job_id }.to_json
#end
#post '/clusters/:cluster_id/profiling' do
#Tendrl.load_definitions(params[:cluster_id])
#body = JSON.parse(request.body.read)
#volume_profiling_flag = if ['enable', 'disable'].include?(body['Cluster.volume_profiling_flag'])
#body['Cluster.volume_profiling_flag']
#else
#'leave-as-is'
#end
#flow = Tendrl::Flow.new('namespace.gluster', 'EnableDisableVolumeProfiling')
#job = Tendrl::Job.new(
#current_user,
#flow,
#integration_id: params[:cluster_id],
#type: 'sds'
#).create('Cluster.volume_profiling_flag' => volume_profiling_flag)
#status 202
#{ job_id: job.job_id }.to_json
#end
#post '/clusters/:cluster_id/volumes/:volume_id/start_profiling' do
#Tendrl.load_definitions(params[:cluster_id])
#flow = Tendrl::Flow.new('namespace.gluster', 'StartProfiling', 'Volume')
#job = Tendrl::Job.new(
#current_user,
#flow,
#integration_id: params[:cluster_id],
#type: 'sds'
#).create('Volume.vol_id' => params[:volume_id])
#status 202
#{ job_id: job.job_id }.to_json
#end
#post '/clusters/:cluster_id/volumes/:volume_id/stop_profiling' do
#Tendrl.load_definitions(params[:cluster_id])
#flow = Tendrl::Flow.new('namespace.gluster', 'StopProfiling', 'Volume')
#job = Tendrl::Job.new(
#current_user,
#flow,
#integration_id: params[:cluster_id],
#type: 'sds'
#).create('Volume.vol_id' => params[:volume_id])
#status 202
#{ job_id: job.job_id }.to_json
#end
end