Class: Confman::API

Inherits:
Object
  • Object
show all
Defined in:
lib/confman/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI



16
17
18
# File 'lib/confman/api.rb', line 16

def initialize
  self.config_dir = "/etc/confman"
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



11
12
13
# File 'lib/confman/api.rb', line 11

def api_key
  @api_key
end

#config_dirObject



20
21
22
23
24
25
26
27
28
# File 'lib/confman/api.rb', line 20

def config_dir
  # Create the directory
  unless Dir.exists?(@config_dir)
    FileUtils.mkdir_p(@config_dir) 
    File.chmod(0755, @config_dir)
  end

  @config_dir
end

#endpoint_urlObject

Returns the value of attribute endpoint_url.



10
11
12
# File 'lib/confman/api.rb', line 10

def endpoint_url
  @endpoint_url
end

#no_cloudObject

Returns the value of attribute no_cloud.



13
14
15
# File 'lib/confman/api.rb', line 13

def no_cloud
  @no_cloud
end

#secretObject

Returns the value of attribute secret.



12
13
14
# File 'lib/confman/api.rb', line 12

def secret
  @secret
end

Instance Method Details

#aws_metadata(fields = %w( ami-id availability-zone instance-id instance-type kernel-id local-hostname mac public-hostname))) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/confman/api.rb', line 115

def (fields = %w(
    ami-id availability-zone
    instance-id instance-type
    kernel-id local-hostname
    mac public-hostname))

  meta = {}
  begin
    fields.each do |key|
      Timeout::timeout(1) do
        begin
          meta[key] = RestClient.get("http://169.254.169.254/latest/meta-data/#{key}").strip
        rescue RestClient::ResourceNotFound => ex
          Confman.logger.info("aws_metadata:#{key} #{ex}")
        end
      end
    end
  rescue => ex
    Confman.logger.info("aws_metadata:#{ex}")
  end
  meta['type'] = 'aws' if meta.size > 0
  return meta
end

#cloud_metadataObject



109
110
111
112
113
# File 'lib/confman/api.rb', line 109

def 
  return nil if no_cloud
   ||= 
  
end

#conf_sets(query = {}) ⇒ Object



69
70
71
72
# File 'lib/confman/api.rb', line 69

def conf_sets(query = {})
  results, response = request(:get, "confman/sets", query)
  response
end

#config_pathObject



30
31
32
# File 'lib/confman/api.rb', line 30

def config_path
  File.join(config_dir, "config.json")
end

#find_by_name(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/confman/api.rb', line 46

def find_by_name(name)
  begin
    results = search(name: name)
    raise RestClient::ResourceNotFound if results[:count] == 0
    conf_set_hash = results[:results].first
    DataStore.write(@api_key, name, conf_set_hash.to_json)
    ConfSet.new(self, conf_set_hash)
  rescue => ex
    Confman.logger.error("find_by_name(#{name}): #{ex}")
    Confman.logger.error(ex.backtrace[0..5].join("\n"))
    if conf_set_str = DataStore.read(@api_key, name)
      ConfSet.new(self, JSON.parse(conf_set_str))
    else
      raise ex
    end
  end
end

#load_config(path = config_path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/confman/api.rb', line 34

def load_config(path = config_path)
  if File.exists?(path)
    config_hash = JSON.parse IO.read(path)

    config_hash.each do |key, value|
      send("#{key}=", value) if respond_to?("#{key}=")
    end
    return config_hash
  end
  return nil
end

#request(method, path, object) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/confman/api.rb', line 82

def request(method, path, object)
  request_args = [{ accept: :json, content_type: :json }]

  if method == :get
    request_args.first[:params] = object
  else
    request_args.unshift(object.to_json)
  end

  response = resource[path].send(method, *request_args)
  results = JSON.parse(response.to_s) if response.to_s.length > 0
  results.symbolize_keys! if results.kind_of?(Hash)
  return results, response
end

#reset_server_key!Object



74
75
76
77
78
79
80
# File 'lib/confman/api.rb', line 74

def reset_server_key!
  results, response = Confman.api.request(:get, "computing_resources/key", {})
  File.open("#{config_dir}/.asym.key", "w") do |fh|
    fh.write results[:data]
  end
  File.chmod(0644, "#{config_dir}/.asym.key")
end

#resourceObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/confman/api.rb', line 97

def resource
  timestamp = Time.now.utc.to_i
  secret_hash = Digest::MD5.hexdigest("#{secret}:#{timestamp}")

  @resource ||= RestClient::Resource.new(endpoint_url, :headers => {
    :cloud_meta => Base64.encode64(.to_json).gsub(/\s+/, "")
  })
  @resource.options[:user] = api_key
  @resource.options[:password] = "#{secret_hash}:#{timestamp}"
  @resource
end

#search(query = {}) ⇒ Object



64
65
66
67
# File 'lib/confman/api.rb', line 64

def search(query = {})
  results, response = request(:get, "confman/sets/search", query)
  results
end