Class: Simbiotes::Portal

Inherits:
Object
  • Object
show all
Defined in:
lib/simbiotes/portal.rb

Class Method Summary collapse

Class Method Details

.ca_certificateObject



149
150
151
152
153
# File 'lib/simbiotes/portal.rb', line 149

def self.ca_certificate
  response = HTTParty.post("#{Simbiotes.configuration.portal}api/ca_certificate", :query => {:public_key => Simbiotes.configuration.public_key, :private_key => Simbiotes.configuration.private_key}).body
  hash = JSON.parse(response)
  return hash["ca_certificate"]
end

.generate_certificate(csr) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/simbiotes/portal.rb', line 134

def self.generate_certificate(csr)
  csr_file = Tempfile.new
  csr_file.write(csr)
  csr_file.rewind
  payload = {
    :multipart => true,
    :csr => csr_file,
    :public_key => Simbiotes.configuration.public_key,
    :private_key => Simbiotes.configuration.private_key
  }
  r = RestClient.post("#{Simbiotes.configuration.portal}api/generate_certificate", payload)
  instance_hash = JSON.parse(r.body)
  return instance_hash["certificate"]
end

.get_attributes(parent_name, model_name) ⇒ Object



8
9
10
11
# File 'lib/simbiotes/portal.rb', line 8

def self.get_attributes(parent_name, model_name)
  configuration = Portal.retrieve_configuration
  attributes = Portal.parse_drivers_and_scripts(configuration, parent_name, model_name)
end

.parse_all_interfacesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/simbiotes/portal.rb', line 63

def self.parse_all_interfaces
  configuration = Portal.retrieve_configuration
  hash = Hash.new
  configuration["workers"].each do |key, value|
    hash[key] = Hash.new
    configuration["workers"][key]["drivers"].each do |k,v|
      a = Array.new
      if v["interfaces"] != nil
        v["interfaces"].each do |key, value|
          a << key
        end
      end
      hash[key][v["metadata"]["common name"]] = a
    end
    configuration["workers"][key]["scripts"].each do |k,v|
      a = Array.new
      if v["interfaces"] != nil
        v["interfaces"].each do |key, value|
          a << key
        end
      end
      hash[key][v["metadata"]["common name"]] = a
    end
  end
  hash
end

.parse_drivers_and_scripts(hash, parent_name, model_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/simbiotes/portal.rb', line 13

def self.parse_drivers_and_scripts(hash, parent_name, model_name)
  attributes_hash = nil
  hash["workers"][parent_name]["drivers"].each do |k,v|
    name = v["metadata"]["common name"].classify
    if name == model_name.classify
      attributes_hash = Hash.new
      attributes_hash[:kind] = "driver"
      attributes_hash[:attributes] = Hash.new
      v["interfaces"].each do |k,v|
        attributes_hash[:attributes][k] = {
          type: v["type"],
          accessor: v["accessor type"],
          units: v["units"],
          noise: v["noise threshold"],
          values: v["values"],
          range: v["range"]
        }
      end
    end     
  end
  hash["workers"][parent_name]["scripts"].each do |k,v|
    name = v["metadata"]["common name"].classify
    if name == model_name.classify
      attributes_hash = Hash.new
      attributes_hash[:kind] = "script"
      attributes_hash[:attributes] = Hash.new
      v["interfaces"].each do |key,value|
        attributes_hash[:attributes][key] = {
          type: value["type"],
          accessor: value["accessor type"],
          units: value["units"],
          noise: value["noise threshold"],
          values: value["values"],
          range: value["range"]
        }
      end
      attributes_hash[:inputs] = Hash.new
      v["inputs"].each do |key,value|
        attributes_hash[:inputs][key] = value
      end
    end
  end
  return attributes_hash
end

.retrieve_configurationObject



58
59
60
61
# File 'lib/simbiotes/portal.rb', line 58

def self.retrieve_configuration
  json = HTTParty.post("#{Simbiotes.configuration.portal}api/hive", :query => {:public_key => Simbiotes.configuration.public_key, :private_key => Simbiotes.configuration.private_key}).body
  hash = Simbiotes::Parse.portal(json)
end

.sync_device_instances(worker_name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/simbiotes/portal.rb', line 90

def self.sync_device_instances(worker_name)
  instance_hash = JSON.parse(HTTParty.post("#{Simbiotes.configuration.portal}api/workers", :query => {:public_key => Simbiotes.configuration.public_key, :private_key => Simbiotes.configuration.private_key}).body)      
  klass = (worker_name.to_s + "::" + worker_name.to_s).constantize rescue nil
  unless klass == nil
    instance_ids = Array.new
    instance_hash[worker_name].each do |instance_id,status|
      instance_ids << instance_id
    end
    stale_instances = klass.where.not(simbiotes_instance: instance_ids)
    stale_instances.destroy_all
    instance_ids.each do |instance_id|
      i = klass.find_by(simbiotes_instance: instance_id)
      if i == nil
        i = klass.new
        i.simbiotes_instance = instance_id
        i.save(:validate => false)
        Simbiotes.configuration.targets[worker_name].keys.each do |key|
          subklass = (worker_name + "::" + key).constantize rescue nil
          unless subklass == nil
            s = subklass.new
            k = worker_name.underscore.gsub(" ","_").to_s + "_id="
            s.send((k).to_sym, i.id)
            s.skip_extract = true
            s.save(:validate => false)
            s.skip_extract = false
          end
        end 
      end
    end
  end
end

.upload_script(worker, script) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/simbiotes/portal.rb', line 122

def self.upload_script(worker, script)
  payload = {
    :multipart => true,
    :script_file => File.open("#{Rails.root}/lib/scripts/simbiotes/#{worker.underscore.downcase}/#{script.underscore.downcase}.rb", 'rb'),
    :public_key => Simbiotes.configuration.public_key,
    :private_key => Simbiotes.configuration.private_key,
    :worker_name => worker,
    :script_name => script
  }
  r = RestClient.post("#{Simbiotes.configuration.portal}api/upload_script", payload)
end