Class: OCCI::Server

Inherits:
Sinatra::Application
  • Object
show all
Defined in:
lib/occi/server.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Server) initialize(config = {})



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/occi/server.rb', line 100

def initialize(config = {})
  # create logger
  config[:log_dest] ||= STDOUT
  config[:log_level] ||= Logger::INFO
  config[:log_level] = case OCCI::Server.config[:log_level]
                         when "debug"
                           Logger::DEBUG
                         when "info"
                           Logger::INFO
                         when "warn"
                           Logger::WARN
                         when "error"
                           Logger::ERROR
                         when "fatal"
                           Logger::FATAL
                         else
                           Logger::INFO
                       end

  @logger = Logger.new(config[:log_dest])
  @logger.level = config[:log_level]

  # subscribe to log messages and send to logger
  @log_subscriber = ActiveSupport::Notifications.subscribe("log") do |name, start, finish, id, payload|
    @logger.log(payload[:level], payload[:message])
  end

  # Configuration of HTTP Authentication
  if OCCI::Server.config['username'] != nil and OCCI::Server.config['password'] != nil
    use Rack::Auth::Basic, "Restricted Area" do |username, password|
      [username, password] == [OCCI::Server.config['username'], OCCI::Server.config['password']]
    end
  end

  OCCI::Server.initialize_core_model
  OCCI::Server.initialize_model(OCCI::Server.config['occi_model_path'])

  # set views explicitly
  set :views, File.dirname(__FILE__) + "/../../views"

  super
end

Class Method Details

+ (Object) config

Read configuration file



84
85
86
# File 'lib/occi/server.rb', line 84

def self.config
  @@config ||= OCCI::Configuration.new('etc/occi-server.conf')
end

+ (Object) initialize_core_model




145
146
147
148
149
150
# File 'lib/occi/server.rb', line 145

def self.initialize_core_model
  OCCI::Log.info("### Initializing OCCI Core Model ###")
  OCCI::Core::Entity.register
  OCCI::Core::Resource.register
  OCCI::Core::Link.register
end

+ (Object) initialize_model(path)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/occi/server.rb', line 152

def self.initialize_model(path)
  OCCI::Log.info("### Initializing OCCI Model from #{path} ###")
  Dir.glob(path + '/**/*.json').each do |file|
    collection = Hashie::Mash.new(JSON.parse(File.read(file)))
    # add location of service provider to scheme if it has a relative location
    collection.kinds.collect { |kind| kind.scheme = self.location + kind.scheme if kind.scheme.start_with? '/' } if collection.kinds
    collection.mixins.collect { |mixin| mixin.scheme = self.location + mixin.scheme if mixin.scheme.start_with? '/' } if collection.mixins
    collection.actions.collect { |action| action.scheme = self.location + action.scheme if action.scheme.start_with? '/' } if collection.actions
    # register categories
    collection.kinds.each { |kind| OCCI::Registry.register(OCCI::Core::Kind.new(kind)) } if collection.kinds
    collection.mixins.each { |mixin| OCCI::Registry.register(OCCI::Core::Mixin.new(mixin)) } if collection.mixins
    collection.actions.each { |action| OCCI::Registry.register(OCCI::Core::Action.new(action)) } if collection.actions
  end
end

+ (Object) location



88
89
90
# File 'lib/occi/server.rb', line 88

def self.location
  OCCI::Server.config[:server].chomp('/')
end

+ (Object) port



92
93
94
# File 'lib/occi/server.rb', line 92

def self.port
  OCCI::Server.config[:port]
end

+ (Object) uri



96
97
98
# File 'lib/occi/server.rb', line 96

def self.uri
  self.port.nil? ? self.location : self.location + ':' + self.port
end

Instance Method Details

- (Object) initialize_backend(auth)




168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/occi/server.rb', line 168

def initialize_backend(auth)

  if auth.provided? && auth.basic? && auth.credentials
    user, password = auth.credentials
  else
    user, password = [OCCI::Server.config['one_user'], OCCI::Server.config['one_password']]
    logger.debug("No basic auth data provided: using defaults from config (user = '#{user}')")
  end

  @backend = case OCCI::Server.config["backend"]
               when "opennebula"
                 require 'occi/backend/opennebula/opennebula'
                 OCCI::Server.initialize_model('etc/backend/opennebula')
                 OCCI::Backend::Manager.register_backend(OCCI::Backend::OpenNebula::OpenNebula, OCCI::Backend::OpenNebula::OpenNebula::OPERATIONS)
                 OCCI::Backend::OpenNebula::OpenNebula.new(user, password)
               when "ec2"
                 require 'occi/backend/ec2/ec2'
                 Bundler.require(:ec2)
                 OCCI::Server.initialize_model('etc/backend/ec2')
                 OCCI::Backend::Manager.register_backend(OCCI::Backend::EC2::EC2, OCCI::Backend::EC2::EC2::OPERATIONS)
                 OCCI::Backend::EC2::EC2.new(user, password)
               when "dummy" then
                 require 'occi/backend/dummy'
                 OCCI::Backend::Manager.register_backend(OCCI::Backend::Dummy, OCCI::Backend::Dummy::OPERATIONS)
                 OCCI::Backend::Dummy.new()
               else
                 raise "Backend '" + OCCI::Server.config["backend"] + "' not found"
             end


end