Browse Source

Implement --output-file to write the bash script

I will output a sourceable bash script with a capitalized
version of the releases dictionary variables.

Change-Id: I1fa8cbfd737d22e8d44723e0274d49d268b213e2
master
Enrique Llorente 1 year ago
parent
commit
8d4adf4233

+ 29
- 4
scripts/emit_releases_file/emit_releases_file.py View File

@@ -43,8 +43,8 @@ def load_featureset_file(featureset_file):
43 43
 def get_dlrn_hash(release, hash_name, retries=10):
44 44
     logger = logging.getLogger('emit-releases')
45 45
     full_hash_pattern = re.compile('[a-z,0-9]{40}_[a-z,0-9]{8}')
46
-    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo'
47
-                % (release, hash_name))
46
+    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo' %
47
+                (release, hash_name))
48 48
     for retry_num in range(retries):
49 49
         repo_file = None
50 50
         # Timeout if initial connection is longer than default
@@ -155,6 +155,28 @@ def shim_convert_old_release_names(releases_names):
155 155
     return releases_names
156 156
 
157 157
 
158
+def write_releases_dictionary_to_bash(releases_dictionary, bash_file_name):
159
+    logger = logging.getLogger('emit-releases')
160
+    # Make it deterministic, expected constants in the proper order
161
+    try:
162
+        bash_script = '''#!/bin/env bash
163
+export UNDERCLOUD_INSTALL_RELEASE="{undercloud_install_release}"
164
+export UNDERCLOUD_INSTALL_HASH="{undercloud_install_hash}"
165
+export UNDERCLOUD_TARGET_RELEASE="{undercloud_target_release}"
166
+export UNDERCLOUD_TARGET_HASH="{undercloud_target_hash}"
167
+export OVERCLOUD_DEPLOY_RELEASE="{overcloud_deploy_release}"
168
+export OVERCLOUD_DEPLOY_HASH="{overcloud_deploy_hash}"
169
+export OVERCLOUD_TARGET_RELEASE="{overcloud_target_release}"
170
+export OVERCLOUD_TARGET_HASH="{overcloud_target_hash}"
171
+'''.format(**releases_dictionary)
172
+        with open(bash_file_name, 'w') as bash_file:
173
+            bash_file.write(bash_script)
174
+    except Exception:
175
+        logger.exception("Writting releases dictionary")
176
+        return False
177
+    return True
178
+
179
+
158 180
 if __name__ == '__main__':
159 181
 
160 182
     default_log_file = '{}.log'.format(os.path.basename(__file__))
@@ -193,5 +215,8 @@ if __name__ == '__main__':
193 215
     releases_dictionary = compose_releases_dictionary(args.stable_release,
194 216
                                                       featureset)
195 217
 
196
-    releases_dictionary = shim_convert_old_release_names(
197
-        releases_dictionary)
218
+    releases_dictionary = shim_convert_old_release_names(releases_dictionary)
219
+
220
+    if not write_releases_dictionary_to_bash(
221
+            releases_dictionary, args.output_file):
222
+        exit(1)

+ 11
- 9
scripts/emit_releases_file/test_get_dlrn_hash.py View File

@@ -7,18 +7,20 @@ import pytest
7 7
 @mock.patch('requests.get')
8 8
 def test_get_dlrn_hash(mock_get):
9 9
     mock_response = mock.Mock()
10
-    mock_response.content = ('[delorean]\nname=delorean-openstack-nova-81c23c04'
11
-                             '7e8e0fc03b54164921f49fdb4103202c\nbaseurl=https:/'
12
-                             '/trunk.rdoproject.org/centos7/81/c2/81c23c047e8e0'
13
-                             'fc03b54164921f49fdb4103202c_b333f915\nenabled=1\n'
14
-                             'gpgcheck=0\npriority=1')
10
+    mock_response.content = (
11
+        '[delorean]\nname=delorean-openstack-nova-81c23c04'
12
+        '7e8e0fc03b54164921f49fdb4103202c\nbaseurl=https:/'
13
+        '/trunk.rdoproject.org/centos7/81/c2/81c23c047e8e0'
14
+        'fc03b54164921f49fdb4103202c_b333f915\nenabled=1\n'
15
+        'gpgcheck=0\npriority=1')
15 16
     release = 'master'
16 17
     hash_name = 'current-tripleo'
17
-    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo'
18
-                % (release, hash_name))
18
+    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo' %
19
+                (release, hash_name))
19 20
     mock_get.return_value = mock_response
20
-    assert (get_dlrn_hash(release, hash_name) ==
21
-            '81c23c047e8e0fc03b54164921f49fdb4103202c_b333f915')
21
+    assert (get_dlrn_hash(
22
+        release,
23
+        hash_name) == '81c23c047e8e0fc03b54164921f49fdb4103202c_b333f915')
22 24
     mock_get.assert_called_once_with(repo_url, timeout=(3.05, 27))
23 25
 
24 26
 

+ 68
- 0
scripts/emit_releases_file/test_write_bash_script.py View File

@@ -0,0 +1,68 @@
1
+from emit_releases_file import write_releases_dictionary_to_bash
2
+
3
+import os
4
+import mock
5
+from mock import mock_open
6
+from six import PY2
7
+
8
+import pytest
9
+
10
+if PY2:
11
+    BUILTINS_OPEN = "__builtin__.open"
12
+else:
13
+    BUILTINS_OPEN = "builtins.open"
14
+
15
+
16
+def test_empty_relases_dictionary_fails():
17
+    assert (not write_releases_dictionary_to_bash({}, ""))
18
+
19
+
20
+@pytest.fixture
21
+def releases_dictionary():
22
+    return {
23
+        'undercloud_install_release': 'queens',
24
+        'undercloud_install_hash': 'current-tripleo',
25
+        'undercloud_target_release': 'master',
26
+        'undercloud_target_hash': 'current-tripleo',
27
+        'overcloud_deploy_release': 'master',
28
+        'overcloud_deploy_hash': 'current-tripleo',
29
+        'overcloud_target_release': 'master',
30
+        'overcloud_target_hash': 'current-tripleo',
31
+    }
32
+
33
+
34
+@pytest.mark.parametrize('deleted_key', [
35
+    'undercloud_install_release',
36
+    'undercloud_install_hash',
37
+    'undercloud_target_release',
38
+    'undercloud_target_hash',
39
+    'overcloud_deploy_release',
40
+    'overcloud_deploy_hash',
41
+    'overcloud_target_release',
42
+    'overcloud_target_hash',
43
+])
44
+def test_missing_key_fails(releases_dictionary, deleted_key):
45
+    wrong_releases_dictionary = releases_dictionary.pop(deleted_key)
46
+    assert (not write_releases_dictionary_to_bash(wrong_releases_dictionary,
47
+                                                  ""))
48
+
49
+
50
+@mock.patch(BUILTINS_OPEN, new_callable=mock_open)
51
+def test_open_exception_fails(mock, releases_dictionary):
52
+    bash_script = '/foo/bar.sh'
53
+    mock.side_effect = IOError
54
+    assert (not write_releases_dictionary_to_bash(releases_dictionary,
55
+                                                  bash_script))
56
+
57
+
58
+@mock.patch(BUILTINS_OPEN, new_callable=mock_open)
59
+def test_output_is_sourceable(mock, releases_dictionary):
60
+    bash_script = '/foo/bar.sh'
61
+    assert (write_releases_dictionary_to_bash(releases_dictionary,
62
+                                              bash_script))
63
+    mock.assert_called_once_with(bash_script, 'w')
64
+    handle = mock()
65
+    args, _ = handle.write.call_args
66
+    written_content = args[0]
67
+    # TODO: check environment variables
68
+    assert (0 == os.system(written_content))

Loading…
Cancel
Save