class Simbiotes::Portal

Public Class Methods

ca_certificate() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_interfaces() click to toggle source
# 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) click to toggle source
# 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_configuration() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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