Class: Vines::Services::CouchModels::System

Inherits:
CouchRest::Model::Base
  • Object
show all
Extended by:
Storage::CouchDB::ClassMethods
Defined in:
lib/vines/services/storage/couchdb/system.rb

Constant Summary collapse

KEYS =
%w[_id ohai created_at updated_at].freeze
VIEW_NAME =
"System/memberships".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Storage::CouchDB::ClassMethods

build_from_database, to_s

Instance Attribute Details

#servicesObject

Query the members view and return the Service objects to which this System belongs. The services are cached so subsequent calls to this method do not query the view.



51
52
53
54
55
56
57
58
# File 'lib/vines/services/storage/couchdb/system.rb', line 51

def services
  unless @services
    rows = database.view(VIEW_NAME, reduce: false, key: [1, name])['rows'] rescue []
    ids = rows.map {|row| row['value'] }
    @services = Service.all.keys(ids).to_a
  end
  @services
end

Class Method Details

.find_allObject



106
107
108
109
110
# File 'lib/vines/services/storage/couchdb/system.rb', line 106

def self.find_all
  by_name.rows.map do |row|
    {name: row['key']}
  end
end

.find_attributesObject



101
102
103
104
# File 'lib/vines/services/storage/couchdb/system.rb', line 101

def self.find_attributes
  view = attributes.reduce.group
  view.rows.map {|row| row['key'] }
end

.find_by_name(name) ⇒ Object



112
113
114
# File 'lib/vines/services/storage/couchdb/system.rb', line 112

def self.find_by_name(name)
  find("system:#{name.downcase}")
end

.find_by_names(names) ⇒ Object

Return an Array of Systems with the given names. This method efficiently bulk loads systems and their services much more quickly than loading systems one by one. Note that the systems returned by this method do not have their ohai data loaded because it’s expensive.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vines/services/storage/couchdb/system.rb', line 120

def self.find_by_names(names)
  keys = names.map {|name| [1, name.downcase] }
  rows = database.view(VIEW_NAME, reduce: false, keys: keys)['rows'] rescue []
  ids = rows.map {|row| row['value'] }.uniq
  services = Service.all.keys(ids).to_a
  by_id = Hash[services.map {|s| [s.id, s] }]
  by_name = Hash.new do |h, k|
    h[k] = System.new(id: "system:#{k}").tap do |system|
      system.services = []
    end
  end
  rows.each do |row|
    name = row['key'][1]
    service = by_id[row['value']]
    by_name[name].services << service
  end
  by_name.values
end

.notify_members(stream, from, members) ⇒ Object

Send updated permissions to each system that belongs to the service (or belonged to it before the save).



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vines/services/storage/couchdb/system.rb', line 87

def self.notify_members(stream, from, members)
  return if members.empty?
  names = members.map {|m| m['name'] }.uniq
  systems = System.find_by_names(names)
  nodes = systems.map do |system|
    Blather::Stanza::Iq::Query.new(:set).tap do |result|
      result.to = Blather::JID.new(system.name, from.domain)
      result.query.content = system.to_result.to_json
      result.query.namespace = 'http://getvines.com/protocol/systems'
    end
  end
  Throttle.new(stream).async_send(nodes)
end

Instance Method Details

#nameObject



44
45
46
# File 'lib/vines/services/storage/couchdb/system.rb', line 44

def name
  id.sub('system:', '')
end

#permissionsObject

Return a Hash of unix user ID to an Array of JID’s allowed to access that account. For example: => [‘[email protected]’],

'root'   => ['[email protected]]


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vines/services/storage/couchdb/system.rb', line 64

def permissions
  {}.tap do |perms|
    services.each do |service|
      service.accounts.each do |unix_id|
        jids = (perms[unix_id] ||= [])
        jids << service.users
        jids.flatten!
        jids.sort!.uniq!
      end
    end
  end
end

#to_resultObject



77
78
79
80
81
82
83
# File 'lib/vines/services/storage/couchdb/system.rb', line 77

def to_result
  to_hash.clone.keep_if {|k, v| KEYS.include?(k) }.tap do |h|
    h['name'] = h.delete('_id').sub('system:', '')
    h['services'] = services.map {|s| {id: s.id, jid: s.jid, name: s.name} }
    h['permissions'] = permissions
  end
end