Skip to content

Commit

Permalink
Merge pull request #60 from skanthed/esi-node-console-connect
Browse files Browse the repository at this point in the history
esi-node-console-connect
  • Loading branch information
tzumainn authored Dec 1, 2023
2 parents e2a2a37 + cd90e4f commit 4bf6994
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
66 changes: 66 additions & 0 deletions esiclient/tests/unit/v1/test_node_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock

from osc_lib import exceptions

from esiclient.tests.unit import base
from esiclient.v1 import node_console


class TestNodeConsoleConnect(base.TestCommand):

def setUp(self):
super(TestNodeConsoleConnect, self).setUp()
self.cmd = node_console.NodeConsoleConnect(self.app, None)

self.node_console_1 = {
"console_enabled": True,
"console_info": {"type": "socat", "url": "tcp://192.168.1.2:8024"}
}
self.node_console_2 = {
"console_enabled": False,
"console_info": None
}

@mock.patch('esiclient.v1.node_console.os.system',
return_value=0, autospec=True)
def test_take_action(self, mock_system):

self.app.client_manager.baremetal.node.get_console.\
return_value = self.node_console_1

arglist = ['node_console_1']
verifylist = []

parsed_args = self.check_parser(self.cmd, arglist, verifylist)

result = self.cmd.take_action(parsed_args)

self.assertEqual(0, result)
mock_system.assert_called_once

def test_take_action_no_console_info(self):

self.app.client_manager.baremetal.node.get_console.\
return_value = self.node_console_2

arglist = ['node_console_2']
verifylist = []
parsed_args = self.check_parser(self.cmd, arglist, verifylist)

self.assertRaisesRegex(
exceptions.CommandError,
'ERROR: No console info for node_console_2. '
'Run openstack baremetal node console '
'enable for given node',
self.cmd.take_action, parsed_args)
58 changes: 58 additions & 0 deletions esiclient/v1/node_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json
import logging
import os

from osc_lib.command import command
from osc_lib import exceptions
from osc_lib.i18n import _


class NodeConsoleConnect(command.Command):
"""Connect the node console"""

log = logging.getLogger(__name__ + ".NodeConsoleConnect")

def get_parser(self, prog_name):
parser = super(NodeConsoleConnect, self).get_parser(prog_name)
parser.add_argument(
"node",
metavar="<node>",
help=_("node"))

return parser

def take_action(self, parsed_args):
self.log.debug("take_action(%s)", parsed_args)

node = parsed_args.node

ironic_client = self.app.client_manager.baremetal

output = ironic_client.node.get_console(node)

console_show_output = json.loads(json.dumps(output))

console_info = console_show_output['console_info']

if console_info is None:
raise exceptions.CommandError(
"ERROR: No console info for %s. "
"Run openstack baremetal node console "
"enable for given node" % node)

else:
connect_url = console_show_output['console_info']['url']
connect_url = connect_url.replace('//', '')
return os.system('socat '+connect_url + ' -')
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ openstack.esiclient.v1 =
esi_trunk_list = esiclient.v1.trunk:List
esi_trunk_add_network = esiclient.v1.trunk:AddNetwork
esi_trunk_remove_network = esiclient.v1.trunk:RemoveNetwork
esi_node_console_connect = esiclient.v1.node_console:NodeConsoleConnect

0 comments on commit 4bf6994

Please sign in to comment.