Generate and source a dib v2 environment

diskimage-builder v2 now relies on the output of "element-info --env
<elements>" to have been sourced into the shell running the element
hooks. Generate the environment and save the output to a file under
environment.d so that dib-run-parts will source it prior to running
hooks.

Change-Id: Ie8ac128e391a7091184b5017959a2a653166062f
Closes-Bug: #1671262
This commit is contained in:
James Slagle 2017-03-08 19:01:57 -05:00
parent 59854cac25
commit db7123ec10
2 changed files with 18 additions and 0 deletions

View File

@ -55,6 +55,9 @@ class ElementRunner(object):
self.no_cleanup = no_cleanup
self.loaded_elements = {}
self.tmp_hook_dir = tempfile.mkdtemp()
self.environment_file = os.path.join(self.tmp_hook_dir,
'environment.d',
'00-dib-v2-env')
# the environment variable should override anything passed in
if 'ELEMENTS_PATH' in os.environ:
@ -72,6 +75,14 @@ class ElementRunner(object):
self.load_dependencies()
self.process_exclude_elements()
self.copy_elements()
self.generate_environment()
def generate_environment(self):
"""Generate a dib v2 environment."""
command = ['element-info', '--env'] + list(self.elements)
env_output = subprocess.check_output(command)
with open(self.environment_file, 'w') as f:
f.write(env_output)
def run(self):
"""Apply the elements by running each specified hook."""

View File

@ -32,11 +32,18 @@ class TestRunner(testtools.TestCase):
test_elements = os.path.join(cwd, 'elements')
self.element_paths = [test_elements]
self.patcher = mock.patch.object(runner.ElementRunner,
'generate_environment')
self.mock_env = self.patcher.start()
self.runner = runner.ElementRunner(['dep2', 'echo', 'os'], [],
self.element_paths)
tmp_dir = tempfile.mkdtemp()
self.runner.tmp_hook_dir = tmp_dir
def tearDown(self):
super(TestRunner, self).tearDown()
self.patcher.stop()
def test_cleanup(self):
self.runner.cleanup()
self.assertFalse(os.path.exists(self.runner.tmp_hook_dir))