Class: AwsUtils::Ec2ListMachines
- Inherits:
-
Object
- Object
- AwsUtils::Ec2ListMachines
- Defined in:
- lib/awsutils/ec2listmachines.rb
Instance Method Summary collapse
- #connect ⇒ Object
- #get_flavor_color(flavor) ⇒ Object
- #get_longest_server_name(servers_sorted) ⇒ Object
- #get_servers(opts) ⇒ Object
- #get_state_color(state) ⇒ Object
-
#initialize ⇒ Ec2ListMachines
constructor
A new instance of Ec2ListMachines.
- #list_instances(opts) ⇒ Object
- #parse_opts ⇒ Object
- #print_counts(servers_sorted, opts_csv) ⇒ Object
- #print_headers(csv_opt, servers_sorted, longest_server_name, show_dates) ⇒ Object
- #print_machine_list(servers_sorted, nocolor_opt, csv_opt, longest_server_name, show_dates) ⇒ Object
- #sort_servers(servers_a, sort_col) ⇒ Object
Constructor Details
#initialize ⇒ Ec2ListMachines
Returns a new instance of Ec2ListMachines.
400 401 402 403 404 |
# File 'lib/awsutils/ec2listmachines.rb', line 400 def initialize list_instances(parse_opts) rescue Interrupt puts 'Interrupted by user (SIGINT, Ctrl+C, etc.)' end |
Instance Method Details
#connect ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/awsutils/ec2listmachines.rb', line 24 def connect @connect = Fog::Compute.new(provider: 'AWS') if $DEBUG puts 'Inspect connection result:' puts connect.inspect end @connect end |
#get_flavor_color(flavor) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/awsutils/ec2listmachines.rb', line 197 def get_flavor_color(flavor) case flavor when 't1.micro' '1;33' when 'm1.large' '0;33' when 'm1.xlarge' '0;34' when 'm2.2xlarge' '0;35' when 'm2.4xlarge' '0;31' when 'm2.xlarge' '0;36' else '0' end end |
#get_longest_server_name(servers_sorted) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/awsutils/ec2listmachines.rb', line 128 def get_longest_server_name(servers_sorted) server_names = [] servers_sorted.each do |server| server_names << server[:name] end server_names.longest.length + 1 end |
#get_servers(opts) ⇒ Object
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 |
# File 'lib/awsutils/ec2listmachines.rb', line 34 def get_servers(opts) static_ips = [] connect.addresses.all.each do |eip| static_ips << eip.public_ip end group_map = {} connect.security_groups.map { |g| group_map[g.name] = g.group_id } if opts[:search] servers = connect.servers.select do |s| s.['Name'] =~ /.*#{opts[:search]}.*/ end else servers = connect.servers end servers_a = [] servers.each do |s| if (opts[:state].nil? || (s.state == opts[:state])) && ( (opts[:terminated] == true) || (s.state != 'terminated') || (opts[:state] == 'terminated') ) && (opts[:flavor].nil? || (s.flavor_id == opts[:flavor])) && (opts[:role].nil? || (s.['Role'] == opts[:role])) && (opts[:zone].nil? || (s.availability_zone.to_s == opts[:zone])) if s.public_ip_address.nil? pub_ip = '' else pub_ip = s.public_ip_address static_ip = static_ips.include?(pub_ip) ? '(S)' : '(D)' end role = s.['Role'] ? '-' : s.['Role'] if s.groups.first =~ /-/ group_to_insert = "#{group_map[s.groups.first]}/#{s.groups.first}" else group_to_insert = s.groups.first end created = s.['created_on'] ||= s.created_at servers_a << { name: s.['Name'].to_s, date: created.to_s, role: role.to_s, az: s.availability_zone.to_s, id: s.id, group: group_to_insert, pub_ip: pub_ip, static_ip: static_ip, flavor: s.flavor_id, state: s.state } end end servers_a end |
#get_state_color(state) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/awsutils/ec2listmachines.rb', line 216 def get_state_color(state) case state when 'running' '1;32' when 'stopped' '1;31' when 'starting' '5;32' when 'stopping' '5;31' else '0' end end |
#list_instances(opts) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/awsutils/ec2listmachines.rb', line 366 def list_instances(opts) servers = get_servers(opts) servers_sorted = sort_servers(servers, opts[:sort]) longest_server_name = get_longest_server_name(servers_sorted) print_headers(opts[:csv], servers_sorted, longest_server_name, opts[:dates]) print_machine_list( servers_sorted, opts[:nocolor], opts[:csv], longest_server_name, opts[:dates] ) print_counts(servers_sorted, opts[:csv]) end |
#parse_opts ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/awsutils/ec2listmachines.rb', line 382 def parse_opts opts = Trollop. do opt :sort, 'Sort order', short: 's', type: String opt :state, 'State', short: 'S', type: String opt :flavor, 'Flavor', short: 'F', type: String opt :role, 'Role', short: 'r', type: String opt :zone, 'Availability Zone', short: 'Z', type: String opt :csv, 'Output in CSV Format', short: 'C', default: false opt :dates, 'Show creation timestamp', short: 'd', default: false opt :terminated, 'Show terminated instances', short: 't', default: false opt :nocolor, 'No color', short: 'c' end opts[:search] = ARGV[0] unless ARGV[0].empty? opts end |
#print_counts(servers_sorted, opts_csv) ⇒ Object
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 |
# File 'lib/awsutils/ec2listmachines.rb', line 333 def print_counts(servers_sorted, opts_csv) flavor_data = {} connect.flavors.each do |f| flavor_data[f.id] = { 'cores' => f.cores, 'disk' => f.disk, 'ram' => f.ram } end total_cores = 0 total_disk = 0.0 total_ram = 0.0 servers_sorted.each do |s| total_cores += flavor_data[s[:flavor]]['cores'] total_disk += flavor_data[s[:flavor]]['disk'].to_f total_ram += flavor_data[s[:flavor]]['ram'].to_f end if opts_csv puts "total_instances,#{servers_sorted.count}" puts "total_cores,#{total_cores}" puts "total_disk,#{total_disk}" puts "total_ram,#{total_ram}" else puts "\nTotals" puts "\tInstances: #{servers_sorted.count}" puts "\tCores: #{total_cores}" printf("\tInstance storage: %.2f TB\n", total_disk / 1024) printf("\tRAM: %.2f GB\n", total_ram / 1024) end end |
#print_headers(csv_opt, servers_sorted, longest_server_name, show_dates) ⇒ Object
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 |
# File 'lib/awsutils/ec2listmachines.rb', line 137 def print_headers(csv_opt, servers_sorted, longest_server_name, show_dates) if csv_opt == true if show_dates printf( "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", 'Name', 'Created', 'Role', 'AZ', 'ID', 'Sec Group', 'Public IP,(S/D)', 'Flavor', 'State' ) else printf( "%s,%s,%s,%s,%s,%s,%s,%s\n", 'Name', 'Role', 'AZ', 'ID', 'Sec Group', 'Public IP,(S/D)', 'Flavor', 'State' ) end else if show_dates printf( "\033[0;4m%-#{longest_server_name}s %-30s %-16s %-10s %-10s " \ "%-24s %-19s %-10s %-7s\033[0m\n", 'Name', 'Created', 'Role', 'AZ', 'ID', 'Sec Group', 'Public IP (S/D)', 'Flavor', 'State' ) else printf( "\033[0;4m%-#{longest_server_name}s %-16s %-10s %-10s %-24s " \ "%-19s %-10s %-7s\033[0m\n", 'Name', 'Role', 'AZ', 'ID', 'Sec Group', 'Public IP (S/D)', 'Flavor', 'State' ) end end end |
#print_machine_list(servers_sorted, nocolor_opt, csv_opt, longest_server_name, show_dates) ⇒ Object
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 |
# File 'lib/awsutils/ec2listmachines.rb', line 231 def print_machine_list(servers_sorted, nocolor_opt, csv_opt, longest_server_name, show_dates) servers_sorted.each do |server| name = server[:name] date = server[:date] role = server[:role] az = server[:az] id = server[:id] group = server[:group] ip = server[:pub_ip] static_ip = server[:static_ip] flavor = server[:flavor] state = server[:state] fcolor = get_flavor_color(flavor) scolor = get_state_color(state) if (nocolor_opt == false) && (csv_opt == false) if show_dates printf( "\033[1m%-#{longest_server_name}s\033[0m %-30s %-16s %-10s %-10s " \ "%-24s %-19s \033[#{fcolor}m%-11s\033[#{scolor}m%-7s\033[0m\n", name, date, role, az, id, group, "#{ip} #{static_ip}", flavor, state ) else printf( "\033[1m%-#{longest_server_name}s\033[0m %-16s %-10s %-10s " \ "%-24s %-19s \033[#{fcolor}m%-11s\033[#{scolor}m%-7s\033[0m\n", name, role, az, id, group, "#{ip} #{static_ip}", flavor, state ) end elsif csv_opt == true if show_dates printf( "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", name, date, role, az, id, group, "#{ip},#{static_ip}", flavor, state ) else printf( "%s,%s,%s,%s,%s,%s,%s,%s\n", name, role, az, id, group, "#{ip},#{static_ip}", flavor, state ) end else if show_dates printf( "%-#{longest_server_name}s %-20s %-16s %-10s %-10s %-20s %-19s %-11s%-7s\n", name, date, role, az, id, group, "#{ip} #{static_ip}", flavor, state ) else printf( "%-#{longest_server_name}s %-16s %-10s %-10s %-20s %-19s %-11s%-7s\n", name, role, az, id, group, "#{ip} #{static_ip}", flavor, state ) end end end end |
#sort_servers(servers_a, sort_col) ⇒ Object
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 |
# File 'lib/awsutils/ec2listmachines.rb', line 101 def sort_servers(servers_a, sort_col) case sort_col when 'role' servers_a.sort_by { |a| a[:role] } when 'az' servers_a.sort_by { |a| a[:az] } when 'name' servers_a.sort_by { |a| a[:name] } when 'id' servers_a.sort_by { |a| a[:id] } when 'group' servers_a.sort_by { |a| a[:group] } when 'flavor' servers_a.sort_by { |a| a[:flavor] } when 'state' servers_a.sort_by { |a| a[:state] } when 'ip' servers_a.sort_by { |a| a[:pub_ip] } when 'static' servers_a.sort_by { |a| a[:static_ip] } when 'date' servers_a.sort_by { |a| a[:date] } else servers_a.sort_by { |a| a[:name] } end end |