You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by lu...@redhat.com on 2011/09/16 00:19:27 UTC
Simplify code loading/layout
These patches shuffle some files around, and change slightly where things
are required to (a) bring locations of files more in line with common Ruby
conventions and (b) make sure we don't play with the load path in too many
places.
David
[PATCH 1/3] Simplify how the server is loaded
Posted by lu...@redhat.com.
From: David Lutterkort <lu...@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 <lu...@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
[PATCH 3/3] Pull changes to Hash out of blob_stream and into core_ext
Posted by lu...@redhat.com.
From: David Lutterkort <lu...@redhat.com>
Signed-off-by: David Lutterkort <lu...@redhat.com>
---
server/lib/deltacloud/core_ext.rb | 1 +
server/lib/deltacloud/core_ext/hash.rb | 29 ++++++++++++++++++++++++++
server/lib/deltacloud/helpers/blob_stream.rb | 17 ---------------
3 files changed, 30 insertions(+), 17 deletions(-)
create mode 100644 server/lib/deltacloud/core_ext/hash.rb
diff --git a/server/lib/deltacloud/core_ext.rb b/server/lib/deltacloud/core_ext.rb
index 225a82a..43b3b25 100644
--- a/server/lib/deltacloud/core_ext.rb
+++ b/server/lib/deltacloud/core_ext.rb
@@ -16,3 +16,4 @@
require 'deltacloud/core_ext/string'
require 'deltacloud/core_ext/integer'
+require 'deltacloud/core_ext/hash'
diff --git a/server/lib/deltacloud/core_ext/hash.rb b/server/lib/deltacloud/core_ext/hash.rb
new file mode 100644
index 0000000..9cdba50
--- /dev/null
+++ b/server/lib/deltacloud/core_ext/hash.rb
@@ -0,0 +1,29 @@
+# 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.
+
+class Hash
+ def gsub_keys(rgx_pattern, replacement)
+ remove = []
+ self.each_key do |key|
+ if key.to_s.match(rgx_pattern)
+ new_key = key.to_s.gsub(rgx_pattern, replacement).downcase
+ self[new_key] = self[key]
+ remove << key
+ end
+ end
+ #remove the original keys
+ self.delete_if{|k,v| remove.include?(k)}
+ end
+end
diff --git a/server/lib/deltacloud/helpers/blob_stream.rb b/server/lib/deltacloud/helpers/blob_stream.rb
index 796bd2d..93f671c 100644
--- a/server/lib/deltacloud/helpers/blob_stream.rb
+++ b/server/lib/deltacloud/helpers/blob_stream.rb
@@ -63,23 +63,6 @@ rescue LoadError => e
end
end
-class Hash
-
- def gsub_keys(rgx_pattern, replacement)
- remove = []
- self.each_key do |key|
- if key.to_s.match(rgx_pattern)
- new_key = key.to_s.gsub(rgx_pattern, replacement).downcase
- self[new_key] = self[key]
- remove << key
- end
- end
- #remove the original keys
- self.delete_if{|k,v| remove.include?(k)}
- end
-
-end
-
module BlobHelper
def self.extract_blob_metadata_hash(env_hash)
--
1.7.6
[PATCH 2/3] * bin/deltacloudd: there's no reason to change cwd here
Posted by lu...@redhat.com.
From: David Lutterkort <lu...@redhat.com>
Signed-off-by: David Lutterkort <lu...@redhat.com>
---
server/bin/deltacloudd | 5 -----
1 files changed, 0 insertions(+), 5 deletions(-)
diff --git a/server/bin/deltacloudd b/server/bin/deltacloudd
index f59caf3..166fca3 100755
--- a/server/bin/deltacloudd
+++ b/server/bin/deltacloudd
@@ -169,10 +169,6 @@ have_rerun = library_present?('rerun')
unless have_thin
require 'rack'
- # We can't chdir with webrick so add our root directory
- # onto the load path
- $: << dirname
-
# Read in config.ru and convert it to an instance of Rack::Builder
cfgfile = File.read(File.join(dirname, 'config.ru'))
inner_app = eval("Rack::Builder.new {(" + cfgfile + "\n )}.to_app",
@@ -200,7 +196,6 @@ else
argv_opts << ['--address', ENV["API_HOST"] ]
argv_opts << ['--port', ENV["API_PORT"] ]
argv_opts << ['--rackup', 'config.ru' ]
- argv_opts << ['--chdir', dirname ]
argv_opts << ['-e', options[:env] ]
argv_opts << ['--timeout', ENV["API_TIMEOUT"] || '60']
argv_opts << ['--threaded', '-D' ]
--
1.7.6
Re: Simplify code loading/layout
Posted by David Lutterkort <lu...@redhat.com>.
On Fri, 2011-09-16 at 11:27 +0200, Michal Fojtik wrote:
> On Sep 16, 2011, at 12:19 AM, lutter@redhat.com wrote:
>
> >
> > These patches shuffle some files around, and change slightly where things
> > are required to (a) bring locations of files more in line with common Ruby
> > conventions and (b) make sure we don't play with the load path in too many
> > places.
>
> Tested, ACK.
Thanks. Pushed.
David
Re: Simplify code loading/layout
Posted by Michal Fojtik <mf...@redhat.com>.
On Sep 16, 2011, at 12:19 AM, lutter@redhat.com wrote:
>
> These patches shuffle some files around, and change slightly where things
> are required to (a) bring locations of files more in line with common Ruby
> conventions and (b) make sure we don't play with the load path in too many
> places.
Tested, ACK.
-- Michal
>
> David
------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org