-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbook
46 lines (37 loc) · 1.79 KB
/
playbook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env -S python3 -m uplaybook.cli
"""
Create a new uplaybook.
This playbook creates a new, example, uplaybook. This can be
used as a skeleton to create new uplaybooks.
"""
from uplaybook import fs, core, ARGS
core.playbook_args(options=[
core.Argument(name="playbook_name",
description="Name of playbook to create, creates directory of this name."),
core.Argument(name="git", default=False, type="bool",
description="Initialize git (only for directory-basd playbooks)."),
core.Argument(name="single-file", default=False, type="bool",
description="Create a single-file uplaybook rather than a directory."),
core.Argument(name="force", default=False, type="bool",
description="Reset the playbook back to the default if it already "
"exists (default is to abort if playbook already exists)."),
])
if ARGS.git and ARGS.single_file:
core.fail(msg="*** ERROR: Cannot do '--git' with '--single-file'.")
playbook_fs_name = ARGS.playbook_name
if ARGS.single_file:
if not playbook_fs_name.endswith(".pb"):
playbook_fs_name = f"{playbook_fs_name}.pb"
if fs.exists(playbook_fs_name) and not ARGS.force:
core.fail(msg=f"*** ERROR: Playbook {ARGS.playbook_name} already exists. Will not overwrite without '--force'")
def git_init():
"Initialize the git repo, only gets run when the directory is created due to 'notify' call below"
with fs.cd(ARGS.playbook_name):
core.run("git init")
core.run("git add .")
if ARGS.single_file:
fs.cp(src="playbook.j2", path=playbook_fs_name)
else:
fs.mkdir(path=ARGS.playbook_name).notify(git_init)
with fs.cd(path=ARGS.playbook_name):
fs.cp(src="playbook.j2", path="playbook")