Congelar/descongelar un sol usuari per línia de comandes (Trivial Freezer)

Hola,
fa temps que utilitzo el Trivial Freezer per congelar i descongelar les màquines de l'Institut, però hi ha un petit problema: és molt farragós anar màquina a màquina per interfície gràfica per congelar i descongelar usuaris.
Seria possible congelar/descongelar usuaris per línia de comandes?
Per exemple:
Congelar usuari: tfreezer -c usuari
Descongelar usuari: tfreezer -d usuari

Ara mateix ja existeix la opció de canviar el mètode d'arrancada del congelador per línia de comandes per: /usr/share/tfreezer/select-inici.sh

Moltes gràcies!
Santi
Hola Santi,

En efecte, el tfreezer es pot executar via línia d'ordres.

Cal que executis l'ordre següent:

sudo tfreezer -r NOM_USUARI

Si executes:

tfreezer -h obtindràs els paràmetres de la línia d'ordres del tfreezer.


Code

tfreezer --help

Trivial Freezer v2
==================
Usage: /usr/bin/tfreezer  [OPTION]

 Opcions:
  -a        Amb -r indica que està sent executat automàticament
  -d nivell Especifica el nivell de depuració
  -h        Mostra aquesta ajuda
  -p        Mostra el fitxer de configuració en XML
  -r        Restaura tot el sistema si així està configurat
  -r nom_usuari Restaura el directori de l'usuari especificat si així està configurat


Fes alguna prova i ens comentes com ha anat.

Joan de Gracia
Moltes gràcies per la resposta,
però he provat per línia de comandes i no funciona correctament.
la comanda tfreezer -r nom_usuari no fa res a nivell de configuració. Com a molt deu tornar a copiar les dades originals a la carpeta de l'usuari, però no canvia el mode de congelat a descongelat ni a l'inversa (ho pots comprovar des de l'aplicatiu gràfic executant tfreezer sense paràmetres).
Per això estaria bé les opcions (per exemple):
tfreezer -s [usuari] : Mostra l'estat de congelació de tot el sistema o dels usuaris passats com a paràmetre.
tfreezer -f : ho congela tot
tfreezer -u: ho descongela tot
tfreezer -f usuari1 usuari2 usuari3 .... : congela els usuaris passats com a paràmetre
tfreezer -u usuari1 usuari2 usuari3 ... : descongela els usuaris passats com a paràmetre

Merci,
Santi
Bones, torno a ser jo.
Acabo de modificar l'script tfreezer.py per a que permeti algunes de les opcions que he comentat anteriorment.
Està fet a corre cuita amb poc temps i per ara no canvia les configuracions, però permet que es copiï la carpeta dels usuaris a congelar i mostra un llistat dels usuaris que es troben congelats:


Code

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#@authors: Pau Ferrer Ocaña. Modificat per Santi Camarasa.

#This file is part of Trivial Freezer.

#Trivial Freezer is an easy freezer for user profiles and desktop in linux.
#Copyright (C) 2009  Pau Ferrer Ocaña

#Trivial Freezer is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#Trivial Freezer is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with Trivial Freezer.  If not, see <http://www.gnu.org/licenses/>.


import sys
sys.path.insert(0, './')
from TFglobals import *  

_ = load_locale()

def __check_root():
    "Checks if it's running with root privileges"
    if os.geteuid() != 0:
        print_error(_("You don't have enough privileges to run this program."))
        sys.exit()


def __do_restore(username = "", auto=true):
    "Runs a restoration for the specified user name or, if not specified, the whole system"
   
    from TFconfig import config
       
    __check_root()
   
    #TOERASE 2
    import time
    start = time.time()
   
    title = "Trivial Freezer " + VERSION
    print title
    print "=" * len(title)
   
    #Load saved configuration
    cfg = config()
    cfg.load()
   
    #If the username is empty,the whole system has to be restored
    if len(username) == 0:
        time_req = TIME_SYSTEM
    else:
        time_req = TIME_SESSION
   
    #Do nothing if time requested and configured time differs and it's an automatic running
    if cfg.time != time_req and auto:
        return
   
    #Get the list of users to restore
    fu = cfg.get_frozen_users(TAR_RESTORE)
   
    if len(fu) > 0:
        #TIME TO RESTORE!
        if time_req == TIME_SYSTEM:
            debug("RESTORE SYSTEM",DEBUG_LOW)
            #Restore every user in the list
            for froze in fu:
                froze.restore_tar()
        else: #time == TIME_SESSION
            #Restore only the specified user from the list
            for froze in fu:
                if username == froze.username:
                    debug("RESTORE SESSION",DEBUG_LOW)
                    froze.restore_tar()
                    break
    else:
        debug("NOTHING TO RESTORE",DEBUG_LOW)
   
    #TOERASE 2
    end = time.time()
    print "Time elapsed = ", end - start, "seconds"

def __do_freeze(username = "", auto=true):
    "Runs a freeze configuration for the specified user name or, if not specified, the whole system"
   
    from TFconfig import config
       
    __check_root()
   
    #TOERASE 2
    import time
    start = time.time()
   
    title = "Trivial Freezer " + VERSION
    print title
    print "=" * len(title)
   
    #Load saved configuration
    cfg = config()
    cfg.load()
   
    #If the username is empty,the whole system has to be frozen
    if len(username) == 0:
        time_req = TIME_SYSTEM
    print "Encara no implementat"
    return
    else:
        time_req = TIME_SESSION
   
    #Do nothing if time requested and configured time differs and it's an automatic running
    if cfg.time != time_req and auto:
        return
   
    #Get the list of users to freeze
    fu = cfg.get_frozen_users(TAR_RESTORE)
   
    if len(fu) > 0:
        #TIME TO FREEZE!
        if time_req == TIME_SYSTEM:
            debug("RESTORE SYSTEM",DEBUG_LOW)
            #Restore every user in the list
            for froze in fu:
                froze.create_tar()
        else: #time == TIME_SESSION
            #Freeze only the specified user from the list
            for froze in fu:
                if username == froze.username:
                    debug("FREEZE SESSION",DEBUG_LOW)
                    froze.create_tar()
                    break
    else:
        debug("NOTHING TO FREEZE",DEBUG_LOW)
   
    #TOERASE 2
    end = time.time()
    print "Time elapsed = ", end - start, "seconds"


def __do_user_status(username = "", auto=true):
    "Check users freeze status, if not specified, the whole system"
   
    from TFconfig import config
       
    #Load saved configuration
    cfg = config()
    cfg.load()
   
#    if len(username) == 0:
#   print "Encara no implementat"
#   return
#    else:
#        time_req = TIME_SESSION
   
    #Get the list of users to freeze
    fu = cfg.get_frozen_users(TAR_RESTORE)
   
    if len(fu) > 0:
    #print "Username\t" + "User id"
        for froze in fu:
        print froze.username

    else:
        debug("NOTHING TO SHOW",DEBUG_LOW)
   

       
def __print_help():
    "Prints usage help in the command line"
   
    title = "Trivial Freezer " + VERSION
    print title
    print "=" * len(title)
    print _("Usage: "+sys.argv[0]+"  [OPTION]\n")
    print " " + _("Options:")
    print "  -a\t\t"+_("With -r indicates that it's being executed automatically")
    print "  -d "+_("level")+"\t"+_("Specify the debug level")
    print "  -h\t\t"+_("Show this help")
    print "  -p\t\t"+_("Show the XML configuration file")
    print "  -r\t\t"+_("Restore the whole system if configured")
    print "  -r "+_("username")+"\t"+_("Restore the specified user home directory if configured")
    print "  -f "+_("username")+"\t"+_("Congela el directori de l'usuari")
    print "  -s "+"\t\t"+_("Mostra els usuaris congelats")

    return

def __print_config():
    "Prints XML formated configuration file in the command line"
   
    from xml.dom import minidom
   
    title = "Trivial Freezer "+VERSION
    print title
    print "=" * len(title)
   
    try:
        xdoc = minidom.parse(os.path.join (CONFIG_DIRECTORY, CONFIG_FILE))
    except:
        print_error(_("Corrupted configuration file"))
    else:
        print xdoc.toprettyxml(indent="  ")

def __show_window():
    "Shows the configuration window"
   
    from TFmainWindow import mainWindow
   
    __check_root()
   
    mainWindow().main()

if __name__ == "__main__":
    "Main function"
   
    argv = sys.argv
    args = len(argv)

    restore = false
    freeze = false
    userstatus = false
    auto = false
    user = ""
   
    #Read all the command line parameters
    for i, arg in enumerate(argv):
        if arg.startswith("-"):
            #Restore
            if arg == "-r":
                #User
                if args > i and not argv[i + 1].startswith("-"):
                    user = argv[i + 1]
               
                if restore: #Already read
                    __print_help()
                    sys.exit(1)
               
                restore = true
                   
            elif arg == "-f":
                #User
                if args > i and not argv[i + 1].startswith("-"):
                    user = argv[i + 1]
               
                if freeze: #Already read
                    __print_help()
                    sys.exit(1)
               
                freeze = true

            elif arg == "-s":
        userstatus = true

            #Automatic execution through gdm or init.d?
            elif arg == "-a":
                auto = true
               
            #Debug level
            elif arg == "-d":
                if args > i + 1:
                    set_debug_level(sys.argv[i+1])
                else: #Level not specified
                    __print_help()
                    sys.exit(1)
           
            #Show config
            elif arg == "-p":
                __print_config()
                sys.exit()
           
            #Show help
            elif arg == "-h":
                __print_help()
                sys.exit()
               
            #Others
            else:
                __print_help()
                sys.exit(1)

    #If restoring
    if restore:
        __do_restore(user,auto)
    elif freeze:
    __do_freeze(user,auto)
    elif userstatus:
    __do_user_status(user,auto)
    #else, configure
    else:
        __show_window()
Hola Santi,

Quote

Està fet a corre cuita amb poc temps i per ara no canvia les configuracions


Si vols fes les millores amb més calma i podem publicar una nova versió del Trivial Freezer amb els teus canvis ;)

Salut.

Projecte Linkat
Bones,
ja he modificat una mica l'arxiu tfreezer.py per a permetre diferents opcions per línia de comandes. L'he anomenat congela.py. Només cal canviar-li el nom per tfreezer.py i ja funcionaria tot igual que amb l'arxiu original, però amb les noves opcions.
També he afegit les traduccions al català d'aquestes opcions (carpeta "locale").
El podeu descarregar d' aquí.
No he comprovat l'apartat de LDAP ja que no el tinc instal·lat. Podeu fer proves, canviar el que vulgueu i si ho creieu convenient, afegir a la nova versió.

Aquí podeu veure com s'executa:

Code

$ tfreezer -h
Trivial Freezer v2
==================
Sintaxi: ./congela.py  [Opcions]

 Opcions:

  -a    Amb -r indica que està sent executat automàticament

  -d nivell
    Especifica el nivell de depuració

  -h    Mostra aquesta ajuda

  -x    Mostra el fitxer de configuració en XML

  -u usuari1 [usuari2 ...]
    Usuaris als que s'apliquen les opcions

  -r    Restaura el directori de l'usuari. Sense usuaris, restaura tot el sistema

  -c    Còpia el directori de l'usuari. Sense usuaris, còpia tot el sistema

  -p Perfil
    Canvia el perfil dels usuaris i el grava a l'arxiu de configuració.
    Valors vàlids:
         0   Descongela-ho tot
         1   Congela-ho tot
         2   Configuració congelada
        -1   Congelació al servidor (LDAP)

  -s    Mostra els usuaris congelats


Atentament,
Santi Camarasa