This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 516
/
openshift.rb
1371 lines (1161 loc) · 45.8 KB
/
openshift.rb
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
328
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'rubygems'
require 'open4'
require 'pp'
require 'json'
require 'zlib'
require 'base64'
require 'etc'
require 'openshift-origin-node'
require 'openshift-origin-node/model/cartridge_repository'
require 'openshift-origin-node/utils/hourglass'
require 'openshift-origin-common/utils/path_utils'
require 'openshift-origin-common/utils/file_needs_sync'
require 'openshift-origin-node/utils/selinux_context'
require 'shellwords'
require 'facter'
require 'stringio'
require 'timeout'
module MCollective
module Agent
class Openshift<RPC::Agent
# Metadata moved to openshift.ddl
# Maps NodeLogger context keys to request data "args" keys
LOGGER_CONTEXT_ENTRIES = {
request_id: '--with-request-id',
container_uuid: '--with-container-uuid',
app_uuid: '--with-app-uuid'
}
activate_when do
@@config = ::OpenShift::Config.new
# We need to call this here so the pthread local destructor function that calls matchpathcon_fini is associated
# with this long lived thread. Otherwise, it will be invoked for every OpenShift mco agent method that ends up
# calling OpenShift::Runtime::Utils::SELinux#chcon
#
# @see https://bugzilla.redhat.com/show_bug.cgi?id=1081249
@@selinux = ::OpenShift::Runtime::Utils::SelinuxContext.instance
PathUtils.flock('/var/lock/oo-cartridge-repository') do
@@cartridge_repository = ::OpenShift::Runtime::CartridgeRepository.instance
Dir.glob(PathUtils.join(@@config.get('CARTRIDGE_BASE_PATH'), '*')).each do |path|
begin
manifest = @@cartridge_repository.install(path)
OpenShift::Runtime::NodeLogger.logger.info("openshift-agent: Installed cartridge (#{manifest.cartridge_vendor}, #{manifest.name}, #{manifest.version}, #{manifest.cartridge_version}) from #{path}")
rescue Exception => e
OpenShift::Runtime::NodeLogger.logger.warn("openshift-agent: Failed to install cartridge from #{path}. #{e.message}")
end
end
end
OpenShift::Runtime::NodeLogger.logger.info(
"openshift-agent: #{@@cartridge_repository.count} cartridge(s) installed in #{@@cartridge_repository.path}")
begin
if path = @@config.get('AGENT_EXTENSION')
require path
include ::Openshift::AgentExtension
agent_startup
end
rescue Exception => e
Log.instance.error("#{e.message}\n#{e.backtrace.join("\n")}")
end
true
end
def startup_hook
# If meta[:timeout] is not set Node operations will fail as default is too low
unless defined?(@@hourglass_timeout)
@@hourglass_timeout = (meta[:timeout] * 0.65).ceil
Log.instance.info("Node transaction timeout is #{@@hourglass_timeout}, mcollective timeout is #{meta[:timeout]}")
end
end
def before_processing_hook(msg, connection)
# Set working directory to a 'safe' directory to prevent Dir.chdir
# calls with block from changing back to a directory which doesn't exist.
Dir.chdir('/tmp')
# Set up the NodeLogger context for the duration of this request
args = request.data[:args] ||= {}
LOGGER_CONTEXT_ENTRIES.each {|k, v| OpenShift::Runtime::NodeLogger.context[k] = args[v].to_s if args[v]}
OpenShift::Runtime::NodeLogger.logger.info("openshift-agent: request start: action=#{request.action} requestid=#{request.uniqid}, "\
"senderid=#{request.sender}, data=#{request.data}")
end
def after_processing_hook
OpenShift::Runtime::NodeLogger.logger.info("openshift-agent: request end: action=#{request.action}, requestid=#{request.uniqid}, "\
"senderid=#{request.sender}, statuscode=#{reply.statuscode}, data=#{reply.data}")
# Clear the NodeLogger context for the request
LOGGER_CONTEXT_ENTRIES.each {|k, v| OpenShift::Runtime::NodeLogger.context.delete(k)}
end
def echo_action
validate :msg, String
reply[:msg] = request[:msg]
end
def cleanpwd(arg)
arg.gsub(/(passwo?r?d\s*[:=]+\s*)\S+/i, '\\1[HIDDEN]').gsub(/(usern?a?m?e?\s*[:=]+\s*)\S+/i, '\\1[HIDDEN]')
end
def get_facts_action
hg = OpenShift::Runtime::Utils::Hourglass.new(@@hourglass_timeout)
max = hg.remaining
reply[:output] = {}
request[:facts].each do |fact|
begin
Timeout::timeout(hg.remaining) do
reply[:output][fact.to_sym] = MCollective::Util.get_fact(fact)
end
rescue Timeout::Error
# forget it
end
if hg.expired?
reply.fail! "Failed to obtain requested facts in #{max}s"
break
end
end
end
# Handles all incoming messages. Validates the input, executes the action, and constructs
# a reply.
def cartridge_do_action
validate :cartridge, :shellsafe
validate :action, :shellsafe
action = request[:action]
args = request[:args] ||= {}
args['--with-hourglass'] = OpenShift::Runtime::Utils::Hourglass.new(@@hourglass_timeout)
# Do the action execution
exitcode, output, addtl_params = execute_action(action, args)
if args['--with-container-uuid']
report_quota(output, args['--with-container-uuid'])
if report_resource(output, args['--with-app-name'])
exitcode = 222
end
end
reply[:exitcode] = exitcode
reply[:output] = output
reply[:addtl_params] = addtl_params
args.delete('--with-hourglass')
if exitcode != 0
reply.fail! "cartridge_do_action failed #{exitcode}. Output #{output}"
end
end
# Dispatches the given action to a method on the agent.
#
# Returns [exitcode, output] from the resulting action execution.
def execute_action(action, args)
start = Time.now
action_method = "oo_#{action.gsub('-', '_')}"
exitcode = 0
output = ""
addtl_params = nil
begin
if self.respond_to?(action_method)
exitcode, output, addtl_params = self.send(action_method.to_sym, args)
else
exitcode = 127
output = "Unsupported action: #{action}/#{action_method}"
end
rescue Exception => e
report_exception e
exitcode = 127
output = "An internal exception occurred processing action #{action}: #{e.message}\n#{e.backtrace.join("\n")}"
end
return exitcode, output, addtl_params
end
# report approaching quota overage.
#
def report_quota(buffer, uuid)
watermark = @@config.get('QUOTA_WARNING_PERCENT', '90.0').to_f
::OpenShift::Runtime::Node.check_quotas(uuid, watermark).each do |line|
buffer << "\nCLIENT_MESSAGE: #{line}\n"
end
end
def report_resource(buffer, app_name = '<app>')
buffer.match(/Resource temporarily unavailable/) do
buffer << "\nCLIENT_MESSAGE: Resources unavailable for operation. You may need to run 'rhc force-stop-app -a #{app_name}' and retry.\n"
return true
end
false
end
# Executes a list of jobs sequentially, adding the exitcode and output
# from execute_action to each job following the execution.
#
# The actual message reply object is set with an exitcode of 0 and
# output containing the job list (in which the individual execution
# results are embedded).
#
# BZ 876942: Disable threading until we can explore proper concurrency management
def execute_parallel_action
start = Time.now
hourglass = OpenShift::Runtime::Utils::Hourglass.new(@@hourglass_timeout)
quota_reported = false
joblist = request[config.identity]
joblist.each do |parallel_job|
job = parallel_job[:job]
action = job[:action]
args = job[:args]
args['--with-hourglass'] = hourglass
exitcode, output, addtl_params = execute_action(action, args)
if args['--with-container-uuid']
if !quota_reported && !['app-state-show'].include?(action)
report_quota(output, args['--with-container-uuid'])
quota_reported = true
end
if report_resource(output, args['--with-app-name'])
exitcode = 222
end
end
parallel_job[:result_exit_code] = exitcode
parallel_job[:result_stdout] = output
parallel_job[:result_addtl_params] = addtl_params
args.delete('--with-hourglass')
end
reply[:output] = joblist
reply[:exitcode] = 0
end
#
# Upgrade between versions
#
def upgrade_action
validate :uuid, /^[a-zA-Z0-9]+$/
validate :app_uuid, /^[a-zA-Z0-9]+$/
validate :version, /^.+$/
validate :namespace, /^.+$/
uuid = request[:uuid]
application_uuid = request[:app_uuid]
namespace = request[:namespace]
version = request[:version]
ignore_cartridge_version = request[:ignore_cartridge_version] == 'true' ? true : false
scalable = request[:scalable]
hostname = Facter.value(:hostname)
error_message = nil
exitcode = 0
begin
require 'openshift-origin-node/model/upgrade'
upgrader = OpenShift::Runtime::Upgrader.new(uuid, application_uuid, namespace, version, hostname, ignore_cartridge_version, scalable,
OpenShift::Runtime::Utils::Hourglass.new(@@hourglass_timeout))
rescue Exception => e
report_exception e
exitcode = 1
error_message = "Failed to instantiate the upgrader; this is typically due to the gear being corrupt or missing its UNIX account.\n"\
"Exception: #{e.message}\n#{e.backtrace.join("\n")}"
else
begin
result = upgrader.execute
rescue LoadError => e
report_exception e
exitcode = 127
error_message = "Upgrade not supported: #{e.message}"
rescue OpenShift::Runtime::Utils::ShellExecutionException => e
report_exception e
exitcode = 2
error_message = "Gear failed to upgrade due to an unhandled shell execution: #{e.message}\n#{e.backtrace.join("\n")}\n"\
"Stdout: #{e.stdout}\nStderr: #{e.stderr}"
rescue Exception => e
report_exception e
exitcode = 3
error_message = "Gear failed to upgrade due to an unhandled internal exception: #{e.message}\n#{e.backtrace.join("\n")}"
end
end
reply[:output] = error_message
reply[:exitcode] = exitcode
reply[:upgrade_result_json] = JSON.dump(result) if result
reply.fail! "upgrade_action failed with exit code #{exitcode}. Output: #{error_message}" unless exitcode == 0
end
#
# Builds a new ApplicationContainer instance from the standard
# argument payload which is expected for any message used for
# gear/cart operations.
#
# Use this to get a new ApplicationContainer instance in all cases.
#
# A new OpenShift::Runtime::Hourglass will be initialized and passed
# to the ApplicationContainerInstance to allow for timing consistency.
# The hourglass will be initialized with a duration shorter than the
# configured MCollective agent timeout.
#
def get_app_container_from_args(args)
app_uuid = args['--with-app-uuid'].to_s if args['--with-app-uuid']
app_name = args['--with-app-name'].to_s if args['--with-app-name']
gear_uuid = args['--with-container-uuid'].to_s if args['--with-container-uuid']
gear_name = args['--with-container-name'].to_s if args['--with-container-name']
namespace = args['--with-namespace'].to_s if args['--with-namespace']
quota_blocks = args['--with-quota-blocks']
quota_files = args['--with-quota-files']
uid = args['--with-uid']
hourglass = args['--with-hourglass'] || OpenShift::Runtime::Utils::Hourglass.new(@@hourglass_timeout)
quota_blocks = nil if quota_blocks && quota_blocks.to_s.empty?
quota_files = nil if quota_files && quota_files.to_s.empty?
uid = nil if uid && uid.to_s.empty?
OpenShift::Runtime::ApplicationContainer.new(app_uuid, gear_uuid, uid, app_name, gear_name,
namespace, quota_blocks, quota_files, hourglass)
end
# Yields an ApplicationContainer constructed from the given args. No exceptions will be raised
# and the method will return a tuple of a return code integer and output string.
def with_container_from_args(args)
output = ''
begin
container = get_app_container_from_args(args)
yield(container, output)
return 0, output
rescue OpenShift::Runtime::Utils::ShellExecutionException => e
# Removed reporting exception, stdout and stderr carry the necessary information to the user from cartridge
# https://bugzilla.redhat.com/show_bug.cgi?id=1101169
# report_exception e
output << "\n" unless output.empty?
output << "#{e.message}" if e.message
output << "\n#{e.stdout}" if e.stdout.is_a?(String)
output << "\n#{e.stderr}" if e.stderr.is_a?(String)
return e.rc, output
rescue Exception => e
report_exception e
return 1, e.message
end
end
def oo_app_create(args)
output = ''
begin
token = args.key?('--with-secret-token') ? args['--with-secret-token'].to_s : nil
generate_app_key = args.key?('--with-generate-app-key') ? args['--with-generate-app-key'] : false
create_initial_deployment_dir = args.key?('--with-initial-deployment-dir') ? args['--with-initial-deployment-dir'] : true
container = get_app_container_from_args(args)
return 0, container.create(token, generate_app_key, create_initial_deployment_dir)
rescue OpenShift::Runtime::UserCreationException => e
report_exception e
return 129, e.message
rescue OpenShift::Runtime::GearCreationException => e
report_exception e
return 146, e.message
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
end
end
def oo_app_destroy(args)
skip_hooks = args['--skip-hooks'] ? args['--skip-hooks'] : false
output = ""
begin
container = get_app_container_from_args(args)
out, err, rc = container.destroy(skip_hooks)
output << out
output << err
return rc, output
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
end
end
def oo_update_configuration(args)
config = args['--with-config']
auto_deploy = config['auto_deploy']
deployment_branch = config['deployment_branch']
keep_deployments = config['keep_deployments']
deployment_type = config['deployment_type']
with_container_from_args(args) do |container|
container.set_auto_deploy(auto_deploy)
container.set_deployment_branch(deployment_branch)
container.set_keep_deployments(keep_deployments)
container.set_deployment_type(deployment_type)
end
end
def oo_deploy(args)
hot_deploy = args['--with-hot-deploy']
force_clean_build = args['--with-force-clean-build']
ref = args['--with-ref']
artifact_url = args['--with-artifact-url']
out = StringIO.new
err = StringIO.new
addtl_params = nil
rc, output = with_container_from_args(args) do |container|
container.deploy(hot_deploy: hot_deploy, force_clean_build: force_clean_build, ref: ref, artifact_url: artifact_url, out: out, err: err)
addtl_params = {deployments: container.calculate_deployments}
end
return rc, output, addtl_params
end
def oo_activate(args)
deployment_id = args['--with-deployment-id']
out = StringIO.new
err = StringIO.new
addtl_params = nil
rc, output = with_container_from_args(args) do |container|
container.activate(deployment_id: deployment_id, out: out, err: err)
addtl_params = {deployments: container.calculate_deployments}
end
return rc, output, addtl_params
end
def oo_authorized_ssh_key_add(args)
ssh_key = args['--with-ssh-key']
key_type = args['--with-ssh-key-type']
comment = args['--with-ssh-key-comment']
with_container_from_args(args) do |container|
container.add_ssh_keys([{:content => ssh_key, :type => key_type, :comment => comment}])
end
end
def oo_authorized_ssh_key_batch_add(args)
ssh_keys = args['--with-ssh-keys']
with_container_from_args(args) do |container|
container.add_ssh_keys(ssh_keys)
end
end
def oo_authorized_ssh_key_remove(args)
ssh_key = args['--with-ssh-key']
key_type = args['--with-ssh-key-type']
comment = args['--with-ssh-comment']
with_container_from_args(args) do |container|
container.remove_ssh_key(ssh_key, key_type, comment)
end
end
def oo_authorized_ssh_key_batch_remove(args)
ssh_keys = args['--with-ssh-keys']
with_container_from_args(args) do |container|
container.remove_ssh_keys(ssh_keys)
end
end
def oo_authorized_ssh_keys_replace(args)
ssh_keys = args['--with-ssh-keys'] || []
begin
container = get_app_container_from_args(args)
container.replace_ssh_keys(ssh_keys)
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
else
return 0, ""
end
end
def oo_broker_auth_key_add(args)
iv = args['--with-iv']
token = args['--with-token']
with_container_from_args(args) do |container|
container.add_broker_auth(iv, token)
end
end
def oo_broker_auth_key_remove(args)
with_container_from_args(args) do |container|
container.remove_broker_auth
end
end
def oo_env_var_add(args)
key = args['--with-key']
value = args['--with-value']
with_container_from_args(args) do |container|
container.add_env_var(key, value)
end
end
def oo_env_var_remove(args)
key = args['--with-key']
with_container_from_args(args) do |container|
container.remove_env_var(key)
end
end
def oo_user_var_add(args)
variables = {}
if args['--with-variables']
JSON.parse(args['--with-variables']).each {|env| variables[env['name']] = env['value']}
end
gears = args['--with-gears'] ? args['--with-gears'].split(';') : []
if variables.empty? and gears.empty?
return -1, "In #{__method__} at least user environment variables or gears must be provided for #{args['--with-app-name']}"
end
cmd_rc, cmd_output = 0, ''
wrapper_rc, wrapper_output = with_container_from_args(args) do |container|
cmd_rc, cmd_output = container.user_var_add(variables, gears)
end
if wrapper_rc == 0
return cmd_rc, cmd_output
else
return wrapper_rc, wrapper_output
end
end
def oo_user_var_remove(args)
unless args['--with-keys']
return -1, "In #{__method__} no user environment variable names provided for #{args['--with-app-name']}"
end
keys = args['--with-keys'].split(' ')
gears = args['--with-gears'] ? args['--with-gears'].split(';') : []
cmd_rc, cmd_output = 0, ''
wrapper_rc, wrapper_output = with_container_from_args(args) do |container|
cmd_rc, cmd_output = container.user_var_remove(keys, gears)
end
if wrapper_rc == 0
return cmd_rc, cmd_output
else
return wrapper_rc, wrapper_output
end
end
def oo_user_var_list(args)
keys = args['--with-keys'] ? args['--with-keys'].split(' ') : []
begin
container = get_app_container_from_args(args)
list = container.user_var_list(keys)
return 0, 'CLIENT_RESULT: ' + list.to_json
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
end
end
def oo_cartridge_list(args)
list_descriptors = true if args['--with-descriptors']
porcelain = true if args['--porcelain']
begin
return 0, OpenShift::Runtime::Node.get_cartridge_list(list_descriptors, porcelain, false)
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
end
end
def oo_app_state_show(args)
with_container_from_args(args) do |container, output|
output << "\nCLIENT_RESULT: #{container.state.value}\n"
end
end
def oo_get_quota(args)
uuid = args['--uuid'].to_s if args['--uuid']
begin
q = OpenShift::Runtime::Node.get_quota(uuid)
return 0, [
q[:device],
q[:blocks_used].to_s, q[:blocks_quota].to_s, q[:blocks_limit].to_s,
q[:inodes_used].to_s, q[:inodes_quota].to_s, q[:inodes_limit].to_s
]
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
end
end
def oo_set_quota(args)
uuid = args['--uuid'].to_s if args['--uuid']
blocks = args['--blocks']
inodes = args['--inodes']
begin
return 0, OpenShift::Runtime::Node.set_quota(uuid, blocks, inodes)
rescue Exception => e
report_exception e
return 1, "#{e.message}\n#{e.backtrace.join("\n")}"
end
end
def oo_force_stop(args)
with_container_from_args(args) do |container|
container.force_stop
end
end
#
# Instantiate the front-end class from the given arguments and
# follow proper exception handling pattern.
#
def with_frontend_rescue_pattern
output = ""
begin
yield(output)
rescue OpenShift::Runtime::FrontendHttpServerExecException => e
report_exception e
OpenShift::Runtime::NodeLogger.logger.error("#{e.message}\n#{e.backtrace.join("\n")}")
return e.rc, e.message + e.stdout + e.stderr
rescue OpenShift::Runtime::FrontendHttpServerException => e
report_exception e
OpenShift::Runtime::NodeLogger.logger.error("#{e.message}\n#{e.backtrace.join("\n")}")
return 129, e.message
rescue Exception => e
report_exception e
OpenShift::Runtime::NodeLogger.logger.error("#{e.message}\n#{e.backtrace.join("\n")}")
return 1, e.message
else
return 0, output
end
end
def with_frontend_from_args(args)
container_uuid = args['--with-container-uuid'].to_s if args['--with-container-uuid']
with_frontend_rescue_pattern do |o|
frontend = OpenShift::Runtime::FrontendHttpServer.new(OpenShift::Runtime::ApplicationContainer.from_uuid(container_uuid))
yield(frontend, o)
end
end
def with_frontend_returns_data(args)
with_frontend_from_args(args) do |f, o|
r = yield(f, o)
o << "CLIENT_RESULT: " + r.to_json + "\n"
end
end
#
# A frontend must be created before any other manipulations are
# performed on it.
#
def oo_frontend_create(args)
with_frontend_from_args(args) do |f, o|
f.create
end
end
def oo_frontend_destroy(args)
with_frontend_from_args(args) do |f, o|
f.destroy
end
end
def oo_frontend_update_name(args)
new_container_name = args['--with-new-container-name']
with_frontend_from_args(args) do |f, o|
f.update_name(new_container_name)
end
end
def oo_frontend_reconnect(args)
only_proxy_carts = args['--only-proxy-carts']
with_container_from_args(args) do |container|
final_carts = []
container.cartridge_model.each_cartridge do |cart|
if cart.web_proxy?
final_carts << cart
else
container.cartridge_model.connect_frontend(cart) unless only_proxy_carts
end
end
final_carts.each do |cart|
container.cartridge_model.connect_frontend(cart, true)
end
end
end
#
# The path-target-option is an array of the path, target and
# options. Multiple arrays may be specified and they are in
# the form of: [ path(String), target(String), options(Hash) ]
# ex: [ "", "127.0.250.1:8080", { "websocket" => 1 } ], ...
#
def oo_frontend_connect(args)
path_target_options = args['--with-path-target-options']
with_frontend_from_args(args) do |f, o|
f.connect(*path_target_options)
end
end
#
# The paths are an array of the paths to remove.
# ex: [ "", "/health", ... ]
def oo_frontend_disconnect(args)
paths = args['--with-paths']
with_frontend_from_args(args) do |f, o|
f.disconnect(*paths)
end
end
def oo_frontend_connections(args)
with_frontend_returns_data(args) do |f, o|
f.connections.to_json
end
end
def oo_frontend_idle(args)
with_frontend_from_args(args) do |f, o|
f.idle
end
end
def oo_frontend_unidle(args)
with_frontend_from_args(args) do |f, o|
f.unidle
end
end
def oo_frontend_check_idle(args)
with_frontend_returns_data(args) do |f, o|
f.idle?
end
end
def oo_frontend_sts(args)
max_age = args['--with-max-age']
with_frontend_from_args(args) do |f, o|
f.sts(max_age)
end
end
def oo_frontend_no_sts(args)
with_frontend_from_args(args) do |f, o|
f.no_sts
end
end
def oo_frontend_get_sts(args)
with_frontend_returns_data(args) do |f, o|
f.get_sts
end
end
def oo_add_alias(args)
alias_name = args['--with-alias-name']
with_frontend_from_args(args) do |f, o|
f.add_alias(alias_name)
end
end
def oo_remove_alias(args)
alias_name = args['--with-alias-name']
with_frontend_from_args(args) do |f, o|
f.remove_alias(alias_name)
end
end
def oo_add_aliases(args)
alias_names = args['--with-aliases']
with_frontend_from_args(args) do |f, o|
alias_names.each do |alias_name|
f.add_alias(alias_name)
end
end
end
def oo_remove_aliases(args)
alias_names = args['--with-aliases']
with_frontend_from_args(args) do |f, o|
alias_names.each do |alias_name|
f.remove_alias(alias_name)
end
end
end
def oo_aliases(args)
with_frontend_returns_data(args) do |f, o|
f.aliases(alias_name)
end
end
def oo_ssl_cert_add(args)
ssl_cert = args['--with-ssl-cert']
priv_key = args['--with-priv-key']
server_alias = args['--with-alias-name']
passphrase = args['--with-passphrase']
with_frontend_from_args(args) do |f, o|
f.add_ssl_cert(ssl_cert, priv_key, server_alias, passphrase)
end
end
def oo_ssl_cert_remove(args)
server_alias = args['--with-alias-name']
with_frontend_from_args(args) do |f, o|
f.remove_ssl_cert(server_alias)
end
end
def oo_ssl_certs(args)
with_frontend_returns_data(args) do |f, o|
f.ssl_certs
end
end
def oo_frontend_to_hash(args)
with_frontend_returns_data(args) do |f, o|
f.to_hash
end
end
# The backup is just a JSON encoded string
#
# TODO: Determine if its necessary to base64 encode
# the output to protect from interpretation.
#
def oo_frontend_backup(args)
oo_frontend_to_hash(args)
end
# Does an implicit instantiation of the FrontendHttpServer class.
def oo_frontend_restore(args)
backup = args['--with-backup']
with_frontend_rescue_pattern do |o|
OpenShift::Runtime::FrontendHttpServer.json_create({'data' => JSON.parse(backup)})
end
end
def oo_tidy(args)
with_container_from_args(args) do |container|
container.tidy
end
end
def oo_expose_port(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container, output|
output << container.create_public_endpoints(cart_name)
end
end
def oo_conceal_port(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container, output|
output << container.delete_public_endpoints(cart_name)
end
end
def oo_connector_execute(args)
cart_name = args['--cart-name']
pub_cart_name = args['--publishing-cart-name']
hook_name = args['--hook-name']
connection_type = args['--connection-type']
input_args = args['--input-args']
with_container_from_args(args) do |container, output|
output << container.connector_execute(cart_name, pub_cart_name, connection_type, hook_name, input_args)
end
end
def oo_configure(args)
cart_name = args['--cart-name']
template_git_url = args['--with-template-git-url']
manifest = args['--with-cartridge-manifest']
do_expose_ports = !!args['--with-expose-ports']
with_container_from_args(args) do |container, output|
output << container.configure(cart_name, template_git_url, manifest, do_expose_ports)
end
end
def oo_post_configure(args)
cart_name = args['--cart-name']
template_git_url = args['--with-template-git-url']
deployments = nil
rc, output = with_container_from_args(args) do |container, output|
output << container.post_configure(cart_name, template_git_url)
if container.cartridge_model.get_cartridge(cart_name).deployable?
::OpenShift::Runtime::Utils::Cgroups.new(container.uuid).boost do
deployments = {deployments: container.calculate_deployments}
end
end
end
return rc, output, deployments
end
def oo_deconfigure(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container, output|
output << container.deconfigure(cart_name)
end
end
def oo_unsubscribe(args)
cart_name = args['--cart-name']
pub_cart_name = args['--publishing-cart-name']
with_container_from_args(args) do |container, output|
output << container.unsubscribe(cart_name, pub_cart_name).to_s
end
end
def oo_deploy_httpd_proxy(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container|
container.deploy_httpd_proxy(cart_name)
end
end
def oo_remove_httpd_proxy(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container|
container.remove_httpd_proxy(cart_name)
end
end
def oo_restart_httpd_proxy(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container|
container.restart_httpd_proxy(cart_name)
end
end
def oo_start(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container, output|
output << container.start(cart_name)
end
end
def oo_stop(args)
cart_name = args['--cart-name']
with_container_from_args(args) do |container, output|
output << container.stop(cart_name)
end
end
def oo_restart(args)
cart_name = args['--cart-name']
options = {}
options[:all] = true if args['--all']
options[:parallel_concurrency_ratio] = args['--parallel_concurrency_ratio'].to_f if args['--parallel_concurrency_ratio']
with_container_from_args(args) do |container, output|
container.restart(cart_name, options)
end
end
def oo_reload(args)