Exemplo n.º 1
0
def runCommand(sysargs):
  """
  Manage subusers
  """
  options,args = parseCliArgs(sysargs)
  try:
    action = args[0]
  except IndexError:
    print("Wrong number of arguments!")
    parseCliArgs(["--help"])
  lockedUser = LockedUser()
  with lockedUser as user:
    user.registry.commit_message = " ".join(["subuser","subuser"]+sysargs)
    permissionsAccepter = AcceptPermissionsAtCLI(user,alwaysAccept = options.accept)
    if action == "add" or action == "change-image":
      if not len(args) == 3:
        sys.exit("Wrong number of arguments to add.  See `subuser subuser -h`.")
      subuserName = args[1]
      imageSourceId = args[2]
      if action == "add":
        if options.homeDir is not None:
          homeDir = os.path.expanduser(options.homeDir)
        else:
          homeDir = None
        subuserlib.subuser.add(user,subuserName,imageSourceId,permissionsAccepter=permissionsAccepter,prompt=options.prompt,forceInternal=options.forceInternal,homeDir=homeDir)
      elif action == "change-image":
        subuserlib.subuser.changeImage(user,subuserName,imageSourceId,permissionsAccepter=permissionsAccepter,prompt=options.prompt)
    else:
      subuserNames = list(set(args[1:]))
      subusers = []
      if not options.prefix is None:
        allSubuserNames = user.registry.subusers.keys()
        subuserNames.extend([subuserName for subuserName in allSubuserNames if subuserName.startswith(options.prefix)])
      for subuserName in subuserNames:
        try:
          subusers.append(user.registry.subusers[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.registry.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.registry.commit()
        sys.exit()
      sys.exit("Action "+args[0]+" does not exist. Try:\n subuser subuser --help")
Exemplo n.º 2
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")
Exemplo n.º 3
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")
Exemplo n.º 4
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")
Exemplo n.º 5
0
def runCommand(sysargs):
    """
  Manage subusers
  """
    options, args = parseCliArgs(sysargs)
    try:
        action = args[0]
    except IndexError:
        print("Wrong number of arguments!")
        parseCliArgs(["--help"])
    lockedUser = LockedUser()
    with lockedUser as user:
        user.registry.commit_message = " ".join(["subuser", "subuser"] +
                                                sysargs)
        user.operation.permissionsAccepter.alwaysAccept = options.accept
        user.operation.build = options.build
        user.operation.prompt = options.prompt
        if action in ("add", "change-image"):
            if not len(args) == 3:
                sys.exit(
                    "Wrong number of arguments to add.  See `subuser subuser -h`."
                )
            subuserName = args[1]
            imageSourceId = args[2]

            if action == "add":
                if subuserName.startswith("!") and not options.forceInternal:
                    sys.exit(
                        "A subusers may not have names beginning with ! as these names are reserved for internal use."
                    )
                if options.homeDir is not None:
                    homeDir = os.path.expanduser(options.homeDir)
                else:
                    homeDir = None
                try:
                    subuserlib.subuser.add(user,
                                           subuserName,
                                           imageSourceId,
                                           homeDir=homeDir)
                except subuserlib.resolve.ResolutionError as re:
                    sys.exit(re)

            elif action == "change-image":
                try:
                    user.operation.loadSubusersByName([subuserName])
                except LookupError as ke:
                    sys.exit(ke)
                subuserlib.subuser.changeImage(user, imageSourceId)

        else:
            subuserNames = list(set(args[1:]))
            if not options.prefix is None:
                allSubuserNames = user.registry.subusers.keys()
                subuserNames.extend([
                    subuserName for subuserName in allSubuserNames
                    if subuserName.startswith(options.prefix)
                ])
            try:
                user.operation.loadSubusersByName(subuserNames)
            except LookupError as le:
                sys.exit(str(le))
            if user.operation.subusers == []:
                sys.exit("No subusers specified. Use --help for help.")

            if action == "remove":
                subuserlib.subuser.remove(user.operation)
                sys.exit()

            addAndRemoveCommands = [
                ("add-to-path", "remove-from-path",
                 subuserlib.subuser.setExecutableShortcutInstalled),
                ("expose-entrypoints", "hide-entrypoints",
                 lambda operation, install: subuserlib.subuser.
                 setEntrypointsExposed(operation, install))
            ]
            for add, remove, command in addAndRemoveCommands:
                if action in (add, remove):
                    install = True if action == add else False
                    command(user.operation, install)
                    sys.exit()

            if action == "edit-permissions":
                user.registry.logChange("Edit the permissions of:" +
                                        " ".join(subuserNames))
                for subuser in user.operation.subusers:
                    subuser.editPermissionsCLI()
                subuserlib.verify.verify(user.operation)
                user.registry.commit()
                sys.exit()
            sys.exit("Action " + args[0] +
                     " does not exist. Try:\n subuser subuser --help")