Class: WrataApi::ServerList

Inherits:
Object
  • Object
show all
Defined in:
lib/wrata_api/server_list.rb

Overview

Class for working with several servers at once

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ ServerList

Returns a new instance of ServerList.



9
10
11
12
13
# File 'lib/wrata_api/server_list.rb', line 9

def initialize(api)
  @api = api
  @servers = []
  @concurrent_operation_at_once = 5
end

Instance Attribute Details

#serversArray, Hash

Returns list of servers.

Returns:

  • (Array, Hash)

    list of servers



7
8
9
# File 'lib/wrata_api/server_list.rb', line 7

def servers
  @servers
end

Instance Method Details

#bookNothing

Book each server

Returns:

  • (Nothing)


68
69
70
71
72
# File 'lib/wrata_api/server_list.rb', line 68

def book
  @servers.each do |current|
    @api.book_server(current['name'])
  end
end

#concurrent_actionObject

Turn on all servers in list



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wrata_api/server_list.rb', line 21

def concurrent_action
  splitted_servers = @servers.each_slice(@concurrent_operation_at_once).to_a
  splitted_servers.each do |each_slice|
    threads = []
    each_slice.each do |current_server|
      threads << Thread.new(current_server) do
        yield(current_server)
      end
    end
    threads.each(&:join)
  end
end

#lengthInteger

Returns count of servers.

Returns:

  • (Integer)

    count of servers



16
17
18
# File 'lib/wrata_api/server_list.rb', line 16

def length
  @servers.length
end

#power_offNothing

Power on all servers

Returns:

  • (Nothing)


52
53
54
55
56
# File 'lib/wrata_api/server_list.rb', line 52

def power_off
  concurrent_action do |server|
    @api.power_off_server(server['name'])
  end
end

#power_on(size = nil) ⇒ Nothing

Power on all servers

Returns:

  • (Nothing)


36
37
38
39
40
# File 'lib/wrata_api/server_list.rb', line 36

def power_on(size = nil)
  concurrent_action do |server|
    @api.power_on_server(server['name'], size)
  end
end

#powered_off?True, False

Returns is all servers powered on?.

Returns:

  • (True, False)

    is all servers powered on?



59
60
61
62
63
64
# File 'lib/wrata_api/server_list.rb', line 59

def powered_off?
  @servers.each do |current|
    return false unless @api.powering_status(current['name']) == :off
  end
  true
end

#powered_on?True, False

Returns is all servers powered on?.

Returns:

  • (True, False)

    is all servers powered on?



43
44
45
46
47
48
# File 'lib/wrata_api/server_list.rb', line 43

def powered_on?
  @servers.each do |current|
    return false unless @api.powering_status(current['name']) == :on
  end
  true
end

#unbookNothing

Unbook each server

Returns:

  • (Nothing)


76
77
78
79
80
# File 'lib/wrata_api/server_list.rb', line 76

def unbook
  @servers.each do |current|
    @api.unbook_server(current['name'])
  end
end