cloudfoundry/vmc

View on GitHub
lib/vmc/cli/app/base.rb

Summary

Maintainability
A
0 mins
Test Coverage
require "vmc/cli/v2_check_cli"

module VMC
  module App
    class Base < V2CheckCLI
      # choose the right color for app/instance state
      def state_color(s)
        case s
        when "STARTING"
          :neutral
        when "STARTED", "RUNNING"
          :good
        when "DOWN"
          :bad
        when "FLAPPING"
          :error
        when "N/A"
          :unknown
        else
          :warning
        end
      end

      def app_status(a)
        health = a.health

        if a.debug_mode == "suspend" && health == "0%"
          c("suspended", :neutral)
        else
          c(health.downcase, state_color(health))
        end
      end

      def memory_choices(exclude = 0)
        info = client.info

        usage = info[:usage]
        limit = info[:limits][:memory]

        ceiling =
          if usage
            used = usage[:memory]
            limit - used + exclude
          else
            limit
          end

        mem = 64
        choices = []
        until mem > ceiling
          choices << human_mb(mem)
          mem *= 2
        end

        choices
      end

      def human_mb(num)
        human_size(num * 1024 * 1024, 0)
      end

      def human_size(num, precision = 1)
        sizes = %w(G M K)
        sizes.each.with_index do |suf, i|
          pow = sizes.size - i
          unit = 1024.0 ** pow
          if num >= unit
            return format("%.#{precision}f%s", num / unit, suf)
          end
        end

        format("%.#{precision}fB", num)
      end

      def megabytes(str)
        if str =~ /T$/i
          str.to_i * 1024 * 1024
        elsif str =~ /G$/i
          str.to_i * 1024
        elsif str =~ /M$/i
          str.to_i
        elsif str =~ /K$/i
          str.to_i / 1024
        else # assume megabytes
          str.to_i
        end
      end
    end
  end
end