Beispiel #1
0
 def export_trait(self, trait, path=None):
     if path is None:
         path = self.tarball_path
     tt = TraitTemplate(self.conn, self.suite)
     tt.set_trait(trait)
     backup_trait(self.conn, self.suite, trait, path)
     dialogs.Message('%s exported to %s' % (trait, path))
Beispiel #2
0
 def __init__(self, conn, suite, cfg):
     Installer.__init__(self, conn, cfg=cfg)
     self.traitpackage = TraitPackage(conn, suite)
     self.traittemplate = TraitTemplate(conn, suite)
     self.traitscripts = TraitScript(conn, suite)
     self.profiledata = {}
     self.familydata = {}
 def export_trait(self, trait, path=None):
     if path is None:
         path = self.tarball_path
     tt = TraitTemplate(self.conn, self.suite)
     tt.set_trait(trait)
     backup_trait(self.conn, self.suite, trait, path)
     dialogs.Message('%s exported to %s' % (trait, path))
Beispiel #4
0
 def __init__(self,
              conn,
              cfg,
              suite,
              trait,
              package,
              template,
              extracted=None):
     MyNotebook.__init__(self)
     self.conn = conn
     self.cmd = TraitTemplate(self.conn, suite)
     self.cfg = cfg
     self.trait = trait
     self._tmp_path = '/nowhere'
     self.template_path = '/nowhere'
     self.workspace = TemplateEnvironment(self.conn)
     self.template_view = TemplateEnvironment(self.conn)
     self.original_view = TemplateEnvironment(self.conn)
     self.set_suite(suite)
     self.workspace.set_trait(trait)
     self.template_view.set_trait(trait)
     self.original_view.set_trait(trait)
     self.template_view.editor.set_editable(False)
     self.original_view.editor.set_editable(False)
     self.append_page(self.workspace, 'workspace')
     self.append_page(self.template_view, 'template')
     self.append_page(self.original_view, 'original')
     self.extracted = extracted
     self.set_template(package, template)
Beispiel #5
0
 def __init__(self, conn, suite, trait):
     self.menu = make_menu(TEMPL_CMDS, self.template_command)
     self.conn = conn
     self.current_trait = trait
     self.traittemplate = TraitTemplate(self.conn, suite)
     self.traittemplate.set_trait(self.current_trait)
     self.traitpackage = TraitPackage(self.conn, suite)
     self.traitpackage.set_trait(self.current_trait)
     self.current_template = None
     self.cfg = PaellaConfig()
     self.template_path = '/nowhere'
     self._tmp_path = '/nowhere'
     self.tarball_path = self.cfg.get('management_gui', 'bkuptarball_path')
     ListNoteBook.__init__(self)
     self.set_suite(suite)
     self.extracted = None
     self.reset_rows()
     self.dialogs = {}.fromkeys(['attach', 'rootsel'])
Beispiel #6
0
    def __init__(self, conn, suite):
        Installer.__init__(self, conn)
        self.traitpackage = TraitPackage(conn, suite)
        self.traittemplate = TraitTemplate(conn, suite)
        self.traitscripts = TraitScript(conn, suite)
        self.profiledata = {}
        self.mtypedata = {}
        self.familydata = {}
        self.trait_processes = DEFAULT_PROCESSES
        if self.defenv.has_option('installer', 'trait_processes'):
            self.trait_processes = self.defenv.get_list(
                'trait_processes', 'installer')

        self._process_map = {
            'pre': self._process_pre,
            'remove': self._process_remove,
            'install': self._process_install,
            'templates': self._process_templates,
            'config': self._process_config,
            'chroot': self._process_chroot,
            'reconfig': self._process_reconfig,
            'post': self._process_post
        }
 def __init__(self, conn, suite, trait):
     self.menu = make_menu(TEMPL_CMDS, self.template_command)
     self.conn = conn
     self.current_trait = trait
     self.traittemplate = TraitTemplate(self.conn, suite)
     self.traittemplate.set_trait(self.current_trait)
     self.traitpackage = TraitPackage(self.conn, suite)
     self.traitpackage.set_trait(self.current_trait)
     self.current_template = None
     self.cfg = PaellaConfig()
     self.template_path = '/nowhere'
     self._tmp_path = '/nowhere'
     self.tarball_path = self.cfg.get('management_gui', 'bkuptarball_path')
     ListNoteBook.__init__(self)
     self.set_suite(suite)
     self.extracted = None
     self.reset_rows()
     self.dialogs = {}.fromkeys(['attach', 'rootsel'])
Beispiel #8
0
 def __init__(self, conn, suite):
     Installer.__init__(self, conn)
     self.traitpackage = TraitPackage(conn, suite)
     self.traittemplate = TraitTemplate(conn, suite)
     self.traitscripts = TraitScript(conn, suite)
     self.profiledata = {}
     self.mtypedata = {}
     self.familydata = {}
     self.trait_processes = DEFAULT_PROCESSES
     if self.defenv.has_option('installer', 'trait_processes'):
         self.trait_processes = self.defenv.get_list('trait_processes', 'installer')
         
     self._process_map = {
         'pre' : self._process_pre,
         'remove' : self._process_remove,
         'install' : self._process_install,
         'templates' : self._process_templates,
         'config' : self._process_config,
         'chroot' : self._process_chroot,
         'reconfig' : self._process_reconfig,
         'post' : self._process_post
         }
Beispiel #9
0
class TraitList(KListView):
    def __init__(self, app, parent, ftype='template', name='TraitList'):
        KListView.__init__(self, parent, name)
        dbwidget(self, app)
        self.ftype = ftype
        self.scripts = None
        self.templates = None
        self.traits = None
        self.trait = None
        self.setRootIsDecorated(True)
        self.addColumn('trait/file')
        self.addColumn('name')
        self.addColumn('package')
        
    def set_suite(self, suite):
        self.scripts = TraitScript(self.conn, suite)
        self.templates = TraitTemplate(self.conn, suite)
        self.traits = Traits(self.conn, suite)
        

    def set_trait(self, trait):
        self.trait = trait
        
    def refreshlistView(self):
        self.clear()
        if self.ftype == 'template':
            self.setColumnText(1, 'template')
        elif self.ftype == 'script':
            self.setColumnText(1, 'script')
        if self.trait is None:
            traits = self.traits.list()
        else:
            traits = [self.trait]
        for trait in traits:
            item = KListViewItem(self, trait)
            item.trait = trait
            if self.ftype == 'template':
                for row in self.templates.templates(trait):
                    titem = KListViewItem(item, str(row.templatefile), row.template, row.package)
                    titem.trait = trait
                    titem.row = row
            elif self.ftype == 'script':
                for row in self.scripts.cmd.select(clause=Eq('trait', trait), order='script'):
                    sitem = KListViewItem(item, str(row.scriptfile), row.script)
                    sitem.trait = trait
                    sitem.row = row
                    
    def getData(self):
        item = self.currentItem()
        if self.ftype == 'template':
            self.templates.set_trait(item.trait)
            return self.templates.templatedata(item.row.package, item.row.template)
        elif self.ftype == 'script':
            self.scripts.set_trait(item.trait)
            return self.scripts.scriptdata(item.row.script)

    def updateData(self, data):
        item = self.currentItem()
        row = item.row
        if self.ftype == 'template':
            self.templates.set_trait(item.trait)
            self.templates.update_templatedata(row.package, row.template, data)
        elif self.ftype == 'script':
            self.scripts.set_trait(item.trait)
            self.scripts.update_scriptdata(row.script, data)
Beispiel #10
0
 def __set_suitecursors__(self):
     self.traitparent = TraitParent(self.conn, self.suite)
     self.traittemplate = TraitTemplate(self.conn, self.suite)
     self.traitpackage = TraitPackage(self.conn, self.suite)
Beispiel #11
0
class TraitInstaller(Installer):
    def __init__(self, conn, suite):
        Installer.__init__(self, conn)
        self.traitpackage = TraitPackage(conn, suite)
        self.traittemplate = TraitTemplate(conn, suite)
        self.traitscripts = TraitScript(conn, suite)
        self.profiledata = {}
        self.mtypedata = {}
        self.familydata = {}
        self.trait_processes = DEFAULT_PROCESSES
        if self.defenv.has_option('installer', 'trait_processes'):
            self.trait_processes = self.defenv.get_list('trait_processes', 'installer')
            
        self._process_map = {
            'pre' : self._process_pre,
            'remove' : self._process_remove,
            'install' : self._process_install,
            'templates' : self._process_templates,
            'config' : self._process_config,
            'chroot' : self._process_chroot,
            'reconfig' : self._process_reconfig,
            'post' : self._process_post
            }
        
    def set_trait(self, trait):
        self.traitpackage.set_trait(trait)
        self.traittemplate.set_trait(trait)
        self.traitscripts.set_trait(trait)
        self._current_trait_ = trait
        self.packages = self.traitpackage.packages()
        self.templates = self.traittemplate.templates()
        os.environ['PAELLA_TRAIT'] = trait
        self.log.info('trait set to %s' % self._current_trait_)

    def _info(self):
        return self._current_trait_, self.packages, self.templates
    
    def _process_pre(self):
        self.process_prepost_script('pre', self._current_trait_)

    def _process_remove(self):
        trait, packages, templates = self._info()
        self.process_packages(trait, 'remove', packages)

    def _process_install(self):
        trait, packages, templates = self._info()
        self.process_packages(trait, 'install', packages, templates)

    def _process_config(self):
        trait, packages, templates = self._info()
        script = self._make_script('config')
        if script is None:
            self.log.info('there is no config script for trait %s' % trait)
        else:
            self.process_hooked_action(script, 'config', trait)

    def _process_generic_script(self, action):
        trait, packages, templates = self._info()
        script = self._make_script(action)
        if script is None:
            self.log.info('there is no %s script for trait %s' % (action, trait))
        else:
            self.process_hooked_action(script, action, trait)
        
    def _process_templates(self):
        trait, packages, templates = self._info()
        script = self._make_script('templates')
        if script is None:
            tpackages = [p for p in packages if p.action in ['install', 'config']]
            if len(tpackages) or len(templates):
                self.install_templates(tpackages, templates)
        else:
            self.process_hooked_action(script, 'templates', trait)
        
    def _process_chroot(self):
        trait, packages, templates = self._info()
        script = self._make_script('chroot', execpath=True)
        if script is not None:
            self.log.info('chroot exists for trait %s' % trait)
            self.run('chroot-script', script)
            if script[0] == '/':
                script = script[1:]
            os.remove(join(self.target, script))
        
    def _process_reconfig(self):
        trait, packages, templates = self._info()
        script = self._make_script('reconfig')
        if script is None:
            self.reconfigure_debconf(packages)
        else:
            self.process_hooked_action(script, 'reconfig', trait)
        
    def _process_post(self):
        self.process_prepost_script('post', self._current_trait_)

    def process(self):
        trait, packages, templates = self._info()
        self.log.info('processing trait:  %s' % trait)
        if 'PAELLA_TARGET' not in os.environ.keys():
            self.log.warn('PAELLA_TARGET not set.')
            os.environ['PAELLA_TARGET'] = self.target
        machine = None
        curenv = {}
        if 'PAELLA_MACHINE' in os.environ.keys():
            machine = os.environ['PAELLA_MACHINE']
            curenv = CurrentEnvironment(self.conn, machine)
        if machine is not None:
            self.log.info('processing trait %s on machine %s' % (trait, machine))
        curenv['current_trait'] = trait
        for proc in self.trait_processes:
            self.log.info('processing %s for trait %s' % (proc, trait))
            curenv['current_trait_process'] = proc
            if proc in self._process_map.keys():
                self._process_map[proc]()
            else:
                self._process_generic_script(proc)
            self.log.info('%s has been processed for trait %s' % (proc, trait))
            
    
    def run(self, name, command, args='', proc=False, chroot=True,
            keeprunning=False):
        tname = 'trait-%s-%s' % (self._current_trait_, name)
        self.log.info('running %s' % tname)
        runvalue = Installer.run(self, tname, command, args=args, proc=proc,
                                 chroot=chroot,
                                 keeprunning=keeprunning)
        return runvalue

    def runscript(self, script, name, info, chroot=False):
        self.log.info(info['start'])
        trait = self._current_trait_
        self.log.info('running script %s for trait %s' % (script, trait))
        runvalue = self.run(name, script, chroot=chroot)
        os.remove(script)
        self.log.info(info['done'])

    # prepost is either 'pre' or 'post'
    def process_prepost_script(self, prepost, trait):
        script = self._make_script(prepost)
        runvalue = 0
        if script is not None:
            info = dict(start='%s script started' % prepost,
                        done='%s script done' % prepost)
            runvalue = self.runscript(script, '%s-script' % prepost, info)
        else:
            self.log.info('no %s script for trait %s' % (prepost, trait))
        if runvalue:
            raise InstallError, 'Error in running %s script for %s' % (prepost, trait)

    def process_hooked_action(self, script, action, trait):
        self.log.info('%s has been hooked for trait %s' % (action, trait))
        info = dict(start='%s script started' % action,
                    done='%s script done' % action)
        runvalue = self.runscript(script, '%s-script' % action, info)
        if runvalue:
            InstallError, 'hooked action %s failed on trait %s' % (action, trait)
        
    def process_packages(self, trait, action, packages, templates=[]):
        script = self._make_script(action)
        if script is None:
            affected = [p for p in packages if p.action == action]
            length = len(affected)
            if length:
                ing = INGDICT[action]
                stmt = '%s %d packages for trait %s' % (ing, length, trait)
                self.log.info(stmt)
                if action == 'remove':
                    self.remove(affected)
                elif action == 'install':
                    self.install(affected, templates)
                else:
                    raise InstallError, '%s not implemented in process_packages'
        else:
            self.process_hooked_action(script, action, trait)
        
    def remove(self, packages):
        packages = ' '.join([p.package for p in packages])
        command, args = 'apt-get -y remove', packages
        runvalue = self.run('remove', command, args=args, proc=True)
        if runvalue:
            self.log.warn('Problem removing packages %s' % ', '.join(packages))
            
                
    def install(self, packages, templates):
        trait = self._current_trait_
        package_args = ' '.join([p.package for p in packages])
        cmd = 'apt-get -y --force-yes install %s' % package_args
        stmt = 'install command for %s is %s' % (trait, cmd)
        self.log.info(stmt)
        runvalue = self.run('install', cmd, proc=False, keeprunning=False)
        if runvalue:
            self.log.warn('PROBLEM installing %s' % trait)
            self.log.warn('packages --> %s' % package_args)
            raise InstallError, 'problem installing packages'
        runvalue = remove_debs(self.target)
        if runvalue:
            self.log.warn('PROBLEM removing downloaded debs')
            raise InstallError, 'problem removing downloaded debs'
        
    def install_templates(self, packages, templates):
        trait = self._current_trait_
        num = len(templates)
        stmt = 'in install_templates, there are %d templates for trait %s' % (num, trait)
        self.log.info(stmt)
        for p in packages:
            for t in [t for t in templates if t.package == p.package]:
                if t.template == 'var/cache/debconf/config.dat':
                    self.log.info('Installing Debconf template ...')
                    self.install_debconf_template(t)
                else:
                    self.make_template(t)
            
    def configure(self, packages, templates):
        trait = self._current_trait_
        stmt = 'in configure, there are %d templates for trait %s' % (len(templates), trait)
        self.log.info(stmt)
        for p in packages:
            for t in [t for t in templates if t.package == p.package]:
                if t.template == 'var/cache/debconf/config.dat':
                    self.log.info('Installing Debconf template ...')
                    self.install_debconf_template(t)
                else:
                    self.make_template(t)
            
    def _make_script(self, name, execpath=False):
        script = self.traitscripts.get(name)
        if script is not None:
            stmt = '%s script exists for trait %s' % (name, self._current_trait_)
            self.log.info(stmt)
            return make_script(name, script, self.target, execpath=execpath)
        else:
            return None
        
    def _make_scriptOrig(self, name, execpath=False):
        script = self.traitscripts.get(name)
        if script is not None:
            stmt = '%s script exists for trait %s' % (name, self._current_trait_)
            self.log.info(stmt)
            exec_path = join('/tmp', name + '-script')
            target_path = join(self.target, 'tmp', name + '-script')
            sfile = file(target_path, 'w')
            sfile.write(script.read())
            sfile.close()
            os.system('chmod 755 %s' % target_path)
            if not execpath:
                return target_path
            else:
                return exec_path
        else:
            return None
        
    def make_template(self, template):
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self._update_templatedata()
        self._make_template_common(template, tmpl)
        

    def make_template_with_data(self, template, data):
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self.traittemplate.template.update(data)
        self._make_template_common(template, tmpl)
        
    def _make_template_common(self, template, tmpl):
        sub = self.traittemplate.template.sub()
        newpath = join(self.target, template.template)
        bkuppath = join(self.target, self.paelladir, 'original_files', template.template)
        makepaths(dirname(newpath), dirname(bkuppath))
        self.log.info('target template %s' % newpath)
        if tmpl != sub:
            self.log.info('%s %s subbed' % (template.package, template.template))
        if isfile(newpath):
            if not isfile(bkuppath):
                os.system('mv %s %s' % (newpath, dirname(bkuppath)))
                self.log.info('%s backed up' % template.template)
            else:
                self.log.info('overwriting previously installed template %s' % template.template)
        else:
            self.log.info('installing new template %s' % template.template)
        newfile = file(newpath, 'w')
        newfile.write(sub)
        newfile.close()
        mode = template.mode
        
        # a simple attempt to insure mode is a valid octal string
        # this is one of the very rare places eval is used
        # there are a few strings with 8's and 9's that will pass
        # the if statement, but the eval will raise SyntaxError then.
        # If the mode is unusable the install will fail at this point.
        if mode[0] == '0' and len(mode) <= 7 and mode.isdigit():
            mode = eval(mode)
            os.chmod(newpath, mode)
        else:
            raise InstallError, 'bad mode %s, please use octal prefixed by 0' % mode
        
        own = ':'.join([template.owner, template.grp_owner])
        os.system(self.command('chown', "%s '%s'" %(own, join('/', template.template))))

    def _update_templatedata(self):
        self.traittemplate.template.update(self.familydata)
        self.traittemplate.template.update(self.mtypedata)
        self.traittemplate.template.update(self.profiledata)
        
    def install_debconf_template(self, template):
        trait = self._current_trait_
        self.log.info('Installing debconf for %s' % trait)
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self._update_templatedata()
        sub = self.traittemplate.template.sub()
        if tmpl == sub:
            self.log.info('static debconf, no substitutions')
            self.log.info('for trait %s ' % trait)
        else:
            self.log.info('templated debconf for trait %s' % trait)
        config_path = join(self.target, 'tmp/paella_debconf')
        if isfile(config_path):
            self.log.warn('%s is not supposed to be there' % config_path)
            raise Error, '%s is not supposed to be there' % config_path
        debconf = file(config_path, 'w')
        debconf.write(sub + '\n')
        debconf.close()
        target_path = join(self.target, 'var/cache/debconf/config.dat')
        self.log.info('debconf config is %s %s' % (config_path, target_path))
        copy_configdb(config_path, target_path)
        os.remove(config_path)

    def set_template_path(self, path):
        self.traittemplate.template.set_path(path)

    def reconfigure_debconf(self, packages):
        self.log.info('running reconfigure')
        reconfig = [p.package for p in packages if p.action == 'reconfig']
        os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
        for package in reconfig:
            self.log.info('RECONFIGURING %s' % package)
            os.system(self.command('dpkg-reconfigure -plow %s' % package))
Beispiel #12
0
class TemplateEnvironment(ScrolledWindow):
    def __init__(self, conn):
        ScrolledWindow.__init__(self)
        self.conn = conn
        self.editor = TemplateEditor()
        self.menu = SimpleMenu()
        self.editor.connect('populate-popup', self.populate_menu, self.menu)
        self.add_with_viewport(self.editor)
        self.show()
        
    def set_suite(self, suite):
        self.suite = suite
        self.__set_suitecursors__()

    def __set_suitecursors__(self):
        self.traitparent = TraitParent(self.conn, self.suite)
        self.traittemplate = TraitTemplate(self.conn, self.suite)
        self.traitpackage = TraitPackage(self.conn, self.suite)
        
    def set_trait(self, trait):
        self.trait = trait
        self.traitparent.set_trait(trait)
        self.traittemplate.set_trait(trait)
        self.traitpackage.set_trait(trait)
        self.__set_environment__()

    def __set_environment__(self):
        self.environ = dict(self.traitparent.get_environment([self.trait]))
        self.sections = dict([(k, v.keys()) for k,v in self.environ.items()])

    def populate_menu(self, widget, mainmenu, tmenu):
        self.__set_environment__()
        populate_menu(mainmenu, '_define_', ['_create_'], self.make_new_tag)
        for section, vars in self.sections.items():
            populate_menu(mainmenu, section, vars, self.make_tag)

    def make_tag(self, menuitem, name, parent):
        parentname = parent.get_name()
        tagname = ujoin(parentname, name)
        self.editor.make_tag(tagname)

    def make_new_tag(self, menuitem, name, parent):
        parentname = parent.get_name()
        if (parentname, name) == ('_define_', '_create_'):
            self.editor.create_new_tag(self.create_entry)
            
    def create_entry(self, *args):
        var = dict(self.editor.dialogs['create'].items())
        debug('var is %s' % var)
        self.environ[self.trait][var['name']] = var['value']
        tagname = ujoin(self.trait, var['name'])
        self.editor.make_tag(tagname)
        self.editor.destroy_dialog(self.editor.dialogs['create'])

    def preview(self, *args):
        subs = self.traitparent.get_superdict(self.environ.keys())
        self.editor.buffer.set_subs(subs)
        self.editor.preview(*args)

    def set_text(self, text):
        self.editor.buffer.set_text(text)

    def get_text(self):
        return self.editor.get_text()
Beispiel #13
0
class TemplateEnvironment(ScrolledWindow):
    def __init__(self, conn):
        ScrolledWindow.__init__(self)
        self.conn = conn
        self.editor = TemplateEditor()
        self.menu = SimpleMenu()
        self.editor.connect('populate-popup', self.populate_menu, self.menu)
        self.add_with_viewport(self.editor)
        self.show()

    def set_suite(self, suite):
        self.suite = suite
        self.__set_suitecursors__()

    def __set_suitecursors__(self):
        self.traitparent = TraitParent(self.conn, self.suite)
        self.traittemplate = TraitTemplate(self.conn, self.suite)
        self.traitpackage = TraitPackage(self.conn, self.suite)

    def set_trait(self, trait):
        self.trait = trait
        self.traitparent.set_trait(trait)
        self.traittemplate.set_trait(trait)
        self.traitpackage.set_trait(trait)
        self.__set_environment__()

    def __set_environment__(self):
        self.environ = dict(self.traitparent.get_environment([self.trait]))
        self.sections = dict([(k, v.keys()) for k, v in self.environ.items()])

    def populate_menu(self, widget, mainmenu, tmenu):
        self.__set_environment__()
        populate_menu(mainmenu, '_define_', ['_create_'], self.make_new_tag)
        for section, vars in self.sections.items():
            populate_menu(mainmenu, section, vars, self.make_tag)

    def make_tag(self, menuitem, name, parent):
        parentname = parent.get_name()
        tagname = ujoin(parentname, name)
        self.editor.make_tag(tagname)

    def make_new_tag(self, menuitem, name, parent):
        parentname = parent.get_name()
        if (parentname, name) == ('_define_', '_create_'):
            self.editor.create_new_tag(self.create_entry)

    def create_entry(self, *args):
        var = dict(self.editor.dialogs['create'].items())
        debug('var is %s' % var)
        self.environ[self.trait][var['name']] = var['value']
        tagname = ujoin(self.trait, var['name'])
        self.editor.make_tag(tagname)
        self.editor.destroy_dialog(self.editor.dialogs['create'])

    def preview(self, *args):
        subs = self.traitparent.get_superdict(self.environ.keys())
        self.editor.buffer.set_subs(subs)
        self.editor.preview(*args)

    def set_text(self, text):
        self.editor.buffer.set_text(text)

    def get_text(self):
        return self.editor.get_text()
Beispiel #14
0
class TemplateBrowser(ListNoteBook):
    def __init__(self, conn, suite, trait):
        self.menu = make_menu(TEMPL_CMDS, self.template_command)
        self.conn = conn
        self.current_trait = trait
        self.traittemplate = TraitTemplate(self.conn, suite)
        self.traittemplate.set_trait(self.current_trait)
        self.traitpackage = TraitPackage(self.conn, suite)
        self.traitpackage.set_trait(self.current_trait)
        self.current_template = None
        self.cfg = PaellaConfig()
        self.template_path = '/nowhere'
        self._tmp_path = '/nowhere'
        self.tarball_path = self.cfg.get('management_gui', 'bkuptarball_path')
        ListNoteBook.__init__(self)
        self.set_suite(suite)
        self.extracted = None
        self.reset_rows()
        self.dialogs = {}.fromkeys(['attach', 'rootsel'])

    def reset_rows(self):
        rows = self.traittemplate.templates(fields=['template', 'package'])
        self.set_rows(rows)
        self.set_row_select(self.template_selected)
        self.templates = [r.template for r in rows]

    def template_selected(self, listbox, row, column, event):
        row = listbox.get_selected_data()[0]
        self.set_template(row.package, row.template)

    def set_template(self, package, template):
        self.current_package = package
        self.current_template = template
        if template not in self.pages:
            trait = self.current_trait
            nbook = TemplateNotebook(self.conn, self.cfg, self.suite, trait,
                                     package, template, self.extracted)
            self.append_page(nbook, template)
        else:
            self.set_current_page(template)

    def template_filename(self, package, template):
        tpath = join(self.template_path, self.suite, self.current_trait)
        return join(tpath, package, template + '.template')

    def suite_template_path(self, filesel=False):
        path = join(self.template_path, self.suite)
        if filesel:
            path += '/'
        return path

    def trait_temp_path(self, filesel=False):
        path = join(self._tmp_path, self.suite, self.current_trait)
        if filesel:
            path += '/'
        return path

    def set_suite(self, suite):
        self.suite = suite

    def save_template(self):
        try:
            template = self.get_selected_data()[0].template
            self.pages[template].save_template()
        except IndexError:
            dialogs.Message('a template must be selected')

    def edit_template(self):
        try:
            template = self.get_selected_data()[0].template
            self.pages[template].edit_template()
        except IndexError:
            dialogs.Message('a template must be selected')

    def template_command(self, meuitem, command):
        if command in ['new']:
            path = self.trait_temp_path(filesel=True)
            select_a_file('new', path, self.fileselect_ok)
        elif command == 'diff':
            path = self.suite_template_path(filesel=True)
            select_a_file('load', path, self.fileselect_ok)
        elif command == 'save':
            self.save_template()
        elif command == 'load':
            path = self.suite_template_path(filesel=True)
            select_a_file('load', path, self.fileselect_ok)
        elif command == 'done':
            try:
                template = self.get_selected_data()[0].template
                self.remove_page(template)
            except IndexError:
                pass
        elif command in ['drop', 'real_templates', 'other_templates']:
            rows = self.get_selected_data()
            row = None
            print row, rows, len(rows)
            if len(rows):
                row = rows[0]
            else:
                dialogs.Message('a template must be selected')
                print row, rows
            if row:
                package, template = row.package, row.template
                print row, 'ifrow'
                if command == 'drop':
                    self.traittemplate.drop_template(package, template)
                    self.remove_page(template)
                elif command == 'real_templates':
                    self.traittemplate.prefix_template(package,
                                                       template,
                                                       type_='real')
                    print 'make it real_templates'
                elif command == 'other_templates':
                    self.traittemplate.prefix_template(package,
                                                       template,
                                                       type_='other')
                    print 'make it other_templates'
                self.reset_rows()
        elif command == 'root':
            if self.dialogs['rootsel'] is None:
                path = self.tarball_path + '/'
                select_a_file('root', path, self.tarball_selected)
        elif command == 'edit':
            self.edit_template()

    def tarball_selected(self, button, fileselect):
        path = fileselect.get_filename()
        fileselect.destroy()
        self.dialogs['rootsel'] = select_from_tarfile('heydude', path,
                                                      self.pull_from_tar)

    def pull_from_tar(self, button, fileselect):
        info, tfile = fileselect.extract_file()
        template = tfile.name
        action = 'attach'
        if self.dialogs[action] is None:
            msg = 'attach to which package?'
            self.dialogs[action] = dialogs.CList(msg, name=action)
            lbox = self.dialogs[action]
            lbox.set_rows(self.traitpackage.packages())
            lbox.set_ok(self.insert_new_template_from_tar)
            lbox.set_cancel(self.destroy_dialog)
            lbox.set_data('tarmember', (info, tfile))

    def insert_new_template_from_tar(self, button):
        lbox = self.dialogs['attach']
        rows = lbox.get_selected_data()
        if len(rows) == 1:
            package = rows[0].package
            info, tfile = lbox.get_data('tarmember')
            self.destroy_dialog(lbox)
            self._insert_new_template(package, info.name, tfile, info)

    def _insert_new_template(self, package, template, fileobj, info):
        fileobj.seek(0)
        filename = self.template_filename(package, template)
        data = dict(owner=info.uname,
                    grp_owner=info.gname,
                    mode=oct(info.mode),
                    package=package,
                    template=template)
        self.traittemplate.insert_template(data, fileobj)
        self.set_template(package, template)
        self.reset_rows()

    def insert_new_template(self, package, template, path):
        newfile = readfile(path)
        filename = self.template_filename(package, template)
        makepaths(dirname(filename))
        writefile(filename, newfile)
        try:
            self.traittemplate.insert_template(
                dict(package=package, template=template), file(path))
        except OperationalError:
            dialogs.Message('template already exists')
        self.set_template(package, template)
        self.reset_rows()

    def load_template(self, path):
        template, package = get_file_path(path, self.suite_template_path())
        if template[-9:] == '.template':
            template = template[:-9]
            if template not in self.templates:
                try:
                    orig = self.find_original(template)
                    self.insert_new_template(package, template, path)
                except NoExistError:
                    dialogs.Message('%s not in extracted packages' % template)

    def _diff(self, template, path):
        current_template = self.template_filename(template)
        os.popen2('meld %s %s' % (current_template, path))

    def fileselect_ok(self, button, filesel):
        path = filesel.get_filename()
        action = filesel.get_data('action')
        filesel.destroy()
        if action == 'new':
            template, package = get_file_path(path, self.trait_temp_path())
            if template not in self.templates:
                self.insert_new_template(package, template, path)
            else:
                dialogs.Message('template already exists')
        elif action == 'diff':
            pass
        elif action == 'load':
            self.load_template(path)

    def set_extracted(self, extracted):
        self.extracted = extracted
        for page in self.pages.values():
            page.extracted = extracted

    def _extract_packages(self):
        dialogs.Message('deprecated(for now)')
        if False:
            if self.extracted is None:
                packages = [p.package for p in self.traitpackage.packages()]
                path = self.trait_temp_path()
                self.set_extracted(True)
                packs = ',\n'.join(packages)
                message = 'extracted packages:\n %s into:\n%s' % (packs, path)
                dialogs.Message(message)
            else:
                dialogs.Message('already extracted')

    def extract_packages(self):
        self._extract_packages()
Beispiel #15
0
 def __set_suitecursors__(self):
     self.traitparent = TraitParent(self.conn, self.suite)
     self.traittemplate = TraitTemplate(self.conn, self.suite)
     self.traitpackage = TraitPackage(self.conn, self.suite)
Beispiel #16
0
class TraitInstaller(Installer):
    def __init__(self, conn, suite, cfg):
        Installer.__init__(self, conn, cfg=cfg)
        self.traitpackage = TraitPackage(conn, suite)
        self.traittemplate = TraitTemplate(conn, suite)
        self.traitscripts = TraitScript(conn, suite)
        self.profiledata = {}
        self.familydata = {}
        
    def set_trait(self, trait):
        self.traitpackage.set_trait(trait)
        self.traittemplate.set_trait(trait)
        self.traitscripts.set_trait(trait)
        self._current_trait_ = trait
        self.log.info('trait set to %s' % self._current_trait_)

    def run(self, name, command, args='', proc=False, chroot=True,
            keeprunning=False):
        tname = 'trait-%s-%s' % (self._current_trait_, name)
        self.log.info('running %s' % tname)
        Installer.run(self, tname, command, args=args, proc=proc,
                      chroot=chroot,
                      keeprunning=keeprunning)
        

    def runscript(self, script, name, info, chroot=False):
        self.log.info(info['start'])
        trait = self._current_trait_
        self.log.info('running script %s trait is currently %s' % (script, trait))
        self.run(name, script, chroot=chroot)
        os.remove(script)
        self.log.info(info['done'])
        
    def process(self):
        trait = self._current_trait_
        self.log.info('processing trait:  %s' % trait)
        os.environ['PAELLA_TARGET'] = self.target
        os.environ['PAELLA_TRAIT'] = trait
        packages = self.traitpackage.packages()
        templates = self.traittemplate.templates()
        stmt = 'this trait has %d packages and %d templates' % (len(packages), len(templates))
        self.log.info(stmt)
        #start pre script
        self.log.info('processing pre for trait %s' % trait)
        script = self._make_script('pre')
        if script is not None:
            info = dict(start='pre script started',
                        done='pre script done')
            self.runscript(script, 'pre-script', info)
        self.log.info('pre has been processed for trait %s' % trait)
        
        #remove packages
        self.log.info('processing remove for trait %s' % trait)
        script = self._make_script('remove')
        if script is None:
            remove = [p for p in packages if p.action == 'remove']
            if len(remove):
                stmt = 'removing %d packages for trait %s' % (len(remove), trait)
                self.log.info(stmt)
                self.remove(remove)
        else:
            self.log.info('remove has been hooked for trait %s' % trait)
            info = dict(start='remove script started',
                        done='remove script done')
            self.runscript(script, 'remove-script', info)
        self.log.info('remove has been processed for trait %s' % trait)

        #install packages
        self.log.info('processing install for trait %s' % trait)
        script = self._make_script('install')
        if script is None:
            install = [p for p in packages if p.action == 'install']
            if len(install):
                stmt = 'installing %d packages for trait %s' % (len(install), trait)
                self.log.info(stmt)
                self.install(install, templates)
        else:
            self.log.info('install has been hooked for trait %s' % trait)
            info = dict(start='install script started',
                        done='install script done')
            self.runscript(script, 'install-script', info)
        self.log.info('install has been processed for trait %s' % trait)
        
        #configure packages
        self.log.info('processing config for trait %s' % trait)
        script = self._make_script('config')
        if script is None:
            config = [p for p in packages if p.action in ['install', 'config']]
            if len(config):
                self.configure(config, templates)
        else:
            self.log.info('config has been hooked for trait %s' % trait)
            info = dict(start='config script started',
                        done='config script done')
            self.runscript(script, 'config-script', info)
        self.log.info('config has been processed for trait %s' % trait)
        
        #reconfigure debconf
        self.log.info('processing reconfig for trait %s' % trait)
        script = self._make_script('reconfig')
        if script is None:
            self.reconfigure_debconf()
        else:
            self.log.info('reconfig has been hooked for trait %s' % trait)
            info = dict(start='reconfig script started',
                        done='reconfig script done')
            self.runscript(script, 'reconfig-script', info)
        self.log.info('reconfig has been processed for trait %s' % trait)
        
        #start post script
        self.log.info('processing post for trait %s' % trait)
        script = self._make_script('post')
        if script is not None:
            info = dict(start='post script started',
                        done='post script done')
            self.runscript(script, 'post-script', info)
        else:
            self.log.info('no post script for trait %s' % trait)
        self.log.info('post has been processed for trait %s' % trait)
        
    def remove(self, packages):
        packages = ' '.join([p.package for p in packages])
        command, args = 'apt-get -y remove', packages
        self.run('remove', command, args=args, proc=True)
                
    def install(self, packages, templates):
        trait = self._current_trait_
        package_args = ' '.join([p.package for p in packages])
        cmd = 'apt-get -y --force-yes install %s\n' % package_args
        cmd += 'rm /var/cache/apt/archives/*.deb -f'
        stmt = 'install command for %s is %s' % (cmd, trait)
        self.log.info(stmt)
        run = self.run('install', cmd, proc=True, keeprunning=True)
        if run:
            self.log.warn('PROBLEM installing %s' % trait)
            self.log.warn('packages --> %s' % package_args)
            

    def configure(self, packages, templates):
        dpkg_rec = False
        trait = self._current_trait_
        stmt = 'in configure, there are %d templates for trait %s' % (len(templates), trait)
        self.log.info(stmt)
        for p in packages:
            for t in [t for t in templates if t.package == p.package]:
                if t.template == 'var/cache/debconf/config.dat':
                    dpkg_rec = True
                    self.log.info('Installing Debconf template ...')
                    self.install_debconf_template(t)
                else:
                    self.make_template(t)
        if dpkg_rec:
            self.log.info('Reconfiguring %d packages' % len(packages))
            for p in packages:
                cmd = 'dpkg-reconfigure -plow %s' % p.package
                run = self.run('dpkg-recfg', cmd, proc=True, keeprunning=True)
                if run:
                    self.log.info('reconfigure failed on %s' % p.package)
                else:
                    self.log.info('%s reconfigured' % p.package)
        script = self._make_script('chroot', execpath=True)
        if script is not None:
            self.log.info('chroot exists for trait %s' % trait)
            self.run('chroot-script', script)
            if script[0] == '/':
                script = script[1:]
            os.remove(join(self.target, script))
            
    def _make_script(self, name, execpath=False):
        script = self.traitscripts.get(name)
        if script is not None:
            stmt = '%s script exists for trait %s' % (name, self._current_trait_)
            self.log.info(stmt)
            exec_path = join('/tmp', name + '-script')
            target_path = join(self.target, 'tmp', name + '-script')
            sfile = file(target_path, 'w')
            sfile.write(script.read())
            sfile.close()
            os.system('chmod 755 %s' % target_path)
            if not execpath:
                return target_path
            else:
                return exec_path
        else:
            return None
        
    def make_template(self, template):
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self.traittemplate.template.update(self.familydata)
        self.traittemplate.template.update(self.profiledata)
        self._make_template_common(template, tmpl)
        

    def make_template_with_data(self, template, data):
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self.traittemplate.template.update(data)
        self._make_template_common(template, tmpl)
        
    def _make_template_common(self, template, tmpl):
        sub = self.traittemplate.template.sub()
        newpath = join(self.target, template.template)
        bkuppath = join(self.target, self.paelladir, 'original_files', template.template)
        makepaths(dirname(newpath), dirname(bkuppath))
        self.log.info('target template %s' % newpath)
        if tmpl != sub:
            self.log.info('%s %s subbed' % (template.package, template.template))
        if isfile(newpath):
            if not isfile(bkuppath):
                os.system('mv %s %s' % (newpath, dirname(bkuppath)))
                self.log.info('%s backed up' % template.template)
            else:
                self.log.info('overwriting previously installed template %s' % template.template)
        else:
            self.log.info('installing new template %s' % template.template)
        newfile = file(newpath, 'w')
        newfile.write(sub)
        newfile.close()
        mode = template.mode
        if mode[0] == '0' and len(mode) <= 7 and mode.isdigit():
            mode = eval(mode)
        os.chmod(newpath, mode)
        own = ':'.join([template.owner, template.grp_owner])
        os.system(self.command('chown', "%s '%s'" %(own, join('/', template.template))))

    def install_debconf_template(self, template):
        trait = self._current_trait_
        self.log.info('Installing debconf for %s' % trait)
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self.traittemplate.template.update(self.profiledata)
        sub = self.traittemplate.template.sub()
        if tmpl == sub:
            self.log.info('static debconf, no substitutions')
            self.log.info('for trait %s ' % trait)
        config_path = join(self.target, 'tmp/paella_debconf')
        if isfile(config_path):
            self.log.warn('%s is not supposed to be there' % config_path)
            raise Error, '%s is not supposed to be there' % config_path
        debconf = file(config_path, 'w')
        debconf.write(sub + '\n')
        debconf.close()
        target_path = join(self.target, 'var/cache/debconf/config.dat')
        self.log.info('debconf config is %s %s' % (config_path, target_path))
        copy_configdb(config_path, target_path)
        os.remove(config_path)

    def set_template_path(self, path):
        self.traittemplate.template.set_path(path)

    def install_debconf(self):
        raise Error, 'install_debconf is deprecated, use install_debconf_template instead'
    
    def reconfigure_debconf(self):
        raise Error, 'reconfigure_debconf is deprecated'
Beispiel #17
0
class TraitList(KListView):
    def __init__(self, app, parent, ftype='template', name='TraitList'):
        KListView.__init__(self, parent, name)
        dbwidget(self, app)
        self.ftype = ftype
        self.scripts = None
        self.templates = None
        self.traits = None
        self.trait = None
        self.setRootIsDecorated(True)
        self.addColumn('trait/file')
        self.addColumn('name')
        self.addColumn('package')

    def set_suite(self, suite):
        self.scripts = TraitScript(self.conn, suite)
        self.templates = TraitTemplate(self.conn, suite)
        self.traits = Traits(self.conn, suite)

    def set_trait(self, trait):
        self.trait = trait

    def refreshlistView(self):
        self.clear()
        if self.ftype == 'template':
            self.setColumnText(1, 'template')
        elif self.ftype == 'script':
            self.setColumnText(1, 'script')
        if self.trait is None:
            traits = self.traits.list()
        else:
            traits = [self.trait]
        for trait in traits:
            item = KListViewItem(self, trait)
            item.trait = trait
            if self.ftype == 'template':
                for row in self.templates.templates(trait):
                    titem = KListViewItem(item, str(row.templatefile),
                                          row.template, row.package)
                    titem.trait = trait
                    titem.row = row
            elif self.ftype == 'script':
                for row in self.scripts.cmd.select(clause=Eq('trait', trait),
                                                   order='script'):
                    sitem = KListViewItem(item, str(row.scriptfile),
                                          row.script)
                    sitem.trait = trait
                    sitem.row = row

    def getData(self):
        item = self.currentItem()
        if self.ftype == 'template':
            self.templates.set_trait(item.trait)
            return self.templates.templatedata(item.row.package,
                                               item.row.template)
        elif self.ftype == 'script':
            self.scripts.set_trait(item.trait)
            return self.scripts.scriptdata(item.row.script)

    def updateData(self, data):
        item = self.currentItem()
        row = item.row
        if self.ftype == 'template':
            self.templates.set_trait(item.trait)
            self.templates.update_templatedata(row.package, row.template, data)
        elif self.ftype == 'script':
            self.scripts.set_trait(item.trait)
            self.scripts.update_scriptdata(row.script, data)
Beispiel #18
0
class TraitInstaller(Installer):
    def __init__(self, conn, suite):
        Installer.__init__(self, conn)
        self.traitpackage = TraitPackage(conn, suite)
        self.traittemplate = TraitTemplate(conn, suite)
        self.traitscripts = TraitScript(conn, suite)
        self.profiledata = {}
        self.mtypedata = {}
        self.familydata = {}
        self.trait_processes = DEFAULT_PROCESSES
        if self.defenv.has_option('installer', 'trait_processes'):
            self.trait_processes = self.defenv.get_list(
                'trait_processes', 'installer')

        self._process_map = {
            'pre': self._process_pre,
            'remove': self._process_remove,
            'install': self._process_install,
            'templates': self._process_templates,
            'config': self._process_config,
            'chroot': self._process_chroot,
            'reconfig': self._process_reconfig,
            'post': self._process_post
        }

    def set_trait(self, trait):
        self.traitpackage.set_trait(trait)
        self.traittemplate.set_trait(trait)
        self.traitscripts.set_trait(trait)
        self._current_trait_ = trait
        self.packages = self.traitpackage.packages()
        self.templates = self.traittemplate.templates()
        os.environ['PAELLA_TRAIT'] = trait
        self.log.info('trait set to %s' % self._current_trait_)

    def _info(self):
        return self._current_trait_, self.packages, self.templates

    def _process_pre(self):
        self.process_prepost_script('pre', self._current_trait_)

    def _process_remove(self):
        trait, packages, templates = self._info()
        self.process_packages(trait, 'remove', packages)

    def _process_install(self):
        trait, packages, templates = self._info()
        self.process_packages(trait, 'install', packages, templates)

    def _process_config(self):
        trait, packages, templates = self._info()
        script = self._make_script('config')
        if script is None:
            self.log.info('there is no config script for trait %s' % trait)
        else:
            self.process_hooked_action(script, 'config', trait)

    def _process_generic_script(self, action):
        trait, packages, templates = self._info()
        script = self._make_script(action)
        if script is None:
            self.log.info('there is no %s script for trait %s' %
                          (action, trait))
        else:
            self.process_hooked_action(script, action, trait)

    def _process_templates(self):
        trait, packages, templates = self._info()
        script = self._make_script('templates')
        if script is None:
            tpackages = [
                p for p in packages if p.action in ['install', 'config']
            ]
            if len(tpackages) or len(templates):
                self.install_templates(tpackages, templates)
        else:
            self.process_hooked_action(script, 'templates', trait)

    def _process_chroot(self):
        trait, packages, templates = self._info()
        script = self._make_script('chroot', execpath=True)
        if script is not None:
            self.log.info('chroot exists for trait %s' % trait)
            self.run('chroot-script', script)
            if script[0] == '/':
                script = script[1:]
            os.remove(join(self.target, script))

    def _process_reconfig(self):
        trait, packages, templates = self._info()
        script = self._make_script('reconfig')
        if script is None:
            self.reconfigure_debconf(packages)
        else:
            self.process_hooked_action(script, 'reconfig', trait)

    def _process_post(self):
        self.process_prepost_script('post', self._current_trait_)

    def process(self):
        trait, packages, templates = self._info()
        self.log.info('processing trait:  %s' % trait)
        if 'PAELLA_TARGET' not in os.environ.keys():
            self.log.warn('PAELLA_TARGET not set.')
            os.environ['PAELLA_TARGET'] = self.target
        machine = None
        curenv = {}
        if 'PAELLA_MACHINE' in os.environ.keys():
            machine = os.environ['PAELLA_MACHINE']
            curenv = CurrentEnvironment(self.conn, machine)
        if machine is not None:
            self.log.info('processing trait %s on machine %s' %
                          (trait, machine))
        curenv['current_trait'] = trait
        for proc in self.trait_processes:
            self.log.info('processing %s for trait %s' % (proc, trait))
            curenv['current_trait_process'] = proc
            if proc in self._process_map.keys():
                self._process_map[proc]()
            else:
                self._process_generic_script(proc)
            self.log.info('%s has been processed for trait %s' % (proc, trait))

    def run(self,
            name,
            command,
            args='',
            proc=False,
            chroot=True,
            keeprunning=False):
        tname = 'trait-%s-%s' % (self._current_trait_, name)
        self.log.info('running %s' % tname)
        runvalue = Installer.run(self,
                                 tname,
                                 command,
                                 args=args,
                                 proc=proc,
                                 chroot=chroot,
                                 keeprunning=keeprunning)
        return runvalue

    def runscript(self, script, name, info, chroot=False):
        self.log.info(info['start'])
        trait = self._current_trait_
        self.log.info('running script %s for trait %s' % (script, trait))
        runvalue = self.run(name, script, chroot=chroot)
        os.remove(script)
        self.log.info(info['done'])

    # prepost is either 'pre' or 'post'
    def process_prepost_script(self, prepost, trait):
        script = self._make_script(prepost)
        runvalue = 0
        if script is not None:
            info = dict(start='%s script started' % prepost,
                        done='%s script done' % prepost)
            runvalue = self.runscript(script, '%s-script' % prepost, info)
        else:
            self.log.info('no %s script for trait %s' % (prepost, trait))
        if runvalue:
            raise InstallError, 'Error in running %s script for %s' % (prepost,
                                                                       trait)

    def process_hooked_action(self, script, action, trait):
        self.log.info('%s has been hooked for trait %s' % (action, trait))
        info = dict(start='%s script started' % action,
                    done='%s script done' % action)
        runvalue = self.runscript(script, '%s-script' % action, info)
        if runvalue:
            InstallError, 'hooked action %s failed on trait %s' % (action,
                                                                   trait)

    def process_packages(self, trait, action, packages, templates=[]):
        script = self._make_script(action)
        if script is None:
            affected = [p for p in packages if p.action == action]
            length = len(affected)
            if length:
                ing = INGDICT[action]
                stmt = '%s %d packages for trait %s' % (ing, length, trait)
                self.log.info(stmt)
                if action == 'remove':
                    self.remove(affected)
                elif action == 'install':
                    self.install(affected, templates)
                else:
                    raise InstallError, '%s not implemented in process_packages'
        else:
            self.process_hooked_action(script, action, trait)

    def remove(self, packages):
        packages = ' '.join([p.package for p in packages])
        command, args = 'apt-get -y remove', packages
        runvalue = self.run('remove', command, args=args, proc=True)
        if runvalue:
            self.log.warn('Problem removing packages %s' % ', '.join(packages))

    def install(self, packages, templates):
        trait = self._current_trait_
        package_args = ' '.join([p.package for p in packages])
        cmd = 'apt-get -y --force-yes install %s' % package_args
        stmt = 'install command for %s is %s' % (trait, cmd)
        self.log.info(stmt)
        runvalue = self.run('install', cmd, proc=False, keeprunning=False)
        if runvalue:
            self.log.warn('PROBLEM installing %s' % trait)
            self.log.warn('packages --> %s' % package_args)
            raise InstallError, 'problem installing packages'
        runvalue = remove_debs(self.target)
        if runvalue:
            self.log.warn('PROBLEM removing downloaded debs')
            raise InstallError, 'problem removing downloaded debs'

    def install_templates(self, packages, templates):
        trait = self._current_trait_
        num = len(templates)
        stmt = 'in install_templates, there are %d templates for trait %s' % (
            num, trait)
        self.log.info(stmt)
        for p in packages:
            for t in [t for t in templates if t.package == p.package]:
                if t.template == 'var/cache/debconf/config.dat':
                    self.log.info('Installing Debconf template ...')
                    self.install_debconf_template(t)
                else:
                    self.make_template(t)

    def configure(self, packages, templates):
        trait = self._current_trait_
        stmt = 'in configure, there are %d templates for trait %s' % (
            len(templates), trait)
        self.log.info(stmt)
        for p in packages:
            for t in [t for t in templates if t.package == p.package]:
                if t.template == 'var/cache/debconf/config.dat':
                    self.log.info('Installing Debconf template ...')
                    self.install_debconf_template(t)
                else:
                    self.make_template(t)

    def _make_script(self, name, execpath=False):
        script = self.traitscripts.get(name)
        if script is not None:
            stmt = '%s script exists for trait %s' % (name,
                                                      self._current_trait_)
            self.log.info(stmt)
            return make_script(name, script, self.target, execpath=execpath)
        else:
            return None

    def _make_scriptOrig(self, name, execpath=False):
        script = self.traitscripts.get(name)
        if script is not None:
            stmt = '%s script exists for trait %s' % (name,
                                                      self._current_trait_)
            self.log.info(stmt)
            exec_path = join('/tmp', name + '-script')
            target_path = join(self.target, 'tmp', name + '-script')
            sfile = file(target_path, 'w')
            sfile.write(script.read())
            sfile.close()
            os.system('chmod 755 %s' % target_path)
            if not execpath:
                return target_path
            else:
                return exec_path
        else:
            return None

    def make_template(self, template):
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self._update_templatedata()
        self._make_template_common(template, tmpl)

    def make_template_with_data(self, template, data):
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self.traittemplate.template.update(data)
        self._make_template_common(template, tmpl)

    def _make_template_common(self, template, tmpl):
        sub = self.traittemplate.template.sub()
        newpath = join(self.target, template.template)
        bkuppath = join(self.target, self.paelladir, 'original_files',
                        template.template)
        makepaths(dirname(newpath), dirname(bkuppath))
        self.log.info('target template %s' % newpath)
        if tmpl != sub:
            self.log.info('%s %s subbed' %
                          (template.package, template.template))
        if isfile(newpath):
            if not isfile(bkuppath):
                os.system('mv %s %s' % (newpath, dirname(bkuppath)))
                self.log.info('%s backed up' % template.template)
            else:
                self.log.info('overwriting previously installed template %s' %
                              template.template)
        else:
            self.log.info('installing new template %s' % template.template)
        newfile = file(newpath, 'w')
        newfile.write(sub)
        newfile.close()
        mode = template.mode

        # a simple attempt to insure mode is a valid octal string
        # this is one of the very rare places eval is used
        # there are a few strings with 8's and 9's that will pass
        # the if statement, but the eval will raise SyntaxError then.
        # If the mode is unusable the install will fail at this point.
        if mode[0] == '0' and len(mode) <= 7 and mode.isdigit():
            mode = eval(mode)
            os.chmod(newpath, mode)
        else:
            raise InstallError, 'bad mode %s, please use octal prefixed by 0' % mode

        own = ':'.join([template.owner, template.grp_owner])
        os.system(
            self.command('chown',
                         "%s '%s'" % (own, join('/', template.template))))

    def _update_templatedata(self):
        self.traittemplate.template.update(self.familydata)
        self.traittemplate.template.update(self.mtypedata)
        self.traittemplate.template.update(self.profiledata)

    def install_debconf_template(self, template):
        trait = self._current_trait_
        self.log.info('Installing debconf for %s' % trait)
        self.traittemplate.set_template(template.package, template.template)
        tmpl = self.traittemplate.template.template
        self._update_templatedata()
        sub = self.traittemplate.template.sub()
        if tmpl == sub:
            self.log.info('static debconf, no substitutions')
            self.log.info('for trait %s ' % trait)
        else:
            self.log.info('templated debconf for trait %s' % trait)
        config_path = join(self.target, 'tmp/paella_debconf')
        if isfile(config_path):
            self.log.warn('%s is not supposed to be there' % config_path)
            raise Error, '%s is not supposed to be there' % config_path
        debconf = file(config_path, 'w')
        debconf.write(sub + '\n')
        debconf.close()
        target_path = join(self.target, 'var/cache/debconf/config.dat')
        self.log.info('debconf config is %s %s' % (config_path, target_path))
        copy_configdb(config_path, target_path)
        os.remove(config_path)

    def set_template_path(self, path):
        self.traittemplate.template.set_path(path)

    def reconfigure_debconf(self, packages):
        self.log.info('running reconfigure')
        reconfig = [p.package for p in packages if p.action == 'reconfig']
        os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
        for package in reconfig:
            self.log.info('RECONFIGURING %s' % package)
            os.system(self.command('dpkg-reconfigure -plow %s' % package))
Beispiel #19
0
 def diff_selection(self, *args):
     lrow = self.view.lbox.get_selected_data()[0]
     rrow = self.view.rbox.get_selected_data()[0]
     ltemplate = TraitTemplate(self.conn, self.lsuite)
     rtemplate = TraitTemplate(self.conn, self.rsuite)
     ltemplate.set_trait(lrow.trait)
     rtemplate.set_trait(rrow.trait)
     ldata = ltemplate.templatedata(lrow.package, lrow.template)
     rdata = rtemplate.templatedata(rrow.package, rrow.template)
     differ = Differ(ldata, rdata)
     differ.diff()
     if differ.isdifferent('left', ldata):
         newdata = differ.get_data('left')
         ltemplate.update_templatedata(lrow.package, lrow.template, newdata)
     if differ.isdifferent('right', rdata):
         newdata = differ.get_data('right')
         rtemplate.update_templatedata(rrow.package, rrow.template, newdata)
Beispiel #20
0
 def set_suite(self, suite):
     self.scripts = TraitScript(self.conn, suite)
     self.templates = TraitTemplate(self.conn, suite)
     self.traits = Traits(self.conn, suite)
Beispiel #21
0
 def set_suite(self, suite):
     self.scripts = TraitScript(self.conn, suite)
     self.templates = TraitTemplate(self.conn, suite)
     self.traits = Traits(self.conn, suite)
Beispiel #22
0
class TemplateBrowser(ListNoteBook):
    def __init__(self, conn, suite, trait):
        self.menu = make_menu(TEMPL_CMDS, self.template_command)
        self.conn = conn
        self.current_trait = trait
        self.traittemplate = TraitTemplate(self.conn, suite)
        self.traittemplate.set_trait(self.current_trait)
        self.traitpackage = TraitPackage(self.conn, suite)
        self.traitpackage.set_trait(self.current_trait)
        self.current_template = None
        self.cfg = PaellaConfig()
        self.template_path = '/nowhere'
        self._tmp_path = '/nowhere'
        self.tarball_path = self.cfg.get('management_gui', 'bkuptarball_path')
        ListNoteBook.__init__(self)
        self.set_suite(suite)
        self.extracted = None
        self.reset_rows()
        self.dialogs = {}.fromkeys(['attach', 'rootsel'])
        
    def reset_rows(self):
        rows = self.traittemplate.templates(fields=['template', 'package'])
        self.set_rows(rows)
        self.set_row_select(self.template_selected)
        self.templates = [r.template for r in rows]
            
    def template_selected(self, listbox, row, column, event):
        row = listbox.get_selected_data()[0]
        self.set_template(row.package, row.template)
    
    def set_template(self, package, template):
        self.current_package = package
        self.current_template = template
        if template not in self.pages:
            trait = self.current_trait
            nbook = TemplateNotebook(self.conn, self.cfg, self.suite, trait,
                                     package, template, self.extracted)
            self.append_page(nbook, template)
        else:
            self.set_current_page(template)
            
    def template_filename(self, package, template):
        tpath = join(self.template_path, self.suite, self.current_trait)
        return join(tpath, package, template + '.template')
        

    def suite_template_path(self, filesel=False):
        path = join(self.template_path, self.suite)
        if filesel:
            path += '/'
        return path

    def trait_temp_path(self, filesel=False):
        path = join(self._tmp_path, self.suite, self.current_trait)
        if filesel:
            path += '/'
        return path
       
    def set_suite(self, suite):
        self.suite = suite

    def save_template(self):
        try:
            template = self.get_selected_data()[0].template
            self.pages[template].save_template()
        except IndexError:
            dialogs.Message('a template must be selected')
            

    def edit_template(self):
        try:
            template = self.get_selected_data()[0].template
            self.pages[template].edit_template()
        except IndexError:
            dialogs.Message('a template must be selected')
            
        
        
    def template_command(self, meuitem, command):
        if command in ['new']:
            path = self.trait_temp_path(filesel=True)
            select_a_file('new', path, self.fileselect_ok)
        elif command == 'diff':
            path = self.suite_template_path(filesel=True)
            select_a_file('load', path, self.fileselect_ok)
        elif command == 'save':
            self.save_template()
        elif command == 'load':
            path = self.suite_template_path(filesel=True)
            select_a_file('load', path, self.fileselect_ok)
        elif command == 'done':
            try:
                template = self.get_selected_data()[0].template
                self.remove_page(template)
            except IndexError:
                pass
        elif command in ['drop', 'real_templates', 'other_templates']:
            rows = self.get_selected_data()
            row = None
            print row, rows, len(rows)
            if len(rows):
                row = rows[0]
            else:
                dialogs.Message('a template must be selected')
                print row, rows
            if row:
                package, template = row.package, row.template
                print row, 'ifrow'
                if command == 'drop':
                    self.traittemplate.drop_template(package, template)
                    self.remove_page(template)
                elif command == 'real_templates':
                    self.traittemplate.prefix_template(package, template, type_='real')
                    print 'make it real_templates'
                elif command == 'other_templates':
                    self.traittemplate.prefix_template(package, template, type_='other')
                    print 'make it other_templates'
                self.reset_rows()
        elif command == 'root':
            if self.dialogs['rootsel'] is None:
                path = self.tarball_path + '/'
                select_a_file('root', path, self.tarball_selected)
        elif command == 'edit':
            self.edit_template()
            
            


    def tarball_selected(self, button, fileselect):
        path = fileselect.get_filename()
        fileselect.destroy()
        self.dialogs['rootsel'] = select_from_tarfile('heydude', path, self.pull_from_tar)
        
    def pull_from_tar(self, button, fileselect):
        info, tfile = fileselect.extract_file()
        template = tfile.name
        action = 'attach'
        if self.dialogs[action] is None:
            msg = 'attach to which package?'
            self.dialogs[action] = dialogs.CList(msg, name=action)
            lbox = self.dialogs[action]
            lbox.set_rows(self.traitpackage.packages())
            lbox.set_ok(self.insert_new_template_from_tar)
            lbox.set_cancel(self.destroy_dialog)
            lbox.set_data('tarmember', (info, tfile))
            
    def insert_new_template_from_tar(self, button):
        lbox = self.dialogs['attach']
        rows = lbox.get_selected_data()
        if len(rows) == 1:
            package = rows[0].package
            info, tfile = lbox.get_data('tarmember')
            self.destroy_dialog(lbox)
            self._insert_new_template(package, info.name, tfile, info)
            
        
    def _insert_new_template(self, package, template, fileobj, info):
        fileobj.seek(0)
        filename = self.template_filename(package, template)
        data = dict(owner=info.uname, grp_owner=info.gname,
                    mode=oct(info.mode), package=package,
                    template=template)
        self.traittemplate.insert_template(data, fileobj)
        self.set_template(package, template)
        self.reset_rows()
        
        
            
    def insert_new_template(self, package, template, path):
        newfile = readfile(path)
        filename = self.template_filename(package, template)
        makepaths(dirname(filename))
        writefile(filename, newfile)
        try:
            self.traittemplate.insert_template(dict(package=package, template=template),
                                               file(path))
        except OperationalError:
            dialogs.Message('template already exists')
        self.set_template(package, template)
        self.reset_rows()

    def load_template(self, path):
        template, package = get_file_path(path, self.suite_template_path())
        if template[-9:] == '.template':
            template = template[:-9]
            if template not in self.templates:
                try:
                    orig = self.find_original(template)
                    self.insert_new_template(package, template, path)
                except NoExistError:
                    dialogs.Message('%s not in extracted packages' %template)


    def _diff(self, template, path):
        current_template = self.template_filename(template)
        os.popen2('meld %s %s' %(current_template, path))

    def fileselect_ok(self, button, filesel):
        path = filesel.get_filename()
        action = filesel.get_data('action')
        filesel.destroy()
        if action == 'new':
            template, package = get_file_path(path, self.trait_temp_path())
            if template not in self.templates:
                self.insert_new_template(package, template, path)
            else:
                dialogs.Message('template already exists')
        elif action == 'diff':
            pass
        elif action == 'load':
            self.load_template(path)

    def set_extracted(self, extracted):
        self.extracted = extracted
        for page in self.pages.values():
            page.extracted = extracted

    def _extract_packages(self):
        dialogs.Message('deprecated(for now)')
        if False:
            if self.extracted is None:
                packages = [p.package for p in self.traitpackage.packages()]
                path = self.trait_temp_path()
                self.set_extracted(True)
                packs = ',\n'.join(packages)
                message = 'extracted packages:\n %s into:\n%s'%(packs, path)
                dialogs.Message(message)
            else:
                dialogs.Message('already extracted')

    def extract_packages(self):
        self._extract_packages()