Skip to content

Commit

Permalink
Read mqtt user and password from environment
Browse files Browse the repository at this point in the history
Environment variables override config variables, if they exist
  • Loading branch information
genkobar committed Sep 13, 2021
1 parent 53b6d35 commit df2c852
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ func main() {
if err != nil {
logger.Fatal("Could not load config", zap.Error(err))
}

mqtt_user := os.Getenv("MQTT2PROM_MQTT_USER")
mqtt_password := os.Getenv("MQTT2PROM_MQTT_PASSWORD")

if mqtt_user != "" {
cfg.MQTT.User = mqtt_user
}
if mqtt_password != "" {
cfg.MQTT.Password = mqtt_password
}

mqttClientOptions := mqtt.NewClientOptions()
mqttClientOptions.AddBroker(cfg.MQTT.Server).SetCleanSession(true)
mqttClientOptions.SetAutoReconnect(true)
Expand Down

7 comments on commit df2c852

@hikhvar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Momeno this looks like a nice addition? Do you mind to create a PR?

@genkobar
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hikhvar, sure! I was planning on making a PR, I just hadn't gotten around to it :)

@genkobar
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hikhvar Actually, I ran into an issue where I was not able to get a container built from the head of the master branch to read the configuration file on launch, even though it worked with the container image at ghcr.io/hikhvar/mqtt2prometheus:latest.

I'm not familiar enough with Go to debug the issue quickly and I ran out of time, so I'm not sure what the cause could be.

I did these steps:

make container
docker run -it -v "$(pwd)/examples/config.yaml:/config.yaml" -p 9641:9641 mqtt2prometheus:latest

And got this error:

2021-09-20T10:27:50Z    fatal   cmd/mqtt2prometheus.go:80       Could not load config   {"error": "open config.yaml: no such file or directory"}
main.main
        /build/mqtt2prometheus/cmd/mqtt2prometheus.go:80
runtime.main
        /usr/local/go/src/runtime/proc.go:225

This stopped me from testing the environment variable load so I abandoned the fork, at least for now.

@hikhvar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

the problem is, that the working directory in the container is set to /home/nonroot. Hence to make this work you must either mount the config to /home/nonroot/config.yaml or set the current working directory to /.

# First option 
docker run -it -v "$(pwd)/examples/config.yaml:/home/nonroot/config.yaml" -p 9641:9641 mqtt2prometheus:latest

# Second option
docker run -it -v "$(pwd)/examples/config.yaml:/config.yaml" -w "/" -p 9641:9641 mqtt2prometheus:latest

I will change the working directory upstream to /. This is more intuitive.

@hikhvar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hikhvar@4418b5a fixed this upstream.

@genkobar
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I see, the working directory was the issue. I'll have the PR ready shortly.

@genkobar
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've posted a PR here: hikhvar#70

Please sign in to comment.