Class: Kakin::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/kakin/cli.rb

Instance Method Summary collapse

Instance Method Details

#calcObject



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
# File 'lib/kakin/cli.rb', line 20

def calc
  Kakin::Configuration.setup

  yaml = YAML.load_file(options[:f])
  start_time = Time.parse(options[:s]).strftime("%FT%T")
  end_time = Time.parse(options[:e]).strftime("%FT%T")

  STDERR.puts "Start: #{start_time}"
  STDERR.puts "End:   #{end_time}"
  client = Yao.default_client.pool['compute']
  tenant_id = Yao::Tenant.get_by_name(Kakin::Configuration.tenant).id
  res = client.get("./os-simple-tenant-usage?start=#{start_time}&end=#{end_time}") do |req|
    req.headers["Accept"] = "application/json"
  end

  if res.status != 200
    raise "usage data fatch is failed"
  else
    result = Hash.new
    tenant_usages = res.body["tenant_usages"]
    tenants = Yao::Tenant.list

    unless options[:t].empty?
      tenant = tenants.find { |tenant| tenant.name == options[:t] }
      raise "Not Found tenant #{options[:t]}" unless tenant

      tenant_usages = tenant_usages.select { |tenant_usage| tenant_usage["tenant_id"] == tenant.id }
    end

    tenant_usages.each do |usage|
      tenant = tenants.find { |tenant| tenant.id == usage["tenant_id"] }

      total_vcpus_usage     = usage["total_vcpus_usage"]
      total_memory_mb_usage = usage["total_memory_mb_usage"]
      total_local_gb_usage  = usage["total_local_gb_usage"]

      bill_vcpu   = total_vcpus_usage * yaml["vcpu_per_hour"]
      bill_memory = total_memory_mb_usage * yaml["memory_mb_per_hour"]
      bill_disk   = total_local_gb_usage * yaml["disk_gb_per_hour"]

      result[tenant.name] = {
        'bill_total'            => bill_vcpu + bill_memory + bill_disk,
        'bill_vcpu'             => bill_vcpu,
        'bill_memory'           => bill_memory,
        'bill_disk'             => bill_disk,
        'total_hours'           => usage["total_hours"],
        'total_vcpus_usage'     => total_vcpus_usage,
        'total_memory_mb_usage' => total_memory_mb_usage,
        'total_local_gb_usage'  => total_local_gb_usage,
      }
    end

    puts YAML.dump(result)
  end
end

#ipObject



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
# File 'lib/kakin/cli.rb', line 115

def ip
  Kakin::Configuration.setup

  yaml = YAML.load_file(options[:f])
  ip_regexp = Regexp.new(yaml["ip_regexp"])

  result = Hash.new
  tenants = unless options[:t].empty?
              Yao::Tenant.list(name: options[:t])
            else
              Yao::Tenant.list
            end
  tenants = [tenants] unless tenants.is_a?(Array)

  tenants.each do |tenant|
    count = tenant.ports.select {|p| p.fixed_ips[0]["ip_address"] =~ ip_regexp}.count
    count += Yao::NetworkingFloatingIP.list(tenant_id: tenant.id).select {|p| p.floating_ip_address =~ ip_regexp}.count
    result[tenant.name] = {
      'count'       => count,
      'total_usage' => count * yaml["cost_per_ip"],
    }
  end

  puts YAML.dump(result)
end

#networkObject



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
# File 'lib/kakin/cli.rb', line 81

def network
  Kakin::Configuration.setup

  yaml = YAML.load_file(options[:f])
  start_time = Time.parse(options[:s])
  end_time = Time.parse(options[:e])

  STDERR.puts "Start: #{start_time}"
  STDERR.puts "End:   #{end_time}"

  result = Hash.new
  tenants = unless options[:t].empty?
              Yao::Tenant.list(name: options[:t])
            else
              Yao::Tenant.list
            end
  tenants = [tenants] unless tenants.is_a?(Array)

  tenants.each do |tenant|
    incoming = tenant.network_usage(Regexp.new(yaml["ip_regexp"]), :incoming, start_time.iso8601, end_time.iso8601)
    outgoing = tenant.network_usage(Regexp.new(yaml["ip_regexp"]), :outgoing, start_time.iso8601, end_time.iso8601)
    result[tenant.name] = {
      'incoming_usage'  => incoming,
      'outgoing_usage'  => outgoing,
      'total_usage'     => incoming + outgoing
    }
  end

  puts YAML.dump(result)
end

#volumeObject



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
# File 'lib/kakin/cli.rb', line 144

def volume
  Kakin::Configuration.setup
  yaml = YAML.load_file(options[:f])

  result = Hash.new
  tenants = unless options[:t].empty?
              Yao::Tenant.list(name: options[:t])
            else
              Yao::Tenant.list
            end
  tenants = [tenants] unless tenants.is_a?(Array)
  volume_types = Yao::VolumeType.list
  volumes = Yao::Volume.list_detail(all_tenants: true)

  tenants.each do |tenant|
    result[tenant.name] ||= {}
    volume_types.each do |volume_type|
      count = volumes.select { |volume| volume.tenant_id == tenant.id && volume.volume_type == volume_type.name }.map(&:size).sum
      result[tenant.name][volume_type.name] = {
          'count': count,
          'total_usage': count * yaml['volume_cost_per_gb'][volume_type.name]
      }
    end
  end

  puts YAML.dump(result)
end