This file is indexed.

/usr/share/openvswitch/scripts/ovndb-servers.ocf is in ovn-common 2.9.0-0ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/bin/bash

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
: ${OVN_CTL_DEFAULT="/usr/share/openvswitch/scripts/ovn-ctl"}
: ${NB_MASTER_PORT_DEFAULT="6641"}
: ${NB_MASTER_PROTO_DEFAULT="tcp"}
: ${SB_MASTER_PORT_DEFAULT="6642"}
: ${SB_MASTER_PROTO_DEFAULT="tcp"}
: ${MANAGE_NORTHD_DEFAULT="no"}
: ${INACTIVE_PROBE_DEFAULT="5000"}

CRM_MASTER="${HA_SBIN_DIR}/crm_master -l reboot"
CRM_ATTR_REPL_INFO="${HA_SBIN_DIR}/crm_attribute --type crm_config --name OVN_REPL_INFO -s ovn_ovsdb_master_server"
OVN_CTL=${OCF_RESKEY_ovn_ctl:-${OVN_CTL_DEFAULT}}
MASTER_IP=${OCF_RESKEY_master_ip}
NB_MASTER_PORT=${OCF_RESKEY_nb_master_port:-${NB_MASTER_PORT_DEFAULT}}
NB_MASTER_PROTO=${OCF_RESKEY_nb_master_protocol:-${NB_MASTER_PROTO_DEFAULT}}
SB_MASTER_PORT=${OCF_RESKEY_sb_master_port:-${SB_MASTER_PORT_DEFAULT}}
SB_MASTER_PROTO=${OCF_RESKEY_sb_master_protocol:-${SB_MASTER_PROTO_DEFAULT}}
MANAGE_NORTHD=${OCF_RESKEY_manage_northd:-${MANAGE_NORTHD_DEFAULT}}
INACTIVE_PROBE=${OCF_RESKEY_inactive_probe_interval:-${INACTIVE_PROBE_DEFAULT}}

# Invalid IP address is an address that can never exist in the network, as
# mentioned in rfc-5737. The ovsdb servers connects to this IP address till
# a master is promoted and the IPAddr2 resource is started.
INVALID_IP_ADDRESS=192.0.2.254

host_name=$(ocf_attribute_target)
if [ "x$host_name" = "x" ]; then
    # function ocf_attribute_target may not be available if the pacemaker
    # version is old. Fall back to ocf_local_nodename.
    host_name=$(ocf_local_nodename)
fi
: ${slave_score=5}
: ${master_score=10}

ovsdb_server_metadata() {
    cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="ovsdb-server">
  <version>1.0</version>

  <longdesc lang="en">
    This resource manages ovsdb-server.
  </longdesc>

  <shortdesc lang="en">
    Manages ovsdb-server.
  </shortdesc>

  <parameters>

  <parameter name="ovn_ctl" unique="1">
  <longdesc lang="en">
  Location to the ovn-ctl script file
  </longdesc>
  <shortdesc lang="en">ovn-ctl script</shortdesc>
  <content type="string" default="${OVN_CTL_DEFAULT}" />
  </parameter>

  <parameter name="master_ip" unique="1">
  <longdesc lang="en">
  The IP address resource which will be available on the master ovsdb server
  </longdesc>
  <shortdesc lang="en">master ip address</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="nb_master_port" unique="1">
  <longdesc lang="en">
  The port which the master Northbound database server is listening
  </longdesc>
  <shortdesc lang="en">master Northbound database port</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="nb_master_protocol" unique="1">
  <longdesc lang="en">
  The protocol which the master Northbound database server used, 'tcp' or 'ssl'.
  </longdesc>
  <shortdesc lang="en">master Northbound database protocol</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="sb_master_port" unique="1">
  <longdesc lang="en">
  The port which the master Southbound database server is listening
  </longdesc>
  <shortdesc lang="en">master Southbound database port</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="sb_master_protocol" unique="1">
  <longdesc lang="en">
  The protocol which the master Southbound database server used, 'tcp' or 'ssl'.
  </longdesc>
  <shortdesc lang="en">master Southbound database protocol</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="manage_northd" unique="1">
  <longdesc lang="en">
  If set to yes, manages ovn-northd service. ovn-northd will be started in
  the master node.
  </longdesc>
  <shortdesc lang="en">manage ovn-northd service</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="inactive_probe_interval" unique="1">
  <longdesc lang="en">
  Inactive probe interval to set for ovsdb-server.
  </longdesc>
  <shortdesc lang="en">Set inactive probe interval</shortdesc>
  <content type="string" />
  </parameter>

  </parameters>

  <actions>
    <action name="notify"       timeout="20s" />
    <action name="start"        timeout="30s" />
    <action name="stop"         timeout="20s" />
    <action name="promote"      timeout="50s" />
    <action name="demote"       timeout="50s" />
    <action name="monitor"      timeout="20s"  depth="0" interval="10s"
     role="Master" />
    <action name="monitor"      timeout="20s"  depth="0" interval="30s"
     role="Slave"/>
    <action name="meta-data"    timeout="5s" />
    <action name="validate-all" timeout="20s" />
  </actions>
</resource-agent>
END
    exit $OCF_SUCCESS
}

ovsdb_server_notify() {
    # requires the notify=true meta resource attribute
    local type_op="${OCF_RESKEY_CRM_meta_notify_type}-${OCF_RESKEY_CRM_meta_notify_operation}"

    if [ "$type_op" != "post-promote" ]; then
        # We are only interested in specific events
        return $OCF_SUCCESS
    fi

    ocf_log debug "ovndb_server: notified of event $type_op"
    if [ "x$(ovsdb_server_last_known_master)" = "x${host_name}" ]; then
        # Record ourselves so that the agent has a better chance of doing
        # the right thing at startup
        ocf_log debug "ovndb_server: $host_name is the master"
        ${CRM_ATTR_REPL_INFO} -v "$host_name"
        if [ "$MANAGE_NORTHD" = "yes" ]; then
            # Startup ovn-northd service
            ${OVN_CTL} --ovn-manage-ovsdb=no start_northd
        fi

        conn=`ovn-nbctl get NB_global . connections`
        if [ "$conn" == "[]" ]
        then
            ovn-nbctl -- --id=@conn_uuid create Connection \
target="p${NB_MASTER_PROTO}\:${NB_MASTER_PORT}\:${MASTER_IP}" \
inactivity_probe=$INACTIVE_PROBE -- set NB_Global . connections=@conn_uuid
        fi

        conn=`ovn-sbctl get SB_global . connections`
        if [ "$conn" == "[]" ]
        then
            ovn-sbctl -- --id=@conn_uuid create Connection \
target="p${SB_MASTER_PROTO}\:${SB_MASTER_PORT}\:${MASTER_IP}" \
inactivity_probe=$INACTIVE_PROBE -- set SB_Global . connections=@conn_uuid
        fi

    else
        if [ "$MANAGE_NORTHD" = "yes" ]; then
            # Stop ovn-northd service. Set --ovn-manage-ovsdb=no so that
            # ovn-ctl doesn't stop ovsdb-servers.
            ${OVN_CTL} --ovn-manage-ovsdb=no stop_northd
        fi
        # Synchronize with the new master
        ocf_log debug "ovndb_server: Connecting to the new master ${OCF_RESKEY_CRM_meta_notify_promote_uname}"
        ${OVN_CTL} demote_ovnnb --db-nb-sync-from-addr=${MASTER_IP} \
                                --db-nb-sync-from-port=${NB_MASTER_PORT} \
                                --db-nb-sync-from-proto=${NB_MASTER_PROTO}
        ${OVN_CTL} demote_ovnsb --db-sb-sync-from-addr=${MASTER_IP} \
                                --db-sb-sync-from-port=${SB_MASTER_PORT} \
                                --db-sb-sync-from-proto=${SB_MASTER_PROTO}
    fi
}

ovsdb_server_usage() {
    cat <<END
usage: $0 {start|stop|status|monitor|notify|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
    exit $1
}

ovsdb_server_find_active_master() {
    # Operation sequence is Demote -> Stop -> Start -> Promote
    # At the point this is run, the only active masters will be
    # previous masters minus any that were scheduled to be demoted

    for master in ${OCF_RESKEY_CRM_meta_notify_master_uname}; do
        found=0
        for old in ${OCF_RESKEY_CRM_meta_notify_demote_uname}; do
            if [ $master = $old ]; then
                found=1
            fi
        done
        if [ $found = 0 ]; then
            # Rely on master-max=1
            # Pacemaker will demote any additional ones it finds before starting new copies
            echo "$master"
            return
        fi
    done

    local expected_master=$($CRM_ATTR_REPL_INFO --query  -q 2>/dev/null)
    case "x${OCF_RESKEY_CRM_meta_notify_start_uname}x" in
        *${expected_master}*) echo "${expected_master}";; # The previous master is expected to start
    esac
}

ovsdb_server_last_known_master()
{
    if [ -z "$MASTER_HOST" ]; then
        MASTER_HOST="$(${CRM_ATTR_REPL_INFO} --query  -q  2>/dev/null)"
    fi
    echo "$MASTER_HOST"
}

ovsdb_server_master_update() {
    case $1 in
        $OCF_SUCCESS)
        $CRM_MASTER -N $host_name -v ${slave_score};;
        $OCF_RUNNING_MASTER)
            $CRM_MASTER -N $host_name -v ${master_score};;
        #*) $CRM_MASTER -D;;
    esac
}

ovsdb_server_monitor() {
    ovsdb_server_check_status $@
    rc=$?

    ovsdb_server_master_update $rc
    return $rc
}

ovsdb_server_check_status() {
    local sb_status=`${OVN_CTL} status_ovnsb`
    local nb_status=`${OVN_CTL} status_ovnnb`

    if [[ $sb_status == "running/backup" && $nb_status == "running/backup" ]]; then
        return $OCF_SUCCESS
    fi

    check_northd="no"
    if [ "$MANAGE_NORTHD" == "yes" ] && [ "$1" != "ignore_northd" ]; then
        check_northd="yes"
    fi

    if [[ $sb_status == "running/active" && $nb_status == "running/active" ]]; then
        if [ "$check_northd" == "yes" ]; then
            # Verify if ovn-northd is running or not.
            ${OVN_CTL} status_northd
            if [ "$?" == "0" ] ; then
                return $OCF_RUNNING_MASTER
            fi
        else
            return $OCF_RUNNING_MASTER
        fi
    fi

    # TODO: What about service running but not in either state above?
    # Eg. a transient state where one db is "active" and the other
    # "backup"

    return $OCF_NOT_RUNNING
}

ovsdb_server_start() {
    ovsdb_server_check_status
    local status=$?
    # If not in stopped state, return
    if [ $status -ne $OCF_NOT_RUNNING ]; then
        return $status
    fi

    local present_master=$(ovsdb_server_find_active_master)

    set ${OVN_CTL}

    set $@ --db-nb-addr=${MASTER_IP} --db-nb-port=${NB_MASTER_PORT}
    set $@ --db-sb-addr=${MASTER_IP} --db-sb-port=${SB_MASTER_PORT}

    if [ "x${NB_MASTER_PROTO}" = xtcp ]; then
        set $@ --db-nb-create-insecure-remote=yes
    fi

    if [ "x${SB_MASTER_PROTO}" = xtcp ]; then
        set $@ --db-sb-create-insecure-remote=yes
    fi

    if [ "x${present_master}" = x ]; then
        # No master detected, or the previous master is not among the
        # set starting.
        #
        # Force all copies to come up as slaves by pointing them into
        # space and let pacemaker pick one to promote:
        #
        set $@ --db-nb-sync-from-addr=${INVALID_IP_ADDRESS} --db-sb-sync-from-addr=${INVALID_IP_ADDRESS}

    elif [ ${present_master} != ${host_name} ]; then
        # An existing master is active, connect to it
        set $@ --db-nb-sync-from-addr=${MASTER_IP} --db-sb-sync-from-addr=${MASTER_IP}
        set $@ --db-nb-sync-from-port=${NB_MASTER_PORT}
        set $@ --db-nb-sync-from-proto=${NB_MASTER_PROTO}
        set $@ --db-sb-sync-from-port=${SB_MASTER_PORT}
        set $@ --db-sb-sync-from-proto=${SB_MASTER_PROTO}
    fi

    $@ start_ovsdb

    while [ 1 = 1 ]; do
        # It is important that we don't return until we're in a functional
        # state. When checking the status of the ovsdb-server's ignore northd.
        # It is possible that when the resource is restarted ovsdb-server's
        # can be started as masters and ovn-northd would not have been started.
        # ovn-northd will be started once a node is promoted to master and
        # 'manage_northd' is set to yes.
        ovsdb_server_monitor ignore_northd
        rc=$?
        case $rc in
            $OCF_SUCCESS)        return $rc;;
            $OCF_RUNNING_MASTER)
                # When a slave node is promoted as master, the action would be
                # STOP -> START -> PROMOTE.
                # When the start action is called, it is possible for the
                # ovsdb-server's to be started as active. This could happen
                # if the node owns the $MASTER_IP. At this point, pacemaker
                # has not promoted this node yet. Demote it and check for
                # status again.
                # Let pacemaker promote it in subsequent actions.
                # As per the OCF guidelines, only monitor action should return
                # OCF_RUNNING_MASTER.
                # http://www.linux-ha.org/doc/dev-guides/_literal_ocf_running_master_literal_8.html
                ${OVN_CTL} demote_ovnnb \
                --db-nb-sync-from-addr=${INVALID_IP_ADDRESS}
                ${OVN_CTL} demote_ovnsb \
                --db-sb-sync-from-addr=${INVALID_IP_ADDRESS}
                ;;
            $OCF_ERR_GENERIC)    return $rc;;
            # Otherwise loop, waiting for the service to start, until
            # the cluster times the operation out
        esac
        ocf_log warn "ovndb_servers: After starting ovsdb, status is $rc. Checking the status again"
    done
}

ovsdb_server_stop() {
    if [ "$MANAGE_NORTHD" = "yes" ]; then
        # Stop ovn-northd service in case it was running. This is required
        # when the master is demoted. For other cases, it would be a no-op.
        # Set --ovn-manage-ovsdb=no so that ovn-ctl doesn't stop ovsdb-servers.
        ${OVN_CTL} --ovn-manage-ovsdb=no stop_northd
    fi

    ovsdb_server_check_status ignore_northd
    case $? in
        $OCF_NOT_RUNNING)
          # Even if one server is down, check_status returns NOT_RUNNING.
          # So before returning call stop_ovsdb to be sure.
          ${OVN_CTL} stop_ovsdb
          return ${OCF_SUCCESS};;
    esac

    ${OVN_CTL} stop_ovsdb
    ovsdb_server_master_update ${OCF_NOT_RUNNING}

    while [ 1 = 1 ]; do
        # It is important that we don't return until we're stopped
        ovsdb_server_check_status ignore_northd
        rc=$?
        case $rc in
        $OCF_SUCCESS)
            # Loop, waiting for the service to stop, until the
            # cluster times the operation out
            ocf_log warn "ovndb_servers: Even after stopping, the servers seems to be running"
            ;;
        $OCF_NOT_RUNNING)
            return $OCF_SUCCESS
            ;;
        *)
            return $rc
            ;;
        esac
    done

    return $OCF_ERR_GENERIC
}

ovsdb_server_promote() {
    ovsdb_server_check_status ignore_northd
    rc=$?
    case $rc in
        ${OCF_SUCCESS}) ;;
        ${OCF_RUNNING_MASTER}) return ${OCF_SUCCESS};;
        *)
            ovsdb_server_master_update $OCF_RUNNING_MASTER
            return ${rc}
            ;;
    esac

    ${OVN_CTL} promote_ovnnb
    ${OVN_CTL} promote_ovnsb

    if [ "$MANAGE_NORTHD" = "yes" ]; then
        # Startup ovn-northd service
        ${OVN_CTL} --ovn-manage-ovsdb=no start_northd
    fi

    ocf_log debug "ovndb_servers: Promoting $host_name as the master"
    # Record ourselves so that the agent has a better chance of doing
    # the right thing at startup
    ${CRM_ATTR_REPL_INFO} -v "$host_name"
    ovsdb_server_master_update $OCF_RUNNING_MASTER
    return $OCF_SUCCESS
}

ovsdb_server_demote() {
    # While demoting, check the status of ovn_northd.
    # In case ovn_northd is not running, we should return OCF_NOT_RUNNING.
    ovsdb_server_check_status
    if [ $? = $OCF_NOT_RUNNING ]; then
        return $OCF_NOT_RUNNING
    fi

    local present_master=$(ovsdb_server_find_active_master)
    local recorded_master=$($CRM_ATTR_REPL_INFO --query  -q 2>/dev/null)

    ocf_log debug "ovndb_servers: Demoting $host_name, present master ${present_master}, recorded master ${recorded_master}"
    if [ "x${recorded_master}" = "x${host_name}" -a "x${present_master}" = x ]; then
        # We are the one and only master
        # This should be the "normal" case
        # The only way to be demoted is to call demote_ovn*
        #
        # The local database is only reset once we successfully
        # connect to the peer.  So specify one that doesn't exist.
        #
        # Eventually a new master will be promoted and we'll resync
        # using the logic in ovsdb_server_notify()
        ${OVN_CTL} demote_ovnnb --db-nb-sync-from-addr=${INVALID_IP_ADDRESS}
        ${OVN_CTL} demote_ovnsb --db-sb-sync-from-addr=${INVALID_IP_ADDRESS}

    elif [ "x${present_master}" = "x${host_name}" ]; then
        # Safety check, should never be called
        #
        # Never allow sync'ing from ourselves, its a great way to
        # erase the local DB
        ${OVN_CTL} demote_ovnnb --db-nb-sync-from-addr=${INVALID_IP_ADDRESS}
        ${OVN_CTL} demote_ovnsb --db-sb-sync-from-addr=${INVALID_IP_ADDRESS}

    elif [ "x${present_master}" != x ]; then
        # There are too many masters and we're an extra one that is
        # being demoted. Sync to the surviving one
        ${OVN_CTL} demote_ovnnb --db-nb-sync-from-addr=${MASTER_IP} \
                                --db-nb-sync-from-port=${NB_MASTER_PORT} \
                                --db-nb-sync-from-proto=${NB_MASTER_PROTO}
        ${OVN_CTL} demote_ovnsb --db-sb-sync-from-addr=${MASTER_IP} \
                                --db-sb-sync-from-port=${SB_MASTER_PORT} \
                                --db-sb-sync-from-proto=${SB_MASTER_PROTO}

    else
        # For completeness, should never be called
        #
        # Something unexpected happened, perhaps CRM_ATTR_REPL_INFO is incorrect
        ${OVN_CTL} demote_ovnnb --db-nb-sync-from-addr=${INVALID_IP_ADDRESS}
        ${OVN_CTL} demote_ovnsb --db-sb-sync-from-addr=${INVALID_IP_ADDRESS}
    fi

    if [ "$MANAGE_NORTHD" = "yes" ]; then
        # Stop ovn-northd service
        ${OVN_CTL} --ovn-manage-ovsdb=no stop_northd
    fi
    ovsdb_server_master_update $OCF_SUCCESS
    return $OCF_SUCCESS
}

ovsdb_server_validate() {
    if [ ! -e ${OVN_CTL} ]; then
        return $OCF_ERR_INSTALLED
    fi
    return $OCF_SUCCESS
}


case $__OCF_ACTION in
start)          ovsdb_server_start;;
stop)           ovsdb_server_stop;;
promote)        ovsdb_server_promote;;
demote)         ovsdb_server_demote;;
notify)         ovsdb_server_notify;;
meta-data)      ovsdb_server_metadata;;
validate-all)   ovsdb_server_validate;;
status|monitor) ovsdb_server_monitor;;
usage|help)     ovsdb_server_usage $OCF_SUCCESS;;
*)              ovsdb_server_usage $OCF_ERR_UNIMPLEMENTED ;;
esac

rc=$?
exit $rc