Class: Berta::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/berta/service.rb

Overview

Berta service for communication with OpenNebula

Constant Summary collapse

FILTERS =
{ 'exclude' => Berta::Utils::ExcludeFilter,
'include' => Berta::Utils::IncludeFilter }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret, endpoint) ⇒ Service

Initializes service object and connects to opennebula backend. If both arguments are nil default ONE_AUTH will be used.

Parameters:

  • secret (String)

    Opennebula secret

  • endpoint (String)

    Endpoint of OpenNebula



18
19
20
21
22
# File 'lib/berta/service.rb', line 18

def initialize(secret, endpoint)
  @endpoint = endpoint
  @client = OpenNebula::Client.new(secret, endpoint)
  create_filter
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/berta/service.rb', line 7

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/berta/service.rb', line 6

def endpoint
  @endpoint
end

Instance Method Details

#clustersOpenNebula::ClusterPool

Fetch clusters from OpenNebula



95
96
97
98
99
100
101
102
# File 'lib/berta/service.rb', line 95

def clusters
  return @cached_clusters if @cached_clusters
  cluster_pool = OpenNebula::ClusterPool.new(client)
  Berta::Utils::OpenNebula::Helper.handle_error { cluster_pool.info }
  logger.debug "Fetched clusters: #{cluster_pool.map(&:id)}"
  @cached_clusters = cluster_pool
  cluster_pool
end

#create_filterObject



24
25
26
27
28
29
30
31
# File 'lib/berta/service.rb', line 24

def create_filter
  filter = FILTERS[Berta::Settings.filter.type]
  raise Berta::Errors::WrongFilterTypeError, "Wrong filter type: #{Berta::Settings.filter.type}" unless filter
  @filter = filter.new(Berta::Settings.filter.ids,
                       Berta::Settings.filter.users,
                       Berta::Settings.filter.groups,
                       filtered_clusters)
end

#group_vms(group) ⇒ Object



83
84
85
# File 'lib/berta/service.rb', line 83

def group_vms(group)
  running_vms.select { |vm| vm.handle['GID'] == group.id }
end

#groupsObject



67
68
69
70
71
72
# File 'lib/berta/service.rb', line 67

def groups
  group_pool = OpenNebula::GroupPool.new(client)
  Berta::Utils::OpenNebula::Helper.handle_error { group_pool.info }
  logger.debug "Fetched groups: #{group_pool.map(&:id)}"
  group_pool.map { |group| Berta::EntityHandler.new(group) }
end

#running_vmsArray<Berta::VirtualMachineHandler>

Fetch running vms from OpenNebula and filter out vms that take no resources.



43
44
45
46
47
48
49
50
# File 'lib/berta/service.rb', line 43

def running_vms
  return @cached_vms if @cached_vms
  vm_pool = OpenNebula::VirtualMachinePool.new(client)
  Berta::Utils::OpenNebula::Helper.handle_error { vm_pool.info_all }
  logger.debug "Fetched vms: #{vm_pool.map(&:id)}"
  @cached_vms = @filter.run(vm_pool.map { |vm| Berta::VirtualMachineHandler.new(vm) })
  @cached_vms
end

#user_vms(user) ⇒ Array<Berta::VirtualMachineHandler>

Note:

calls running_vms

Return vms that belong to given user

Parameters:

  • user (Berta::UserHandler)

    User to find vms for

Returns:



79
80
81
# File 'lib/berta/service.rb', line 79

def user_vms(user)
  running_vms.select { |vm| vm.handle['UID'] == user.id }
end

#usersArray<OpenNebula::UserHandler>

Fetch users from OpenNebula



60
61
62
63
64
65
# File 'lib/berta/service.rb', line 60

def users
  user_pool = OpenNebula::UserPool.new(client)
  Berta::Utils::OpenNebula::Helper.handle_error { user_pool.info }
  logger.debug "Fetched users: #{user_pool.map(&:id)}"
  user_pool.map { |user| Berta::EntityHandler.new(user) }
end