lxc-ruby 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -85,10 +85,21 @@ c.stop # => {:state => 'STOPPED', :pid => -1}
85
85
  c.freeze
86
86
  c.unfreeze
87
87
 
88
+ # Destroy container
89
+ c.destroy # => true
90
+ ```
91
+
92
+ Container metrics:
93
+
94
+ ```ruby
88
95
  # Get container memory usage (in bytes)
89
96
  c.memory_usage
90
97
  c.memory_limit
91
98
 
99
+ # Get container cpu shares and usage (in seconds)
100
+ c.cpu_shares # => 1024
101
+ c.cpu_usage # => 4312.08
102
+
92
103
  # Get running processes
93
104
  c.processes
94
105
  # =>
@@ -98,9 +109,6 @@ c.processes
98
109
  # "memory"=>"0.1",
99
110
  # "command"=>"/sbin/init",
100
111
  # "args"=>""}]
101
-
102
- # Destroy container
103
- c.destroy # => true
104
112
  ```
105
113
 
106
114
  To create a new container:
@@ -54,14 +54,14 @@ module LXC
54
54
  # Start container
55
55
  # @return [Hash] container status hash
56
56
  def start
57
- LXC.run('start', '-d', '-n', name)
57
+ run('start', '-d')
58
58
  status
59
59
  end
60
60
 
61
61
  # Stop container
62
62
  # @return [Hash] container status hash
63
63
  def stop
64
- LXC.run('stop', '-n', name)
64
+ run('stop')
65
65
  status
66
66
  end
67
67
 
@@ -75,14 +75,14 @@ module LXC
75
75
  # Freeze container
76
76
  # @return [Hash] container status hash
77
77
  def freeze
78
- LXC.run('freeze', '-n', name)
78
+ run('freeze')
79
79
  status
80
80
  end
81
81
 
82
82
  # Unfreeze container
83
83
  # @return [Hash] container status hash
84
84
  def unfreeze
85
- LXC.run('unfreeze', '-n', name)
85
+ run('unfreeze')
86
86
  status
87
87
  end
88
88
 
@@ -92,26 +92,42 @@ module LXC
92
92
  if !LXC::Shell.valid_state?(state)
93
93
  raise ArgumentError, "Invalid container state: #{state}"
94
94
  end
95
- LXC.run('wait', '-n', name, '-s', state)
95
+
96
+ run('wait', '-s', state)
96
97
  end
97
98
 
98
99
  # Get container memory usage in bytes
99
100
  # @return [Integer]
100
101
  def memory_usage
101
- LXC.run('cgroup', '-n', name, 'memory.usage_in_bytes').strip.to_i
102
+ run('cgroup', 'memory.usage_in_bytes').strip.to_i
102
103
  end
103
104
 
104
105
  # Get container memory limit in bytes
105
106
  # @return [Integer]
106
107
  def memory_limit
107
- LXC.run('cgroup', '-n', name, 'memory.limit_in_bytes').strip.to_i
108
+ run('cgroup', 'memory.limit_in_bytes').strip.to_i
109
+ end
110
+
111
+ # Get container cpu shares
112
+ # @return [Integer]
113
+ def cpu_shares
114
+ result = run('cgroup', "cpu.shares").strip
115
+ result.empty? ? nil : result.to_i
116
+ end
117
+
118
+ # Get container cpu usage in seconds
119
+ # @return [Float]
120
+ def cpu_usage
121
+ result = run('cgroup', "cpuacct.usage").strip
122
+ result.empty? ? nil : Float('%.4f' % (result.to_i / 1E9))
108
123
  end
109
124
 
110
125
  # Get container processes
111
126
  # @return [Array] list of all processes
112
127
  def processes
113
128
  raise ContainerError, "Container is not running" if !running?
114
- str = LXC.run('ps', '-n', name, '--', '-eo pid,user,%cpu,%mem,args').strip
129
+
130
+ str = run('ps', '--', '-eo pid,user,%cpu,%mem,args').strip
115
131
  lines = str.split("\n") ; lines.delete_at(0)
116
132
  lines.map { |l| parse_process_line(l) }
117
133
  end
@@ -121,6 +137,7 @@ module LXC
121
137
  # @return [Boolean]
122
138
  def create(path)
123
139
  raise ContainerError, "Container already exists." if exists?
140
+
124
141
  if path.is_a?(Hash)
125
142
  args = "-n #{name}"
126
143
 
@@ -186,21 +203,27 @@ module LXC
186
203
  #
187
204
  def destroy(force=false)
188
205
  raise ContainerError, "Container does not exist." unless exists?
206
+
189
207
  if running?
190
208
  if force
191
209
  # This will force stop and destroy container automatically
192
- LXC.run('destroy', '-n', '-f', name)
210
+ run('destroy', '-f')
193
211
  else
194
212
  raise ContainerError, "Container is running. Stop it first or use force=true"
195
213
  end
196
214
  else
197
- LXC.run('destroy', '-n', name)
215
+ run('destroy')
198
216
  end
217
+
199
218
  !exists?
200
219
  end
201
220
 
202
221
  private
203
222
 
223
+ def run(command, *args)
224
+ LXC.run(command, "-n", name, *args)
225
+ end
226
+
204
227
  def parse_process_line(line)
205
228
  chunks = line.split(' ')
206
229
  chunks.delete_at(0)
@@ -1,3 +1,3 @@
1
1
  module LXC
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -120,4 +120,65 @@ describe LXC::Container do
120
120
  end
121
121
  end
122
122
  end
123
+
124
+ describe '#cpu_shares' do
125
+ context 'when container is running' do
126
+ before do
127
+ stub_lxc("cgroup", "-n", "app", "cpu.shares") { "1024\n" }
128
+ end
129
+
130
+ it 'returns cpu shares value' do
131
+ subject.cpu_shares.should eq 1024
132
+ end
133
+ end
134
+
135
+ context 'when container is stopped' do
136
+ before do
137
+ stub_lxc("cgroup", "-n", "app", "cpu.shares") { "\n" }
138
+ end
139
+
140
+ it 'returns nil' do
141
+ subject.cpu_shares.should be_nil
142
+ end
143
+ end
144
+ end
145
+
146
+ describe '#cpu_usage' do
147
+ context 'when container is running' do
148
+ before do
149
+ stub_lxc("cgroup", "-n", "app", "cpuacct.usage") { "4239081939568\n" }
150
+ end
151
+
152
+ it 'returns usage in seconds' do
153
+ subject.cpu_usage.should eq 4239.0819
154
+ end
155
+ end
156
+
157
+ context 'when container is stopped' do
158
+ before do
159
+ stub_lxc("cgroup", "-n", "app", "cpuacct.usage") { "\n" }
160
+ end
161
+
162
+ it 'returns nil' do
163
+ subject.cpu_usage.should be_nil
164
+ end
165
+ end
166
+ end
167
+
168
+ describe '#run' do
169
+ let(:subject) do
170
+ class Kontainer < LXC::Container
171
+ def info
172
+ run('info')
173
+ end
174
+ end
175
+
176
+ Kontainer.new('app')
177
+ end
178
+
179
+ it 'executes a command with container name' do
180
+ stub_lxc("info", "-n", "app") { "info" }
181
+ subject.info.should eq "info"
182
+ end
183
+ end
123
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lxc-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-20 00:00:00.000000000 Z
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -102,15 +102,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
102
  - - ! '>='
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 2803095474129589431
105
108
  required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  none: false
107
110
  requirements:
108
111
  - - ! '>='
109
112
  - !ruby/object:Gem::Version
110
113
  version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 2803095474129589431
111
117
  requirements: []
112
118
  rubyforge_project:
113
- rubygems_version: 1.8.24
119
+ rubygems_version: 1.8.25
114
120
  signing_key:
115
121
  specification_version: 3
116
122
  summary: Ruby wrapper to LXC
@@ -125,4 +131,3 @@ test_files:
125
131
  - spec/fixtures/lxc-version.txt
126
132
  - spec/lxc_spec.rb
127
133
  - spec/spec_helper.rb
128
- has_rdoc: