incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@redhat.com
Subject [PATCH 1/3] Simplify how the server is loaded
Date Thu, 15 Sep 2011 22:19:28 GMT
From: David Lutterkort <lutter@redhat.com>

We had requires in all the wrong places, and were changing the load path in
way too many places, too.

This patch makes sure that the way we do requires follows common Ruby style.

The only place in which we touch the load path now is in config.ru

Signed-off-by: David Lutterkort <lutter@redhat.com>
---
 server/config.ru                 |    6 ++-
 server/deltacloud.rb             |   43 ----------------------
 server/lib/deltacloud.rb         |   27 ++++++++++++++
 server/lib/deltacloud/drivers.rb |   72 ++++++++++++++++++++++++++++++++++++++
 server/lib/deltacloud/helpers.rb |    1 +
 server/lib/deltacloud/models.rb  |   30 ++++++++++++++++
 server/lib/drivers.rb            |   71 -------------------------------------
 server/server.rb                 |    2 -
 server/tests/rabbit_test.rb      |    2 +-
 9 files changed, 135 insertions(+), 119 deletions(-)
 delete mode 100644 server/deltacloud.rb
 create mode 100644 server/lib/deltacloud.rb
 create mode 100644 server/lib/deltacloud/drivers.rb
 create mode 100644 server/lib/deltacloud/models.rb
 delete mode 100644 server/lib/drivers.rb

diff --git a/server/config.ru b/server/config.ru
index 8f244ab..08a7ae2 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -16,8 +16,10 @@
 
 require 'rubygems'
 
-$:.unshift File.join(File.dirname(__FILE__), '.')
+top_srcdir = File.dirname(__FILE__)
 
-require 'server.rb'
+$:.unshift File.join(top_srcdir, 'lib')
+
+load File.join(top_srcdir, 'server.rb')
 
 run Sinatra::Application
diff --git a/server/deltacloud.rb b/server/deltacloud.rb
deleted file mode 100644
index 44a5dbb..0000000
--- a/server/deltacloud.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-$:.unshift File.join(File.dirname(__FILE__), 'lib')
-
-require 'drivers'
-
-require 'deltacloud/core_ext'
-
-require 'deltacloud/base_driver'
-require 'deltacloud/hardware_profile'
-require 'deltacloud/state_machine'
-require 'deltacloud/helpers'
-require 'deltacloud/models/base_model'
-require 'deltacloud/models/realm'
-require 'deltacloud/models/image'
-require 'deltacloud/models/instance'
-require 'deltacloud/models/key'
-require 'deltacloud/models/address'
-require 'deltacloud/models/instance_profile'
-require 'deltacloud/models/storage_snapshot'
-require 'deltacloud/models/storage_volume'
-require 'deltacloud/models/bucket'
-require 'deltacloud/models/blob'
-require 'deltacloud/models/load_balancer'
-require 'deltacloud/models/firewall'
-require 'deltacloud/models/firewall_rule'
-
-require 'deltacloud/validation'
-require 'deltacloud/runner'
diff --git a/server/lib/deltacloud.rb b/server/lib/deltacloud.rb
new file mode 100644
index 0000000..6ff547e
--- /dev/null
+++ b/server/lib/deltacloud.rb
@@ -0,0 +1,27 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+require 'deltacloud/drivers'
+
+require 'deltacloud/core_ext'
+
+require 'deltacloud/base_driver'
+require 'deltacloud/hardware_profile'
+require 'deltacloud/state_machine'
+require 'deltacloud/helpers'
+require 'deltacloud/models'
+require 'deltacloud/validation'
+require 'deltacloud/runner'
diff --git a/server/lib/deltacloud/drivers.rb b/server/lib/deltacloud/drivers.rb
new file mode 100644
index 0000000..dfc998d
--- /dev/null
+++ b/server/lib/deltacloud/drivers.rb
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module Deltacloud
+
+  module Drivers
+
+    require 'yaml'
+
+    DEFAULT_COLLECTIONS = [
+      :hardware_profiles,
+      :images,
+      :instances,
+      :instance_states,
+      :realms,
+      :storage_volumes,
+      :storage_snapshots
+    ]
+
+    DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock
+
+    def driver_config
+      if Thread::current[:drivers].nil?
+        Thread::current[:drivers] = {}
+        top_srcdir = File.join(File.dirname(__FILE__), '..', '..')
+        Dir[File.join(top_srcdir, 'config', 'drivers', '*.yaml')].each do |driver_file|
+          Thread::current[:drivers].merge!(YAML::load(File::read(driver_file)))
+        end
+      end
+      Thread::current[:drivers]
+    end
+
+    def driver_symbol
+      (Thread.current[:driver] || DRIVER).to_sym
+    end
+
+    def driver_name
+      driver_config[:"#{driver_symbol}"][:name]
+    end
+
+    def driver_class
+      basename = driver_config[:"#{driver_symbol}"][:class] || "#{driver_name}Driver"
+      Deltacloud::Drivers.const_get(driver_name).const_get(basename)
+    end
+
+    def driver_source_name
+      File.join("deltacloud", "drivers", "#{driver_symbol}", "#{driver_symbol}_driver.rb")
+    end
+
+    def driver_mock_source_name
+      return File.join('deltacloud', 'drivers', "#{driver_symbol}",
+		       "#{driver_symbol}_driver.rb") if driver_name.eql? 'Mock'
+    end
+
+    def driver
+      require driver_source_name
+      @driver ||= driver_class.new
+    end
+  end
+end
diff --git a/server/lib/deltacloud/helpers.rb b/server/lib/deltacloud/helpers.rb
index 6f8c168..cf8531a 100644
--- a/server/lib/deltacloud/helpers.rb
+++ b/server/lib/deltacloud/helpers.rb
@@ -18,5 +18,6 @@ require 'deltacloud/helpers/application_helper'
 require 'deltacloud/helpers/json_helper'
 require 'deltacloud/helpers/conversion_helper'
 require 'deltacloud/helpers/hardware_profiles_helper'
+require 'deltacloud/helpers/blob_stream'
 
 helpers ApplicationHelper, ConversionHelper, HardwareProfilesHelper, JSONHelper
diff --git a/server/lib/deltacloud/models.rb b/server/lib/deltacloud/models.rb
new file mode 100644
index 0000000..8317232
--- /dev/null
+++ b/server/lib/deltacloud/models.rb
@@ -0,0 +1,30 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+require 'deltacloud/models/base_model'
+require 'deltacloud/models/realm'
+require 'deltacloud/models/image'
+require 'deltacloud/models/instance'
+require 'deltacloud/models/key'
+require 'deltacloud/models/address'
+require 'deltacloud/models/instance_profile'
+require 'deltacloud/models/storage_snapshot'
+require 'deltacloud/models/storage_volume'
+require 'deltacloud/models/bucket'
+require 'deltacloud/models/blob'
+require 'deltacloud/models/load_balancer'
+require 'deltacloud/models/firewall'
+require 'deltacloud/models/firewall_rule'
diff --git a/server/lib/drivers.rb b/server/lib/drivers.rb
deleted file mode 100644
index 722e1d7..0000000
--- a/server/lib/drivers.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-module Deltacloud
-
-  module Drivers
-
-    require 'yaml'
-
-    DEFAULT_COLLECTIONS = [
-      :hardware_profiles,
-      :images,
-      :instances,
-      :instance_states,
-      :realms,
-      :storage_volumes,
-      :storage_snapshots
-    ]
-
-    DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock
-
-    def driver_config
-      if Thread::current[:drivers].nil?
-        Thread::current[:drivers] = {}
-        Dir[File.join(File::dirname(__FILE__), '..', 'config', 'drivers', '*.yaml')].each
do |driver_file|
-          Thread::current[:drivers].merge!(YAML::load(File::read(driver_file)))
-        end
-      end
-      Thread::current[:drivers]
-    end
-
-    def driver_symbol
-      (Thread.current[:driver] || DRIVER).to_sym
-    end
-
-    def driver_name
-      driver_config[:"#{driver_symbol}"][:name]
-    end
-
-    def driver_class
-      basename = driver_config[:"#{driver_symbol}"][:class] || "#{driver_name}Driver"
-      Deltacloud::Drivers.const_get(driver_name).const_get(basename)
-    end
-
-    def driver_source_name
-      File.join("deltacloud", "drivers", "#{driver_symbol}", "#{driver_symbol}_driver.rb")
-    end
-
-    def driver_mock_source_name
-      return File.join('deltacloud', 'drivers', "#{driver_symbol}",
-		       "#{driver_symbol}_driver.rb") if driver_name.eql? 'Mock'
-    end
-
-    def driver
-      require driver_source_name
-      @driver ||= driver_class.new
-    end
-  end
-end
diff --git a/server/server.rb b/server/server.rb
index ae8d63d..1f8461e 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -15,7 +15,6 @@
 
 require 'sinatra'
 require 'deltacloud'
-require 'drivers'
 require 'json'
 require 'sinatra/rack_accept'
 require 'sinatra/static_assets'
@@ -25,7 +24,6 @@ require 'erb'
 require 'haml'
 require 'open3'
 require 'sinatra/sinatra_verbose'
-require 'lib/deltacloud/helpers/blob_stream'
 require 'sinatra/rack_driver_select'
 require 'sinatra/rack_runtime'
 require 'sinatra/rack_etag'
diff --git a/server/tests/rabbit_test.rb b/server/tests/rabbit_test.rb
index 339a930..336545f 100644
--- a/server/tests/rabbit_test.rb
+++ b/server/tests/rabbit_test.rb
@@ -17,7 +17,7 @@
 $:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
 require 'tests/common'
 
-require 'drivers'
+require 'deltacloud/drivers'
 require 'deltacloud/drivers/mock/mock_driver'
 
 module DeltacloudUnitTest
-- 
1.7.6


Mime
View raw message