class UnitHosting::Commands

Attributes

agent[RW]

CommandLineUtils::COMMANDS +=

cache[RW]

CommandLineUtils::COMMANDS +=

keyname[RW]

CommandLineUtils::COMMANDS +=

Public Class Methods

new(endpoint =nil) click to toggle source
Calls superclass method
# File lib/unit-hosting/commands.rb, line 17
def initialize endpoint =nil
  @keyname = "unit-hosting"
  @endpoint = endpoint
  @command_options = []
  super()
  @commands += ["login","logout","update","groups","group"]
  @agent = Agent.new
  @agent.endpoint = @endpoint
  @cache = nil
end

Public Instance Methods

group() click to toggle source
# File lib/unit-hosting/commands.rb, line 96
def group
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "List servers in group."
  @banner = ""
  return opt if @help

  id = @command_options.shift
  group = ask_group(id,cache.groups)
  puts group.tablize if group
end
groups() click to toggle source
# File lib/unit-hosting/commands.rb, line 87
def groups
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "List sever groups."
  @banner = ""
  return opt if @help
  STDOUT.puts cache.groups.try(:tablize)
end
login() click to toggle source
# File lib/unit-hosting/commands.rb, line 32
def login
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "Login to #{@endpoint} ."
  @banner = ""
  return opt if @help

  ok = false
  while(!ok) 
    user = ask('Enter user: ') do |q|
      q.validate = /\w+/
    end
    password = ask("Enter your password: ") do |q|
      q.validate = /\w+/
      q.echo = false
    end
    @agent.login(user,password)
    if @agent.login?
      ok = true
      Keystorage.set(@keyname,user,password)
      $stderr.puts "login OK"
    else
      $stderr.puts "password mismatch"
    end
  end
end
logout() click to toggle source
# File lib/unit-hosting/commands.rb, line 59
def logout
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "Logout from #{@endpoint} ."
  @banner = ""
  return opt if @help
  Keystorage.delete(@keyname)
end
update(all = false) click to toggle source
# File lib/unit-hosting/commands.rb, line 69
def update all = false
  opt = OptionParser.new
  opt.on('-a','--all', 'update all cache') { all = true }
  opt.parse!(@command_options)
  @summery = "Update cache."
  @banner = "GID [-a|--all]"
  return opt if @help
  gid = @command_options.shift

  start
  if all
    cache.update_all_groups!(@agent.groups)
  else
    group = ask_group(gid,cache.groups)
    cache.update_group!(group)
  end
end
vif_plug() click to toggle source
# File lib/unit-hosting/commands.rb, line 108
def vif_plug
  
end
vm_create() click to toggle source
# File lib/unit-hosting/commands.rb, line 112
def vm_create
  # vm:create tumf-sg-1
end

Private Instance Methods

ask_group(id,gs) click to toggle source
# File lib/unit-hosting/commands.rb, line 128
def ask_group(id,gs)
  unless id
    puts gs.extend(Groups).tablize
    id = ask('Enter group id: ',gs.ids) { |q|
      q.validate = /\w+/
      q.readline = true
    }
  end
  group = gs.find { |g| id == g.instance_id  }
  raise GroupNotFound,"Group #{id} is not exists." unless group
  group
end
cache_file(file = ".unit-hosting.cache") click to toggle source
# File lib/unit-hosting/commands.rb, line 141
def cache_file file = ".unit-hosting.cache"
  File.join ENV['HOME'],file
end
start() click to toggle source
# File lib/unit-hosting/commands.rb, line 117
def start
  return true if @agent.login?
  
  user = Keystorage.list(@keyname).shift
  login unless user
  if user
    @agent.login(user,Keystorage.get(@keyname,user))
    login unless @agent.login?
  end
  raise LoginError,"Can't start session" unless @agent.login?
end