Class: Vzlimit::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/vzlimit/cli.rb

Constant Summary collapse

ACTIONS =
[:limit, :list]
VZ_CONF_DIR =
'/etc/vz/conf'

Instance Method Summary collapse

Constructor Details

#initialize(stdout, arguments) ⇒ CLI

Returns a new instance of CLI.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vzlimit/cli.rb', line 10

def initialize(stdout, arguments)
  @stdout = stdout
  @arguments = arguments
  @h = HighLine.new
  
  begin
    # Check action argument
if !@arguments[0] || (!ACTIONS.include?(@arguments[0].to_sym) &&! @arguments[0] =~ /^-.*/)
      usage
    end
    
    # list
    if @arguments[0].to_sym == :list
      list
    end
    
    # limit
    if @arguments[0].to_sym == :set
      set
    end

# help
if @arguments[0] == '-h' || @arguments[0] == '--help'
	help
end

# version
    if @arguments[0] == '-v' || @arguments[0] == '--version'
      version
    end
  rescue
    @h.say $!
  end
end

Instance Method Details

#execute(command) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/vzlimit/cli.rb', line 193

def execute(command)
  @h.say "Will execute the following command:"
  @h.say "=================================================================================="
  @h.say command
  @h.say "=================================================================================="
  
  unless @h.agree @h.color("Execute this command?", :red) + " [yes/no]  "
    raise 'User aborted.'
  end
  
  retval = system command
  raise @h.color "Error: Command execution failed, exit status #{$?}.", :red unless retval
end

#helpObject



216
217
218
219
220
221
222
223
# File 'lib/vzlimit/cli.rb', line 216

def help
	@h.say "vzlimit list | set <veid>"
	@h.say "vzlimit set <veid>"
 		@h.say "\t[--ram, -r <soft-limit-in-mb>:<hard-limit-in-mb>]"
 		@h.say "\t[--diskspace, -d <soft-limit-in-mb>:<hard-limit-in-mb>]"
	@h.say "vzlimit --help, -h"
	@h.say "vzlimit --version, -v"
end

#limit_disk_space(vpsid, disk) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/vzlimit/cli.rb', line 170

def limit_disk_space(vpsid, disk)
  # Get separate soft and hard limit
  soft_mb, hard_mb = disk.split(':')
  
  # Desaster validation
  if soft_mb.to_f > hard_mb.to_f || soft_mb.to_f < 0 || hard_mb.to_f < 0
    raise "Disk limits not valid."
  end
  
  # Convert MB limits to 1K-block
  soft = (soft_mb.to_f * 1024).to_i
  hard = (hard_mb.to_f * 1024).to_i
  
  # Execute command 
  execute "vzctl set #{vpsid} --diskspace #{soft}:#{hard} --save"
  
  # Success message
  @h.say @h.color "Diskspace limits have been set:", :green
  @h.say "  VPS-ID:             #{vpsid}"
  @h.say "  Soft memory limit:  #{soft_mb} MB"
  @h.say "  Hard memory limit:  #{hard_mb} MB"
end

#limit_memory(vpsid, memory) ⇒ Object



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

def limit_memory(vpsid, memory)
  # Get separate soft and hard limit
  soft_mb, hard_mb = memory.split(':')
  
  # Desaster validation
  if soft_mb.to_f > hard_mb.to_f || soft_mb.to_f < 0 || hard_mb.to_f < 0
    raise "Memory limits not valid."
  end
  
  # Convert MB limits to mempages
  soft = (soft_mb.to_f * 1024 / 4).to_i
  hard = (hard_mb.to_f * 1024 / 4).to_i
  
  # Execute command 
  execute "vzctl set #{vpsid} --privvmpages #{soft}:#{hard} --save"
  
  # Success message
  @h.say @h.color "Memory limits have been set:", :green
  @h.say "  VPS-ID:             #{vpsid}"
  @h.say "  Soft memory limit:  #{soft_mb} MB"
  @h.say "  Hard memory limit:  #{hard_mb} MB"
end

#listObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vzlimit/cli.rb', line 45

def list
  # Scan directory /etc/vz/conf/
  conf_files = Dir.entries(VZ_CONF_DIR)
  
  vps_list = []
  
  conf_files.each do |file|
    if !(file =~ /^\d\d+\.conf$/)
      next;
    end
    
    vpsid = /^(\d\d+)/.match(file)[0]
    conf = File.open(VZ_CONF_DIR + '/' + file, "rb").read
    
    # Get memory limits
    null, memory_soft, memory_hard = /\nPRIVVMPAGES="(\d+):(\d+)"\n/.match(conf).to_a
    memory_soft = (memory_soft.to_f / 1024 * 4).to_i.to_s
    memory_hard = (memory_hard.to_f / 1024 * 4).to_i.to_s
    
    # Get diskspace limits
    null, diskspace_soft, diskspace_hard = /\nDISKSPACE="(\d+):(\d+)"\n/.match(conf).to_a
    diskspace_soft = (diskspace_soft.to_f / 1024).to_i.to_s
    diskspace_hard = (diskspace_hard.to_f / 1024).to_i.to_s
    
    # Get ip
    ip = /\nIP_ADDRESS="(\d+\.\d+\.\d+\.\d+)"\n/.match(conf)[1]
    
    vps_list << {
      :vpsid            => vpsid,
      :ip               => ip,
      :memory_soft      => @h.color(memory_soft + ' MB', :yellow),
      :memory_hard      => @h.color(memory_hard + ' MB', :red),
      :diskspace_soft   => @h.color(diskspace_soft + ' MB', :yellow),
      :diskspace_hard   => @h.color(diskspace_hard + ' MB', :red) 
    }
    
    #vps_list << [
    #  vpsid + "e[32mDONEe[0m",
    #  ip,
    #  memory_soft,
    #  memory_hard,
    #  diskspace_soft,
    #  diskspace_hard
    #]
  end
  
  #@h.say Hirb::Helpers::Table.render(vps_list, 
  #  :fields => [:vpsid, :ip, :memory_soft, :memory_hard, :diskspace_soft, :diskspace_hard],
  #  :headers => {:vpsid => 'CTID', :ip => 'IP', :memory_soft => 'MEMORY SOFT LIMIT', :memory_hard => 'MEMORY HARD LIMIT', :diskspace_soft => 'DISKSPACE SOFT LIMIT', :diskspace_hard => 'DISKSPACE HARD LIMIT'}
  #)
  HighLine.use_color=false

  @h.say sprintf("%-8s\t%-15s\t%-20s\t%-20s\t%-20s\t%-20s", 'CTID', 'IP', 'MEMORY SOFT LIMIT', 'MEMORY HARD LIMIT', 'DISKSPACE SOFT LIMIT', 'DISKSPACE HARD LIMIT') + "\n"
  @h.say sprintf("%-8s\t%-15s\t%-17s\t%-17s\t%-20s\t%-20s", '='*8, '='*15, '='*17, '='*17, '='*20, '='*20) + "\n"

  vps_list.each do |vps|
    @h.say sprintf("%-8s\t%-15s\t%-17s\t\t%-17s\t\t%-20s\t\t%-20s", vps[:vpsid], vps[:ip], vps[:memory_soft], vps[:memory_hard], vps[:diskspace_soft], vps[:diskspace_hard]) + "\n"
  end
  
  #@h.say sprintf("%10sworld", "hello");
  #@h.say @h.list vps_list, :columns_down
  
end

#setObject



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
# File 'lib/vzlimit/cli.rb', line 109

def set
  @arguments.shift
  usage = 'Usage: vzlimit set vpsid [options]'
  raise usage unless @arguments[0]
  
  vpsid = @arguments.shift
  memory = nil
  disk = nil
  
  while (arg = @arguments.shift) do
    if ['-r', '--ram'].include? arg
      memory = @arguments.shift
      
      if !(memory =~ /^[-+]?[0-9]*\.?[0-9]+:[-+]?[0-9]*\.?[0-9]+$/)
        raise "Usage: vzlimit set vpsid #{arg} soft:hard"
      end
    elsif ['-d', '--diskspace'].include? arg
      disk = @arguments.shift
      
      if !(disk =~ /^\d+:\d+$/)
        raise "Usage: vzlimit set vpsid #{arg} soft:hard"
      end
    else
      raise "Argument #{arg} not valid for action 'set'."
    end
  end
  
  if !memory &&! disk
    raise "Nothing to set."
  end
  
  # Limit memory
  limit_memory(vpsid, memory) if memory
  
  # Limit disk space
  limit_disk_space(vpsid, disk) if disk
end

#usageObject



207
208
209
210
# File 'lib/vzlimit/cli.rb', line 207

def usage
  @h.say "Usage: vzlimit action [params...]"
  exit
end

#versionObject



212
213
214
# File 'lib/vzlimit/cli.rb', line 212

def version
	@h.say "vzlimit version #{VERSION}"
end