You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2013/02/08 09:16:21 UTC

[PATCH core 1/4] Core: Added support for migrations to database

From: Michal Fojtik <mf...@redhat.com>

Currently everytime we add or remove attributes from
DB, users need to rerun the 'rake' task to recreate the
database.

This patch will make possible to update the DB schema
without executing the rake task.

The 'migrations' extension in Sequel will check the 'schema_version'
table and apply all migrations >= than the current schema.

Migrations must be prefixed with the schema number in chronological
order and they are stored in server/lib/db/migrations.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/db.rb                                   | 18 +++++++++++++++++
 .../migrations/1_add_realm_to_machine_template.rb  | 23 ++++++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 server/lib/db/migrations/1_add_realm_to_machine_template.rb

diff --git a/server/lib/db.rb b/server/lib/db.rb
index 694450d..33217a3 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -1,3 +1,18 @@
+# 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
 
   def self.test_environment?
@@ -8,6 +23,7 @@ module Deltacloud
     require 'sequel'
     require 'logger'
     Sequel::Model.plugin :validation_class_methods
+    Sequel.extension :migration
   end
 
   if RUBY_PLATFORM == 'java'
@@ -77,6 +93,8 @@ module Deltacloud
       column :volume_config, :string
       column :volume_image, :string
     }
+
+    Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), 'db', 'migrations'))
   end
 
 end
diff --git a/server/lib/db/migrations/1_add_realm_to_machine_template.rb b/server/lib/db/migrations/1_add_realm_to_machine_template.rb
new file mode 100644
index 0000000..8261efc
--- /dev/null
+++ b/server/lib/db/migrations/1_add_realm_to_machine_template.rb
@@ -0,0 +1,23 @@
+# 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.
+
+Sequel.migration do
+  up do
+    add_column :entities, :realm, String
+  end
+  down do
+    drop_column :entities, :realm
+  end
+end
-- 
1.8.1


[PATCH core 2/4] CIMI: Add support for 'realm' in MachineTemplate

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>

Clients can now specify the 'realm' attribute when
creating the new MachineTemplate entity. If they then use
this MachineTemplate to create a new Machine, the Machine
will be placed into that realm.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/models/machine.rb          | 4 +++-
 server/lib/cimi/models/machine_template.rb | 6 ++++++
 server/support/cimi/machine_from_temp.json | 2 +-
 server/support/cimi/machine_template.json  | 1 +
 4 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 0e180ce..04fdba4 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -55,10 +55,11 @@ class CIMI::Model::Machine < CIMI::Model::Base
     additional_params={}
     machine_template = json['machineTemplate']
     if !machine_template['href'].nil?
-      template = current_db.machine_templates.first(:id => machine_template['href'].split('/').last)
+      template = current_db.machine_templates_dataset.first(:id => machine_template['href'].split('/').last)
       raise 'Could not find the MachineTemplate' if template.nil?
       hardware_profile_id = template.machine_config.split('/').last
       image_id = template.machine_image.split('/').last
+      json['realm'] = template.realm
     else
       hardware_profile_id = machine_template['machineConfig']["href"].split('/').last
       image_id = machine_template['machineImage']["href"].split('/').last
@@ -92,6 +93,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
       template = current_db.machine_templates_dataset.first(:id => xml['machineTemplate'][0]['href'].split('/').last)
       hardware_profile_id = template.machine_config.split('/').last
       image_id = template.machine_image.split('/').last
+      xml['realm'] = [ template.realm ]
     else
       machine_template = xml['machineTemplate'][0]
       hardware_profile_id = machine_template['machineConfig'].first["href"].split('/').last
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index 5ba9b3e..685fef5 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -21,6 +21,9 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
   href :machine_image
   href :credential
 
+  resource_attr :realm, :required => false,
+    :constraints => lambda { |c| c.driver.realms(c.credentials).map { |r| r.id }}
+
   array :volumes do
     scalar :href
     scalar :protocol
@@ -59,6 +62,7 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
         :description => json['description'],
         :machine_config => json['machineConfig']['href'],
         :machine_image => json['machineImage']['href'],
+        :realm => json['realm'],
         :ent_properties => json['properties'] ? json['properties'].to_json : {}
       )
       from_db(new_template, context)
@@ -71,6 +75,7 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
         :description => xml['description'].first,
         :machine_config => xml['machineConfig'].first['href'],
         :machine_image => xml['machineImage'].first['href'],
+        :realm => xml['realm'].first,
         :ent_properties => xml['property'] ? JSON::dump(xml['property'].inject({}) { |r, p| r[p['key']]=p['content']; r }) : {}
       )
       from_db(new_template, context)
@@ -89,6 +94,7 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
         :description => model.description,
         :machine_config => { :href => model.machine_config },
         :machine_image => { :href => model.machine_image },
+        :realm => model.realm,
         :property => (model.ent_properties ? JSON::parse(model.ent_properties) :  nil),
         :created => Time.parse(model.created_at.to_s).xmlschema,
         :operations => [
diff --git a/server/support/cimi/machine_from_temp.json b/server/support/cimi/machine_from_temp.json
index c7d7ab8..b4cd0d7 100644
--- a/server/support/cimi/machine_from_temp.json
+++ b/server/support/cimi/machine_from_temp.json
@@ -6,5 +6,5 @@
     "foo": "bar",
     "life": "is life"
   },
-  "machineTemplate": { "href": "http://localhost:3001/cimi/machine_templates/8" }
+  "machineTemplate": { "href": "http://localhost:3001/cimi/machine_templates/1" }
 }
diff --git a/server/support/cimi/machine_template.json b/server/support/cimi/machine_template.json
index 3f219dc..edc1294 100644
--- a/server/support/cimi/machine_template.json
+++ b/server/support/cimi/machine_template.json
@@ -4,6 +4,7 @@
   "description": "My very loved machine template",
   "machineConfig": { "href": "http://localhost:3001/cimi/machine_configurations/m1-xlarge" },
   "machineImage": { "href": "http://localhost:3001/cimi/machine_images/img3" },
+  "realm": "us",
   "properties": {
     "foo": "bar",
     "life": "is life"
-- 
1.8.1


[PATCH core 4/4] Core: Don't run migrations when server starts

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>

Users can now run migrations using:

- bin/deltacloud-db-upgrade
- rake db:migrate

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/Rakefile                  | 10 ++++++++++
 server/bin/deltacloud-db-upgrade | 13 +++++++++++++
 server/lib/db.rb                 |  4 +---
 3 files changed, 24 insertions(+), 3 deletions(-)
 create mode 100755 server/bin/deltacloud-db-upgrade

diff --git a/server/Rakefile b/server/Rakefile
index 5494148..5938b9e 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -46,6 +46,16 @@ Gem::PackageTask.new(spec) do |pkg|
   pkg.need_tar = true
 end
 
+namespace :db do
+  desc 'Execute the database migrations'
+  task 'migrate' do
+    ENV['API_VERBOSE'] = 'true'
+    load File.join(File.dirname(__FILE__), 'lib', 'db.rb')
+    db = Deltacloud.database
+    Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), 'db', 'migrations'))
+  end
+end
+
 namespace :mock do
   namespace :fixtures do
     desc "Setup Mock driver fixtures"
diff --git a/server/bin/deltacloud-db-upgrade b/server/bin/deltacloud-db-upgrade
new file mode 100755
index 0000000..2698350
--- /dev/null
+++ b/server/bin/deltacloud-db-upgrade
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+ENV['API_VERBOSE'] = 'true'
+
+load File.join(File.dirname(__FILE__), '..', 'lib', 'db.rb')
+
+# Initialize the database
+#
+db = Deltacloud.initialize_database
+
+# Apply the migrations
+#
+Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), '..', 'db', 'migrations'))
diff --git a/server/lib/db.rb b/server/lib/db.rb
index 33217a3..9e24b7e 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -93,8 +93,6 @@ module Deltacloud
       column :volume_config, :string
       column :volume_image, :string
     }
-
-    Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), 'db', 'migrations'))
+    db
   end
-
 end
-- 
1.8.1


[PATCH core 3/4] Core: Moved migrations to server/db/migrations

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 .../migrations/1_add_realm_to_machine_template.rb  | 23 ++++++++++++++++++++++
 .../migrations/1_add_realm_to_machine_template.rb  | 23 ----------------------
 2 files changed, 23 insertions(+), 23 deletions(-)
 create mode 100644 server/db/migrations/1_add_realm_to_machine_template.rb
 delete mode 100644 server/lib/db/migrations/1_add_realm_to_machine_template.rb

diff --git a/server/db/migrations/1_add_realm_to_machine_template.rb b/server/db/migrations/1_add_realm_to_machine_template.rb
new file mode 100644
index 0000000..8261efc
--- /dev/null
+++ b/server/db/migrations/1_add_realm_to_machine_template.rb
@@ -0,0 +1,23 @@
+# 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.
+
+Sequel.migration do
+  up do
+    add_column :entities, :realm, String
+  end
+  down do
+    drop_column :entities, :realm
+  end
+end
diff --git a/server/lib/db/migrations/1_add_realm_to_machine_template.rb b/server/lib/db/migrations/1_add_realm_to_machine_template.rb
deleted file mode 100644
index 8261efc..0000000
--- a/server/lib/db/migrations/1_add_realm_to_machine_template.rb
+++ /dev/null
@@ -1,23 +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.
-
-Sequel.migration do
-  up do
-    add_column :entities, :realm, String
-  end
-  down do
-    drop_column :entities, :realm
-  end
-end
-- 
1.8.1