-
Notifications
You must be signed in to change notification settings - Fork 20
/
openstudio_meta
executable file
·1121 lines (952 loc) · 38.9 KB
/
openstudio_meta
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
#!/usr/bin/env ruby
# *******************************************************************************
# OpenStudio(R), Copyright (c) Alliance for Sustainable Energy, LLC.
# See also https://openstudio.net/license
# *******************************************************************************
Signal.trap('INT') {abort}
require 'logger'
$logger = ::Logger.new(STDOUT)
$logger.level = ::Logger::WARN
ENV['GEM_HOME'] = File.absolute_path(File.join(__FILE__, './../../gems/'))
ENV['GEM_PATH'] = File.absolute_path(File.join(__FILE__, './../../gems/')).to_s + File::PATH_SEPARATOR + File.absolute_path(File.join(__FILE__, './../../gems/bundler/gems')).to_s
# Set GEM_HOME and GEM_PATH vs using ENV for ruby 2.5.x
Gem.paths = {
'GEM_HOME' => File.absolute_path(File.join(__FILE__, './../../gems/')),
'GEM_PATH' => File.absolute_path(File.join(__FILE__, './../../gems/')).to_s + File::PATH_SEPARATOR + File.absolute_path(File.join(__FILE__, './../../gems/bundler/gems')).to_s
}
# Debugging: Print Ruby configuration values
puts "BLB RbConfig::CONFIG['prefix']: #{RbConfig::CONFIG['prefix']}"
puts "RbConfig::CONFIG['EXECUTABLE_EXTS']: #{RbConfig::CONFIG['EXECUTABLE_EXTS']}"
$ruby_path = "#{RbConfig::CONFIG['prefix']}/bin/ruby#{RbConfig::CONFIG['EXECUTABLE_EXTS'].split[0]}"
# Debugging: Print constructed Ruby path
puts "Constructed Ruby path: #{$ruby_path}"
# This is the save puts to use to catch EPIPE. Uses `puts` on the given IO object and safely ignores any Errno::EPIPE
#
# @param [String] message Message to output
# @param [Hash] opts Options hash
#
def safe_puts(message = nil, opts = nil)
message ||= ''
opts = {
io: $stdout,
printer: :puts
}.merge(opts || {})
begin
opts[:io].send(opts[:printer], message)
rescue Errno::EPIPE
# This is what makes this a `safe` puts
return
end
end
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end
# This is a convenience method that properly handles duping the originally argv array so that it is not destroyed. This
# method will also automatically detect "-h" and "--help" and print help. And if any invalid options are detected, the
# help will be printed, as well
#
# @param [Object, nil] opts An instance of OptionParse to parse against, defaults to a new OptionParse instance
# @param [Array, nil] argv The argv input to be parsed, defaults to $argv
# @return[Array, nil] If this method returns `nil`, then you should assume that help was printed and parsing failed
#
def parse_options(opts = nil, argv = nil)
# Creating a shallow copy of the arguments so the OptionParser
# doesn't destroy the originals.
argv ||= $argv.dup
# Default opts to a blank optionparser if none is given
opts ||= ::OptionParser.new
opts.on('--ruby-lib-path PATH', 'Additional Ruby path, to locate openstudio') do |path|
ENV['RUBYLIB'] = path
ENV['RUBY_DLL_PATH'] = path
end
require 'tmpdir'
require 'fileutils'
ENV['OS_SERVER_LOG_PATH'] = Dir.mktmpdir
opts.on('--server-log-path [PATH]', 'Path to server logs') do |path|
FileUtils.remove_entry ENV['OS_SERVER_LOG_PATH']
ENV['OS_SERVER_LOG_PATH'] = path
end
ENV['RAILS_ENV'] = 'local'
opts.on('--rails-env [ENV]', 'Rails Environment') do |env|
ENV['RAILS_ENV'] = env
end
ENV['OS_SERVER_HOST_URL'] = 'http://localhost:8080'
opts.on('--server-host-url [URL]', 'Rails host url') do |url|
ENV['OS_SERVER_HOST_URL'] = url
end
# Add the help option, which must be on every command.
opts.on_tail('-h', '--help', 'Print this help') do
safe_puts(opts.help)
exit(0)
end
opts.parse!(argv)
return argv
rescue ::OptionParser::InvalidOption, ::OptionParser::MissingArgument
raise "Error: Invalid CLI option, #{opts.help.chomp}"
end
# This method will split the argv given into three parts: the flags to this command, the subcommand, and the flags to
# the subcommand. For example:
# -v status -h -v
# The above would yield 3 parts:
# ["-v"]
# "status"
# ["-h", "-v"]
# These parts are useful because the first is a list of arguments given to the current command, the second is a
# subcommand, and the third are the commands given to the subcommand
#
# @param [Array] argv The input to be split
# @return [Array] The split command as [main arguments, sub command, sub command arguments]
#
def split_main_and_subcommand(argv)
# Initialize return variables
main_args = nil
sub_command = nil
sub_args = []
# We split the arguments into two: One set containing any flags before a word, and then the rest. The rest are what
# get actually sent on to the subcommand
argv.each_index do |i|
unless argv[i].start_with?('-')
# We found the beginning of the sub command. Split the
# args up.
main_args = argv[0, i]
sub_command = argv[i]
sub_args = argv[i + 1, argv.length - i + 1]
# Break so we don't find the next non flag and shift our main args
break
end
end
# Handle the case that argv was empty or didn't contain any subcommand
main_args = argv.dup if main_args.nil?
[main_args, sub_command, sub_args]
end
# This CLI class processes the input args and invokes the proper command class
class CLI
# This constant maps subcommands to classes in this CLI and stores meta-data on them
COMMAND_LIST = {
install_gems: [proc {::InstallGems}, {primary: true, working: true}],
start_local: [proc {::StartLocal}, {primary: true, working: true}],
start_remote: [proc {::StartRemote}, {primary: true, working: true}],
stop_local: [proc {::StopLocal}, {primary: true, working: true}],
stop_remote: [proc {::StopRemote}, {primary: true, working: true}],
run_analysis: [proc {::RunAnalysis}, {primary: true, working: true}],
run_rspec: [proc {::RunRspec}, {primary: true, working: true}],
run_codecov: [proc {::RunCoverage}, {primary: false, working: false}]
}.freeze
# This method instantiates the global variables $main_args, $sub_command, and $sub_args
#
# @param [Array] argv The arguments passed through the CLI
# @return [Object] An instance of the CLI class with initialized globals
#
def initialize(argv)
$main_args, $sub_command, $sub_args = split_main_and_subcommand(argv)
$logger.info("CLI Parsed Inputs: #{$main_args.inspect} #{$sub_command.inspect} #{$sub_args.inspect}")
end
# Checks to see if it should print the main help, and if not parses the subcommand into a class and executes it
def execute
$logger.debug "Main arguments are #{$main_args}"
$logger.debug "Sub-command is #{$sub_command}"
$logger.debug "Sub-arguments are #{$sub_args}"
if $main_args.include?('-h') || $main_args.include?('--help')
# Help is next in short-circuiting everything. Print
# the help and exit.
help
exit 0
end
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_plugin = nil
if $sub_command
command_plugin = COMMAND_LIST[$sub_command.to_sym]
end
if !command_plugin || !$sub_command
help
exit 1
end
command_class = command_plugin[0].call
$logger.debug("Invoking command class: #{command_class} #{$sub_args.inspect}")
# Initialize and execute the command class, returning the exit status.
result = 0
begin
result = command_class.new.execute($sub_args)
rescue Interrupt
$logger.error '?'
result = 1
end
result = 0 unless result.is_a?(Integer) # use to be Fixnum which is going to removed in ruby eventually
result
end
# Prints out the help text for the CLI
#
# @param [Boolean] list_all If set to true, the help prints all commands, however it otherwise only prints those
# marked as primary in #COMMAND_LIST
# @return [void]
# @see #COMMAND_LIST #::ListCommands
#
def help(list_all = false)
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta [options] <command> [<args>]'
o.separator ''
o.on('-h', '--help', 'Print this help.')
o.on('--verbose', 'Print the full log to STDOUT')
o.separator ''
o.separator 'Common commands:'
# Add the available subcommands as separators in order to print them
# out as well.
commands = {}
longest = 0
COMMAND_LIST.each do |key, data|
# Skip non-primary commands. These only show up in extended
# help output.
next unless list_all || data[1][:primary]
key = key.to_s
klass = data[0].call
commands[key] = klass.synopsis
longest = key.length if key.length > longest
end
commands.keys.sort.each do |key|
o.separator " #{key.ljust(longest + 2)} #{commands[key]}"
end
o.separator ''
o.separator 'For help on any individual command run `openstudio_meta COMMAND -h`'
end
safe_puts opts.help
end
end
# Class to initialize packaged gems in ./../server/vendor/cache
class InstallGems
require 'fileutils'
# Provides text for the main help functionality
def self.synopsis
'Installs the required packaged Gems'
end
# Executes the required gem install command from the correct folder
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
options = {debug: false, export_dir: nil, test_dev_build: false, use_cached_gems: false}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta install_gems [options]'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('--debug', 'Whatever makes debugging easy') do |f|
options[:debug] = f
end
o.on('-e', '--export DIRECTORY', 'DIRECTORY to export server build') do |e|
options[:export_dir] = e
end
o.on('--with_test_develop', 'Include test and develop gems in the installation package') do |_w|
options[:test_dev_build] = true
end
o.on('--use_cached_gems', 'Use the cached gems') do |_w|
options[:use_cached_gems] = true
end
end
# Parse the options
argv = parse_options(opts, sub_argv)
unless argv == []
$logger.error 'Extra arguments passed to the install_gems command. Please refer to the help documentation.'
exit 1
end
$logger.debug("InstallGems command: #{argv.inspect} #{options.inspect}")
# test that we have git in the path
unless system('git --version')
$logger.error 'Git is not in your path.'
exit 1
end
# Delete existing gem dir
# GEM_HOME is set to location in this directory at top of script
if File.exist?(ENV['GEM_HOME']) && !options[:use_cached_gems]
FileUtils.rm_rf(ENV['GEM_HOME'])
end
FileUtils.mkdir_p(ENV['GEM_HOME'])
# Set a phony os_server_project_path and os_server_rails_temp_dir because rails needs it.
::ENV['OS_SERVER_PROJECT_PATH'] = Dir.home
::ENV['OS_SERVER_RAILS_TMP_PATH'] = Dir.mktmpdir
# Execute the required bundler command
::Dir.chdir File.absolute_path(File.join(__FILE__, './../../server/'))
bundler_bin = File.absolute_path('./../gems/bin/bundle')
rake_bin = File.absolute_path('./../gems/bin/rake/')
sys_cmds = []
sys_cmds << "#{$ruby_path} #{RbConfig::CONFIG['prefix']}/bin/gem install --no-env-shebang bundler -v 2.4.10"
if /darwin/.match(RUBY_PLATFORM)
sys_cmds << "#{$ruby_path} #{bundler_bin} config build.nokogiri --use-system-libraries --with-xml2-lib=/usr/lib --with-xml2-config=/usr/bin/xml2-config"
sys_cmds << "#{$ruby_path} #{bundler_bin} config build.libxml-ruby --use-system-libraries --with-xml2-config=/usr/bin/xml2-config"
end
if options[:test_dev_build]
sys_cmds << "#{$ruby_path} #{bundler_bin} install --with default development test"
else
sys_cmds << "#{$ruby_path} #{bundler_bin} install --without development test"
end
sys_cmds << "#{$ruby_path} #{bundler_bin} update"
sys_cmds << 'echo "Calling assets:precompile"'
sys_cmds << "#{$ruby_path} #{bundler_bin} exec #{rake_bin} assets:precompile" # DLM: consider deleting /server/public/assets each time
sys_call = sys_cmds.join(' && ')
puts "Environment variables: #{::ENV.inspect}" if options[:debug]
puts "System call will be: '#{sys_call}' in directory '#{Dir.pwd}'" if options[:debug]
unless system(sys_call)
$logger.error "System call '#{sys_call}' failed"
exit 1
end
# copy all files to staging location
if options[:export_dir]
puts "Exporting OpenStudio-server to #{options[:export_dir]}" if options[:debug]
if File.exist?(options[:export_dir])
puts "Removing '#{options[:export_dir]}'" if options[:debug]
FileUtils.rm_rf(options[:export_dir])
end
puts "Creating '#{options[:export_dir]}'" if options[:debug]
FileUtils.mkdir_p(options[:export_dir])
# we require root dir named OpenStudio-server
dest = File.join(options[:export_dir], 'OpenStudio-server')
FileUtils.mkdir_p(dest)
# have to remove these .git files before copying, they were causing failures on Windows
src = File.absolute_path('./../')
puts "Removing .git folders from gems in '#{src}'" if options[:debug]
Dir.glob(src + '/gems/**/.git').each do |f|
puts "Removing '#{f}'" if options[:debug]
FileUtils.rm_rf(f)
end
puts "Copying files from '#{src}' to '#{dest}'" if options[:debug]
Dir.glob(src + '/*').each do |f|
next if /\.git/ =~ f
puts "Copying files from '#{f}' to '#{dest}/.'" if options[:debug]
FileUtils.cp_r(f, dest + '/.')
end
FileUtils.rm_rf(File.join(dest, 'gems/cache'))
FileUtils.rm_rf(File.join(dest, 'server/coverage'))
FileUtils.rm_rf(File.join(dest, 'server/log'))
FileUtils.rm_rf(File.join(dest, 'server/spec'))
FileUtils.rm_rf(File.join(dest, 'server/tmp'))
FileUtils.rm_rf(File.join(dest, 'spec'))
FileUtils.rm_rf(File.join(dest, 'reports'))
FileUtils.rm_rf(File.join(dest, 'worker-nodes'))
Dir.glob(dest + '/**/OpenStudio-workflow*/spec/').each {|p| FileUtils.rm_rf(p)}
Dir.glob(dest + '/**/OpenStudio-workflow*/test/').each {|p| FileUtils.rm_rf(p)}
platform = 'win32'
if /darwin/.match(RUBY_PLATFORM)
platform = 'darwin'
elsif /linux/.match(RUBY_PLATFORM)
platform = 'linux'
end
::Dir.chdir(options[:export_dir])
puts "Dir.pwd = #{Dir.pwd}" if options[:debug]
server_sha = `git -C \"#{src}\" rev-parse --short=10 HEAD`.strip
puts "Server SHA = #{server_sha}" if options[:debug]
sys_call = "tar -czf OpenStudio-server-#{server_sha}-#{platform}.tar.gz OpenStudio-server"
puts "Creating tar.gz in #{options[:export_dir]}"
puts sys_call if options[:debug]
unless system(sys_call)
$logger.error "System call '#{sys_call}' failed"
exit 1
end
end
end
end
# Class to initialize a local server
class StartLocal
require_relative 'resources/local.rb'
# Provides text for the main help functionality
def self.synopsis
'Starts local processes for the OS Server'
end
# Executes the command line calls through threads, and sends their pids to file for shutdown
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
mongod = which 'mongod'
mongodir = ''
if mongod
mongodir = File.dirname mongod
end
openstudio = which 'openstudio'
options = {debug: false, force_directories: false, workers: 1, ruby_path: $ruby_path, mongo_dir: mongodir, oscli: openstudio}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta start_local [options] PROJECT_DIR'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('--debug', 'Whatever makes debugging easy') do |f|
options[:debug] = f
end
o.on('--force-directories', 'Force creation of directories') do |_|
options[:force_directories] = true
end
o.on('--worker-number COUNT', 'Number of workers to use') do |w|
options[:workers] = w.to_i
end
o.on('--ruby-path PATH', 'Location of ruby executable for workers. Defaults to the calling instance of ruby') do |path|
options[:ruby_path] = path
end
o.on('--mongo-dir PATH', 'Location of mongo installation prefix. Defaults to the location of any mongod in the current system path.') do |path|
options[:mongo_dir] = path
end
o.on('--energyplus-exe-path PATH', 'Location of the EnergyPlus executable') do |path|
options[:energyplus_exe_path] = path
end
o.on('--openstudio-exe-path PATH', 'Location of the OpenStudio executable') do |path|
options[:openstudio_exe_path] = path
end
end
# Parse the options
argv = parse_options(opts, sub_argv)
project_dir = argv.shift
unless argv == []
$logger.error 'Extra arguments passed to the start_local command. Please refer to the help documentation.'
exit 1
end
$logger.debug("StartLocal command: #{argv.inspect} #{options.inspect}")
# Check the project directory exists
unless ::File.exist? project_dir
if options[:force_directories]
FileUtils.mkdir_p project_dir
else
$logger.error "Unable to find directory '#{project_dir}'"
exit 1
end
end
# Check if a local_configuration.json exists
if ::File.exist? ::File.join(project_dir, 'local_configuration.json')
$logger.error "A `local_configuration.json` file already exists at `#{project_dir}`"
exit 1
end
# Also force the creation of the log dir in the project_dir
if options[:force_directories]
FileUtils.mkdir_p "#{project_dir}/logs" unless Dir.exist? "#{project_dir}/logs"
end
if options[:energyplus_exe_path] && ::File.exist?(options[:energyplus_exe_path])
::ENV['ENERGYPLUS_EXE_PATH'] = options[:energyplus_exe_path]
end
if options[:openstudio_exe_path] && ::File.exist?(options[:openstudio_exe_path])
::ENV['OPENSTUDIO_EXE_PATH'] = options[:openstudio_exe_path]
elsif options[:oscli]
if ::File.exist?(options[:oscli])
::ENV['OPENSTUDIO_EXE_PATH'] = options[:oscli]
end
end
# Set the server project path and temp dir
::ENV['OS_SERVER_PROJECT_PATH'] = File.join(project_dir, 'temp_data')
::ENV['OS_SERVER_RAILS_TMP_PATH'] = Dir.mktmpdir
# Assemble the commands and spin off the threads
start_local_server project_dir, options[:mongo_dir], options[:ruby_path], options[:workers], options[:debug]
0
end
end
# Class to stop the local server
class StopLocal
require_relative 'resources/local.rb'
# Provides text for the main help functionality
def self.synopsis
'Stops local processes for the OS Server'
end
# Stops the local server associated with a PAT 2.0 project directory
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
options = {debug: false}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta stop_local [options] PROJECT_DIR'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('--debug', 'Whatever makes debugging easy') do |f|
options[:debug] = f
end
end
# Parse the options
argv = parse_options(opts, sub_argv)
project_dir = argv.shift
unless argv == []
$logger.error 'Extra arguments passed to the stop_local command. Please refer to the help documentation.'
exit 1
end
$logger.debug("StopLocal command: #{argv.inspect} #{options.inspect}")
# Retrieve the pids
local_state_file = ::File.absolute_path(File.join(project_dir, 'local_configuration.json'))
unless ::File.exist? local_state_file
$logger.error "Unable to find the local configuration json at #{local_state_file}"
exit 1
end
find_windows_pids(local_state_file) if Gem.win_platform?
local_server_config = ::JSON.parse(File.read(local_state_file), symbolize_names: true)
rails_pid = local_server_config[:rails_pid]
dj_pids = local_server_config[:dj_pids]
mongod_pid = local_server_config[:mongod_pid]
child_pids = local_server_config[:child_pids] ? local_server_config[:child_pids] : []
# Stop the pids, with lots of error catches
successful = stop_local_server rails_pid, dj_pids, mongod_pid, child_pids
# Remove files, if PID kills were successful
if successful
::File.delete(local_state_file) if ::File.exist? local_state_file
::File.delete(local_state_file.gsub('.json', '.receipt')) if ::File.exist? local_state_file.gsub('.json', '.receipt')
server_pid_file = ::File.join(::File.dirname(local_state_file), 'server.pid')
::File.delete(server_pid_file) if ::File.exist? server_pid_file
exit 0
else
exit 1
end
end
end
# Class to boot an OS Server AMI instance on AWS or return the URL of a known remote server, i.e. nrel24a
class StartRemote
require_relative 'resources/remote.rb'
# Provides text for the main help functionality
def self.synopsis
'Starts a remote OS Server'
end
# Boots an OS Server on AWS and returns it's URL, or returns the URL of a known server
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
$logger.debug 'Requiring gems for StartRemote'
$logger.debug 'Requiring os-aws gem'
require 'openstudio-aws'
$logger.debug 'Requiring os-analysis gem'
require 'openstudio-analysis'
$logger.debug 'Successfully required all gems for StartRemote'
options = {
debug: false,
aws_yml: '',
server_options_json: '',
project_dir: ''
}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta start_remote [options] target'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('--debug', 'Whatever makes debugging easy') do |f|
options[:debug] = f
end
o.on('-s', '--server_config FILE', 'JSON FILE containing the AWS server instance configuration') do |s|
options[:server_options_json] = s
end
o.on('-p', '--project DIRECTORY', 'DIRECTORY containing the project to save the AWS connection to') do |p|
options[:project_dir] = p
end
end
# Parse the options
argv = parse_options(opts, sub_argv)
exit 1 unless argv
$logger.debug("StartRemote command: #{argv.inspect} #{options.inspect}")
target = argv.shift.to_s.downcase
unless argv == []
$logger.error 'Extra arguments passed to the start_remote command. Please refer to the help documentation.'
exit 1
end
# If target is AWS, ensure that needed directories and files exist and set the ENV for AWS
if target == 'aws'
unless ::ENV['AWS_ACCESS_KEY'] && ::ENV['AWS_SECRET_KEY'] && ::ENV['AWS_DEFAULT_REGION']
$logger.error 'Unable to find required AWS_ACCESS_KEY, AWS_SECRET_KEY, and AWS_DEFAULT_REGION in the env vars.'
exit 1
end
unless ::File.exist? options[:server_options_json]
$logger.error "Unable to find server configuration file #{options[:server_options_json]}"
exit 1
end
unless ::File.exist? options[:project_dir]
$logger.error "Unable to find project directory #{options[:project_dir]}"
exit 1
end
end
# Get OpenStudioServerApi object and ensure the instance is running
server_options = target == 'aws' ? ::JSON.parse(File.read(options[:server_options_json]), symbolize_names: true) : {}
server_dns = find_or_create_target(target, server_options, options[:project_dir])
if server_dns == 1
$logger.error 'Encountered error in find_or_create_target; exiting with exit code 1'
exit 1
end
$logger.info "Server DNS: #{server_dns}"
server_dns
end
end
# Class to stop a remote AWS instance
class StopRemote
require_relative 'resources/remote.rb'
# Provides text for the main help functionality
def self.synopsis
'Stops a remote OS Server on AWS'
end
# Terminates an AWS instance
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
$logger.debug 'Requiring gems for StopRemote'
$logger.debug 'Requiring os-aws gem'
require 'openstudio-aws'
$logger.debug 'Successfully required all gems for StopRemote'
options = {
debug: false
}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta stop_remote [options] aws_conn_json'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('--debug', 'Whatever makes debugging easy') do |f|
options[:debug] = f
end
end
# Parse the options
argv = parse_options(opts, sub_argv)
raise 1 unless argv
$logger.debug("StopRemote command: #{argv.inspect} #{options.inspect}")
aws_conn_json = argv.shift.to_s
unless ::File.exist? aws_conn_json
$logger.error "Unable to find AWS connection file at #{aws_conn_json}"
raise 1
end
unless ::ENV['AWS_ACCESS_KEY'] && ::ENV['AWS_SECRET_KEY'] && ::ENV['AWS_DEFAULT_REGION']
$logger.error 'Unable to find required AWS_ACCESS_KEY, AWS_SECRET_KEY, and AWS_DEFAULT_REGION in the env vars.'
raise 1
end
unless argv == []
$logger.error 'Extra arguments passed to the stop_remote command. Please refer to the help documentation.'
raise 1
end
# Set the env variables for AWS, load the instance controller, and terminate the instances.
cluster_folder = File.dirname aws_conn_json
aws_init_options = {credentials: {access_key_id: ::ENV['AWS_ACCESS_KEY'], secret_access_key: ::ENV['AWS_SECRET_KEY'],
region: ::ENV['AWS_DEFAULT_REGION']},
save_directory: cluster_folder}
aws = OpenStudio::Aws::Aws.new(aws_init_options)
aws.load_instance_info_from_file(aws_conn_json)
aws.terminate
0
end
end
# Class to submit an analysis to a OS Server instance, defined by URL
class RunAnalysis
require_relative 'resources/remote.rb'
BATCH_RUN_METHODS = %w(lhs preflight single_run repeat_run doe diag baseline_perturbation batch_datapoints).freeze
puts "BLB RunAnalysis Environment is: #{::ENV.inspect}"
# Provides text for the main help functionality
def self.synopsis
'Runs an analysis on an OS Server instance'
end
# Submits an analysis to a URL
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
$logger.debug "sub_argv = #{sub_argv} in RunAnalysis execute"
$logger.debug "RunAnalysis Environment is: #{::ENV.inspect}"
# Print environment variables for debugging
puts "BLB Environment variables during run_analysis:"
puts "PATH: #{ENV['PATH']}"
puts "GEM_HOME: #{ENV['GEM_HOME']}"
puts "GEM_PATH: #{ENV['GEM_PATH']}"
puts "RUBYLIB: #{ENV['RUBYLIB']}"
puts "OPENSTUDIO_TEST_EXE: #{ENV['OPENSTUDIO_TEST_EXE']}"
# Method to print paths and contents of the bundle script
def print_paths_and_bundle_script
bundle_path = `which bundle`.strip
bundler_path = `which bundler`.strip
ruby_path = `which ruby`.strip
puts "BUNDLE_PATH: #{bundle_path}"
puts "BUNDLER_PATH: #{bundler_path}"
puts "RUBY_PATH: #{ruby_path}"
if File.exist?(bundle_path)
puts "Content of the bundle script:"
puts "------------------------------------"
File.open(bundle_path, "r") do |file|
file.each_line { |line| puts line }
end
puts "------------------------------------"
else
puts "Bundle script not found at #{bundle_path}"
end
end
# Call the method to print the paths and bundle script content
print_paths_and_bundle_script
$logger.debug 'Requiring gems for RunAnalysis'
$logger.debug 'Requiring zip gem'
require 'zip'
$logger.debug 'Requiring os-analysis gem'
require 'openstudio-analysis'
$logger.debug 'Requiring os-aws gem'
require 'openstudio-aws'
$logger.debug 'Successfully required all gems for RunAnalysis'
options = {
debug: false,
dencity: false,
zip_file: nil,
analysis_type: 'batch_datapoints'
}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta run_analysis [options] project server_dns'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('-a', '--analysis TYPE', 'Analysis type to run') {|a| options[:analysis_type] = a}
o.on('-z', '--zip NAME', 'relative path/name of project zip file from .json file') {|z| options[:zip_file] = z}
o.on('-d', '--push_to_dencity', 'Push the analysis to DEnCity') {|_| options[:dencity] = true}
o.on('--debug', 'Whatever makes debugging easy') {|_| options[:debug] = true}
end
# Parse the options
argv = parse_options(opts, sub_argv)
exit 1 unless argv
$logger.debug("RunAnalysis command: #{argv.inspect} #{options.inspect}")
project_path = argv.shift.to_s
server_dns = argv.shift.to_s
unless argv == []
$logger.error 'Extra arguments passed to the run_analysis command. Please refer to the help documentation.'
exit 1
end
::ENV['OS_SERVER_HOSTNAME'] = server_dns
# Convert Windows style path separators
project_path = project_path.tr('\\', '/')
# Create temporary folder for server inputs
temp_dir = ::File.join(File.dirname(project_path), '.temp')
::Dir.mkdir temp_dir unless ::File.exist?(temp_dir)
temp_filepath = temp_dir + '/analysis'
# Process project file and retrieve cluster options
if ::File.extname(project_path).casecmp('.xlsx').zero?
analysis_type = process_excel_project(project_path, temp_filepath)
elsif ::File.extname(project_path).casecmp('.csv').zero?
analysis_type = process_csv_project(project_path, temp_filepath)
elsif ::File.extname(project_path).casecmp('.json').zero?
temp_filepath = File.dirname(project_path) + '/' + File.basename(project_path).gsub('.json', '')
analysis_type = options[:analysis_type]
if !options[:zip_file].nil?
temp_zipfilepath = File.expand_path(File.join(File.dirname(project_path), options[:zip_file])).gsub('.zip', '')
$logger.debug("temp_zipfilepath: #{temp_zipfilepath}")
$logger.debug("project_path: #{project_path}")
$logger.debug("options[:zip_file]: #{options[:zip_file]}")
if !::File.exist?(temp_zipfilepath + '.zip')
$logger.error "Zip file #{temp_zipfilepath}.zip does not exist"
exit 1
end
end
else
$logger.error "Did not recognize project file extension #{::File.extname(project_path)}"
exit 1
end
# Get OpenStudioServerApi object and ensure the instance is running
server_api = OpenStudio::Analysis::ServerApi.new(hostname: server_dns)
unless server_api.machine_status
$logger.error "ERROR: Server at #{server_api.hostname} is not responding"
exit 1
end
# Submit the job
formulation_file = temp_filepath + '.json'
if !options[:zip_file].nil?
analysis_zip_file = temp_zipfilepath + '.zip'
else
analysis_zip_file = temp_filepath + '.zip'
end
batch_run_method = 'batch_run'
run_options = {
push_to_dencity: options[:dencity],
batch_run_method: batch_run_method
}
server_api.run(formulation_file, analysis_zip_file, analysis_type, run_options)
end
end
# Class to initialize a local server
class RunRspec
require_relative 'resources/local.rb'
# Provides text for the main help functionality
def self.synopsis
'Executes a suite of local tests for the OS Server'
end
# Executes the command line calls through threads, and sends their pids to file for shutdown
#
# @param [Array] sub_argv Options passed to the run subcommand from the user input
# @return [Fixnum] Return status
#
def execute(sub_argv)
mongod = which 'mongod'
mongodir = ''
if mongod
mongodir = File.dirname mongod
end
openstudio = which 'openstudio'
options = {debug: false, force_directories: true, ruby_path: $ruby_path, mongo_dir: mongodir, oscli: openstudio}
opts = ::OptionParser.new do |o|
o.banner = 'Usage: openstudio_meta run_rspec [options] TEST_DIR'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('--debug', 'Whatever makes debugging easy') do |f|
options[:debug] = f
end
o.on('--no-force-directories', 'Do not force creation of directories') do |_|
options[:force_directories] = false
end
o.on('--ruby-path PATH', 'Location of ruby executable for workers. Defaults to the calling instance of ruby') do |path|
options[:ruby_path] = path
end
o.on('--mongo-dir PATH', 'Location of mongo installation prefix. Defaults to the location of any mongod in the current system path.') do |path|
options[:mongo_dir] = path
end
o.on('--energyplus-exe-path PATH', 'Location of the EnergyPlus executable') do |path|
options[:energyplus_exe_path] = path
end
o.on('--openstudio-exe-path PATH', 'Location of the OpenStudio executable') do |path|
options[:openstudio_exe_path] = path
end
end
# Parse the options
argv = parse_options(opts, sub_argv)
test_dir = argv.shift
unless argv == []
$logger.error 'Extra arguments passed to the start_local command. Please refer to the help documentation.'
exit 1
end
$logger.debug("RunRSpec command: #{argv.inspect} #{options.inspect}")
# Check the project directory exists
unless ::File.exist? test_dir
FileUtils.mkdir_p test_dir if options[:force_directories]
end
# Check if a local_configuration.json exists
if ::File.exist? ::File.join(test_dir, 'local_test_configuration.json')
$logger.error "A `local_test_configuration.json` file already exists at `#{test_dir}`"
exit 1
end
# Set the E+ path
if options[:energyplus_exe_path] && ::File.exist?(options[:energyplus_exe_path])
::ENV['ENERGYPLUS_EXE_PATH'] = options[:energyplus_exe_path]
end
# Set the OSCLI path
if options[:openstudio_exe_path] && ::File.exist?(options[:openstudio_exe_path])
::ENV['OPENSTUDIO_EXE_PATH'] = options[:openstudio_exe_path]
elsif options[:oscli]
if ::File.exist?(options[:oscli])
::ENV['OPENSTUDIO_EXE_PATH'] = options[:oscli]
end
end
# Set the server project path and temp dir
::ENV['OS_SERVER_PROJECT_PATH'] = test_dir
::ENV['OS_SERVER_RAILS_TMP_PATH'] = Dir.mktmpdir
# Create the required project files
if options[:force_directories]
FileUtils.mkdir_p File.join(test_dir, 'logs')
FileUtils.mkdir_p File.join(test_dir, 'data', 'db')
end
# Assemble the commands and spin off the threads
run_rspec test_dir, options[:mongo_dir], options[:ruby_path], options[:debug]
0
end
end
# Class to initialize a local server
class RunCoverage
require_relative 'resources/local.rb'