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:



80
81
82
# File 'lib/opennebula/template.rb', line 80

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:



157
158
159
160
161
# File 'lib/opennebula/template.rb', line 157

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:



148
149
150
# File 'lib/opennebula/template.rb', line 148

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



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

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



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

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



85
86
87
# File 'lib/opennebula/template.rb', line 85

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

#gidObject

Returns the group identifier

return

Integer the element’s group ID



193
194
195
# File 'lib/opennebula/template.rb', line 193

def gid
    self['GID'].to_i
end

#infoObject Also known as: info!

Retrieves the information of the given Template.



68
69
70
# File 'lib/opennebula/template.rb', line 68

def info()
    super(TEMPLATE_METHODS[:info], 'VMTEMPLATE')
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:



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/opennebula/template.rb', line 100

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



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

def owner_id
    self['UID'].to_i
end

#public?Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
# File 'lib/opennebula/template.rb', line 201

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



126
127
128
# File 'lib/opennebula/template.rb', line 126

def publish
    set_publish(true)
end

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

Renames this Template

Parameters:

  • name (String)

    New name for the Template.

Returns:



183
184
185
# File 'lib/opennebula/template.rb', line 183

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

#unpublishObject

Unplubishes the Image



131
132
133
# File 'lib/opennebula/template.rb', line 131

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:



121
122
123
# File 'lib/opennebula/template.rb', line 121

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