Developer's guide#
Getting Mainflux#
Mainflux source can be found in the official Mainflux GitHub repository. You should fork this repository in order to make changes to the project. The forked version of the repository should be cloned using the following:
git clone <forked repository> $SOMEPATH/mainflux
cd $SOMEPATH/mainflux
Note: If your $SOMEPATH is equal to $GOPATH/src/github.com/MainfluxLabs/mainflux, make sure that your $GOROOT and $GOPATH do not overlap (otherwise, go modules won't work).
Building#
Prerequisites#
Make sure that you have Protocol Buffers (version 3.19.1) compiler (protoc) installed.
Go Protobuf installation instructions are here.
Go Protobuf uses C bindings, so you will need to install C++ protobuf as a prerequisite.
Mainflux uses Protocol Buffers for Go with Gadgets to generate faster marshaling and unmarshaling Go code. Protocol Buffers for Go with Gadgets installation instructions can be found here.
Go is also required, in the version declared in go.mod.
Build All Services#
Use the GNU Make tool to build all Mainflux services:
make
Build artifacts will be put in the build directory.
N.B. All Mainflux services are built as statically linked binaries. This makes them portable (they can be transferred to any platform and run there) because they contain all required libraries and do not rely on shared system libraries. This allows building FROM scratch Docker images.
Build Individual Microservice#
Individual microservices can be built with:
make <microservice_name>
For example:
make http
will build the HTTP Adapter microservice.
Building Dockers#
Dockers can be built with:
make dockers
or individually with:
make docker_<microservice_name>
For example:
make docker_http
N.B. Mainflux creates
FROM scratchdocker containers which are compact and small in size.N.B. The
things-dbandusers-dbcontainers are built from a vanilla PostgreSQL docker image downloaded from docker hub which does not persist the data when these containers are rebuilt. Thus, rebuilding of all docker containers withmake dockersor rebuilding thethings-dbandusers-dbcontainers separately withmake docker_things-dbandmake docker_users-dbrespectively, will cause data loss. All your users, things, profiles, groups will be lost! As we use this setup only for development, we don't guarantee any permanent data persistence. Though, in order to enable data retention, we have configured persistent volumes for each container that stores some data. If you want to update your Mainflux dockerized installation and want to keep your data, usemake cleandockerto clean the containers and images and keep the data (stored in docker persistent volumes) and thenmake runto update the images and the containers. Check the Cleaning up your dockerized Mainflux setup section for details. Please note that this kind of updating might not work if there are database changes.
Building Docker images for development#
In order to speed up build process, you can use commands such as:
make dockers_dev
or individually with
make docker_dev_<microservice_name>
Commands make dockers and make dockers_dev are similar. The main difference is that building images in the development mode is done on the local machine, rather than an intermediate image, which makes building images much faster. Before running this command, corresponding binary needs to be built in order to make changes visible. This can be done using make or make <service_name> command. Commands make dockers_dev and make docker_dev_<service_name> should be used only for development to speed up the process of image building. For deployment images, commands from section above should be used.
Suggested workflow#
When the project is first cloned to your system, you will need to make sure and build all of the Mainflux services.
make
make dockers_dev
As you develop and test changes, only the services related to your changes will need to be rebuilt. This will reduce compile time and create a much more enjoyable development experience.
make <microservice_name>
make docker_dev_<microservice_name>
make run
Overriding the default docker-compose configuration#
Sometimes, depending on the use case and the user's needs it might be useful to override or add some extra parameters to the docker-compose configuration. These configuration changes can be done by specifying multiple compose files with the docker-compose command line option -f as described here.
The following format of the docker-compose command can be used to extend or override the configuration:
docker-compose -f docker/docker-compose.yml -f docker/docker-compose.custom1.yml -f docker/docker-compose.custom2.yml up [-d]
In the command above each successive file overrides the previous parameters.
A practical example in our case would be to enable debugging and tracing in NATS so that we can see better how are the messages moving around.
docker-compose.nats-debugging.yml
version: "3"
services:
nats:
command: --debug -DV
When we have the override files in place, to compose the whole infrastructure including the persistent volumes we can execute:
docker-compose -f docker/docker-compose.yml -f docker/docker-compose.nats-debugging.yml up -d
Note: Please store your customizations to some folder outside the Mainflux's source folder and maybe add them to some other git repository. You can always apply your customizations by pointing to the right file using docker-compose -f ....
Cleaning up your dockerized Mainflux setup#
If you want to clean your whole dockerized Mainflux installation you can use the make pv=true cleandocker command. Please note that by default the make cleandocker command will stop and delete all of the containers and images, but NOT DELETE persistent volumes. If you want to delete the gathered data in the system (the persistent volumes) please use the following command make pv=true cleandocker (pv = persistent volumes). This form of the command will stop and delete the containers, the images and will also delete the persistent volumes.
Protobuf#
If you've made any changes to .proto files, you should call protoc command prior to compiling individual microservices.
To do this by hand, execute:
protoc --gofast_out=plugins=grpc:. *.proto
protoc --gogo_out=plugins=grpc:. broker/*.proto
A shorthand to do this via make tool is:
make proto
N.B. This must be done once at the beginning in order to generate protobuf Go structures needed for the build. However, if you don't change any of
.protofiles, this step is not mandatory, since all generated files are included in the repository (those are files with.pb.goextension).
Cross-compiling for ARM#
Mainflux can be compiled for ARM platform and run on Raspberry Pi or other similar IoT gateways, by following the instructions here or here as well as information
found here. The environment variables GOARCH=arm and GOARM=7 must be set for the compilation.
Cross-compilation for ARM with Mainflux make:
GOOS=linux GOARCH=arm GOARM=7 make
Running tests#
To run all of the tests you can execute:
make test
Dockertest is used for the tests, so to run them, you will need the Docker daemon/service running.
Installing#
Installing Go binaries is simple: just move them from build to $GOBIN (do not forget to add $GOBIN to your $PATH).
You can execute:
make install
which will do this copying of the binaries.
Deployment#
Prerequisites#
Mainflux depends on several infrastructural services, notably NATS broker and PostgreSQL database.
NATS#
Mainflux uses NATS as its central message bus. For development purposes (when not run via Docker), it expects that NATS is installed on the local system.
To do this execute:
go install github.com/nats-io/nats-server/v2@latest
This installs the nats-server binary, which can be run with:
nats-server -js
PostgreSQL#
Mainflux uses PostgreSQL to store metadata (users,orgs,groups,things and profiles entities alongside with authorization tokens).
It expects that PostgreSQL DB is installed, set up and running on the local system.
Information how to set-up (prepare) PostgreSQL database can be found here, and it is done by executing following commands:
# Create `users` and `things` databases
sudo -u postgres createdb users
sudo -u postgres createdb things
# Set-up Postgres roles
sudo su - postgres
psql -U postgres
postgres=# CREATE ROLE mainflux WITH LOGIN ENCRYPTED PASSWORD 'mainflux';
postgres=# ALTER USER mainflux WITH LOGIN ENCRYPTED PASSWORD 'mainflux';
Mainflux Services#
Running the Mainflux microservices can be tricky, as there are many of them and each one requires configuration through environment variables.
The whole system (set of microservices) can be run with one command:
make rundev
which will properly configure and run all microservices.
N.B.
make rundevactually calls helper scriptscripts/run.sh, so you can inspect this script for the details.
Events#
Mainflux uses Redis Streams as an event log for event sourcing.
The things service publishes events to the mainflux.things stream, and the auth service publishes
events to the mainflux.auth stream.
Besides the operation-specific fields described below, every event carries the following fields:
- operation - the operation that produced the event,
- occurred_at - the time of the operation in RFC 3339 format,
- evt_org_id and evt_group_id - the org and the group to which the event belongs, empty when not applicable,
- jwt_identity_user_id and jwt_identity_user_email - the user who performed the operation, present only for user-initiated operations.
Things Service#
For every operation that has side effects (that is changing service state) things
service will generate new event and publish it to Redis Stream called mainflux.things.
Every event has its own event ID that is automatically generated and operation
field that can have one of the following values:
- thing.create for thing creation,
- thing.update for thing update,
- thing.update_group_and_profile for thing group and profile reassignment,
- thing.remove for thing removal,
- profile.create for profile creation,
- profile.update for profile update,
- profile.remove for profile removal,
- group.remove for group removal.
The auth service publishes org.create and org.remove events.
By fetching and processing these events you can reconstruct things service state.
If you store some of your custom data in metadata field, this is the perfect
way to fetch it and process it. If you want to integrate through
docker-compose.yml
you can use the mainfluxlabs-es-redis container. Connect to it and consume events
from the Redis Stream named mainflux.things.
Thing create event#
Whenever thing is created, things service will generate new create event. This
event will have the following format:
1) "1555334740911-0"
2) 1) "id"
2) "1c36273a-94ea-4802-84d6-a51de140112a"
3) "group_id"
4) "2c36273a-94ea-4802-84d6-a51de140112b"
5) "profile_id"
6) "3c36273a-94ea-4802-84d6-a51de140112c"
7) "operation"
8) "thing.create"
9) "name"
10) "d0"
11) "metadata"
12) "{}"
As you can see from this example, every odd field represents field name while every
even field represents field value. This is standard event format for Redis Streams.
If you want to extract metadata field from this event, you'll have to read it as
string first and then you can deserialize it to some structured format.
Thing update event#
Whenever thing instance is updated, things service will generate new update event.
This event will have the following format:
1) "1555336161544-0"
2) 1) "id"
2) "3c36273a-94ea-4802-84d6-a51de140112e"
3) "profile_id"
4) "3c36273a-94ea-4802-84d6-a51de140112c"
5) "operation"
6) "thing.update"
7) "name"
8) "weio"
Note that thing update event will contain only those fields that were updated using update endpoint.
Thing remove event#
Whenever thing instance is removed from the system, things service will generate and
publish new remove event. This event will have the following format:
1) 1) "1555339313003-0"
2) 1) "id"
2) "3c36273a-94ea-4802-84d6-a51de140112e"
3) "operation"
4) "thing.remove"
Profile create event#
Whenever profile instance is created, things service will generate and publish new
create event. This event will have the following format:
1) "1555334740918-0"
2) 1) "id"
2) "16fb2748-8d3b-4783-b272-bb5f4ad4d661"
3) "group_id"
4) "2c36273a-94ea-4802-84d6-a51de140112b"
5) "operation"
6) "profile.create"
7) "name"
8) "p1"
Profile update event#
Whenever profile instance is updated, things service will generate and publish new
update event. This event will have the following format:
1) "1555338870341-0"
2) 1) "id"
2) "d9d8f31b-f8d4-49c5-b943-6db10d8e2949"
3) "name"
4) "profile1"
5) "operation"
6) "profile.update"
Note that update profile event will contain only those fields that were updated using update profile endpoint.
Profile remove event#
Whenever profile instance is removed from the system, things service will generate and
publish new remove event. This event will have the following format:
1) 1) "1555339429661-0"
2) 1) "id"
2) "d9d8f31b-f8d4-49c5-b943-6db10d8e2949"
3) "operation"
4) "profile.remove"
Note: Every one of these events will omit fields that were not used or are not relevant for specific operation. Also, field ordering is not guaranteed, so DO NOT rely on it.