Skip to content

Commit

Permalink
Merge pull request #2 from somasm-dev/soma
Browse files Browse the repository at this point in the history
Support for Script/PostActions
  • Loading branch information
ydnath authored Mar 12, 2020
2 parents 4cf64ca + 98f7b07 commit facf081
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
32 changes: 13 additions & 19 deletions jet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand Down
22 changes: 15 additions & 7 deletions jet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import shutil
import yaml
import sys
import re

try:
FileNotFoundError
Expand Down Expand Up @@ -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:
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand All @@ -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"))

0 comments on commit facf081

Please sign in to comment.