Revision e3b9eb63 src/cloud/common/CloudServer.rb

View differences:

src/cloud/common/CloudServer.rb
31 31
    attr_reader :one_client
32 32

  
33 33
    # Initializes the Cloud server based on a config file
34
    # config_file:: _String_ for the server. MUST include the following 
34
    # config_file:: _String_ for the server. MUST include the following
35 35
    # variables:
36 36
    #   USER
37 37
    #   PASSWORD
......
43 43
        # --- Load the Cloud Server configuration file ---
44 44

  
45 45
        @config = Configuration.new(config_file)
46
        
46

  
47 47
        @instance_types = Hash.new
48 48

  
49 49
        if @config[:vm_type].kind_of?(Array)
......
55 55
        end
56 56

  
57 57
        # --- Start an OpenNebula Session ---
58
        
58

  
59 59
        @one_client = Client.new()
60 60
        @user_pool  = UserPool.new(@one_client)
61 61
    end
......
72 72
        puts "--------------------------------------"
73 73
        puts "      Registered Instance Types       "
74 74
        puts "--------------------------------------"
75
        pp @instance_types 
75
        pp @instance_types
76 76
    end
77 77

  
78 78
    ###########################################################################
......
80 80
    ###########################################################################
81 81

  
82 82
    # Generates an OpenNebula Session for the given user
83
    # user:: _Hash_ the user information  
84
    # [return] an OpenNebula client session 
83
    # user:: _Hash_ the user information
84
    # [return] an OpenNebula client session
85 85
    def one_client_user(user)
86 86
        client = Client.new("dummy:dummy")
87 87
        client.one_auth = "#{user[:name]}:#{user[:password]}"
88
    
88

  
89 89
        return client
90 90
    end
91 91

  
92 92
    # Authenticates a user
93 93
    # name:: _String_ of the user
94 94
    # password:: _String_ of the user
95
    # [return] true if authenticated    
95
    # [return] true if authenticated
96 96
    def authenticate?(name, password)
97 97
        user = get_user(name)
98 98

  
......
104 104
    # [return] _Hash_ with the user data
105 105
    def get_user(name)
106 106
        user = nil
107
    
107

  
108 108
        @user_pool.info
109 109
        @user_pool.each{ |u|
110 110
            if u.name==name
......
117 117
        }
118 118
        return user
119 119
   end
120
   
121
   def xml_to_hash(xml)
122
       begin
123
           hash = Crack::XML.parse(xml)
124
       rescue Exception => e
125
           error = OpenNebula::Error.new(e.message)
126
           return error
127
       end    
128
       
129
       return hash
130
   end
131
   
120

  
132 121
   def get_template_path(instance_type_name)
133
       if instance_type_name.nil?
134
           instance_type=@instance_types.first
135
       end
136
       
122

  
137 123
       instance_type=@instance_types[instance_type_name]
138
       
124

  
139 125
       if !instance_type
140 126
           error = OpenNebula::Error.new("Bad instance type")
141
           return error    
127
           return error
142 128
       end
143
       
144
       return @config[:template_location]+"/#{instance_type['TEMPLATE']}" 
129

  
130
       return @config[:template_location]+"/#{instance_type['TEMPLATE']}"
145 131
   end
146 132

  
147 133
    ###########################################################################
......
162 148
                error = OpenNebula::Error.new(error_msg)
163 149
                return error
164 150
            end
165
        
151

  
166 152
            if !File.exists?(file_path)
167 153
                error_msg = "Image file could not be found, aborting."
168 154
                error = OpenNebula::Error.new(error_msg)
169 155
                return error
170 156
            end
171 157
        end
172
            
158

  
173 159
        template = image.to_one_template
174 160

  
175 161
        rc = image.allocate(template)
......
181 167
        image.info
182 168
        template=image.to_hash
183 169
        template=template['IMAGE']['TEMPLATE']
184
        
170

  
185 171
        if file_path
186 172
            rc = image.copy(file_path, image['SOURCE'])
187 173
            file[:tempfile].unlink
188 174
        elsif template['SIZE'] and template['FSTYPE']
189 175
            rc = image.mk_datablock(
190
                    template['SIZE'], 
191
                    template['FSTYPE'], 
176
                    template['SIZE'],
177
                    template['FSTYPE'],
192 178
                    image['SOURCE'])
193 179
        end
194
        
180

  
195 181
        if OpenNebula.is_error?(rc)
196 182
           image.delete
197 183
           return rc
198 184
        end
199
 
185

  
200 186
        return nil
201 187
    end
202 188

  

Also available in: Unified diff