-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from skanthed/esi-node-console-connect
esi-node-console-connect
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + ' -') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters