From cd389ca988379bb4c98826b5c6f3546c5e5b0622 Mon Sep 17 00:00:00 2001 From: Lunfan Zhang Date: Sun, 18 May 2025 22:22:18 -0400 Subject: [PATCH 1/2] CP-54331 Enable XSConsole to Configure Auto mode Signed-off-by: Lunfan Zhang --- XSConsoleData.py | 5 ++ plugins-base/XSFeatureRemoteShell.py | 88 +++++++++++++++++++++++----- 2 files changed, 78 insertions(+), 15 deletions(-) diff --git a/XSConsoleData.py b/XSConsoleData.py index 9677ca2..960e727 100644 --- a/XSConsoleData.py +++ b/XSConsoleData.py @@ -1099,6 +1099,11 @@ def DisableSSH(self): self.RequireSession() self.session.xenapi.host.disable_ssh(self.host.opaqueref()) + def SetSSHAutoMode(self, inMode): + Auth.Inst().AssertAuthenticatedOrPasswordUnset() + self.RequireSession() + self.session.xenapi.host.set_ssh_auto_mode(self.host.opaqueref(), inMode) + def Ping(self, inDest): # Must be careful that no unsanitised data is passed to the command if not re.match(r'[0-9a-zA-Z][-0-9a-zA-Z.]*$', inDest): diff --git a/plugins-base/XSFeatureRemoteShell.py b/plugins-base/XSFeatureRemoteShell.py index 17d8fe2..a4e4d3e 100644 --- a/plugins-base/XSFeatureRemoteShell.py +++ b/plugins-base/XSFeatureRemoteShell.py @@ -18,6 +18,68 @@ from XSConsoleStandard import * +DISABLE = 0 +ENABLE = 1 +AUTO = 2 + +class DisableOptionsDialogue(Dialogue): + def __init__(self): + Dialogue.__init__(self) + + pane = self.NewPane(DialoguePane(self.parent)) + pane.TitleSet(Lang("Auto-mode Options")) + pane.AddBox() + + self.disableMenu = Menu(self, None, Lang("Disable Options"), [ + ChoiceDef(Lang("Standard Disable"), lambda: self.HandleChoice(DISABLE)), + ChoiceDef(Lang("Disable and Turn on Auto-mode"), lambda: self.HandleChoice(AUTO)) + ]) + + self.UpdateFields() + + def UpdateFields(self): + pane = self.Pane() + pane.ResetFields() + + pane.AddTitleField(Lang("When auto-mode is enabled: SSH is disabled when XAPI is running properly, " + "and enabled when XAPI is down for emergency troubleshooting.")) + pane.AddMenuField(self.disableMenu) + pane.AddKeyHelpField( { Lang("") : Lang("OK"), Lang("") : Lang("Cancel") } ) + + def HandleKey(self, inKey): + handled = self.disableMenu.HandleKey(inKey) + + if not handled and inKey == 'KEY_ESCAPE': + Layout.Inst().PopDialogue() + handled = True + + return handled + + def HandleChoice(self, inChoice): + data = Data.Inst() + Layout.Inst().PopDialogue() + + try: + message = Lang("Configuration Successful") + data.DisableSSH() + + if ShellPipe(['/sbin/pidof', 'sshd-session']).CallRC() == 0: # If PIDs are available + message = Lang("New connections via the remote shell are now disabled, but there are " + "ssh connections still ongoing. If necessary, use 'killall sshd-session' from the Local " + "Command Shell to terminate them.") + if inChoice == AUTO: + data.SetSSHAutoMode(True) + message = Lang("Auto configuration for ssh xapi console has been set. New connections via " + "the remote shell will be enabled/disabled based on the state of the xapi") + + Layout.Inst().PushDialogue(InfoDialogue(message)) + + except Exception as e: + Layout.Inst().PushDialogue(InfoDialogue(Lang("Failed: ")+Lang(e))) + + data.Update() + + class RemoteShellDialogue(Dialogue): def __init__(self): Dialogue.__init__(self) @@ -27,8 +89,8 @@ def __init__(self): pane.AddBox() self.remoteShellMenu = Menu(self, None, Lang("Configure Remote Shell"), [ - ChoiceDef(Lang("Enable"), lambda: self.HandleChoice(True) ), - ChoiceDef(Lang("Disable"), lambda: self.HandleChoice(False) ) + ChoiceDef(Lang("Enable"), lambda: self.HandleChoice(ENABLE)), + ChoiceDef(Lang("Disable"), lambda: self.HandleDisable()) ]) self.UpdateFields() @@ -50,29 +112,25 @@ def HandleKey(self, inKey): return handled - def HandleChoice(self, inChoice): + def HandleChoice(self, inChoice): data = Data.Inst() Layout.Inst().PopDialogue() try: message = Lang("Configuration Successful") - if inChoice: + if inChoice == ENABLE: data.EnableSSH() - else: - data.DisableSSH() - - if ShellPipe(['/sbin/pidof', 'sshd']).CallRC() == 0: # If PIDs are available - message = Lang("New connections via the remote shell are now disabled, but there are " - "ssh connections still ongoing. If necessary, use 'killall sshd' from the Local " - "Command Shell to terminate them.") - - Layout.Inst().PushDialogue(InfoDialogue(message)) + Layout.Inst().PushDialogue(InfoDialogue(message)) except Exception as e: - Layout.Inst().PushDialogue(InfoDialogue( Lang("Failed: ")+Lang(e))) + Layout.Inst().PushDialogue(InfoDialogue(Lang("Failed: ")+Lang(e))) data.Update() + def HandleDisable(self): + Layout.Inst().PopDialogue() + Layout.Inst().PushDialogue(DisableOptionsDialogue()) + class XSFeatureRemoteShell: @classmethod @@ -106,7 +164,7 @@ def Register(self): { 'menuname' : 'MENU_REMOTE', 'menupriority' : 100, - 'menutext' : Lang('Enable/Disable Remote Shell') , + 'menutext' : Lang('Enable/Disable/Auto Remote Shell') , 'statusupdatehandler' : self.StatusUpdateHandler, 'activatehandler' : self.ActivateHandler } From 7cc15f5c78883623b4036a471d73ce5a2ec4249d Mon Sep 17 00:00:00 2001 From: Lunfan Zhang Date: Mon, 19 May 2025 00:22:36 -0400 Subject: [PATCH 2/2] [maintenance]: Update to Ubuntu 22.04 in github workflow Signed-off-by: Lunfan Zhang --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d238968..b026c19 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,14 +15,14 @@ env: jobs: python-checks: name: minimaltest (pre-commit) - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Checkout code uses: actions/checkout@v3 - name: Install dependencies run: | - #: Install Python 2.7 from Ubuntu 20.04 using apt-get install + #: Install Python 2.7 from Ubuntu 22.04 using apt-get install sudo apt-get update && sudo apt-get install -y python2 curl -sSL https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py python2 get-pip.py