Class: Fleet::Client

Inherits:
Object
  • Object
show all
Includes:
Machines, State, Unit, Connection, Request
Defined in:
lib/fleet/client.rb,
lib/fleet/client/unit.rb,
lib/fleet/client/state.rb,
lib/fleet/client/machines.rb

Defined Under Namespace

Modules: Machines, State, Unit

Constant Summary

Constants included from State

State::STATE_RESOURCE

Constants included from Unit

Unit::UNITS_RESOURCE

Constants included from Machines

Machines::MACHINES_RESOURCE

Instance Method Summary collapse

Methods included from State

#list_states

Methods included from Unit

#create_unit, #delete_unit, #get_unit, #list_units

Methods included from Machines

#list_machines

Methods included from Connection

#connection

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/fleet/client.rb', line 14

def initialize(options={})
  options = Fleet.options.merge(options)
  Configuration::VALID_OPTIONS_KEYS.each do |key|
    send("#{key}=", options[key])
  end
end

Instance Method Details

#destroy(name) ⇒ Object



95
96
97
# File 'lib/fleet/client.rb', line 95

def destroy(name)
  delete_unit(name)
end

#get_unit_state(name) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/fleet/client.rb', line 103

def get_unit_state(name)
  options = { unitName: name }
  states = list_states(options)
  if states["states"]
    states["states"].first
  else
    fail NotFound, "Unit '#{name}' not found"
  end
end

#listObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fleet/client.rb', line 28

def list
  machines = list_machines['machines'] || []
  machine_ips = machines.each_with_object({}) do |machine, h|
    h[machine['id']] = machine['primaryIP']
  end

  states = list_states['states'] || []
  states.map do |service|
    {
      name: service['name'],
      load_state: service['systemdLoadState'],
      active_state: service['systemdActiveState'],
      sub_state: service['systemdSubState'],
      machine_id: service['machineID'],
      machine_ip: machine_ips[service['machineID']]
    }
  end
end

#list_fleet_machinesObject



51
52
53
# File 'lib/fleet/client.rb', line 51

def list_fleet_machines
  list_machines['machines'] || []
end

#list_unit_statesObject



47
48
49
# File 'lib/fleet/client.rb', line 47

def list_unit_states
  list_states['states'] || []
end

#load(name, service_def = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/fleet/client.rb', line 70

def load(name, service_def=nil)

  if service_def
    submit(name, service_def)
  end

  opts = { 'desiredState' => 'loaded', 'name' => name }
  update_unit(name, opts)
end

#start(name) ⇒ Object



80
81
82
83
# File 'lib/fleet/client.rb', line 80

def start(name)
  opts = { 'desiredState' => 'launched', 'name' => name }
  update_unit(name, opts)
end

#status(name) ⇒ Object



99
100
101
# File 'lib/fleet/client.rb', line 99

def status(name)
  get_unit(name)["currentState"].to_sym
end

#stop(name) ⇒ Object



85
86
87
88
# File 'lib/fleet/client.rb', line 85

def stop(name)
  opts = { 'desiredState' => 'loaded', 'name' => name }
  update_unit(name, opts)
end

#submit(name, service_def) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fleet/client.rb', line 55

def submit(name, service_def)
  unless name =~ /\A[a-zA-Z0-9:_.@-]+\Z/
    raise ArgumentError, 'name may only contain [a-zA-Z0-9:_.@-]'
  end

  unless service_def.is_a?(ServiceDefinition)
    service_def = ServiceDefinition.new(service_def)
  end

  begin
    create_unit(name, service_def.to_unit(name))
  rescue Fleet::PreconditionFailed
  end
end

#unload(name) ⇒ Object



90
91
92
93
# File 'lib/fleet/client.rb', line 90

def unload(name)
  opts = { 'desiredState' => 'inactive', 'name' => name }
  update_unit(name, opts)
end