schroot 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1cbd1e243c8faebe108938506ba91250bcda4a47
4
- data.tar.gz: 95a0de9e53b9e1fbc5fe1fd8a5aa390044f02721
3
+ metadata.gz: 360e830115b4cb4068f260942452b45c4c064752
4
+ data.tar.gz: 0ac8b4afcb132b04a561e55211fefbdaf966deed
5
5
  SHA512:
6
- metadata.gz: 19ec77eef2486c3c77704cd1edc62df255dabcceb2169946a98524ab23a72d650dd1c181facf3caf82ca9762aa9b22e9e15d77c33fff8651c5e2df9b7dfa405f
7
- data.tar.gz: e983818aa490af8ff537aa15949fc3fb127fbeb4fb2df328f0ac32e4db2970d8eec2ea1a163141456a854d69a17a52d0b81081c32e162c7e046a522687b9db96
6
+ metadata.gz: 5bf309ce86c4c7277d7a836d3160359035d7bf53ce6533aca5a73e0b5d760de1e93b7d32396cbd9144cbfaff133ca61512d577b1c6bf7bd6653656bfc40b61f1
7
+ data.tar.gz: cb7abbeff9362b59b39737f0d6cff71b0b3a20dd2779389a3f68c5397c59d41dbfcdb5a8bdf8be35549be615ed6dafd149363ccbcb79cf1ee9d5b21ef29084c5
data/README.md CHANGED
@@ -27,15 +27,19 @@ Little example:
27
27
 
28
28
  Library installation is pretty simple:
29
29
 
30
- $ rake
31
- $ gem install ./*.gem
30
+ ```bash
31
+ $ rake
32
+ $ gem install ./*.gem
33
+ ```
32
34
 
33
35
  or just
34
-
35
- $ rake install
36
-
36
+ ```bash
37
+ $ rake install
38
+ ```
37
39
  Examples
38
40
  ------
41
+
42
+ Simple example:
39
43
 
40
44
  ```ruby
41
45
  irb(main):005:0> require 'schroot'
@@ -47,3 +51,27 @@ irb(main):007:0> stdin, stdout, stderr = my_session.run("echo Hello, World!")
47
51
  irb(main):008:0> stdout.gets
48
52
  => "Hello, World!\n"
49
53
  ```
54
+ Using logger:
55
+ ```ruby
56
+ irb(main):001:0> require 'schroot'
57
+ => true
58
+ irb(main):002:0> my_logger = Logger.new(STDOUT)
59
+ => #<Logger:0x0000000199b460 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x0000000199b438 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x0000000199b3c0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x0000000199b370 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x0000000199b2f8>>>>
60
+ irb(main):003:0> session = Schroot.new('default') do
61
+ irb(main):004:1* log my_logger
62
+ irb(main):005:1> end
63
+ D, [2014-05-06T19:49:15.497952 #3084] DEBUG -- : Hello there!
64
+ D, [2014-05-06T19:49:15.498035 #3084] DEBUG -- : Starting chroot session
65
+ I, [2014-05-06T19:49:15.498069 #3084] INFO -- : Executing schroot -b -c default
66
+ I, [2014-05-06T19:49:15.584809 #3084] INFO -- : Done!
67
+ I, [2014-05-06T19:49:15.584939 #3084] INFO -- : Executing schroot --location -c session:sid-7cefa94f-4bea-4d30-b4a9-d3008c255360
68
+ I, [2014-05-06T19:49:15.591380 #3084] INFO -- : Done!
69
+ D, [2014-05-06T19:49:15.591504 #3084] DEBUG -- : Session sid-7cefa94f-4bea-4d30-b4a9-d3008c255360 with default started in /var/lib/schroot/mount/sid-7cefa94f-4bea-4d30-b4a9-d3008c255360
70
+ => #<Schroot:0x000000019acda0 @logger=#<Logger:0x0000000199b460 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x0000000199b438 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x0000000199b3c0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x0000000199b370 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x0000000199b2f8>>>>, @chroot="default", @session="sid-7cefa94f-4bea-4d30-b4a9-d3008c255360", @location="/var/lib/schroot/mount/sid-7cefa94f-4bea-4d30-b4a9-d3008c255360">
71
+ irb(main):006:0> stream = session.run('uname -a')
72
+ I, [2014-05-06T19:50:35.057816 #3084] INFO -- : Executing schroot -r -c sid-7cefa94f-4bea-4d30-b4a9-d3008c255360 -- uname -a
73
+ I, [2014-05-06T19:50:35.251893 #3084] INFO -- : Done!
74
+ => [#<IO:fd 13>, #<IO:fd 15>, #<IO:fd 17>]
75
+ irb(main):007:0> stream[1].gets
76
+ => "Linux dan-desktop 3.13-1-amd64 #1 SMP Debian 3.13.7-1 (2014-03-25) x86_64 GNU/Linux\n"
77
+ ```
data/lib/schroot.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'open3'
2
+ require 'logger'
2
3
 
3
4
  SCHROOT_BASE="/var/lib/schroot"
4
5
 
@@ -7,15 +8,27 @@ end
7
8
 
8
9
  # Schroot session handler
9
10
  class Schroot
10
- def initialize(chroot_name = 'default')
11
+ def initialize(chroot_name = 'default', &block)
12
+ @logger = Logger.new nil
13
+
14
+ if block_given?
15
+ if block.arity == 1
16
+ yield self
17
+ elsif block.arity == 0
18
+ instance_eval &block
19
+ end
20
+ end
21
+
11
22
  start(chroot_name)
12
23
  end
13
24
 
14
25
  def safe_run(cmd)
26
+ @logger.info("Executing %s" % cmd)
15
27
  stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
16
28
  if wait_thr.value != 0
17
29
  raise SchrootError, "\ncmd=\"%s\"\nreturn_code= %i\nstdout= \"%s\"" % [cmd, wait_thr.value, stdout.gets]
18
30
  end
31
+ @logger.info("Done!")
19
32
  return stdin, stdout, stderr
20
33
  end
21
34
 
@@ -60,6 +73,7 @@ class Schroot
60
73
  # @return [String] schroot session id
61
74
  # A string representing schroot session id.
62
75
  def start(chroot_name = 'default')
76
+ @logger.debug("Starting chroot session")
63
77
  stop if @session
64
78
  command = ['schroot', '-b', '-c', chroot_name]
65
79
  ObjectSpace.define_finalizer(self, proc { stop })
@@ -68,6 +82,7 @@ class Schroot
68
82
  @session = stdout.gets.strip
69
83
  stdin, stdout, stderr = safe_run("schroot --location -c session:%s" % @session)
70
84
  @location = stdout.gets.strip
85
+ @logger.debug("Session %s with %s started in %s" % [@session, @chroot, @location])
71
86
  return @session
72
87
  end
73
88
 
@@ -76,11 +91,20 @@ class Schroot
76
91
  # @param chroot_name [String] name of configured chroot
77
92
  # @return [nil] session_id of killed session (should be nil)
78
93
  def stop
94
+ @logger.debug("Stopping session %s with %s" % [@session, @chroot])
79
95
  stdin, stdout, stderr = safe_run("schroot -e -c %s" % @session)
96
+ @logger.debug("Session %s of %s should be stopped" % [@session, @chroot])
80
97
  @location = nil
81
98
  @session = nil
99
+
100
+ end
101
+
102
+ # Sets log object
103
+ def log(log=@logger)
104
+ @logger = log
105
+ @logger.debug("Hello there!")
82
106
  end
83
107
 
84
108
  private :safe_run, :command
85
- attr_reader :session, :location, :chroot
109
+ attr_reader :session, :location, :chroot, :logger
86
110
  end
data/test/test_schroot.rb CHANGED
@@ -17,9 +17,10 @@ class SchrootTest < Test::Unit::TestCase
17
17
  assert_not_nil test_clone.chroot
18
18
  assert_not_nil test_clone.location
19
19
 
20
- assert_not_equal test.session, test_clone.session
21
- assert_not_equal test.session, test_clone.location
22
- assert_equal test.chroot, test_clone.chroot
20
+ assert_not_nil test.session
21
+ assert_not_nil test_clone.session
22
+ assert_not_nil test.location
23
+ assert_not_nil test_clone.location
23
24
 
24
25
  test.stop
25
26
  print_debug(test,:stopped)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schroot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniil Guzanov
@@ -20,7 +20,7 @@ files:
20
20
  - test/test_schroot.rb
21
21
  - README.md
22
22
  - Rakefile
23
- homepage: http://rubygems.org/gems/schroot
23
+ homepage: https://github.com/melkor217/ruby-schroot
24
24
  licenses:
25
25
  - WTFPL
26
26
  metadata: {}