Пример #1
0
 def _findEggs(self, zenpackID, zenpackVersion):
     # if the version has a dash, replace with an underscore
     # and format it
     zenpackVersion = parse_version(zenpackVersion.replace("-", "_", 1))
     eggs = []
     for dirpath in zenPath(".ZenPacks"), zenPath("packs"):
         for f in os.listdir(dirpath):
             # verify the .egg extension and strip it from the egg name
             base, ext = os.path.splitext(f)
             if ext != ".egg":
                 continue
             # make sure this is the standard egg name format
             match = EGG_NAME(base)
             if not match:
                 continue
             # extrapolate the values of the name and version
             # NOTE: also has groupings for 'pyver' and 'plat' if needed for
             # later.
             name, version = match.group('name', 'ver')
             # formatting version to a standart
             version = parse_version(version)
             if name == zenpackID and (not zenpackVersion or version == zenpackVersion):
                 eggs.append(os.path.join(dirpath, f))
         # no point in checking the other dirpaths if an egg has been found
         if len(eggs) > 0:
             break
     return eggs
Пример #2
0
 def _findEggs(self, zenpackID, zenpackVersion):
     # if the version has a dash, replace with an underscore
     # and format it
     zenpackVersion = parse_version(zenpackVersion.replace("-", "_", 1))
     eggs = []
     for dirpath in zenPath(".ZenPacks"), zenPath("packs"):
         for f in os.listdir(dirpath):
             # verify the .egg extension and strip it from the egg name
             base, ext = os.path.splitext(f)
             if ext != ".egg":
                 continue
             # make sure this is the standard egg name format
             match = EGG_NAME(base)
             if not match:
                 continue
             # extrapolate the values of the name and version
             # NOTE: also has groupings for 'pyver' and 'plat' if needed for
             # later.
             name, version = match.group('name', 'ver')
             # formatting version to a standart
             version = parse_version(version)
             if name == zenpackID and (not zenpackVersion
                                       or version == zenpackVersion):
                 eggs.append(os.path.join(dirpath, f))
         # no point in checking the other dirpaths if an egg has been found
         if len(eggs) > 0:
             break
     return eggs
Пример #3
0
    def __init__(self, url, **kw):
        super(EggPackage, self).__init__(url, **kw)
        filename, ext = os.path.splitext(self.filename)
        if ext.lower() != '.egg':
            raise self.InvalidPackage('Not an egg: %s' % filename)
        matcher = EGG_NAME(filename)
        if not matcher:
            raise self.InvalidPackage('Could not match egg: %s' % filename)

        self._name, self._raw_version, self._py_version, self._platform = matcher.group(
            'name', 'ver', 'pyver', 'plat')

        if self._raw_version is None or self._py_version is None:
            raise self.InvalidPackage(
                'url with .egg extension but bad name: %s' % url)

        abi_tag = 'none'
        tag_platform = self._platform or 'any'
        self._supported_tags = set()
        self._supported_tags.add(
            ('py' + self._py_version.replace('.', ''), abi_tag, tag_platform))
        # Work around PyPy versions being weird in pep425tags.get_supported
        if self.py_version == '2.7':
            self._supported_tags.add(('pp2', abi_tag, tag_platform))
        elif self.py_version == '3.2':
            self._supported_tags.add(('pp321', abi_tag, tag_platform))
        elif self.py_version == '3.3':
            # N.B. PyPy 3.3 maps to `pp352` because of PyPy versioning.
            # see e.g. http://doc.pypy.org/en/latest/release-pypy3.3-v5.2-alpha1.html
            self._supported_tags.add(('pp352', abi_tag, tag_platform))
        elif self.py_version == '3.5':
            self._supported_tags.add(('pp357', abi_tag, tag_platform))
Пример #4
0
 def __init__(self, url, **kw):
   super(EggLink, self).__init__(url, **kw)
   filename, ext = os.path.splitext(self.filename)
   if ext.lower() != '.egg':
     raise self.InvalidLink('Not an egg: %s' % filename)
   matcher = EGG_NAME(filename)
   if not matcher:
     raise self.InvalidLink('Could not match egg: %s' % filename)
   self._name, self._raw_version, self._py_version, self._platform = matcher.group(
       'name', 'ver', 'pyver', 'plat')
Пример #5
0
 def __init__(self, url, **kw):
     super(EggLink, self).__init__(url, **kw)
     filename, ext = os.path.splitext(self.filename)
     if ext.lower() != '.egg':
         raise self.InvalidLink('Not an egg: %s' % filename)
     matcher = EGG_NAME(filename)
     if not matcher:
         raise self.InvalidLink('Could not match egg: %s' % filename)
     self._name, self._raw_version, self._py_version, self._platform = matcher.group(
         'name', 'ver', 'pyver', 'plat')
Пример #6
0
 def _restore(self, zenpackID, zenpackVersion, filesOnly):
     # if the version has a dash, replace with an underscore
     zenpackVersion = zenpackVersion.replace("-", "_", 1)
     # look for the egg
     eggs = []
     for dirpath in zenPath(".ZenPacks"), zenPath("packs"):
         for f in os.listdir(dirpath):
             # verify the .egg extension and strip it from the egg name
             base, ext = os.path.splitext(f)
             if ext != ".egg":
                 continue
             # make sure this is the standard egg name format
             match = EGG_NAME(base)
             if not match:
                 continue
             # extrapolate the values of the name and version
             # NOTE: also has groupings for 'pyver' and 'plat' if needed for
             # later.
             name, version = match.group('name', 'ver')
             if name == zenpackID and (not zenpackVersion or version == zenpackVersion):
                 eggs.append(os.path.join(dirpath, f))
         # no point in checking the other dirpaths if an egg has been found
         if len(eggs) > 0:
             break
     if len(eggs) == 0:
         self.log.info("Could not find install candidate for %s (%s)", zenpackID, zenpackVersion)
         return
     elif len(eggs) > 1:
         eggpaths = ", ".join(eggs)
         self.log.error("Found more than one install candidate for %s (%s): %s [skipping]", zenpackID, zenpackVersion, eggpaths)
         return
     candidate = eggs[0]
     self.log.info("Loading candidate %s", candidate)
     if candidate.lower().endswith(".egg"):
         try:
             shutil.copy(candidate, tempfile.gettempdir())
             cmd = ["zenpack"]
             if filesOnly:
                 cmd.append("--files-only")
             cmd.extend(["--install", os.path.join(tempfile.gettempdir(), os.path.basename(candidate))])
             try:
                 with open(os.devnull, 'w') as fnull:
                     # the first time fixes the easy-install path
                     subprocess.check_call(cmd, stdout=fnull, stderr=fnull)
             except Exception:
                 pass
             # the second time runs the loaders
             subprocess.check_call(cmd)
         finally:
             try:
                 os.remove(os.path.join(tempfile.gettempdir(), os.path.basename(candidate)))
             except Exception:
                 pass
     else:
         self.log.warning("non-egg zenpacks can not currently be restored automatically: %s", candidate)
Пример #7
0
  def __init__(self, url, **kw):
    super(EggPackage, self).__init__(url, **kw)
    filename, ext = os.path.splitext(self.filename)
    if ext.lower() != '.egg':
      raise self.InvalidPackage('Not an egg: %s' % filename)
    matcher = EGG_NAME(filename)
    if not matcher:
      raise self.InvalidPackage('Could not match egg: %s' % filename)

    self._name, self._raw_version, self._py_version, self._platform = matcher.group(
        'name', 'ver', 'pyver', 'plat')

    if self._raw_version is None or self._py_version is None:
      raise self.InvalidPackage('url with .egg extension but bad name: %s' % url)
Пример #8
0
  def __init__(self, url, **kw):
    super(EggPackage, self).__init__(url, **kw)
    filename, ext = os.path.splitext(self.filename)
    if ext.lower() != '.egg':
      raise self.InvalidPackage('Not an egg: %s' % filename)
    matcher = EGG_NAME(filename)
    if not matcher:
      raise self.InvalidPackage('Could not match egg: %s' % filename)

    self._name, self._raw_version, self._py_version, self._platform = matcher.group(
        'name', 'ver', 'pyver', 'plat')

    if self._raw_version is None or self._py_version is None:
      raise self.InvalidPackage('url with .egg extension but bad name: %s' % url)
Пример #9
0
    def run(self):
        """Execute the user's request"""
        if self.args:
            print "Require one of --install, --remove, --export, --create, or --list flags."
            self.parser.print_help()
            return

        if self.options.installPackName:
            audit('Shell.ZenPack.Install',
                  zenpack=self.options.installPackName)
        elif self.options.fetch:
            audit('Shell.ZenPack.Fetch', zenpack=self.options.fetch)
        elif self.options.exportPack:
            audit('Shell.ZenPack.Export', zenpack=self.options.exportPack)
        elif self.options.removePackName:
            audit('Shell.ZenPack.Remove', zenpack=self.options.removePackName)
        elif self.options.createPackName:
            audit('Shell.ZenPack.Create', zenpack=self.options.createPackName)

        ZenPack.currentServiceId = self.options.serviceId

        if self.options.createPackName:
            devDir, packName = os.path.split(self.options.createPackName)
            try:
                self.connect()
                self.dmd.ZenPackManager.manage_addZenPack(packName,
                                                          devDir=devDir)
            except Exception as ex:
                self.log.fatal("could not create zenpack: %s", ex)
                sys.exit(1)
            sys.exit(0)

        if self.options.installPackName:
            eggInstall = (self.options.installPackName.lower().endswith('.egg')
                          or os.path.exists(
                              os.path.join(self.options.installPackName,
                                           'setup.py')))

        if self.options.removePackName and self.options.filesOnly:
            # A files-only remove implies a files-only install.  Egg packs are only supported here.
            installedEggs = [p for p in iter_entry_points('zenoss.zenpacks')]
            theOne = filter(lambda x: x.name == self.options.removePackName,
                            installedEggs)
            if not theOne:
                raise ZenPackNotFoundException(
                    "Specified zenpack not installed")
            if len(theOne) > 1:
                raise ZenPackException(
                    "Multiple matching distributions for {} found - aborting.".
                    format(self.options.removePackName))
            actualZPDir = theOne[0].dist.location

            class ZPProxy:
                def __init__(self, zpId, actualPath):
                    self.id = zpId
                    self.actualPath = actualPath

                def path(self, *parts):
                    return self.actualPath

            proxy = ZPProxy(self.options.removePackName, actualZPDir)
            for loader in (ZPL.ZPLDaemons(), ZPL.ZPLBin(), ZPL.ZPLLibExec()):
                loader.unload(proxy, None)

            try:
                os.system('pip uninstall -y "%s"' %
                          self.options.removePackName)
            except Exception as ex:
                self.log.error('Could not uninstall "%s"',
                               self.options.removePackName)
                raise ex  # treat it as a fatal error

            return

        self.connect()

        if not getattr(self.dmd, 'ZenPackManager', None):
            raise ZenPackNeedMigrateException(
                'Your Zenoss database appears'
                ' to be out of date. Try running zenmigrate to update.')

        if (self.options.installPackName or self.options.removePackName
            ) and not self._verifyZepRunning(timeout=600, delay=5):
            print >> sys.stderr, "Error: Required daemon zeneventserver not running."
            print >> sys.stderr, "Execute 'zeneventserver start' and retry the ZenPack installation."
            sys.exit(1)

        if self.options.installPackName:
            # Process each install filter utility - order does not matter
            for name, util in getUtilitiesFor(IZenPackInstallFilter):
                # Get normalized pack name to compare.  Split on path separator
                # in case absolute path was given
                egg_name = EGG_NAME(
                    os.path.basename(
                        os.path.normpath(self.options.installPackName)))
                if not egg_name:
                    self.stop('Could not determine egg name from "%s"' %
                              self.options.installPackName)
                packName = egg_name.group('name')
                if not util.installable(packName):
                    self.stop('Filter %s does not allow %s to be installed' \
                              % (name, self.options.installPackName))
            if self.options.skipSameVersion and self._sameVersion():
                return
            if not self.preInstallCheck(eggInstall):
                self.stop('%s not installed' % self.options.installPackName)
            if eggInstall:
                try:
                    return EggPackCmd.InstallEggAndZenPack(
                        self.dmd,
                        self.options.installPackName,
                        link=self.options.link,
                        filesOnly=self.options.filesOnly,
                        previousVersion=self.options.previousVersion,
                        fromUI=self.options.fromui,
                        serviceId=self.options.serviceId,
                        ignoreServiceInstall=self.options.ignoreServiceInstall)
                except (OSError, ) as e:
                    if self.options.link:
                        self.stop('%s cannot be installed with --link option' %
                                  self.options.installPackName)
                    else:
                        self.stop('%s could not be installed' %
                                  self.options.installPackName)
            if os.path.isfile(self.options.installPackName):
                packName = self.extract(self.options.installPackName)
            elif os.path.isdir(self.options.installPackName):
                if self.options.link:
                    packName = self.linkDir(self.options.installPackName)
                else:
                    packName = self.copyDir(self.options.installPackName)
            else:
                self.stop(
                    '%s does not appear to be a valid file or directory.' %
                    self.options.installPackName)
            # We want to make sure all zenpacks have a skins directory and that it
            # is registered. The zip file may not contain a skins directory, so we
            # create one here if need be.  The directory should be registered
            # by the zenpack's __init__.py and the skin should be registered
            # by ZPLSkins loader.
            skinsSubdir = zenPath('Products', packName, 'skins', packName)
            if not os.path.exists(skinsSubdir):
                os.makedirs(skinsSubdir, 0750)
            self.install(packName)

        elif self.options.fetch:
            return EggPackCmd.FetchAndInstallZenPack(self.dmd,
                                                     self.options.fetch)

        elif self.options.exportPack:
            return EggPackCmd.ExportZenPack(self.dmd, self.options.exportPack)
        elif self.options.removePackName:
            try:
                self.dmd.startPauseADM()
                pack = self.dmd.ZenPackManager.packs._getOb(
                    self.options.removePackName, None)

                if not pack:
                    if not self.options.ifinstalled:
                        self.log.info('ZenPack %s is not installed.',
                                      self.options.removePackName)
                        return False
                else:
                    if pack:
                        removeZenPackQueuesExchanges(pack.path())
                        if pack.isEggPack():
                            return EggPackCmd.RemoveZenPack(
                                self.dmd, self.options.removePackName)
                    RemoveZenPack(self.dmd, self.options.removePackName,
                                  self.log)
            finally:
                self.dmd.stopPauseADM()

        elif self.options.list:
            for zpId in self.dmd.ZenPackManager.packs.objectIds():
                try:
                    zp = self.dmd.ZenPackManager.packs._getOb(zpId, None)
                except AttributeError:
                    zp = None
                if not zp:
                    desc = 'broken'
                elif zp.isEggPack():
                    try:
                        desc = zp.eggPath()
                    except DistributionNotFound:
                        desc = "zenpack missing"
                else:
                    desc = zp.path()
                print('%s (%s)' % (zpId, desc))

        elif self.options.restore:
            self.log.info("Restoring zenpacks")
            self.restore()

        transaction.commit()
Пример #10
0
 def _restore(self, zenpackID, zenpackVersion, filesOnly):
     # if the version has a dash, replace with an underscore
     zenpackVersion = zenpackVersion.replace("-", "_", 1)
     # look for the egg
     eggs = []
     for dirpath in zenPath(".ZenPacks"), zenPath("packs"):
         for f in os.listdir(dirpath):
             # verify the .egg extension and strip it from the egg name
             base, ext = os.path.splitext(f)
             if ext != ".egg":
                 continue
             # make sure this is the standard egg name format
             match = EGG_NAME(base)
             if not match:
                 continue
             # extrapolate the values of the name and version
             # NOTE: also has groupings for 'pyver' and 'plat' if needed for
             # later.
             name, version = match.group('name', 'ver')
             if name == zenpackID and (not zenpackVersion
                                       or version == zenpackVersion):
                 eggs.append(os.path.join(dirpath, f))
         # no point in checking the other dirpaths if an egg has been found
         if len(eggs) > 0:
             break
     if len(eggs) == 0:
         self.log.info("Could not find install candidate for %s (%s)",
                       zenpackID, zenpackVersion)
         return
     elif len(eggs) > 1:
         eggpaths = ", ".join(eggs)
         self.log.error(
             "Found more than one install candidate for %s (%s): %s [skipping]",
             zenpackID, zenpackVersion, eggpaths)
         return
     candidate = eggs[0]
     self.log.info("Loading candidate %s", candidate)
     if candidate.lower().endswith(".egg"):
         try:
             shutil.copy(candidate, tempfile.gettempdir())
             cmd = ["zenpack"]
             if filesOnly:
                 cmd.append("--files-only")
             cmd.extend([
                 "--install",
                 os.path.join(tempfile.gettempdir(),
                              os.path.basename(candidate))
             ])
             try:
                 with open(os.devnull, 'w') as fnull:
                     # the first time fixes the easy-install path
                     subprocess.check_call(cmd, stdout=fnull, stderr=fnull)
             except Exception:
                 pass
             # the second time runs the loaders
             subprocess.check_call(cmd)
         finally:
             try:
                 os.remove(
                     os.path.join(tempfile.gettempdir(),
                                  os.path.basename(candidate)))
             except Exception:
                 pass
     else:
         self.log.warning(
             "non-egg zenpacks can not currently be restored automatically: %s",
             candidate)
Пример #11
0
    def run(self):
        """Execute the user's request"""
        if self.args:
            print "Require one of --install, --remove, --export, --create, or --list flags."
            self.parser.print_help()
            return

        if self.options.installPackName:
            audit('Shell.ZenPack.Install', zenpack=self.options.installPackName)
        elif self.options.fetch:
            audit('Shell.ZenPack.Fetch', zenpack=self.options.fetch)
        elif self.options.exportPack:
            audit('Shell.ZenPack.Export', zenpack=self.options.exportPack)
        elif self.options.removePackName:
            audit('Shell.ZenPack.Remove', zenpack=self.options.removePackName)
        elif self.options.createPackName:
            audit('Shell.ZenPack.Create', zenpack=self.options.createPackName)

        ZenPack.currentServiceId = self.options.serviceId

        if self.options.createPackName:
            devDir, packName = os.path.split(self.options.createPackName)
            try:
                self.connect()
                self.dmd.ZenPackManager.manage_addZenPack(packName, devDir=devDir)
            except Exception as ex:
                self.log.fatal("could not create zenpack: %s", ex)
                sys.exit(1)
            sys.exit(0)

        if self.options.installPackName:
            eggInstall = (self.options.installPackName.lower().endswith('.egg')
                or os.path.exists(os.path.join(self.options.installPackName,
                                                'setup.py')))

        if self.options.removePackName and self.options.filesOnly:
            # A files-only remove implies a files-only install.  Egg packs are only supported here.
            installedEggs = [ p for p in iter_entry_points('zenoss.zenpacks') ]
            theOne = filter(lambda x: x.name == self.options.removePackName, installedEggs)
            if not theOne:
                raise ZenPackNotFoundException("Specified zenpack not installed")
            if len(theOne) > 1:
                raise ZenPackException("Multiple matching distributions for {} found - aborting.".format(self.options.removePackName))
            actualZPDir = theOne[0].dist.location
            class ZPProxy:
                def __init__(self, zpId, actualPath):
                    self.id = zpId
                    self.actualPath = actualPath
                def path(self, *parts):
                    return self.actualPath
            proxy = ZPProxy(self.options.removePackName, actualZPDir)
            for loader in (ZPL.ZPLDaemons(), ZPL.ZPLBin(), ZPL.ZPLLibExec()):
                loader.unload(proxy, None)

            try:
                os.system('pip uninstall -y "%s"' % self.options.removePackName)
            except Exception as ex:
                self.log.error('Could not uninstall "%s"', self.options.removePackName)
                raise ex    # treat it as a fatal error

            return

        self.connect()

        if not getattr(self.dmd, 'ZenPackManager', None):
            raise ZenPackNeedMigrateException('Your Zenoss database appears'
                ' to be out of date. Try running zenmigrate to update.')

        if (self.options.installPackName or self.options.removePackName) and not self._verifyZepRunning(timeout=600, delay=5):
            print >> sys.stderr, "Error: Required daemon zeneventserver not running."
            print >> sys.stderr, "Execute 'zeneventserver start' and retry the ZenPack installation."
            sys.exit(1)

        if self.options.installPackName:
            # Process each install filter utility - order does not matter
            for name, util in getUtilitiesFor(IZenPackInstallFilter):
                # Get normalized pack name to compare.  Split on path separator
                # in case absolute path was given
                egg_name = EGG_NAME(os.path.basename(os.path.normpath(self.options.installPackName)))
                if not egg_name:
                    self.stop('Could not determine egg name from "%s"' % self.options.installPackName)
                packName = egg_name.group('name')
                if not util.installable(packName):
                    self.stop('Filter %s does not allow %s to be installed' \
                              % (name, self.options.installPackName))
            if self.options.skipSameVersion and self._sameVersion():
                return
            if not self.preInstallCheck(eggInstall):
                self.stop('%s not installed' % self.options.installPackName)
            if eggInstall:
                try:
                    return EggPackCmd.InstallEggAndZenPack(
                        self.dmd,
                        self.options.installPackName,
                        link=self.options.link,
                        filesOnly=self.options.filesOnly,
                        previousVersion= self.options.previousVersion,
                        fromUI=self.options.fromui,
                        serviceId=self.options.serviceId,
                        ignoreServiceInstall=self.options.ignoreServiceInstall)
                except (OSError,) as e:
                    if self.options.link:
                        self.stop('%s cannot be installed with --link option' % self.options.installPackName)
                    else:
                        self.stop('%s could not be installed' % self.options.installPackName)
            if os.path.isfile(self.options.installPackName):
                packName = self.extract(self.options.installPackName)
            elif os.path.isdir(self.options.installPackName):
                if self.options.link:
                    packName = self.linkDir(self.options.installPackName)
                else:
                    packName = self.copyDir(self.options.installPackName)
            else:
                self.stop('%s does not appear to be a valid file or directory.'
                          % self.options.installPackName)
            # We want to make sure all zenpacks have a skins directory and that it
            # is registered. The zip file may not contain a skins directory, so we
            # create one here if need be.  The directory should be registered
            # by the zenpack's __init__.py and the skin should be registered
            # by ZPLSkins loader.
            skinsSubdir = zenPath('Products', packName, 'skins', packName)
            if not os.path.exists(skinsSubdir):
                os.makedirs(skinsSubdir, 0750)
            self.install(packName)

        elif self.options.fetch:
            return EggPackCmd.FetchAndInstallZenPack(self.dmd, self.options.fetch)

        elif self.options.exportPack:
            return EggPackCmd.ExportZenPack(
                self.dmd, self.options.exportPack)
        elif self.options.removePackName:
            try:
                self.dmd.startPauseADM()
                pack = self.dmd.ZenPackManager.packs._getOb(
                                            self.options.removePackName, None)

                if not pack:
                    if not self.options.ifinstalled:
                        self.log.info(
                            'ZenPack %s is not installed.',
                            self.options.removePackName
                        )
                        return False
                else:
                    if pack:
                        removeZenPackQueuesExchanges(pack.path())
                        if pack.isEggPack():
                            return EggPackCmd.RemoveZenPack(
                                    self.dmd, self.options.removePackName)
                    RemoveZenPack(self.dmd, self.options.removePackName, self.log)
            finally:
                self.dmd.stopPauseADM()

        elif self.options.list:
            for zpId in self.dmd.ZenPackManager.packs.objectIds():
                try:
                    zp = self.dmd.ZenPackManager.packs._getOb(zpId, None)
                except AttributeError:
                    zp = None
                if not zp:
                    desc = 'broken'
                elif zp.isEggPack():
                    try:
                        desc = zp.eggPath()
                    except DistributionNotFound:
                        desc = "zenpack missing"
                else:
                    desc = zp.path()
                print('%s (%s)' % (zpId,  desc))

        elif self.options.restore:
            self.log.info("Restoring zenpacks")
            self.restore()

        transaction.commit()