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
=== modified file 'landscape/lib/tests/test_vm_info.py'
--- landscape/lib/tests/test_vm_info.py 2013-10-09 07:50:54 +0000
+++ landscape/lib/tests/test_vm_info.py 2013-10-14 09:45:01 +0000
@@ -127,34 +127,16 @@
127127
128class GetContainerInfoTest(LandscapeTest):128class GetContainerInfoTest(LandscapeTest):
129129
130 def setUp(self):130 def test_no_container(self):
131 super(GetContainerInfoTest, self).setUp()131 """If not running in a container, an empty string is returned."""
132 self.root_path = self.makeDir()132 self.assertEqual("", get_container_info(path="/does/not/exist"))
133 self.proc_path = self.makeDir(133
134 path=os.path.join(self.root_path, "proc"))134 def test_in_container(self):
135135 """If running in a container, the container type is returned."""
136 def make_cgroup(self, content):136 path = self.makeFile(content="lxc")
137 """Create /sys/class/dmi/id/sys_vendor with the specified content."""137 self.assertEqual("lxc", get_container_info(path=path))
138 init_proc_path = os.path.join(self.proc_path, "1")138
139 self.makeDir(path=init_proc_path)139 def test_strip_newline(self):
140 self.makeFile(140 """The container type doesn't contain newlines."""
141 dirname=init_proc_path, basename="cgroup", content=content)141 path = self.makeFile(content="lxc\n")
142142 self.assertEqual("lxc", get_container_info(path=path))
143 def test_no_cgroup_file(self):
144 """If the cgroup file doesn't exist, it's not an LXC."""
145 self.assertEqual("", get_container_info(root_path=self.root_path))
146
147 def test_cgroup_not_in_lxc(self):
148 """
149 If the cgroup file exists, but there is no mention of LXC, it's not an
150 LXC.
151 """
152 self.make_cgroup("2:cpu:/")
153 self.assertEqual("", get_container_info(root_path=self.root_path))
154
155 def test_in_lxc(self):
156 """
157 If the cgroup file exists, and the group name is LXC, it's not an LXC.
158 """
159 self.make_cgroup("2:cpu:/lxc/test")
160 self.assertEqual("lxc", get_container_info(root_path=self.root_path))
161143
=== modified file 'landscape/lib/vm_info.py'
--- landscape/lib/vm_info.py 2013-10-09 07:50:54 +0000
+++ landscape/lib/vm_info.py 2013-10-14 09:45:01 +0000
@@ -50,15 +50,9 @@
50 return ""50 return ""
5151
5252
53def get_container_info(root_path="/"):53def get_container_info(path="/run/container_type"):
54 """54 """
55 Return a string with the type of container the client is running in if it's55 Return a string with the type of container the client is running in, if
56 known, an empty string otherwise.56 any, an empty string otherwise.
57 """57 """
58 cgroup_file = os.path.join(root_path, "proc/1/cgroup")58 return read_file(path).strip() if os.path.exists(path) else ""
59 if os.path.exists(cgroup_file):
60 for line in read_file(cgroup_file).splitlines():
61 tokens = line.split(":")
62 if tokens[-1].startswith("/lxc/"):
63 return "lxc"
64 return ""

Subscribers

People subscribed via source and target branches

to all changes: