incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962199 - in /incubator/deltacloud/trunk/server: bin/ libexec/ libexec/views/images/ libexec/views/instance_states/ libexec/views/instances/ libexec/views/storage_snapshots/ libexec/views/storage_volumes/
Date Thu, 08 Jul 2010 23:36:05 GMT
Author: lutter
Date: Thu Jul  8 23:36:04 2010
New Revision: 962199

URL: http://svn.apache.org/viewvc?rev=962199&view=rev
Log:
Sinatra scaffold

Added:
    incubator/deltacloud/trunk/server/libexec/config.ru
    incubator/deltacloud/trunk/server/libexec/server.rb
    incubator/deltacloud/trunk/server/libexec/views/instances/new.html.haml
Removed:
    incubator/deltacloud/trunk/server/libexec/views/instances/new.html.erb
Modified:
    incubator/deltacloud/trunk/server/bin/deltacloudd
    incubator/deltacloud/trunk/server/libexec/views/images/show.html.haml
    incubator/deltacloud/trunk/server/libexec/views/instance_states/show.gv.erb
    incubator/deltacloud/trunk/server/libexec/views/instance_states/show.html.haml
    incubator/deltacloud/trunk/server/libexec/views/instances/show.html.haml
    incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/index.html.haml
    incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/show.html.haml
    incubator/deltacloud/trunk/server/libexec/views/storage_volumes/index.html.haml
    incubator/deltacloud/trunk/server/libexec/views/storage_volumes/show.html.haml

Modified: incubator/deltacloud/trunk/server/bin/deltacloudd
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/bin/deltacloudd?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/bin/deltacloudd (original)
+++ incubator/deltacloud/trunk/server/bin/deltacloudd Thu Jul  8 23:36:04 2010
@@ -1,12 +1,67 @@
-#!/usr/bin/env ruby
+#!/bin/bash
 
-if ( ARGV.size != 1 )
-  puts "Usage: deltacloudd <drivername>"
+usage()
+{
+cat << EOF
+usage: $0 options
+
+Deltacloud API
+
+OPTIONS:
+   -h                Show this message
+   -i <driver>       Use driver
+   -r <hostname>     Server hostname (default: localhost)
+   -p <port>         Server port (default: 3000)
+EOF
+}
+
+[ "$API_PORT" == "" ] && API_PORT='3000'
+[ "$API_HOST" == "" ] && API_HOST='localhost'
+
+API_DRIVER=
+
+while getopts "hr:p:i:" OPTION
+do
+     case $OPTION in
+         h)
+             usage
+             exit 1
+             ;;
+         i)
+             API_DRIVER=$OPTARG
+             ;;
+         r)
+             API_HOST=$OPTARG
+             ;;
+         p)
+             API_PORT=$OPTARG
+             ;;
+         ?)
+             usage
+             exit
+             ;;
+     esac
+done
+
+if [[ -z $API_DRIVER ]]
+then
+  echo "empty"
+  usage
   exit 1
-end
+fi
+
+topdir=$(cd $(dirname $0)/.. && pwd)
+rerun=$(which rerun 2>/dev/null)
+
+if [ -z "$rerun" ]; then
+  echo "Can not find 'rerun' (gem install rerun)"
+  exit 1
+fi
+
+export API_DRIVER
+export API_HOST
+export API_PORT
 
-DRIVER=ARGV[0].to_sym
+echo "Starting Deltacloud API :: '$API_DRIVER' driver :: http://$API_HOST:$API_PORT/api"
 
-puts "Booting with driver #{DRIVER}"
-require File.dirname(__FILE__) + '/../libexec/config/boot'
-require 'commands/server'
+rerun "thin start --debug --port=$API_PORT --address=$API_HOST -c '$topdir/libexec' -R config.ru"

Added: incubator/deltacloud/trunk/server/libexec/config.ru
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/config.ru?rev=962199&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/config.ru (added)
+++ incubator/deltacloud/trunk/server/libexec/config.ru Thu Jul  8 23:36:04 2010
@@ -0,0 +1,13 @@
+require 'sinatra'
+require 'rubygems'
+
+set :environment => :production
+set :raise_errors => false
+set :run => false
+
+#log = File.new("log/deltacloud.log", "a+")
+#STDOUT.reopen(log)
+#STDERR.reopen(log)
+
+require 'server.rb'
+run Sinatra::Application

Added: incubator/deltacloud/trunk/server/libexec/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/server.rb?rev=962199&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/server.rb (added)
+++ incubator/deltacloud/trunk/server/libexec/server.rb Thu Jul  8 23:36:04 2010
@@ -0,0 +1,311 @@
+require 'rubygems'
+require 'deltacloud'
+require 'sinatra'
+require 'sinatra/respond_to'
+require 'erb'
+require 'haml'
+require 'open3'
+require 'builder'
+require 'drivers'
+require 'sinatra/static_assets'
+require 'sinatra/rabbit'
+require 'sinatra/validation'
+require 'sinatra/lazy_auth'
+
+DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock
+
+# You could use $API_HOST environment variable to change your hostname to
+# whatever you want (eg. if you running API behind NAT)
+HOSTNAME=ENV['API_HOST'] ? ENV['API_HOST'] : nil
+
+Rack::Mime::MIME_TYPES.merge!({ ".gv" => "text/plain" })
+
+module Sinatra
+  register Sinatra::RespondTo
+end
+
+# Common actions
+#
+
+def filter_all(model)
+    filter = {}
+    filter.merge!(:id => params[:id]) if params[:id]
+    filter.merge!(:architecture => params[:architecture]) if params[:architecture]
+    filter.merge!(:owner_id => params[:owner_id]) if params[:owner_id]
+    filter = nil if filter.keys.size.eql?(0)
+    safely do
+      singular = model.to_s.singularize.to_sym
+      @elements = driver.send(model.to_sym, credentials, filter)
+      instance_variable_set(:"@#{model}", @elements)
+      respond_to do |format|
+        format.xml  { return convert_to_xml(singular, @elements) }
+        format.html { haml :"#{model}/index" }
+      end
+    end
+end
+
+def show(model)
+  safely do
+    @element = driver.send(model, credentials, { :id => params[:id]} )
+    instance_variable_set("@#{model}", @element)
+    respond_to do |format|
+      format.xml { return convert_to_xml(model, @element) }
+      format.html { haml :"#{model.to_s.pluralize}/show" }
+    end
+  end
+end
+
+# Redirect to /api
+get '/' do redirect '/api'; end
+
+get '/api\/?' do
+    @version = 1.0
+    respond_to do |format|
+        format.html { haml :"api/show" }
+        format.xml { haml :"api/show" }
+    end
+end
+
+# Rabbit DSL
+
+collection :flavors do
+  description "Flavors are yummy"
+
+  operation :index do
+    description 'This action will do...'
+    param :architecture,  :string,  :optional,  [ 'i386', 'x86_64' ]
+    param :id,            :string,  :optional
+    control { filter_all(:flavors) }
+  end
+
+  operation :show do
+    description "Show a flavor"
+    param :id,           :string, :required
+    control { show(:flavor) }
+  end
+
+end
+
+collection :realms do
+  description "Realms are usefull"
+
+  operation :index do
+    description "List all realms"
+    param :id,            :string
+    param :architecture,  :string,  :optional,  [ 'i386', 'x86_64' ]
+    control { filter_all(:realms) }
+  end
+
+  #FIXME: It always shows whole list
+  operation :show do
+    description "Show a realm"
+    param :id,           :string, :required
+    control { show(:realm) }
+  end
+
+end
+
+collection :images do
+  description "Operating system Images"
+
+  operation :index do
+    description "List of all deployable images"
+    param :id,            :string
+    param :owner_id,      :string
+    param :architecture,  :string,  :optional,  [ 'i386', 'x86_64' ]
+    control { filter_all(:images) }
+  end
+
+  operation :show do
+    description "Show an image"
+    param :id,           :string, :required
+    control { show(:image) }
+  end
+
+end
+
+collection :instance_states do
+  description "The possible states of an instance, and how to traverse between them "
+
+  operation :index do
+    control do
+      @machine = driver.instance_state_machine
+      respond_to do |format|
+        format.xml { haml :'instance_states/show', :layout => false }
+        format.html { haml :'instance_states/show'}
+        format.gv { erb :"instance_states/show" }
+        format.png do
+          # Trick respond_to into looking up the right template for the
+          # graphviz file
+          format(:gv); gv = erb :"instance_states/show"; format(:png)
+          png =  ''
+          cmd = 'dot -Kdot -Gpad="0.2,0.2" -Gsize="5.0,8.0" -Gdpi="180" -Tpng'
+          Open3.popen3( cmd ) do |stdin, stdout, stderr|
+            stdin.write( gv )
+            stdin.close()
+            png = stdout.read
+          end
+          png
+        end
+      end
+    end
+  end
+end
+
+# Special instance get operations that we only allow for HTML
+get "/api/instances/:id/:action" do
+   meth = :"#{params[:action]}_instance"
+   not_found unless driver.respond_to?(meth)
+   respond_to do |format|
+     format.html do
+       driver.send(meth, credentials, params[:id])
+       if params[:action] == 'destroy'
+         redirect instances_url
+       else
+         redirect instance_url(params[:id])
+       end
+     end
+   end
+end
+
+get "/api/instances/new" do
+  @instance = Instance.new( { :id=>params[:id], :image_id=>params[:image_id] } )
+  @image   = driver.image( credentials, :id => params[:image_id] )
+  @flavors = driver.flavors( credentials, { :architecture=>@image.architecture } )
+  @realms = driver.realms(credentials)
+  respond_to do |format|
+    format.html { haml :"instances/new" }
+  end
+end
+
+def instance_action(name)
+  safely do
+    @instance = driver.send(:"#{name}_instance", credentials, params[:id])
+    respond_to do |format|
+      format.xml { return convert_to_xml(:instance, @instance) }
+      format.html { haml :"instances/show" }
+    end
+  end
+end
+
+collection :instances do
+  description "Instances description will go here"
+
+  operation :index do
+    description "List of instances"
+    param :id,            :string
+    control { filter_all(:instances) }
+  end
+
+  operation :show do
+    description "Show an instance"
+    param :id,           :string, :required
+    control { show(:instance) }
+  end
+
+  operation :create do
+    description "Create a new instance"
+    param :image_id,     :string, :required
+    param :realm_id,     :string, :optional
+    param :flavor_id,    :string, :optional
+    # FIXME: name is really a driver-specific feature
+    param :name,         :string, :optional
+    control do
+      @image = driver.image(credentials, :id => params[:image_id])
+      instance = driver.create_instance(credentials, @image.id, params)
+      respond_to do |format|
+        format.html { redirect instance_url(instance.id) }
+        format.xml do
+          response.status = 201  # Created
+          response['Location'] = instance_url(instance.id)
+          convert_to_xml(:instance, instance)
+        end
+      end
+    end
+  end
+
+  operation :reboot, :method => :post, :member => true do
+    description "Reboot running instance"
+    param :id,           :string, :required
+    control { instance_action(:reboot) }
+  end
+
+  operation :start, :method => :post, :member => true do
+    description "Start an instance"
+    param :id,           :string, :required
+    control { instance_action(:start) }
+  end
+
+  operation :stop, :method => :post, :member => true do
+    description "Stop running instance"
+    param :id,           :string, :required
+    control { instance_action(:stop) }
+  end
+
+  operation :destroy do
+    description "Destroy instance"
+    param :id,           :string, :required
+    control { instance_action(:destroy) }
+  end
+end
+
+collection :hardware_profiles do
+  description "Hardware profiles"
+
+  operation :index do
+    description "List of available hardware profiles"
+    param :id,          :string
+    control do
+        @profiles = driver.hardware_profiles
+        respond_to do |format|
+          format.xml  { convert_to_xml(:hardware_profiles, @profiles) }
+          format.html  { haml :'hardware_profiles/index' }
+        end
+    end
+  end
+
+  operation :show do
+    description "Show specific hardware profile"
+    param :id,          :string,    :required
+    control do
+      @profile =  driver.hardware_profile(params[:id])
+      respond_to do |format|
+        format.xml { haml :'hardware_profiles/show', :layout => false }
+        format.html { haml :'hardware_profiles/show' }
+      end
+    end
+  end
+
+end
+
+collection :storage_snapshots do
+  description "Storage snapshots description here"
+
+  operation :index do
+    description "Listing of storage snapshots"
+    param :id,            :string
+    control { filter_all(:storage_snapshots) }
+  end
+
+  operation :show do
+    description "Show storage snapshot"
+    param :id,          :string,    :required
+    control { show(:storage_snapshot) }
+  end
+end
+
+collection :storage_volumes do
+  description "Storage volumes description here"
+
+  operation :index do
+    description "Listing of storage volumes"
+    param :id,            :string
+    control { filter_all(:storage_volumes) }
+  end
+
+  operation :show do
+    description "Show storage volume"
+    param :id,          :string,    :required
+    control { show(:storage_volume) }
+  end
+end

Modified: incubator/deltacloud/trunk/server/libexec/views/images/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/images/show.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/images/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/images/show.html.haml Thu Jul  8 23:36:04
2010
@@ -18,5 +18,4 @@
       = @image.description
     %dt
     %dd
-      = link_to 'Launch', new_instance_url( :image_id => @image.id )
-
+      = link_to 'Launch', "/api/instances/new?image_id=#{@image.id}"

Modified: incubator/deltacloud/trunk/server/libexec/views/instance_states/show.gv.erb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/instance_states/show.gv.erb?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/instance_states/show.gv.erb (original)
+++ incubator/deltacloud/trunk/server/libexec/views/instance_states/show.gv.erb Thu Jul  8
23:36:04 2010
@@ -1,7 +1,7 @@
 digraph instance_states {
   rankdir=LR;
   <% for state in @machine.states do %>
-    <%= state -%> 
+    <%= state %>
       [
         label="<%= state.to_s.titlecase %>",
         labelloc="c",
@@ -25,8 +25,8 @@ digraph instance_states {
 
   <% for state in @machine.states do %>
     <% for transition in state.transitions do %>
-      <%= state %> -> <%= transition.destination %> 
-        [ 
+      <%= state %> -> <%= transition.destination %>
+        [
           label="<%= transition.automatically? ? 'auto' : transition.action %>",
           fontsize="14.0",
           penwidth="2.0",

Modified: incubator/deltacloud/trunk/server/libexec/views/instance_states/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/instance_states/show.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/instance_states/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/instance_states/show.html.haml Thu Jul
 8 23:36:04 2010
@@ -1,7 +1,7 @@
 %h1 Instance States
 
 %div#state_graph
-  = image_tag url_for( :format => :png )
+  = image_tag url_for( '/api/instance_states.png' )
 
 %table.states
   %thead

Added: incubator/deltacloud/trunk/server/libexec/views/instances/new.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/instances/new.html.haml?rev=962199&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/instances/new.html.haml (added)
+++ incubator/deltacloud/trunk/server/libexec/views/instances/new.html.haml Thu Jul  8 23:36:04
2010
@@ -0,0 +1,31 @@
+%h1 New Instance
+
+%h2= @image.id
+%h3= @image.description
+
+%form{ :action => instances_url, :method => :post }
+  %input{ :name => :image_id, :type => :hidden, :value => @instance.image_id }/
+  %label
+    Instance Name:
+  %input{ :name => 'name', :size => 30 }/
+  - if !@flavors.empty?
+    %h3 What flavor of machine?
+    - for flavor in @flavors
+      .radio-group
+        %label{ :for => "flavor_id_#{flavor.id}" }
+          %input{ :type => :radio, :name => 'flavor_id', :value => flavor.id }/
+          = flavor.id
+          %br/
+          %span.radio-group-details
+            = "#{flavor.architecture}, #{flavor.memory} GB, #{flavor.storage} GB"
+  - if !@realms.empty?
+    %h3 Where do you want it?
+    - for realm in @realms
+      .radio-group
+        %label{ :for => "realm_id_#{realm.id}" }
+          %input{ :type => :radio, :name => 'realm_id', :value => realm.id }/
+          = realm.id
+          %br/
+          %span.radio-group-details
+            = "#{realm.name}, #{realm.limit}"
+  %input{ :type => :submit, :name => "commit", :value => "create" }/

Modified: incubator/deltacloud/trunk/server/libexec/views/instances/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/instances/show.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/instances/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/instances/show.html.haml Thu Jul  8 23:36:04
2010
@@ -37,5 +37,4 @@
   %di
     %dt
     %dd
-      = @instance.actions.collect { |action| link_to(action, send("#{action}_instance_url",
@instance.id, :method => :post))}.join("&nbsp;")
-
+      = @instance.actions.collect { |action| link_to(action, "/api/instances/#{@instance.id}/#{action}")
}.join("&nbsp;")

Modified: incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/index.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/index.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/index.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/index.html.haml Thu
Jul  8 23:36:04 2010
@@ -8,7 +8,7 @@
       %th Created
       %th State
   %tbody
-    - @snapshots.each do |snapshot|
+    - @storage_snapshots.each do |snapshot|
       %tr
         %td
           = link_to snapshot.id, storage_snapshot_url( snapshot.id )

Modified: incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/show.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/storage_snapshots/show.html.haml Thu Jul
 8 23:36:04 2010
@@ -1,14 +1,14 @@
 %h1
-  = @snapshot.id
+  = @storage_snapshot.id
 
 %dl
   %di
     %dt Volume
     %dd
-      = @snapshot.storage_volume_id
+      = @storage_snapshot.storage_volume_id
     %dt Created
     %dd
-      = @snapshot.created
+      = @storage_snapshot.created
     %dt State
     %dd
-      = @snapshot.state
+      = @storage_snapshot.state

Modified: incubator/deltacloud/trunk/server/libexec/views/storage_volumes/index.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/storage_volumes/index.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/storage_volumes/index.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/storage_volumes/index.html.haml Thu Jul
 8 23:36:04 2010
@@ -8,7 +8,7 @@
       %th Capacity
       %th Status
   %tbody
-    - @volumes.each do |volume|
+    - @storage_volumes.each do |volume|
       %tr
         %td
           = link_to volume.id, storage_volume_url( volume.id )

Modified: incubator/deltacloud/trunk/server/libexec/views/storage_volumes/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/storage_volumes/show.html.haml?rev=962199&r1=962198&r2=962199&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/storage_volumes/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/storage_volumes/show.html.haml Thu Jul
 8 23:36:04 2010
@@ -1,20 +1,20 @@
 %h1
-  = @volume.id
+  = @storage_volume.id
 
 %dl
   %di
     %dt Created
     %dd
-      = @volume.created
+      = @storage_volume.created
   %di
     %dt Capacity
     %dd
-      = "#{@volume.capacity} GB"
+      = "#{@storage_volume.capacity} GB"
   %di
     %dt State
     %dd
-      = @volume.state
+      = @storage_volume.state
   %di
     %dt Attached
     %dd
-      = link_to(@volume.instance_id, instance_url( @volume.instance_id)) + ' - ' + @volume.device.to_s
if @volume.instance_id
+      = link_to(@storage_volume.instance_id, instance_url( @storage_volume.instance_id))
+ ' - ' + @storage_volume.device.to_s if @storage_volume.instance_id



Mime
View raw message