https://github.com/TheThingsNetwork/lorawan-stack/blob/master/doc/gettingstarted.md
这篇笔记记录了 stack 运行前所需的几个准备工作,包含了证书、数据库、账户创建、OAuth配置、环境变量等。参考了官方的 docker 配置文件,琢磨出了各个依赖组件和启动命令,是一套针对非docker环境的操作记录。
小能手这段时间在学习 The Things Network LoRaWAN Stack V3,从使用和代码等角度对该 Stack 进行了分析,详细可点此查看。
为了让内容可以使用 TLS 进行传输,整个 Stack 需要 cert.pem
和 key.pem
。
实际上 在 make init 之后,已经自动创建了。
lorawan-stack Generating certificates
2019/03/15 15:24:06 wrote cert.pem
2019/03/15 15:24:06 wrote key.pem
make[1]: Leaving directory '/home/ubuntu/lorawan-stack'
自签名证书会出现浏览器和OS不信任的情况,导致一些警告或者错误。可以考虑使用 Let’s Encrypt 申请免费的 TLS 证书。
By default, frequency plans are fetched by the stack from the TheThingsNetwork/lorawan-frequency-plans
repository. To set a new source:
TTN_LW_FREQUENCY_PLANS_URL
allows you to serve frequency plans fetched from a HTTP server.
TTN_LW_FREQUENCY_PLANS_DIRECTORY
allows you to serve frequency plans from a local directory.
配置其实支持3种方式:
CockroachDB 是一个分布式数据库,被用在了项目中的 Identity Server。
具体安装方法 点击这里。
cockroach start --http-port 26256 --insecure
Redis 是个比较流行的内存数据存储的数据库,在这个项目中也有用到。
具体安装方法 点击这里。
redis-server --appendonly yes
必须保证 cockroach 先运行起来。
$ ./ttn-lw-stack is-db init
INFO Connecting to Identity Server database...
INFO Detected database CockroachDB CCL v2.1.6 (x86_64-unknown-linux-gnu, built 2019/03/04 23:21:07, go1.10.7)
INFO Creating database "ttn_lorawan_dev"...
INFO Creating tables in "ttn_lorawan_dev"...
INFO Successfully initialized
$ ./ttn-lw-stack is-db create-admin-user --id admin --email admin@localhost
INFO Connecting to Identity Server database...
Please enter user password:**
Please repeat user password:**
INFO Creating user...
INFO Created user
$ ./ttn-lw-stack is-db create-oauth-client \
--id cli \
--name "Command Line Interface" \
--owner admin \
--no-secret \
--redirect-uri 'local-callback' \
--redirect-uri 'code'
$ ./ttn-lw-stack is-db create-oauth-client \
--id console --name "Console" \
--owner admin \
--redirect-uri 'http://example.com:1885/console/oauth/callback' \
--redirect-uri 'https://example.com:8885/console/oauth/callback'
INFO Connecting to Identity Server database...
INFO Creating OAuth client...
INFO Created OAuth client secret=TQVLMKM2RR26VDCEXSWWEM7CKNS7T4GPB67L32SVXQHW24QGLY7Q
// 这里的 redirect-uri 没想好怎么用,得再深入研究下 OAuth。
在 docker yaml 文件内,还可以看到定义了一些环境变量。
- TTN_LW_HTPP_COOKIE_HASH_KEY
- TTN_LW_HTTP_COOKIE_BLOCK_KEY
- TTN_LW_CLUSTER_KEYS
- TTN_LW_FREQUENCY_PLANS_URL
- TTN_LW_CONSOLE_OAUTH_CLIENT_SECRET
- TTN_LW_IS_DATABASE_URI=postgres://root@cockroach:26257/${DEV_DATABASE_NAME:-ttn_lorawan}?sslmode=disable
- TTN_LW_REDIS_ADDRESS=redis:6379
- TTN_LW_TLS_CERTIFICATE=/run/secrets/cert.pem
- TTN_LW_CA=/run/secrets/cert.pem
- TTN_LW_TLS_KEY=/run/secrets/key.pem
为了简单方便,写个脚本。重点把 CONSOLE_OAUTH_CLIENT_SECRET 配置下,以及把证书文件的路径调整一下。
export TTN_LW_COOKIE_HASHKEY=
export TTN_LW_COOKIE_BLOCKKEY=
export TTN_LW_CLUSTER_KEYS=
export TTN_LW_FREQUENCY_PLANS_URL=
export TTN_LW_CONSOLE_OAUTH_CLIENT_SECRET=TQVLMKM2RR26VDCEXSWWEM7CKNS7T4GPB67L32SVXQHW24QGLY7Q
export TTN_LW_IS_DATABASE_URI=postgres://root@localhost:26257/ttn_lorawan_dev?sslmode=disable
export TTN_LW_REDIS_ADDRESS=localhost:6379
export TTN_LW_TLS_CERTIFICATE=/home/ubuntu/lorawan-stack/cert.pem
export TTN_LW_CA=/home/ubuntu/lorawan-stack/cert.pem
export TTN_LW_TLS_KEY=/home/ubuntu/lorawan-stack/key.pem
./ttn-lw-stack start
可以在脚本里加上这一句,一起跑起来。
ubuntu@VM-0-6-ubuntu:~/lorawan-stack$ ./run.sh
INFO Setting up core component
WARN No cookie hash key configured, generated a random one hash_key=37588D493A48C90BC9F9BFB2D54B3940F8B0BF79934C30DFA5E1E412E0F2D09B904554FCE192B3C4FF7B7FB11F564DD805DBD730B7244189B651C4B3BC5CD569 namespace=web
WARN No cookie block key configured, generated a random one block_key=88DAC6E8003625D5F200CF969BAB793DCD3D735E2E73531F6BAE2D800D33DEEC namespace=web
INFO Setting up Identity Server
INFO Setting up Gateway Server
INFO Setting up Network Server
INFO Setting up Application Server
INFO Setting up Join Server
INFO Starting...
INFO Listening for gRPC connections address=:1884 namespace=grpc
INFO Listening for gRPC/tls connections address=:8884 namespace=grpc
INFO Listening for HTTP connections address=:1885 namespace=web
INFO Listening for HTTPS connections address=:8885 namespace=web
WARN No cluster key configured, generated a random one key=c9c8fe8eaae03f4f044c64868ee5316e93e935552bd344fb65a6deecaa0d32df