@@ -869,7 +869,126 @@ def get_summary(target_region: nil, options: nil, global_region: "alicloud-v4")
869
869
end
870
870
print_grand_summary ( grand_summary )
871
871
end
872
+ end
873
+
874
+ class IBMCloudSummary < InstanceSummary
875
+ attr_accessor :ibm , :ibm_prices
876
+ def initialize ( svc_name : :ibmcloud , jenkins : nil )
877
+ @ibm = IBMCloud . new ( service_name : svc_name , region : 'us-east' )
878
+ @jenkins = jenkins
879
+ @table = Text ::Table . new
880
+ # hard-coded pricing lookup table name: => price/hr
881
+ @ibm_prices = {
882
+ "bx2-2x8" => 0.096 ,
883
+ "bx2d-2x8" => 0.104 ,
884
+ "bx2-4x16" => 0.192 ,
885
+ "bx2d-4x16" => 0.208 ,
886
+ "bx2-8x32" => 0.384 ,
887
+ "bx2d-8x32" => 0.417 ,
888
+ "bx2-16x64" => 0.768 ,
889
+ "bx2d-16x64" => 0.834 ,
890
+ "bx2-32x128" => 1.536 ,
891
+ "bx2d-32x128" => 1.668 ,
892
+ "bx2-48x192" => 2.305 ,
893
+ "bx2d-48x192" => 2.502 ,
894
+ "bx2-64x256" => 3.073 ,
895
+ "bx2d-64x256" => 3.336 ,
896
+ "bx2-96x384" => 4.609 ,
897
+ "bx2d-96x384" => 5.004 ,
898
+ "bx2-128x512" => 6.146 ,
899
+ "bx2d-128x512" => 6.672 ,
900
+ }
901
+ end
902
+
903
+ # @return <Hashed Array of Instances> with each hash key being keyed on the `owned` tag.
904
+ def regroup_instances ( instances )
905
+ cluster_map = { }
906
+ instances . each do |r |
907
+ begin
908
+ owned = r . dig ( "resource_group" , "name" )
909
+ rescue
910
+ # for bastion hosts, there doesn't seem to be a tag associated with
911
+ # them, so just set it as empty
912
+ owned = no_owner
913
+ end
914
+ if cluster_map [ owned ]
915
+ cluster_map [ owned ] << r
916
+ else
917
+ cluster_map [ owned ] = [ r ]
918
+ end
919
+ end
920
+ return cluster_map
921
+ end
922
+
923
+ # @instances <Array of unordered Instance obj>
924
+ def summarize_instances ( region , instances )
925
+ summary = [ ]
926
+ ibm = @ibm
927
+ jenkins = @jenkins
928
+ cm = regroup_instances ( instances )
929
+ cm . each do | owned , inst_list |
930
+ inst_list . each do | inst |
931
+ inst_summary = { }
932
+ # inst_summary[:inst_obj] = inst
933
+ inst_summary [ :region ] = region
934
+ inst_summary [ :name ] = inst [ "name" ]
935
+ inst_summary [ :type ] = inst [ 'profile' ] [ 'name' ]
936
+ inst_summary [ :uptime ] = ibm . instance_uptime inst
937
+ inst_hourly_price = @ibm_prices [ inst_summary [ :type ] ]
938
+ cost = 0.0
939
+ if inst_hourly_price . nil?
940
+ inst_hourly_price = 0.0
941
+ puts "##### WARNING, setting hourly price for '#{ inst_summary [ :type ] } ' to 0.0 because it's not known"
942
+ end
943
+
944
+ cost = inst_summary [ :uptime ] * inst_hourly_price
945
+ inst_summary [ :cost ] = cost . round ( 2 )
946
+ inst_summary [ :owned ] = owned
947
+ if inst_summary [ :owned ]
948
+ inst_summary [ :flexy_job_id ] , inst_summary [ :inst_prefix ] = jenkins . get_jenkins_flexy_job_id ( inst_summary [ :owned ] )
949
+ else
950
+ inst_summary [ :flexy_job_id ] , inst_summary [ :inst_prefix ] = nil , nil
951
+ end
952
+ summary << inst_summary
953
+ end
954
+ end
955
+ return summary
956
+ end
872
957
958
+ def get_summary ( target_region : nil , options : nil , global_region : "ibmcloud" )
959
+ regions = ibm . regions
960
+ ibm_instances = { }
961
+ threads = [ ]
962
+ # regions is an Array of ihash
963
+ # for example: {"name"=>"au-syd", "href"=>"https://us-south.iaas.cloud.ibm.com/v1/regions/au-syd", "endpoint"=>"https://au-syd.iaas.cloud.ibm.com", "status"=>"available"
964
+ regions . each do | region |
965
+ if target_region
966
+ # first check name is valid
967
+ raise "Unsupported region '#{ target_region } '" unless regions . map { |r | r [ 'name' ] } . include? target_region
968
+ region_name = target_region
969
+ else
970
+ region_name = region [ 'name' ]
971
+ end
972
+ end_point = region . dig ( 'endpoint' )
973
+ puts ( "Getting instances for region '#{ region_name } '...\n " )
974
+ ibm = IBMCloud . new ( region_id : region_name )
975
+ ibm . set_region ( region_name )
976
+ instances = ibm . get_instances_by_status ( status : 'running' ) #, region_name: region['name'], region_endpoint: end_point)
977
+ ibm_instances [ region_name ] = instances
978
+ break if target_region
979
+ end
980
+ grand_summary = [ ]
981
+ ibm_instances . each do |region , inst_list |
982
+ total_cost = 0.0
983
+ summary = summarize_instances ( region , inst_list )
984
+ print_summary ( summary ) if inst_list . count > 0
985
+ options . platform = "IBMCloud #{ region } "
986
+ print_longlived_clusters ( summary , options ) if inst_list . count > 0
987
+ summary . each { |s | total_cost += s [ :cost ] }
988
+ grand_summary << { platform : 'IBMCloud' , region : region , inst_count : inst_list . count , total_cost : total_cost }
989
+ end
990
+ print_grand_summary ( grand_summary )
991
+ end
873
992
end
874
993
end
875
994
0 commit comments