Class: OneMarketPlaceAppHelper

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

Overview

OneMarketApp Command Helper

Constant Summary collapse

TEMPLATE_OPTIONS =
[
    {
        :name        => 'name',
        :large       => '--name name',
        :format      => String,
        :description => 'Name of the new MarketPlaceApp'
    },
    {
        :name        => 'description',
        :large       => '--description description',
        :format      => String,
        :description => 'Description for the new MarketPlaceApp'
    },
    {
        :name         => 'image',
        :large        => '--image id|name',
        :description  => 'Selects the image',
        :format       => String,
        :template_key => 'origin_id',
        :proc         => lambda {|o, _options|
            OpenNebulaHelper.rname_to_id(o, 'IMAGE')
        }
    },
    OpenNebulaHelper::DRY
]
VMNAME =
{
    :name   => 'vmname',
    :large  => '--vmname name',
    :description => 'Selects the name for the new VM Template, ' \
                    'if the App contains one',
    :format => String
}
MARKETS =

Available market place mads to import apps on them

%w[http s3]

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, #create_resource, #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



69
70
71
# File 'lib/one_helper/onemarketapp_helper.rb', line 69

def self.conf_file
    'onemarketapp.yaml'
end

.create_datastore_template(options) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/one_helper/onemarketapp_helper.rb', line 471

def create_datastore_template(options)
    template_options = TEMPLATE_OPTIONS.map do |o|
        o[:name].to_sym
    end

    template = create_variables(options,
                                template_options-[:dry, :image])

    template << "ORIGIN_ID=#{options[:image]}\n" if options[:image]
    template << "TYPE=image\n"

    [0, template]
end

.create_template_options_used?(options) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/one_helper/onemarketapp_helper.rb', line 132

def self.create_template_options_used?(options)
    # Get the template options names as symbols. options hash
    # uses symbols
    template_options=self::TEMPLATE_OPTIONS.map do |o|
        o[:name].to_sym
    end

    # Check if one at least one of the template options is
    # in options hash
    (template_options-options.keys)!=template_options
end

.create_variables(options, name) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/one_helper/onemarketapp_helper.rb', line 458

def create_variables(options, name)
    names = [name].flatten
    t     = ''

    names.each do |n|
        next unless options[n]

        t << "#{n.to_s.upcase}=\"#{options[n]}\"\n"
    end

    t
end

.market_exist?(pool, market) ⇒ Boolean

Check if marketplace exists

Parameters:

  • pool (MarketPlacePool)

    Marketplaces pool

  • market (String)

    ID to check

Returns:

  • (Boolean)

    True if exists, false otherwise



491
492
493
# File 'lib/one_helper/onemarketapp_helper.rb', line 491

def market_exist?(pool, market)
    !pool.select {|v| v['ID'].to_i == market }.empty?
end

.rnameObject



65
66
67
# File 'lib/one_helper/onemarketapp_helper.rb', line 65

def self.rname
    'MARKETPLACEAPP'
end

.state_to_str(id) ⇒ Object



73
74
75
76
77
# File 'lib/one_helper/onemarketapp_helper.rb', line 73

def self.state_to_str(id)
    id = id.to_i
    state_str = MarketPlaceApp::MARKETPLACEAPP_STATES[id]
    MarketPlaceApp::SHORT_MARKETPLACEAPP_STATES[state_str]
end

Instance Method Details

#format_pool(_options) ⇒ Object



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
# File 'lib/one_helper/onemarketapp_helper.rb', line 79

def format_pool(_options)
    CLIHelper::ShowTable.new(self.class.table_conf, self) do
        column :ID,
               'ONE identifier for the marketplace app',
               :size=>4 do |d|
            d['ID']
        end

        column :NAME, 'Name of the marketplace app', :left, :size=>25 do |d|
            d['NAME']
        end

        column :VERSION, 'Version of the app', :size=>10 do |d|
            d['VERSION']
        end

        column :SIZE, 'App size', :size =>5 do |d|
            OpenNebulaHelper.unit_to_str(d['SIZE'].to_i, {}, 'M')
        end

        column :STAT, 'State of the app', :size=>4 do |d|
            OneMarketPlaceAppHelper.state_to_str(d['STATE'])
        end

        column :REGTIME, 'Registration time of the app', :size=>8 do |d|
            Time.at(d['REGTIME'].to_i).strftime('%D')
        end

        column :TYPE, 'Marketplace app type', :size=>4 do |d|
            type = MarketPlaceApp::MARKETPLACEAPP_TYPES[d['TYPE'].to_i]
            MarketPlaceApp::SHORT_MARKETPLACEAPP_TYPES[type]
        end

        column :MARKET, 'Name of the MarketPlace', :left, :size=>20 do |d|
            d['MARKETPLACE']
        end

        column :ZONE, 'Zone ID', :size=>4 do |d|
            d['ZONE_ID']
        end

        default :ID,
                :NAME,
                :VERSION,
                :SIZE,
                :STAT,
                :TYPE,
                :REGTIME,
                :MARKET,
                :ZONE
    end
end

#get_obj_id(id, obj) ⇒ Integer

Get object ID from name

Parameters:

  • id (String)

    ID or NAME

  • obj (OpenNebula::Class)

    OpenNebula pool class

Returns:

  • (Integer)

    Object ID



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/one_helper/onemarketapp_helper.rb', line 360

def get_obj_id(id, obj)
    if (id.is_a? Integer) || id.match(/^\d+$/)
        id.to_i
    else
        pool = obj.new(@client)
        rc   = pool.info_all

        return rc if OpenNebula.is_error?(rc)

        rc = pool.find {|v| v['NAME'] == id }

        return Error.new("Object `#{id}` not found") unless rc

        rc['ID'].to_i
    end
end

#import(object, o_class) ⇒ Object

Import object into marketplace

Parameters:

  • object (String)

    Object ID or name

  • o_class (Class)

    Object class



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/one_helper/onemarketapp_helper.rb', line 148

def import(object, o_class)
    id = get_obj_id(object, o_class)

    if OpenNebula.is_error?(id)
        STDERR.puts id.message
        exit(-1)
    end

    rc = yield(id) if block_given?

    if OpenNebula.is_error?(rc)
        STDERR.puts rc.message
        exit(-1)
    elsif rc[0] == -1
        STDERR.puts rc[1]
        exit(-1)
    end

    0
end

#import_service_template(id, options) ⇒ Error code, error message

Import service template into marketplace

Parameters:

  • id (Integer)

    Service Template ID

  • options (Hash)

    User CLI options

Returns:

  • (Error code, error message)

    0 if everything was correct 1 otherwhise



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
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/one_helper/onemarketapp_helper.rb', line 176

def import_service_template(id, options)
    require 'json'

    pool = private_markets

    return pool if OpenNebula.is_error?(pool)

    s_template = ServiceTemplate.new_with_id(id, @client)
    rc         = s_template.info

    return rc if OpenNebula.is_error?(rc)

    body = JSON.parse(s_template.to_json)['DOCUMENT']['TEMPLATE']['BODY']
    import_all = options[:yes] || options[:no]

    while import_all != 'no' && import_all != 'yes'
        print 'Do you want to import images too? (yes/no, default=yes): '
        import_all = STDIN.readline.chop.downcase

        if import_all.empty?
            puts 'yes'
            import_all = 'yes'
        end
    end

    # Turn import all into a boolean
    import_all == 'yes' ? import_all = true : import_all = false

    main_market = options[:market]

    # Read marketplace where import the service template
    # It can only be a private marketplace
    unless main_market
        puts
        puts 'Available Marketplaces (please enter ID)'
        pool.each {|market| puts "- #{market['ID']}: #{market['NAME']}" }
        puts
    end

    default_mp = pool.first['ID'].to_i

    # Keep asking while user selects an incorrect marketplace
    until OneMarketPlaceAppHelper.market_exist?(pool, main_market)
        print 'Where do you want to import the Service template? ' \
              "(default=#{default_mp})"
        main_market = STDIN.readline.chop

        if main_market.empty?
            puts default_mp
            main_market = default_mp
        else
            main_market = main_market.to_i
        end
    end

    markets = import_service_template_roles(import_all,
                                            body,
                                            pool,
                                            options[:market])
    ids = []

    # Create roles apps
    if import_all
        rc  = create_apps(markets, import_all)
        ids = rc[1]

        return [-1, rc[0]] unless rc[0].empty?
    end

    # Create service template app
    s_template.extend(ServiceTemplateExt)

    rc = s_template.mp_import(markets, main_market, options[:vmname])

    if rc[0] == -1
        rc
    else
        ids.each {|i| puts "ID: #{i}" }
        puts "ID: #{rc[1]}"
        [0, nil]
    end
end

#import_vm_template(id, options) ⇒ Error code, error message

Import VM Template into marketplace

Parameters:

  • id (Integer)

    VM Template ID

  • options (Hash)

    User CLI options

Returns:

  • (Error code, error message)

    0 if everything was correct 1 otherwhise



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/one_helper/onemarketapp_helper.rb', line 266

def import_vm_template(id, options)
    pool = private_markets

    return pool if OpenNebula.is_error?(pool)

    template = Template.new_with_id(id, @client)
    rc       = template.info

    return rc if OpenNebula.is_error?(rc)

    import_all = options[:yes] || options[:no]

    while import_all != 'no' && import_all != 'yes'
        print 'Do you want to import images too? (yes/no, default=yes): '
        import_all = STDIN.readline.chop.downcase

        if import_all.empty?
            puts 'yes'
            import_all = 'yes'
        end
    end

    # Turn import all into a boolean
    import_all == 'yes' ? import_all = true : import_all = false

    main_market = options[:market]

    # Read marketplace where import the VM template
    # It can only be a private marketplace
    unless main_market
        puts
        puts 'Available Marketplaces (please enter ID)'
        pool.each {|market| puts "- #{market['ID']}: #{market['NAME']}" }
        puts
    end

    default_mp = pool.first['ID'].to_i

    # Keep asking while user selects an incorrect marketplace
    until OneMarketPlaceAppHelper.market_exist?(pool, main_market)
        print 'Where do you want to import the VM template? ' \
              "(default=#{default_mp})"
        main_market = STDIN.readline.chop

        if main_market.empty?
            puts default_mp
            main_market = default_mp
        else
            main_market = main_market.to_i
        end
    end

    # Templates import information
    #
    # template_id => :market   => marketplace ID to import it
    #                :template => ONE Template information
    #                :name     => generated name for the template
    markets                            = {}
    markets[template['ID']]            = {}
    markets[template['ID']][:market]   = main_market
    markets[template['ID']][:template] = template

    rc = create_apps(markets, import_all, options[:vmname])

    if rc[0] == -1
        rc
    else
        rc[1].each {|i| puts "ID: #{i}" }
        [0, nil]
    end
end

#save_as_template(id) ⇒ Integer

Save VM as template to be able to import it

Parameters:

  • id (Integer)

    VM ID

Returns:

  • (Integer)

    New template ID



343
344
345
346
347
348
349
350
351
352
# File 'lib/one_helper/onemarketapp_helper.rb', line 343

def save_as_template(id)
    vm = VirtualMachine.new_with_id(id, @client)
    rc = vm.info

    return rc if OpenNebula.is_error?(rc)

    vm.extend(VirtualMachineExt)

    vm.save_as_template("#{vm['NAME']}-saved", 'App to import')
end