|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +import os.path |
| 13 | + |
| 14 | +from testinfra.backend import base |
| 15 | + |
| 16 | + |
| 17 | +class ChrootBackend(base.BaseBackend): |
| 18 | + """Run commands in a chroot folder |
| 19 | +
|
| 20 | + Requires root access or sudo |
| 21 | + Can be invoked by --hosts=/path/to/chroot/ --connection=chroot --sudo |
| 22 | + """ |
| 23 | + |
| 24 | + NAME = "chroot" |
| 25 | + |
| 26 | + def __init__(self, name, *args, **kwargs): |
| 27 | + self.name = name |
| 28 | + super().__init__(self.name, *args, **kwargs) |
| 29 | + |
| 30 | + def run(self, command, *args, **kwargs): |
| 31 | + if not os.path.exists(self.name) and os.path.isdir(self.name): |
| 32 | + raise RuntimeError( |
| 33 | + "chroot path {} not found or not a directory".format(self.name) |
| 34 | + ) |
| 35 | + cmd = self.get_command(command, *args) |
| 36 | + out = self.run_local("chroot %s /bin/sh -c %s", self.name, cmd) |
| 37 | + out.command = self.encode(cmd) |
| 38 | + return out |
0 commit comments