ganeti-noded 16.4 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"""

24
25
26
# functions in this module need to have a given name structure, so:
# pylint: disable-msg=C0103

Iustin Pop's avatar
Iustin Pop committed
27
28
29
30
import os
import sys
import resource
import traceback
Iustin Pop's avatar
Iustin Pop committed
31
32
import BaseHTTPServer
import simplejson
33
import errno
Iustin Pop's avatar
Iustin Pop committed
34
35
36
37
38
39
40
41
42
43

from optparse import OptionParser


from ganeti import backend
from ganeti import logger
from ganeti import constants
from ganeti import objects
from ganeti import errors
from ganeti import ssconf
44
from ganeti import utils
Iustin Pop's avatar
Iustin Pop committed
45
46


Iustin Pop's avatar
Iustin Pop committed
47
class ServerObject(BaseHTTPServer.BaseHTTPRequestHandler):
48
49
50
51
52
  """The server implementation.

  This class holds all methods exposed over the RPC interface.

  """
Iustin Pop's avatar
Iustin Pop committed
53
54
  def do_PUT(self):
    """Handle a post request.
Iustin Pop's avatar
Iustin Pop committed
55

56
    """
Iustin Pop's avatar
Iustin Pop committed
57
58
59
60
61
62
63
64
65
    path = self.path
    if path.startswith("/"):
      path = path[1:]
    mname = "perspective_%s" % path
    if not hasattr(self, mname):
      self.send_error(404)
      return False

    method = getattr(self, mname)
Iustin Pop's avatar
Iustin Pop committed
66
    try:
Iustin Pop's avatar
Iustin Pop committed
67
68
69
70
      body_length = int(self.headers.get('Content-Length', '0'))
    except ValueError:
      self.send_error(400, 'No Content-Length header or invalid format')
      return False
Iustin Pop's avatar
Iustin Pop committed
71

Iustin Pop's avatar
Iustin Pop committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    try:
      body = self.rfile.read(body_length)
    except socket.error, err:
      logger.Error("Socket error while reading: %s" % str(err))
      return
    try:
      params = simplejson.loads(body)
      result = method(params)
      payload = simplejson.dumps(result)
    except Exception, err:
      self.send_error(500, "Error: %s" % str(err))
      return False
    self.send_response(200)
    self.send_header('Content-Length', str(len(payload)))
    self.end_headers()
    self.wfile.write(payload)
    return True

  def log_message(self, format, *args):
    """Log a request to the log.

    This is the same as the parent, we just log somewhere else.

    """
    msg = ("%s - - [%s] %s\n" %
           (self.address_string(),
            self.log_date_time_string(),
            format % args))
    logger.Debug(msg)
Iustin Pop's avatar
Iustin Pop committed
101
102
103

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

104
105
106
107
108
  @staticmethod
  def perspective_blockdev_create(params):
    """Create a block device.

    """
109
    bdev_s, size, owner, on_primary, info = params
110
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
111
112
    if bdev is None:
      raise ValueError("can't unserialize data!")
113
    return backend.CreateBlockDevice(bdev, size, owner, on_primary, info)
Iustin Pop's avatar
Iustin Pop committed
114

115
116
117
118
119
  @staticmethod
  def perspective_blockdev_remove(params):
    """Remove a block device.

    """
Iustin Pop's avatar
Iustin Pop committed
120
    bdev_s = params[0]
121
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
122
123
    return backend.RemoveBlockDevice(bdev)

Iustin Pop's avatar
Iustin Pop committed
124
125
126
127
128
129
130
131
  @staticmethod
  def perspective_blockdev_rename(params):
    """Remove a block device.

    """
    devlist = [(objects.Disk.FromDict(ds), uid) for ds, uid in params]
    return backend.RenameBlockDevices(devlist)

132
133
134
135
136
  @staticmethod
  def perspective_blockdev_assemble(params):
    """Assemble a block device.

    """
137
    bdev_s, owner, on_primary = params
138
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
139
140
    if bdev is None:
      raise ValueError("can't unserialize data!")
141
    return backend.AssembleBlockDevice(bdev, owner, on_primary)
Iustin Pop's avatar
Iustin Pop committed
142

143
144
145
146
147
  @staticmethod
  def perspective_blockdev_shutdown(params):
    """Shutdown a block device.

    """
Iustin Pop's avatar
Iustin Pop committed
148
    bdev_s = params[0]
149
    bdev = objects.Disk.FromDict(bdev_s)
Iustin Pop's avatar
Iustin Pop committed
150
151
152
153
    if bdev is None:
      raise ValueError("can't unserialize data!")
    return backend.ShutdownBlockDevice(bdev)

154
  @staticmethod
155
  def perspective_blockdev_addchildren(params):
156
157
158
159
160
161
    """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
162
    bdev_s, ndev_s = params
163
    bdev = objects.Disk.FromDict(bdev_s)
164
165
    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
166
      raise ValueError("can't unserialize data!")
167
    return backend.MirrorAddChildren(bdev, ndevs)
Iustin Pop's avatar
Iustin Pop committed
168

169
  @staticmethod
170
  def perspective_blockdev_removechildren(params):
171
172
173
174
175
176
    """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
177
    bdev_s, ndev_s = params
178
    bdev = objects.Disk.FromDict(bdev_s)
179
180
    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
181
      raise ValueError("can't unserialize data!")
182
    return backend.MirrorRemoveChildren(bdev, ndevs)
Iustin Pop's avatar
Iustin Pop committed
183

184
185
186
187
188
  @staticmethod
  def perspective_blockdev_getmirrorstatus(params):
    """Return the mirror status for a list of disks.

    """
189
    disks = [objects.Disk.FromDict(dsk_s)
Iustin Pop's avatar
Iustin Pop committed
190
191
192
            for dsk_s in params]
    return backend.GetMirrorStatus(disks)

193
194
195
196
197
198
199
  @staticmethod
  def perspective_blockdev_find(params):
    """Expose the FindBlockDevice functionality for a disk.

    This will try to find but not activate a disk.

    """
200
    disk = objects.Disk.FromDict(params[0])
Iustin Pop's avatar
Iustin Pop committed
201
202
    return backend.FindBlockDevice(disk)

203
204
205
206
207
208
209
210
211
  @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.

    """
212
    cfbd = objects.Disk.FromDict(params[0])
Iustin Pop's avatar
Iustin Pop committed
213
214
215
216
    return backend.SnapshotBlockDevice(cfbd)

  # export/import  --------------------------

217
218
219
220
221
  @staticmethod
  def perspective_snapshot_export(params):
    """Export a given snapshot.

    """
222
    disk = objects.Disk.FromDict(params[0])
Iustin Pop's avatar
Iustin Pop committed
223
    dest_node = params[1]
224
    instance = objects.Instance.FromDict(params[2])
225
226
227
228
229
    return backend.ExportSnapshot(disk, dest_node, instance)

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

231
    """
232
233
    instance = objects.Instance.FromDict(params[0])
    snap_disks = [objects.Disk.FromDict(str_data)
Iustin Pop's avatar
Iustin Pop committed
234
235
236
                  for str_data in params[1]]
    return backend.FinalizeExport(instance, snap_disks)

237
238
239
240
241
242
243
244
245
246
  @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]
    einfo = backend.ExportInfo(path)
Iustin Pop's avatar
Iustin Pop committed
247
248
249
250
    if einfo is None:
      return einfo
    return einfo.Dumps()

251
252
253
254
255
256
257
258
259
  @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
260
261
    return backend.ListExports()

262
263
264
265
266
  @staticmethod
  def perspective_export_remove(params):
    """Remove an export.

    """
Iustin Pop's avatar
Iustin Pop committed
267
268
269
270
271
    export = params[0]
    return backend.RemoveExport(export)

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

272
273
274
275
276
  @staticmethod
  def perspective_volume_list(params):
    """Query the list of logical volumes in a given volume group.

    """
Iustin Pop's avatar
Iustin Pop committed
277
278
279
    vgname = params[0]
    return backend.GetVolumeList(vgname)

280
281
282
283
284
  @staticmethod
  def perspective_vg_list(params):
    """Query the list of volume groups.

    """
Iustin Pop's avatar
Iustin Pop committed
285
286
287
288
    return backend.ListVolumeGroups()

  # bridge  --------------------------

289
290
291
292
293
  @staticmethod
  def perspective_bridges_exist(params):
    """Check if all bridges given exist on this node.

    """
Iustin Pop's avatar
Iustin Pop committed
294
295
296
297
298
    bridges_list = params[0]
    return backend.BridgesExist(bridges_list)

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

299
300
301
302
303
  @staticmethod
  def perspective_instance_os_add(params):
    """Install an OS on a given instance.

    """
Iustin Pop's avatar
Iustin Pop committed
304
    inst_s, os_disk, swap_disk = params
305
    inst = objects.Instance.FromDict(inst_s)
Iustin Pop's avatar
Iustin Pop committed
306
307
    return backend.AddOSToInstance(inst, os_disk, swap_disk)

308
309
310
311
312
313
  @staticmethod
  def perspective_instance_run_rename(params):
    """Runs the OS rename script for an instance.

    """
    inst_s, old_name, os_disk, swap_disk = params
314
    inst = objects.Instance.FromDict(inst_s)
315
316
    return backend.RunRenameInstance(inst, old_name, os_disk, swap_disk)

317
318
319
320
321
  @staticmethod
  def perspective_instance_os_import(params):
    """Run the import function of an OS onto a given instance.

    """
Iustin Pop's avatar
Iustin Pop committed
322
    inst_s, os_disk, swap_disk, src_node, src_image = params
323
    inst = objects.Instance.FromDict(inst_s)
Iustin Pop's avatar
Iustin Pop committed
324
325
326
    return backend.ImportOSIntoInstance(inst, os_disk, swap_disk,
                                        src_node, src_image)

327
328
329
330
331
  @staticmethod
  def perspective_instance_shutdown(params):
    """Shutdown an instance.

    """
332
    instance = objects.Instance.FromDict(params[0])
Iustin Pop's avatar
Iustin Pop committed
333
334
    return backend.ShutdownInstance(instance)

335
336
337
338
339
  @staticmethod
  def perspective_instance_start(params):
    """Start an instance.

    """
340
    instance = objects.Instance.FromDict(params[0])
Iustin Pop's avatar
Iustin Pop committed
341
342
343
    extra_args = params[1]
    return backend.StartInstance(instance, extra_args)

344
345
346
347
348
349
350
351
352
353
  @staticmethod
  def perspective_instance_reboot(params):
    """Reboot an instance.

    """
    instance = objects.Instance.FromDict(params[0])
    reboot_type = params[1]
    extra_args = params[2]
    return backend.RebootInstance(instance, reboot_type, extra_args)

354
355
356
357
358
  @staticmethod
  def perspective_instance_info(params):
    """Query instance information.

    """
Iustin Pop's avatar
Iustin Pop committed
359
360
    return backend.GetInstanceInfo(params[0])

361
362
363
364
365
  @staticmethod
  def perspective_all_instances_info(params):
    """Query information about all instances.

    """
Iustin Pop's avatar
Iustin Pop committed
366
367
    return backend.GetAllInstancesInfo()

368
369
370
371
372
  @staticmethod
  def perspective_instance_list(params):
    """Query the list of running instances.

    """
Iustin Pop's avatar
Iustin Pop committed
373
374
375
376
    return backend.GetInstanceList()

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

377
378
379
380
381
  @staticmethod
  def perspective_node_tcp_ping(params):
    """Do a TcpPing on the remote node.

    """
382
383
    return utils.TcpPing(params[1], params[2], timeout=params[3],
                         live_port_needed=params[4], source=params[0])
384

385
386
387
388
389
  @staticmethod
  def perspective_node_info(params):
    """Query node information.

    """
Iustin Pop's avatar
Iustin Pop committed
390
391
392
    vgname = params[0]
    return backend.GetNodeInfo(vgname)

393
394
395
396
397
  @staticmethod
  def perspective_node_add(params):
    """Complete the registration of this node in the cluster.

    """
Iustin Pop's avatar
Iustin Pop committed
398
399
400
    return backend.AddNode(params[0], params[1], params[2],
                           params[3], params[4], params[5])

401
402
403
404
405
  @staticmethod
  def perspective_node_verify(params):
    """Run a verify sequence on this node.

    """
Iustin Pop's avatar
Iustin Pop committed
406
407
    return backend.VerifyNode(params[0])

408
409
410
411
412
  @staticmethod
  def perspective_node_start_master(params):
    """Promote this node to master status.

    """
Iustin Pop's avatar
Iustin Pop committed
413
414
    return backend.StartMaster()

415
416
417
418
419
  @staticmethod
  def perspective_node_stop_master(params):
    """Demote this node from master status.

    """
Iustin Pop's avatar
Iustin Pop committed
420
421
    return backend.StopMaster()

422
423
424
425
426
  @staticmethod
  def perspective_node_leave_cluster(params):
    """Cleanup after leaving a cluster.

    """
Iustin Pop's avatar
Iustin Pop committed
427
428
    return backend.LeaveCluster()

429
430
431
432
433
  @staticmethod
  def perspective_node_volumes(params):
    """Query the list of all logical volume groups.

    """
434
435
    return backend.NodeVolumes()

Iustin Pop's avatar
Iustin Pop committed
436
437
  # cluster --------------------------

438
439
440
441
442
  @staticmethod
  def perspective_version(params):
    """Query version information.

    """
Iustin Pop's avatar
Iustin Pop committed
443
444
    return constants.PROTOCOL_VERSION

445
446
447
448
449
450
451
452
  @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
453
454
455
456
457
    return backend.UploadFile(*params)


  # os -----------------------

458
459
460
461
462
  @staticmethod
  def perspective_os_diagnose(params):
    """Query detailed information about existing OSes.

    """
463
    return [os.ToDict() for os in backend.DiagnoseOS()]
Iustin Pop's avatar
Iustin Pop committed
464

465
466
467
468
469
  @staticmethod
  def perspective_os_get(params):
    """Query information about a given OS.

    """
Iustin Pop's avatar
Iustin Pop committed
470
471
    name = params[0]
    try:
472
      os_obj = backend.OSFromDisk(name)
Iustin Pop's avatar
Iustin Pop committed
473
    except errors.InvalidOS, err:
474
475
      os_obj = objects.OS.FromInvalidOS(err)
    return os_obj.ToDict()
Iustin Pop's avatar
Iustin Pop committed
476
477
478

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

479
480
481
482
483
  @staticmethod
  def perspective_hooks_runner(params):
    """Run hook scripts.

    """
Iustin Pop's avatar
Iustin Pop committed
484
485
486
487
    hpath, phase, env = params
    hr = backend.HooksRunner()
    return hr.RunHooks(hpath, phase, env)

488
489
490
491
492
493
494
495
496
497
  # test -----------------------

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

    """
    duration = params[0]
    return utils.TestDelay(duration)

Iustin Pop's avatar
Iustin Pop committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

def ParseOptions():
  """Parse the command line options.

  Returns:
    (options, args) as from OptionParser.parse_args()

  """
  parser = OptionParser(description="Ganeti node daemon",
                        usage="%prog [-f] [-d]",
                        version="%%prog (ganeti) %s" %
                        constants.RELEASE_VERSION)

  parser.add_option("-f", "--foreground", dest="fork",
                    help="Don't detach from the current terminal",
                    default=True, action="store_false")
  parser.add_option("-d", "--debug", dest="debug",
                    help="Enable some debug messages",
                    default=False, action="store_true")
  options, args = parser.parse_args()
  return options, args


def main():
522
523
524
  """Main function for the node daemon.

  """
Iustin Pop's avatar
Iustin Pop committed
525
  options, args = ParseOptions()
526
  utils.debug = options.debug
Iustin Pop's avatar
Iustin Pop committed
527
528
529
530
531
532
533
534
535
536
537
538
539
  for fname in (constants.SSL_CERT_FILE,):
    if not os.path.isfile(fname):
      print "config %s not there, will not run." % fname
      sys.exit(5)

  try:
    ss = ssconf.SimpleStore()
    port = ss.GetNodeDaemonPort()
    pwdata = ss.GetNodeDaemonPassword()
  except errors.ConfigurationError, err:
    print "Cluster configuration incomplete: '%s'" % str(err)
    sys.exit(5)

540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  # create /var/run/ganeti if not existing, in order to take care of
  # tmpfs /var/run
  if not os.path.exists(constants.BDEV_CACHE_DIR):
    try:
      os.mkdir(constants.BDEV_CACHE_DIR, 0755)
    except EnvironmentError, err:
      if err.errno != errno.EEXIST:
        print ("Node setup wrong, cannot create directory %s: %s" %
               (constants.BDEV_CACHE_DIR, err))
        sys.exit(5)
  if not os.path.isdir(constants.BDEV_CACHE_DIR):
    print ("Node setup wrong, %s is not a directory" %
           constants.BDEV_CACHE_DIR)
    sys.exit(5)

Iustin Pop's avatar
Iustin Pop committed
555
556
557
558
559
560
561
  # become a daemon
  if options.fork:
    createDaemon()

  logger.SetupLogging(twisted_workaround=True, debug=options.debug,
                      program="ganeti-noded")

Iustin Pop's avatar
Iustin Pop committed
562
563
  httpd = BaseHTTPServer.HTTPServer(('', port), ServerObject)
  httpd.serve_forever()
Iustin Pop's avatar
Iustin Pop committed
564
565
566
567
568


def createDaemon():
  """Detach a process from the controlling terminal and run it in the
  background as a daemon.
569

Iustin Pop's avatar
Iustin Pop committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  """
  UMASK = 077
  WORKDIR = "/"
  # Default maximum for the number of available file descriptors.
  if 'SC_OPEN_MAX' in os.sysconf_names:
    try:
      MAXFD = os.sysconf('SC_OPEN_MAX')
      if MAXFD < 0:
        MAXFD = 1024
    except OSError:
      MAXFD = 1024
  else:
    MAXFD = 1024
  # The standard I/O file descriptors are redirected to /dev/null by default.
  #REDIRECT_TO = getattr(os, "devnull", "/dev/null")
  REDIRECT_TO = constants.LOG_NODESERVER
  try:
    pid = os.fork()
  except OSError, e:
589
    raise Exception("%s [%d]" % (e.strerror, e.errno))
Michael Hanselmann's avatar
Michael Hanselmann committed
590
  if (pid == 0):  # The first child.
Iustin Pop's avatar
Iustin Pop committed
591
592
    os.setsid()
    try:
Michael Hanselmann's avatar
Michael Hanselmann committed
593
      pid = os.fork() # Fork a second child.
Iustin Pop's avatar
Iustin Pop committed
594
    except OSError, e:
595
      raise Exception("%s [%d]" % (e.strerror, e.errno))
Michael Hanselmann's avatar
Michael Hanselmann committed
596
    if (pid == 0):  # The second child.
Iustin Pop's avatar
Iustin Pop committed
597
598
599
600
      os.chdir(WORKDIR)
      os.umask(UMASK)
    else:
      # exit() or _exit()?  See below.
Michael Hanselmann's avatar
Michael Hanselmann committed
601
      os._exit(0) # Exit parent (the first child) of the second child.
Iustin Pop's avatar
Iustin Pop committed
602
  else:
Michael Hanselmann's avatar
Michael Hanselmann committed
603
    os._exit(0) # Exit parent of the first child.
Iustin Pop's avatar
Iustin Pop committed
604
605
606
607
608
609
610
611
  maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  if (maxfd == resource.RLIM_INFINITY):
    maxfd = MAXFD

  # Iterate through and close all file descriptors.
  for fd in range(0, maxfd):
    try:
      os.close(fd)
Michael Hanselmann's avatar
Michael Hanselmann committed
612
    except OSError: # ERROR, fd wasn't open to begin with (ignored)
Iustin Pop's avatar
Iustin Pop committed
613
      pass
614
  os.open(REDIRECT_TO, os.O_RDWR|os.O_CREAT|os.O_APPEND, 0600)
Iustin Pop's avatar
Iustin Pop committed
615
  # Duplicate standard input to standard output and standard error.
Michael Hanselmann's avatar
Michael Hanselmann committed
616
617
  os.dup2(0, 1)     # standard output (1)
  os.dup2(0, 2)     # standard error (2)
Iustin Pop's avatar
Iustin Pop committed
618
619
620
  return(0)


621
if __name__ == '__main__':
Iustin Pop's avatar
Iustin Pop committed
622
  main()