|
| 1 | +#!/usr/bin/env bashio |
| 2 | + |
| 3 | +#set -euxo pipefail |
| 4 | +set -euo pipefail |
| 5 | +# source: https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425 |
| 6 | + |
| 7 | +CONFIG="/etc/nats/nats-server.conf" |
| 8 | + |
| 9 | +# Root |
| 10 | +{ |
| 11 | + echo "port: 4222" |
| 12 | + echo "monitor_port: 8222" |
| 13 | + |
| 14 | + SERVER_NAME=$(bashio::config 'server_name' '143ce9cf-nats-server') |
| 15 | + echo "server_name: \"${SERVER_NAME}\"" |
| 16 | + |
| 17 | + # JetStream |
| 18 | + # source: https://docs.nats.io/running-a-nats-service/configuration/resource_management |
| 19 | + if bashio::config.true 'jetstream.enabled'; then |
| 20 | + echo "jetstream {" |
| 21 | + echo " store_dir: /data/storage" |
| 22 | + if bashio::config.exists 'jetstream.max_mem'; then |
| 23 | + echo " max_mem: $(bashio::config 'jetstream.max_mem')" |
| 24 | + fi |
| 25 | + if bashio::config.exists 'jetstream.max_file'; then |
| 26 | + echo " max_file: $(bashio::config 'jetstream.max_file')" |
| 27 | + fi |
| 28 | + if bashio::config.exists 'jetstream.chiper'; then |
| 29 | + echo " chiper: $(bashio::config 'jetstream.chiper')" |
| 30 | + fi |
| 31 | + if bashio::config.exists 'jetstream.key'; then |
| 32 | + echo " key: $(bashio::config 'jetstream.key')" |
| 33 | + fi |
| 34 | + echo "}" |
| 35 | + fi |
| 36 | + |
| 37 | + # MQTT |
| 38 | + # source: https://docs.nats.io/running-a-nats-service/configuration/mqtt/mqtt_config |
| 39 | + if bashio::config.true 'mqtt.enabled'; then |
| 40 | + echo "mqtt {" |
| 41 | + echo " port: 1883" |
| 42 | + if bashio::config.exists 'mqtt.username' || bashio::config.exists 'mqtt.password'; then |
| 43 | + echo " authorization {" |
| 44 | + echo " username: \"$(bashio::config 'mqtt.username' '')\"" |
| 45 | + echo " password: \"$(bashio::config 'mqtt.password' '')\"" |
| 46 | + if bashio::config.exists 'mqtt.timeout'; then |
| 47 | + echo " timeout: $(bashio::config 'mqtt.timeout')" |
| 48 | + fi |
| 49 | + echo " }" |
| 50 | + fi |
| 51 | + |
| 52 | + if bashio::config.true 'cluster.tls'; then |
| 53 | + echo " tls {" |
| 54 | + if bashio::config.exists 'cluster.tls_cert_file'; then |
| 55 | + echo " cert_file: \"$(bashio::config 'cluster.tls_cert_file')\"" |
| 56 | + fi |
| 57 | + if bashio::config.exists 'cluster.tls_key_file'; then |
| 58 | + echo " key_file: \"$(bashio::config 'cluster.tls_key_file')\"" |
| 59 | + fi |
| 60 | + if bashio::config.exists 'cluster.tls_ca_file'; then |
| 61 | + echo " ca_file: \"$(bashio::config 'cluster.tls_ca_file')\"" |
| 62 | + fi |
| 63 | + if bashio::config.exists 'cluster.tls_verify'; then |
| 64 | + echo " verify: $(bashio::config 'cluster.tls_verify')" |
| 65 | + fi |
| 66 | + if bashio::config.exists 'cluster.tls_timeout'; then |
| 67 | + echo " timeout: \"$(bashio::config 'cluster.tls_timeout')\"" |
| 68 | + fi |
| 69 | + if bashio::config.exists 'cluster.tls_verify_and_map'; then |
| 70 | + echo " verify_and_map: $(bashio::config 'cluster.tls_verify_and_map')" |
| 71 | + fi |
| 72 | + echo " }" # tls |
| 73 | + fi |
| 74 | + echo "}" |
| 75 | + fi |
| 76 | + |
| 77 | + # Clustering |
| 78 | + # source: https://docs.nats.io/running-a-nats-service/configuration/clustering/cluster_config |
| 79 | + if bashio::config.true 'cluster.enabled'; then |
| 80 | + echo "cluster {" |
| 81 | + echo " port: 6222" |
| 82 | + |
| 83 | + if bashio::config.exists 'cluster.name'; then |
| 84 | + echo " name: \"$(bashio::config 'cluster.name')\"" |
| 85 | + fi |
| 86 | + if bashio::config.exists 'cluster.host'; then |
| 87 | + echo " host: \"$(bashio::config 'cluster.host')\"" |
| 88 | + fi |
| 89 | + |
| 90 | + if bashio::config.exists 'cluster.username' || bashio::config.exists 'cluster.password'; then |
| 91 | + echo " authorization {" |
| 92 | + echo " username: \"$(bashio::config 'cluster.username')\"" |
| 93 | + echo " password: \"$(bashio::config 'cluster.password')\"" |
| 94 | + if bashio::config.exists 'cluster.timeout'; then |
| 95 | + echo " timeout: $(bashio::config 'cluster.timeout')" |
| 96 | + fi |
| 97 | + echo " }" |
| 98 | + fi |
| 99 | + |
| 100 | + # Routes |
| 101 | + echo " routes = [" |
| 102 | + for route in $(bashio::config 'cluster.routes') |
| 103 | + do |
| 104 | + echo " $route" |
| 105 | + done |
| 106 | + echo " ]" |
| 107 | + |
| 108 | + |
| 109 | + if bashio::config.true 'mqtt.tls'; then |
| 110 | + echo " tls {" |
| 111 | + if bashio::config.exists 'mqtt.tls_cert_file'; then |
| 112 | + echo " cert_file: \"$(bashio::config 'mqtt.tls_cert_file')\"" |
| 113 | + fi |
| 114 | + if bashio::config.exists 'mqtt.tls_key_file'; then |
| 115 | + echo " key_file: \"$(bashio::config 'mqtt.tls_key_file')\"" |
| 116 | + fi |
| 117 | + if bashio::config.exists 'mqtt.tls_ca_file'; then |
| 118 | + echo " ca_file: \"$(bashio::config 'mqtt.tls_ca_file')\"" |
| 119 | + fi |
| 120 | + if bashio::config.exists 'mqtt.tls_verify'; then |
| 121 | + echo " verify: $(bashio::config 'mqtt.tls_verify')" |
| 122 | + fi |
| 123 | + if bashio::config.exists 'mqtt.tls_timeout'; then |
| 124 | + echo " timeout: \"$(bashio::config 'mqtt.tls_timeout')\"" |
| 125 | + fi |
| 126 | + if bashio::config.exists 'mqtt.tls_verify_and_map'; then |
| 127 | + echo " verify_and_map: $(bashio::config 'mqtt.tls_verify_and_map')" |
| 128 | + fi |
| 129 | + echo " }" # tls |
| 130 | + if bashio::config.exists 'mqtt.no_auth_user'; then |
| 131 | + echo " no_auth_user: \"$(bashio::config 'mqtt.no_auth_user')\"" |
| 132 | + fi |
| 133 | + if bashio::config.exists 'mqtt.ack_wait'; then |
| 134 | + echo " ack_wait: \"$(bashio::config 'mqtt.ack_wait')\"" |
| 135 | + fi |
| 136 | + if bashio::config.exists 'mqtt.max_ack_pending'; then |
| 137 | + echo " max_ack_pending: $(bashio::config 'mqtt.max_ack_pending')" |
| 138 | + fi |
| 139 | + fi |
| 140 | + fi |
| 141 | + |
| 142 | + echo "}" |
| 143 | + |
| 144 | + # Logging |
| 145 | + # source: https://docs.nats.io/running-a-nats-service/configuration/logging |
| 146 | + if bashio::config.exists 'debug'; then |
| 147 | + echo "debug: $(bashio::config 'debug')" |
| 148 | + fi |
| 149 | + if bashio::config.exists 'trace'; then |
| 150 | + echo "trace: $(bashio::config 'trace')" |
| 151 | + fi |
| 152 | + if bashio::config.exists 'logtime'; then |
| 153 | + echo "logtime: $(bashio::config 'logtime')" |
| 154 | + fi |
| 155 | + if bashio::config.exists 'logfile_size_limit'; then |
| 156 | + echo "logfile_size_limit: $(bashio::config 'logfile_size_limit')" |
| 157 | + fi |
| 158 | + if bashio::config.exists 'log_file'; then |
| 159 | + echo "log_file: $(bashio::config 'log_file')" |
| 160 | + fi |
| 161 | +} > "$CONFIG" |
| 162 | + |
| 163 | +# this if will check if the first argument is a flag |
| 164 | +# but only works if all arguments require a hyphenated flag |
| 165 | +# -v; -SL; -f arg; etc will work, but not arg1 arg2 |
| 166 | +if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then |
| 167 | + set -- nats-server "$@" |
| 168 | +fi |
| 169 | +# else default to run whatever the user wanted like "bash" or "sh" |
| 170 | +exec "$@" |
0 commit comments