Flow¶
Grafanarmadillo includes tools to help with exporting and importing multiple templates. For example: You might have a dashboard for developers to test new alerts and visualisations titled “MySystem TEST”. When the devs are happy, they want an instance of the dashboard created for each of 3 deployments with the correct names and deployment_ids. They also want to be able to check the template into git so they can revert to a previous version.
Grafanarmadillo’s Flow makes this easy! The CLI is good for simple scenarios or for integration with shell scripts. Flow can be a better choice for more complicated deployments or for integrating with other systems.
Create a
Flowobject. Specify the locations where templates and objects will be stored. Add flowables to theFlowfor the objects you want to capture templates. Then, run the flow. The following code defines a functionexport_templatesto capture the template and save it in the file “/dev/MySystem.json”; and a functionimport_templatesto expand that template for each of the deployments.#!/usr/bin/env python3 from pathlib import Path import click from grafanarmadillo.cmd import grafanarmadillo as gcmd from grafanarmadillo.cmd import make_grafana from grafanarmadillo.flow import Dashboard, FileStore, Flow, GrafanaStore from grafanarmadillo.templator import make_mapping_templator from grafanarmadillo.util import load_data def export_templates(grafana, basedir: Path): """Export templates from the dev instance.""" filestore = FileStore(basedir) grafanastore = GrafanaStore(grafana) mapping = load_data("file://tests/usage/mapping.json") templator = make_mapping_templator(mapping, "dev", "template") # define flows flow = Flow( store_obj=grafanastore, store_tmpl=filestore, flows=[ Dashboard( name_obj="/dev0/MySystem TEST", name_tmpl="/dev0/MySystem", templator=templator ) ] ) # run flows result = flow.obj_to_tmpl() # react to flows if result.failures: result.raise_first() def import_templates(grafana, basedir: Path): """Import your templates to the prod instance.""" filestore = FileStore(basedir) grafanastore = GrafanaStore(grafana) mapping = load_data("file://tests/usage/mapping.json") deployments = {"east", "west", "north"} # define flows flow = Flow( store_obj=grafanastore, store_tmpl=filestore, flows=[ Dashboard( name_tmpl="/dev0/MySystem", name_obj=f"/{deployment}0/my_system", templator=(make_mapping_templator(mapping, env_grafana=deployment, env_template="template")) ) for deployment in deployments ] ) # run flows result = flow.tmpl_to_obj() # react to flows if result.failures: result.raise_first() # Add a custom command as an extension of the provided cli by creating a click group or command @gcmd.group() def my_system(): """Wire your commands into the main grafanarmadillo command.""" @my_system.command(name="export") @click.option("--basedir") @click.pass_context def _export_templates(ctx, basedir): """Export templates from the dev instance.""" gfn = make_grafana(ctx.obj["cfg"]) export_templates(gfn, Path(basedir)) @my_system.command(name="import") @click.option("--basedir") @click.pass_context def _import_templates(ctx, basedir): """Import your templates to the prod instance.""" gfn = make_grafana(ctx.obj["cfg"]) import_templates(gfn, Path(basedir)) if __name__ == "__main__": gcmd(auto_envvar_prefix="GRAFANARMADILLO")