paunch: support a directory for "config" parameter

Before, the "config" parameter would only support a JSON file, which
would be sent to Paunch via YAML.
Now, we accept a directory path, where all .json files in that directory
will be sent as a single dict into Paunch via YAML.

It's part of the effort to break down the container config into
individual files per step and per container.

Story: 2006732
Task: 37162
Change-Id: Ie8bda685c499ff5ef1dc2799a85287662b5af407
This commit is contained in:
Emilien Macchi 2019-10-15 20:37:53 -04:00
parent 18ece9fd5f
commit 6992d5e880
1 changed files with 18 additions and 4 deletions

View File

@ -20,8 +20,10 @@ __metaclass__ = type
from ansible.module_utils.basic import AnsibleModule
import yaml
import json
import os
import paunch as p
import yaml
from paunch import runner as prunner
from paunch.builder import compose1 as pcompose1
from paunch.builder import podman as ppodman
@ -50,7 +52,7 @@ description:
options:
config:
description:
- YAML or JSON file containing configuration data
- JSON file or directory of JSON files containing configuration data
config_id:
description:
- ID to assign to containers
@ -143,8 +145,20 @@ class PaunchManager:
log_file=self.log_file)
if self.config:
with open(self.config, 'r') as f:
self.config_yaml = yaml.safe_load(f)
if os.path.isdir(self.config):
container_configs = {}
config_files = [c_json for c_json in
os.listdir(self.config)
if c_json.endswith('.json')]
for cf in config_files:
with open(os.path.join(self.config, cf), 'r') as f:
c = os.path.splitext(cf)[0]
container_configs[c] = {}
container_configs[c].update(yaml.safe_load(f))
self.config_yaml = container_configs
else:
with open(self.config, 'r') as f:
self.config_yaml = yaml.safe_load(f)
if self.action == 'apply':
self.paunch_apply()