Class: OpenNebula::Template

Inherits:
PoolElement show all
Defined in:
lib/opennebula/template.rb

Constant Summary collapse

TEMPLATE_METHODS =
{
    :allocate    => "template.allocate",
    :instantiate => "template.instantiate",
    :info        => "template.info",
    :update      => "template.update",
    :delete      => "template.delete",
    :chown       => "template.chown",
    :chmod       => "template.chmod",
    :clone       => "template.clone",
    :rename      => "template.rename"
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, #name, new_with_id, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml

Constructor Details

#initialize(xml, client) ⇒ Template

Class constructor



57
58
59
60
61
# File 'lib/opennebula/template.rb', line 57

def initialize(xml, client)
    super(xml,client)

    @client = client
end

Class Method Details

.build_xml(pe_id = nil) ⇒ Object

Creates a Template description with just its identifier this method should be used to create plain Template objects. id the id of the user

Example:

template = Template.new(Template.build_xml(3),rpc_client)


46
47
48
49
50
51
52
53
54
# File 'lib/opennebula/template.rb', line 46

def Template.build_xml(pe_id=nil)
    if pe_id
        obj_xml = "<VMTEMPLATE><ID>#{pe_id}</ID></VMTEMPLATE>"
    else
        obj_xml = "<VMTEMPLATE></VMTEMPLATE>"
    end

    XMLElement.build_xml(obj_xml,'VMTEMPLATE')
end

Instance Method Details

#allocate(description) ⇒ nil, OpenNebula::Error

Allocates a new Template in OpenNebula

Parameters:

  • description (String)

    The contents of the Template.

Returns:



94
95
96
# File 'lib/opennebula/template.rb', line 94

def allocate(description)
    super(TEMPLATE_METHODS[:allocate], description)
end

#chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) ⇒ nil, OpenNebula::Error

Changes the Template permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change

Returns:



171
172
173
174
175
# File 'lib/opennebula/template.rb', line 171

def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
        other_m, other_a)
    super(TEMPLATE_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
        group_m, group_a, other_u, other_m, other_a)
end

#chmod_octet(octet) ⇒ nil, OpenNebula::Error

Changes the Template permissions.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



162
163
164
# File 'lib/opennebula/template.rb', line 162

def chmod_octet(octet)
    super(TEMPLATE_METHODS[:chmod], octet)
end

#chown(uid, gid) ⇒ Object

Changes the owner/group

uid

Integer the new owner id. Set to -1 to leave the current one

gid

Integer the new group id. Set to -1 to leave the current one

return

nil in case of success or an Error object



153
154
155
# File 'lib/opennebula/template.rb', line 153

def chown(uid, gid)
    super(TEMPLATE_METHODS[:chown], uid, gid)
end

#clone(name) ⇒ Integer, OpenNebula::Error

Clones this Template into a new one

Parameters:

  • name (String)

    for the new Template.

Returns:

  • (Integer, OpenNebula::Error)

    The new Template ID in case of success, Error otherwise



183
184
185
186
187
188
189
# File 'lib/opennebula/template.rb', line 183

def clone(name)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(TEMPLATE_METHODS[:clone], @pe_id, name)

    return rc
end

#deleteObject

Deletes the Template



99
100
101
# File 'lib/opennebula/template.rb', line 99

def delete()
    super(TEMPLATE_METHODS[:delete])
end

#gidObject

Returns the group identifier

return

Integer the element’s group ID



207
208
209
# File 'lib/opennebula/template.rb', line 207

def gid
    self['GID'].to_i
end

#info(extended = false) ⇒ Object Also known as: info!

Retrieves the information of the given Template. include extended information, such as the SIZE for each DISK

Parameters:

  • extended (true, false) (defaults to: false)

    optional flag to process the template and



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/opennebula/template.rb', line 70

def info(extended=false)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(TEMPLATE_METHODS[:info], @pe_id, extended)

    if !OpenNebula.is_error?(rc)
        initialize_xml(rc, 'VMTEMPLATE')
        rc   = nil

        @pe_id = self['ID'].to_i if self['ID']
        @name  = self['NAME'] if self['NAME']
    end

    return rc
end

#instantiate(name = "", hold = false, template = "") ⇒ Integer, OpenNebula::Error

Creates a VM instance from a Template

Parameters:

  • name (String) (defaults to: "")

    Name for the VM instance. If it is an empty string OpenNebula will set a default name

  • hold (true, false) (defaults to: false)

    false to create the VM in pending state, true to create it on hold

  • template (String) (defaults to: "")

    User provided Template to merge with the one being instantiated

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/opennebula/template.rb', line 114

def instantiate(name="", hold=false, template="")
    return Error.new('ID not defined') if !@pe_id

    name ||= ""
    hold = false if hold.nil?
    template ||= ""

    rc = @client.call(
        TEMPLATE_METHODS[:instantiate], @pe_id, name, hold, template)

    return rc
end

#owner_idObject



211
212
213
# File 'lib/opennebula/template.rb', line 211

def owner_id
    self['UID'].to_i
end

#public?Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
# File 'lib/opennebula/template.rb', line 215

def public?
    if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
        true
    else
        false
    end
end

#publishObject

Publishes the Template, to be used by other users



140
141
142
# File 'lib/opennebula/template.rb', line 140

def publish
    set_publish(true)
end

#rename(name) ⇒ nil, OpenNebula::Error

Renames this Template

Parameters:

  • name (String)

    New name for the Template.

Returns:



197
198
199
# File 'lib/opennebula/template.rb', line 197

def rename(name)
    return call(TEMPLATE_METHODS[:rename], @pe_id, name)
end

#unpublishObject

Unplubishes the Image



145
146
147
# File 'lib/opennebula/template.rb', line 145

def unpublish
    set_publish(false)
end

#update(new_template, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • new_template (String)

    New template contents

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



135
136
137
# File 'lib/opennebula/template.rb', line 135

def update(new_template, append=false)
    super(TEMPLATE_METHODS[:update], new_template, append ? 1 : 0)
end