ctdb-tools: Add logging config options to config tool
[samba.git] / ctdb / wscript
old mode 100755 (executable)
new mode 100644 (file)
index 12779bc..0546cbe
@@ -30,23 +30,38 @@ default_prefix = Options.default_prefix = '/usr/local'
 
 samba_dist.DIST_DIRS('''ctdb:. lib/replace:lib/replace lib/talloc:lib/talloc
                         lib/tevent:lib/tevent lib/tdb:lib/tdb
-                        lib/socket_wrapper:lib/socket_wrapper
+                        third_party/socket_wrapper:third_party/socket_wrapper
                         third_party/popt:third_party/popt
                         lib/util:lib/util lib/tdb_wrap:lib/tdb_wrap
                         lib/ccan:lib/ccan libcli/util:libcli/util
+                        lib/async_req:lib/async_req
                         buildtools:buildtools third_party/waf:third_party/waf''')
 
-manpages = [
+manpages_binary = [
     'ctdb.1',
-    'ctdb.7',
     'ctdbd.1',
-    'ctdbd.conf.5',
+    'ltdbtool.1',
+    'ping_pong.1'
+]
+
+manpages_misc = [
+    'ctdb_diagnostics.1',
     'ctdbd_wrapper.1',
+    'onnode.1',
+    'ctdb-script.options.5',
+    'ctdb.sysconfig.5',
+    'ctdbd.conf.5',
+    'ctdb.7',
     'ctdb-statistics.7',
     'ctdb-tunables.7',
-    'ltdbtool.1',
-    'onnode.1',
-    'ping_pong.1'
+]
+
+manpages_etcd = [
+    'ctdb-etcd.7',
+]
+
+manpages_ceph = [
+    'ctdb_mutex_ceph_rados_helper.7',
 ]
 
 
@@ -67,6 +82,12 @@ def set_options(opt):
     opt.add_option('--enable-pmda',
                    help=("Turn on PCP pmda support (default=no)"),
                    action="store_true", dest='ctdb_pmda', default=False)
+    opt.add_option('--enable-etcd-reclock',
+                   help=("Enable etcd recovery lock helper (default=no)"),
+                   action="store_true", dest='ctdb_etcd_reclock', default=False)
+    opt.add_option('--enable-ceph-reclock',
+                   help=("Enable Ceph CTDB recovery lock helper (default=no)"),
+                   action="store_true", dest='ctdb_ceph_reclock', default=False)
 
     opt.add_option('--with-logdir',
                    help=("Path to log directory"),
@@ -78,9 +99,6 @@ def set_options(opt):
 
 def configure(conf):
 
-    # CTDB relies on nested event loops
-    conf.env.TEVENT_DEPRECATED = 1
-
     # No need to build python bindings for talloc/tevent/tdb
     if conf.IN_LAUNCH_DIR():
         conf.env.standalone_ctdb = True
@@ -91,24 +109,34 @@ def configure(conf):
     if conf.env.standalone_ctdb:
         conf.SAMBA_CHECK_PERL(mandatory=True)
 
-        conf.SAMBA_CHECK_PYTHON(mandatory=True, version=(2,5,0))
-        conf.SAMBA_CHECK_PYTHON_HEADERS(mandatory=True)
+        conf.SAMBA_CHECK_PYTHON(mandatory=False, version=(2,5,0))
+        conf.SAMBA_CHECK_PYTHON_HEADERS(mandatory=False)
 
     if conf.CHECK_FOR_THIRD_PARTY():
         conf.RECURSE('third_party/popt')
+        if conf.env.standalone_ctdb or conf.CONFIG_GET('ENABLE_SELFTEST'):
+            conf.RECURSE('third_party/socket_wrapper')
+            conf.env.SOCKET_WRAPPER_SO_PATH = conf.CONFIG_GET('LIBSOCKET_WRAPPER_SO_PATH')
     else:
         if not conf.CHECK_POPT():
             raise Utils.WafError('popt development packages have not been found\nIf third_party is installed, check that it is in the proper place.')
         else:
             conf.define('USING_SYSTEM_POPT', 1)
+        conf.env.SOCKET_WRAPPER_SO_PATH = ''
+
+
+        if conf.env.standalone_ctdb or conf.CONFIG_GET('ENABLE_SELFTEST'):
+            if not conf.CHECK_SOCKET_WRAPPER():
+                raise Utils.WafError('socket_wrapper package has not been found.\nIf third_party is installed, check that it is in the proper place.')
+            else:
+                conf.define('USING_SYSTEM_SOCKET_WRAPPER', 1)
+                conf.env.SOCKET_WRAPPER_SO_PATH = conf.CONFIG_GET('LIBSOCKET_WRAPPER_SO_PATH')
 
     conf.RECURSE('lib/util')
 
     conf.RECURSE('lib/talloc')
     conf.RECURSE('lib/tevent')
     conf.RECURSE('lib/tdb')
-    if conf.env.standalone_ctdb or conf.CONFIG_GET('ENABLE_SELFTEST'):
-        conf.RECURSE('lib/socket_wrapper')
 
     conf.CHECK_HEADERS('sched.h')
     conf.CHECK_HEADERS('procinfo.h')
@@ -149,9 +177,11 @@ def configure(conf):
         if not conf.CHECK_FUNCS_IN('pmdaDaemon', 'pcp_pmda'):
             pmda_support = False
         if pmda_support:
+            conf.CHECK_TYPE_IN('__pmID_int', 'pcp/pmapi.h pcp/impl.h')
             have_pmda = True
         else:
             Logs.error("PMDA support not available")
+            sys.exit(1)
     if have_pmda:
         Logs.info('Building with PMDA support')
         conf.define('HAVE_PMDA', 1)
@@ -172,15 +202,38 @@ def configure(conf):
             have_infiniband = True
         else:
             Logs.error("Infiniband support not available")
+            sys.exit(1)
     if have_infiniband:
         Logs.info('Building with Infiniband support')
         conf.define('HAVE_INFINIBAND', 1)
         conf.define('USE_INFINIBAND', 1)
 
+    have_etcd_reclock = False
+    if Options.options.ctdb_etcd_reclock:
+        try:
+            conf.check_python_module('etcd')
+            have_etcd_reclock = True
+        except:
+            Logs.error('etcd support not available')
+            sys.exit(1)
+    if have_etcd_reclock:
+        Logs.info('Building with etcd support')
+    conf.env.etcd_reclock = have_etcd_reclock
+
+    if Options.options.ctdb_ceph_reclock:
+        if (conf.CHECK_HEADERS('rados/librados.h', False, False, 'rados') and
+                                       conf.CHECK_LIB('rados', shlib=True)):
+            Logs.info('Building with Ceph librados recovery lock support')
+            conf.define('HAVE_LIBRADOS', 1)
+        else:
+            Logs.error("Missing librados for Ceph recovery lock support")
+            sys.exit(1)
+
     conf.env.CTDB_BINDIR = os.path.join(conf.env.EXEC_PREFIX, 'bin')
     conf.env.CTDB_ETCDIR = os.path.join(conf.env.SYSCONFDIR, 'ctdb')
     conf.env.CTDB_VARDIR = os.path.join(conf.env.LOCALSTATEDIR, 'lib/ctdb')
     conf.env.CTDB_RUNDIR = os.path.join(conf.env.LOCALSTATEDIR, 'run/ctdb')
+    conf.env.CTDB_HELPER_BINDIR = os.path.join(conf.env.LIBEXECDIR, 'ctdb')
 
     if Options.options.ctdb_logdir:
         conf.env.CTDB_LOGDIR = Options.options.ctdb_logdir
@@ -192,37 +245,34 @@ def configure(conf):
     else:
         conf.env.CTDB_SOCKPATH = os.path.join(conf.env.CTDB_RUNDIR,
                                               'ctdbd.socket')
+    conf.define('CTDB_SOCKET', conf.env.CTDB_SOCKPATH)
 
     conf.ADD_CFLAGS('''-DCTDB_HELPER_BINDIR=\"%s\"
                        -DLOGDIR=\"%s\"
-                       -DSOCKPATH=\"%s\"
                        -DCTDB_ETCDIR=\"%s\"
                        -DCTDB_VARDIR=\"%s\"
                        -DCTDB_RUNDIR=\"%s\"''' % (
-                    conf.env.CTDB_BINDIR,
+                    conf.env.CTDB_HELPER_BINDIR,
                     conf.env.CTDB_LOGDIR,
-                    conf.env.CTDB_SOCKPATH,
                     conf.env.CTDB_ETCDIR,
                     conf.env.CTDB_VARDIR,
                     conf.env.CTDB_RUNDIR))
 
     conf.env.CTDB_TEST_DATADIR = os.path.join(conf.env.EXEC_PREFIX,
-                                              'share/ctdb-tests')
-    conf.env.CTDB_TEST_LIBDIR = os.path.join(conf.env.LIBDIR, 'ctdb-tests')
+                                              'share/ctdb/tests')
+    conf.env.CTDB_TEST_LIBEXECDIR = os.path.join(conf.env.LIBEXECDIR, 'ctdb/tests')
 
     # Allow unified compilation and separate compilation of utilities
     # to find includes
     if not conf.env.standalone_ctdb:
-        conf.ADD_EXTRA_INCLUDES('#include/public #ctdb/include')
+        conf.ADD_EXTRA_INCLUDES('#include/public #ctdb/include #ctdb')
     else:
         if srcdir == '.':
             # Building from tarball
             conf.ADD_EXTRA_INCLUDES('#include')
-            conf.ADD_EXTRA_INCLUDES('#include/internal')
         else:
             # Building standalone CTDB from within Samba tree
             conf.ADD_EXTRA_INCLUDES('#ctdb/include')
-            conf.ADD_EXTRA_INCLUDES('#ctdb/include/internal')
             conf.ADD_EXTRA_INCLUDES('#ctdb')
         conf.ADD_EXTRA_INCLUDES('#lib #lib/replace')
 
@@ -230,53 +280,66 @@ def configure(conf):
         conf.DEFINE('SAMBA_UTIL_CORE_ONLY', 1, add_to_cflags=True)
         conf.SAMBA_CONFIG_H()
 
+    if 'XSLTPROC_MANPAGES' in conf.env and conf.env['XSLTPROC_MANPAGES']:
+        conf.env.ctdb_generate_manpages = True
+    else:
+        conf.env.ctdb_generate_manpages = False
+
+        Logs.info("xsltproc unavailable, checking for pre-built manpages")
+        conf.env.ctdb_prebuilt_manpages = []
+        manpages = manpages_binary + manpages_misc
+        if conf.env.etcd_reclock:
+            manpages += manpages_etcd
+        if conf.env.HAVE_LIBRADOS:
+            manpages += manpages_ceph
+        for m in manpages:
+            if os.path.exists(os.path.join("doc", m)):
+                Logs.info("  %s: yes" % (m))
+                conf.env.ctdb_prebuilt_manpages.append(m)
+            else:
+                Logs.info("  %s: no" % (m))
+
+def gen_ctdb_version(task):
+    fp = file(task.outputs[0].bldpath(task.env), 'w')
+    fp.write('/* This file is auto-generated from waf */\n')
+    fp.write('#include "version.h"\n')
+    fp.write('\n')
+    fp.write('#define CTDB_VERSION_STRING "%s"\n' % VERSION)
+    fp.close()
+
 
 def build(bld):
     if bld.env.standalone_ctdb:
         # enable building of public headers in the build tree
         bld.env.build_public_headers = 'include/public'
 
-    version_h = samba_utils.os_path_relpath(os.path.join(Options.launch_dir,
-                                                         "version.h"),
-                                            bld.curdir)
-
     if bld.env.standalone_ctdb:
-        ctdb_mkversion = '../packaging/mkversion.sh'
-        t = bld.SAMBA_GENERATOR('ctdb-version-header',
-                                target='include/ctdb_version.h',
-                                rule='%s ${TGT} %s' % (ctdb_mkversion, VERSION),
-                                dep_vars=['VERSION'])
-        t.env.VERSION = VERSION
-
-        t = bld.SAMBA_GENERATOR('ctdb-samba-version-header',
-                                target=version_h,
-                                rule='printf "#include \\"ctdb_version.h\\" \\n#define SAMBA_VERSION_STRING CTDB_VERSION_STRING\\n" > ${TGT}',
-                                dep_vars=['VERSION'])
-        t.env.VERSION = VERSION
-    else:
-        t = bld.SAMBA_GENERATOR('ctdb-samba-version-header',
-                                target='include/ctdb_version.h',
-                                rule='printf "#include \\"%s\\" \\n#define CTDB_VERSION_STRING SAMBA_VERSION_STRING\\n" > ${TGT}' % version_h,
-                                dep_vars=['VERSION'])
-        t.env.VERSION = VERSION
+        bld.SAMBA_MKVERSION('version.h', '%s/VERSION' % vdir)
+
+    t = bld.SAMBA_GENERATOR('ctdb-version-header',
+                            target='include/ctdb_version.h',
+                            rule=gen_ctdb_version,
+                            dep_vars=['VERSION'])
+    t.env.VERSION = VERSION
 
     bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'
 
     bld.RECURSE('lib/replace')
     if bld.CHECK_FOR_THIRD_PARTY():
         bld.RECURSE('third_party/popt')
+        if bld.env.standalone_ctdb or bld.CONFIG_GET('SOCKET_WRAPPER'):
+            bld.RECURSE('third_party/socket_wrapper')
 
     bld.RECURSE('lib/tdb_wrap')
     bld.RECURSE('lib/util')
+    bld.RECURSE('lib/async_req')
 
     bld.RECURSE('lib/talloc')
     bld.RECURSE('lib/tevent')
     bld.RECURSE('lib/tdb')
-    if bld.env.standalone_ctdb or bld.CONFIG_GET('SOCKET_WRAPPER'):
-        bld.RECURSE('lib/socket_wrapper')
 
     if bld.env.standalone_ctdb:
-        # In a combined build is implemented, CTDB will wanted to
+        # If a combined build is implemented, CTDB will want to
         # build against samba-util rather than samba-util-core.
         # Similarly, other Samba subsystems expect samba-util.  So,
         # for a standalone build, just define a fake samba-util
@@ -288,7 +351,7 @@ def build(bld):
     bld.SAMBA_SUBSYSTEM('ctdb-tcp',
                         source=bld.SUBDIR('tcp',
                                           'tcp_connect.c tcp_init.c tcp_io.c'),
-                        includes='include include/internal',
+                        includes='include',
                         deps='replace tdb talloc tevent')
 
     ib_deps = ''
@@ -297,8 +360,8 @@ def build(bld):
                             source=bld.SUBDIR('ib',
                                               '''ibwrapper.c ibw_ctdb.c
                                                  ibw_ctdb_init.c'''),
-                            includes='include include/internal',
-                            deps='replace')
+                            includes='include',
+                            deps='replace talloc tevent tdb')
         ib_deps = ' ctdb-ib rdmacm ibverbs'
 
     if sys.platform.startswith('linux'):
@@ -307,7 +370,7 @@ def build(bld):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_aix.c')
     elif sys.platform.startswith('freebsd'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_freebsd.c')
-    elif sys.platform == 'kfreebsd':
+    elif sys.platform.startswith('gnukfreebsd'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_kfreebsd.c')
     elif sys.platform == 'gnu':
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_gnu.c')
@@ -315,37 +378,101 @@ def build(bld):
         Logs.error("Platform %s not supported" % sys.platform)
 
     bld.SAMBA_SUBSYSTEM('ctdb-system',
-                        source='common/system_common.c ' + CTDB_SYSTEM_SRC,
-                        includes='include include/internal',
-                        deps='replace talloc tevent tdb pcap')
+                        source=bld.SUBDIR('common',
+                                          'system_common.c system_util.c') +
+                               CTDB_SYSTEM_SRC,
+                        includes='include',
+                        deps='replace talloc tevent tdb pcap samba-util')
 
     bld.SAMBA_SUBSYSTEM('ctdb-common',
                         source=bld.SUBDIR('common',
                                           '''ctdb_io.c ctdb_util.c ctdb_ltdb.c
-                                             cmdline.c rb_tree.c
-                                             ctdb_fork.c'''),
-                        includes='include include/internal common .',
-                        deps='replace popt talloc tevent tdb popt ctdb-system')
-
-    bld.SAMBA_SUBSYSTEM('ctdb-common-util',
-                        source=bld.SUBDIR('common',
-                                          'system_util.c ctdb_logging.c'),
-                        includes='include include/internal',
-                        deps='replace tevent tdb')
+                                             sock_io.c'''),
+                        includes='include',
+                        deps='''replace popt talloc tevent tdb popt ctdb-system
+                                ctdb-protocol-util''')
 
     bld.SAMBA_SUBSYSTEM('ctdb-util',
                         source=bld.SUBDIR('common',
-                                          '''db_hash.c srvid.c reqid.c'''),
-                        deps='replace talloc tevent tdb')
+                                          '''db_hash.c srvid.c reqid.c
+                                             pkt_read.c pkt_write.c comm.c
+                                             logging.c rb_tree.c tunable.c
+                                             pidfile.c run_proc.c
+                                             hash_count.c run_event.c
+                                             sock_client.c version.c
+                                             cmdline.c path.c conf.c
+                                          '''),
+                        deps='''samba-util sys_rw tevent-util
+                                replace talloc tevent tdb popt''')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-logging-conf',
+                        source='common/logging_conf.c',
+                        deps='ctdb-util talloc')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-protocol',
+                        source=bld.SUBDIR('protocol',
+                                          '''protocol_header.c protocol_packet.c
+                                             protocol_basic.c protocol_types.c
+                                             protocol_call.c
+                                             protocol_message.c
+                                             protocol_control.c
+                                             protocol_keepalive.c
+                                             protocol_tunnel.c
+                                             protocol_client.c
+                                             protocol_debug.c
+                                             protocol_event.c
+                                             protocol_sock.c'''),
+                        includes='include',
+                        deps='replace talloc tdb')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-protocol-util',
+                        source='protocol/protocol_util.c',
+                        deps='replace talloc tdb')
 
     bld.SAMBA_SUBSYSTEM('ctdb-client',
-                        source=bld.SUBDIR('client', 'ctdb_client.c'),
-                        includes='include include/internal',
-                        deps='''replace popt talloc tevent tdb
-                                samba-util tdb-wrap ctdb-util''')
+                        source=bld.SUBDIR('client',
+                                          '''client_connect.c client_call.c
+                                             client_message.c client_control.c
+                                             client_message_sync.c
+                                             client_control_sync.c
+                                             client_db.c client_util.c
+                                             client_event.c client_tunnel.c
+                                          '''),
+                        includes='include',
+                        deps='replace talloc tevent tdb tdb-wrap')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-server-util',
+                        source=bld.SUBDIR('common',
+                                          '''sock_daemon.c'''),
+                        deps='''samba-util ctdb-util tevent-util
+                                LIBASYNC_REQ replace talloc tevent''')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-ipalloc',
+                        source=bld.SUBDIR('server',
+                                          '''ipalloc_deterministic.c
+                                             ipalloc_nondeterministic.c
+                                             ipalloc_lcp2.c
+                                             ipalloc_common.c
+                                             ipalloc.c
+                                          '''),
+                        includes='include',
+                        deps='ctdb-protocol-util replace talloc tevent')
+
+    bld.SAMBA_BINARY('ctdb-path',
+                     source='common/path_tool.c',
+                     cflags='-DCTDB_PATH_TOOL',
+                     deps='''ctdb-util samba-util talloc replace popt''',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
+    bld.SAMBA_BINARY('ctdb-config',
+                     source='common/conf_tool.c',
+                     cflags='-DCTDB_CONF_TOOL',
+                     deps='''ctdb-logging-conf
+                             ctdb-util samba-util talloc replace popt''',
+                     install_path='${CTDB_HELPER_BINDIR}')
 
-    bld.SAMBA_SUBSYSTEM('ctdb-server',
-                        source='server/ctdbd.c ' +
+    bld.SAMBA_BINARY('ctdbd',
+                     source='server/ctdbd.c ' +
                                bld.SUBDIR('server',
                                           '''ctdb_daemon.c ctdb_recoverd.c
                                              ctdb_recover.c ctdb_freeze.c
@@ -353,35 +480,43 @@ def build(bld):
                                              ctdb_server.c ctdb_control.c
                                              ctdb_call.c ctdb_ltdb_server.c
                                              ctdb_traverse.c eventscript.c
-                                             ctdb_takeover.c ctdb_serverids.c
+                                             ctdb_takeover.c
                                              ctdb_persistent.c ctdb_keepalive.c
+                                             ctdb_cluster_mutex.c
                                              ctdb_logging.c
-                                             ctdb_logging_syslog.c
-                                             ctdb_logging_file.c
                                              ctdb_uptime.c
                                              ctdb_vacuum.c ctdb_banning.c
                                              ctdb_statistics.c
                                              ctdb_update_record.c
-                                             ctdb_lock.c'''),
-                        includes='include include/internal',
-                        deps='replace popt talloc tevent tdb talloc_report')
-
-    bld.SAMBA_BINARY('ctdbd',
-                     source='',
-                     deps='''ctdb-server ctdb-client ctdb-common
-                             ctdb-common-util ctdb-tcp ctdb-util''' +
+                                             ctdb_lock.c ctdb_fork.c
+                                             ctdb_tunnel.c ctdb_client.c'''),
+                     includes='include',
+                     deps='''ctdb-common ctdb-system ctdb-protocol
+                             ctdb-tcp ctdb-util replace sys_rw popt
+                             talloc tevent tdb-wrap tdb talloc_report''' +
                           ib_deps,
                      install_path='${SBINDIR}',
                      manpages='ctdbd.1')
 
     bld.SAMBA_BINARY('ctdb',
-                     source='tools/ctdb.c tools/ctdb_vacuum.c',
-                     deps='''ctdb-client ctdb-common ctdb-common-util
-                             ctdb-util''',
-                     includes='include include/internal',
+                     source='tools/ctdb.c',
+                     deps='''ctdb-client ctdb-protocol ctdb-protocol-util
+                             ctdb-util ctdb-system samba-util sys_rw popt''',
                      install_path='${BINDIR}',
                      manpages='ctdb.1')
 
+    bld.SAMBA_BINARY('ctdb_killtcp',
+                     source='tools/ctdb_killtcp.c',
+                     deps='''ctdb-protocol-util ctdb-util ctdb-system
+                             samba-util replace''',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
+    bld.SAMBA_BINARY('ctdb_event',
+                     source='tools/ctdb_event.c',
+                     deps='''ctdb-client ctdb-protocol ctdb-protocol-util
+                             ctdb-util ctdb-system samba-util replace''',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
     bld.SAMBA_BINARY('ltdbtool',
                      source='tools/ltdbtool.c',
                      includes='include',
@@ -389,17 +524,36 @@ def build(bld):
                      install_path='${BINDIR}',
                      manpages='ltdbtool.1')
 
+    bld.SAMBA_BINARY('ctdb_eventd',
+                     source='server/ctdb_eventd.c',
+                     deps='''ctdb-server-util ctdb-protocol ctdb-protocol-util
+                             ctdb-util samba-util replace popt''',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
     bld.SAMBA_BINARY('ctdb_lock_helper',
                      source='server/ctdb_lock_helper.c',
-                     deps='samba-util ctdb-common-util talloc tdb',
-                     includes='include include/internal',
-                     install_path='${BINDIR}')
-
-    bld.SAMBA_BINARY('ctdb_event_helper',
-                     source='server/ctdb_event_helper.c',
-                     includes='include include/internal',
-                     deps='samba-util ctdb-common-util replace tdb',
-                     install_path='${BINDIR}')
+                     deps='''samba-util sys_rw ctdb-system tevent-util
+                             talloc tevent tdb''',
+                     includes='include',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
+    bld.SAMBA_BINARY('ctdb_recovery_helper',
+                     source='server/ctdb_recovery_helper.c',
+                     deps='''ctdb-client ctdb-protocol ctdb-util
+                             samba-util sys_rw replace tdb''',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
+    bld.SAMBA_BINARY('ctdb_takeover_helper',
+                     source='server/ctdb_takeover_helper.c',
+                     deps='''ctdb-client ctdb-protocol ctdb-util
+                             samba-util sys_rw replace ctdb-ipalloc popt''',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
+    bld.SAMBA_BINARY('ctdb_mutex_fcntl_helper',
+                     source='server/ctdb_mutex_fcntl_helper.c',
+                     deps='sys_rw ctdb-system',
+                     includes='include',
+                     install_path='${CTDB_HELPER_BINDIR}')
 
     bld.SAMBA_GENERATOR('ctdb-smnotify-h',
                         source='utils/smnotify/smnotify.x',
@@ -421,9 +575,9 @@ def build(bld):
     bld.SAMBA_BINARY('smnotify',
                      source=bld.SUBDIR('utils/smnotify',
                                        'smnotify.c gen_smnotify.c gen_xdr.c'),
-                     deps='ctdb-smnotify-h ctdb-smnotify-c ctdb-smnotify-x popt',
+                     deps='ctdb-smnotify-h ctdb-smnotify-c ctdb-smnotify-x popt tirpc',
                      includes='utils utils/smnotify',
-                     install_path='${BINDIR}')
+                     install_path='${CTDB_HELPER_BINDIR}')
 
     bld.SAMBA_BINARY('ping_pong',
                      source='utils/ping_pong/ping_pong.c',
@@ -434,9 +588,9 @@ def build(bld):
     if bld.env.HAVE_PMDA:
         bld.SAMBA_BINARY('pmdactdb',
                          source='utils/pmda/pmda_ctdb.c',
-                         includes='include include/internal',
-                         deps='''ctdb-client ctdb-common
-                                 ctdb-common-util pcp_pmda pcp''',
+                         includes='include',
+                         deps='''ctdb-client ctdb-protocol ctdb-util
+                                 samba-util pcp_pmda pcp''',
                          install_path='${CTDB_PMDADIR}')
         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/Install',
                           destname='Install')
@@ -451,27 +605,40 @@ def build(bld):
         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/README',
                           destname='README')
 
+    if bld.env.HAVE_LIBRADOS:
+        bld.SAMBA_BINARY('ctdb_mutex_ceph_rados_helper',
+                         source='utils/ceph/ctdb_mutex_ceph_rados_helper.c',
+                        deps='talloc tevent rados',
+                        includes='include',
+                        install_path='${CTDB_HELPER_BINDIR}')
+
     sed_expr1 = 's|/usr/local/var/lib/ctdb|%s|g'  % (bld.env.CTDB_VARDIR)
     sed_expr2 = 's|/usr/local/etc/ctdb|%s|g'      % (bld.env.CTDB_ETCDIR)
     sed_expr3 = 's|/usr/local/var/log|%s|g'       % (bld.env.CTDB_LOGDIR)
     sed_expr4 = 's|/usr/local/var/run/ctdb|%s|g'  % (bld.env.CTDB_RUNDIR)
     sed_expr5 = 's|/usr/local/sbin|%s|g'          % (bld.env.SBINDIR)
-    sed_cmdline = '-e "%s" -e "%s" -e "%s" -e "%s" -e "%s"' % \
-                       (sed_expr1, sed_expr2, sed_expr3, sed_expr4, sed_expr5)
-
-    for f in manpages:
+    sed_expr6 = 's|/usr/local/libexec/ctdb|%s|g'  % (bld.env.CTDB_HELPER_BINDIR)
+    sed_expr7 = 's|/usr/local/bin|%s|g'           % (bld.env.BINDIR)
+    sed_cmdline = '-e "%s" -e "%s" -e "%s" -e "%s" -e "%s" -e "%s" -e "%s"' % \
+                       (sed_expr1, sed_expr2, sed_expr3, sed_expr4, sed_expr5,
+                        sed_expr6, sed_expr7)
+
+    manpages_extra = manpages_misc
+    if bld.env.etcd_reclock:
+        manpages_extra += manpages_etcd
+    if bld.env.HAVE_LIBRADOS:
+        manpages_extra += manpages_ceph
+    for f in manpages_binary + manpages_extra:
         x = '%s.xml' % (f)
         bld.SAMBA_GENERATOR(x,
                             source=os.path.join('doc', x),
                             target=x,
                             rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
 
-    if 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
-        bld.MANPAGES('''onnode.1 ctdbd_wrapper.1 ctdbd.conf.5
-                        ctdb.7 ctdb-statistics.7 ctdb-tunables.7''',
-                      True)
+    if bld.env.ctdb_generate_manpages:
+        bld.MANPAGES(' '.join(manpages_extra), True)
     else:
-        for m in manpages:
+        for m in bld.env.ctdb_prebuilt_manpages:
             bld.SAMBA_GENERATOR(m,
                                 source=os.path.join("doc", m),
                                 target=m,
@@ -492,6 +659,28 @@ def build(bld):
     bld.INSTALL_FILES('${BINDIR}', 'ctdb_diagnostics',
                       destname='ctdb_diagnostics', chmod=0755)
 
+    if bld.env.etcd_reclock:
+        bld.SAMBA_GENERATOR('ctdb-etcd-lock',
+                            source='utils/etcd/ctdb_etcd_lock',
+                            target='ctdb_etcd_lock',
+                            rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+        bld.INSTALL_FILES('${CTDB_HELPER_BINDIR}', 'ctdb_etcd_lock',
+                          destname='ctdb_etcd_lock', chmod=0744)
+
+    bld.SAMBA_GENERATOR('ctdb-natgw',
+                        source='tools/ctdb_natgw',
+                        target='ctdb_natgw',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES('${CTDB_HELPER_BINDIR}', 'ctdb_natgw',
+                      destname='ctdb_natgw', chmod=0755)
+
+    bld.SAMBA_GENERATOR('ctdb-lvs',
+                        source='tools/ctdb_lvs',
+                        target='ctdb_lvs',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES('${CTDB_HELPER_BINDIR}', 'ctdb_lvs',
+                      destname='ctdb_lvs', chmod=0755)
+
     bld.SAMBA_GENERATOR('ctdbd-wrapper',
                         source='config/ctdbd_wrapper',
                         target='ctdbd_wrapper',
@@ -540,7 +729,6 @@ def build(bld):
         'ctdb-crash-cleanup.sh',
         'debug-hung-script.sh',
         'debug_locks.sh',
-        'gcore_trace.sh',
         'nfs-linux-kernel-callout',
         'notify.sh',
         'statd-callout'
@@ -564,19 +752,23 @@ def build(bld):
     bld.install_dir(bld.env.CTDB_RUNDIR)
     bld.install_dir(bld.env.CTDB_VARDIR)
 
-    sed_expr = 's/@PACKAGE_VERSION@/%s/g' % VERSION
-    t = bld.SAMBA_GENERATOR('ctdb-pc',
-                            source='ctdb.pc.in',
-                            target='ctdb.pc',
-                            rule='sed -e "%s" ${SRC} > ${TGT}' % sed_expr,
-                            dep_vars=['VERSION'])
-    t.env.VERSION = VERSION
-    bld.INSTALL_FILES('${LIBDIR}/pkgconfig', 'ctdb.pc')
-
     # Unit tests
     ctdb_unit_tests = [
         'db_hash_test',
         'srvid_test',
+        'pkt_read_test',
+        'pkt_write_test',
+        'comm_test',
+        'comm_server_test',
+        'comm_client_test',
+        'pidfile_test',
+        'run_proc_test',
+        'sock_daemon_test',
+        'sock_io_test',
+        'hash_count_test',
+        'run_event_test',
+        'cmdline_test',
+        'conf_test',
     ]
 
     for target in ctdb_unit_tests:
@@ -584,32 +776,80 @@ def build(bld):
 
         bld.SAMBA_BINARY(target,
                          source=src,
-                         deps='talloc tevent tdb',
-                         install_path='${CTDB_TEST_LIBDIR}')
+                         deps='''talloc tevent tdb tevent-util popt
+                                 LIBASYNC_REQ samba-util sys_rw''',
+                         install_path='${CTDB_TEST_LIBEXECDIR}')
 
     bld.SAMBA_BINARY('reqid_test',
                      source='tests/src/reqid_test.c',
                      deps='samba-util',
-                     install_path='${CTDB_TEST_LIBDIR}')
+                     install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    bld.SAMBA_BINARY('rb_test',
+                     source='tests/src/rb_test.c',
+                     deps='samba-util talloc',
+                     install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    bld.SAMBA_BINARY('ctdb_packet_parse',
+                     source='tests/src/ctdb_packet_parse.c',
+                     deps='talloc tevent tdb ctdb-protocol',
+                     install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    bld.SAMBA_BINARY('porting_tests',
+                     source='tests/src/porting_tests.c',
+                     deps='samba-util ctdb-system popt',
+                     install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    bld.SAMBA_SUBSYSTEM('protocol-tests-common',
+                        source=bld.SUBDIR('tests/src',
+                                          '''protocol_common.c
+                                             protocol_common_ctdb.c
+                                             protocol_common_event.c
+                                          '''),
+                        includes='include',
+                        deps='replace popt talloc tevent tdb')
+
+    ctdb_protocol_tests = [
+            'protocol_basic_test',
+            'protocol_types_test',
+            'protocol_ctdb_test',
+            'protocol_event_test',
+            'protocol_util_test',
+            'protocol_types_compat_test',
+            'protocol_ctdb_compat_test',
+    ]
+
+    for target in ctdb_protocol_tests:
+        src = 'tests/src/' + target + '.c'
+
+        bld.SAMBA_BINARY(target,
+                         source=src,
+                         deps='''protocol-tests-common
+                                 samba-util talloc tdb''',
+                         install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-tests-common',
+                        source=bld.SUBDIR('tests/src',
+                                          'cluster_wait.c test_options.c'),
+                        includes='include',
+                        deps='replace popt talloc tevent tdb')
 
     # Test binaries
     ctdb_tests = [
-        'rb_test',
-        'ctdb_bench',
-        'ctdb_fetch',
-        'ctdb_fetch_one',
-        'ctdb_fetch_readonly_once',
-        'ctdb_fetch_readonly_loop',
-        'ctdb_trackingdb_test',
-        'ctdb_update_record',
-        'ctdb_update_record_persistent',
-        'ctdb_store',
-        'ctdb_traverse',
-        'ctdb_randrec',
-        'ctdb_persistent',
-        'ctdb_porting_tests',
-        'ctdb_transaction',
-        'ctdb_lock_tdb'
+        'g_lock_loop',
+        'message_ring',
+        'fetch_ring',
+        'fetch_loop',
+        'fetch_loop_key',
+        'fetch_readonly',
+        'fetch_readonly_loop',
+        'transaction_loop',
+        'update_record',
+        'update_record_persistent',
+        'lock_tdb',
+        'dummy_client',
+        'tunnel_test',
+        'tunnel_cmd',
     ]
 
     for target in ctdb_tests:
@@ -617,54 +857,62 @@ def build(bld):
 
         bld.SAMBA_BINARY(target,
                          source=src,
-                         includes='include include/internal',
-                         deps='''ctdb-client ctdb-common ctdb-common-util
-                                 ctdb-util''',
-                         install_path='${CTDB_TEST_LIBDIR}')
+                         includes='include',
+                         deps='''ctdb-client ctdb-protocol ctdb-util
+                                 samba-util ctdb-tests-common''',
+                         install_path='${CTDB_TEST_LIBEXECDIR}')
 
     bld.SAMBA_BINARY('ctdb_takeover_tests',
-                     source='tests/src/ctdb_takeover_tests.c',
+                     source='''tests/src/ctdb_takeover_tests.c
+                               tests/src/ipalloc_read_known_ips.c''',
                      deps='''replace popt tdb tevent talloc ctdb-system
-                             samba-util tdb-wrap talloc_report''' +
-                          ib_deps,
-                     includes='include include/internal',
-                     install_path='${CTDB_TEST_LIBDIR}')
-
-    bld.SAMBA_BINARY('ctdb_functest',
-                     source='tests/src/ctdb_functest.c',
-                     deps='''replace tdb tevent talloc popt ctdb-system
-                             samba-util tdb-wrap''',
-                     includes='include include/internal',
-                     install_path='${CTDB_TEST_LIBDIR}')
-
-    bld.SAMBA_BINARY('ctdb_stubtest',
-                     source='tests/src/ctdb_test.c',
-                     deps='''replace tdb tevent talloc popt ctdb-system
-                             samba-util tdb-wrap''',
-                     includes='include include/internal',
-                     install_path='${CTDB_TEST_LIBDIR}')
+                             samba-util tdb-wrap talloc_report
+                             ctdb-ipalloc ctdb-protocol ctdb-util''',
+                     includes='include',
+                     install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    bld.SAMBA_BINARY('fake_ctdbd',
+                     source='''tests/src/fake_ctdbd.c
+                               tests/src/ipalloc_read_known_ips.c''',
+                     deps='''ctdb-util ctdb-protocol ctdb-protocol-util
+                             ctdb-system samba-util tevent-util
+                             LIBASYNC_REQ popt''',
+                     install_path='${CTDB_TEST_LIBEXECDIR}')
 
     if bld.env.HAVE_INFINIBAND:
         bld.SAMBA_BINARY('ibwrapper_test',
                          source='ib/ibwrapper_test.c',
-                         includes='include include/internal',
-                         deps='replace talloc ctdb-client ctdb-common ' +
+                         includes='include',
+                         deps='replace talloc ctdb-common sys_rw' +
                               ib_deps,
-                         install_path='${CTDB_TEST_LIBDIR}')
+                         install_path='${CTDB_TEST_LIBEXECDIR}')
+
+    if bld.env.HAVE_ROBUST_MUTEXES and sys.platform.startswith('linux'):
+        bld.SAMBA_BINARY('test_mutex_raw',
+                         source='tests/src/test_mutex_raw.c',
+                         deps='pthread',
+                         install_path='${CTDB_TEST_LIBEXECDIR}')
 
     test_subdirs = [
         'complex',
         'cunit',
-        'events.d',
+        'eventd',
         'eventscripts',
         'onnode',
+        'shellcheck',
         'simple',
         'takeover',
+        'takeover_helper',
         'tool'
     ]
 
+    if bld.env.standalone_ctdb:
+        testdir = 'tests'
+    else:
+        testdir = 'ctdb/tests'
+
     for t in test_subdirs:
-        files = SUBDIR_MODE('tests/%s' % t, trim_path='tests')
+        files = SUBDIR_MODE('%s/%s' % (testdir, t), trim_path=testdir)
         for fmode in files:
             bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR, 'tests/%s' % fmode[0],
                               destname=fmode[0], chmod=fmode[1])
@@ -682,7 +930,7 @@ def build(bld):
                           destname=os.path.join('scripts', t))
 
     sed_expr = 's@^TEST_SCRIPTS_DIR=.*@&\\nexport TEST_BIN_DIR=\"%s\"@' % (
-               bld.env.CTDB_TEST_LIBDIR)
+               bld.env.CTDB_TEST_LIBEXECDIR)
     bld.SAMBA_GENERATOR('ctdb-test-wrap',
                         source='tests/scripts/test_wrap',
                         target='test_wrap',
@@ -690,8 +938,17 @@ def build(bld):
     bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR+"/scripts", 'test_wrap',
                       destname='test_wrap', chmod=0755)
 
-    sed_expr1 = 's@^test_dir=.*@test_dir=%s\\nexport TEST_BIN_DIR=\"%s\"@' % (
-                bld.env.CTDB_TEST_DATADIR, bld.env.CTDB_TEST_LIBDIR)
+    bld.SAMBA_GENERATOR('ctdb-test-script-install-paths',
+                        source='tests/scripts/script_install_paths.sh',
+                        target='script_install_paths.sh',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR+"/scripts",
+                      'script_install_paths.sh',
+                      destname='script_install_paths.sh', chmod=0644)
+
+    sed_expr1 = 's@^\(export %s\)=.*@\\1=%s\\nexport %s=\"%s\"@''' % (
+                    'CTDB_TEST_DIR', bld.env.CTDB_TEST_DATADIR,
+                    'TEST_BIN_DIR', bld.env.CTDB_TEST_LIBEXECDIR)
     sed_expr2 = 's@^\(export CTDB_TESTS_ARE_INSTALLED\)=false@\\1=true@'
     bld.SAMBA_GENERATOR('ctdb-test-runner',
                         source='tests/run_tests.sh',
@@ -703,31 +960,6 @@ def build(bld):
     bld.symlink_as(os.path.join(bld.env.BINDIR, 'ctdb_run_cluster_tests'),
                    'ctdb_run_tests')
 
-    test_eventscript_links = [
-        'events.d',
-        'functions',
-        'nfs-checks.d',
-        'nfs-linux-kernel-callout',
-        'statd-callout'
-    ]
-
-    test_link_dir = os.path.join(bld.env.CTDB_TEST_DATADIR,
-                                 'eventscripts/etc-ctdb')
-    for t in test_eventscript_links:
-        bld.symlink_as(os.path.join(test_link_dir, t),
-                       os.path.join(bld.env.CTDB_ETCDIR, t))
-
-    # Tests that use onnode need to overwrite link to in-tree
-    # functions file when installed
-    bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'onnode/functions'),
-                   os.path.join(bld.env.CTDB_ETCDIR, 'functions'))
-    bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'simple/functions'),
-                   os.path.join(bld.env.CTDB_ETCDIR, 'functions'))
-
-    # Need a link to nodes file because $CTDB_BASE is overridden
-    bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'simple/nodes'),
-                   os.path.join(bld.env.CTDB_ETCDIR, 'nodes'))
-
 
 def testonly(ctx):
     cmd = 'tests/run_tests.sh -V tests/var'
@@ -744,8 +976,8 @@ def test(ctx):
 
 
 def autotest(ctx):
-    ld = 'LD_PRELOAD=%s/bin/shared/libsocket-wrapper.so' % os.getcwd()
-    cmd = '%s tests/run_tests.sh -e -S -C' % ld
+    env = samba_utils.LOAD_ENVIRONMENT()
+    cmd = 'tests/run_tests.sh -e -S %s -C' % env.SOCKET_WRAPPER_SO_PATH
     ret = samba_utils.RUN_COMMAND(cmd)
     if ret != 0:
         print('autotest exited with exit status %d' % ret)
@@ -756,16 +988,34 @@ def show_version(ctx):
     print VERSION
 
 
-def dist():
+def manpages(ctx):
+    BASE_URL = 'http://docbook.sourceforge.net/release/xsl/current'
+    MAN_XSL = '%s/manpages/docbook.xsl' % BASE_URL
+    HTML_XSL = '%s/html/docbook.xsl' % BASE_URL
+    CMD_TEMPLATE = 'xsltproc --xinclude -o %s --nonet %s %s'
+    manpages = manpages_binary + manpages_misc + manpages_etcd + manpages_ceph
+    for t in manpages:
+        cmd = CMD_TEMPLATE % ('doc/%s' % t, MAN_XSL, 'doc/%s.xml' % t)
+        ret = samba_utils.RUN_COMMAND(cmd)
+        if ret != 0:
+            print('Command %s failed with exit status %d' % (cmd, ret))
+            sys.exit(ret)
+
+        cmd = CMD_TEMPLATE % ('doc/%s.html' % t, HTML_XSL, 'doc/%s.xml' % t)
+        ret = samba_utils.RUN_COMMAND(cmd)
+        if ret != 0:
+            print('Command %s failed with exit status %d' % (cmd, ret))
+            sys.exit(ret)
+
+
+def distonly(ctx):
     samba_dist.DIST_FILES('VERSION:VERSION', extend=True)
 
-    t = 'include/ctdb_version.h'
-    cmd = 'packaging/mkversion.sh %s %s' % (t, VERSION)
-    ret = samba_utils.RUN_COMMAND(cmd)
-    if ret != 0:
-        print('Command "%s" failed with exit status %d' % (cmd, ret))
-        sys.exit(ret)
-    samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
+    distfile = file('.distversion', 'w')
+    for field in version.vcs_fields:
+        distfile.write('%s=%s\n' % (field, str(version.vcs_fields[field])))
+    distfile.close()
+    samba_dist.DIST_FILES('ctdb/.distversion:.distversion', extend=True)
 
     t = 'ctdb.spec'
     sed_expr1 = 's/@VERSION@/%s/g' % VERSION
@@ -778,11 +1028,7 @@ def dist():
         sys.exit(ret)
     samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
 
-    cmd = 'make -C doc'
-    ret = samba_utils.RUN_COMMAND(cmd)
-    if ret != 0:
-        print('Command "%s" failed with exit status %d' % (cmd, ret))
-        sys.exit(ret)
+    manpages = manpages_binary + manpages_misc + manpages_etcd + manpages_ceph
     for t in manpages:
         samba_dist.DIST_FILES('ctdb/doc/%s:doc/%s' % (t, t), extend=True)
         samba_dist.DIST_FILES('ctdb/doc/%s.html:doc/%s.html' % (t, t),
@@ -791,6 +1037,12 @@ def dist():
     samba_dist.dist()
 
 
+def dist():
+    import Scripting
+    Scripting.commands.append('manpages')
+    Scripting.commands.append('distonly')
+
+
 def rpmonly(ctx):
     opts = os.getenv('RPM_OPTIONS') or ''
     cmd = 'rpmbuild -ta --clean --rmsource %s ctdb-%s.tar.gz' % (opts, VERSION)
@@ -802,7 +1054,8 @@ def rpmonly(ctx):
 
 def rpm(ctx):
     import Scripting
-    Scripting.commands.append('dist')
+    Scripting.commands.append('manpages')
+    Scripting.commands.append('distonly')
     Scripting.commands.append('rpmonly')