linux-lxc 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c35fca8caf7359999dc398d555d1ec956694e350
4
- data.tar.gz: f6f67f5613ed298430178054a4fb722ef53d9346
3
+ metadata.gz: 2fe1bf7762531084aa64d739d830692e7fcecf3a
4
+ data.tar.gz: a5d0069c27df95bc06b6f7d9d00e1d52a62b7d7e
5
5
  SHA512:
6
- metadata.gz: 507e65ce27c9717e6edd413c693b876cddbfacff2f3cc0e77fd1482570586606d44238edda1d51156641aa872a105abb08ef267a3d7a43ddc904bb37888e1ae0
7
- data.tar.gz: 1723646ae1902658ef60650c5b559803ed2007c1e90d3d31e4fd4ce6930a0f417b87a77648c5482fc581a1a2fdf56d190ffcd438572499c5dbb287ed02fa34d0
6
+ metadata.gz: 1fa95c148a460561a821951ff434b94add69d757033a98cca9d6d93c38fe615c1e998dae35b2560b63d587dd7bc832dccb38ba3b08dd9a75b668a982b315dc96
7
+ data.tar.gz: 9a558c103146ba683a53c78d157577f69d82cd11b5cd735cbf8b5c8f63be219a86178505f1ac043bd5e87ffc3a9ca56218b99e51ef2d314e1b09b94791d284cb
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in linux-ip-addr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Meno Abels
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Linux::Lxc
2
+
3
+ This gem is able to parse a lxc-config file and than
4
+ allow to modify the content of a lxc-config and write
5
+ the in-memory represation back to disc.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'linux-lxc'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install linux-lxc
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/mabels/linux-lxc/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -1,5 +1,5 @@
1
1
  module Linux
2
2
  class Lxc
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/linux/lxc.rb CHANGED
@@ -15,6 +15,14 @@ module Linux
15
15
  def file
16
16
  @lxc.file
17
17
  end
18
+ def comment!
19
+ return if @key == '#'
20
+ # remove from index
21
+ lxc.remove_from_index(self)
22
+ @key = '#'
23
+ @value = '# ' + self.to_s
24
+ lxc.add_to_index(self)
25
+ end
18
26
  def to_s
19
27
  if value
20
28
  "#{key} = #{value}"
@@ -42,14 +50,26 @@ module Linux
42
50
  @lines.map{|i| i.value}
43
51
  end
44
52
 
53
+ def remove(line)
54
+ @lines = @lines.select{|i| i != line }
55
+ end
56
+
45
57
  def [](idx)
46
58
  @lines[idx]
47
59
  end
48
60
 
61
+ def comment!
62
+ @lines.each{|i| i.comment! }
63
+ end
64
+
49
65
  def length
50
66
  @lines.length
51
67
  end
52
68
 
69
+ def empty?
70
+ @lines.empty?
71
+ end
72
+
53
73
  def first
54
74
  @lines.first
55
75
  end
@@ -65,17 +85,35 @@ module Linux
65
85
  @index[key]
66
86
  end
67
87
 
88
+ def key_to_path(key, &block)
89
+ path = ""
90
+ dot = ""
91
+ key.split('.').each do |element|
92
+ path += dot + element
93
+ dot = "."
94
+ #puts ">>>>#{path}"
95
+ block.call(path)
96
+ end
97
+ end
98
+
99
+ def remove_from_index(line)
100
+ key_to_path(line.key) do |path|
101
+ get(path).remove(line)
102
+ get(path).empty? && @index.delete(path)
103
+ end
104
+ end
105
+
68
106
  def add(key, value = nil)
69
107
  key = key.strip
70
108
  if value and value.instance_of?(String)
71
109
  value = value.strip
72
110
  end
73
111
  line = Line.new(self, key, value)
74
- path = ""
75
- dot = ""
76
- key.split('.').each do |element|
77
- path += dot + element
78
- dot = "."
112
+ add_to_index(line)
113
+ end
114
+
115
+ def add_to_index(line)
116
+ key_to_path(line.key) do |path|
79
117
  @index[path] ||= Lines.new
80
118
  @index[path].add(line)
81
119
  end
@@ -100,8 +138,10 @@ module Linux
100
138
  lxc = Lxc.new(file, index)
101
139
  IO.read(file).lines.each do |line|
102
140
  line = line.chop
103
- if line.match(/^\s*$/) or line.match(/^\s*#.*$/)
141
+ if line.match(/^\s*$/)
104
142
  lxc.add(line, nil)
143
+ elsif line.match(/^\s*#.*$/)
144
+ lxc.add('#', line)
105
145
  else
106
146
  match = line.match(/^\s*([a-z\.]+)\s*=\s*(.*)\s*$/)
107
147
  throw "illegal line in #{@file}:#{@lines.length}" unless match
data/linux-lxc.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'linux/lxc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "linux-lxc"
8
+ spec.version = Linux::Lxc::VERSION
9
+ spec.authors = ["Meno Abels"]
10
+ spec.email = ["meno.abels@adviser.com"]
11
+ spec.summary = %q{Parse the output of ip addr on a linux system}
12
+ spec.description = %q{Parse the output of ip addr on a linux system}
13
+ spec.homepage = "https://github.com/mabels/gem-linux-lxc"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ # spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -154,6 +154,7 @@ SAMPLE
154
154
  lxc.write
155
155
 
156
156
  lxc_read = Linux::Lxc.parse(lxc.file)
157
+ assert_equal lxc_read.get('#').length, 2
157
158
  assert_equal lxc_read.get('lxc.cgroup.devices.allow').values, ['meno']
158
159
  assert_equal lxc_read.get('lxc.cgroup.devices.allow').first.file, lxc.file
159
160
  assert_equal lxc_read.get('lxc.cgroup.devices.allow').first.line, 2
@@ -163,6 +164,19 @@ SAMPLE
163
164
  assert_equal lxc_read.get('lxc.network.hwaddr').first.line, 2
164
165
  end
165
166
 
167
+ def test_comment
168
+ lxc = Linux::Lxc.parse(@lxc_config)
169
+ assert_equal lxc.get('#').length, 42
170
+ assert_equal lxc.get('lxc.cgroup.devices.allow').length, 16
171
+ lxc.get('lxc.cgroup.devices.allow')[0].comment!
172
+ assert_equal lxc.get('lxc.cgroup.devices.allow').length, 15
173
+ assert_equal lxc.get('#').length, 43
174
+ lxc.get('lxc.network').comment!
175
+ assert_equal lxc.get('#').length, 47
176
+ assert_equal lxc.get('lxc.network'), nil
177
+
178
+ end
179
+
166
180
  def test_write
167
181
  lxc = Linux::Lxc.parse(@lxc_config)
168
182
  lxc.file = "#{@lxc_config}.new"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux-lxc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meno Abels
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-19 00:00:00.000000000 Z
11
+ date: 2015-09-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Parse the output of ip addr on a linux system
14
14
  email:
@@ -17,8 +17,14 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
20
25
  - lib/linux/lxc.rb
21
26
  - lib/linux/lxc/version.rb
27
+ - linux-lxc.gemspec
22
28
  - test/linux_lxc_test.rb
23
29
  homepage: https://github.com/mabels/gem-linux-lxc
24
30
  licenses: