Make config-download directory a git repository

Initialize the config-download directory as a git repository.
Snapshot and commit any changes to the config-download directory on
subsequent runs.

Change-Id: Ia1ca309cc7476bc84e101a3cd109352de3e0c06c
This commit is contained in:
Sam Doran 2018-06-28 18:42:30 -04:00 committed by James Slagle
parent 648aa43675
commit cb6c10c8cd
3 changed files with 32 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
features:
- |
Initialize a git repository in the config-download directory and
automatically snapshot changes made to the repository.

View File

@ -97,11 +97,13 @@ class TestConfig(base.TestCase):
mock_mkdir.assert_has_calls(expected_mkdir_calls, any_order=True)
mock_open.assert_has_calls(expected_calls, any_order=True)
@patch.object(ooo_config.git, 'Repo')
@mock.patch('os.mkdir')
@mock.patch('six.moves.builtins.open')
@patch.object(ooo_config.shutil, 'rmtree')
def test_overcloud_config_wrong_config_type(self, mock_rmtree,
mock_open, mock_mkdir):
mock_open, mock_mkdir,
mock_repo):
args = {'name': 'overcloud', 'config_dir': '/tmp/tht',
'config_type': ['bad_config']}
heat = mock.MagicMock()
@ -414,6 +416,7 @@ class TestConfig(base.TestCase):
self.assertRaises(ValueError,
self.config.download_config, stack, self.tmp_dir)
@patch.object(ooo_config.git, 'Repo')
@patch.object(ooo_config.shutil, 'copyfile')
@patch.object(ooo_config.Config, '_mkdir')
@patch.object(ooo_config.Config, '_open_file')
@ -424,7 +427,8 @@ class TestConfig(base.TestCase):
mock_rmtree,
mock_open,
mock_mkdir,
mock_copyfile):
mock_copyfile,
mock_repo):
config_type_list = ['config_settings', 'global_config_settings',
'logging_sources', 'monitoring_subscriptions',
'service_config_settings',

View File

@ -12,6 +12,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import git
import json
import logging
import os
@ -133,6 +134,19 @@ class Config(object):
yaml.safe_dump(playbook, conf_file, default_flow_style=False)
return sorted_tasks
def initialize_git_repo(self, dirname):
repo = git.Repo.init(dirname)
return repo
def snapshot_config_dir(self, repo, commit_message):
if repo.is_dirty(untracked_files=True):
self.log.info('Snapshotting {}'.format(repo.working_dir))
repo.index.add('*')
commit = repo.index.commit(commit_message)
self.log.info('Created commit {}'.format(commit.hexsha))
else:
self.log.info('No changes to commit')
def _mkdir(self, dirname):
if not os.path.exists(dirname):
try:
@ -390,12 +404,18 @@ class Config(object):
return config_dir
def download_config(self, name, config_dir, config_type=None,
preserve_config_dir=True):
preserve_config_dir=True, commit_message=None):
if commit_message is None:
commit_message = 'Automatic commit of config-download'
# One step does it all
stack = self.fetch_config(name)
self.create_config_dir(config_dir, preserve_config_dir)
self._mkdir(config_dir)
git_repo = self.initialize_git_repo(config_dir)
self.log.info("Generating configuration under the directory: "
"%s" % config_dir)
self.write_config(stack, name, config_dir, config_type)
self.snapshot_config_dir(git_repo, commit_message)
return config_dir