Class: VagrantSubutai::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-subutai/subutai_commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg, env) ⇒ Commands

Returns a new instance of Commands.



9
10
11
# File 'lib/vagrant-subutai/subutai_commands.rb', line 9

def initialize(arg, env)
  super(arg, env)
end

Instance Method Details

#baseObject



18
19
20
# File 'lib/vagrant-subutai/subutai_commands.rb', line 18

def base
  "sudo /usr/bin/subutai".freeze
end

#bazaar(url) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/vagrant-subutai/subutai_commands.rb', line 458

def bazaar(url)
  Put.success "\n-------------------------------------"
  Put.success "| Blueprint provisioning via Bazaar |"
  Put.success "-------------------------------------\n"
  
  email = SubutaiConfig.get(:BAZAAR_EMAIL)
  password = SubutaiConfig.get(:BAZAAR_PASSWORD)

  # Register Peer Os to Bazaar
  unless registered?(url)
    username = SubutaiConfig.get(:SUBUTAI_USERNAME)
    pwd = SubutaiConfig.get(:SUBUTAI_PASSWORD)

    username, pwd = get_input_token if username.nil? || pwd.nil?

    res = Rest::SubutaiConsole.password(url, username, Configs::SubutaiConsoleAPI::DEFAULT_PASSWORDS, pwd)

    case res
      when Net::HTTPOK
        Put.info "\nSuccessfully changed default password.\n"
    end

    response = Rest::SubutaiConsole.token(url, username, pwd)

    case response
      when Net::HTTPOK
        email, password = register_by_token(response.body, url)
      else
        Put.error "PeerOS: #{response.body}"
        Put.error response.message
        return
    end
  end

  email, password =  if email.nil? || password.nil?

  response = Rest::Bazaar.(email, password)

  case response
    when Net::HTTPOK
      all_cookies = response.get_fields('set-cookie')
      cookies_array = Array.new
      all_cookies.each { | cookie |
        cookies_array.push(cookie.split('; ')[0])
      }
      cookies = cookies_array.join('; ')

      rh_id = info('id')
      peer_id = Rest::SubutaiConsole.fingerprint(url)

      env = Blueprint::EnvironmentController.new
      env.build(url, cookies, rh_id, peer_id, Configs::Blueprint::MODE::BAZAAR)
    else
      Put.error "Bazaar: #{response.body}"
  end
end

#blueprint(url, attempt) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/vagrant-subutai/subutai_commands.rb', line 328

def blueprint(url, attempt)
  begin
    response = Rest::SubutaiConsole.ready(url)

    case response
      when Net::HTTPOK                       # 200 Ready
        #Put.info "http::ok #{attempt}"
        # start provisioning
        variable = VagrantSubutai::Blueprint::VariablesController.new(0, 0, nil)

        resource = info('system')

        if variable.validate && variable.check_quota?(resource)
          mode = SubutaiConfig.get(:SUBUTAI_ENV_TYPE)

          if mode.nil?
            # check smart defaults
            fingerprint = Rest::SubutaiConsole.fingerprint(url)
            response = Rest::Bazaar.registered(fingerprint)

            case response
              when Net::HTTPOK
                # [MODE] The blueprint provisioning via Bazaar
                bazaar(url)
              when Net::HTTPNotFound
                # [MODE] blueprint provisioning via Peer Os (local)
                peer(url, resource)
              else
                Put.error response.message
                Put.error response.body
            end
          elsif mode == Configs::Blueprint::MODE::PEER
            # [MODE] blueprint provisioning via Peer Os (local)
            peer(url, resource)
          elsif mode == Configs::Blueprint::MODE::BAZAAR
            # [MODE] The blueprint provisioning via Bazaar
            bazaar(url)
          end
        end
      when response.code == 503
        if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
          #Put.info "status code 503 attempt: #{attempt}"
          sleep(2**attempt) #
          blueprint(url, attempt+1)
        end
      when Net::HTTPNotFound
        if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
          #Put.info "net::httpnotfound attempt: #{attempt}"
          sleep(2**attempt) #
          blueprint(url, attempt+1)
        end
      when response.code == 500       # management happened something wrong
        Put.error "PeerOS failed to run!"
      else
        # PeerOs not ready
        Put.error "PeerOS failed to run"
    end
  rescue Net::OpenTimeout
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      #Put.info "net::opentimeout attempt: #{attempt}"
      sleep(2**attempt) 
      blueprint(url, attempt+1)
    end
  rescue Errno::ECONNRESET
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      #Put.info "errno::econnreset attempt: #{attempt}"
      sleep(2**attempt) #
      blueprint(url, attempt+1)
    end
  rescue Errno::ECONNABORTED
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      #Put.info "errno::econnaborted attempt: #{attempt}"
      sleep(2**attempt) #
      blueprint(url, attempt+1)
    end
  rescue OpenSSL::OpenSSLError # generic openssl error
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      #Put.info "openssl::opensslerror attempt: #{attempt}"
      sleep(2**attempt) #
      blueprint(url, attempt+1)
    end
  rescue OpenSSL::SSL::SSLError
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      #Put.info "openssl::ssl::sslerror attempt: #{attempt}"
      sleep(2**attempt) #
      blueprint(url, attempt+1)
    end
  rescue => e
    if attempt == 1 && ARGV[0] == 'up' # fails first attempt then try
      #Put.info "e attempt: #{attempt} error: #{e} arg: #{ARGV[0]}"
      sleep(10)
      blueprint(url, attempt+1)
    else
      Put.error e
    end
  end
end

#deregister(username, password, url) ⇒ Object

Unregister Subutai Peer OS from Bazaar



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vagrant-subutai/subutai_commands.rb', line 61

def deregister(username, password, url)
  if registered?(url)
      username, password = get_input_token if username.nil? || password.nil?

      response = Rest::SubutaiConsole.token(url, username, password)

      case response
        when Net::HTTPOK

          response = Rest::SubutaiConsole.deregister(response.body, url)

          case response
            when Net::HTTPOK
              Put.success response.body
              Put.success "Successfully PeerOS unregistered from Bazaar."
            else
              Put.error "Error: #{response.body}\n"
          end
        else
          Put.error "Error: #{response.body}\n"
      end
  else
    Put.warn "The PeerOs not registered to Bazaar"
  end
end

#finger(url) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/vagrant-subutai/subutai_commands.rb', line 212

def finger(url)
  begin
    peer_id = Rest::SubutaiConsole.fingerprint(url)
    ip = info('ipaddr')

    STDOUT.puts ip
    STDOUT.puts peer_id
  rescue Net::OpenTimeout => e
    Put.error e
  rescue => e
    Put.error e
  end
end

#fingerprint(url) ⇒ Object

Show Subutai Console finger print



203
204
205
206
207
208
209
210
# File 'lib/vagrant-subutai/subutai_commands.rb', line 203

def fingerprint(url)
  begin
    peer_id = Rest::SubutaiConsole.fingerprint(url)
    Put.info peer_id
  rescue Net::OpenTimeout => e
    Put.error e
  end
end

#get_input_loginObject

Get Bazaar credentials from input



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/vagrant-subutai/subutai_commands.rb', line 256

def 

  email = nil
  password = nil

  if SubutaiConfig.get(:BAZAAR_EMAIL).nil?
    # Bazaar email
    Put.warn "\nPlease enter credentials Bazaar:\n"
    Put.info "\nemail: "
    email = STDIN.gets.chomp
  else
    email = SubutaiConfig.get(:BAZAAR_EMAIL)
  end


  if SubutaiConfig.get(:BAZAAR_PASSWORD).nil?
    # Bazaar password
    begin
      Put.info "\nEnter Bazaar password: "
      password = STDIN.noecho(&:gets).chomp
    rescue Errno::EBADF
      Put.warn "\nStdin doesn't support echo less input. Stdin can't hide password\n"
      password = STDIN.gets
    end
  else
    password = SubutaiConfig.get(:BAZAAR_PASSWORD)
  end

  [email, password]
end

#get_input_registerObject

Get Bazaar credentials and peer info



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
# File 'lib/vagrant-subutai/subutai_commands.rb', line 288

def get_input_register
  Put.warn "\nRegister your PeerOS to Bazaar:\n"

  hub_password = nil
  hub_email = nil

  if SubutaiConfig.get(:BAZAAR_EMAIL).nil?
    # Hub email
    Put.info "\nEnter Bazaar email: "
    hub_email = STDIN.gets.chomp
  else
    hub_email = SubutaiConfig.get(:BAZAAR_EMAIL)
  end


  if SubutaiConfig.get(:BAZAAR_PASSWORD).nil?
    # Hub password
    begin
      Put.info "\nEnter Bazaar password: "
      hub_password = STDIN.noecho(&:gets).chomp
    rescue Errno::EBADF
      Put.warn "\nStdin doesn't support echo less input. Stdin can't hide password\n"
      hub_password = STDIN.gets
    end
  else
    hub_password = SubutaiConfig.get(:BAZAAR_PASSWORD)
  end

  # Peer name
  peer_name = SubutaiConfig.get(:SUBUTAI_NAME)

  peer_scope = SubutaiConfig.get(:SUBUTAI_SCOPE)

  [hub_email, hub_password, peer_name, peer_scope]
end

#get_input_tokenObject

Get Subutai Peer Os credentials from input



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
# File 'lib/vagrant-subutai/subutai_commands.rb', line 227

def get_input_token
  password = nil
  username = nil

  if SubutaiConfig.get(:SUBUTAI_USERNAME).nil?
    Put.warn "\nPlease enter credentials Subutai Peer Os:\n"
    Put.info "\nPeerOS username: "
    username = STDIN.gets.chomp
  else
    username = SubutaiConfig.get(:SUBUTAI_USERNAME)
  end


  if SubutaiConfig.get(:SUBUTAI_PASSWORD).nil?
    begin
      Put.info "\nSet PeerOS password: "
      password = STDIN.noecho(&:gets).chomp
    rescue Errno::EBADF
      Put.warn "\nStdin doesn't support echo less input. Stdin can't hide password\n"
      password = STDIN.gets
    end
  else
    password = SubutaiConfig.get(:SUBUTAI_PASSWORD)
  end

  [username, password]
end

#info(arg) ⇒ Object

info id



23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-subutai/subutai_commands.rb', line 23

def info(arg)
  with_target_vms(nil, single_target: true) do |vm|
    vm.communicate.sudo("#{base} #{Configs::SubutaiAgentCommand::INFO} #{arg}") do |type, data|
      if type == :stdout
        result = data.split(/[\r\n]+/)
        return result.first
      end
    end
  end
end

#is_management_ready?(url, attempt) ⇒ Boolean

it will wait exponential time until management is ready

Returns:

  • (Boolean)


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
# File 'lib/vagrant-subutai/subutai_commands.rb', line 127

def is_management_ready?(url, attempt)
  begin
    response = Rest::SubutaiConsole.ready(url)

    case response
      when Net::HTTPOK                       # 200 Ready
        return true
      when response.code == 503
        if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
          sleep(2**attempt) #
          is_management_ready?(url, attempt+1)
        end
      when Net::HTTPNotFound
        if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
          sleep(2**attempt) #
          is_management_ready?(url, attempt+1)
        end
      when response.code == 500       # management happened something wrong
        Put.error "PeerOS failed to run!"
        return false
      else
        # PeerOs not ready
        Put.error "PeerOS failed to run"
        return false
    end
  rescue Net::OpenTimeout
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      sleep(2**attempt)
      is_management_ready?(url, attempt+1)
    end
  rescue Errno::ECONNRESET
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      sleep(2**attempt) #
      is_management_ready?(url, attempt+1)
    end
  rescue Errno::ECONNABORTED
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      sleep(2**attempt) #
      is_management_ready?(url, attempt+1)
    end
  rescue OpenSSL::OpenSSLError # generic openssl error
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      sleep(2**attempt) #
      is_management_ready?(url, attempt+1)
    end
  rescue OpenSSL::SSL::SSLError
    if attempt < VagrantSubutai::Configs::Blueprint::ATTEMPT
      sleep(2**attempt) #
      is_management_ready?(url, attempt+1)
    end
  rescue => e
    if attempt == 1 && ARGV[0] == 'up' # fails first attempt then try
      sleep(10)
      is_management_ready?(url, attempt+1)
    else
      return false
    end
  end
end

#list(arg) ⇒ Object



324
325
326
# File 'lib/vagrant-subutai/subutai_commands.rb', line 324

def list(arg)
  ssh(base + "#{Configs::SubutaiAgentCommand::LIST} #{arg}")
end

#logObject

show logs



14
15
16
# File 'lib/vagrant-subutai/subutai_commands.rb', line 14

def log
  ssh(base + Configs::SubutaiAgentCommand::LOG)
end

#open(link) ⇒ Object

opens browser



516
517
518
519
520
521
522
523
524
# File 'lib/vagrant-subutai/subutai_commands.rb', line 516

def open(link)
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
    system "start #{link}"
  elsif RbConfig::CONFIG['host_os'] =~ /darwin/
    system "open #{link}"
  elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
    system "xdg-open #{link}"
  end
end

#peer(url, resource) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/vagrant-subutai/subutai_commands.rb', line 426

def peer(url, resource)
  Put.success "\n--------------------------------------"
  Put.success "| Blueprint provisioning via Peer Os |"
  Put.success "--------------------------------------\n"

  username = SubutaiConfig.get(:SUBUTAI_USERNAME)
  password = SubutaiConfig.get(:SUBUTAI_PASSWORD)

  username, password = get_input_token if username.nil? || password.nil?

  res = Rest::SubutaiConsole.password(url, username, Configs::SubutaiConsoleAPI::DEFAULT_PASSWORDS, password)

  case res
    when Net::HTTPOK
      Put.info "\nSuccessfully changed default password.\n"
  end

  response = Rest::SubutaiConsole.token(url, username, password)

  case response
    when Net::HTTPOK
      rh_id = info('id')
      peer_id = Rest::SubutaiConsole.fingerprint(url)

      env = Blueprint::EnvironmentController.new
      env.check_free_quota(resource)
      env.build(url, response.body, rh_id, peer_id, Configs::Blueprint::MODE::PEER)
    else
      Put.error "PeerOS: Error #{response.body}"
  end
end

#register(username, password, url) ⇒ Object

register Subutai Peer Os to Bazaar by username and password



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
# File 'lib/vagrant-subutai/subutai_commands.rb', line 88

def register(username, password, url)
  if registered?(url)
    Put.warn "\nThe PeerOs already registered to Bazaar.\n"
  else
    begin
      username, password = get_input_token if username.nil? || password.nil?

      res = Rest::SubutaiConsole.password(url, username, Configs::SubutaiConsoleAPI::DEFAULT_PASSWORDS, password)

      case res
        when Net::HTTPOK
          Put.info "\nSuccessfully changed default password.\n"
      end

      response = Rest::SubutaiConsole.token(url, username, password)

      case response
        when Net::HTTPOK
          hub_email, hub_password, peer_name, peer_scope = get_input_register

          response = Rest::SubutaiConsole.register(response.body, url, hub_email, hub_password, peer_name, peer_scope)

          case response
            when Net::HTTPOK
              Put.success response.body
              Put.success "\"#{peer_name}\" successfully registered to Bazaar."
            else
              Put.error "Error: #{response.body}\n"
          end
        else
          Put.error "Error: #{response.body}\n"
      end
    rescue Net::OpenTimeout => e
      Put.error e
    end
  end
end

#register_by_token(token, url) ⇒ Object

register Subutai Peer Os to Bazaar by token



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/vagrant-subutai/subutai_commands.rb', line 188

def register_by_token(token, url)
  hub_email, hub_password, peer_name, peer_scope = get_input_register
  response = Rest::SubutaiConsole.register(token, url, hub_email, hub_password, peer_name, peer_scope)

  case response
    when Net::HTTPOK
      Put.success response.body
      Put.success "\"#{peer_name}\" successfully registered to Bazaar."
      [hub_email, hub_password]
    else
      Put.error "Error: #{response.body}\n"
  end
end

#registered?(url) ⇒ Boolean

checks The Peer Os registered or not registered to Bazaar

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-subutai/subutai_commands.rb', line 40

def registered?(url)
  begin
    fingerprint = Rest::SubutaiConsole.fingerprint(url)
    response = Rest::Bazaar.registered(fingerprint)

    case response
      when Net::HTTPOK
        return true
      when Net::HTTPNotFound
        return false
      else
        Put.error response.body
        Put.error response.message
        exit
    end
  rescue Net::OpenTimeout => e
    Put.error e
  end
end

#ssh(command) ⇒ Object



526
527
528
529
530
# File 'lib/vagrant-subutai/subutai_commands.rb', line 526

def ssh(command)
  with_target_vms(nil, single_target: true) do |vm|
    vm.action(:ssh_run, ssh_run_command: command, ssh_opts: {extra_args: ['-q']})
  end
end

#update(name) ⇒ Object

update Subutai rh or management



35
36
37
# File 'lib/vagrant-subutai/subutai_commands.rb', line 35

def update(name)
  ssh(base + Configs::SubutaiAgentCommand::UPDATE + " #{name}")
end