Bash variable workaround

For test, use meta with example:

  write_files:
   - path: /test
     content: |
        export TVAR1=111
        export TVAR2=222
        echo TVAR1=${TVAR1} > /test
        echo TVAR2=$TVAR2 >> /test
        echo date=$(date) >> /test
        echo "${TVAR3:-defffaaaul}" >> /test
        # Jinja-like style still not work :(
        echo {{ REPOSITORY_SUITE }} >> /test

Change-Id: If1fde6b7d561e3e457e305505872fa114195ad63
This commit is contained in:
azvyagintsev 2017-05-30 16:52:06 +03:00
parent 8fb07e2cc1
commit ee1164d49a
2 changed files with 12 additions and 7 deletions

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from copy import deepcopy
import os
from devops.helpers import subprocess_runner
@ -77,11 +78,16 @@ def generate_cloud_image_settings(cloud_image_settings_path, meta_data_path,
" - sudo route add default gw "
"{gateway} {interface_name}")
logger.debug("user_data contains next data: \n{}".format(
user_data_content.format(**data_context)))
# FIXME this hack should be rewrited!
# Workaround to be able pass bash-style variables ${}
fmt_user_data_content = deepcopy(user_data_content)
for _key in data_context.keys():
logger.warning("Searching key:{} in user_data".format(_key))
_repl = "{{{0}}}".format(_key)
fmt_user_data_content = \
fmt_user_data_content.replace(_repl, data_context[_key])
with open(user_data_path, 'w') as f:
f.write(user_data_content.format(**data_context))
f.write(fmt_user_data_content)
# Generate cloud_ISO
cmd = "genisoimage -output {} " \
@ -89,5 +95,4 @@ def generate_cloud_image_settings(cloud_image_settings_path, meta_data_path,
"-rock {} {}".format(cloud_image_settings_path,
user_data_path,
meta_data_path)
subprocess_runner.Subprocess.check_call(cmd)

View File

@ -57,7 +57,6 @@ class TestCloudImageSettings(unittest.TestCase):
interface_name=u'enp0s3')
self.os_mock.makedirs.assert_called_once_with('/mydir')
self.open_mock.assert_has_calls((
mock.call('/mydir/meta-data', 'w'),
mock.call().__enter__(),
@ -72,7 +71,8 @@ class TestCloudImageSettings(unittest.TestCase):
' gateway 10.109.0.1\n'
' dns-nameservers 8.8.8.8\n'
'local-hostname: nailgun.domain.local'),
mock.call().__exit__(None, None, None),
mock.call().__exit__(None, None, None)))
self.open_mock.assert_has_calls((
mock.call('/mydir/user-data', 'w'),
mock.call().__enter__(),
mock.call().write(