Statistics
| Branch: | Tag: | Revision:

one / src / image_mad / one_image.rb @ 0722f4c9

History | View | Annotate | Download (4.15 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- */
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may    */
6
# not use this file except in compliance with the License. You may obtain    */
7
# a copy of the License at                                                   */
8
#                                                                            */
9
# http://www.apache.org/licenses/LICENSE-2.0                                 */
10
#                                                                            */
11
# Unless required by applicable law or agreed to in writing, software        */
12
# distributed under the License is distributed on an "AS IS" BASIS,          */
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
14
# See the License for the specific language governing permissions and        */
15
# limitations under the License.                                             */
16
# -------------------------------------------------------------------------- */
17

    
18
# ---------------------------------------------------------------------------- 
19
# Set up the environment for the driver                                        
20
# ---------------------------------------------------------------------------- 
21

    
22
ONE_LOCATION = ENV["ONE_LOCATION"]
23

    
24
if !ONE_LOCATION
25
    RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
26
    VAR_LOCATION      = "/var/lib/one"
27
else
28
    RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
29
    VAR_LOCATION      = ONE_LOCATION + "/var"
30
end
31

    
32
$: << RUBY_LIB_LOCATION
33

    
34
require "OpenNebulaDriver"
35
require 'getoptlong'
36

    
37
# This class provides basic messaging and logging functionality
38
# to implement Image Repository Drivers. A image repository driver 
39
# is a program (or a set of) that  specialize the OpenNebula behavior
40
# by interfacing with specific infrastructure storage solutions.
41
class ImageDriver < OpenNebulaDriver
42

    
43
    # -------------------------------------------------------------------------
44
    # Image Driver Protocol constants
45
    # -------------------------------------------------------------------------
46
    ACTION = {
47
        :mv   => "MV",
48
        :cp   => "CP",
49
        :rm   => "RM",
50
        :mkfs => "MKFS",
51
        :log  => "LOG"
52
    }
53

    
54
    # -------------------------------------------------------------------------
55
    # Register default actions for the protocol
56
    # -------------------------------------------------------------------------
57
    def initialize(fs_type, concurrency=10, threaded=true)
58
        super(concurrency,threaded,0)
59

    
60
        @actions_path = "#{VAR_LOCATION}/remotes/image/#{fs_type}"
61

    
62
        register_action(ACTION[:mv].to_sym, method("mv"))
63
        register_action(ACTION[:cp].to_sym, method("cp"))
64
        register_action(ACTION[:rm].to_sym, method("rm"))
65
        register_action(ACTION[:mkfs].to_sym, method("mkfs"))
66
    end
67

    
68
    # -------------------------------------------------------------------------
69
    # Image Manager Protocol Actions (generic implementation
70
    # -------------------------------------------------------------------------
71
    def mv(id, src, dst)
72
        local_action("#{@actions_path}/mv #{src} #{dst}",id,ACTION[:mv])
73
    end
74

    
75
    def cp(id, src, dst)
76
        local_action("#{@actions_path}/cp #{src} #{dst}",id,ACTION[:cp])
77
    end
78

    
79
    def rm(id, dst)
80
        local_action("#{@actions_path}/rm #{dst}",id,ACTION[:rm])
81
    end
82

    
83
    def mkfs(id, dst, fs, size)
84
        local_action("#{@actions_path}/mkfs #{dst} #{fs} #{size}",id,ACTION[:mkfs])
85
    end
86
end
87

    
88
# ---------------------------------------------------------------------------- #
89
# ImageDriver Main program
90
# ---------------------------------------------------------------------------- #
91
opts = GetoptLong.new(
92
    [ '--threads',    '-t', GetoptLong::OPTIONAL_ARGUMENT ]
93
)
94

    
95
fs_type    = ''
96
threads    = 15
97

    
98
begin
99
    opts.each do |opt, arg|
100
        case opt
101
            when '--threads'
102
                threads   = arg.to_i
103
        end
104
    end
105
rescue Exception => e
106
    exit(-1)
107
end 
108

    
109
if ARGV.length >= 1 
110
    fs_type = ARGV.shift
111
else
112
    exit(-1)
113
end
114

    
115
image_driver = ImageDriver.new(fs_type, threads)
116
image_driver.start_driver