-
Notifications
You must be signed in to change notification settings - Fork 15
CFE-4074: Added a new input-type: file #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -855,3 +855,120 @@ $ cat ./out/masterfiles/def.json | |
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Referencing a file example | ||
|
|
||
| The `"file"` input type lets a module ask the user for the path to an existing file (a script, playbook, etc.) instead of a value typed in directly - the file must already exist, `cfbs` doesn't generate its contents. | ||
| Two optional attributes are available to the `"file"` input-type: `filetype` restricts which file extension(s) are accepted (a string or list of strings; any extension is accepted if omitted), and `while`, just like for `"list"`, lets the user supply any number of files instead of just one. | ||
|
|
||
| ```json | ||
| "input": [ | ||
| { | ||
| "type": "file", | ||
| "variable": "scripts", | ||
| "namespace": "cfbs", | ||
| "bundle": "run_scripts", | ||
| "label": "Script", | ||
| "question": "Which script should be run?", | ||
| "filetype": ".sh", | ||
| "while": "Do you want to add another script?" | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| ``` | ||
| $ cfbs input run-scripts | ||
| Collecting input for module 'run-scripts' | ||
| Which script should be run? /tmp/notes.txt | ||
| '/tmp/notes.txt' does not have one of the accepted file extensions (.sh), please try again | ||
| Which script should be run? /tmp/deploy.sh | ||
| Do you want to add another script? yes | ||
| Which script should be run? /tmp/rollback.sh | ||
| Do you want to add another script? no | ||
| $ cat ./run-scripts/input.json | ||
| [ | ||
| { | ||
| "type": "file", | ||
| "variable": "scripts", | ||
| "namespace": "cfbs", | ||
| "bundle": "run_scripts", | ||
| "label": "Script", | ||
| "question": "Which script should be run?", | ||
| "filetype": ".sh", | ||
| "while": "Do you want to add another script?", | ||
| "response": ["./run-scripts/deploy.sh", "./run-scripts/rollback.sh"] | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| Without `"while"`, `"response"` is a single path instead of a list. | ||
| A file already inside the project is referred to as-is; a file from outside (as above) is copied into the module's own directory, next to `input.json`. | ||
|
|
||
|
|
||
| Sometimes you may want to place all your files in a separate directory for easier management. | ||
| ``` | ||
| $ eza -T --level 2 | ||
| . | ||
| ├── cfbs.json | ||
| ├── out | ||
| │ ├── masterfiles | ||
| │ ├── masterfiles.tgz | ||
| │ └── steps | ||
| ├── README.md | ||
| ├── run-script-module | ||
| │ ├── input.json | ||
| │ └── main.cf | ||
| └── scripts | ||
| ├── script1.sh | ||
| ├── script2.sh | ||
| ├── script3.sh | ||
| ├── script4.sh | ||
| ├── script5.sh | ||
| └── script6.sh | ||
| ``` | ||
|
|
||
| Then you can add the entire directory as a local module and it will all get copied over to the host. | ||
| Reference the files as usual with either `cfbs input ./run-scripts-module` or by manually adding them to "input.json" | ||
|
|
||
| ``` | ||
| $ cfbs add ./scripts/ | ||
| --snip-- | ||
| [main 48af05f] Added module './scripts/' | ||
| $ cfbs build | ||
| --snip-- | ||
|
|
||
| $ cat out/masterfiles/def.json | ||
| { | ||
| "inputs": ["services/cfbs/modules/run-script-module/main.cf"], | ||
| "vars": { "control_common_bundlesequence_end": ["run_script_module:main"] }, | ||
| "variables": { | ||
| "run_script_module:main.scripts": { | ||
| "value": [ | ||
| "$(sys.inputdir)/services/cfbs/scripts/script1.sh", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the file input paths end up in the augments? Should you be able to add files without having them modifying augments? E.g., by omitting |
||
| "$(sys.inputdir)/services/cfbs/scripts/script2.sh", | ||
| "$(sys.inputdir)/services/cfbs/scripts/script3.sh", | ||
| "$(sys.inputdir)/services/cfbs/scripts/script4.sh", | ||
| "$(sys.inputdir)/services/cfbs/scripts/script5.sh", | ||
| "$(sys.inputdir)/services/cfbs/scripts/script6.sh" | ||
| ], | ||
| "comment": "Added by 'cfbs input'" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| After deployment your module with the expected files will now live on the host and are ready to be run as part of your policy-set. | ||
| ``` | ||
| ~/tmp/run-script-module main* | ||
| $ eza -T --level 3 out/masterfiles/services/cfbs | ||
| out/masterfiles/services/cfbs | ||
| ├── modules | ||
| │ └── run-script-module | ||
| │ └── main.cf | ||
| └── scripts | ||
| ├── script1.sh | ||
| ├── script2.sh | ||
| ├── script3.sh | ||
| ├── script4.sh | ||
| ├── script5.sh | ||
| └── script6.sh | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,7 +243,81 @@ def _perform_directory_step(args, source, destination, prefix): | |
| write_json(defjson, merged) | ||
|
|
||
|
|
||
| def _perform_input_step(args, name, destination, prefix): | ||
| def _path_if_already_shipped(path, build_modules, destination): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this function both tests if the file is already shipped and computes the if not? It feels a bit messy, but I don't know how to make it better. |
||
| """If `path` is already inside a local module's directory that has its own | ||
| "directory" build step shipping it to masterfiles, return the on-host path | ||
| that step will produce. That step already copies the whole directory | ||
| during this same build, so the caller doesn't need to (and shouldn't) | ||
| copy the file itself - just point at where it will end up. | ||
| """ | ||
| abs_path = os.path.abspath(path) | ||
| for module in build_modules: | ||
| module_name = module.get("name", "") | ||
| if not (module_name.startswith("./") and module_name.endswith("/")): | ||
| continue | ||
| module_root = os.path.abspath(module_name) | ||
| if os.path.commonpath([abs_path, module_root]) != module_root: | ||
| continue | ||
|
Comment on lines
+259
to
+260
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment here would be nice |
||
| for step in module.get("steps", []): | ||
| operation, args = split_build_step(step) | ||
| if operation != "directory" or len(args) != 2: | ||
| continue | ||
| src, dst = args | ||
| if src not in (".", "./"): | ||
| continue | ||
| rel = os.path.relpath(abs_path, module_root) | ||
| dst = "" if dst in (".", "./") else dst | ||
| dest = os.path.join(destination, dst, rel) | ||
| return "$(sys.inputdir)/" + os.path.relpath(dest, destination) | ||
| return None | ||
|
|
||
|
|
||
| def _localize_file_inputs(name, input_data, destination, build_modules): | ||
| """Copy files referenced by "file" type input responses into the built | ||
| masterfiles, so they're actually part of what gets deployed instead of | ||
| only existing in the project directory. Rewrites the responses in place | ||
| to the resulting on-host path. | ||
|
|
||
| If a response is already shipped by another module's own "directory" | ||
| build step (e.g. the project author set one up manually), that step's | ||
| destination is used instead of making a redundant copy. | ||
| """ | ||
| if not isinstance(input_data, list): | ||
| return | ||
|
|
||
| module_dir_name = name[2:] if name.startswith("./") else name | ||
| module_dir_name = os.path.basename(module_dir_name.rstrip("/")) | ||
|
|
||
| def _localize(path): | ||
| if not path or not os.path.isfile(path): | ||
| return path | ||
|
|
||
| already_shipped = _path_if_already_shipped(path, build_modules, destination) | ||
| if already_shipped is not None: | ||
| return already_shipped | ||
|
|
||
| dest = os.path.join( | ||
| destination, | ||
| "services", | ||
| "cfbs", | ||
| "modules", | ||
| module_dir_name, | ||
| os.path.basename(path), | ||
| ) | ||
| cp(path, dest) | ||
| return "$(sys.inputdir)/" + os.path.relpath(dest, destination) | ||
|
|
||
| for element in input_data: | ||
| if not isinstance(element, dict) or element.get("type") != "file": | ||
| continue | ||
| response = element.get("response") | ||
| if isinstance(response, list): | ||
| element["response"] = [_localize(path) for path in response] | ||
| else: | ||
| element["response"] = _localize(response) | ||
|
|
||
|
|
||
| def _perform_input_step(args, name, destination, prefix, build_modules): | ||
| src, dst = args | ||
| if dst in [".", "./"]: | ||
| dst = "" | ||
|
|
@@ -264,6 +338,7 @@ def _perform_input_step(args, name, destination, prefix): | |
| ) | ||
| return | ||
| extras, original = read_json(src), read_json(dst) | ||
| _localize_file_inputs(name, extras, destination, build_modules) | ||
| extras = generate_augment(name, extras) | ||
| log.debug("Generated augment: %s", pretty(extras)) | ||
| if not extras: | ||
|
|
@@ -424,7 +499,7 @@ def perform_build(config: CFBSConfig, diffs_filename=None) -> int: | |
| elif operation == "directory": | ||
| _perform_directory_step(args, source, destination, prefix) | ||
| elif operation == "input": | ||
| _perform_input_step(args, name, destination, prefix) | ||
| _perform_input_step(args, name, destination, prefix, config["build"]) | ||
| elif operation == "policy_files": | ||
| _perform_policy_files_step(args, destination, prefix) | ||
| elif operation == "bundles": | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.