Skip to content

CP-54331 Enable XSConsole to Configure Auto mode #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feature/configure-ssh
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions XSConsoleData.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
88 changes: 73 additions & 15 deletions plugins-base/XSFeatureRemoteShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<Enter>") : Lang("OK"), Lang("<Esc>") : 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)
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down