Merge lp:~ack/landscape-client/container-info-from-run-container-type into lp:~landscape/landscape-client/trunk

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: 738
Merged at revision: 739
Proposed branch: lp:~ack/landscape-client/container-info-from-run-container-type
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 77 lines (+19/-43)
2 files modified
landscape/lib/tests/test_vm_info.py (+13/-31)
landscape/lib/vm_info.py (+6/-12)
To merge this branch: bzr merge lp:~ack/landscape-client/container-info-from-run-container-type
Reviewer Review Type Date Requested Status
Geoff Teale (community) Approve
Free Ekanayaka (community) Approve
Review via email: mp+190899@code.launchpad.net

Commit message

This changes container detection to use /run/container_type (which is created by /etc/init/container-detect.conf if running in a container).

Description of the change

This changes container detection to use /run/container_type (which is created by /etc/init/container-detect.conf if running in a container). The file contains a single line with the container type.

This fixes the issue with the current LXC detection on Saucy and supports other types of containers too (openvz, vserver, ...).

To post a comment you must log in.
Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Looks good, +1!

review: Approve
Revision history for this message
Geoff Teale (tealeg) wrote :

+1, I don't like the one-line if style, but it's pythonic, so I'll get over it ;-)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/lib/tests/test_vm_info.py'
2--- landscape/lib/tests/test_vm_info.py 2013-10-09 07:50:54 +0000
3+++ landscape/lib/tests/test_vm_info.py 2013-10-14 09:45:01 +0000
4@@ -127,34 +127,16 @@
5
6 class GetContainerInfoTest(LandscapeTest):
7
8- def setUp(self):
9- super(GetContainerInfoTest, self).setUp()
10- self.root_path = self.makeDir()
11- self.proc_path = self.makeDir(
12- path=os.path.join(self.root_path, "proc"))
13-
14- def make_cgroup(self, content):
15- """Create /sys/class/dmi/id/sys_vendor with the specified content."""
16- init_proc_path = os.path.join(self.proc_path, "1")
17- self.makeDir(path=init_proc_path)
18- self.makeFile(
19- dirname=init_proc_path, basename="cgroup", content=content)
20-
21- def test_no_cgroup_file(self):
22- """If the cgroup file doesn't exist, it's not an LXC."""
23- self.assertEqual("", get_container_info(root_path=self.root_path))
24-
25- def test_cgroup_not_in_lxc(self):
26- """
27- If the cgroup file exists, but there is no mention of LXC, it's not an
28- LXC.
29- """
30- self.make_cgroup("2:cpu:/")
31- self.assertEqual("", get_container_info(root_path=self.root_path))
32-
33- def test_in_lxc(self):
34- """
35- If the cgroup file exists, and the group name is LXC, it's not an LXC.
36- """
37- self.make_cgroup("2:cpu:/lxc/test")
38- self.assertEqual("lxc", get_container_info(root_path=self.root_path))
39+ def test_no_container(self):
40+ """If not running in a container, an empty string is returned."""
41+ self.assertEqual("", get_container_info(path="/does/not/exist"))
42+
43+ def test_in_container(self):
44+ """If running in a container, the container type is returned."""
45+ path = self.makeFile(content="lxc")
46+ self.assertEqual("lxc", get_container_info(path=path))
47+
48+ def test_strip_newline(self):
49+ """The container type doesn't contain newlines."""
50+ path = self.makeFile(content="lxc\n")
51+ self.assertEqual("lxc", get_container_info(path=path))
52
53=== modified file 'landscape/lib/vm_info.py'
54--- landscape/lib/vm_info.py 2013-10-09 07:50:54 +0000
55+++ landscape/lib/vm_info.py 2013-10-14 09:45:01 +0000
56@@ -50,15 +50,9 @@
57 return ""
58
59
60-def get_container_info(root_path="/"):
61- """
62- Return a string with the type of container the client is running in if it's
63- known, an empty string otherwise.
64- """
65- cgroup_file = os.path.join(root_path, "proc/1/cgroup")
66- if os.path.exists(cgroup_file):
67- for line in read_file(cgroup_file).splitlines():
68- tokens = line.split(":")
69- if tokens[-1].startswith("/lxc/"):
70- return "lxc"
71- return ""
72+def get_container_info(path="/run/container_type"):
73+ """
74+ Return a string with the type of container the client is running in, if
75+ any, an empty string otherwise.
76+ """
77+ return read_file(path).strip() if os.path.exists(path) else ""

Subscribers

People subscribed via source and target branches

to all changes: