]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
Add a step for the root password
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 9 May 2021 10:29:46 +0000 (10:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 9 May 2021 10:29:46 +0000 (10:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py
src/python/errors.py
src/python/step.py
src/python/tui.py

index 58667bf11e666439cebd64dd6f39733f2b58f712..0e81c14aa7cd5d353979600a55cd3097684d00fa 100644 (file)
@@ -78,6 +78,7 @@ class Bricklayer(object):
                step.Welcome,
                disk.SelectDisk,
                disk.CalculatePartitionLayout,
+               step.RootPassword,
 
                # Go!
                disk.CreatePartitionLayout,
index 1b94b0065600453c04432101963a4e5f2da9a14f..df55c9aaedc20f7b1517bd7d5a5a3a12d83c90d7 100644 (file)
@@ -30,3 +30,7 @@ class UserCanceledError(InstallError):
                Raised when the user wants to cancel the process
        """
        pass
+
+
+class TryAgainError(Exception):
+       pass
index e58b9035d6a7187b3cbfb65f07e43e5b97734410..3626e9fd4352a98f20ce0ea6c04e26ff7baf110f 100644 (file)
@@ -132,3 +132,14 @@ class UnattendedWarning(Step):
                                time.sleep(0.1)
 
                                p.update(i + 1)
+
+
+class RootPassword(InteractiveStep):
+       def run(self, tui):
+               password = tui.passwd(
+                       _("Root Password"),
+                       _("Please enter the password for the 'root' user"),
+               )
+
+               # Save the password for now
+               self.bricklayer.settings["root-password"] = password
index b8b659a28fcdba61c2da41c0825344cb3e554214..19a646e2ceacfc3ec8f2f994c4254569f44792c4 100644 (file)
@@ -165,6 +165,11 @@ class Tui(object):
 
                return window.run()
 
+       def passwd(self, title, text, height=None, width=40, help=None):
+               window = PasswordWindow(self, title, text, height=height, width=width, help=help)
+
+               return window.run()
+
 
 class Window(object):
        def __init__(self, tui, title, text, height=None, width=None, help=None):
@@ -192,13 +197,17 @@ class ButtonsWindow(Window):
 
                # Configure some default buttons
                if buttons is None:
-                       buttons = [
-                               (_("Ok"), None),
-                               (_("Cancel"), cancel),
-                       ]
+                       buttons = self.default_buttons
 
                self.buttons = snack.ButtonBar(self.tui.screen, buttons)
 
+       @property
+       def default_buttons(self):
+               return (
+                       (_("Ok"), None),
+                       (_("Cancel"), cancel),
+               )
+
        def _make_window(self):
                # Create a grid
                grid = snack.GridFormHelp(self.tui.screen, self.title, self.help, 1, 2)
@@ -215,18 +224,28 @@ class ButtonsWindow(Window):
        def run(self):
                window = self._make_window()
 
-               # Run the window
-               button = window.runOnce()
+               # Run the window in a loop. The callback must return a value, otherwise the lo
+               try:
+                       while True:
+                               button = window.run()
+
+                               # Which button was pressed?
+                               callback = self.buttons.buttonPressed(button)
 
-               # Which button was pressed?
-               callback = self.buttons.buttonPressed(button)
+                               try:
+                                       # If the button had a callback, we will call the callback
+                                       if callable(callback):
+                                               return callback()
 
-               # If the button had a callback, we will call the callback
-               if callable(callback):
-                       return callback()
+                                       # Otherwise call the default action
+                                       return self.default_action()
 
-               # Otherwise call the default action
-               return self.default_action()
+                               # If any of the callbacks raise TryAgain, we go back and let the user
+                               # edit the form again
+                               except TryAgainError:
+                                       continue
+               finally:
+                       self.tui.screen.popWindow()
 
        def default_action(self):
                """
@@ -341,3 +360,68 @@ class ProgressWindow(Window):
                if self.max_value:
                        self.scale.set(value)
                        self.tui.refresh()
+
+
+class PasswordWindow(ButtonsWindow):
+       @property
+       def default_buttons(self):
+               return (
+                       (_("Set Password"), self.set_password),
+                       (_("Cancel"), cancel),
+               )
+
+       def _make_window(self):
+               # Create a grid
+               grid = snack.GridFormHelp(self.tui.screen, self.title, self.help, 1, 3)
+
+               # Create the box that shows the text
+               textbox = snack.TextboxReflowed(self.width, self.text, maxHeight=self.max_height)
+               grid.add(textbox, 0, 0, padding=(0, 0, 0, 1))
+
+               # Create password fields
+               passwords = snack.Grid(2, 2)
+
+               self.password1 = snack.Entry(24, password=True)
+               label = snack.Label(_("Password"))
+
+               passwords.setField(label, 0, 0, padding=(0, 0, 1, 0), anchorLeft=True)
+               passwords.setField(self.password1, 1, 0, anchorLeft=True)
+
+               self.password2 = snack.Entry(24, password=True)
+               label = snack.Label(_("Confirm"))
+
+               passwords.setField(label, 0, 1, padding=(0, 0, 1, 0), anchorLeft=True)
+               passwords.setField(self.password2, 1, 1, anchorLeft=True)
+
+               grid.add(passwords, 0, 1, padding=(0, 0, 0, 1))
+
+               # Create the button bar
+               grid.add(self.buttons, 0, 2, growx=True)
+
+               return grid
+
+       def set_password(self):
+               # Fetch the entered values
+               password1 = self.password1.value()
+               password2 = self.password2.value()
+
+               # Has something been entered?
+               if not password1:
+                       self.tui.message(
+                               _("Error"),
+                               _("You must enter a password"),
+                               buttons=[_("OK")],
+                       )
+                       raise TryAgainError
+
+               # Do the passwords match?
+               if not password1 == password2:
+                       self.tui.message(
+                               _("Error"),
+                               _("The entered passwords do not match"),
+                               buttons=[_("OK")],
+                       )
+                       raise TryAgainError
+
+               # Return the password to the caller
+               return password1