Statistics
| Branch: | Tag: | Revision:

root / lib / server / noded.py @ b262a5c6

History | View | Annotate | Download (32.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2010, 2011, 2012 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
"""Ganeti node daemon"""
23

    
24
# pylint: disable=C0103,W0142
25

    
26
# C0103: Functions in this module need to have a given name structure,
27
# and the name of the daemon doesn't match
28

    
29
# W0142: Used * or ** magic, since we do use it extensively in this
30
# module
31

    
32
import os
33
import sys
34
import logging
35
import signal
36
import codecs
37

    
38
from optparse import OptionParser
39

    
40
from ganeti import backend
41
from ganeti import constants
42
from ganeti import objects
43
from ganeti import errors
44
from ganeti import jstore
45
from ganeti import daemon
46
from ganeti import http
47
from ganeti import utils
48
from ganeti.storage import container
49
from ganeti import serializer
50
from ganeti import netutils
51
from ganeti import pathutils
52
from ganeti import ssconf
53

    
54
import ganeti.http.server # pylint: disable=W0611
55

    
56

    
57
queue_lock = None
58

    
59

    
60
def _extendReasonTrail(trail, source, reason=""):
61
  """Extend the reason trail with noded information
62

63
  The trail is extended by appending the name of the noded functionality
64
  """
65
  assert trail is not None
66
  trail_source = "%s:%s" % (constants.OPCODE_REASON_SRC_NODED, source)
67
  trail.append((trail_source, reason, utils.EpochNano()))
68

    
69

    
70
def _PrepareQueueLock():
71
  """Try to prepare the queue lock.
72

73
  @return: None for success, otherwise an exception object
74

75
  """
76
  global queue_lock # pylint: disable=W0603
77

    
78
  if queue_lock is not None:
79
    return None
80

    
81
  # Prepare job queue
82
  try:
83
    queue_lock = jstore.InitAndVerifyQueue(must_lock=False)
84
    return None
85
  except EnvironmentError, err:
86
    return err
87

    
88

    
89
def _RequireJobQueueLock(fn):
90
  """Decorator for job queue manipulating functions.
91

92
  """
93
  QUEUE_LOCK_TIMEOUT = 10
94

    
95
  def wrapper(*args, **kwargs):
96
    # Locking in exclusive, blocking mode because there could be several
97
    # children running at the same time. Waiting up to 10 seconds.
98
    if _PrepareQueueLock() is not None:
99
      raise errors.JobQueueError("Job queue failed initialization,"
100
                                 " cannot update jobs")
101
    queue_lock.Exclusive(blocking=True, timeout=QUEUE_LOCK_TIMEOUT)
102
    try:
103
      return fn(*args, **kwargs)
104
    finally:
105
      queue_lock.Unlock()
106

    
107
  return wrapper
108

    
109

    
110
def _DecodeImportExportIO(ieio, ieioargs):
111
  """Decodes import/export I/O information.
112

113
  """
114
  if ieio == constants.IEIO_RAW_DISK:
115
    assert len(ieioargs) == 1
116
    return (objects.Disk.FromDict(ieioargs[0]), )
117

    
118
  if ieio == constants.IEIO_SCRIPT:
119
    assert len(ieioargs) == 2
120
    return (objects.Disk.FromDict(ieioargs[0]), ieioargs[1])
121

    
122
  return ieioargs
123

    
124

    
125
def _DefaultAlternative(value, default):
126
  """Returns value or, if evaluating to False, a default value.
127

128
  Returns the given value, unless it evaluates to False. In the latter case the
129
  default value is returned.
130

131
  @param value: Value to return if it doesn't evaluate to False
132
  @param default: Default value
133
  @return: Given value or the default
134

135
  """
136
  if value:
137
    return value
138

    
139
  return default
140

    
141

    
142
class MlockallRequestExecutor(http.server.HttpServerRequestExecutor):
143
  """Subclass ensuring request handlers are locked in RAM.
144

145
  """
146
  def __init__(self, *args, **kwargs):
147
    utils.Mlockall()
148

    
149
    http.server.HttpServerRequestExecutor.__init__(self, *args, **kwargs)
150

    
151

    
152
class NodeRequestHandler(http.server.HttpServerHandler):
153
  """The server implementation.
154

155
  This class holds all methods exposed over the RPC interface.
156

157
  """
158
  # too many public methods, and unused args - all methods get params
159
  # due to the API
160
  # pylint: disable=R0904,W0613
161
  def __init__(self):
162
    http.server.HttpServerHandler.__init__(self)
163
    self.noded_pid = os.getpid()
164

    
165
  def HandleRequest(self, req):
166
    """Handle a request.
167

168
    """
169
    if req.request_method.upper() != http.HTTP_POST:
170
      raise http.HttpBadRequest("Only the POST method is supported")
171

    
172
    path = req.request_path
173
    if path.startswith("/"):
174
      path = path[1:]
175

    
176
    method = getattr(self, "perspective_%s" % path, None)
177
    if method is None:
178
      raise http.HttpNotFound()
179

    
180
    try:
181
      result = (True, method(serializer.LoadJson(req.request_body)))
182

    
183
    except backend.RPCFail, err:
184
      # our custom failure exception; str(err) works fine if the
185
      # exception was constructed with a single argument, and in
186
      # this case, err.message == err.args[0] == str(err)
187
      result = (False, str(err))
188
    except errors.QuitGanetiException, err:
189
      # Tell parent to quit
190
      logging.info("Shutting down the node daemon, arguments: %s",
191
                   str(err.args))
192
      os.kill(self.noded_pid, signal.SIGTERM)
193
      # And return the error's arguments, which must be already in
194
      # correct tuple format
195
      result = err.args
196
    except Exception, err:
197
      logging.exception("Error in RPC call")
198
      result = (False, "Error while executing backend function: %s" % str(err))
199

    
200
    return serializer.DumpJson(result)
201

    
202
  # the new block devices  --------------------------
203

    
204
  @staticmethod
205
  def perspective_blockdev_create(params):
206
    """Create a block device.
207

208
    """
209
    (bdev_s, size, owner, on_primary, info, excl_stor) = params
210
    bdev = objects.Disk.FromDict(bdev_s)
211
    if bdev is None:
212
      raise ValueError("can't unserialize data!")
213
    return backend.BlockdevCreate(bdev, size, owner, on_primary, info,
214
                                  excl_stor)
215

    
216
  @staticmethod
217
  def perspective_blockdev_pause_resume_sync(params):
218
    """Pause/resume sync of a block device.
219

220
    """
221
    disks_s, pause = params
222
    disks = [objects.Disk.FromDict(bdev_s) for bdev_s in disks_s]
223
    return backend.BlockdevPauseResumeSync(disks, pause)
224

    
225
  @staticmethod
226
  def perspective_blockdev_wipe(params):
227
    """Wipe a block device.
228

229
    """
230
    bdev_s, offset, size = params
231
    bdev = objects.Disk.FromDict(bdev_s)
232
    return backend.BlockdevWipe(bdev, offset, size)
233

    
234
  @staticmethod
235
  def perspective_blockdev_remove(params):
236
    """Remove a block device.
237

238
    """
239
    bdev_s = params[0]
240
    bdev = objects.Disk.FromDict(bdev_s)
241
    return backend.BlockdevRemove(bdev)
242

    
243
  @staticmethod
244
  def perspective_blockdev_rename(params):
245
    """Remove a block device.
246

247
    """
248
    devlist = [(objects.Disk.FromDict(ds), uid) for ds, uid in params[0]]
249
    return backend.BlockdevRename(devlist)
250

    
251
  @staticmethod
252
  def perspective_blockdev_assemble(params):
253
    """Assemble a block device.
254

255
    """
256
    bdev_s, owner, on_primary, idx = params
257
    bdev = objects.Disk.FromDict(bdev_s)
258
    if bdev is None:
259
      raise ValueError("can't unserialize data!")
260
    return backend.BlockdevAssemble(bdev, owner, on_primary, idx)
261

    
262
  @staticmethod
263
  def perspective_blockdev_shutdown(params):
264
    """Shutdown a block device.
265

266
    """
267
    bdev_s = params[0]
268
    bdev = objects.Disk.FromDict(bdev_s)
269
    if bdev is None:
270
      raise ValueError("can't unserialize data!")
271
    return backend.BlockdevShutdown(bdev)
272

    
273
  @staticmethod
274
  def perspective_blockdev_addchildren(params):
275
    """Add a child to a mirror device.
276

277
    Note: this is only valid for mirror devices. It's the caller's duty
278
    to send a correct disk, otherwise we raise an error.
279

280
    """
281
    bdev_s, ndev_s = params
282
    bdev = objects.Disk.FromDict(bdev_s)
283
    ndevs = [objects.Disk.FromDict(disk_s) for disk_s in ndev_s]
284
    if bdev is None or ndevs.count(None) > 0:
285
      raise ValueError("can't unserialize data!")
286
    return backend.BlockdevAddchildren(bdev, ndevs)
287

    
288
  @staticmethod
289
  def perspective_blockdev_removechildren(params):
290
    """Remove a child from a mirror device.
291

292
    This is only valid for mirror devices, of course. It's the callers
293
    duty to send a correct disk, otherwise we raise an error.
294

295
    """
296
    bdev_s, ndev_s = params
297
    bdev = objects.Disk.FromDict(bdev_s)
298
    ndevs = [objects.Disk.FromDict(disk_s) for disk_s in ndev_s]
299
    if bdev is None or ndevs.count(None) > 0:
300
      raise ValueError("can't unserialize data!")
301
    return backend.BlockdevRemovechildren(bdev, ndevs)
302

    
303
  @staticmethod
304
  def perspective_blockdev_getmirrorstatus(params):
305
    """Return the mirror status for a list of disks.
306

307
    """
308
    disks = [objects.Disk.FromDict(dsk_s)
309
             for dsk_s in params[0]]
310
    return [status.ToDict()
311
            for status in backend.BlockdevGetmirrorstatus(disks)]
312

    
313
  @staticmethod
314
  def perspective_blockdev_getmirrorstatus_multi(params):
315
    """Return the mirror status for a list of disks.
316

317
    """
318
    (node_disks, ) = params
319

    
320
    disks = [objects.Disk.FromDict(dsk_s) for dsk_s in node_disks]
321

    
322
    result = []
323

    
324
    for (success, status) in backend.BlockdevGetmirrorstatusMulti(disks):
325
      if success:
326
        result.append((success, status.ToDict()))
327
      else:
328
        result.append((success, status))
329

    
330
    return result
331

    
332
  @staticmethod
333
  def perspective_blockdev_find(params):
334
    """Expose the FindBlockDevice functionality for a disk.
335

336
    This will try to find but not activate a disk.
337

338
    """
339
    disk = objects.Disk.FromDict(params[0])
340

    
341
    result = backend.BlockdevFind(disk)
342
    if result is None:
343
      return None
344

    
345
    return result.ToDict()
346

    
347
  @staticmethod
348
  def perspective_blockdev_snapshot(params):
349
    """Create a snapshot device.
350

351
    Note that this is only valid for LVM disks, if we get passed
352
    something else we raise an exception. The snapshot device can be
353
    remove by calling the generic block device remove call.
354

355
    """
356
    cfbd = objects.Disk.FromDict(params[0])
357
    return backend.BlockdevSnapshot(cfbd)
358

    
359
  @staticmethod
360
  def perspective_blockdev_grow(params):
361
    """Grow a stack of devices.
362

363
    """
364
    if len(params) < 5:
365
      raise ValueError("Received only %s parameters in blockdev_grow,"
366
                       " old master?" % len(params))
367
    cfbd = objects.Disk.FromDict(params[0])
368
    amount = params[1]
369
    dryrun = params[2]
370
    backingstore = params[3]
371
    excl_stor = params[4]
372
    return backend.BlockdevGrow(cfbd, amount, dryrun, backingstore, excl_stor)
373

    
374
  @staticmethod
375
  def perspective_blockdev_close(params):
376
    """Closes the given block devices.
377

378
    """
379
    disks = [objects.Disk.FromDict(cf) for cf in params[1]]
380
    return backend.BlockdevClose(params[0], disks)
381

    
382
  @staticmethod
383
  def perspective_blockdev_getdimensions(params):
384
    """Compute the sizes of the given block devices.
385

386
    """
387
    disks = [objects.Disk.FromDict(cf) for cf in params[0]]
388
    return backend.BlockdevGetdimensions(disks)
389

    
390
  @staticmethod
391
  def perspective_blockdev_export(params):
392
    """Compute the sizes of the given block devices.
393

394
    """
395
    disk = objects.Disk.FromDict(params[0])
396
    dest_node, dest_path, cluster_name = params[1:]
397
    return backend.BlockdevExport(disk, dest_node, dest_path, cluster_name)
398

    
399
  @staticmethod
400
  def perspective_blockdev_setinfo(params):
401
    """Sets metadata information on the given block device.
402

403
    """
404
    (disk, info) = params
405
    disk = objects.Disk.FromDict(disk)
406
    return backend.BlockdevSetInfo(disk, info)
407

    
408
  # blockdev/drbd specific methods ----------
409

    
410
  @staticmethod
411
  def perspective_drbd_disconnect_net(params):
412
    """Disconnects the network connection of drbd disks.
413

414
    Note that this is only valid for drbd disks, so the members of the
415
    disk list must all be drbd devices.
416

417
    """
418
    nodes_ip, disks, target_node_uuid = params
419
    disks = [objects.Disk.FromDict(cf) for cf in disks]
420
    return backend.DrbdDisconnectNet(target_node_uuid, nodes_ip, disks)
421

    
422
  @staticmethod
423
  def perspective_drbd_attach_net(params):
424
    """Attaches the network connection of drbd disks.
425

426
    Note that this is only valid for drbd disks, so the members of the
427
    disk list must all be drbd devices.
428

429
    """
430
    nodes_ip, disks, instance_name, multimaster, target_node_uuid = params
431
    disks = [objects.Disk.FromDict(cf) for cf in disks]
432
    return backend.DrbdAttachNet(target_node_uuid, nodes_ip, disks,
433
                                 instance_name, multimaster)
434

    
435
  @staticmethod
436
  def perspective_drbd_wait_sync(params):
437
    """Wait until DRBD disks are synched.
438

439
    Note that this is only valid for drbd disks, so the members of the
440
    disk list must all be drbd devices.
441

442
    """
443
    nodes_ip, disks, target_node_uuid = params
444
    disks = [objects.Disk.FromDict(cf) for cf in disks]
445
    return backend.DrbdWaitSync(target_node_uuid, nodes_ip, disks)
446

    
447
  @staticmethod
448
  def perspective_drbd_needs_activation(params):
449
    """Checks if the drbd devices need activation
450

451
    Note that this is only valid for drbd disks, so the members of the
452
    disk list must all be drbd devices.
453

454
    """
455
    nodes_ip, disks, target_node_uuid = params
456
    disks = [objects.Disk.FromDict(cf) for cf in disks]
457
    return backend.DrbdNeedsActivation(target_node_uuid, nodes_ip, disks)
458

    
459
  @staticmethod
460
  def perspective_drbd_helper(params):
461
    """Query drbd helper.
462

463
    """
464
    return backend.GetDrbdUsermodeHelper()
465

    
466
  # export/import  --------------------------
467

    
468
  @staticmethod
469
  def perspective_finalize_export(params):
470
    """Expose the finalize export functionality.
471

472
    """
473
    instance = objects.Instance.FromDict(params[0])
474

    
475
    snap_disks = []
476
    for disk in params[1]:
477
      if isinstance(disk, bool):
478
        snap_disks.append(disk)
479
      else:
480
        snap_disks.append(objects.Disk.FromDict(disk))
481

    
482
    return backend.FinalizeExport(instance, snap_disks)
483

    
484
  @staticmethod
485
  def perspective_export_info(params):
486
    """Query information about an existing export on this node.
487

488
    The given path may not contain an export, in which case we return
489
    None.
490

491
    """
492
    path = params[0]
493
    return backend.ExportInfo(path)
494

    
495
  @staticmethod
496
  def perspective_export_list(params):
497
    """List the available exports on this node.
498

499
    Note that as opposed to export_info, which may query data about an
500
    export in any path, this only queries the standard Ganeti path
501
    (pathutils.EXPORT_DIR).
502

503
    """
504
    return backend.ListExports()
505

    
506
  @staticmethod
507
  def perspective_export_remove(params):
508
    """Remove an export.
509

510
    """
511
    export = params[0]
512
    return backend.RemoveExport(export)
513

    
514
  # block device ---------------------
515
  @staticmethod
516
  def perspective_bdev_sizes(params):
517
    """Query the list of block devices
518

519
    """
520
    devices = params[0]
521
    return backend.GetBlockDevSizes(devices)
522

    
523
  # volume  --------------------------
524

    
525
  @staticmethod
526
  def perspective_lv_list(params):
527
    """Query the list of logical volumes in a given volume group.
528

529
    """
530
    vgname = params[0]
531
    return backend.GetVolumeList(vgname)
532

    
533
  @staticmethod
534
  def perspective_vg_list(params):
535
    """Query the list of volume groups.
536

537
    """
538
    return backend.ListVolumeGroups()
539

    
540
  # Storage --------------------------
541

    
542
  @staticmethod
543
  def perspective_storage_list(params):
544
    """Get list of storage units.
545

546
    """
547
    (su_name, su_args, name, fields) = params
548
    return container.GetStorage(su_name, *su_args).List(name, fields)
549

    
550
  @staticmethod
551
  def perspective_storage_modify(params):
552
    """Modify a storage unit.
553

554
    """
555
    (su_name, su_args, name, changes) = params
556
    return container.GetStorage(su_name, *su_args).Modify(name, changes)
557

    
558
  @staticmethod
559
  def perspective_storage_execute(params):
560
    """Execute an operation on a storage unit.
561

562
    """
563
    (su_name, su_args, name, op) = params
564
    return container.GetStorage(su_name, *su_args).Execute(name, op)
565

    
566
  # bridge  --------------------------
567

    
568
  @staticmethod
569
  def perspective_bridges_exist(params):
570
    """Check if all bridges given exist on this node.
571

572
    """
573
    bridges_list = params[0]
574
    return backend.BridgesExist(bridges_list)
575

    
576
  # instance  --------------------------
577

    
578
  @staticmethod
579
  def perspective_instance_os_add(params):
580
    """Install an OS on a given instance.
581

582
    """
583
    inst_s = params[0]
584
    inst = objects.Instance.FromDict(inst_s)
585
    reinstall = params[1]
586
    debug = params[2]
587
    return backend.InstanceOsAdd(inst, reinstall, debug)
588

    
589
  @staticmethod
590
  def perspective_instance_run_rename(params):
591
    """Runs the OS rename script for an instance.
592

593
    """
594
    inst_s, old_name, debug = params
595
    inst = objects.Instance.FromDict(inst_s)
596
    return backend.RunRenameInstance(inst, old_name, debug)
597

    
598
  @staticmethod
599
  def perspective_instance_shutdown(params):
600
    """Shutdown an instance.
601

602
    """
603
    instance = objects.Instance.FromDict(params[0])
604
    timeout = params[1]
605
    trail = params[2]
606
    _extendReasonTrail(trail, "shutdown")
607
    return backend.InstanceShutdown(instance, timeout, trail)
608

    
609
  @staticmethod
610
  def perspective_instance_start(params):
611
    """Start an instance.
612

613
    """
614
    (instance_name, startup_paused, trail) = params
615
    instance = objects.Instance.FromDict(instance_name)
616
    _extendReasonTrail(trail, "start")
617
    return backend.StartInstance(instance, startup_paused, trail)
618

    
619
  @staticmethod
620
  def perspective_hotplug_device(params):
621
    """Hotplugs device to a running instance.
622

623
    """
624
    (idict, action, dev_type, ddict, extra, seq) = params
625
    instance = objects.Instance.FromDict(idict)
626
    if dev_type == constants.HOTPLUG_DISK:
627
      device = objects.Disk.FromDict(ddict)
628
    elif dev_type == constants.HOTPLUG_NIC:
629
      device = objects.NIC.FromDict(ddict)
630
    return backend.HotplugDevice(instance, action, dev_type, device, extra, seq)
631

    
632
  @staticmethod
633
  def perspective_migration_info(params):
634
    """Gather information about an instance to be migrated.
635

636
    """
637
    instance = objects.Instance.FromDict(params[0])
638
    return backend.MigrationInfo(instance)
639

    
640
  @staticmethod
641
  def perspective_accept_instance(params):
642
    """Prepare the node to accept an instance.
643

644
    """
645
    instance, info, target = params
646
    instance = objects.Instance.FromDict(instance)
647
    return backend.AcceptInstance(instance, info, target)
648

    
649
  @staticmethod
650
  def perspective_instance_finalize_migration_dst(params):
651
    """Finalize the instance migration on the destination node.
652

653
    """
654
    instance, info, success = params
655
    instance = objects.Instance.FromDict(instance)
656
    return backend.FinalizeMigrationDst(instance, info, success)
657

    
658
  @staticmethod
659
  def perspective_instance_migrate(params):
660
    """Migrates an instance.
661

662
    """
663
    cluster_name, instance, target, live = params
664
    instance = objects.Instance.FromDict(instance)
665
    return backend.MigrateInstance(cluster_name, instance, target, live)
666

    
667
  @staticmethod
668
  def perspective_instance_finalize_migration_src(params):
669
    """Finalize the instance migration on the source node.
670

671
    """
672
    instance, success, live = params
673
    instance = objects.Instance.FromDict(instance)
674
    return backend.FinalizeMigrationSource(instance, success, live)
675

    
676
  @staticmethod
677
  def perspective_instance_get_migration_status(params):
678
    """Reports migration status.
679

680
    """
681
    instance = objects.Instance.FromDict(params[0])
682
    return backend.GetMigrationStatus(instance).ToDict()
683

    
684
  @staticmethod
685
  def perspective_instance_reboot(params):
686
    """Reboot an instance.
687

688
    """
689
    instance = objects.Instance.FromDict(params[0])
690
    reboot_type = params[1]
691
    shutdown_timeout = params[2]
692
    trail = params[3]
693
    _extendReasonTrail(trail, "reboot")
694
    return backend.InstanceReboot(instance, reboot_type, shutdown_timeout,
695
                                  trail)
696

    
697
  @staticmethod
698
  def perspective_instance_balloon_memory(params):
699
    """Modify instance runtime memory.
700

701
    """
702
    instance_dict, memory = params
703
    instance = objects.Instance.FromDict(instance_dict)
704
    return backend.InstanceBalloonMemory(instance, memory)
705

    
706
  @staticmethod
707
  def perspective_instance_info(params):
708
    """Query instance information.
709

710
    """
711
    (instance_name, hypervisor_name, hvparams) = params
712
    return backend.GetInstanceInfo(instance_name, hypervisor_name, hvparams)
713

    
714
  @staticmethod
715
  def perspective_instance_migratable(params):
716
    """Query whether the specified instance can be migrated.
717

718
    """
719
    instance = objects.Instance.FromDict(params[0])
720
    return backend.GetInstanceMigratable(instance)
721

    
722
  @staticmethod
723
  def perspective_all_instances_info(params):
724
    """Query information about all instances.
725

726
    """
727
    (hypervisor_list, all_hvparams) = params
728
    return backend.GetAllInstancesInfo(hypervisor_list, all_hvparams)
729

    
730
  @staticmethod
731
  def perspective_instance_list(params):
732
    """Query the list of running instances.
733

734
    """
735
    (hypervisor_list, hvparams) = params
736
    return backend.GetInstanceList(hypervisor_list, hvparams)
737

    
738
  # node --------------------------
739

    
740
  @staticmethod
741
  def perspective_node_has_ip_address(params):
742
    """Checks if a node has the given ip address.
743

744
    """
745
    return netutils.IPAddress.Own(params[0])
746

    
747
  @staticmethod
748
  def perspective_node_info(params):
749
    """Query node information.
750

751
    """
752
    (storage_units, hv_specs) = params
753
    return backend.GetNodeInfo(storage_units, hv_specs)
754

    
755
  @staticmethod
756
  def perspective_etc_hosts_modify(params):
757
    """Modify a node entry in /etc/hosts.
758

759
    """
760
    backend.EtcHostsModify(params[0], params[1], params[2])
761

    
762
    return True
763

    
764
  @staticmethod
765
  def perspective_node_verify(params):
766
    """Run a verify sequence on this node.
767

768
    """
769
    (what, cluster_name, hvparams) = params
770
    return backend.VerifyNode(what, cluster_name, hvparams)
771

    
772
  @classmethod
773
  def perspective_node_verify_light(cls, params):
774
    """Run a light verify sequence on this node.
775

776
    """
777
    # So far it's the same as the normal node_verify
778
    return cls.perspective_node_verify(params)
779

    
780
  @staticmethod
781
  def perspective_node_start_master_daemons(params):
782
    """Start the master daemons on this node.
783

784
    """
785
    return backend.StartMasterDaemons(params[0])
786

    
787
  @staticmethod
788
  def perspective_node_activate_master_ip(params):
789
    """Activate the master IP on this node.
790

791
    """
792
    master_params = objects.MasterNetworkParameters.FromDict(params[0])
793
    return backend.ActivateMasterIp(master_params, params[1])
794

    
795
  @staticmethod
796
  def perspective_node_deactivate_master_ip(params):
797
    """Deactivate the master IP on this node.
798

799
    """
800
    master_params = objects.MasterNetworkParameters.FromDict(params[0])
801
    return backend.DeactivateMasterIp(master_params, params[1])
802

    
803
  @staticmethod
804
  def perspective_node_stop_master(params):
805
    """Stops master daemons on this node.
806

807
    """
808
    return backend.StopMasterDaemons()
809

    
810
  @staticmethod
811
  def perspective_node_change_master_netmask(params):
812
    """Change the master IP netmask.
813

814
    """
815
    return backend.ChangeMasterNetmask(params[0], params[1], params[2],
816
                                       params[3])
817

    
818
  @staticmethod
819
  def perspective_node_leave_cluster(params):
820
    """Cleanup after leaving a cluster.
821

822
    """
823
    return backend.LeaveCluster(params[0])
824

    
825
  @staticmethod
826
  def perspective_node_volumes(params):
827
    """Query the list of all logical volume groups.
828

829
    """
830
    return backend.NodeVolumes()
831

    
832
  @staticmethod
833
  def perspective_node_demote_from_mc(params):
834
    """Demote a node from the master candidate role.
835

836
    """
837
    return backend.DemoteFromMC()
838

    
839
  @staticmethod
840
  def perspective_node_powercycle(params):
841
    """Tries to powercycle the nod.
842

843
    """
844
    (hypervisor_type, hvparams) = params
845
    return backend.PowercycleNode(hypervisor_type, hvparams)
846

    
847
  # cluster --------------------------
848

    
849
  @staticmethod
850
  def perspective_version(params):
851
    """Query version information.
852

853
    """
854
    return constants.PROTOCOL_VERSION
855

    
856
  @staticmethod
857
  def perspective_upload_file(params):
858
    """Upload a file.
859

860
    Note that the backend implementation imposes strict rules on which
861
    files are accepted.
862

863
    """
864
    return backend.UploadFile(*(params[0]))
865

    
866
  @staticmethod
867
  def perspective_master_info(params):
868
    """Query master information.
869

870
    """
871
    return backend.GetMasterInfo()
872

    
873
  @staticmethod
874
  def perspective_run_oob(params):
875
    """Runs oob on node.
876

877
    """
878
    output = backend.RunOob(params[0], params[1], params[2], params[3])
879
    if output:
880
      result = serializer.LoadJson(output)
881
    else:
882
      result = None
883
    return result
884

    
885
  @staticmethod
886
  def perspective_restricted_command(params):
887
    """Runs a restricted command.
888

889
    """
890
    (cmd, ) = params
891

    
892
    return backend.RunRestrictedCmd(cmd)
893

    
894
  @staticmethod
895
  def perspective_write_ssconf_files(params):
896
    """Write ssconf files.
897

898
    """
899
    (values,) = params
900
    return ssconf.WriteSsconfFiles(values)
901

    
902
  @staticmethod
903
  def perspective_get_watcher_pause(params):
904
    """Get watcher pause end.
905

906
    """
907
    return utils.ReadWatcherPauseFile(pathutils.WATCHER_PAUSEFILE)
908

    
909
  @staticmethod
910
  def perspective_set_watcher_pause(params):
911
    """Set watcher pause.
912

913
    """
914
    (until, ) = params
915
    return backend.SetWatcherPause(until)
916

    
917
  # os -----------------------
918

    
919
  @staticmethod
920
  def perspective_os_diagnose(params):
921
    """Query detailed information about existing OSes.
922

923
    """
924
    return backend.DiagnoseOS()
925

    
926
  @staticmethod
927
  def perspective_os_get(params):
928
    """Query information about a given OS.
929

930
    """
931
    name = params[0]
932
    os_obj = backend.OSFromDisk(name)
933
    return os_obj.ToDict()
934

    
935
  @staticmethod
936
  def perspective_os_validate(params):
937
    """Run a given OS' validation routine.
938

939
    """
940
    required, name, checks, params = params
941
    return backend.ValidateOS(required, name, checks, params)
942

    
943
  # extstorage -----------------------
944

    
945
  @staticmethod
946
  def perspective_extstorage_diagnose(params):
947
    """Query detailed information about existing extstorage providers.
948

949
    """
950
    return backend.DiagnoseExtStorage()
951

    
952
  # hooks -----------------------
953

    
954
  @staticmethod
955
  def perspective_hooks_runner(params):
956
    """Run hook scripts.
957

958
    """
959
    hpath, phase, env = params
960
    hr = backend.HooksRunner()
961
    return hr.RunHooks(hpath, phase, env)
962

    
963
  # iallocator -----------------
964

    
965
  @staticmethod
966
  def perspective_iallocator_runner(params):
967
    """Run an iallocator script.
968

969
    """
970
    name, idata = params
971
    iar = backend.IAllocatorRunner()
972
    return iar.Run(name, idata)
973

    
974
  # test -----------------------
975

    
976
  @staticmethod
977
  def perspective_test_delay(params):
978
    """Run test delay.
979

980
    """
981
    duration = params[0]
982
    status, rval = utils.TestDelay(duration)
983
    if not status:
984
      raise backend.RPCFail(rval)
985
    return rval
986

    
987
  # file storage ---------------
988

    
989
  @staticmethod
990
  def perspective_file_storage_dir_create(params):
991
    """Create the file storage directory.
992

993
    """
994
    file_storage_dir = params[0]
995
    return backend.CreateFileStorageDir(file_storage_dir)
996

    
997
  @staticmethod
998
  def perspective_file_storage_dir_remove(params):
999
    """Remove the file storage directory.
1000

1001
    """
1002
    file_storage_dir = params[0]
1003
    return backend.RemoveFileStorageDir(file_storage_dir)
1004

    
1005
  @staticmethod
1006
  def perspective_file_storage_dir_rename(params):
1007
    """Rename the file storage directory.
1008

1009
    """
1010
    old_file_storage_dir = params[0]
1011
    new_file_storage_dir = params[1]
1012
    return backend.RenameFileStorageDir(old_file_storage_dir,
1013
                                        new_file_storage_dir)
1014

    
1015
  # jobs ------------------------
1016

    
1017
  @staticmethod
1018
  @_RequireJobQueueLock
1019
  def perspective_jobqueue_update(params):
1020
    """Update job queue.
1021

1022
    """
1023
    (file_name, content) = params
1024
    return backend.JobQueueUpdate(file_name, content)
1025

    
1026
  @staticmethod
1027
  @_RequireJobQueueLock
1028
  def perspective_jobqueue_purge(params):
1029
    """Purge job queue.
1030

1031
    """
1032
    return backend.JobQueuePurge()
1033

    
1034
  @staticmethod
1035
  @_RequireJobQueueLock
1036
  def perspective_jobqueue_rename(params):
1037
    """Rename a job queue file.
1038

1039
    """
1040
    # TODO: What if a file fails to rename?
1041
    return [backend.JobQueueRename(old, new) for old, new in params[0]]
1042

    
1043
  @staticmethod
1044
  @_RequireJobQueueLock
1045
  def perspective_jobqueue_set_drain_flag(params):
1046
    """Set job queue's drain flag.
1047

1048
    """
1049
    (flag, ) = params
1050

    
1051
    return jstore.SetDrainFlag(flag)
1052

    
1053
  # hypervisor ---------------
1054

    
1055
  @staticmethod
1056
  def perspective_hypervisor_validate_params(params):
1057
    """Validate the hypervisor parameters.
1058

1059
    """
1060
    (hvname, hvparams) = params
1061
    return backend.ValidateHVParams(hvname, hvparams)
1062

    
1063
  # Crypto
1064

    
1065
  @staticmethod
1066
  def perspective_x509_cert_create(params):
1067
    """Creates a new X509 certificate for SSL/TLS.
1068

1069
    """
1070
    (validity, ) = params
1071
    return backend.CreateX509Certificate(validity)
1072

    
1073
  @staticmethod
1074
  def perspective_x509_cert_remove(params):
1075
    """Removes a X509 certificate.
1076

1077
    """
1078
    (name, ) = params
1079
    return backend.RemoveX509Certificate(name)
1080

    
1081
  # Import and export
1082

    
1083
  @staticmethod
1084
  def perspective_import_start(params):
1085
    """Starts an import daemon.
1086

1087
    """
1088
    (opts_s, instance, component, (dest, dest_args)) = params
1089

    
1090
    opts = objects.ImportExportOptions.FromDict(opts_s)
1091

    
1092
    return backend.StartImportExportDaemon(constants.IEM_IMPORT, opts,
1093
                                           None, None,
1094
                                           objects.Instance.FromDict(instance),
1095
                                           component, dest,
1096
                                           _DecodeImportExportIO(dest,
1097
                                                                 dest_args))
1098

    
1099
  @staticmethod
1100
  def perspective_export_start(params):
1101
    """Starts an export daemon.
1102

1103
    """
1104
    (opts_s, host, port, instance, component, (source, source_args)) = params
1105

    
1106
    opts = objects.ImportExportOptions.FromDict(opts_s)
1107

    
1108
    return backend.StartImportExportDaemon(constants.IEM_EXPORT, opts,
1109
                                           host, port,
1110
                                           objects.Instance.FromDict(instance),
1111
                                           component, source,
1112
                                           _DecodeImportExportIO(source,
1113
                                                                 source_args))
1114

    
1115
  @staticmethod
1116
  def perspective_impexp_status(params):
1117
    """Retrieves the status of an import or export daemon.
1118

1119
    """
1120
    return backend.GetImportExportStatus(params[0])
1121

    
1122
  @staticmethod
1123
  def perspective_impexp_abort(params):
1124
    """Aborts an import or export.
1125

1126
    """
1127
    return backend.AbortImportExport(params[0])
1128

    
1129
  @staticmethod
1130
  def perspective_impexp_cleanup(params):
1131
    """Cleans up after an import or export.
1132

1133
    """
1134
    return backend.CleanupImportExport(params[0])
1135

    
1136

    
1137
def CheckNoded(_, args):
1138
  """Initial checks whether to run or exit with a failure.
1139

1140
  """
1141
  if args: # noded doesn't take any arguments
1142
    print >> sys.stderr, ("Usage: %s [-f] [-d] [-p port] [-b ADDRESS]" %
1143
                          sys.argv[0])
1144
    sys.exit(constants.EXIT_FAILURE)
1145
  try:
1146
    codecs.lookup("string-escape")
1147
  except LookupError:
1148
    print >> sys.stderr, ("Can't load the string-escape code which is part"
1149
                          " of the Python installation. Is your installation"
1150
                          " complete/correct? Aborting.")
1151
    sys.exit(constants.EXIT_FAILURE)
1152

    
1153

    
1154
def PrepNoded(options, _):
1155
  """Preparation node daemon function, executed with the PID file held.
1156

1157
  """
1158
  if options.mlock:
1159
    request_executor_class = MlockallRequestExecutor
1160
    try:
1161
      utils.Mlockall()
1162
    except errors.NoCtypesError:
1163
      logging.warning("Cannot set memory lock, ctypes module not found")
1164
      request_executor_class = http.server.HttpServerRequestExecutor
1165
  else:
1166
    request_executor_class = http.server.HttpServerRequestExecutor
1167

    
1168
  # Read SSL certificate
1169
  if options.ssl:
1170
    ssl_params = http.HttpSslParams(ssl_key_path=options.ssl_key,
1171
                                    ssl_cert_path=options.ssl_cert)
1172
  else:
1173
    ssl_params = None
1174

    
1175
  err = _PrepareQueueLock()
1176
  if err is not None:
1177
    # this might be some kind of file-system/permission error; while
1178
    # this breaks the job queue functionality, we shouldn't prevent
1179
    # startup of the whole node daemon because of this
1180
    logging.critical("Can't init/verify the queue, proceeding anyway: %s", err)
1181

    
1182
  handler = NodeRequestHandler()
1183

    
1184
  mainloop = daemon.Mainloop()
1185
  server = \
1186
    http.server.HttpServer(mainloop, options.bind_address, options.port,
1187
                           handler, ssl_params=ssl_params, ssl_verify_peer=True,
1188
                           request_executor_class=request_executor_class)
1189
  server.Start()
1190

    
1191
  return (mainloop, server)
1192

    
1193

    
1194
def ExecNoded(options, args, prep_data): # pylint: disable=W0613
1195
  """Main node daemon function, executed with the PID file held.
1196

1197
  """
1198
  (mainloop, server) = prep_data
1199
  try:
1200
    mainloop.Run()
1201
  finally:
1202
    server.Stop()
1203

    
1204

    
1205
def Main():
1206
  """Main function for the node daemon.
1207

1208
  """
1209
  parser = OptionParser(description="Ganeti node daemon",
1210
                        usage="%prog [-f] [-d] [-p port] [-b ADDRESS]\
1211
                              \ [-i INTERFACE]",
1212
                        version="%%prog (ganeti) %s" %
1213
                        constants.RELEASE_VERSION)
1214
  parser.add_option("--no-mlock", dest="mlock",
1215
                    help="Do not mlock the node memory in ram",
1216
                    default=True, action="store_false")
1217

    
1218
  daemon.GenericMain(constants.NODED, parser, CheckNoded, PrepNoded, ExecNoded,
1219
                     default_ssl_cert=pathutils.NODED_CERT_FILE,
1220
                     default_ssl_key=pathutils.NODED_CERT_FILE,
1221
                     console_logging=True)