Class: OneGroupHelper

Inherits:
OpenNebulaHelper::OneHelper show all
Defined in:
lib/one_helper/onegroup_helper.rb

Instance Attribute Summary

Attributes inherited from OpenNebulaHelper::OneHelper

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebulaHelper::OneHelper

#check_orphan, client, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, #to_id, to_id_desc, #user_name

Constructor Details

This class inherits a constructor from OpenNebulaHelper::OneHelper

Class Method Details

.conf_fileObject



25
26
27
# File 'lib/one_helper/onegroup_helper.rb', line 25

def self.conf_file
    "onegroup.yaml"
end

.rnameObject



21
22
23
# File 'lib/one_helper/onegroup_helper.rb', line 21

def self.rname
    "GROUP"
end

Instance Method Details

#create_complete_resource(group_hash) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/one_helper/onegroup_helper.rb', line 51

def create_complete_resource(group_hash)
    group = factory
    rc = group.create(group_hash)

    if OpenNebula.is_error?(rc)
        return -1, rc.message
    end

    puts "ID: #{group.id}"
    return 0
end

#create_resource(options, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/one_helper/onegroup_helper.rb', line 29

def create_resource(options, &block)
    group = factory

    rc = block.call(group)

    if OpenNebula.is_error?(rc)
        return -1, rc.message
    else
        puts "ID: #{group.id.to_s}"
    end

    puts "Creating default ACL rules: #{GROUP_DEFAULT_ACLS}" if options[:verbose]

    exit_code , msg = group.create_default_acls

    if OpenNebula.is_error?(exit_code)
        return -1, exit_code.message
    end

    exit_code.to_i
end

#format_pool(options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/one_helper/onegroup_helper.rb', line 63

def format_pool(options)
    config_file = self.class.table_conf

    table = CLIHelper::ShowTable.new(config_file, self) do
        def pool_default_quotas(path)
            @data.dsearch('/GROUP_POOL/DEFAULT_GROUP_QUOTAS/'+path)
        end

        def quotas
            if !defined?(@quotas)
                quotas = @data.dsearch('GROUP_POOL/QUOTAS')
                @quotas = Hash.new

                if (!quotas.nil?)
                    quotas = [quotas].flatten

                    quotas.each do |q|
                        @quotas[q['ID']] = q
                    end
                end
            end
            @quotas
        end

        column :ID, "ONE identifier for the Group", :size=>4 do |d|
            d["ID"]
        end

        column :NAME, "Name of the Group", :left, :size=>29 do |d|
            d["NAME"]
        end

        column :USERS, "Number of Users in this group", :size=>5 do |d|
            ids = d["USERS"]["ID"]

            case ids
            when String
                "1"
            when Array
                ids.size
            else
                "0"
            end
        end

        column :VMS , "Number of VMS", :size=>9 do |d|
            begin
                q = quotas[d['ID']]

                if q['VM_QUOTA']['VM'].nil? && d["ID"].to_i != 0
                    q['VM_QUOTA']['VM'] = {
                        "VMS"                   => OneQuotaHelper::LIMIT_DEFAULT,
                        "VMS_USED"              => "0",
                        "CPU"                   => OneQuotaHelper::LIMIT_DEFAULT,
                        "CPU_USED"              => "0",
                        "MEMORY"                => OneQuotaHelper::LIMIT_DEFAULT,
                        "MEMORY_USED"           => "0",
                        "RUNNING_VMS"           => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_VMS_USED"      => "0",
                        "RUNNING_CPU"           => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_CPU_USED"      => "0",
                        "RUNNING_MEMORY"        => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_MEMORY_USED"   => "0",
                        "SYSTEM_DISK_SIZE"      => OneQuotaHelper::LIMIT_DEFAULT,
                        "SYSTEM_DISK_SIZE_USED" => "0"
                    }

                    quotas[d['ID']] = q
                end

                limit = q['VM_QUOTA']['VM']["VMS"]

                if limit == OneQuotaHelper::LIMIT_DEFAULT
                    limit = pool_default_quotas("VM_QUOTA/VM/VMS")
                    limit = OneQuotaHelper::LIMIT_UNLIMITED if limit.nil? || limit == ""
                end

                if limit == OneQuotaHelper::LIMIT_UNLIMITED
                    "%3d /   -" % [q['VM_QUOTA']['VM']["VMS_USED"]]
                else
                    "%3d / %3d" % [q['VM_QUOTA']['VM']["VMS_USED"], limit]
                end

            rescue NoMethodError
                "-"
            end
        end

        column :MEMORY, "Total memory allocated to user VMs", :size=>17 do |d|
            begin
                q = quotas[d['ID']]

                if q['VM_QUOTA']['VM'].nil? && d["ID"].to_i != 0
                    q['VM_QUOTA']['VM'] = {
                        "VMS"                   => OneQuotaHelper::LIMIT_DEFAULT,
                        "VMS_USED"              => "0",
                        "CPU"                   => OneQuotaHelper::LIMIT_DEFAULT,
                        "CPU_USED"              => "0",
                        "MEMORY"                => OneQuotaHelper::LIMIT_DEFAULT,
                        "MEMORY_USED"           => "0",
                        "RUNNING_VMS"           => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_VMS_USED"      => "0",
                        "RUNNING_CPU"           => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_CPU_USED"      => "0",
                        "RUNNING_MEMORY"        => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_MEMORY_USED"   => "0",
                        "SYSTEM_DISK_SIZE"      => OneQuotaHelper::LIMIT_DEFAULT,
                        "SYSTEM_DISK_SIZE_USED" => "0"
                    }

                    quotas[d['ID']] = q
                end

                limit = q['VM_QUOTA']['VM']["MEMORY"]

                if limit == OneQuotaHelper::LIMIT_DEFAULT
                    limit = pool_default_quotas("VM_QUOTA/VM/MEMORY")
                    limit = OneQuotaHelper::LIMIT_UNLIMITED if limit.nil? || limit == ""
                end

                if limit == OneQuotaHelper::LIMIT_UNLIMITED
                    "%7s /       -" % [
                        OpenNebulaHelper.unit_to_str(q['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M")]
                else
                    "%7s / %7s" % [
                        OpenNebulaHelper.unit_to_str(q['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M"),
                        OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")]
                end

            rescue NoMethodError
                "-"
            end
        end

        column :CPU, "Total CPU allocated to user VMs", :size=>11 do |d|
            begin
                q = quotas[d['ID']]

                if q['VM_QUOTA']['VM'].nil? && d["ID"].to_i != 0
                    q['VM_QUOTA']['VM'] = {
                        "VMS"                   => OneQuotaHelper::LIMIT_DEFAULT,
                        "VMS_USED"              => "0",
                        "CPU"                   => OneQuotaHelper::LIMIT_DEFAULT,
                        "CPU_USED"              => "0",
                        "MEMORY"                => OneQuotaHelper::LIMIT_DEFAULT,
                        "MEMORY_USED"           => "0",
                        "RUNNING_VMS"           => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_VMS_USED"      => "0",
                        "RUNNING_CPU"           => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_CPU_USED"      => "0",
                        "RUNNING_MEMORY"        => OneQuotaHelper::LIMIT_DEFAULT,
                        "RUNNING_MEMORY_USED"   => "0",
                        "SYSTEM_DISK_SIZE"      => OneQuotaHelper::LIMIT_DEFAULT,
                        "SYSTEM_DISK_SIZE_USED" => "0"
                    }

                    quotas[d['ID']] = q
                end

                limit = q['VM_QUOTA']['VM']["CPU"]

                if limit == OneQuotaHelper::LIMIT_DEFAULT
                    limit = pool_default_quotas("VM_QUOTA/VM/CPU")
                    limit = OneQuotaHelper::LIMIT_UNLIMITED if limit.nil? || limit == ""
                end

                if limit == OneQuotaHelper::LIMIT_UNLIMITED
                    "%3.1f /    -" % [q['VM_QUOTA']['VM']["CPU_USED"]]
                else
                    "%3.1f / %3.1f" % [q['VM_QUOTA']['VM']["CPU_USED"], limit]
                end

            rescue NoMethodError
                "-"
            end
        end

        default :ID, :NAME, :USERS, :VMS, :MEMORY, :CPU
    end

    table
end

#parse_template(tmpl_str) ⇒ Hash, Zona::Error

Parses a OpenNebula template string and turns it into a Hash

Parameters:

  • tmpl_str (String)

    template

Returns:

  • (Hash, Zona::Error)

    Hash or Error



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/one_helper/onegroup_helper.rb', line 249

def parse_template(tmpl_str)
    name_reg     =/[\w\d_-]+/
    variable_reg =/\s*(#{name_reg})\s*=\s*/
    single_variable_reg =/^#{variable_reg}([^\[]+?)(#.*)?$/

    tmpl                      = Hash.new
    tmpl['user']              = Hash.new

    tmpl_str.scan(single_variable_reg) do | m |
        key   = m[0].strip.downcase
        value = m[1].strip.gsub("\"","")
        case key
            when "admin_user_name"
                tmpl['user']['name']=value
            when "admin_user_password"
                tmpl['user']['password']=value
            when "admin_user_auth_driver"
                tmpl['user']['auth_driver']=value
            else
                tmpl[key] = value
        end
    end

    return tmpl
end