Revision 55cc0a44

b/Makefile.am
449 449
	test/ganeti.ht_unittest.py \
450 450
	test/ganeti.http_unittest.py \
451 451
	test/ganeti.hypervisor_unittest.py \
452
	test/ganeti.hypervisor.hv_chroot_unittest.py \
453
	test/ganeti.hypervisor.hv_fake_unittest.py \
452 454
	test/ganeti.hypervisor.hv_kvm_unittest.py \
455
	test/ganeti.hypervisor.hv_lxc_unittest.py \
456
	test/ganeti.hypervisor.hv_xen_unittest.py \
453 457
	test/ganeti.impexpd_unittest.py \
454 458
	test/ganeti.jqueue_unittest.py \
455 459
	test/ganeti.locking_unittest.py \
b/lib/cmdlib.py
7691 7691
    # instance and then saving the defaults in the instance itself.
7692 7692
    hvparams = cluster.FillHV(instance)
7693 7693
    beparams = cluster.FillBE(instance)
7694
    console_cmd = hyper.GetShellCommandForConsole(instance, hvparams, beparams)
7695

  
7696
    console = objects.InstanceConsole(instance=instance.name,
7697
                                      kind=constants.CONS_SSH,
7698
                                      host=node,
7699
                                      user="root",
7700
                                      command=console_cmd)
7694
    console = hyper.GetInstanceConsole(instance, hvparams, beparams)
7701 7695

  
7696
    assert console.instance == instance.name
7702 7697
    assert console.Validate()
7703 7698

  
7704 7699
    return console.ToDict()
b/lib/hypervisor/hv_base.py
206 206
    raise NotImplementedError
207 207

  
208 208
  @classmethod
209
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
210
    """Return a command for connecting to the console of an instance.
209
  def GetInstanceConsole(cls, instance, hvparams, beparams):
210
    """Return information for connecting to the console of an instance.
211 211

  
212 212
    """
213 213
    raise NotImplementedError
b/lib/hypervisor/hv_chroot.py
31 31
from ganeti import constants
32 32
from ganeti import errors # pylint: disable-msg=W0611
33 33
from ganeti import utils
34
from ganeti import objects
34 35
from ganeti.hypervisor import hv_base
35 36
from ganeti.errors import HypervisorError
36 37

  
......
246 247
    return self.GetLinuxNodeInfo()
247 248

  
248 249
  @classmethod
249
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
250
    """Return a command for connecting to the console of an instance.
250
  def GetInstanceConsole(cls, instance, # pylint: disable-msg=W0221
251
                         hvparams, beparams, root_dir=None):
252
    """Return information for connecting to the console of an instance.
251 253

  
252 254
    """
253
    root_dir = cls._InstanceDir(instance.name)
254
    if not os.path.ismount(root_dir):
255
      raise HypervisorError("Instance %s is not running" % instance.name)
256

  
257
    return "chroot %s" % root_dir
255
    if root_dir is None:
256
      root_dir = cls._InstanceDir(instance.name)
257
      if not os.path.ismount(root_dir):
258
        raise HypervisorError("Instance %s is not running" % instance.name)
259

  
260
    return objects.InstanceConsole(instance=instance.name,
261
                                   kind=constants.CONS_SSH,
262
                                   host=instance.primary_node,
263
                                   user=constants.GANETI_RUNAS,
264
                                   command=["chroot", root_dir])
258 265

  
259 266
  def Verify(self):
260 267
    """Verify the hypervisor.
b/lib/hypervisor/hv_fake.py
30 30
from ganeti import utils
31 31
from ganeti import constants
32 32
from ganeti import errors
33
from ganeti import objects
33 34
from ganeti.hypervisor import hv_base
34 35

  
35 36

  
......
204 205
    return result
205 206

  
206 207
  @classmethod
207
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
208
    """Return a command for connecting to the console of an instance.
208
  def GetInstanceConsole(cls, instance, hvparams, beparams):
209
    """Return information for connecting to the console of an instance.
209 210

  
210 211
    """
211
    return "echo Console not available for fake hypervisor"
212
    return objects.InstanceConsole(instance=instance.name,
213
                                   kind=constants.CONS_MESSAGE,
214
                                   message=("Console not available for fake"
215
                                            " hypervisor"))
212 216

  
213 217
  def Verify(self):
214 218
    """Verify the hypervisor.
b/lib/hypervisor/hv_kvm.py
1035 1035
    return self.GetLinuxNodeInfo()
1036 1036

  
1037 1037
  @classmethod
1038
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
1038
  def GetInstanceConsole(cls, instance, hvparams, beparams):
1039 1039
    """Return a command for connecting to the console of an instance.
1040 1040

  
1041 1041
    """
1042 1042
    if hvparams[constants.HV_SERIAL_CONSOLE]:
1043
      shell_command = ("%s STDIO,%s UNIX-CONNECT:%s" %
1044
                       (constants.SOCAT_PATH, cls._SocatUnixConsoleParams(),
1045
                        utils.ShellQuote(cls._InstanceSerial(instance.name))))
1046
    else:
1047
      shell_command = "echo 'No serial shell for instance %s'" % instance.name
1043
      cmd = [constants.SOCAT_PATH,
1044
             "STDIO,%s" % cls._SocatUnixConsoleParams(),
1045
             "UNIX-CONNECT:%s" % cls._InstanceSerial(instance.name)]
1046
      return objects.InstanceConsole(instance=instance.name,
1047
                                     kind=constants.CONS_SSH,
1048
                                     host=instance.primary_node,
1049
                                     user=constants.GANETI_RUNAS,
1050
                                     command=cmd)
1048 1051

  
1049 1052
    vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
1050
    if vnc_bind_address:
1051
      if instance.network_port > constants.VNC_BASE_PORT:
1052
        display = instance.network_port - constants.VNC_BASE_PORT
1053
        vnc_command = ("echo 'Instance has VNC listening on %s:%d"
1054
                       " (display: %d)'" % (vnc_bind_address,
1055
                                            instance.network_port,
1056
                                            display))
1057
        shell_command = "%s; %s" % (vnc_command, shell_command)
1058

  
1059
    return shell_command
1053
    if vnc_bind_address and instance.network_port > constants.VNC_BASE_PORT:
1054
      display = instance.network_port - constants.VNC_BASE_PORT
1055
      return objects.InstanceConsole(instance=instance.name,
1056
                                     kind=constants.CONS_VNC,
1057
                                     host=vnc_bind_address,
1058
                                     port=instance.network_port,
1059
                                     display=display)
1060

  
1061
    return objects.InstanceConsole(instance=instance.name,
1062
                                   kind=constants.CONS_MESSAGE,
1063
                                   message=("No serial shell for instance %s" %
1064
                                            instance.name))
1060 1065

  
1061 1066
  def Verify(self):
1062 1067
    """Verify the hypervisor.
......
1069 1074
    if not os.path.exists(constants.SOCAT_PATH):
1070 1075
      return "The socat binary ('%s') does not exist." % constants.SOCAT_PATH
1071 1076

  
1072

  
1073 1077
  @classmethod
1074 1078
  def CheckParameterSyntax(cls, hvparams):
1075 1079
    """Check the given parameters for validity.
b/lib/hypervisor/hv_lxc.py
31 31
from ganeti import constants
32 32
from ganeti import errors # pylint: disable-msg=W0611
33 33
from ganeti import utils
34
from ganeti import objects
34 35
from ganeti.hypervisor import hv_base
35 36
from ganeti.errors import HypervisorError
36 37

  
......
374 375
    return self.GetLinuxNodeInfo()
375 376

  
376 377
  @classmethod
377
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
378
  def GetInstanceConsole(cls, instance, hvparams, beparams):
378 379
    """Return a command for connecting to the console of an instance.
379 380

  
380 381
    """
381
    return "lxc-console -n %s" % instance.name
382
    return objects.InstanceConsole(instance=instance.name,
383
                                   kind=constants.CONS_SSH,
384
                                   host=instance.primary_node,
385
                                   user=constants.GANETI_RUNAS,
386
                                   command=["lxc-console", "-n", instance.name])
382 387

  
383 388
  def Verify(self):
384 389
    """Verify the hypervisor.
b/lib/hypervisor/hv_xen.py
31 31
from ganeti import utils
32 32
from ganeti.hypervisor import hv_base
33 33
from ganeti import netutils
34
from ganeti import objects
34 35

  
35 36

  
36 37
class XenHypervisor(hv_base.BaseHypervisor):
......
294 295
    return result
295 296

  
296 297
  @classmethod
297
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
298
  def GetInstanceConsole(cls, instance, hvparams, beparams):
298 299
    """Return a command for connecting to the console of an instance.
299 300

  
300 301
    """
301
    return "xm console %s" % instance.name
302

  
302
    return objects.InstanceConsole(instance=instance.name,
303
                                   kind=constants.CONS_SSH,
304
                                   host=instance.primary_node,
305
                                   user=constants.GANETI_RUNAS,
306
                                   command=["xm", "console", instance.name])
303 307

  
304 308
  def Verify(self):
305 309
    """Verify the hypervisor.
b/test/ganeti.hypervisor.hv_chroot_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for testing ganeti.hypervisor.hv_chroot"""
23

  
24
import unittest
25
import tempfile
26
import shutil
27

  
28
from ganeti import constants
29
from ganeti import objects
30
from ganeti import hypervisor
31

  
32
from ganeti.hypervisor import hv_chroot
33

  
34
import testutils
35

  
36

  
37
class TestConsole(unittest.TestCase):
38
  def setUp(self):
39
    self.tmpdir = tempfile.mkdtemp()
40

  
41
  def tearDown(self):
42
    shutil.rmtree(self.tmpdir)
43

  
44
  def test(self):
45
    instance = objects.Instance(name="fake.example.com", primary_node="node837")
46
    cons = hv_chroot.ChrootManager.GetInstanceConsole(instance, {}, {},
47
                                                      root_dir=self.tmpdir)
48
    self.assertTrue(cons.Validate())
49
    self.assertEqual(cons.kind, constants.CONS_SSH)
50
    self.assertEqual(cons.host, instance.primary_node)
51

  
52

  
53
if __name__ == "__main__":
54
  testutils.GanetiTestProgram()
b/test/ganeti.hypervisor.hv_fake_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for testing ganeti.hypervisor.hv_fake"""
23

  
24
import unittest
25

  
26
from ganeti import constants
27
from ganeti import objects
28
from ganeti import hypervisor
29

  
30
from ganeti.hypervisor import hv_fake
31

  
32
import testutils
33

  
34

  
35
class TestConsole(unittest.TestCase):
36
  def test(self):
37
    instance = objects.Instance(name="fake.example.com")
38
    cons = hv_fake.FakeHypervisor.GetInstanceConsole(instance, {}, {})
39
    self.assertTrue(cons.Validate())
40
    self.assertEqual(cons.kind, constants.CONS_MESSAGE)
41

  
42

  
43
if __name__ == "__main__":
44
  testutils.GanetiTestProgram()
b/test/ganeti.hypervisor.hv_kvm_unittest.py
1 1
#!/usr/bin/python
2 2
#
3 3

  
4
# Copyright (C) 2010 Google Inc.
4
# Copyright (C) 2010, 2011 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
80 80
                      inst, nic, 2)
81 81

  
82 82

  
83
class TestConsole(unittest.TestCase):
84
  def _Test(self, instance, hvparams):
85
    cons = hv_kvm.KVMHypervisor.GetInstanceConsole(instance, hvparams, {})
86
    self.assertTrue(cons.Validate())
87
    return cons
88

  
89
  def testSerial(self):
90
    instance = objects.Instance(name="kvm.example.com",
91
                                primary_node="node6017")
92
    hvparams = {
93
      constants.HV_SERIAL_CONSOLE: True,
94
      constants.HV_VNC_BIND_ADDRESS: None,
95
      }
96
    cons = self._Test(instance, hvparams)
97
    self.assertEqual(cons.kind, constants.CONS_SSH)
98
    self.assertEqual(cons.host, instance.primary_node)
99
    self.assertEqual(cons.command[0], constants.SOCAT_PATH)
100

  
101
  def testVnc(self):
102
    instance = objects.Instance(name="kvm.example.com",
103
                                primary_node="node7235",
104
                                network_port=constants.VNC_BASE_PORT + 10)
105
    hvparams = {
106
      constants.HV_SERIAL_CONSOLE: False,
107
      constants.HV_VNC_BIND_ADDRESS: "192.0.2.1",
108
      }
109
    cons = self._Test(instance, hvparams)
110
    self.assertEqual(cons.kind, constants.CONS_VNC)
111
    self.assertEqual(cons.host, "192.0.2.1")
112
    self.assertEqual(cons.port, constants.VNC_BASE_PORT + 10)
113
    self.assertEqual(cons.display, 10)
114

  
115
  def testNoConsole(self):
116
    instance = objects.Instance(name="kvm.example.com",
117
                                primary_node="node24325",
118
                                network_port=0)
119
    hvparams = {
120
      constants.HV_SERIAL_CONSOLE: False,
121
      constants.HV_VNC_BIND_ADDRESS: None,
122
      }
123
    cons = self._Test(instance, hvparams)
124
    self.assertEqual(cons.kind, constants.CONS_MESSAGE)
125

  
126

  
83 127
if __name__ == "__main__":
84 128
  testutils.GanetiTestProgram()
b/test/ganeti.hypervisor.hv_lxc_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for testing ganeti.hypervisor.hv_lxc"""
23

  
24
import unittest
25

  
26
from ganeti import constants
27
from ganeti import objects
28
from ganeti import hypervisor
29

  
30
from ganeti.hypervisor import hv_lxc
31

  
32
import testutils
33

  
34

  
35
class TestConsole(unittest.TestCase):
36
  def test(self):
37
    instance = objects.Instance(name="lxc.example.com", primary_node="node199")
38
    cons = hv_lxc.LXCHypervisor.GetInstanceConsole(instance, {}, {})
39
    self.assertTrue(cons.Validate())
40
    self.assertEqual(cons.kind, constants.CONS_SSH)
41
    self.assertEqual(cons.host, instance.primary_node)
42
    self.assertEqual(cons.command[-1], instance.name)
43

  
44

  
45
if __name__ == "__main__":
46
  testutils.GanetiTestProgram()
b/test/ganeti.hypervisor.hv_xen_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for testing ganeti.hypervisor.hv_lxc"""
23

  
24
import unittest
25

  
26
from ganeti import constants
27
from ganeti import objects
28
from ganeti import hypervisor
29

  
30
from ganeti.hypervisor import hv_xen
31

  
32
import testutils
33

  
34

  
35
class TestConsole(unittest.TestCase):
36
  def test(self):
37
    for cls in [hv_xen.XenPvmHypervisor, hv_xen.XenHvmHypervisor]:
38
      instance = objects.Instance(name="xen.example.com",
39
                                  primary_node="node24828")
40
      cons = cls.GetInstanceConsole(instance, {}, {})
41
      self.assertTrue(cons.Validate())
42
      self.assertEqual(cons.kind, constants.CONS_SSH)
43
      self.assertEqual(cons.host, instance.primary_node)
44
      self.assertEqual(cons.command[-1], instance.name)
45

  
46

  
47
if __name__ == "__main__":
48
  testutils.GanetiTestProgram()

Also available in: Unified diff