kakin 0.2.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f4cbb82bf72ff7739687b18cb6a6c2d059a8d5b4630cffbd464760154e336d8
4
- data.tar.gz: fb806dbb427e76cd69cf59350faf790633f0e6584dfb4d667a7e2b7dbf7ff91b
3
+ metadata.gz: c4d79cc2ffb8deb8ebb267963327939ee177c60ddb014217ee433a0394981c9f
4
+ data.tar.gz: bf19b09fc2874309ed228ed06a9179e659d5f3d54ecfa7db6e4357b8898ad9cd
5
5
  SHA512:
6
- metadata.gz: 3ea58c1be161a984a54b026bd749497215920e2b7fc7ba4dd3a7427e6c168da1497be417a41df0958e5cfaf186fac64dff1af417d65bfb0cbd299b6314107975
7
- data.tar.gz: 759198768dc66b2fb180a47d962a406505e8a36a39a7a421368e99570c75d8db5a8269358a9ad64707f058ee99fe738d08cd6b84f243a9c3562f3b7a918561bc
6
+ metadata.gz: 276941d537c9c47a2e31b88564cf9d12a8c11ce2b56fd035d0b35113fcbbf0add7e2cdaa8ea5cbfe43fc4b4a9893fa1e6b8925aae4aff1c90a7d4bf1a72735cc
7
+ data.tar.gz: f7f2dc5c6369018a79060ce2a7b947b69efbb7625fe2eb11d120e5b4d24c5fe3e848667e8b29e526c69ddb3f15c2ed51ca8ac04fcd58c8b275a77bc7d79234c4
data/README.md CHANGED
@@ -12,12 +12,20 @@ You need to create configuration file located `~/.kakin` for openstack credentia
12
12
 
13
13
  ```
14
14
  auth_url: "http://your-openstack-host:35357/v2.0/tokens"
15
- management_url: "http://your-openstack-host:8774/v2"
16
15
  username: "username"
17
16
  tenant: "your-admin-tenant"
18
17
  password: "password"
19
18
  ```
20
19
 
20
+ Or set with environment variable.
21
+
22
+ ```
23
+ export OS_AUTH_URL=<your openstack auth url>
24
+ export OS_USER=<your username>
25
+ export OS_TENANT_NAME=<your tenant>
26
+ export OS_PASSWORD=<your password>
27
+ ```
28
+
21
29
  You can get resource usage with following command.
22
30
 
23
31
  ```
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency 'thor'
22
22
  spec.add_dependency 'yao'
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "bundler", "~> 2.0.1"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "minitest"
27
27
  end
@@ -3,6 +3,7 @@ require 'yaml'
3
3
  require 'json'
4
4
  require 'net/http'
5
5
  require 'yao'
6
+ require 'kakin/yao_ext/yao'
6
7
  require 'kakin/yao_ext/tenant'
7
8
  require 'kakin/yao_ext/server'
8
9
  require 'kakin/yao_ext/floatingip'
@@ -26,20 +27,17 @@ module Kakin
26
27
 
27
28
  STDERR.puts "Start: #{start_time}"
28
29
  STDERR.puts "End: #{end_time}"
29
- url = URI.parse("#{Kakin::Configuration.management_url}/#{Yao::Tenant.get_by_name(Kakin::Configuration.tenant).id}/os-simple-tenant-usage?start=#{start_time}&end=#{end_time}")
30
- req = Net::HTTP::Get.new(url)
31
- req["Accept"] = "application/json"
32
- req["X-Auth-Token"] = Yao::Auth.try_new.token
33
- res = Net::HTTP.start(url.host, url.port) {|http|
34
- http.request(req)
35
- }
36
-
37
- if res.code != "200"
30
+ client = Yao.default_client.pool['compute']
31
+ res = client.get("./os-simple-tenant-usage?start=#{start_time}&end=#{end_time}") do |req|
32
+ req.headers["Accept"] = "application/json"
33
+ end
34
+
35
+ if res.status != 200
38
36
  raise "usage data fatch is failed"
39
37
  else
40
38
  result = Hash.new
41
- tenant_usages = JSON.load(res.body)["tenant_usages"]
42
- tenants = Yao::Tenant.list
39
+ tenant_usages = res.body["tenant_usages"]
40
+ tenants = Yao.tenant_klass.list
43
41
 
44
42
  unless options[:t].empty?
45
43
  tenant = tenants.find { |tenant| tenant.name == options[:t] }
@@ -49,7 +47,9 @@ module Kakin
49
47
  end
50
48
 
51
49
  tenant_usages.each do |usage|
52
- tenant = tenants.find { |tenant| tenant.id == usage["tenant_id"] }
50
+ tenant_id = usage["tenant_id"]
51
+ tenant = tenants.find { |tenant| tenant.id == tenant_id }
52
+ tenant_name = tenant&.name || tenant_id
53
53
 
54
54
  total_vcpus_usage = usage["total_vcpus_usage"]
55
55
  total_memory_mb_usage = usage["total_memory_mb_usage"]
@@ -59,7 +59,7 @@ module Kakin
59
59
  bill_memory = total_memory_mb_usage * yaml["memory_mb_per_hour"]
60
60
  bill_disk = total_local_gb_usage * yaml["disk_gb_per_hour"]
61
61
 
62
- result[tenant.name] = {
62
+ result[tenant_name] = {
63
63
  'bill_total' => bill_vcpu + bill_memory + bill_disk,
64
64
  'bill_vcpu' => bill_vcpu,
65
65
  'bill_memory' => bill_memory,
@@ -92,9 +92,9 @@ module Kakin
92
92
 
93
93
  result = Hash.new
94
94
  tenants = unless options[:t].empty?
95
- Yao::Tenant.list(name: options[:t])
95
+ Yao.tenant_klass.list(name: options[:t])
96
96
  else
97
- Yao::Tenant.list
97
+ Yao.tenant_klass.list
98
98
  end
99
99
  tenants = [tenants] unless tenants.is_a?(Array)
100
100
 
@@ -122,9 +122,9 @@ module Kakin
122
122
 
123
123
  result = Hash.new
124
124
  tenants = unless options[:t].empty?
125
- Yao::Tenant.list(name: options[:t])
125
+ Yao.tenant_klass.list(name: options[:t])
126
126
  else
127
- Yao::Tenant.list
127
+ Yao.tenant_klass.list
128
128
  end
129
129
  tenants = [tenants] unless tenants.is_a?(Array)
130
130
 
@@ -149,9 +149,9 @@ module Kakin
149
149
 
150
150
  result = Hash.new
151
151
  tenants = unless options[:t].empty?
152
- Yao::Tenant.list(name: options[:t])
152
+ Yao.tenant_klass.list(name: options[:t])
153
153
  else
154
- Yao::Tenant.list
154
+ Yao.tenant_klass.list
155
155
  end
156
156
  tenants = [tenants] unless tenants.is_a?(Array)
157
157
  volume_types = Yao::VolumeType.list
@@ -160,10 +160,10 @@ module Kakin
160
160
  tenants.each do |tenant|
161
161
  result[tenant.name] ||= {}
162
162
  volume_types.each do |volume_type|
163
- total = volumes.select { |volume| volume.tenant_id == tenant.id && volume.volume_type == volume_type.name }.map(&:size).sum
163
+ count = volumes.select { |volume| volume.tenant_id == tenant.id && volume.volume_type == volume_type.name }.map(&:size).sum
164
164
  result[tenant.name][volume_type.name] = {
165
- 'total': total,
166
- 'total_usage': total * yaml['volume_cost_per_gb'][volume_type.name]
165
+ 'count': count,
166
+ 'total_usage': count * yaml['volume_cost_per_gb'][volume_type.name]
167
167
  }
168
168
  end
169
169
  end
@@ -1,28 +1,46 @@
1
1
  module Kakin
2
2
  class Configuration
3
3
 
4
- def self.management_url
5
- @@_management_url
6
- end
7
-
8
4
  def self.tenant
9
5
  @@_tenant
10
6
  end
11
7
 
12
8
  def self.setup
13
- yaml = YAML.load_file(File.expand_path('~/.kakin'))
9
+ config = {
10
+ 'auth_url' => ENV['OS_AUTH_URL'],
11
+ 'tenant' => ENV['OS_TENANT_NAME'] || ENV['OS_PROJECT_NAME'],
12
+ 'username' => ENV['OS_USERNAME'],
13
+ 'password' => ENV['OS_PASSWORD'],
14
+ 'client_cert' => ENV['OS_CERT'],
15
+ 'client_key' => ENV['OS_KEY'],
16
+ 'identity_api_version' => ENV['OS_IDENTITY_API_VERSION'],
17
+ 'user_domain_name' => ENV['OS_USER_DOMAIN_NAME'],
18
+ 'project_domain_name' => ENV['OS_PROJECT_DOMAIN_NAME'],
19
+ 'timeout' => ENV['YAO_TIMEOUT'],
20
+ 'management_url' => ENV['YAO_MANAGEMENT_URL'],
21
+ 'debug' => ENV['YAO_DEBUG'] || false,
22
+ }
23
+
24
+ file_path = File.expand_path('~/.kakin')
25
+ if File.exist?(file_path)
26
+ yaml = YAML.load_file(file_path)
27
+ config.merge!(yaml)
28
+ end
14
29
 
15
- @@_management_url = yaml['management_url']
16
- @@_tenant = yaml['tenant']
30
+ @@_tenant = config['tenant']
17
31
 
18
32
  Yao.configure do
19
- auth_url yaml['auth_url']
20
- tenant_name yaml['tenant']
21
- username yaml['username']
22
- password yaml['password']
23
- timeout yaml['timeout'] if yaml['timeout']
24
- client_cert yaml['client_cert'] if yaml['client_cert']
25
- client_key yaml['client_key'] if yaml['client_key']
33
+ auth_url config['auth_url']
34
+ tenant_name config['tenant']
35
+ username config['username']
36
+ password config['password']
37
+ timeout config['timeout'].to_i if config['timeout']
38
+ client_cert config['client_cert'] if config['client_cert']
39
+ client_key config['client_key'] if config['client_key']
40
+ identity_api_version config['identity_api_version'] if config['identity_api_version']
41
+ user_domain_name config['user_domain_name'] if config['user_domain_name']
42
+ project_domain_name config['project_domain_name'] if config['project_domain_name']
43
+ debug config['debug']
26
44
  end
27
45
  end
28
46
  end
@@ -1,3 +1,3 @@
1
1
  module Kakin
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -0,0 +1,10 @@
1
+ module Yao
2
+ def self.tenant_klass
3
+ Yao.keystone_v2? ? Yao::Tenant : Yao::Project
4
+ end
5
+
6
+ # @return [Bool]
7
+ def self.keystone_v2?
8
+ Yao.default_client.pool["identity"].url_prefix.to_s.match(/v2\.0/)
9
+ end
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kakin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - buty4649
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-11-16 00:00:00.000000000 Z
12
+ date: 2020-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '1.10'
48
+ version: 2.0.1
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '1.10'
55
+ version: 2.0.1
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rake
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -107,6 +107,7 @@ files:
107
107
  - lib/kakin/yao_ext/floatingip.rb
108
108
  - lib/kakin/yao_ext/server.rb
109
109
  - lib/kakin/yao_ext/tenant.rb
110
+ - lib/kakin/yao_ext/yao.rb
110
111
  homepage: https://github.com/yaocloud/kakin
111
112
  licenses: []
112
113
  metadata: {}
@@ -125,8 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  - !ruby/object:Gem::Version
126
127
  version: '0'
127
128
  requirements: []
128
- rubyforge_project:
129
- rubygems_version: 2.7.6
129
+ rubygems_version: 3.1.2
130
130
  signing_key:
131
131
  specification_version: 4
132
132
  summary: kakin is resource calcuration tool for OpenStack