Exemple #1
0
def listservers():

    OUT.notice('\n'.join(['apache',
                          'lighttpd',
                          'cherokee',
                          'nginx',
                          'gatling']))
Exemple #2
0
    def listunused(self, db):
        '''
        Outputs a list of what has not been installed so far
        '''

        packages = self.list_locations()

        if not packages:
            OUT.die('No packages found!')

        keys = sorted(packages)

        OUT.debug('Check for unused web applications', 7)

        for i in keys:

            db.set_category(packages[i][0])
            db.set_package(packages[i][1])
            db.set_version(packages[i][2])

            if not db.has_installs():
                if packages[i][0]:
                    OUT.notice(packages[i][0] + '/' + packages[i][1] + '-' +
                               packages[i][2])
                else:
                    OUT.notice(packages[i][1] + '-' + packages[i][2])
Exemple #3
0
    def listunused(self, db):
        '''
        Outputs a list of what has not been installed so far
        '''

        packages = self.list_locations()

        if not packages:
            OUT.die('No packages found!')

        keys = sorted(packages)

        OUT.debug('Check for unused web applications', 7)

        for i in keys:

            db.set_category(packages[i][0])
            db.set_package (packages[i][1])
            db.set_version (packages[i][2])

            if not db.has_installs():
                if packages[i][0]:
                    OUT.notice(packages[i][0] + '/' + packages[i][1] + '-' + packages[i][2])
                else:
                    OUT.notice(packages[i][1] + '-' + packages[i][2])
Exemple #4
0
def listservers():

    OUT.notice('\n'.join(['apache',
                          'lighttpd',
                          'cherokee',
                          'nginx',
                          'gatling',
                          'hiawatha',
                          'tracd',]))
def listservers():

    OUT.notice('\n'.join([
        'apache',
        'lighttpd',
        'cherokee',
        'nginx',
        'gatling',
        'tracd',
    ]))
Exemple #6
0
 def db_print(self):
     ''' Print all enties of the contents file.'''
     entries = self.get_sorted_files()
     values = []
     for i in entries:
         # Fix relative entry
         s = self.__content[i]
         s[1] = str(int(s[1]))
         values.append(' '.join(s))
     OUT.notice('\n'.join(values))
Exemple #7
0
 def db_print(self):
     ''' Print all enties of the contents file.'''
     entries = self.get_sorted_files()
     values = []
     for i in entries:
         # Fix relative entry
         s = self.__content[i]
         s[1] = str(int(s[1]))
         values.append(' '.join(s))
     OUT.notice('\n'.join(values))
Exemple #8
0
    def clean(self):

        self.file_behind_flag = False

        OUT.debug('Basic server clean', 7)

        self.file_behind_flag |= self.__del.remove_files()

        self.file_behind_flag |= self.__del.remove_dirs()

        OUT.info('Any files or directories listed above must be removed b'
                 'y hand')

        # okay, let's finish off
        #
        # we don't need the contents file anymore

        self.file_behind_flag |= not self.__content.kill()

        # right - we need to run the hook scripts now
        # if they fail, we don't actually care

        # run the hooks

        self.__ebuild.run_hooks('clean', self)

        # do we need the dotconfig file?
        #
        # if the .webapp file is the only one in the dir, we believe
        # that we can remove it

        self.__dotconfig.kill()

        # is the installation directory empty?

        if not os.listdir(self.__destd) and os.path.isdir(self.__destd):
            if not self.__p:
                os.rmdir(self.__destd)
        else:
            OUT.notice('--- ' + self.__destd)

        # update the list of installs

        self.__db.remove(self.__destd)

        # Remove the selinux module

        if self.__selinux is not None:
            self.__selinux.remove_module()

        # did we leave anything behind?

        if self.file_behind_flag:
            OUT.warn('Remove whatever is listed above by hand')
Exemple #9
0
    def clean(self):

        self.file_behind_flag = False

        OUT.debug('Basic server clean', 7)

        self.file_behind_flag |= self.__del.remove_files()

        self.file_behind_flag |= self.__del.remove_dirs()

        OUT.info('Any files or directories listed above must be removed b'
                 'y hand')

        # okay, let's finish off
        #
        # we don't need the contents file anymore

        self.file_behind_flag |= not self.__content.kill()

        # right - we need to run the hook scripts now
        # if they fail, we don't actually care

        # run the hooks

        self.__ebuild.run_hooks('clean', self)

        # do we need the dotconfig file?
        #
        # if the .webapp file is the only one in the dir, we believe
        # that we can remove it

        self.__dotconfig.kill()

        # is the installation directory empty?

        if not os.listdir(self.__destd) and os.path.isdir(self.__destd):
            if not self.__p:
                os.rmdir(self.__destd)
        else:
            OUT.notice('--- ' + self.__destd)

        # update the list of installs

        self.__db.remove(self.__destd)

        # did we leave anything behind?

        if self.file_behind_flag:
            OUT.warn('Remove whatever is listed above by hand')
Exemple #10
0
    def show_installed(self):
        ''' Show which application has been installed in the install 
        location.'''
        if not self.has_dotconfig():
            OUT.die('No ' + self.__file + ' file in ' + self.__instdir
                    + '; unable to continue')

        self.read()

        if 'WEB_CATEGORY' in self.__data:
            OUT.notice(self.__data['WEB_CATEGORY'] + ' ' +
                   self.__data['WEB_PN'] + ' ' +
                   self.__data['WEB_PVR'])
        else:
            OUT.notice(
                   self.__data['WEB_PN'] + ' ' +
                   self.__data['WEB_PVR'])
Exemple #11
0
    def kill(self):
        ''' Remove the dot config file.'''

        empty = self.is_empty()

        OUT.debug('Trying to removing .webapp file', 7)

        if not empty:
            if not self.__p:
                try:
                    os.unlink(self.__dot_config())
                except:
                    OUT.warn('Failed to remove '
                             + self.__dot_config() + '!')
                    return False
            else:
                OUT.info('Would have removed ' + self.__dot_config())
            return True
        else:
            OUT.notice('--- ' + empty)
            return False
Exemple #12
0
    def remove(self, entry):
        '''
        Decide whether to delete something - and then go ahead and do so

        Just like portage, we only remove files that have not changed
        from when we installed them.  If the timestamp or checksum is
        different, we leave the file in place.

        Inputs

          entry    - file/dir/sym to remove
        '''

        OUT.debug('Trying to remove file', 6)

        # okay, deal with the file | directory | symlink

        removeable = self.__content.get_canremove(entry)

        if not removeable:

            # Remove directory or file.

            # Report if we are only pretending
            if self.__p:
                OUT.info('    pretending to remove: ' + entry)

            # try to remove the entry
            try:
                entry_type = self.__content.etype(entry)
                if self.__content.etype(entry) == 'dir':
                    # its a directory -> rmdir
                    if not self.__p:
                        os.rmdir(entry)
                else:
                    # its a file -> unlink
                    if not self.__p:
                        os.unlink(entry)
            except:
                # Report if there is a problem
                OUT.notice('!!!      '
                           + self.__content.epath(entry))
                return

            if self.__v and not self.__p:
                # Report successful deletion

                OUT.notice('<<< ' + entry_type + ' '
                           * (5 - len(entry_type))
                           + self.__content.epath(entry))

            self.__content.delete(entry)

            return True

        else:

            OUT.notice(removeable)

            return False
Exemple #13
0
    def show_post(self, filename, ptype, server=None):
        '''
        Display one of the post files.
        '''

        post_file = self.__sourced + '/' + filename

        OUT.debug('Check for instruction file', 7)

        if not os.path.isfile(post_file):
            return

        self.run_vars(server)

        post_instructions = open(post_file).readlines()

        OUT.debug('Read post instructions', 7)

        post = [
            '',
            '=================================================================',
            'POST-' + ptype.upper() + ' INSTRUCTIONS',
            '=================================================================',
            ''
        ]

        for i in post_instructions:
            i = i.replace('"', '\\"')
            post.append(os.popen('echo -n "' + i + '"\n').read()[:-1])

        post = post + [
            '',
            '=================================================================',
            ''
        ]

        for i in post:
            OUT.notice(i)
Exemple #14
0
    def remove(self, entry):
        '''
        Decide whether to delete something - and then go ahead and do so

        Just like portage, we only remove files that have not changed
        from when we installed them.  If the timestamp or checksum is
        different, we leave the file in place.

        Inputs

          entry    - file/dir/sym to remove
        '''

        OUT.debug('Trying to remove file', 6)

        # okay, deal with the file | directory | symlink

        removeable = self.__content.get_canremove(entry)

        if not removeable:

            # Remove directory or file.

            # Report if we are only pretending
            if self.__p:
                OUT.info('    pretending to remove: ' + entry)

            # try to remove the entry
            try:
                entry_type = self.__content.etype(entry)
                if self.__content.etype(entry) == 'dir':
                    # its a directory -> rmdir
                    if not self.__p:
                        os.rmdir(entry)
                else:
                    # its a file -> unlink
                    if not self.__p:
                        os.unlink(entry)
            except:
                # Report if there is a problem
                OUT.notice('!!!      ' + self.__content.epath(entry))
                return

            if self.__v and not self.__p:
                # Report successful deletion

                OUT.notice('<<< ' + entry_type + ' ' * (5 - len(entry_type)) +
                           self.__content.epath(entry))

            self.__content.delete(entry)

            return True

        else:

            OUT.notice(removeable)

            return False
Exemple #15
0
    def show_post(self, filename, ptype, server = None):
        '''
        Display one of the post files.
        '''

        post_file =  self.__sourced + '/' + filename

        OUT.debug('Check for instruction file', 7)

        if not os.path.isfile(post_file):
            return

        self.run_vars(server)

        post_instructions = open(post_file).readlines()

        OUT.debug('Read post instructions', 7)

        post = [
            '',
            '=================================================================',
            'POST-' + ptype.upper() + ' INSTRUCTIONS',
            '=================================================================',
            '']

        for i in post_instructions:
            i = i.replace('"', '\\"')
            post.append(os.popen('echo -n "' + i + '"\n').read()[:-1])

        post = post + [
            '',
            '=================================================================',
            '']

        for i in post:
            OUT.notice(i)
Exemple #16
0
    def add(self,
            dsttype,
            ctype,
            destination,
            path,
            real_path,
            relative = True):
        '''
        Add an entry to the contents file.

        Just like Portage, when we install an app, we create a contents
        file to say what we installed and when.  We use this contents
        file to help us safely remove & upgrade apps.

        CONTENTS file format:

        <what> <rel> <type> <filename> <timestamp> <sum> [<optional>]

        where

        <what>      is one of dir|sym|file|hardlink

        <rel>       is 1 for relative filenames, 0 for absolute
                        filenames

        <type>      is one of
                        server-owned|default-owned|config-owned|virtual

        <timestamp> is the timestamp when the file was installed

        <sum>       is the md5sum of the file
                        (this is 0 for directories and symlinks)

        <filename>      is the actual name of the file we have installed

        <optional>      is additional data that depends upon <what>

        NOTE:
            Filenames used to be on the end of the line.  This made
                the old bash version more complicated, and
                prone to failure. So I have moved the filename into the
                middle of the line. -- Stuart

        Portage uses absolute names for its files, dirs, and symlinks.
        We do not.
        In theory, you can move a directory containing a web-based app,
        and

        a) the app itself will not break, and
        b) webapp-config will still work on that directory
           for upgrades and cleans.

        Position-independence *is* a design constraint that all future
        changes to this script need to honour.

        Inputs:

          dsttype     - type to add (one of dir|sym|file|hardlink)
          ctype       - internal webapp-config type
                      - (server-owned | config-owned | virtual)
          destination - install dir (normally $G_INSTALLDIR)
          path        - filename inside 'destination'
          real_path   - for config-protected files realpath =! path
                        (and this is important for md5)
          relative    - 1 for storing a relative filename, 0 otherwise
        '''

        OUT.debug('Adding entry to content dictionary', 6)

        # Build the full path that we use as index in the contents list
        while path[0] == '/':
            path = path[1:]
        while destination[-1] == '/':
            destination = destination[:-1]

        entry = destination + '/' + path

        # special case - we don't add entries for '.'

        if os.path.basename(entry) == '.':
            return

        if (not self.__p
                and not os.path.islink(entry)
                and (not os.path.exists(entry)
                    or not os.access(entry, os.R_OK))):
            OUT.warn('Cannot access file ' + entry + ' to add it as'
                     ' installation content. This should not happen!')
            return

        allowed_types = {
            'file'    : [ 'file', self.file_md5,  self.file_null ],
            'hardlink': [ 'file', self.file_md5,  self.file_null ],
            'dir'     : [  'dir', self.file_zero, self.file_null ],
            'sym'     : [  'sym', self.file_zero, self.file_link ],
            }

        if not dsttype in list(allowed_types.keys()):
            OUT.die('Oops, webapp-config bug. "dsttype" is ' + dsttype)

        # Generate handler for file attributes
        a = allowed_types[dsttype]

        # For absolute entries the path must match the entry
        if not relative:
            path = entry

        OUT.debug('Adding entry', 7)

        # report if pretending
        if self.__p:

            OUT.info('    pretending to add: ' +
                     ' '.join([dsttype,
                               str(int(relative)),
                               ctype,
                               '"' + path + '"']))
        else:

            # Only the path is enclosed in quotes, NOT the link targets
            self.__content[entry] = [ a[0],
                                      str(int(relative)),
                                      ctype,
                                      '"' + path + '"',
                                      self.file_time(entry),
                                      a[1](real_path),
                                      a[2](entry)]

            if self.__v:
                msg = path
                if msg[0] == "/":
                    msg = self.__root + msg
                    msg = self.__re.sub('/', msg)
                OUT.notice('>>> ' + a[0] + ' ' * (4 - len(a[0])) + ' ('  \
                           + ctype + ') ' + msg)
Exemple #17
0
    def mkfile(self, filename, current_type):
        '''
        This is what we are all about.  No more games - lets take a file
        from the master image of the web-based app, and make it available
        inside the install directory.

        filename    - name of the file

        '''

        OUT.debug('Creating file', 6)

        dst_name  = self.__destd + '/' + filename
        file_type = self.__ws.filetype(self.__sourced + '/' + filename, current_type)

        OUT.debug('File type determined', 7)

        # are we overwriting an existing file?

        OUT.debug('Check for existing file', 7)

        if os.path.exists(dst_name):

            OUT.debug('File in the way!', 7)

            my_canremove = True

            # o-oh - we're going to be overwriting something that already
            # exists

            # If we are upgrading, check if the file can be removed
            if self.__u:
                my_canremove = self.__remove.remove(self.__destd, filename)
            # Config protected file definitely cannot be removed
            elif file_type[0:6] == 'config':
                my_canremove = False

            if not my_canremove:
                # not able to remove the file
                #           or
                # file is config-protected

                dst_name = self.__protect.get_protectedname(self.__destd,
                                                            filename)
                OUT.notice('^o^ hiding ' + filename)
                self.config_protected_dirs.append(self.__destd + '/' 
                                                  + os.path.dirname(filename))

                OUT.debug('Hiding config protected file', 7)

            else:

                # it's a file we do not know about - so get rid
                # of it anyway
                #
                # this behaviour here *is* by popular request
                # personally, I'm not comfortable with it -- Stuart

                if not self.__p:
                    if os.path.isdir(dst_name):
                        os.rmdir(dst_name)
                    else:
                        os.unlink(dst_name)
                else:
                    OUT.info('    would have removed "' +  dst_name + '" s'
                             'ince it is in the way for the current instal'
                             'l. It should not be present in that location'
                             '!')


        # if we get here, we can get on with the business of making
        # the file available

        (user, group, perm) = self.__perm['file'][file_type]
        my_contenttype = ''

        src_name = self.__ws.appdir() + '/' + self.__sourced + '/' + filename

        # Fix the paths
        src_name = re.compile('/+').sub('/', src_name)
        dst_name = re.compile('/+').sub('/', dst_name)

        OUT.debug('Creating File', 7)

        # this is our default file type
        #
        # we link in (soft and hard links are supported)
        # if we're allowed to
        #
        # some applications (/me points at PHP scripts)
        # won't run if symlinked in.
        # so we now support copying files in too
        #
        # default behaviour is to hard link (if we can), and
        # to copy if we cannot
        #
        # if the user wants symlinks, then the user has to
        # use the new '--soft' option

        if file_type == 'virtual' or os.path.islink(src_name):

            if self.__link_type == 'soft':
                try:

                    OUT.debug('Trying to softlink', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> SOFTLINKING FILE: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        os.symlink(src_name, dst_name)

                    my_contenttype = 'sym'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed to softlink (' + str(e) + ')')

            elif self.__link_type == 'copy':
                try:

                    OUT.debug('Trying to copy files directly', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> COPYING FILE: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        shutil.copy(src_name, dst_name)

                    my_contenttype = 'file'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed to copy (' + str(e) + ')')

            elif os.path.islink(src_name):
                try:

                    OUT.debug('Trying to copy symlink', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> SYMLINK COPY: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        os.symlink(os.readlink(src_name), dst_name)

                    my_contenttype = 'sym'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed copy symlink (' + str(e) + ')')

            else:
                try:

                    OUT.debug('Trying to hardlink', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> HARDLINKING FILE: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        os.link(src_name, dst_name)

                    my_contenttype = 'file'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed to hardlink (' + str(e) + ')')

        if not my_contenttype:

            if not self.__p:
                if self.__v:
                    print("\n>>> COPYING FILE: ")
                    print(">>> Source: " + src_name +
                          "\n>>> Destination: " + dst_name + "\n")
                shutil.copy(src_name, dst_name)
            my_contenttype = 'file'


        if not self.__p and not os.path.islink(src_name):

            old_perm =  os.stat(src_name)[stat.ST_MODE] & 511

            os.chown(dst_name,
                     user,
                     group)

            os.chmod(dst_name,
                     perm(old_perm))

        self.__content.add(my_contenttype,
                           file_type,
                           self.__destd,
                           filename,
                           dst_name,
                           self.__relative)

        return file_type
Exemple #18
0
    def add(self, dsttype, ctype, destination, path, real_path, relative=True):
        '''
        Add an entry to the contents file.

        Just like Portage, when we install an app, we create a contents
        file to say what we installed and when.  We use this contents
        file to help us safely remove & upgrade apps.

        CONTENTS file format:

        <what> <rel> <type> <filename> <timestamp> <sum> [<optional>]

        where

        <what>      is one of dir|sym|file|hardlink

        <rel>       is 1 for relative filenames, 0 for absolute
                        filenames

        <type>      is one of
                        server-owned|default-owned|config-owned|virtual

        <timestamp> is the timestamp when the file was installed

        <sum>       is the md5sum of the file
                        (this is 0 for directories and symlinks)

        <filename>      is the actual name of the file we have installed

        <optional>      is additional data that depends upon <what>

        NOTE:
            Filenames used to be on the end of the line.  This made
                the old bash version more complicated, and
                prone to failure. So I have moved the filename into the
                middle of the line. -- Stuart

        Portage uses absolute names for its files, dirs, and symlinks.
        We do not.
        In theory, you can move a directory containing a web-based app,
        and

        a) the app itself will not break, and
        b) webapp-config will still work on that directory
           for upgrades and cleans.

        Position-independence *is* a design constraint that all future
        changes to this script need to honour.

        Inputs:

          dsttype     - type to add (one of dir|sym|file|hardlink)
          ctype       - internal webapp-config type
                      - (server-owned | config-owned | virtual)
          destination - install dir (normally $G_INSTALLDIR)
          path        - filename inside 'destination'
          real_path   - for config-protected files realpath =! path
                        (and this is important for md5)
          relative    - 1 for storing a relative filename, 0 otherwise
        '''

        OUT.debug('Adding entry to content dictionary', 6)

        # Build the full path that we use as index in the contents list
        while path[0] == '/':
            path = path[1:]
        while destination[-1] == '/':
            destination = destination[:-1]

        entry = destination + '/' + path

        # special case - we don't add entries for '.'

        if os.path.basename(entry) == '.':
            return

        if (not self.__p and not os.path.islink(entry) and
            (not os.path.exists(entry) or not os.access(entry, os.R_OK))):
            OUT.warn('Cannot access file ' + entry + ' to add it as'
                     ' installation content. This should not happen!')
            return

        allowed_types = {
            'file': ['file', self.file_md5, self.file_null],
            'hardlink': ['file', self.file_md5, self.file_null],
            'dir': ['dir', self.file_zero, self.file_null],
            'sym': ['sym', self.file_zero, self.file_link],
        }

        if not dsttype in list(allowed_types.keys()):
            OUT.die('Oops, webapp-config bug. "dsttype" is ' + dsttype)

        # Generate handler for file attributes
        a = allowed_types[dsttype]

        # For absolute entries the path must match the entry
        if not relative:
            path = entry

        OUT.debug('Adding entry', 7)

        # report if pretending
        if self.__p:

            OUT.info('    pretending to add: ' + ' '.join(
                [dsttype, str(int(relative)), ctype, '"' + path + '"']))
        else:

            # Only the path is enclosed in quotes, NOT the link targets
            self.__content[entry] = [
                a[0],
                str(int(relative)), ctype, '"' + path + '"',
                self.file_time(entry), a[1](real_path), a[2](entry)
            ]

            if self.__v:
                msg = path
                if msg[0] == "/":
                    msg = self.__root + msg
                    msg = self.__re.sub('/', msg)
                OUT.notice('>>> ' + a[0] + ' ' * (4 - len(a[0])) + ' ('  \
                           + ctype + ') ' + msg)
Exemple #19
0
    def add(self,
            dsttype,
            ctype,
            destination,
            path,
            real_path,
            relative = True):
        '''
        Add an entry to the contents file.

        Just like Portage, when we install an app, we create a contents
        file to say what we installed and when.  We use this contents
        file to help us safely remove & upgrade apps.

        CONTENTS file format:

        <what> <rel> <type> <filename> <timestamp> <sum> [<optional>]

        where

        <what>      is one of dir|sym|file|hardlink

        <rel>       is 1 for relative filenames, 0 for absolute
                        filenames

        <type>      is one of
                        server-owned|default-owned|config-owned|virtual

        <timestamp> is the timestamp when the file was installed

        <sum>       is the md5sum of the file
                        (this is 0 for directories and symlinks)

        <filename>      is the actual name of the file we have installed

        <optional>      is additional data that depends upon <what>

        NOTE:
            Filenames used to be on the end of the line.  This made
                the old bash version more complicated, and
                prone to failure. So I have moved the filename into the
                middle of the line. -- Stuart

        Portage uses absolute names for its files, dirs, and symlinks.
        We do not.
        In theory, you can move a directory containing a web-based app,
        and

        a) the app itself will not break, and
        b) webapp-config will still work on that directory
           for upgrades and cleans.

        Position-independence *is* a design constraint that all future
        changes to this script need to honour.

        Inputs:

          dsttype     - type to add (one of dir|sym|file|hardlink)
          ctype       - internal webapp-config type
                      - (server-owned | config-owned | virtual)
          destination - install dir (normally $G_INSTALLDIR)
          path        - filename inside 'destination'
          real_path   - for config-protected files realpath =! path
                        (and this is important for md5)
          relative    - 1 for storing a relative filename, 0 otherwise

        OUT.color_off()
        import os.path
        here = os.path.dirname(os.path.realpath(__file__))

        One for pretending:

        a = Contents(here + '/tests/testfiles/contents/app/',
        ...              package = 'test', version = '1.0',
        ...              pretend = True)

        And this one is for real:

        b = Contents(here + '/tests/testfiles/contents/app/',
        ...              package = 'test', version = '1.0')

        Pretend to add a file:

        a.add('file', 'config-owned',
        ...       destination = here + '/tests/testfiles/contents/app/',
        ...       path = '/test1', relative = True)
        *     pretending to add: file 1 config-owned "test1"

        Lets not pretend this time:

        b.add('file', 'config-owned',
        ...       destination = here + '/tests/testfiles/contents/app/',
        ...       path = '/test1', relative = True)
        b.entry(here + '/tests/testfiles/contents/app/test1') #doctest: +ELLIPSIS
        'file 1 config-owned "test1" ... d8e8fca2dc0f896fd7cb4cb0031ba249 '

        Lets produce an error with a file that does not exist:

        b.add('file', 'config-owned',
        ...       destination = here + '/tests/testfiles/contents/app/',
        ...       path = '/nothere', relative = True) #doctest: +ELLIPSIS
        * Cannot access file .../tests/testfiles/contents/app/nothere to add it as installation content. This should not happen!

        Other file types:

        b.add('hardlink', 'config-owned',
        ...       destination = here + '/tests/testfiles/contents/app/',
        ...       path = '/test2', relative = True)
        b.entry(here + '/tests/testfiles/contents/app/test2') #doctest: +ELLIPSIS
        'file 1 config-owned "test2" ... d8e8fca2dc0f896fd7cb4cb0031ba249 '
        b.add('dir', 'default-owned',
        ...       destination = here + '/tests/testfiles/contents/app/',
        ...       path = '/dir1', relative = True)
        b.entry(here + '/tests/testfiles/contents/app/dir1') #doctest: +ELLIPSIS
        'dir 1 default-owned "dir1" ... 0 '
        b.add('dir', 'default-owned', destination = here + '/tests/testfiles/contents/app',
        ...       path = '/dir1',
        ...       relative = False)
        b.entry(here + '/tests/testfiles/contents/app/dir1') #doctest: +ELLIPSIS
        'dir 0 default-owned ".../tests/testfiles/contents/app/dir1" ... 0 '

        Q: Is the full link to the target what we want?
        A: Yes, since the link will still be ok even if we move the directory.

        b.add('sym', 'virtual',
        ...       destination = here + '/tests/testfiles/contents/app/',
        ...       path = '/test3', relative = True)
        b.entry(here + '/tests/testfiles/contents/app/test3') #doctest: +ELLIPSIS
        'sym 1 virtual "test3" ... 0 .../tests/testfiles/contents/app/test1'

        b.db_print() #doctest: +ELLIPSIS
        file 1 config-owned "test1" ... d8e8fca2dc0f896fd7cb4cb0031ba249 
        file 1 config-owned "test2" ... d8e8fca2dc0f896fd7cb4cb0031ba249 
        sym 1 virtual "test3" ... 0 .../tests/testfiles/contents/app/test1
        dir 0 default-owned ".../tests/testfiles/contents/app/dir1" ... 0 

        '''

        OUT.debug('Adding entry to content dictionary', 6)

        # Build the full path that we use as index in the contents list
        while path[0] == '/':
            path = path[1:]
        while destination[-1] == '/':
            destination = destination[:-1]

        entry = destination + '/' + path

        # special case - we don't add entries for '.'

        if os.path.basename(entry) == '.':
            return

        if (not self.__p
                and not os.path.islink(entry)
                and (not os.path.exists(entry)
                    or not os.access(entry, os.R_OK))):
            OUT.warn('Cannot access file ' + entry + ' to add it as'
                     ' installation content. This should not happen!')
            return

        allowed_types = {
            'file'    : [ 'file', self.file_md5,  self.file_null ],
            'hardlink': [ 'file', self.file_md5,  self.file_null ],
            'dir'     : [  'dir', self.file_zero, self.file_null ],
            'sym'     : [  'sym', self.file_zero, self.file_link ],
            }

        if not dsttype in allowed_types.keys():
            OUT.die('Oops, webapp-config bug. "dsttype" is ' + dsttype)

        # Generate handler for file attributes
        a = allowed_types[dsttype]

        # For absolute entries the path must match the entry
        if not relative:
            path = entry

        OUT.debug('Adding entry', 7)

        # report if pretending
        if self.__p:

            OUT.info('    pretending to add: ' +
                     ' '.join([dsttype,
                               str(int(relative)),
                               ctype,
                               '"' + path + '"']))
        else:

            # Only the path is enclosed in quotes, NOT the link targets
            self.__content[entry] = [ a[0],
                                      str(int(relative)),
                                      ctype,
                                      '"' + path + '"',
                                      self.file_time(entry),
                                      a[1](real_path),
                                      a[2](entry)]

            if self.__v:
                msg = path
                if msg[0] == "/":
                    msg = self.__root + msg
                    msg = self.__re.sub('/', msg)
                OUT.notice('>>> ' + a[0] + ' ' * (4 - len(a[0])) + ' ('  \
                           + ctype + ') ' + msg)
Exemple #20
0
    def mkfile(self, filename):
        '''
        This is what we are all about.  No more games - lets take a file
        from the master image of the web-based app, and make it available
        inside the install directory.

        filename    - name of the file

        '''

        OUT.debug('Creating file', 6)

        dst_name = self.__destd + '/' + filename
        file_type = self.__ws.filetype(self.__sourced + '/' + filename)

        OUT.debug('File type determined', 7)

        # are we overwriting an existing file?

        OUT.debug('Check for existing file', 7)

        if os.path.exists(dst_name):

            OUT.debug('File in the way!', 7)

            my_canremove = True

            # o-oh - we're going to be overwriting something that already
            # exists

            # If we are upgrading, check if the file can be removed
            if self.__u:
                my_canremove = self.__remove.remove(self.__destd, filename)
            # Config protected file definitely cannot be removed
            elif file_type[0:6] == 'config':
                my_canremove = False

            if not my_canremove:
                # not able to remove the file
                #           or
                # file is config-protected

                dst_name = self.__protect.get_protectedname(
                    self.__destd, filename)
                OUT.notice('^o^ hiding ' + filename)
                self.config_protected_dirs.append(self.__destd + '/' +
                                                  os.path.dirname(filename))

                OUT.debug('Hiding config protected file', 7)

            else:

                # it's a file we do not know about - so get rid
                # of it anyway
                #
                # this behaviour here *is* by popular request
                # personally, I'm not comfortable with it -- Stuart

                if not self.__p:
                    if os.path.isdir(dst_name):
                        os.rmdir(dst_name)
                    else:
                        os.unlink(dst_name)
                else:
                    OUT.info('    would have removed "' + dst_name + '" s'
                             'ince it is in the way for the current instal'
                             'l. It should not be present in that location'
                             '!')

        # if we get here, we can get on with the business of making
        # the file available

        (user, group, perm) = self.__perm['file'][file_type]
        my_contenttype = ''

        src_name = self.__ws.appdir() + '/' + self.__sourced + '/' + filename

        # Fix the paths
        src_name = re.compile('/+').sub('/', src_name)
        dst_name = re.compile('/+').sub('/', dst_name)

        OUT.debug('Creating File', 7)

        # this is our default file type
        #
        # we link in (soft and hard links are supported)
        # if we're allowed to
        #
        # some applications (/me points at PHP scripts)
        # won't run if symlinked in.
        # so we now support copying files in too
        #
        # default behaviour is to hard link (if we can), and
        # to copy if we cannot
        #
        # if the user wants symlinks, then the user has to
        # use the new '--soft' option

        if file_type == 'virtual' or os.path.islink(src_name):

            if self.__link_type == 'soft':
                try:

                    OUT.debug('Trying to softlink', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> SOFTLINKING FILE: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        os.symlink(src_name, dst_name)

                    my_contenttype = 'sym'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed to softlink (' + str(e) + ')')

            elif self.__link_type == 'copy':
                try:

                    OUT.debug('Trying to copy files directly', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> COPYING FILE: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        shutil.copy(src_name, dst_name)

                    my_contenttype = 'file'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed to copy (' + str(e) + ')')

            elif os.path.islink(src_name):
                try:

                    OUT.debug('Trying to copy symlink', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> SYMLINK COPY: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        os.symlink(os.readlink(src_name), dst_name)

                    my_contenttype = 'sym'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed copy symlink (' + str(e) + ')')

            else:
                try:

                    OUT.debug('Trying to hardlink', 8)

                    if not self.__p:
                        if self.__v:
                            print("\n>>> HARDLINKING FILE: ")
                            print(">>> Source: " + src_name +
                                  "\n>>> Destination: " + dst_name + "\n")
                        os.link(src_name, dst_name)

                    my_contenttype = 'file'

                except Exception as e:

                    if self.__v:
                        OUT.warn('Failed to hardlink (' + str(e) + ')')

        if not my_contenttype:

            if not self.__p:
                if self.__v:
                    print("\n>>> COPYING FILE: ")
                    print(">>> Source: " + src_name + "\n>>> Destination: " +
                          dst_name + "\n")
                shutil.copy(src_name, dst_name)
            my_contenttype = 'file'

        if not self.__p and not os.path.islink(src_name):

            old_perm = os.stat(src_name)[stat.ST_MODE] & 511

            os.chown(dst_name, user, group)

            os.chmod(dst_name, perm(old_perm))

        self.__content.add(my_contenttype, file_type, self.__destd, filename,
                           dst_name, self.__relative)