aboutsummaryrefslogtreecommitdiff
path: root/net-p2p/cardano-node/files/cardano_node.in
blob: 785b7efebc6f9a132677eca30b11027a853d2175 (plain) (blame)
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
#!/bin/sh

# PROVIDE: cardano_node
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# -----------------------------------------------------------------------------
#
# This script supports running multiple instances of the daemon.
# To run additional instances make a symlink to script under different name:
#
# % ln -s %%PREFIX%%/etc/rc.d/cardano_node %%PREFIX%%/etc/rc.d/SOMENAME
#
# and define corresponding SOMENAME_* variables in /etc/rc.conf
# For example, if you linked the script to cardano_node_testnet, then each
# variable listed below should read as cardano_node_testnet_enable, etc.
#
# -----------------------------------------------------------------------------
#
# Add the following lines to /etc/rc.conf to enable this service:
#
# cardano_node_enable:      Set to YES to enable cardano-node.
#                           Default: "NO"
#
# cardano_node_jail_enable: Set to NO to disable running cardano-node in the jail.
#                           Default: "YES"
#
# cardano_node_home:        An absolute path to the daemon home directory.
#                           The directory will be created if not exists.
#                           Default: "/var/db/cardano_node"
#
# cardano_node_net:         A network name to connect to.
#                           Default: "mainnet"
#
# cardano_node_port:        Port to listen for connections on.
#                           Default: "6000"
#
# Advanced settings that usually don't need to be changed for simple usage cases:
#
# cardano_node_host:        Host address to bind to.
#                           Default: "0.0.0.0"
#
#
# cardano_node_socket:      An absolute path to the daemon socket file.
#                           Default: "${cardano_node_home}/cardano-node.sock"
#
# cardano_node_db:          An absolute path to the database directory.
#                           Default: "${cardano_node_home}/${cardano_node_net}-db"
#
# cardano_node_topology:    An absolute or a relative to ${cardano_node_home} path
#                           to the topology JSON file.
#                           Default: "${cardano_node_net}-configs/topology.json"
#
# cardano_node_config:      An absolute or a relative to ${cardano_node_home} path
#                           to the cardano-node config.json file.
#                           Default: "${cardano_node_net}-configs/config.json"
#
# cardano_node_rts_flags:   GHC runtime flags to be passed between "+RTS" and "-RTS".
#                           See https://downloads.haskell.org/ghc/latest/docs/html/users_guide/runtime_control.html
#                           for the meaning of these flags.
#                           Default: "-N -A64m -n4m -F1.2 -qg1"
#
# cardano_node_flags:       Any additional command line flags to pass to cardano-node.
#                           Default: ""
#

. /etc/rc.subr

# The following code snippet was taken from security/openvpn/files/openvpn.in rc script.

# service(8) does not create an authentic environment, try to guess,
# and as of 10.3-RELEASE-p0, it will not find the indented name=
# assignments below. So give it a default.
# Trailing semicolon also for service(8)'s benefit:
name="$file" ;

case "$0" in
/etc/rc*)
	# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
	# so get the name of the script from $_file
	name="$_file"
	;;
*/service)
	# do not use this as $0
	;;
*)
	name="$0"
	;;
esac

# default name to "cardano_node" if guessing failed
# Trailing semicolon also for service(8)'s benefit:
name="${name:-cardano_node}" ;
name="${name##*/}"

desc="Cardano Node daemon"
rcvar="${name}_enable"
command=%%PREFIX%%/bin/cardano-node

cardano_deployment_url="https://raw.githubusercontent.com/cardano-bsd-alliance/freebsd-ports-cardano-artifacts/master/cardano-node"
cardano_config_files="config byron-genesis shelley-genesis alonzo-genesis conway-genesis topology submit-api-config"
cardano_networks="mainnet preview preprod"

start_cmd="cardano_node_start"
start_precmd="cardano_node_prestart"
stop_cmd="cardano_node_stop"
status_cmd="cardano_node_status"
reload_cmd="cardano_node_reload"
fetch_cmd="cardano_node_fetch"

extra_commands="status fetch reload"

load_rc_config $name
eval ": \${${name}_enable:=NO}"
eval ": \${${name}_jail_enable:=YES}"
eval ": \${${name}_home:=\"/var/db/cardano_node\"}"
eval ": \${${name}_net:=\"mainnet\"}"
eval ": \${${name}_host:=\"0.0.0.0\"}"
eval ": \${${name}_port:=\"6000\"}"
eval ": \${${name}_socket:=\"\${${name}_home}/cardano-node.sock\"}"
eval ": \${${name}_db:=\"\${${name}_home}/\${${name}_net}-db\"}"
eval ": \${${name}_topology:=\"\${${name}_net}-configs/topology.json\"}"
eval ": \${${name}_config:=\"\${${name}_net}-configs/config.json\"}"
eval ": \${${name}_rts_flags:=\"-N -A64m -n4m -F1.2 -qg1\"}"
eval ": \${${name}_flags:=\"\"}"

# aliases
eval "_jail_enable=\${${name}_jail_enable}"
eval "_home=\${${name}_home}"
eval "_topology=\${${name}_topology}"
eval "_config=\${${name}_config}"
eval "_socket=\${${name}_socket}"
eval "_db=\${${name}_db}"
eval "_rts_flags=\${${name}_rts_flags}"
eval "_host=\${${name}_host}"
eval "_port=\${${name}_port}"
eval "_flags=\${${name}_flags}"

jail_topology="/topology_dir/`basename ${_topology}`"
jail_config="/config_dir/`basename ${_config}`"
jail_socket="/socket/`basename ${_socket}`"
jail_args="name=${name}_jail exec.jail_user=cardano exec.system_jail_user host=inherit"
jail_command=/bin/cardano-node

jail_root="${_home}/jail"
jail_copy_resolv_conf=yes
jail_copy_services=yes
jail_copy_programs="$command /usr/sbin/nologin"
jail_mount_devfs=yes
jail_ip_inherit=yes
#TODO: daemon fails with "Network.Socket.bind: permission denied" without suid ;\
jail_prepare_inside_cmds="mkdir ./socket ;\
                          ln -s ${_home}/jail/${jail_socket} ${_socket} ;\
                          chmod +s .${jail_command}"
jail_nullfs_mounts="${_db} ${jail_root}/db rw \
                    ${_home}/logs ${jail_root}/logs rw"

if checkyesno "_jail_enable"; then
    _socket_arg="${jail_socket}"
    _topology_arg="${jail_topology}"
    _config_arg="${jail_config}"
    _db_arg="/db"
    # We need to override ${command} to make check_pidfile work correctly when
    # rc.subr calls it as "check_pidfile ${pidfile} ${command}"
    command=/usr/sbin/jail
else
    _socket_arg="${_socket}"
    _topology_arg="${_topology}"
    _config_arg="${_config}"
    _db_arg="${_db}"
fi

pidfile="/var/run/${name}.pid"
flags="run +RTS ${_rts_flags} -RTS \
        --database-path ${_db_arg} \
        --host-addr ${_host} \
        --port ${_port} \
        --socket-path ${_socket_arg} \
        --topology ${_topology_arg} \
        --config ${_config_arg} \
        ${_flags}"

. %%LOCALBASE%%/share/rc-subr-jail/rc.subr.jail

# realpath2 path
# Returns an absolute path to ${_home}/${path} if it exists, otherwise
# treats ${path} as absolute
realpath2()
{
    local _path _realpath
    _path=$1

    _realpath=$(/bin/sh -c "cd ${_home} && realpath ${_path}" 2> /dev/null)
    if [ $? != "0" ]
    then
        return 1
    else
        echo $_realpath
    fi
}

sanity_check()
{
    realpath2 ${_topology} > /dev/null
    if [ $? != "0" ]
    then
        echo "Invalid value for ${name}_topology: missing file ${_topology}"
        echo "You might want to run service ${name} onefetch to download this file into default dir"
        exit 1
    fi
    realpath2 ${_config} > /dev/null
    if [ $? != "0" ]
    then
        echo "Invalid value for cardano_node_config: missing file ${_config}"
        echo "You might want to run service ${name} onefetch to download this file into the default dir"
        exit 1
    fi
    return 0
}

cardano_node_prestart()
{
    # Create Cardano home directory, if not exists
    if [ ! -d "${_home}" ]; then
        mkdir -p "${_home}"
        chown cardano:cardano "${_home}"
    fi
    # Do the same for the logs directory
    if [ ! -d "${_home}/logs" ]; then
        mkdir -p "${_home}/logs"
        chown cardano:cardano "${_home}/logs"
    fi

    # Remove the symlink to the socket file if there is no pid file
    if [ -L "${_socket}" -a ! -f $pidfile ]; then
        rm "${_socket}"
    fi

    sanity_check
}

cardano_node_start()
{
    check_startmsgs && echo "Starting ${name}."

    local _topo _conf
    _topo=$(realpath2 ${_topology})
    _conf=$(realpath2 ${_config})

    jail_nullfs_mounts="$jail_nullfs_mounts $(dirname ${_topo}) ${jail_root}/topology_dir ro"
    jail_nullfs_mounts="$jail_nullfs_mounts $(dirname ${_conf}) ${jail_root}/config_dir ro"

    if checkyesno "_jail_enable"; then
        prepare_jail $jail_root
        if [ "$?" != "0" ]; then
            echo "Failed to start ${name}: jail creation error"
            return 1
        fi

        cd $_home && /bin/sh -c "/usr/sbin/daemon -p $pidfile -S -T cardano-node \
            ${command} -c ${jail_prepared_args} ${jail_args} command=${jail_command} ${flags}"
    else
        cd $_home && /usr/sbin/daemon -p $pidfile -S -T cardano-node \
            ${command} ${flags}
    fi
}

cardano_node_stop()
{
    local _topo _conf _ret
    _topo=$(realpath2 ${_topology})
    _conf=$(realpath2 ${_config})

    jail_nullfs_mounts="$jail_nullfs_mounts $(dirname ${_topo}) ${jail_root}/topology_dir ro"
    jail_nullfs_mounts="$jail_nullfs_mounts $(dirname ${_conf}) ${jail_root}/config_dir ro"

    pid=$(check_pidfile "${pidfile}" "$command")

    if [ -z "${pid}" ]
    then
        echo "${name} is not running"
        _ret=1
    else
        echo "Stopping ${name}."
        kill_jail "$pid" -INT "_jail_enable"
        wait_for_pids "$pid"
        _ret=0
    fi

    if checkyesno "_jail_enable"; then
            destroy_jail $jail_root 2> /dev/null
    fi
    rm -rf ${_socket}

    return $_ret
}

cardano_node_status()
{
    pid=$(check_pidfile "${pidfile}" "$command")

    if [ -z "${pid}" ]
    then
        echo "${name} is not running"
        return 1
    else
        echo ${name} is running as pid $pid
    fi
}

cardano_node_reload()
{
    pid=$(check_pidfile "${pidfile}" "$command")

    if [ -z "${pid}" ]
    then
        echo "${name} is not running"
        return 1
    else
        kill_jail "$pid" -HUP "_jail_enable"
    fi
}

cardano_node_fetch()
{
    for net in ${cardano_networks}
    do
        echo "===> Fetching configuration files for ${net}"
        mkdir -p "${_home}/${net}-configs"
        mkdir -p "${_home}/${net}-db"
        /usr/bin/apply "/usr/bin/fetch -a -o \
        ${_home}/${net}-configs ${cardano_deployment_url}/${net}-configs/%1.json" $cardano_config_files
        chown -R cardano:cardano "${_home}/${net}-configs"
        chown -R cardano:cardano "${_home}/${net}-db"
    done
}

run_rc_command "$1"