From 98f7b0766abbf8bbff0a121030f1fc07cda9c811 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 5 Mar 2020 01:12:37 +0530 Subject: [PATCH] Support for Script/PostActions Made scripts/events optional. Provide scripts and events in the below format in the yaml file. Do ot give these params under 'files:' section. These params are peer to Made'files:' scripts: "actions.sh" actions: "mounted,deleting" --- jet/main.py | 32 +++++++++++++------------------- jet/utils.py | 22 +++++++++++++++------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/jet/main.py b/jet/main.py index 9ac0296..d7346a5 100644 --- a/jet/main.py +++ b/jet/main.py @@ -72,9 +72,7 @@ def main(): shutil.rmtree(args.build) os.makedirs(args.build) os.makedirs('%s/contents' % args.build) - os.makedirs('%s/scripts' % args.build) contents = "%s/contents/contents" % args.build - scripts = "%s/scripts/activate" % args.build os.makedirs(contents) contents_pkg = '%s/pkg' % contents os.makedirs(contents_pkg) @@ -99,11 +97,16 @@ def main(): shutil.copy(os.path.join(args.source, f['source']), destination) # add file to manifest sha1 = crypto.generate_sha1(destination) - content_manifest += "%s sha1=%s uid=%s gid=%s mode=%s program_id=%s\n" % \ + content_manifest += "%s sha1=%s uid=%s gid=%s mode=%s\n" % \ (f["destination"][1:] if f["destination"][0] == "/" else f["destination"], - sha1, f["uid"], f["gid"], f["mode"], f["program_id"]) + sha1, f["uid"], f["gid"], f["mode"]) if f["symlink"]: contents_symlink += "%s%s %s\n" % (mount_dir, f["destination"], f["destination"]) + if project["scripts"] is not None: + dscripts = os.path.join(args.build, "scripts") + os.makedirs(dscripts) + log.info("copy file %s to %s/%s", project['scripts'], dscripts, project['scripts']) + shutil.copy(os.path.join(args.source, project['scripts']), dscripts) content_manifest_file = '%s/manifest' % contents_pkg log.info("create manifest file %s", content_manifest_file) @@ -119,17 +122,6 @@ def main(): with open(contents_symlink_file, "w") as f: f.write(contents_symlink) - given_scripts_file = os.path.join(args.source, "scripts/activate.sh") - init_content = "" - with open(given_scripts_file, 'r') as file: - init_content = file.read() - - scripts_file = '%s.sh' % scripts - log.info("create symlink file %s", scripts_file) - with open(scripts_file, "w") as f: - f.write(init_content) - os.chmod(scripts_file, 0o755) - log.info("sign manifest file %s" % content_manifest_file) crypto.sign(content_manifest_file, "%s.sig" % content_manifest_file, args.key, args.cert) @@ -144,13 +136,15 @@ def main(): utils.create_package_xml(project, version, package, args.build) package_manifest = "/set package_id=31 role=Provider_Daemon\n" - package_manifest_files = ["contents/contents.iso", "contents/contents.symlinks", "scripts/activate.sh", "package.xml"] + package_manifest_files = ["contents/contents.iso", "contents/contents.symlinks", "package.xml"] + if project["scripts"] is not None: + package_manifest_files.append("scripts/%s" % project["scripts"]) for f in package_manifest_files: - if "scripts" not in f: - package_manifest += "%s sha1=%s\n" % (f, crypto.generate_sha1(os.path.join(args.build, f))) - else: + if f == 'scripts/%s' % project['scripts']: package_manifest += "%s sha1=%s program_id=1\n" % (f, crypto.generate_sha1(os.path.join(args.build, f))) + else: + package_manifest += "%s sha1=%s\n" % (f, crypto.generate_sha1(os.path.join(args.build, f))) package_manifest_file = os.path.join(args.build, "manifest") log.info("create manifest file %s", package_manifest_file) diff --git a/jet/utils.py b/jet/utils.py index 65853bd..61ae025 100644 --- a/jet/utils.py +++ b/jet/utils.py @@ -31,6 +31,7 @@ import shutil import yaml import sys +import re try: FileNotFoundError @@ -101,7 +102,7 @@ def load_project(project_file, version): :rtype: dict """ with open(project_file , 'r') as f: - project_yaml = yaml.load(f, Loader=yaml.FullLoader) + project_yaml = yaml.load(f) def required(d, k): if k in d: @@ -112,13 +113,13 @@ def required(d, k): timestamp = datetime.datetime.now() project = { "basename": required(project_yaml, "basename"), + "scripts": project_yaml.get("scripts", None), + "actions": project_yaml.get("actions", None), "files": [], "comment": project_yaml.get("comment", "JET app %s" % project_yaml["basename"]), "arch": required(project_yaml, "arch"), "abi": required(project_yaml, "abi"), "copyright": project_yaml.get("copyright", "Copyright %s, Juniper Networks, Inc." % timestamp.year), - "mounted-action": project_yaml.get("mounted-action", "scripts/activate.sh"), - "deleting-action": project_yaml.get("deleting-action", "scripts/activate.sh"), "package_id": project_yaml.get("package_id", 31), "role": project_yaml.get("role", "Provider_Daemon"), "date": timestamp.strftime("%Y%m%d"), @@ -188,13 +189,17 @@ def package_xml_file(filename): etree.SubElement(package_xml, "basename").text = project["basename"] etree.SubElement(package_xml, "comment").text = "%s [%s]" % (project["comment"], version) etree.SubElement(package_xml, "copyright").text = project["copyright"] - etree.SubElement(package_xml, "deleting-action").text = project["deleting-action"] etree.SubElement(package_xml, "description").text = project["basename"] etree.SubElement(package_xml, "mntname").text = "%s%s-%s" % (project["basename"], project["abi"], binascii.b2a_hex(os.urandom(4)).decode()) - etree.SubElement(package_xml, "mounted-action").text = project["mounted-action"] etree.SubElement(package_xml, "require").text = "junos-runtime32" etree.SubElement(package_xml, "version").text = project["date"] etree.SubElement(package_xml, "spin").text = project["time"] + + if project['actions'] is not None: + act_list = re.split("[, ]", project['actions']) + for act in act_list: + etree.SubElement(package_xml, "%s-action"%act).text = "scripts/%s" % project["scripts"] + etree.SubElement(package_xml, "sb-location").text = "JetEZ" # XMLPKG_TOGGLE_LIST @@ -205,7 +210,10 @@ def package_xml_file(filename): dir = etree.SubElement(package_xml, "dir", name="contents") package_xml_file("contents/contents.iso") package_xml_file("contents/contents.symlinks") - dir = etree.SubElement(package_xml, "dir", name="scripts") - package_xml_file("scripts/activate.sh") with open("%s/package.xml" % path, "w+") as f: f.write(etree.tostring(package_xml, pretty_print=True).decode("utf8")) + if project['scripts'] is not None: + dir = etree.SubElement(package_xml, "dir", name="scripts") + package_xml_file("scripts/%s" % project['scripts']) + with open("%s/package.xml" % path, "w+") as f: + f.write(etree.tostring(package_xml, pretty_print=True).decode("utf8"))