def runCommand(realArgs): """ Run packaging subcommands. """ options, args = parseCliArgs(realArgs) try: subcommand = args[0] subcommands = ["init", "add", "test"] if not subcommand in subcommands: sys.exit( subcommand + " not a recognized subcommand of pkg. Use --help for help.") except IndexError: sys.exit("No command given, use -h for help.") user = User() if subcommand == "init": if not os.path.exists("./.subuser.json"): repoConfig = {} if options.imageSourcesDir: repoConfig["image-sources-dir"] = options.imageSourcesDir with user.getEndUser().get_file("./.subuser.json", "w") as subuserDotJson: json.dump(repoConfig, subuserDotJson) if options.imageSourcesDir: user.getEndUser().makedirs(options.imageSourcesDir) subuserlib.print.printWithoutCrashing( "Subuser repository initialized successfully!") subuserlib.print.printWithoutCrashing( "You can add new image sources with:") subuserlib.print.printWithoutCrashing( "$ subuser pkg add image-source-name") else: sys.exit("Subuser repository already initialized. Exiting.") if subcommand == "add": repo = subuserlib.resolve.getRepositoryFromPath( user, os.environ["PWD"]) imageSourceToAdd = args[1] if imageSourceToAdd in repo: sys.exit( "An image source named " + imageSourceToAdd + " is already present in the repository. Cannot add. Exiting.") subuserlib.print.printWithoutCrashing("Adding new image source " + imageSourceToAdd) useDefaultLocations = options.imageFile is None and options.permissionsFile is None and options.buildContext is None if useDefaultLocations: imageSourceDir = os.path.join(repo.getImageSourcesDir(), imageSourceToAdd) buildContext = os.path.join(imageSourceDir, "image") imageFile = os.path.join(buildContext, "SubuserImagefile") permissionsFile = os.path.join(imageSourceDir, "permissions.json") try: user.getEndUser().makedirs(buildContext) except OSError: pass else: if repo.getRepoConfig() is None: sys.exit( "You must initialize your repository with 'pkg init' before adding image sources to it." ) if options.buildContext is None or options.imageFile is None or options.permissionsFile is None: sys.exit( "If you specify non-default paths you must specify all of them. That is --image-file, --build-context AND --permissions-file. Cannot add image. Exiting..." ) imageFile = options.imageFile try: user.getEndUser().makedirs(os.path.dirname(imageFile)) except OSError: pass buildContext = options.buildContext try: user.getEndUser().makedirs(buildContext) except OSError: pass permissionsFile = options.permissionsFile try: user.getEndUser().makedirs(os.path.dirname(permissionsFile)) except OSError: pass repoConfig = repo.getRepoConfig() if not "explicit-image-sources" in repoConfig: repoConfig["explicit-image-sources"] = {} repoConfig["explicit-image-sources"][imageSourceToAdd] = { "image-file": imageFile, "build-context": buildContext, "permissions-file": permissionsFile } with user.getEndUser().get_file("./.subuser.json", "w") as subuserDotJson: json.dump(repoConfig, subuserDotJson, indent=1, separators=(",", ": ")) permissions = defaultPermissions (returncode, maintainerName, stderr) = user.getEndUser().callCollectOutput( ["git", "config", "user.name"]) subuserlib.print.printWithoutCrashing(stderr) (returncode, maintainerEmail, stderr) = user.getEndUser().callCollectOutput( ["git", "config", "user.email"]) subuserlib.print.printWithoutCrashing(stderr) permissions["maintainer"] = maintainerName.rstrip( "\n") + " <" + maintainerEmail.rstrip("\n") + ">" if not os.path.exists(permissionsFile): with user.getEndUser().get_file(permissionsFile, "w") as pf: json.dump(permissions, pf, indent=1, separators=(",", ": ")) while True: user.getEndUser().runEditor(permissionsFile) try: Permissions(user, initialPermissions=subuserlib.permissions.load( permissionsFilePath=permissionsFile), writePath=permissionsFile).save() break except SyntaxError as e: input(str(e) + "\nPress ENTER to edit the file again.") if not os.path.exists(imageFile): with user.getEndUser().get_file(imageFile, "w") as imgf: imgf.write(defaultImageFileTemplate) user.getEndUser().runEditor(imageFile) try: if input("Would you like to test your new image? [Y/n]") == "n": sys.exit(0) except EOFError: subuserlib.print.printWithoutCrashing("") if not subuserlib.test.testing: sys.exit(0) if subcommand == "test" or subcommand == "add": user = User() repo = subuserlib.resolve.getRepositoryFromPath( user, os.environ["PWD"]) permissionsAccepter = AcceptPermissionsAtCLI( user, alwaysAccept=options.accept) imageSourceNames = args[1:] with user.getRegistry().getLock() as lockFileHandler: subusers = [] subuserNamePrefix = "!test-image-subuser-" # Build the images for imageSourceName in imageSourceNames: subuserName = subuserNamePrefix + imageSourceName subuserlib.subuser.addFromImageSourceNoVerify( user, subuserName, repo[imageSourceName]) subusers.append(user.getRegistry().getSubusers()[subuserName]) subuserlib.verify.verify(user, subusers=subusers, permissionsAccepter=permissionsAccepter, prompt=options.prompt, useCache=True) subusersToTryBuildingAgain = None while not subusersToTryBuildingAgain == []: subusersToTryBuildingAgain = [] for subuser in subusers: if subuser.getImageId() is None: try: if not input(subuser.getName( ) + " failed to build. Edit its image file and try again? [Y/n]" ) == "n": subusersToTryBuildingAgain.append(subuser) user.getEndUser().runEditor( subuser.getImageSource().getImageFile()) except EOFError: subuserlib.print.printWithoutCrashing("") subuserlib.print.printWithoutCrashing( "Not editing and not trying again due to lack of terminal." ) if subusersToTryBuildingAgain: subuserlib.verify.verify( user, subusers=subusersToTryBuildingAgain, permissionsAccepter=permissionsAccepter, prompt=options.prompt, useCache=True) user.getRegistry().commit( "subuser pkg %s\n%s" % (subcommand, "\n".join(imageSourceNames))) # Run the images for subuser in subusers: if subuser.getPermissions()["executable"] and subuser.getImageId(): try: arguments = input( "Running " + subuser.getName() + " enter arguments and press enter to continue:") except EOFError: subuserlib.print.printWithoutCrashing("") arguments = "" arguments = arguments.split(" ") if arguments == [""]: arguments = [] runtime = subuser.getRuntime(os.environ) if runtime: runtime.run(arguments) with user.getRegistry().getLock() as lockFileHandler: # Remove the subusers user = User() subuserlib.subuser.remove(user, subusers) user.getRegistry().commit( "subuser pkg: Remove temp subusers after test.")
def pkg(realArgs): """ Run packaging subcommands. """ options, args = parseCliArgs(realArgs) user = User() subcommand = args[0] if subcommand == "init": if not os.path.exists("./.subuser.json"): repoConfig = {} if options.imageSourcesDir: repoConfig["image-sources-dir"] = options.imageSourcesDir with open("./.subuser.json", "w") as subuserDotJson: json.dump(repoConfig, subuserDotJson) if options.imageSourcesDir: user.getEndUser().makedirs(options.imageSourcesDir) user.getEndUser().chown("./.subuser.json") print("Subuser repository initialized successfully!") print("You can add new image sources with:") print("$ subuser pkg add image-source-name") else: sys.exit("Subuser repository already initialized. Exiting.") if subcommand == "add": repo = subuserlib.resolve.getRepositoryFromPath( user, os.environ["PWD"]) imageSourceToAdd = args[1] if imageSourceToAdd in repo: sys.exit( "An image source named " + imageSourceToAdd + " is already present in the repository. Cannot add. Exiting.") useDefaultLocations = options.imageFile is None and options.permissionsFile is None and options.buildContext is None if useDefaultLocations: imageSourceDir = os.path.join(repo.getImageSourcesDir(), imageSourceToAdd) buildContext = os.path.join(imageSourceDir, "image") imageFile = os.path.join(buildContext, "SubuserImagefile") permissionsFile = os.path.join(imageSourceDir, "permissions.json") try: user.getEndUser().makedirs(buildContext) except OSError: pass else: if options.buildContext is None or options.imageFile is None or options.permissionsFile is None: sys.exit( "If you specify non-default paths you must specify all of them. That is --image-file, --build-context AND --permissions-file. Cannot add image. Exiting..." ) imageFile = options.imageFile try: user.getEndUser().makedirs(os.path.dirname(imageFile)) except OSError: pass buildContext = options.buildContext try: user.getEndUser().makedirs(os.path.dirname(buildContext)) except OSError: pass permissionsFile = options.permissionsFile try: user.getEndUser().makedirs(os.path.dirname(permissionsFile)) except OSError: pass repoConfig = repo.getRepoConfig() if not "explicit-image-sources" in repoConfig: repoConfig["explicit-image-sources"] = {} repoConfig["explicit-image-sources"][imageSourceToAdd] = { "image-file": imageFile, "build-context": buildContext, "permissions-file": permissionsFile } with open("./.subuser.json", "w") as subuserDotJson: json.dump(repoConfig, subuserDotJson, indent=1, separators=(",", ": ")) permissions = copy.deepcopy(subuserlib.permissions.defaults) (returncode, maintainerName) = subuserlib.subprocessExtras.callCollectOutput( ["git", "config", "user.name"]) (returncode, maintainerEmail) = subuserlib.subprocessExtras.callCollectOutput( ["git", "config", "user.email"]) permissions["maintainer"] = maintainerName.rstrip( "\n") + " <" + maintainerEmail.rstrip("\n") + ">" if not os.path.exists(permissionsFile): with open(permissionsFile, "w") as pf: json.dump(permissions, pf, indent=1, separators=(",", ": ")) subuserlib.subprocessExtras.runEditor(permissionsFile) Permissions(user, initialPermissions=subuserlib.permissions.load( permissionsFilePath=permissionsFile), writePath=permissionsFile).save() user.getEndUser().chown(permissionsFile) if not os.path.exists(imageFile): with open(imageFile, "w") as imgf: imgf.write("""FROM-SUBUSER-IMAGE libx11@default RUN apt-get update && apt-get upgrade -y && apt-get install -y PKG""") user.getEndUser().chown(imageFile) subuserlib.subprocessExtras.runEditor(imageFile) if raw_input("Would you like to test your new image? [Y/n]") == "n": sys.exit(0) if subcommand == "test" or subcommand == "add": user = User() repo = subuserlib.resolve.getRepositoryFromPath( user, os.environ["PWD"]) permissionsAccepter = AcceptPermissionsAtCLI( user, alwaysAccept=options.accept) imageSourceNames = args[1:] with user.getRegistry().getLock() as lockFileHandler: subuserNames = [] subuserNamePrefix = "!test-image-subuser-" # Build the images for imageSourceName in imageSourceNames: subuserName = subuserNamePrefix + imageSourceName subuserNames.append(subuserName) subuserlib.subuser.addFromImageSourceNoVerify( user, subuserName, repo[imageSourceName]) subuserlib.verify.verify(user, subuserNames=subuserNames, permissionsAccepter=permissionsAccepter, prompt=options.prompt) for subuserName in subuserNames: if user.getRegistry().getSubusers()[subuserName].getImageId( ) is None and not raw_input( subuserName + " failed to build. Edit its image file and try again? [Y/n]" ) == "n": subuserlib.subprocessExtras.runEditor( user.getRegistry().getSubusers() [subuserName].getImageSource().getImageFile()) subuserlib.verify.verify( user, subuserNames=[subuserName], permissionsAccepter=permissionsAccepter, prompt=options.prompt) user.getRegistry().commit() # Create a list of the names of the new subusers subuserNames = [] for imageSourceName in imageSourceNames: subuserNames.append(subuserNamePrefix + imageSourceName) # Run the images for subuserName in subuserNames: arguments = raw_input( "Running " + subuserName + " enter arguments and press enter to continue:") arguments = arguments.split(" ") if arguments == [""]: arguments = [] subuser = user.getRegistry().getSubusers()[subuserName] if subuser.getPermissions()["executable"]: runtime = subuser.getRuntime(os.environ) if runtime: runtime.run(arguments) with user.getRegistry().getLock() as lockFileHandler: # Remove the subusers user = User() subuserlib.subuser.remove(user, subuserNames) user.getRegistry().commit()