Esempio n. 1
0
def runCommand(sysargs):
  """
  Manage named subuser repositories.
  """
  options,args = parseCliArgs(sysargs)
  user = User()
  try:
    action = args[0]
  except IndexError:
    sys.exit("Use subuser repository --help for help.")
  if action == "add":
    if not len(args) == 3:
      sys.exit("Use subuser repository --help for help.")
    name = args[1]
    url = args[2]
    with user.getRegistry().getLock():
      subuserlib.repository.add(user,name,url)
  elif action == "remove":
    if not len(args) == 2:
      sys.exit("Use subuser repository --help for help.")
    name = args[1]
    with user.getRegistry().getLock():
      subuserlib.repository.remove(user,name)
  else:
     sys.exit("Action "+args[0]+" not supported. Please see:\n subuser repository --help")
Esempio n. 2
0
def lockSubusers(operation,commit):
  """
  Lock the subusers to the images and permissions they had at a given registry commit.
  """
  user = operation.user
  from subuserlib.classes.user import User
  from subuserlib.classes.registry import Registry
  oldUser = User(name=user.name,homeDir=user.homeDir)
  oldUser.registry = Registry(oldUser,gitReadHash=commit,ignoreVersionLocks=True,initialized=True)
  if not oldUser.registry.gitRepository.doesCommitExist(commit):
    sys.exit("Commit "+commit+" does not exist. Cannot lock to commit.")
  oldSubusers = []
  for subuser in operation.subusers:
    try:
      oldSubusers.append((subuser, oldUser.registry.subusers[subuser.name]))
    except KeyError:
      sys.exit("Subuser, "+subuser.name+" did not exist yet at commit "+commit+". Cannot lock to commit.")
  for (subuser, oldSubuser) in oldSubusers:
    subuser.imageId = oldSubuser.imageId
    oldSubuser.permissions.save(_have_lock=True)
    oldSubuser.getPermissionsTemplate().save(_have_lock=True)
    subuser.locked = True
    user.registry.logChange("Locking subuser "+subuser.name+" to commit: "+commit)
    user.registry.logChange("New image id is "+subuser.imageId)
  subuserlib.verify.verify(user.operation)
  user.registry.commit()
Esempio n. 3
0
def runCommand(sysargs):
    """
  Manage named subuser repositories.
  """
    options, args = parseCliArgs(sysargs)
    user = User()
    try:
        action = args[0]
    except IndexError:
        sys.exit("Use subuser repository --help for help.")
    if action == "add":
        if not len(args) == 3:
            sys.exit("Use subuser repository --help for help.")
        name = args[1]
        url = args[2]
        with user.getRegistry().getLock():
            subuserlib.repository.add(user, name, url)
    elif action == "remove":
        if not len(args) == 2:
            sys.exit("Use subuser repository --help for help.")
        name = args[1]
        with user.getRegistry().getLock():
            subuserlib.repository.remove(user, name)
    else:
        sys.exit("Action " + args[0] +
                 " not supported. Please see:\n subuser repository --help")
Esempio n. 4
0
def runCommand(realArgs):
  options,arguments=parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  with user.getRegistry().getLock() as LockFileHandle:
    subusers = user.getRegistry().getSubusers().getSortedList()
    subuserlib.verify.verify(user,subusers=subusers,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
    user.getRegistry().commit()
Esempio n. 5
0
def runCommand(realArgs):
  """
  Interact with the subuser registry.
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  if len(args) < 1:
    sys.exit("No arguments given. Please use subuser registry -h for help.")
  elif ["log"] == args:
    subuserlib.registry.showLog(user)
  elif "rollback" == args[0]:
    try:
      commit = args[1]
    except KeyError:
      sys.exit("Wrong number of arguments.  Expected a commit.  Try running \nsubuser regsitry --help\nfor more info.")
    with user.getRegistry().getLock():
      if not user.getRegistry().getGitRepository().doesCommitExist(commit):
        sys.exit("The commit "+commit+" does not exist. Use --help for help.")
      subuserlib.registry.rollback(user,commit=commit)
  elif ["live-log"] == args:
    liveLogDir = os.path.join(user.homeDir,".subuser/registry-live-log")
    liveLogPath = os.path.join(liveLogDir,str(os.getpid()))
    if not os.path.exists(liveLogDir):
      os.makedirs(liveLogDir)
    os.mkfifo(liveLogPath)
    # Why use os.open? http://stackoverflow.com/questions/5782279/why-does-a-read-only-open-of-a-named-pipe-block
    liveLog = os.open(liveLogPath,os.O_RDONLY|os.O_NONBLOCK)
    q = False
    line = ""
    while not q:
      ready,_,_ = select.select([sys.stdin,liveLog],[],[])
      for selection in ready:
        if selection == liveLog:
          line += os.read(liveLog,1)
          try:
            announcement = json.loads(line)
            if options.json:
              print(line)
            else:
              print("New commit to registry:" + announcement["commit"])
            line = ""
          except ValueError:
            pass
        elif selection == sys.stdin:
          stdinLine = sys.stdin.readline()
          if "q" in stdinLine or not stdinLine:
            q = True
            print("Quitting...")
            break
        else:
          raise Exception("IMPOSSIBLE!"+str(selection))
    os.close(liveLog)
    os.remove(liveLogPath)
    sys.exit()
  elif len(args) == 1:
    sys.exit(" ".join(args) + " is not a valid registry subcommand. Please use subuser registry -h for help.")
  else:
    sys.exit(" ".join(args) + " is not a valid registry subcommand. Please use subuser registry -h for help.")
Esempio n. 6
0
def registry(realArgs):
  """
  Interact with the subuser registry.
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  if len(args) < 1:
    sys.exit("No arguments given. Please use subuser registry -h for help.")
  elif ["log"] == args:
    subuserlib.registry.showLog(user)
  elif "rollback" == args[0]:
    try:
      commit = args[1]
    except KeyError:
      sys.exit("Wrong number of arguments.  Expected a commit.  Try running \nsubuser regsitry --help\nfor more info.")
    with user.getRegistry().getLock():
      subuserlib.registry.rollback(user,commit=commit)
  elif ["live-log"] == args:
    liveLogPath=os.path.join(user.homeDir,".subuser/registry-live-log")
    try:
      os.mkfifo(liveLogPath)
    except OSError:
      pass
    # Why use os.open? http://stackoverflow.com/questions/5782279/why-does-a-read-only-open-of-a-named-pipe-block
    liveLog = os.open(liveLogPath, os.O_RDONLY|os.O_NONBLOCK)
    liveLog = os.fdopen(liveLog)
    q = False
    while not q:
      ready,_,_ = select.select([sys.stdin,liveLog],[],[])
      for selection in ready:
        if selection == liveLog:
          line = liveLog.readline()
          if not line:
            liveLog.close()
            liveLog = os.open(liveLogPath, os.O_RDONLY|os.O_NONBLOCK)
            liveLog = os.fdopen(liveLog)
            break
          if options.json:
            print(line)
          else:
            try:
              announcement = json.loads(line)
              print(announcement["commit"])
            except ValueError:
              pass
        elif selection == sys.stdin:
          if "q" in sys.stdin.readline():
            q = True
            break
        else:
          raise Exception("IMPOSSIBLE!"+str(selection))
    liveLog.close()
    os.remove(liveLogPath)
    sys.exit()
  elif len(args) == 1:
    sys.exit(" ".join(args) + " is not a valid registry subcommand. Please use subuser registry -h for help.")
  else:
    sys.exit(" ".join(args) + " is not a valid registry subcommand. Please use subuser registry -h for help.")
Esempio n. 7
0
def verify(realArgs):
  options,arguments=parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  with user.getRegistry().getLock() as LockFileHandle:
    subuserNames = list(user.getRegistry().getSubusers().keys())
    subuserNames.sort()
    subuserlib.verify.verify(user,subuserNames=subuserNames,permissionsAccepter=permissionsAccepter)
    user.getRegistry().commit()
Esempio n. 8
0
def verify(realArgs):
  options,arguments=parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  try:
    with user.getRegistry().getLock() as LockFileHandle:
      subuserNames = list(user.getRegistry().getSubusers().keys())
      subuserNames.sort()
      subuserlib.verify.verify(user,subuserNames=subuserNames,permissionsAccepter=permissionsAccepter)
      user.getRegistry().commit()
  except subuserlib.portalocker.portalocker.LockException:
    sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
Esempio n. 9
0
def testImages(realArgs):
  """
  Test the given images.
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  try:
    with user.getRegistry().getLock() as lockFileHandler:
      subuserlib.testImages.testImages(user=user,sourceRepoId=args[0],imageSourceNames=args[1:],permissionsAccepter=permissionsAccepter)
  except subuserlib.portalocker.portalocker.LockException:
    sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
Esempio n. 10
0
File: ps.py Progetto: duerrp/subuser
def runCommand(realArgs):
  options,args = parseCliArgs(realArgs)
  user = User()
  runningImages = [container["Image"] for container in user.getDockerDaemon().getContainers(onlyRunning=True)]
  for _,subuser in user.getRegistry().getSubusers().items():
    try:
      if subuser.getRunReadyImage().getId() in runningImages:
        if not options.internal:
          if subuser.getName().startswith("!"):
            continue
        print(subuser.getName())
    except (KeyError,subuserlib.classes.subuserSubmodules.run.runtimeCache.NoRuntimeCacheForSubusersWhichDontHaveExistantImagesException):
      pass
Esempio n. 11
0
def runCommand(realArgs):
  options,args = parseCliArgs(realArgs)
  user = User()
  runningImages = [container["Image"] for container in user.getDockerDaemon().getContainers(onlyRunning=True)]
  for _,subuser in user.getRegistry().getSubusers().items():
    try:
      if subuser.getRunReadyImage().getId() in runningImages:
        if not options.internal:
          if subuser.getName().startswith("!"):
            continue
        print(subuser.getName())
    except (KeyError,subuserlib.classes.subuserSubmodules.run.runtimeCache.NoRuntimeCacheForSubusersWhichDontHaveExistantImagesException):
      pass
Esempio n. 12
0
def subuser(sysargs):
  """
  Manage subusers
  """
  options,args = parseCliArgs(sysargs)
  try:
    action = args[0]
  except IndexError:
    print("Wrong number of arguments!")
    parseCliArgs(["--help"])
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  if action == "add":
    if not len(args) == 3:
      sys.exit("Wrong number of arguments to add.  See `subuser subuser -h`.")
    subuserName = args[1]
    imageSourceId = args[2]
    with user.getRegistry().getLock():
      subuserlib.subuser.add(user,subuserName,imageSourceId,permissionsAccepter=permissionsAccepter,prompt=options.prompt,forceInternal=options.forceInternal)
  else:
    subuserNames = args[1:]
    with user.getRegistry().getLock():
      subusers = []
      if not options.prefix is None:
        allSubuserNames = user.getRegistry().getSubusers().keys()
        subuserNames.extend([subuserName for subuserName in allSubuserNames if subuserName.startswith(options.prefix)])

      for subuserName in subuserNames:
        try:
          subusers.append(user.getRegistry().getSubusers()[subuserName])
        except KeyError:
          sys.exit("Subuser "+subuserName+" does not exist. Use --help for help.")
      if subusers == []:
        sys.exit("No subusers specified. Use --help for help.")
      if action == "remove":
          subuserlib.subuser.remove(user,subusers)
          sys.exit()
      addAndRemoveCommands = [("add-to-path","remove-from-path",subuserlib.subuser.setExecutableShortcutInstalled),("expose-entrypoints","hide-entrypoints",lambda user,subusers,install:subuserlib.subuser.setEntrypointsExposed(user,subusers,install,permissionsAccepter))]
      for add,remove,command in addAndRemoveCommands:
        if action == add or action == remove:
          if action == add:
            install = True
          elif action == remove:
            install = False
          command(user,subusers,install)
          sys.exit()
      if action == "edit-permissions":
        user.getRegistry().logChange("Edit the permissions of:"+ " ".join(subuserNames))
        for subuser in subusers:
          subuser.editPermissionsCLI()
        subuserlib.verify.verify(user,subusers=subusers,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
        user.getRegistry().commit()
        sys.exit()
      sys.exit("Action "+args[0]+" does not exist. Try:\n subuser subuser --help")
Esempio n. 13
0
def runCommand(realArgs):
  """
  Remove images that are installed, but are not associated with any subusers.
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  with user.getRegistry().getLock() as lockFileHandler:
    if not options.repoId is None:
      if not options.repoId in user.getRegistry().getRepositories():
        repo = subuserlib.resolve.lookupRepositoryByURI(user,options.repoId)
        if repo is None:
          sys.exit("The repository <"+options.repoId+"> does not exist.")
    else:
      repo = None
    subuserlib.removeOldImages.removeOldImages(user=user,dryrun=options.dryrun,yes=options.yes,sourceRepo=repo,imageSourceName=options.imageSourceName)
Esempio n. 14
0
def update(realArgs):
  """
  Update your subuser installation.
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  if len(args) < 1:
    sys.exit("No arguments given. Please use subuser update -h for help.")
  elif ["all"] == args:
    with user.getRegistry().getLock():
      subuserlib.update.all(user,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
  elif "subusers" == args[0]:
    subuserNamesToUpdate = args[1:]
    subusersToUpdate = []
    for subuserName in subuserNamesToUpdate:
      try:
        subusersToUpdate.append(user.getRegistry().getSubusers()[subuserName])
      except KeyError:
        sys.exit("Subuser "+subuserName+" does not exist. Use --help for help.")
    if subusersToUpdate:
      with user.getRegistry().getLock():
        subuserlib.update.subusers(user,subusers=subusersToUpdate,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
    else:
      sys.exit("You did not specify any subusers to be updated. Use --help for help. Exiting...")
  elif "lock-subuser-to" == args[0]:
    try:
      subuserName = args[1]
      commit = args[2]
    except IndexError:
      sys.exit("Wrong number of arguments.  Expected a subuser name and a commit.  Try running\nsubuser update --help\n for more info.")
    with user.getRegistry().getLock():
      try:
        subuser = user.getRegistry().getSubusers()[subuserName]
      except KeyError:
        sys.exit("Subuser "+subuserName+" does not exist and therefore cannot be locked. Use --help for help.")
      subuserlib.update.lockSubuser(user,subuser=subuser,commit=commit)
  elif "unlock-subuser" == args[0]:
    try:
      subuserName = args[1]
    except IndexError:
      sys.exit("Wrong number of arguments.  Expected a subuser's name. Try running\nsubuser update --help\nfor more information.")
    try:
      subuser = user.getRegistry().getSubusers()[subuserName]
    except KeyError:
      sys.exit("Subuser "+subuserName+" does not exist. Cannot lock. Use --help for help.")
    with user.getRegistry().getLock():
      subuserlib.update.unlockSubuser(user,subuser=subuser,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
  elif len(args) == 1:
    sys.exit(" ".join(args) + " is not a valid update subcommand. Please use subuser update -h for help.")
  else:
    sys.exit(" ".join(args) + " is not a valid update subcommand. Please use subuser update -h for help.")
Esempio n. 15
0
def runCommand(args):
  preArgs = []
  argParser = ArgParser()
  for arg in args:
    argParser.readArg(arg)

  if not argParser.consumedSubuserName:
    print("Subuser name not listed.")
    parseCliArgs(["--help"])

  options,_ = parseCliArgs(argParser.preArgs)

  user = User()
  if not "SUBUSER_VERBOSITY" in os.environ:
    user.getRegistry().setLogOutputVerbosity(0)
  if argParser.subuserName in user.getRegistry().getSubusers():
    try:
      extraDockerFlags = os.environ["SUBUSER_EXTRA_DOCKER_ARGS"].split(" ")
    except KeyError:
      extraDockerFlags = []
    try:
      subuser = user.getRegistry().getSubusers()[argParser.subuserName]
      runtime = subuser.getRuntime(os.environ,extraDockerFlags=extraDockerFlags,entrypoint=options.entrypoint)
      if runtime:
        if not options.dry:
          runtime.run(argParser.subuserArgs)
        else:
          if subuser.getImageId():
            print("The image will be prepared using the Dockerfile:")
            print(subuser.getRunReadyImage().generateImagePreparationDockerfile())
            print("The command to launch the image is:")
            print(runtime.getPrettyCommand(argParser.subuserArgs))
          else:
            sys.exit("There is no installed image for this subuser. Cannot run.")
      else:
        sys.exit("The subuser's image failed to build. Please use the subuser registry log and subuser repair commands for more information.")
    except (subuserlib.classes.subuser.SubuserHasNoPermissionsException,subuserlib.classes.subuserSubmodules.run.runtimeCache.NoRuntimeCacheForSubusersWhichDontHaveExistantImagesException) as e:
      sys.exit(str(e))
  else:
    sys.exit(argParser.subuserName + " not found.\nUse --help for help.")
Esempio n. 16
0
def lockSubuser(user, subuser, commit):
    """
  Lock the subuser to the image and permissions that it had at a given registry commit.
  """
    from subuserlib.classes.user import User
    from subuserlib.classes.registry import Registry
    oldUser = User(name=user.name, homeDir=user.homeDir)
    oldUser.setRegistry(
        Registry(oldUser,
                 gitReadHash=commit,
                 ignoreVersionLocks=True,
                 initialized=True))
    if not oldUser.getRegistry().getGitRepository().doesCommitExist(commit):
        sys.exit("Commit " + commit +
                 " does not exist. Cannot lock to commit.")
    try:
        oldSubuser = oldUser.getRegistry().getSubusers()[subuser.getName()]
    except KeyError:
        sys.exit("Subuser, " + subuser.getName() +
                 " did not exist yet at commit " + commit +
                 ". Cannot lock to commit.")
    subuser.setImageId(oldSubuser.getImageId())
    oldSubuser.getPermissions().save()
    oldSubuser.getPermissionsTemplate().save()
    user.getRegistry().logChange("Locking subuser " + subuser.getName() +
                                 " to commit: " + commit)
    user.getRegistry().logChange("New image id is " + subuser.getImageId())
    subuser.setLocked(True)
    subuserlib.verify.verify(user)
    user.getRegistry().commit()
Esempio n. 17
0
def runCommand(realArgs):
    """
  Remove images that are installed, but are not associated with any subusers.
  """
    options, args = parseCliArgs(realArgs)
    user = User()
    with user.getRegistry().getLock() as lockFileHandler:
        if not options.repoId is None:
            if not options.repoId in user.getRegistry().getRepositories():
                repo = subuserlib.resolve.lookupRepositoryByURI(
                    user, options.repoId)
                if repo is None:
                    sys.exit("The repository <" + options.repoId +
                             "> does not exist.")
        else:
            repo = None
        subuserlib.removeOldImages.removeOldImages(
            user=user,
            dryrun=options.dryrun,
            yes=options.yes,
            sourceRepo=repo,
            imageSourceName=options.imageSourceName)
Esempio n. 18
0
def lockSubuser(user,subuser,commit):
  """
  Lock the subuser to the image and permissions that it had at a given registry commit.
  """
  from subuserlib.classes.user import User
  from subuserlib.classes.registry import Registry
  oldUser = User(name=user.name,homeDir=user.homeDir)
  oldUser.registry = Registry(oldUser,gitReadHash=commit,ignoreVersionLocks=True,initialized=True)
  if not oldUser.registry.gitRepository.doesCommitExist(commit):
    sys.exit("Commit "+commit+" does not exist. Cannot lock to commit.")
  try:
    oldSubuser = oldUser.registry.subusers[subuser.name]
  except KeyError:
    sys.exit("Subuser, "+subuser.name+" did not exist yet at commit "+commit+". Cannot lock to commit.")
  subuser.imageId = oldSubuser.imageId
  oldSubuser.permissions.save(_have_lock=True)
  oldSubuser.getPermissionsTemplate().save(_have_lock=True)
  user.registry.logChange("Locking subuser "+subuser.name+" to commit: "+commit)
  user.registry.logChange("New image id is "+subuser.imageId)
  subuser.locked = True
  subuserlib.verify.verify(user)
  user.registry.commit()
Esempio n. 19
0
def run(args):
  if len(args) == 1 or args[1] == "-h" or args[1] == "--help":
    print(helpString)
    sys.exit()

  subuserName = args[1]
  argsToPassToImage = args[2:]

  user = User()
  user.getRegistry().setLogOutputVerbosity(0)
  if subuserName in user.getRegistry().getSubusers():
    try:
      extraDockerFlags = os.environ["SUBUSER_EXTRA_DOCKER_ARGS"].split(" ")
    except KeyError:
      extraDockerFlags = []
    runtime = user.getRegistry().getSubusers()[subuserName].getRuntime(os.environ,extraDockerFlags=extraDockerFlags)
    if runtime:
      runtime.run(argsToPassToImage)
    else:
      sys.exit("The subuser's image failed to build. Please use the subuser registry log and subuser repair commands for more information.")
  else:
    sys.exit(subuserName + " not found.\n"+helpString)
Esempio n. 20
0
def verify(realArgs):
  options,arguments=parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  with user.getRegistry().getLock() as LockFileHandle:
    subuserNames = list(user.getRegistry().getSubusers().keys())
    subuserNames.sort()
    subuserlib.verify.verify(user,subuserNames=subuserNames,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
    user.getRegistry().commit()
Esempio n. 21
0
def runCommand(realArgs):
    options, args = parseCliArgs(realArgs)
    user = User()
    runningImages = [
        container["Image"]
        for container in user.dockerDaemon.getContainers(onlyRunning=True)
    ]
    for _, subuser in user.registry.subusers.items():
        try:
            if subuser.getRunReadyImage().id in runningImages:
                if options.internal or not subuser.internal:
                    print(subuser.name)
        except (KeyError,
                subuserlib.classes.subuserSubmodules.run.runtimeCache.
                NoRuntimeCacheForSubusersWhichDontHaveExistantImagesException):
            pass
Esempio n. 22
0
def runCommand(realArgs):
    options, arguments = parseCliArgs(realArgs)
    user = User()
    user.getRegistry().commit_message = " ".join(["subuser", "repair"] +
                                                 realArgs)
    permissionsAccepter = AcceptPermissionsAtCLI(user,
                                                 alwaysAccept=options.accept)
    with user.getRegistry().getLock() as LockFileHandle:
        subusers = user.getRegistry().getSubusers().getSortedList()
        subuserlib.verify.verify(user,
                                 subusers=subusers,
                                 permissionsAccepter=permissionsAccepter,
                                 prompt=options.prompt)
        user.getRegistry().commit()
Esempio n. 23
0
def runCommand(args):
    preArgs = []
    argParser = ArgParser()
    for arg in args:
        argParser.readArg(arg)

    if not argParser.consumedSubuserName:
        print("Subuser name not listed.")
        parseCliArgs(["--help"])

    options, _ = parseCliArgs(argParser.preArgs)

    user = User()
    if not "SUBUSER_VERBOSITY" in os.environ:
        user.getRegistry().setLogOutputVerbosity(0)
    if argParser.subuserName in user.getRegistry().getSubusers():
        try:
            extraDockerFlags = os.environ["SUBUSER_EXTRA_DOCKER_ARGS"].split(
                " ")
        except KeyError:
            extraDockerFlags = []
        try:
            subuser = user.getRegistry().getSubusers()[argParser.subuserName]
            runtime = subuser.getRuntime(os.environ,
                                         extraDockerFlags=extraDockerFlags,
                                         entrypoint=options.entrypoint)
            if runtime:
                if not options.dry:
                    runtime.run(argParser.subuserArgs)
                else:
                    if subuser.getImageId():
                        print(
                            "The image will be prepared using the Dockerfile:")
                        print(subuser.getRunReadyImage().
                              generateImagePreparationDockerfile())
                        print("The command to launch the image is:")
                        print(runtime.getPrettyCommand(argParser.subuserArgs))
                    else:
                        sys.exit(
                            "There is no installed image for this subuser. Cannot run."
                        )
            else:
                sys.exit(
                    "The subuser's image failed to build. Please use the subuser registry log and subuser repair commands for more information."
                )
        except (subuserlib.classes.subuser.SubuserHasNoPermissionsException,
                subuserlib.classes.subuserSubmodules.run.runtimeCache.
                NoRuntimeCacheForSubusersWhichDontHaveExistantImagesException
                ) as e:
            sys.exit(str(e))
    else:
        sys.exit(argParser.subuserName + " not found.\nUse --help for help.")
Esempio n. 24
0
def runCommand(realArgs):
    """
  >>> import version #import self
  >>> version.runCommand([])
  Subuser version: 0.5
  Docker info:
   Foo: bar
  """
    user = User()
    (options, args) = parseCliArgs(realArgs)
    if options.json:
        print(
            json.dumps(subuserlib.version.getInfo(user),
                       indent=1,
                       separators=(",", ": ")))
    else:
        print("Subuser version: " + subuserlib.version.getSubuserVersion(user))
        print("Docker info:")
        for key, value in subuserlib.version.getDockerInfo(user).items():
            print(" " + key + ": " + str(value))
Esempio n. 25
0
def verify(realArgs):
    options, arguments = parseCliArgs(realArgs)
    user = User()
    permissionsAccepter = AcceptPermissionsAtCLI(user,
                                                 alwaysAccept=options.accept)
    try:
        with user.getRegistry().getLock() as LockFileHandle:
            subuserNames = list(user.getRegistry().getSubusers().keys())
            subuserNames.sort()
            subuserlib.verify.verify(user,
                                     subuserNames=subuserNames,
                                     permissionsAccepter=permissionsAccepter)
            user.getRegistry().commit()
    except subuserlib.portalocker.portalocker.LockException:
        sys.exit(
            "Another subuser process is currently running and has a lock on the registry. Please try again later."
        )
Esempio n. 26
0
def run(args):
  if len(args) == 1 or args[1] == "-h" or args[1] == "--help":
    print(helpString)
    sys.exit()

  subuserName = args[1]
  argsToPassToImage = args[2:]

  user = User()
  user.getRegistry().setLogOutputVerbosity(0)
  if subuserName in user.getRegistry().getSubusers():
    runtime = user.getRegistry().getSubusers()[subuserName].getRuntime(os.environ)
    if runtime:
      runtime.run(argsToPassToImage)
    else:
      sys.exit("The subuser's image failed to build. Please use the subuser update log and subuser repair commands for more information.")
  else:
    sys.exit(subuserName + " not found.\n"+helpString)
Esempio n. 27
0
def run(args):
  if len(args) == 1 or args[1] == "-h" or args[1] == "--help":
    print(helpString)
    sys.exit()

  subuserName = args[1]
  argsToPassToImage = args[2:]

  user = User()
  user.getRegistry().setLogOutputVerbosity(0)
  if subuserName in user.getRegistry().getSubusers():
    try:
      extraDockerFlags = os.environ["SUBUSER_EXTRA_DOCKER_ARGS"].split(" ")
    except KeyError:
      extraDockerFlags = []
    runtime = user.getRegistry().getSubusers()[subuserName].getRuntime(os.environ,extraDockerFlags=extraDockerFlags)
    if runtime:
      runtime.run(argsToPassToImage)
    else:
      sys.exit("The subuser's image failed to build. Please use the subuser registry log and subuser repair commands for more information.")
  else:
    sys.exit(subuserName + " not found.\n"+helpString)
Esempio n. 28
0
def subuser(sysargs):
  """
  Manage subusers

  Tests
  -----

  **Setup:**

  >>> subuser = __import__("subuser-subuser") #import self
  >>> import subuserlib.classes.user

  At the start of our tests, the test environment has one subuser named ``foo``.

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set([u'foo'])
  True

  We add another subuser named ``bar``.

  >>> subuser.subuser(["add","--accept","bar","bar@file:///home/travis/remote-test-repo"])
  Adding subuser bar bar@file:///home/travis/remote-test-repo
  Adding new temporary repository file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  bar would like to have the following permissions:
   Description: bar
   Maintainer: fred
   Executable: /usr/bin/bar
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser bar is up to date.
  New images for the following subusers need to be installed:
  bar
  Installing bar ...
  Building...
  Building...
  Building...
  Successfully built 6
  Building...
  Building...
  Building...
  Successfully built 7
  Installed new image <7> for subuser bar
  Running garbage collector on temporary repositories...

  Now we have two subusers.

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set([u'foo', 'bar'])
  True

  We remove ``bar``.

  >>> subuser.subuser(["remove","bar"])
  Removing subuser bar
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  Now we only have one subuser.

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set([u'foo'])
  True

  We add another subuser named ``bar`` using a local folder rather than from a git repo.

  >>> subuser.subuser(["add","--accept","bar","bar@/home/travis/remote-test-repo"])
  Adding subuser bar bar@/home/travis/remote-test-repo
  Adding new temporary repository /home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  bar would like to have the following permissions:
   Description: bar
   Maintainer: fred
   Executable: /usr/bin/bar
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser bar is up to date.
  New images for the following subusers need to be installed:
  bar
  Installing bar ...
  Building...
  Building...
  Building...
  Successfully built 10
  Building...
  Building...
  Building...
  Successfully built 11
  Installed new image <11> for subuser bar
  Running garbage collector on temporary repositories...

  Now we have two subusers.

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set([u'foo', 'bar'])
  True

  We remove ``bar``.

  >>> subuser.subuser(["remove","bar"])
  Removing subuser bar
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  Now we only have one subuser.

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set([u'foo'])
  True

  If we try adding a subuser which fails to install do to a bad ``SubuserImagefile`` an error is displayed, a cleanup process occures, and nothing terribly bad happens.

  This works for syntax errors.

  >>> subuser.subuser(["add","--accept","broken-syntax","broken-syntax@file:///home/travis/remote-test-repo"])
  Adding subuser broken-syntax broken-syntax@file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  broken-syntax would like to have the following permissions:
   Description: broken-syntax
   Maintainer: fred
   Is a library.
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser broken-syntax is up to date.
  Error in broken-syntax's SubuserImagefile on line 0
   Subuser image does not exist: ""
  Images for the following subusers failed to build:
  broken-syntax
  Running garbage collector on temporary repositories...

  >>> subuser.subuser(["add","--accept","broken-non-existant-dependency","broken-non-existant-dependency@file:///home/travis/remote-test-repo"])
  Adding subuser broken-non-existant-dependency broken-non-existant-dependency@file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  broken-non-existant-dependency would like to have the following permissions:
   Description: broken-non-existant-dependency
   Maintainer: fred
   Is a library.
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser broken-non-existant-dependency is up to date.
  Error in broken-non-existant-dependency's SubuserImagefile on line 0
   Subuser image does not exist: "non-existant-I-do-not-exist!!!!!"
  Images for the following subusers failed to build:
  broken-non-existant-dependency
  Running garbage collector on temporary repositories...

  >>> subuser.subuser(["add","--accept","broken-permissions-file","broken-permissions-file@file:///home/travis/remote-test-repo"])
  Adding subuser broken-permissions-file broken-permissions-file@file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Checking if images need to be updated or installed...
  Error, user dir permissions may not contain system wide absolute paths. The user dir: "/etc/" is forbidden.
  Running garbage collector on temporary repositories...

  >>> subuser.subuser(["remove","broken-syntax","broken-non-existant-dependency","broken-permissions-file"])
  Removing subuser broken-syntax
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Removing subuser broken-non-existant-dependency
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Removing subuser broken-permissions-file
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...
  """
  options,args = parseCliArgs(sysargs)
  try:
    action = args[0]
  except IndexError:
    parseCliArgs(["--help"])
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  if action == "add":
    if not len(args) == 3:
      sys.exit("Wrong number of arguments to add.  See `subuser subuser -h`.")
    name = args[1]
    imageSourceId = args[2]
    with user.getRegistry().getLock():
      subuserlib.subuser.add(user,name,imageSourceId,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
  elif action == "remove":
    names = args[1:]
    if not options.prefix is None:
      allSubuserNames = user.getRegistry().getSubusers().keys()
      names.extend([subuserName for subuserName in allSubuserNames if subuserName.startswith(options.prefix)])
    with user.getRegistry().getLock():
      subuserlib.subuser.remove(user,names)
  elif action == "create-shortcut":
    name = args[1]
    with user.getRegistry().getLock():
      subuserlib.subuser.setExecutableShortcutInstalled(user,name,True)
  elif action == "remove-shortcut":
    name = args[1]
    with user.getRegistry().getLock():
      subuserlib.subuser.setExecutableShortcutInstalled(user,name,False)
  elif action == "edit-permissions":
    name = args[1]
    with user.getRegistry().getLock():
      user.getRegistry().logChange("Edit "+name+"'s permissions.")
      subuser = user.getRegistry().getSubusers()[name]
      subuser.editPermissionsCLI()
      subuserlib.verify.verify(user,subuserNames=[name],permissionsAccepter=permissionsAccepter,prompt=options.prompt)
      user.getRegistry().commit()
  else:
    sys.exit("Action "+args[0]+" does not exist. Try:\n subuser subuser --help")
Esempio n. 29
0
 def getUser():
   from subuserlib.classes.user import User
   return User(homeDir=homeDir)
Esempio n. 30
0
def registry(realArgs):
    """
  Interact with the subuser registry.
  """
    options, args = parseCliArgs(realArgs)
    user = User()
    if len(args) < 1:
        sys.exit(
            "No arguments given. Please use subuser registry -h for help.")
    elif ["log"] == args:
        subuserlib.registry.showLog(user)
    elif "rollback" == args[0]:
        try:
            commit = args[1]
        except KeyError:
            sys.exit(
                "Wrong number of arguments.  Expected a commit.  Try running \nsubuser regsitry --help\nfor more info."
            )
        with user.getRegistry().getLock():
            subuserlib.registry.rollback(user, commit=commit)
    elif ["live-log"] == args:
        liveLogPath = os.path.join(user.homeDir, ".subuser/registry-live-log")
        try:
            os.mkfifo(liveLogPath)
        except OSError:
            pass
        # Why use os.open? http://stackoverflow.com/questions/5782279/why-does-a-read-only-open-of-a-named-pipe-block
        liveLog = os.open(liveLogPath, os.O_RDONLY | os.O_NONBLOCK)
        liveLog = os.fdopen(liveLog)
        q = False
        while not q:
            ready, _, _ = select.select([sys.stdin, liveLog], [], [])
            for selection in ready:
                if selection == liveLog:
                    line = liveLog.readline()
                    if not line:
                        liveLog.close()
                        liveLog = os.open(liveLogPath,
                                          os.O_RDONLY | os.O_NONBLOCK)
                        liveLog = os.fdopen(liveLog)
                        break
                    if options.json:
                        print(line)
                    else:
                        try:
                            announcement = json.loads(line)
                            print(announcement["commit"])
                        except ValueError:
                            pass
                elif selection == sys.stdin:
                    if "q" in sys.stdin.readline():
                        q = True
                        break
                else:
                    raise Exception("IMPOSSIBLE!" + str(selection))
        liveLog.close()
        os.remove(liveLogPath)
        sys.exit()
    elif len(args) == 1:
        sys.exit(
            " ".join(args) +
            " is not a valid registry subcommand. Please use subuser registry -h for help."
        )
    else:
        sys.exit(
            " ".join(args) +
            " is not a valid registry subcommand. Please use subuser registry -h for help."
        )
Esempio n. 31
0
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)
      # Remove the subusers
      subuserlib.subuser.remove(user,subuserNames)
Esempio n. 32
0
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.")
Esempio n. 33
0
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()
def removeOldImages(realArgs):
    """
  Remove images that are installed, but are not associated with any subusers.

  Tests
  -----

  **Setup:**

  >>> remove_old_images = __import__("subuser-remove-old-images")#import self
  >>> subuser = __import__("subuser-subuser")
  >>> import subuserlib.classes.user

  Check our assumptions about what subusers are installed in the test environment.  We load a new user object each time we checked, because we are interested about whether the changes we want are present on disk.

  >>> user = User()
  >>> subuserList = list(user.getRegistry().getSubusers().keys())
  >>> subuserList.sort()
  >>> for sb in subuserList:
  ...   print(sb)
  foo

  Add a ``bar`` subuser, which we will then remove.  This will leave us with a leftover image.

  >>> subuser.subuser(["add","--accept","bar","bar@file:///home/travis/remote-test-repo"])
  Adding subuser bar bar@file:///home/travis/remote-test-repo
  Adding new temporary repository file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  bar would like to have the following permissions:
   Description: bar
   Maintainer: fred
   Executable: /usr/bin/bar
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser bar is up to date.
  New images for the following subusers need to be installed:
  bar
  Installing bar ...
  Building...
  Building...
  Building...
  Successfully built 5
  Building...
  Building...
  Building...
  Successfully built 6
  Installed new image <6> for subuser bar
  Running garbage collector on temporary repositories...

  Check to see if subuser ``bar`` was successfully added.

  >>> user = User()
  >>> subuserList = list(user.getRegistry().getSubusers().keys())
  >>> subuserList.sort()
  >>> for sb in subuserList:
  ...   print(sb)
  bar
  foo

  Check to see if the image for ``bar`` was also installed.

  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  bar
  foo

  Remove the ``bar`` subuser.

  >>> subuser.subuser(["remove","bar"])
  Removing subuser bar
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  See that the image for ``bar`` was indeed left behind.

  >>> user = User()
  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  bar
  foo

  Use dry-run to see which images are to be deleted.

  >>> remove_old_images.removeOldImages(["--dry-run"])
  The following images are uneeded and would be deleted.
  DOCKER-ID : SUBUSER-ID
  Removing unneeded image 6 : bar@file:///home/travis/remote-test-repo

  Check to see that dry-run didn't actually remove the un-needed image.

  >>> user = User()
  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  bar
  foo

  Add another subuser blah

  >>> subuser.subuser(["add","--accept","blah","blah@/home/travis/local-test-repo"])
  Adding subuser blah blah@/home/travis/local-test-repo
  Adding new temporary repository /home/travis/local-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  blah would like to have the following permissions:
   Description: blah
   Maintainer: fred
   Executable: /usr/bin/foo
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser blah is up to date.
  New images for the following subusers need to be installed:
  blah
  Installing blah ...
  Building...
  Building...
  Building...
  Successfully built 8
  Building...
  Building...
  Building...
  Successfully built 9
  Installed new image <9> for subuser blah
  Running garbage collector on temporary repositories...

  Check to see if subuser ``blah`` was successfully added.

  >>> user = User()
  >>> subuserList = list(user.getRegistry().getSubusers().keys())
  >>> subuserList.sort()
  >>> for sb in subuserList:
  ...   print(sb)
  blah
  foo

  Check to see if the image for ``blah`` was also installed.

  >>> installedImageList = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImageList.sort()
  >>> for installedImage in installedImageList:
  ...   print(installedImage)
  bar
  blah
  foo

  Remove the ``blah`` subuser.

  >>> subuser.subuser(["remove","blah"])
  Removing subuser blah
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  See that the image for ``blah`` was indeed left behind.

  >>> user = User()
  >>> installedImageList = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImageList.sort()
  >>> for installedImage in installedImageList:
  ...   print(installedImage)
  bar
  blah
  foo

  Now we use ``remove-old-images`` to remove images which belong to the local repository.

  >>> remove_old_images.removeOldImages(["--repo=/home/travis/local-test-repo"])
  Removing unneeded image 9 : blah@/home/travis/local-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...
  Removing uneeded temporary repository: /home/travis/local-test-repo

  Now we use ``remove-old-images`` to clean up the rest of our un-needed installed images.

  >>> remove_old_images.removeOldImages([])
  Removing unneeded image 6 : bar@file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...
  Removing uneeded temporary repository: file:///home/travis/remote-test-repo

  And now the uneccesary ``bar`` image is gone.

  >>> user = User()
  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  foo

  """
    options, args = parseCliArgs(realArgs)
    user = User()
    with user.getRegistry().getLock() as lockFileHandler:
        if options.dryrun:
            print("The following images are uneeded and would be deleted.")
            print("DOCKER-ID : SUBUSER-ID")
        repoId = options.repo
        if not repoId is None:
            if not repoId in user.getRegistry().getRepositories():
                repo = subuserlib.resolve.lookupRepositoryByURI(
                    user, options.repo)
                if repo is None:
                    sys.exit("The repository <" + repoId + "> does not exist.")
                else:
                    repoId = repo.getName()
        subuserlib.removeOldImages.removeOldImages(user=user,
                                                   dryrun=options.dryrun,
                                                   sourceRepoId=repoId)
Esempio n. 35
0
File: pkg.py Progetto: xeor/subuser
def runCommand(realArgs):
    """
  Run packaging subcommands.
  """
    options, args = parseCliArgs(realArgs)
    try:
        subcommand = args[0]
        subcommands = ["init", "add"]
        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.endUser.get_file("./.subuser.json",
                                       "w") as subuserDotJson:
                json.dump(repoConfig, subuserDotJson)
                if options.imageSourcesDir:
                    user.endUser.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.imageSourcesDir,
                                          imageSourceToAdd)
            buildContext = os.path.join(imageSourceDir, "image")
            imageFile = os.path.join(buildContext, "SubuserImagefile")
            permissionsFile = os.path.join(imageSourceDir, "permissions.json")
            try:
                user.endUser.makedirs(buildContext)
            except OSError:
                pass
        else:
            if repo.repoConfig 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.endUser.makedirs(os.path.dirname(imageFile))
            except OSError:
                pass
            buildContext = options.buildContext
            try:
                user.endUser.makedirs(buildContext)
            except OSError:
                pass
            permissionsFile = options.permissionsFile
            try:
                user.endUser.makedirs(os.path.dirname(permissionsFile))
            except OSError:
                pass
            repoConfig = repo.repoConfig
            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.endUser.get_file("./.subuser.json",
                                       "w") as subuserDotJson:
                json.dump(repoConfig,
                          subuserDotJson,
                          indent=1,
                          separators=(",", ": "))
        permissions = defaultPermissions
        (returncode, maintainerName, stderr) = user.endUser.callCollectOutput(
            ["git", "config", "user.name"])
        subuserlib.print.printWithoutCrashing(stderr)
        (returncode, maintainerEmail, stderr) = user.endUser.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.endUser.get_file(permissionsFile, "w") as pf:
                json.dump(permissions, pf, indent=1, separators=(",", ": "))
        while True:
            user.endUser.runEditor(permissionsFile)
            try:
                permissions = subuserlib.permissions.load(
                    permissionsFilePath=permissionsFile, logger=user.registry)
                with user.endUser.get_file(permissionsFile, 'w') as fd:
                    fd.write(subuserlib.permissions.getJSONString(permissions))
                break
            except SyntaxError as e:
                input(str(e) + "\nPress ENTER to edit the file again.")
        if not os.path.exists(imageFile):
            with user.endUser.get_file(imageFile, "w") as imgf:
                imgf.write(defaultImageFileTemplate)
        user.endUser.runEditor(imageFile)
        subuserlib.print.printWithoutCrashing(
            "Your application has now been packaged. Run subuser dev %s to test your work. Use subuser dev --update %s to iterate."
            % (imageSourceToAdd, imageSourceToAdd))
Esempio n. 36
0
def repository(sysargs):
  """
  Manage named subuser repositories.

  Tests
  -----

  **Setup:**

  >>> import repository #import self

  Check our assumptions about the initial state of the test environment.

  >>> user = User()
  >>> set(user.getRegistry().getRepositories().keys()) == set([u'default'])
  True

  Add a new repository named ``remote-repo``.

  >>> repository.repository(["add","remote-repo","file:///home/travis/remote-test-repo"])
  Adding new repository remote-repo

  See that it was actually successfully added.

  >>> user = User()
  >>> set(user.getRegistry().getRepositories().keys()) == set([u'default', 'remote-repo'])
  True

  Remove the ``remote-repo`` repository.

  >>> repository.repository(["remove","remote-repo"])
  Removing repository remote-repo

  See that it was actually removed.

  >>> user = User()
  >>> set(user.getRegistry().getRepositories().keys()) == set([u'default'])
  True


  Add a new repository named ``local-repo`` which is just a folder on the local system.

  >>> repository.repository(["add","local-repo","/home/travis/remote-test-repo"])
  Adding new repository local-repo

  See that it was actually successfully added.

  >>> user = User()
  >>> set(user.getRegistry().getRepositories().keys()) == set([u'default', 'local-repo'])
  True

  Remove the ``local-repo`` repository.

  >>> repository.repository(["remove","local-repo"])
  Removing repository local-repo

  See that it was actually removed.

  >>> user = User()
  >>> set(user.getRegistry().getRepositories().keys()) == set([u'default'])
  True

  """
  options,args = parseCliArgs(sysargs)
  user = User()
  action = args[0]
  if action == "add":
    if not len(args) == 3:
      sys.exit("Use subuser repository --help for help.")
    name = args[1]
    url = args[2]
    try:
      with user.getRegistry().getLock():
        subuserlib.repository.add(user,name,url)
    except subuserlib.portalocker.portalocker.LockException:
      sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
  elif action == "remove":
    if not len(args) == 2:
      sys.exit("Use subuser repository --help for help.")
    name = args[1]
    try:
      with user.getRegistry().getLock():
        subuserlib.repository.remove(user,name)
    except subuserlib.portalocker.portalocker.LockException:
      sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
  else:
     sys.exit("Action "+args[0]+" not supported. Please see:\n subuser repository --help")
Esempio n. 37
0
def removeOldImages(realArgs):
  """
  Remove images that are installed, but are not associated with any subusers.

  Tests
  -----

  **Setup:**

  >>> remove_old_images = __import__("remove-old-images")#import self
  >>> import subuser
  >>> import subuserlib.classes.user

  Check our assumptions about what subusers are installed in the test environment.  We load a new user object each time we checked, because we are interested about whether the changes we want are present on disk.

  >>> user = User()
  >>> subuserList = list(user.getRegistry().getSubusers().keys())
  >>> subuserList.sort()
  >>> for sb in subuserList:
  ...   print(sb)
  foo

  Add a ``bar`` subuser, which we will then remove.  This will leave us with a leftover image.

  >>> subuser.subuser(["add","--accept","bar","bar@file:///home/travis/remote-test-repo"])
  Adding subuser bar bar@file:///home/travis/remote-test-repo
  Adding new temporary repository file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  bar would like to have the following permissions:
   Description: 
   Maintainer: 
   Executable: /usr/bin/bar
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser bar is up to date.
  Installing bar ...
  Building...
  Building...
  Building...
  Successfully built 5
  Building...
  Building...
  Building...
  Successfully built 6
  Installed new image <6> for subuser bar
  Running garbage collector on temporary repositories...

  Check to see if subuser ``bar`` was successfully added.

  >>> user = User()
  >>> subuserList = list(user.getRegistry().getSubusers().keys())
  >>> subuserList.sort()
  >>> for sb in subuserList:
  ...   print(sb)
  bar
  foo

  Check to see if the image for ``bar`` was also installed.

  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  bar
  foo

  Remove the ``bar`` subuser.

  >>> subuser.subuser(["remove","bar"])
  Removing subuser bar
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  See that the image for ``bar`` was indeed left behind.

  >>> user = User()
  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  bar
  foo

  Use dry-run to see which images are to be deleted.

  >>> remove_old_images.removeOldImages(["--dry-run"])
  The following images are uneeded and would be deleted.
  DOCKER-ID : SUBUSER-ID
  Removing unneeded image 6 : bar@file:///home/travis/remote-test-repo

  Check to see that dry-run didn't actually remove the un-needed image.

  >>> user = User()
  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  bar
  foo

  Add another subuser blah

  >>> subuser.subuser(["add","--accept","blah","blah@/home/travis/local-test-repo"])
  Adding subuser blah blah@/home/travis/local-test-repo
  Adding new temporary repository /home/travis/local-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  blah would like to have the following permissions:
   Description: 
   Maintainer: 
   Executable: /usr/bin/foo
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser blah is up to date.
  Installing blah ...
  Building...
  Building...
  Building...
  Successfully built 8
  Building...
  Building...
  Building...
  Successfully built 9
  Installed new image <9> for subuser blah
  Running garbage collector on temporary repositories...

  Check to see if subuser ``blah`` was successfully added.

  >>> user = User()
  >>> subuserList = list(user.getRegistry().getSubusers().keys())
  >>> subuserList.sort()
  >>> for sb in subuserList:
  ...   print(sb)
  blah
  foo

  Check to see if the image for ``blah`` was also installed.

  >>> installedImageList = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImageList.sort()
  >>> for installedImage in installedImageList:
  ...   print(installedImage)
  bar
  blah
  foo

  Remove the ``blah`` subuser.

  >>> subuser.subuser(["remove","blah"])
  Removing subuser blah
   If you wish to remove the subusers image, issue the command $ subuser remove-old-images
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  See that the image for ``blah`` was indeed left behind.

  >>> user = User()
  >>> installedImageList = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImageList.sort()
  >>> for installedImage in installedImageList:
  ...   print(installedImage)
  bar
  blah
  foo

  Now we use ``remove-old-images`` to remove images which belong to the local repository.

  >>> remove_old_images.removeOldImages(["--repo=/home/travis/local-test-repo"])
  Removing unneeded image 9 : blah@/home/travis/local-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...
  Removing uneeded temporary repository: /home/travis/local-test-repo

  Now we use ``remove-old-images`` to clean up the rest of our un-needed installed images.

  >>> remove_old_images.removeOldImages([])
  Removing unneeded image 6 : bar@file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...
  Removing uneeded temporary repository: file:///home/travis/remote-test-repo

  And now the uneccesary ``bar`` image is gone.

  >>> user = User()
  >>> installedImages = list([i.getImageSourceName() for i in user.getInstalledImages().values()])
  >>> installedImages.sort()
  >>> for installedImage in installedImages:
  ...   print(installedImage)
  foo
 
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  try:
    with user.getRegistry().getLock() as lockFileHandler:
      if options.dryrun:
        print("The following images are uneeded and would be deleted.")
        print("DOCKER-ID : SUBUSER-ID")
      repoId = options.repo
      if not repoId is None:
        if not repoId in user.getRegistry().getRepositories():
          repo = subuserlib.resolve.lookupRepositoryByURI(user,options.repo)
          if repo is None:
            sys.exit("The repository <"+repoId+"> does not exist.")
          else:
            repoId = repo.getName()
      subuserlib.removeOldImages.removeOldImages(user=user,dryrun=options.dryrun,sourceRepoId=repoId)
  except subuserlib.portalocker.portalocker.LockException:
    sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
Esempio n. 38
0
def runCommand(realArgs):
    """
  Interact with the subuser registry.
  """
    options, args = parseCliArgs(realArgs)
    user = User()
    if len(args) < 1:
        sys.exit(
            "No arguments given. Please use subuser registry -h for help.")
    elif ["log"] == args:
        subuserlib.registry.showLog(user)
    elif "rollback" == args[0]:
        try:
            commit = args[1]
        except KeyError:
            sys.exit(
                "Wrong number of arguments.  Expected a commit.  Try running \nsubuser regsitry --help\nfor more info."
            )
        with user.getRegistry().getLock():
            if not user.getRegistry().getGitRepository().doesCommitExist(
                    commit):
                sys.exit("The commit " + commit +
                         " does not exist. Use --help for help.")
            subuserlib.registry.rollback(user, commit=commit)
    elif ["live-log"] == args:
        liveLogDir = os.path.join(user.homeDir, ".subuser/registry-live-log")
        liveLogPath = os.path.join(liveLogDir, str(os.getpid()))
        if not os.path.exists(liveLogDir):
            os.makedirs(liveLogDir)
        os.mkfifo(liveLogPath)
        # Why use os.open? http://stackoverflow.com/questions/5782279/why-does-a-read-only-open-of-a-named-pipe-block
        liveLog = os.open(liveLogPath, os.O_RDONLY | os.O_NONBLOCK)
        q = False
        line = ""
        while not q:
            ready, _, _ = select.select([sys.stdin, liveLog], [], [])
            for selection in ready:
                if selection == liveLog:
                    line += os.read(liveLog, 1)
                    try:
                        announcement = json.loads(line)
                        if options.json:
                            print(line)
                        else:
                            print("New commit to registry:" +
                                  announcement["commit"])
                        line = ""
                    except ValueError:
                        pass
                elif selection == sys.stdin:
                    stdinLine = sys.stdin.readline()
                    if "q" in stdinLine or not stdinLine:
                        q = True
                        print("Quitting...")
                        break
                else:
                    raise Exception("IMPOSSIBLE!" + str(selection))
        os.close(liveLog)
        os.remove(liveLogPath)
        sys.exit()
    elif len(args) == 1:
        sys.exit(
            " ".join(args) +
            " is not a valid registry subcommand. Please use subuser registry -h for help."
        )
    else:
        sys.exit(
            " ".join(args) +
            " is not a valid registry subcommand. Please use subuser registry -h for help."
        )
Esempio n. 39
0
def runCommand(sysargs):
    """
  Manage subusers
  """
    options, args = parseCliArgs(sysargs)
    try:
        action = args[0]
    except IndexError:
        print("Wrong number of arguments!")
        parseCliArgs(["--help"])
    user = User()
    permissionsAccepter = AcceptPermissionsAtCLI(user,
                                                 alwaysAccept=options.accept)
    if action == "add":
        if not len(args) == 3:
            sys.exit(
                "Wrong number of arguments to add.  See `subuser subuser -h`.")
        subuserName = args[1]
        imageSourceId = args[2]
        with user.getRegistry().getLock():
            subuserlib.subuser.add(user,
                                   subuserName,
                                   imageSourceId,
                                   permissionsAccepter=permissionsAccepter,
                                   prompt=options.prompt,
                                   forceInternal=options.forceInternal)
    else:
        subuserNames = args[1:]
        with user.getRegistry().getLock():
            subusers = []
            if not options.prefix is None:
                allSubuserNames = user.getRegistry().getSubusers().keys()
                subuserNames.extend([
                    subuserName for subuserName in allSubuserNames
                    if subuserName.startswith(options.prefix)
                ])

            for subuserName in subuserNames:
                try:
                    subusers.append(
                        user.getRegistry().getSubusers()[subuserName])
                except KeyError:
                    sys.exit("Subuser " + subuserName +
                             " does not exist. Use --help for help.")
            if subusers == []:
                sys.exit("No subusers specified. Use --help for help.")
            if action == "remove":
                subuserlib.subuser.remove(user, subusers)
                sys.exit()
            addAndRemoveCommands = [
                ("add-to-path", "remove-from-path",
                 subuserlib.subuser.setExecutableShortcutInstalled),
                ("expose-entrypoints", "hide-entrypoints", lambda user,
                 subusers, install: subuserlib.subuser.setEntrypointsExposed(
                     user, subusers, install, permissionsAccepter))
            ]
            for add, remove, command in addAndRemoveCommands:
                if action == add or action == remove:
                    if action == add:
                        install = True
                    elif action == remove:
                        install = False
                    command(user, subusers, install)
                    sys.exit()
            if action == "edit-permissions":
                user.getRegistry().logChange("Edit the permissions of:" +
                                             " ".join(subuserNames))
                for subuser in subusers:
                    subuser.editPermissionsCLI()
                subuserlib.verify.verify(
                    user,
                    subusers=subusers,
                    permissionsAccepter=permissionsAccepter,
                    prompt=options.prompt)
                user.getRegistry().commit()
                sys.exit()
            sys.exit("Action " + args[0] +
                     " does not exist. Try:\n subuser subuser --help")
Esempio n. 40
0
def runCommand(realArgs):
    """
  Update your subuser installation.
  """
    options, args = parseCliArgs(realArgs)
    user = User()
    user.getRegistry().commit_message = " ".join(["subuser", "update"] +
                                                 realArgs)
    permissionsAccepter = AcceptPermissionsAtCLI(user,
                                                 alwaysAccept=options.accept)
    if len(args) < 1:
        sys.exit("No arguments given. Please use subuser update -h for help.")
    elif ["all"] == args:
        with user.getRegistry().getLock():
            subuserlib.update.all(user,
                                  permissionsAccepter=permissionsAccepter,
                                  prompt=options.prompt,
                                  useCache=options.useCache)
    elif "subusers" == args[0]:
        subuserNamesToUpdate = args[1:]
        subusersToUpdate = []
        for subuserName in subuserNamesToUpdate:
            try:
                subusersToUpdate.append(
                    user.getRegistry().getSubusers()[subuserName])
            except KeyError:
                sys.exit("Subuser " + subuserName +
                         " does not exist. Use --help for help.")
        if subusersToUpdate:
            with user.getRegistry().getLock():
                subuserlib.update.subusers(
                    user,
                    subusers=subusersToUpdate,
                    permissionsAccepter=permissionsAccepter,
                    prompt=options.prompt,
                    useCache=options.useCache)
        else:
            sys.exit(
                "You did not specify any subusers to be updated. Use --help for help. Exiting..."
            )
    elif "lock-subuser-to" == args[0]:
        try:
            subuserName = args[1]
            commit = args[2]
        except IndexError:
            sys.exit(
                "Wrong number of arguments.  Expected a subuser name and a commit.  Try running\nsubuser update --help\n for more info."
            )
        with user.getRegistry().getLock():
            try:
                subuser = user.getRegistry().getSubusers()[subuserName]
            except KeyError:
                sys.exit(
                    "Subuser " + subuserName +
                    " does not exist and therefore cannot be locked. Use --help for help."
                )
            subuserlib.update.lockSubuser(user, subuser=subuser, commit=commit)
    elif "unlock-subuser" == args[0]:
        try:
            subuserName = args[1]
        except IndexError:
            sys.exit(
                "Wrong number of arguments.  Expected a subuser's name. Try running\nsubuser update --help\nfor more information."
            )
        try:
            subuser = user.getRegistry().getSubusers()[subuserName]
        except KeyError:
            sys.exit("Subuser " + subuserName +
                     " does not exist. Cannot lock. Use --help for help.")
        with user.getRegistry().getLock():
            subuserlib.update.unlockSubuser(
                user,
                subuser=subuser,
                permissionsAccepter=permissionsAccepter,
                prompt=options.prompt)
    elif len(args) == 1:
        sys.exit(
            " ".join(args) +
            " is not a valid update subcommand. Please use subuser update -h for help."
        )
    else:
        sys.exit(
            " ".join(args) +
            " is not a valid update subcommand. Please use subuser update -h for help."
        )
Esempio n. 41
0
def update(realArgs):
  """
  Update your subuser installation.

  Tests
  -----

  **Setup:**

  >>> import os
  >>> import subuserlib.permissions
  >>> from subuserlib.classes.permissions import Permissions
  >>> update = __import__("subuser-update")
  >>> subuser = __import__("subuser-subuser")
  >>> repository = __import__("subuser-repository")
  >>> import subuserlib.classes.gitRepository

  Check initial test environment:

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set([u'foo'])
  True
  >>> set([i.getImageSourceName() for i in user.getInstalledImages().values()]) == set([u'foo', u'bar'])
  True

  Add a subuser who's image has a lot of dependencies.

  >>> subuser.subuser(["add","--accept","dependent","dependent@file:///home/travis/remote-test-repo"])
  Adding subuser dependent dependent@file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  dependent would like to have the following permissions:
   Description: a dependent
   Maintainer: 
   Is a library.
   Moderate permissions(These are probably safe):
    - user-dirs: To access to the following user directories: '~/Downloads'
    - sound-card: To access to your soundcard, can play sounds/record sound.
   Liberal permissions(These may pose a security risk):
    - x11: To display X11 windows and interact with your X11 server directly(log keypresses, read over your shoulder, steal your passwords, controll your computer ect.)
    - system-dirs: To read and write to the host's `/var/log` directory, mounted in the container as:`/var/log`
   WARNING: These permissions give the subuser full access to your system when run.
    - privileged: To have full access to your system.  To even do things as root outside of its container.
  A - Accept and apply changes
  E - Apply changes and edit result
  A
  Checking if images need to be updated or installed...
  Checking if subuser dependent is up to date.
  Installing dependency1 ...
  Building...
  Building...
  Building...
  Successfully built 13
  Building...
  Building...
  Building...
  Successfully built 14
  Installing intermediary ...
  Building...
  Building...
  Building...
  Successfully built 15
  Building...
  Building...
  Building...
  Successfully built 16
  Installing dependent ...
  Building...
  Building...
  Building...
  Successfully built 17
  Building...
  Building...
  Building...
  Successfully built 18
  Installed new image <18> for subuser dependent
  Running garbage collector on temporary repositories...

  Check that our new subuser was successfully added.

  >>> user = User()
  >>> subuserNamesBeforeUpdate = user.getRegistry().getSubusers().keys()
  >>> set(subuserNamesBeforeUpdate) == set(['dependent', u'foo'])
  True

  And that its image, along with all of its dependencies were added as well.

  >>> installedImagesBeforeUpdate = [i.getImageSourceName() for i in user.getInstalledImages().values()]
  >>> set(installedImagesBeforeUpdate) == set([u'foo', u'dependency1', u'bar', u'dependent', u'intermediary'])
  True

  Running update, when there is nothing to be updated, does nothing.

  >>> update.update(["all","--accept"])
  Updating...
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Checking if images need to be updated or installed...
  Checking if subuser dependent is up to date.
  Checking for updates to: dependency1@file:///home/travis/remote-test-repo
  Checking for updates to: intermediary@file:///home/travis/remote-test-repo
  Checking for updates to: dependent@file:///home/travis/remote-test-repo
  Checking if subuser foo is up to date.
  Checking for updates to: foo@default
  Running garbage collector on temporary repositories...

  The same subusers are still installed.

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set(subuserNamesBeforeUpdate)
  True

  And the same images too.

  >>> set([i.getImageSourceName() for i in user.getInstalledImages().values()]) == set(installedImagesBeforeUpdate)
  True

  However, if we change ``dependent``'s image source's permissions, the user is asked to approve the new permissions:

  >>> permissionsPath = "/home/travis/remote-test-repo/dependent/permissions.json"
  >>> permissions = Permissions(user,subuserlib.permissions.load(permissionsFilePath=permissionsPath),writePath=permissionsPath)
  >>> del permissions["sound-card"]
  >>> permissions["user-dirs"] = ["Images","Downloads"]
  >>> permissions.save()

  >>> try:
  ...   import urlparse
  ... except ImportError:
  ...   import urllib.parse as urlparse
  >>> repo1 = subuserlib.classes.gitRepository.GitRepository("/home/travis/remote-test-repo/")
  >>> repo1.run(["commit","-a","-m","changed dependent's permissions"])
  0

  >>> update.update(["all","--accept"])
  Updating...
  Updated repository file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  dependent would like to add/change the following permissions:
     - To access to the following user directories: '~/Images' '~/Downloads'
  dependent no longer needs the following permissions:
     - To access to your soundcard, can play sounds/record sound.
  A - Accept and apply changes
  E - Apply changes and edit result
  e - Ignore request and edit permissions by hand
  A
  Checking if images need to be updated or installed...
  Checking if subuser dependent is up to date.
  Checking for updates to: dependency1@file:///home/travis/remote-test-repo
  Checking for updates to: intermediary@file:///home/travis/remote-test-repo
  Checking for updates to: dependent@file:///home/travis/remote-test-repo
  Checking if subuser foo is up to date.
  Checking for updates to: foo@default
  Running garbage collector on temporary repositories...

  Now we change the ImageSource for the ``intermediary`` image.

  >>> with open("/home/travis/remote-test-repo/images/intermediary/image/SubuserImagefile",mode="w") as subuserImagefile:
  ...   _ = subuserImagefile.write("FROM-SUBUSER-IMAGE dependency2")

  And commit the changes to git.

  >>> repo1.run(["commit","-a","-m","changed dependency for intermediate from dependency1 to dependency2"])
  0

  Running an update after a change installs new images and registers them with their subusers.  But it does not delete the old ones.

  >>> update.update(["all","--accept"])
  Updating...
  Updated repository file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Checking if images need to be updated or installed...
  Checking if subuser dependent is up to date.
  Installing dependency2 ...
  Building...
  Building...
  Building...
  Successfully built 21
  Building...
  Building...
  Building...
  Successfully built 22
  Installing intermediary ...
  Building...
  Building...
  Building...
  Successfully built 23
  Building...
  Building...
  Building...
  Successfully built 24
  Installing dependent ...
  Building...
  Building...
  Building...
  Successfully built 25
  Building...
  Building...
  Building...
  Successfully built 26
  Installed new image <26> for subuser dependent
  Checking if subuser foo is up to date.
  Checking for updates to: foo@default
  Running garbage collector on temporary repositories...

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set(['dependent', u'foo'])
  True

  >>> set([i.getImageSourceName() for i in user.getInstalledImages().values()]) == set([u'foo', u'dependency1', u'bar', u'dependent', u'intermediary', u'intermediary', u'dependency2', u'dependent'])
  True

  Old images are not deleted so that update lock-subuser-to and update rollback still work. In this example, dependency1 stays installed.

  Now we lock the dependent subuser and try changing it again.

  >>> update.update(["lock-subuser-to","dependent","HEAD"])
  Locking subuser dependent to commit: HEAD
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Running garbage collector on temporary repositories...

  >>> with open("/home/travis/remote-test-repo/images/intermediary/image/SubuserImagefile",mode="w") as subuserImagefile:
  ...   _ = subuserImagefile.write("FROM-SUBUSER-IMAGE dependency3")

  And commit the changes to git.

  >>> repo1.run(["commit","-a","-m","changed dependency for intermediate from dependency2 to dependency3"])
  0

  Running an update after a change does nothing because the affected subuser is locked.

  >>> update.update(["all","--accept"])
  Updating...
  Updated repository file:///home/travis/remote-test-repo
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Checking if images need to be updated or installed...
  Checking if subuser foo is up to date.
  Checking for updates to: foo@default
  Running garbage collector on temporary repositories...

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set(['dependent', u'foo'])
  True

  >>> set([i.getImageSourceName() for i in user.getInstalledImages().values()]) == set([u'foo', u'dependency1', u'bar', u'dependent', u'intermediary', u'intermediary', u'dependency2', u'dependent'])
  True

  When we unlock the subuser it gets updated imediately.

  >>> update.update(["unlock-subuser","--accept","dependent"])
  Unlocking subuser dependent
  Verifying subuser configuration.
  Verifying registry consistency...
  Unregistering any non-existant installed images.
  Checking if images need to be updated or installed...
  Checking if subuser dependent is up to date.
  Installing dependency3 ...
  Building...
  Building...
  Building...
  Successfully built 28
  Building...
  Building...
  Building...
  Successfully built 29
  Installing intermediary ...
  Building...
  Building...
  Building...
  Successfully built 30
  Building...
  Building...
  Building...
  Successfully built 31
  Installing dependent ...
  Building...
  Building...
  Building...
  Successfully built 32
  Building...
  Building...
  Building...
  Successfully built 33
  Installed new image <33> for subuser dependent
  Running garbage collector on temporary repositories...

  >>> user = User()
  >>> set(user.getRegistry().getSubusers().keys()) == set(['dependent', u'foo'])
  True

  >>> set([i.getImageSourceName() for i in user.getInstalledImages().values()]) == set([u'foo', u'dependency1', u'bar', u'dependent', u'intermediary', u'intermediary', u'dependency2',u'dependency3', u'dependent'])
  True
  """
  options,args = parseCliArgs(realArgs)
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  if len(args) < 1:
    sys.exit("No arguments given. Please use subuser update -h for help.")
  elif ["all"] == args:
    with user.getRegistry().getLock():
      subuserlib.update.all(user,permissionsAccepter=permissionsAccepter)
  elif "subusers" == args[0]:
    with user.getRegistry().getLock():
      subuserlib.update.subusers(user,args[1:],permissionsAccepter=permissionsAccepter)
  elif "lock-subuser-to" == args[0]:
    try:
      subuserName = args[1]
      commit = args[2]
    except KeyError:
      sys.exit("Wrong number of arguments.  Expected a subuser name and a commit.  Try running\nsubuser update --help\n for more info.")
    with user.getRegistry().getLock():
      subuserlib.update.lockSubuser(user,subuserName=subuserName,commit=commit)
  elif "unlock-subuser" == args[0]:
    try:
      subuserName = args[1]
    except KeyError:
      sys.exit("Wrong number of arguments.  Expected a subuser's name. Try running\nsubuser update --help\nfor more information.")
    with user.getRegistry().getLock():
      subuserlib.update.unlockSubuser(user,subuserName=subuserName,permissionsAccepter=permissionsAccepter)
  elif len(args) == 1:
    sys.exit(" ".join(args) + " is not a valid update subcommand. Please use subuser update -h for help.")
  else:
    sys.exit(" ".join(args) + " is not a valid update subcommand. Please use subuser update -h for help.")
Esempio n. 42
0
#external imports
import sys
import optparse
import json
import os
import uuid
import subprocess
from collections import OrderedDict
#internal imports
import subuserlib.commandLineArguments
import subuserlib.profile
import subuserlib.paths as paths
from subuserlib.classes.user import User

user = User()


def parseCliArgs(realArgs):
    usage = "usage: subuser dev <args> DEV-IMAGE-NAME"
    description = """ Create and run a subuser related to a dev image.
  """
    parser = optparse.OptionParser(
        usage=usage,
        description=description,
        formatter=subuserlib.commandLineArguments.
        HelpFormatterThatDoesntReformatDescription())
    parser.add_option("--ls",
                      dest="ls",
                      action="store_true",
                      default=False,
Esempio n. 43
0
def subuser(sysargs):
  """
  Manage subusers
  """
  options,args = parseCliArgs(sysargs)
  try:
    action = args[0]
  except IndexError:
    parseCliArgs(["--help"])
  user = User()
  permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
  if action == "add":
    if not len(args) == 3:
      sys.exit("Wrong number of arguments to add.  See `subuser subuser -h`.")
    name = args[1]
    imageSourceId = args[2]
    with user.getRegistry().getLock():
      subuserlib.subuser.add(user,name,imageSourceId,permissionsAccepter=permissionsAccepter,prompt=options.prompt,forceInternal=options.forceInternal)
  elif action == "remove":
    names = args[1:]
    if not options.prefix is None:
      allSubuserNames = user.getRegistry().getSubusers().keys()
      names.extend([subuserName for subuserName in allSubuserNames if subuserName.startswith(options.prefix)])
    with user.getRegistry().getLock():
      subusers = []
      for subuserName in names:
        try:
          subusers.append(user.getRegistry().getSubusers()[subuserName])
        except KeyError:
          sys.exit("Subuser "+subuserName+" does not exist and therefore cannot be removed. Use --help for help.")
      subuserlib.subuser.remove(user,subusers)
  elif action == "create-shortcut":
    name = args[1]
    with user.getRegistry().getLock():
      subuserlib.subuser.setExecutableShortcutInstalled(user,name,True)
  elif action == "remove-shortcut":
    name = args[1]
    with user.getRegistry().getLock():
      subuserlib.subuser.setExecutableShortcutInstalled(user,name,False)
  elif action == "edit-permissions":
    try:
      name = args[1]
    except IndexError:
      sys.exit("No subusers specified for editing. Use --help for help.")
    with user.getRegistry().getLock():
      user.getRegistry().logChange("Edit "+name+"'s permissions.")
      try:
        subuser = user.getRegistry().getSubusers()[name]
      except KeyError:
        sys.exit("Subuser "+name+" does not exist. Use --help for help.")
      subuser.editPermissionsCLI()
      subuserlib.verify.verify(user,subusers=[subuser],permissionsAccepter=permissionsAccepter,prompt=options.prompt)
      user.getRegistry().commit()
  else:
    sys.exit("Action "+args[0]+" does not exist. Try:\n subuser subuser --help")
Esempio n. 44
0
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 open("./.subuser.json","w") as subuserDotJson:
        json.dump(repoConfig,subuserDotJson)
        if options.imageSourcesDir:
          user.getEndUser().makedirs(options.imageSourcesDir)
      user.getEndUser().chown("./.subuser.json")
      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 open("./.subuser.json","w") as subuserDotJson:
        json.dump(repoConfig,subuserDotJson,indent=1,separators=(",",": "))
    permissions = defaultPermissions
    (returncode,maintainerName,stderr) = subuserlib.subprocessExtras.callCollectOutput(["git","config","user.name"])
    subuserlib.print.printWithoutCrashing(stderr)
    (returncode,maintainerEmail,stderr) = subuserlib.subprocessExtras.callCollectOutput(["git","config","user.email"])
    subuserlib.print.printWithoutCrashing(stderr)
    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=(",",": "))
    while True:
      subuserlib.subprocessExtras.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.")
    user.getEndUser().chown(permissionsFile)
    if not os.path.exists(imageFile):
      with open(imageFile,"w") as imgf:
        imgf.write(defaultImageFileTemplate)
      user.getEndUser().chown(imageFile)
    subuserlib.subprocessExtras.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)
                subuserlib.subprocessExtras.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()
    # 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()