ganeti-noded 21.9 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python
#

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


"""Ganeti node daemon"""

Iustin Pop's avatar
Iustin Pop committed
24
25
26
27
28
29
30
# pylint: disable-msg=C0103,W0142

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

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

Iustin Pop's avatar
Iustin Pop committed
32
33
import os
import sys
Guido Trotter's avatar
Guido Trotter committed
34
import SocketServer
Iustin Pop's avatar
Iustin Pop committed
35
import logging
36
import signal
Iustin Pop's avatar
Iustin Pop committed
37
38
39
40
41
42
43

from optparse import OptionParser

from ganeti import backend
from ganeti import constants
from ganeti import objects
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 import storage
Iustin Pop's avatar
Iustin Pop committed
49

50
51
import ganeti.http.server

Iustin Pop's avatar
Iustin Pop committed
52

53
54
55
queue_lock = None


56
57
58
59
def _RequireJobQueueLock(fn):
  """Decorator for job queue manipulating functions.

  """
60
61
  QUEUE_LOCK_TIMEOUT = 10

62
63
  def wrapper(*args, **kwargs):
    # Locking in exclusive, blocking mode because there could be several
64
    # children running at the same time. Waiting up to 10 seconds.
65
    queue_lock.Exclusive(blocking=True, timeout=QUEUE_LOCK_TIMEOUT)
66
67
68
69
    try:
      return fn(*args, **kwargs)
    finally:
      queue_lock.Unlock()
70

71
72
73
  return wrapper


74
class NodeHttpServer(http.server.HttpServer):
75
76
77
78
79
  """The server implementation.

  This class holds all methods exposed over the RPC interface.

  """
80
81
82
  # too many public methods, and unused args - all methods get params
  # due to the API
  # pylint: disable-msg=R0904,W0613
83
  def __init__(self, *args, **kwargs):
84
    http.server.HttpServer.__init__(self, *args, **kwargs)
85
86
87
    self.noded_pid = os.getpid()

  def HandleRequest(self, req):
88
    """Handle a request.
Iustin Pop's avatar
Iustin Pop committed
89

90
    """
91
    if req.request_method.upper() != http.HTTP_PUT:
92
      raise http.HttpBadRequest()
93

94
    path = req.request_path
Iustin Pop's avatar
Iustin Pop committed
95
96
97
    if path.startswith("/"):
      path = path[1:]

98
99
    method = getattr(self, "perspective_%s" % path, None)
    if method is None:
100
      raise http.HttpNotFound()
Iustin Pop's avatar
Iustin Pop committed
101

Iustin Pop's avatar
Iustin Pop committed
102
    try:
103
      rvalue = method(req.request_body)
104
      return True, rvalue
105

106
107
108
109
110
    except backend.RPCFail, err:
      # our custom failure exception; str(err) works fine if the
      # exception was constructed with a single argument, and in
      # this case, err.message == err.args[0] == str(err)
      return (False, str(err))
111
    except errors.QuitGanetiException, err:
112
      # Tell parent to quit
113
114
      logging.info("Shutting down the node daemon, arguments: %s",
                   str(err.args))
115
      os.kill(self.noded_pid, signal.SIGTERM)
116
117
118
      # And return the error's arguments, which must be already in
      # correct tuple format
      return err.args
119
    except Exception, err:
120
      logging.exception("Error in RPC call")
121
      return False, "Error while executing backend function: %s" % str(err)
Iustin Pop's avatar
Iustin Pop committed
122
123
124

  # the new block devices  --------------------------

125
126
127
128
129
  @staticmethod
  def perspective_blockdev_create(params):
    """Create a block device.

    """
130
    bdev_s, size, owner, on_primary, info = params
131
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
132
133
    if bdev is None:
      raise ValueError("can't unserialize data!")
134
    return backend.BlockdevCreate(bdev, size, owner, on_primary, info)
Iustin Pop's avatar
Iustin Pop committed
135

136
137
138
139
140
  @staticmethod
  def perspective_blockdev_remove(params):
    """Remove a block device.

    """
Iustin Pop's avatar
Iustin Pop committed
141
    bdev_s = params[0]
142
    bdev = objects.Disk.FromDict(bdev_s)
143
    return backend.BlockdevRemove(bdev)
Iustin Pop's avatar
Iustin Pop committed
144

Iustin Pop's avatar
Iustin Pop committed
145
146
147
148
149
150
  @staticmethod
  def perspective_blockdev_rename(params):
    """Remove a block device.

    """
    devlist = [(objects.Disk.FromDict(ds), uid) for ds, uid in params]
151
    return backend.BlockdevRename(devlist)
Iustin Pop's avatar
Iustin Pop committed
152

153
154
155
156
157
  @staticmethod
  def perspective_blockdev_assemble(params):
    """Assemble a block device.

    """
158
    bdev_s, owner, on_primary = params
159
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
160
161
    if bdev is None:
      raise ValueError("can't unserialize data!")
162
    return backend.BlockdevAssemble(bdev, owner, on_primary)
Iustin Pop's avatar
Iustin Pop committed
163

164
165
166
167
168
  @staticmethod
  def perspective_blockdev_shutdown(params):
    """Shutdown a block device.

    """
Iustin Pop's avatar
Iustin Pop committed
169
    bdev_s = params[0]
170
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
171
172
    if bdev is None:
      raise ValueError("can't unserialize data!")
173
    return backend.BlockdevShutdown(bdev)
Iustin Pop's avatar
Iustin Pop committed
174

175
  @staticmethod
176
  def perspective_blockdev_addchildren(params):
177
178
179
180
181
182
    """Add a child to a mirror device.

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

    """
Iustin Pop's avatar
Iustin Pop committed
183
    bdev_s, ndev_s = params
184
    bdev = objects.Disk.FromDict(bdev_s)
185
186
    ndevs = [objects.Disk.FromDict(disk_s) for disk_s in ndev_s]
    if bdev is None or ndevs.count(None) > 0:
Iustin Pop's avatar
Iustin Pop committed
187
      raise ValueError("can't unserialize data!")
188
    return backend.BlockdevAddchildren(bdev, ndevs)
Iustin Pop's avatar
Iustin Pop committed
189

190
  @staticmethod
191
  def perspective_blockdev_removechildren(params):
192
193
194
195
196
197
    """Remove a child from a mirror device.

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

    """
Iustin Pop's avatar
Iustin Pop committed
198
    bdev_s, ndev_s = params
199
    bdev = objects.Disk.FromDict(bdev_s)
200
201
    ndevs = [objects.Disk.FromDict(disk_s) for disk_s in ndev_s]
    if bdev is None or ndevs.count(None) > 0:
Iustin Pop's avatar
Iustin Pop committed
202
      raise ValueError("can't unserialize data!")
203
    return backend.BlockdevRemovechildren(bdev, ndevs)
Iustin Pop's avatar
Iustin Pop committed
204

205
206
207
208
209
  @staticmethod
  def perspective_blockdev_getmirrorstatus(params):
    """Return the mirror status for a list of disks.

    """
210
    disks = [objects.Disk.FromDict(dsk_s)
211
212
213
             for dsk_s in params]
    return [status.ToDict()
            for status in backend.BlockdevGetmirrorstatus(disks)]
Iustin Pop's avatar
Iustin Pop committed
214

215
216
217
218
219
220
221
  @staticmethod
  def perspective_blockdev_find(params):
    """Expose the FindBlockDevice functionality for a disk.

    This will try to find but not activate a disk.

    """
222
    disk = objects.Disk.FromDict(params[0])
223
224
225
226
227
228

    result = backend.BlockdevFind(disk)
    if result is None:
      return None

    return result.ToDict()
Iustin Pop's avatar
Iustin Pop committed
229

230
231
232
233
234
235
236
237
238
  @staticmethod
  def perspective_blockdev_snapshot(params):
    """Create a snapshot device.

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

    """
239
    cfbd = objects.Disk.FromDict(params[0])
240
    return backend.BlockdevSnapshot(cfbd)
Iustin Pop's avatar
Iustin Pop committed
241

242
243
244
245
246
247
248
  @staticmethod
  def perspective_blockdev_grow(params):
    """Grow a stack of devices.

    """
    cfbd = objects.Disk.FromDict(params[0])
    amount = params[1]
249
    return backend.BlockdevGrow(cfbd, amount)
250

251
252
253
254
255
  @staticmethod
  def perspective_blockdev_close(params):
    """Closes the given block devices.

    """
256
    disks = [objects.Disk.FromDict(cf) for cf in params[1]]
257
    return backend.BlockdevClose(params[0], disks)
258

259
260
261
262
263
264
265
266
  @staticmethod
  def perspective_blockdev_getsize(params):
    """Compute the sizes of the given block devices.

    """
    disks = [objects.Disk.FromDict(cf) for cf in params[0]]
    return backend.BlockdevGetsize(disks)

267
268
269
270
271
272
273
274
275
  @staticmethod
  def perspective_blockdev_export(params):
    """Compute the sizes of the given block devices.

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

Iustin Pop's avatar
Iustin Pop committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  # blockdev/drbd specific methods ----------

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

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

    """
    nodes_ip, disks = params
    disks = [objects.Disk.FromDict(cf) for cf in disks]
    return backend.DrbdDisconnectNet(nodes_ip, disks)

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

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

    """
    nodes_ip, disks, instance_name, multimaster = params
    disks = [objects.Disk.FromDict(cf) for cf in disks]
300
301
    return backend.DrbdAttachNet(nodes_ip, disks,
                                     instance_name, multimaster)
Iustin Pop's avatar
Iustin Pop committed
302
303
304
305
306
307
308
309
310
311
312
313
314

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

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

    """
    nodes_ip, disks = params
    disks = [objects.Disk.FromDict(cf) for cf in disks]
    return backend.DrbdWaitSync(nodes_ip, disks)

Iustin Pop's avatar
Iustin Pop committed
315
316
  # export/import  --------------------------

317
318
319
320
321
  @staticmethod
  def perspective_snapshot_export(params):
    """Export a given snapshot.

    """
322
    disk = objects.Disk.FromDict(params[0])
Iustin Pop's avatar
Iustin Pop committed
323
    dest_node = params[1]
324
    instance = objects.Instance.FromDict(params[2])
325
    cluster_name = params[3]
326
327
328
    dev_idx = params[4]
    return backend.ExportSnapshot(disk, dest_node, instance,
                                  cluster_name, dev_idx)
329
330
331
332

  @staticmethod
  def perspective_finalize_export(params):
    """Expose the finalize export functionality.
Iustin Pop's avatar
Iustin Pop committed
333

334
    """
335
336
    instance = objects.Instance.FromDict(params[0])
    snap_disks = [objects.Disk.FromDict(str_data)
Iustin Pop's avatar
Iustin Pop committed
337
338
339
                  for str_data in params[1]]
    return backend.FinalizeExport(instance, snap_disks)

340
341
342
343
344
345
346
347
348
  @staticmethod
  def perspective_export_info(params):
    """Query information about an existing export on this node.

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

    """
    path = params[0]
349
    return backend.ExportInfo(path)
Iustin Pop's avatar
Iustin Pop committed
350

351
352
353
354
355
356
357
358
359
  @staticmethod
  def perspective_export_list(params):
    """List the available exports on this node.

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

    """
Iustin Pop's avatar
Iustin Pop committed
360
361
    return backend.ListExports()

362
363
364
365
366
  @staticmethod
  def perspective_export_remove(params):
    """Remove an export.

    """
Iustin Pop's avatar
Iustin Pop committed
367
368
369
370
371
    export = params[0]
    return backend.RemoveExport(export)

  # volume  --------------------------

372
  @staticmethod
373
  def perspective_lv_list(params):
374
375
376
    """Query the list of logical volumes in a given volume group.

    """
Iustin Pop's avatar
Iustin Pop committed
377
    vgname = params[0]
378
    return backend.GetVolumeList(vgname)
Iustin Pop's avatar
Iustin Pop committed
379

380
381
382
383
384
  @staticmethod
  def perspective_vg_list(params):
    """Query the list of volume groups.

    """
Iustin Pop's avatar
Iustin Pop committed
385
386
    return backend.ListVolumeGroups()

387
388
389
390
391
392
393
394
395
396
  # Storage --------------------------

  @staticmethod
  def perspective_storage_list(params):
    """Get list of storage units.

    """
    (su_name, su_args, name, fields) = params
    return storage.GetStorage(su_name, *su_args).List(name, fields)

397
398
399
400
401
402
403
404
  @staticmethod
  def perspective_storage_modify(params):
    """Modify a storage unit.

    """
    (su_name, su_args, name, changes) = params
    return storage.GetStorage(su_name, *su_args).Modify(name, changes)

405
406
407
408
409
410
411
412
  @staticmethod
  def perspective_storage_execute(params):
    """Execute an operation on a storage unit.

    """
    (su_name, su_args, name, op) = params
    return storage.GetStorage(su_name, *su_args).Execute(name, op)

Iustin Pop's avatar
Iustin Pop committed
413
414
  # bridge  --------------------------

415
416
417
418
419
  @staticmethod
  def perspective_bridges_exist(params):
    """Check if all bridges given exist on this node.

    """
Iustin Pop's avatar
Iustin Pop committed
420
421
422
423
424
    bridges_list = params[0]
    return backend.BridgesExist(bridges_list)

  # instance  --------------------------

425
426
427
428
429
  @staticmethod
  def perspective_instance_os_add(params):
    """Install an OS on a given instance.

    """
430
    inst_s = params[0]
431
    inst = objects.Instance.FromDict(inst_s)
432
433
    reinstall = params[1]
    return backend.InstanceOsAdd(inst, reinstall)
Iustin Pop's avatar
Iustin Pop committed
434

435
436
437
438
439
  @staticmethod
  def perspective_instance_run_rename(params):
    """Runs the OS rename script for an instance.

    """
440
    inst_s, old_name = params
441
    inst = objects.Instance.FromDict(inst_s)
442
    return backend.RunRenameInstance(inst, old_name)
443

444
445
446
447
448
  @staticmethod
  def perspective_instance_os_import(params):
    """Run the import function of an OS onto a given instance.

    """
449
    inst_s, src_node, src_images, cluster_name = params
450
    inst = objects.Instance.FromDict(inst_s)
451
452
    return backend.ImportOSIntoInstance(inst, src_node, src_images,
                                        cluster_name)
Iustin Pop's avatar
Iustin Pop committed
453

454
455
456
457
458
  @staticmethod
  def perspective_instance_shutdown(params):
    """Shutdown an instance.

    """
459
    instance = objects.Instance.FromDict(params[0])
460
461
    timeout = params[1]
    return backend.InstanceShutdown(instance, timeout)
Iustin Pop's avatar
Iustin Pop committed
462

463
464
465
466
467
  @staticmethod
  def perspective_instance_start(params):
    """Start an instance.

    """
468
    instance = objects.Instance.FromDict(params[0])
469
    return backend.StartInstance(instance)
Iustin Pop's avatar
Iustin Pop committed
470

471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  @staticmethod
  def perspective_migration_info(params):
    """Gather information about an instance to be migrated.

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

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

    """
    instance, info, target = params
    instance = objects.Instance.FromDict(instance)
    return backend.AcceptInstance(instance, info, target)

  @staticmethod
  def perspective_finalize_migration(params):
    """Finalize the instance migration.

    """
    instance, info, success = params
    instance = objects.Instance.FromDict(instance)
    return backend.FinalizeMigration(instance, info, success)

497
498
499
500
501
502
  @staticmethod
  def perspective_instance_migrate(params):
    """Migrates an instance.

    """
    instance, target, live = params
503
    instance = objects.Instance.FromDict(instance)
504
505
    return backend.MigrateInstance(instance, target, live)

506
507
508
509
510
511
512
  @staticmethod
  def perspective_instance_reboot(params):
    """Reboot an instance.

    """
    instance = objects.Instance.FromDict(params[0])
    reboot_type = params[1]
513
514
    shutdown_timeout = params[2]
    return backend.InstanceReboot(instance, reboot_type, shutdown_timeout)
515

516
517
518
519
520
  @staticmethod
  def perspective_instance_info(params):
    """Query instance information.

    """
Iustin Pop's avatar
Iustin Pop committed
521
    return backend.GetInstanceInfo(params[0], params[1])
Iustin Pop's avatar
Iustin Pop committed
522

523
524
525
526
527
528
529
530
  @staticmethod
  def perspective_instance_migratable(params):
    """Query whether the specified instance can be migrated.

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

531
532
533
534
535
  @staticmethod
  def perspective_all_instances_info(params):
    """Query information about all instances.

    """
536
    return backend.GetAllInstancesInfo(params[0])
Iustin Pop's avatar
Iustin Pop committed
537

538
539
540
541
542
  @staticmethod
  def perspective_instance_list(params):
    """Query the list of running instances.

    """
543
    return backend.GetInstanceList(params[0])
Iustin Pop's avatar
Iustin Pop committed
544
545
546

  # node --------------------------

547
548
549
550
551
  @staticmethod
  def perspective_node_tcp_ping(params):
    """Do a TcpPing on the remote node.

    """
552
553
    return utils.TcpPing(params[1], params[2], timeout=params[3],
                         live_port_needed=params[4], source=params[0])
554

555
556
557
558
559
  @staticmethod
  def perspective_node_has_ip_address(params):
    """Checks if a node has the given ip address.

    """
560
    return utils.OwnIpAddress(params[0])
561

562
563
564
565
566
  @staticmethod
  def perspective_node_info(params):
    """Query node information.

    """
567
568
    vgname, hypervisor_type = params
    return backend.GetNodeInfo(vgname, hypervisor_type)
Iustin Pop's avatar
Iustin Pop committed
569

570
571
572
573
574
  @staticmethod
  def perspective_node_add(params):
    """Complete the registration of this node in the cluster.

    """
Iustin Pop's avatar
Iustin Pop committed
575
576
577
    return backend.AddNode(params[0], params[1], params[2],
                           params[3], params[4], params[5])

578
579
580
581
582
  @staticmethod
  def perspective_node_verify(params):
    """Run a verify sequence on this node.

    """
583
    return backend.VerifyNode(params[0], params[1])
Iustin Pop's avatar
Iustin Pop committed
584

585
586
587
588
589
  @staticmethod
  def perspective_node_start_master(params):
    """Promote this node to master status.

    """
590
    return backend.StartMaster(params[0], params[1])
Iustin Pop's avatar
Iustin Pop committed
591

592
593
594
595
596
  @staticmethod
  def perspective_node_stop_master(params):
    """Demote this node from master status.

    """
597
    return backend.StopMaster(params[0])
Iustin Pop's avatar
Iustin Pop committed
598

599
600
601
602
603
  @staticmethod
  def perspective_node_leave_cluster(params):
    """Cleanup after leaving a cluster.

    """
604
    return backend.LeaveCluster(params[0])
Iustin Pop's avatar
Iustin Pop committed
605

606
607
608
609
610
  @staticmethod
  def perspective_node_volumes(params):
    """Query the list of all logical volume groups.

    """
611
612
    return backend.NodeVolumes()

613
614
615
616
617
618
619
620
  @staticmethod
  def perspective_node_demote_from_mc(params):
    """Demote a node from the master candidate role.

    """
    return backend.DemoteFromMC()


Iustin Pop's avatar
Iustin Pop committed
621
622
623
624
625
626
627
628
629
  @staticmethod
  def perspective_node_powercycle(params):
    """Tries to powercycle the nod.

    """
    hypervisor_type = params[0]
    return backend.PowercycleNode(hypervisor_type)


Iustin Pop's avatar
Iustin Pop committed
630
631
  # cluster --------------------------

632
633
634
635
636
  @staticmethod
  def perspective_version(params):
    """Query version information.

    """
637
    return constants.PROTOCOL_VERSION
Iustin Pop's avatar
Iustin Pop committed
638

639
640
641
642
643
644
645
646
  @staticmethod
  def perspective_upload_file(params):
    """Upload a file.

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

    """
Iustin Pop's avatar
Iustin Pop committed
647
648
    return backend.UploadFile(*params)

649
650
651
652
653
654
  @staticmethod
  def perspective_master_info(params):
    """Query master information.

    """
    return backend.GetMasterInfo()
Iustin Pop's avatar
Iustin Pop committed
655

656
657
658
659
660
  @staticmethod
  def perspective_write_ssconf_files(params):
    """Write ssconf files.

    """
661
662
    (values,) = params
    return backend.WriteSsconfFiles(values)
663

Iustin Pop's avatar
Iustin Pop committed
664
665
  # os -----------------------

666
667
668
669
670
  @staticmethod
  def perspective_os_diagnose(params):
    """Query detailed information about existing OSes.

    """
671
    return backend.DiagnoseOS()
Iustin Pop's avatar
Iustin Pop committed
672

673
674
675
676
677
  @staticmethod
  def perspective_os_get(params):
    """Query information about a given OS.

    """
Iustin Pop's avatar
Iustin Pop committed
678
    name = params[0]
679
    os_obj = backend.OSFromDisk(name)
680
    return os_obj.ToDict()
Iustin Pop's avatar
Iustin Pop committed
681
682
683

  # hooks -----------------------

684
685
686
687
688
  @staticmethod
  def perspective_hooks_runner(params):
    """Run hook scripts.

    """
Iustin Pop's avatar
Iustin Pop committed
689
690
691
692
    hpath, phase, env = params
    hr = backend.HooksRunner()
    return hr.RunHooks(hpath, phase, env)

693
694
695
696
697
698
699
700
701
702
703
  # iallocator -----------------

  @staticmethod
  def perspective_iallocator_runner(params):
    """Run an iallocator script.

    """
    name, idata = params
    iar = backend.IAllocatorRunner()
    return iar.Run(name, idata)

704
705
706
707
708
709
710
711
  # test -----------------------

  @staticmethod
  def perspective_test_delay(params):
    """Run test delay.

    """
    duration = params[0]
712
713
714
715
    status, rval = utils.TestDelay(duration)
    if not status:
      raise backend.RPCFail(rval)
    return rval
716

717
718
  # file storage ---------------

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  @staticmethod
  def perspective_file_storage_dir_create(params):
    """Create the file storage directory.

    """
    file_storage_dir = params[0]
    return backend.CreateFileStorageDir(file_storage_dir)

  @staticmethod
  def perspective_file_storage_dir_remove(params):
    """Remove the file storage directory.

    """
    file_storage_dir = params[0]
    return backend.RemoveFileStorageDir(file_storage_dir)

  @staticmethod
  def perspective_file_storage_dir_rename(params):
    """Rename the file storage directory.

    """
    old_file_storage_dir = params[0]
    new_file_storage_dir = params[1]
    return backend.RenameFileStorageDir(old_file_storage_dir,
                                        new_file_storage_dir)

745
746
  # jobs ------------------------

747
  @staticmethod
748
  @_RequireJobQueueLock
749
750
751
752
753
  def perspective_jobqueue_update(params):
    """Update job queue.

    """
    (file_name, content) = params
754
    return backend.JobQueueUpdate(file_name, content)
755
756

  @staticmethod
757
  @_RequireJobQueueLock
758
759
760
761
762
763
  def perspective_jobqueue_purge(params):
    """Purge job queue.

    """
    return backend.JobQueuePurge()

764
765
766
767
768
769
  @staticmethod
  @_RequireJobQueueLock
  def perspective_jobqueue_rename(params):
    """Rename a job queue file.

    """
770
    # TODO: What if a file fails to rename?
771
    return [backend.JobQueueRename(old, new) for old, new in params]
772

773
774
775
776
777
778
779
780
781
  @staticmethod
  def perspective_jobqueue_set_drain(params):
    """Set/unset the queue drain flag.

    """
    drain_flag = params[0]
    return backend.JobQueueSetDrainFlag(drain_flag)


782
783
784
785
786
787
788
789
790
791
  # hypervisor ---------------

  @staticmethod
  def perspective_hypervisor_validate_params(params):
    """Validate the hypervisor parameters.

    """
    (hvname, hvparams) = params
    return backend.ValidateHVParams(hvname, hvparams)

Iustin Pop's avatar
Iustin Pop committed
792

793
794
795
796
797
798
799
800
801
802
def CheckNoded(_, args):
  """Initial checks whether to run or exit with a failure.

  """
  if args: # noded doesn't take any arguments
    print >> sys.stderr, ("Usage: %s [-f] [-d] [-p port] [-b ADDRESS]" %
                          sys.argv[0])
    sys.exit(constants.EXIT_FAILURE)


803
def ExecNoded(options, _):
Michael Hanselmann's avatar
Michael Hanselmann committed
804
  """Main node daemon function, executed with the PID file held.
805
806

  """
Iustin Pop's avatar
Iustin Pop committed
807
  global queue_lock # pylint: disable-msg=W0603
808

809
  # Read SSL certificate
810
811
812
813
814
  if options.ssl:
    ssl_params = http.HttpSslParams(ssl_key_path=options.ssl_key,
                                    ssl_cert_path=options.ssl_cert)
  else:
    ssl_params = None
Iustin Pop's avatar
Iustin Pop committed
815

816
817
  # Prepare job queue
  queue_lock = jstore.InitAndVerifyQueue(must_lock=False)
Iustin Pop's avatar
Iustin Pop committed
818

819
820
821
822
823
824
825
826
  mainloop = daemon.Mainloop()
  server = NodeHttpServer(mainloop, options.bind_address, options.port,
                          ssl_params=ssl_params, ssl_verify_peer=True)
  server.Start()
  try:
    mainloop.Run()
  finally:
    server.Stop()
Iustin Pop's avatar
Iustin Pop committed
827
828


829
830
831
832
833
834
835
836
def main():
  """Main function for the node daemon.

  """
  parser = OptionParser(description="Ganeti node daemon",
                        usage="%prog [-f] [-d] [-p port] [-b ADDRESS]",
                        version="%%prog (ganeti) %s" %
                        constants.RELEASE_VERSION)
837
838
839
  dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS]
  dirs.append((constants.LOG_OS_DIR, 0750))
  dirs.append((constants.LOCK_DIR, 1777))
840
  daemon.GenericMain(constants.NODED, parser, dirs, CheckNoded, ExecNoded)
841

Iustin Pop's avatar
Iustin Pop committed
842

843
if __name__ == '__main__':
Iustin Pop's avatar
Iustin Pop committed
844
  main()