From 7bc7e6c74cd2f6b8afa911f535990a1c39cf224c Mon Sep 17 00:00:00 2001 From: PuneetPunamiya Date: Fri, 14 May 2021 12:20:42 +0530 Subject: [PATCH] Adds a tool to create a task template - This patch adds a tool which help to create a manifest template for task/pipeline based on Tekton Catalog Organization Proposal Signed-off-by: Puneet Punamiya --- tools/template.yaml | 30 +++++++ tools/tep-template.md.template | 13 +++ tools/tools.py | 152 +++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 tools/template.yaml create mode 100644 tools/tep-template.md.template create mode 100644 tools/tools.py diff --git a/tools/template.yaml b/tools/template.yaml new file mode 100644 index 0000000000..38db551044 --- /dev/null +++ b/tools/template.yaml @@ -0,0 +1,30 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: '' + labels: + app.kubernetes.io/version: '' + annotations: + tekton.dev/pipelines.minVersion: '' + tekton.dev/tags: '' + tekton.dev/displayName: '' +spec: + description: '' + + workspaces: + - name: '' # Name of the workspace + description: '' # Description of the workspace + optional: true # Optional value of the workspace + + params: + - name: '' # Name of parms + description: '' # Description of the params + default: '' # Default value of the params + + steps: + - name: '' # Name of steps + image: '' # Name of the image to be used for the resource + script: | # Actions resource performs + + args: [] # Arguments to execute diff --git a/tools/tep-template.md.template b/tools/tep-template.md.template new file mode 100644 index 0000000000..4e116621b3 --- /dev/null +++ b/tools/tep-template.md.template @@ -0,0 +1,13 @@ +## Install the Task + +``` +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/ +``` + +## Parameters + + +## Workspaces + + +## Usage \ No newline at end of file diff --git a/tools/tools.py b/tools/tools.py new file mode 100644 index 0000000000..6cbf926636 --- /dev/null +++ b/tools/tools.py @@ -0,0 +1,152 @@ +import os +import yaml as y +from ruamel.yaml import YAML +import json +import sys +import argparse + +# Parent Directory path +parent_dir = os.path.normpath( + os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +TEP_TEMPLATE = os.path.normpath( + os.path.join(os.path.dirname( + os.path.abspath(__file__)), 'tep-template.md.template')) + +print(TEP_TEMPLATE) +# Type of Resource +task = {'task', 't', 'Task'} +pipeline = {'pipeline', 'p', 'Pipeline'} + +# Basic Template of Manifest according to TEP +template = "template.yaml" + +yaml = YAML() +with open(template) as fpi: + data = yaml.load(fpi) + +jsondata = json.dumps(data, indent=2) +json_object = json.loads(jsondata) + + +def createResourceTemplate(name: str, version: str, finalPath: str, file: str): + if not os.path.exists(finalPath): + os.makedirs(finalPath) + print("\n" + "Directory 📁 '% s' created" % name + "\n") + + minPipelineVersion = input("Enter min pipeline version: ") + tags = input("Enter tags related to task: ") + displayName = input("Enter displayName of task: ") + + metadata = json_object["metadata"] + + metadata["name"] = name + metadata["labels"]["app.kubernetes.io/version"] = version + metadata["annotations"]["tekton.dev/tags"] = tags + metadata["annotations"]["tekton.dev/pipelines.minVersion"] = minPipelineVersion + metadata["annotations"]["tekton.dev/displayName"] = displayName + + # Creating a file at specified location + with open(os.path.join(finalPath, file), 'w') as yaml_file: + y.dump(json_object, yaml_file, default_flow_style=False) + else: + print( + f"Resource with name `{name}` and version `{version}` already exists") + sys.exit(1) + + +def kind(args): + # Parent Directory path + parent_dir = os.path.normpath( + os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + + resourcetype = args.kind[0] + if resourcetype in task: + parent_dir = parent_dir + "/task/" + return resourcetype, parent_dir + elif resourcetype in pipeline: + parent_dir = parent_dir + "/pipeline/" + return resourcetype, parent_dir + else: + sys.stdout.write("Please respond with 'task' or 'pipeline'") + sys.exit(1) + + +def resName(args): + return args.name[0] + + +def ver(args): + return args.version[0] + + +def readmeTemplate(tep, tep_io): + header = { + 'title': tep['title'], + } + tep_io.write(f'# {header["title"].capitalize()}\n\n') + + +def main(): + + resourcetype = "" + name = "" + version = "" + + parser = argparse.ArgumentParser(description="Resource Template Tool!") + + parser.add_argument("-k", "--kind", type=str, nargs=1, + metavar=('type'), + help="Type of the resource.") + + parser.add_argument("-n", "--name", type=str, nargs=1, + metavar=('resourceName'), + help="Name of the resource.") + + parser.add_argument("-v", "--version", type=str, nargs=1, + metavar=('version'), + help="Version of the resource.") + + args = parser.parse_args() + + if args.kind != None: + resourcetype, parent_dir = kind(args) + if args.name != None: + name = resName(args) + if args.version != None: + version = ver(args) + + if resourcetype == "": + sys.stdout.write("Please enter the type of resource") + sys.exit(1) + + if name == "": + sys.stdout.write("Please enter the name of resource") + sys.exit(1) + + if version == "": + sys.stdout.write("Please enter the version of resource") + sys.exit(1) + + # Path + path = os.path.join(parent_dir, name.lower()) + + finalPath = os.path.join(path, version) + + # Speicfy the file name + file = name + ".yaml" + + createResourceTemplate(name, version, finalPath, file) + + resource = dict(title=name) + + with open(os.path.join(finalPath, "README.md"), 'w+') as new_resource: + readmeTemplate(resource, new_resource) + with open(TEP_TEMPLATE, 'r') as template: + new_resource.write(template.read()) + + +if __name__ == '__main__': + main()