Class: UnitHosting::Commands

Inherits:
CommandLineUtils::Commands
  • Object
show all
Includes:
Api
Defined in:
lib/unit-hosting/commands.rb

Instance Method Summary collapse

Methods included from Api

keypath

Constructor Details

#initializeCommands

CommandLineUtils::COMMANDS +=



14
15
16
17
18
19
20
21
# File 'lib/unit-hosting/commands.rb', line 14

def initialize
  super
  @commands += ["login","logout","update","groups","group"]
  @agent = Agent.new
  @cache_file =  ENV['HOME']+"/.unit-hosting.cache"
  @cache = Cache.new(@cache_file)

end

Instance Method Details

#groupObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/unit-hosting/commands.rb', line 109

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

#groupsObject



99
100
101
102
103
104
105
106
107
# File 'lib/unit-hosting/commands.rb', line 99

def groups
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "List sever groups."
  @banner = ""
  return opt if @help

  puts @cache.groups.tablize
end

#loginObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/unit-hosting/commands.rb', line 23

def 
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "Login to https://cloud.unit-hosting.com ."
  @banner = ""
  return opt if @help

  user = ask('Enter user: ') do |q|
    q.validate = /\w+/
  end

  ok = false
  while(!ok) 
    password = ask("Enter your password: ") do |q|
      q.validate = /\w+/
      q.echo = false
    end

    @agent.(user,password)
    if @agent.login? # => OK
      ok = true
      Keystorage.set("unit-hosting",user,password)
      puts "login OK"
    else
      puts "password mismatch"
    end
  end
end

#logoutObject



52
53
54
55
56
57
58
59
# File 'lib/unit-hosting/commands.rb', line 52

def logout
  opt = OptionParser.new
  opt.parse!(@command_options)
  @summery = "Logout from https://cloud.unit-hosting.com ."
  @banner = ""
  return opt if @help
  Keystorage.delete("unit-hosting")
end

#updateObject



62
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
# File 'lib/unit-hosting/commands.rb', line 62

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
  gs = @agent.groups.collect { |g|
    unless g.key.empty?
      v = @cache.groups.find{ |c| c.instance_id == g.instance_id }
      v.update if v and v.vms == nil
      g.vms = v.vms if v
      g
    end
  }.compact
  @cache.transaction { |ps|
    if all
      ps["groups"] = gs.find_all { |g|
        !g.key.empty?
      }.extend(Groups).update
    else
      group = ask_group(gid,gs)
      ps["groups"] = gs.collect{ |g|
        unless g.key.empty?
          g.update if g.instance_id == group.instance_id
          g
        end
      }.compact.extend(Groups)
    end
  }
end

#vif_plugObject



121
122
123
# File 'lib/unit-hosting/commands.rb', line 121

def vif_plug
  
end

#vm_createObject



125
126
127
# File 'lib/unit-hosting/commands.rb', line 125

def vm_create
  # vm:create tumf-sg-1
end