Sha256: 5c98ad725a0dee01863737e8c8fe2a6a2c28de9b2f50ffcaffc5c562797e9623

Contents?: true

Size: 1.69 KB

Versions: 22

Compression:

Stored size: 1.69 KB

Contents

require "vmc/cli"

module VMC
  module App
    class Base < CLI
      # 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
        used = info[:usage][:memory]
        limit = info[:limits][:memory]
        available = limit - used + exclude

        mem = 64
        choices = []
        until mem > available
          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

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
static-1.0.3 vendor/vmc-0.5.0/lib/vmc/cli/app/base.rb
af-0.5.0.beta.11 lib/vmc/cli/app/base.rb
af-0.5.0.beta.10 lib/vmc/cli/app/base.rb
static-1.0.1 vendor/vmc-0.5.0/lib/vmc/cli/app/base.rb
af-0.5.0.beta.9 lib/vmc/cli/app/base.rb
af-0.5.0.beta.8 lib/vmc/cli/app/base.rb
af-0.5.0.beta.7 lib/vmc/cli/app/base.rb
af-0.5.0.beta.6 lib/vmc/cli/app/base.rb
af-0.5.0.beta.5 lib/vmc/cli/app/base.rb
af-0.5.0.beta.4 lib/vmc/cli/app/base.rb
af-0.5.0.beta.3 lib/vmc/cli/app/base.rb
af-0.5.0.beta.2 lib/vmc/cli/app/base.rb
af-0.5.0.beta.1 lib/vmc/cli/app/base.rb
vmc-0.5.0 lib/vmc/cli/app/base.rb
vmc-0.5.0.rc4 lib/vmc/cli/app/base.rb
vmc-0.5.0.rc3 lib/vmc/cli/app/base.rb
vmc-0.5.0.rc2 lib/vmc/cli/app/base.rb
vmc-0.5.0.rc1 lib/vmc/cli/app/base.rb
vmc-0.5.0.beta.12 lib/vmc/cli/app/base.rb
vmc-0.5.0.beta.11 lib/vmc/cli/app/base.rb