Class: Nodectl::Service

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest) ⇒ Service

Returns a new instance of Service.



6
7
8
9
10
# File 'lib/nodectl/service.rb', line 6

def initialize(manifest)
  @manifest = manifest

  Nodectl::Service[name] = self
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



4
5
6
# File 'lib/nodectl/service.rb', line 4

def manifest
  @manifest
end

Class Method Details

.load_registerObject



120
121
122
123
124
# File 'lib/nodectl/service.rb', line 120

def load_register
  Nodectl.manifest["services"].each do |service_manifest|
    new(service_manifest)
  end
end

Instance Method Details

#as_jsonObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nodectl/service.rb', line 95

def as_json
  json = {
    "id"                => name,
    "status"            => status,
    "instance_ids"      => instances.map(&:pid),
    "recipe_name"       => recipe_name,
    "supported_actions" => supported_actions,
    "supported_slots"   => supported_slots,
    "logs"              => logs,
    "params"            => recipe.params
  }

  binding = recipe_binding

  if status == :installed && binding.supported_slots.include?(:version)
    version = binding.run_slot(:version)
    json["version_id"]        = version["id"]
    json["version_timestamp"] = version["timestamp"]
    json["version_message"]   = version["message"]
  end
  
  json
end

#depsObject



16
17
18
19
20
# File 'lib/nodectl/service.rb', line 16

def deps
  (@manifest["deps"] || []).map do |service_name|
    self.class.find!(service_name)
  end
end

#exist?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/nodectl/service.rb', line 34

def exist?
  # TODO: think of a better criterion for existing installation
  source_path.exist?
end

#inspectObject



26
27
28
# File 'lib/nodectl/service.rb', line 26

def inspect
  "#<#{self.class} name:#{name}>"
end

#instancesObject



51
52
53
# File 'lib/nodectl/service.rb', line 51

def instances
  Nodectl::Instance.all.select { |i| i.service == self }
end

#logsObject

TODO: Refactor this method (and may be whole log-related classes):

service class should not know about log internals and streaming
urls


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nodectl/service.rb', line 76

def logs
  unless @logs
    if recipe
      binding = recipe_binding
      @logs = recipe.logs.map do |log|
        {
          "id"   => "#{name}-#{log}",
          "name" => log,
          "path" => "/logs/#{name}/#{log}",
        }
      end
    else
      @logs = []
    end
  end

  @logs
end

#nameObject



12
# File 'lib/nodectl/service.rb', line 12

def name; @manifest["name"]; end

#pathObject



13
# File 'lib/nodectl/service.rb', line 13

def path; @manifest["path"]; end

#recipeObject



43
44
45
# File 'lib/nodectl/service.rb', line 43

def recipe
  @recipe ||= Nodectl::Recipe.find(recipe_name)
end

#recipe_bindingObject



47
48
49
# File 'lib/nodectl/service.rb', line 47

def recipe_binding
  recipe.bind(self) if recipe
end

#recipe_nameObject



22
23
24
# File 'lib/nodectl/service.rb', line 22

def recipe_name
  @manifest["recipe"]
end

#repoObject



14
# File 'lib/nodectl/service.rb', line 14

def repo; @manifest["repo"]; end

#source_pathObject



30
31
32
# File 'lib/nodectl/service.rb', line 30

def source_path
  Nodectl.options[:source_dir].join(path)
end

#statusObject



39
40
41
# File 'lib/nodectl/service.rb', line 39

def status
  exist? ? :installed : :available
end

#supported_actionsObject



55
56
57
58
59
60
61
62
# File 'lib/nodectl/service.rb', line 55

def supported_actions
  binding = recipe_binding
  if binding
    binding.supported_actions
  else
    []
  end
end

#supported_slotsObject



64
65
66
67
68
69
70
71
# File 'lib/nodectl/service.rb', line 64

def supported_slots
  binding = recipe_binding
  if binding
    binding.supported_slots
  else
    []
  end
end