├── .fixtures.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── CONTRIBUTORS ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── lib └── puppet │ ├── provider │ ├── grafana.rb │ ├── grafana_dashboard │ │ └── grafana.rb │ └── grafana_datasource │ │ └── grafana.rb │ └── type │ ├── grafana_dashboard.rb │ └── grafana_datasource.rb ├── manifests ├── config.pp ├── init.pp ├── install.pp ├── params.pp └── service.pp ├── metadata.json ├── spec ├── acceptance │ ├── class_spec.rb │ └── nodesets │ │ ├── centos-64-x64.yml │ │ ├── default.yml │ │ └── ubuntu-server-12042-x64.yml ├── classes │ ├── coverage_spec.rb │ └── grafana_spec.rb ├── spec.opts ├── spec_helper.rb ├── spec_helper_acceptance.rb └── unit │ └── puppet │ └── type │ ├── grafana_dashboard_type_spec.rb │ └── grafana_datasource_type_spec.rb ├── templates └── config.ini.erb └── tests └── init.pp /.fixtures.yml: -------------------------------------------------------------------------------- 1 | fixtures: 2 | repositories: 3 | stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git" 4 | archive: "https://github.com/voxpupuli/puppet-archive.git" 5 | docker: "https://github.com/garethr/garethr-docker.git" 6 | wget: "https://github.com/maestrodev/puppet-wget.git" 7 | apt: "https://github.com/puppetlabs/puppetlabs-apt.git" 8 | symlinks: 9 | grafana: "#{source_dir}" 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .*.sw? 2 | pkg 3 | spec/fixtures 4 | .rspec_system 5 | .vagrant 6 | log/ 7 | 8 | # for a library or gem, you might want to ignore these files since the code is 9 | # intended to run in multiple environments; otherwise, check them in: 10 | Gemfile.lock 11 | .ruby-version 12 | .ruby-gemset 13 | 14 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 15 | .rvmrc 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | bundler_args: --without development 4 | before_install: rm Gemfile.lock || true 5 | rvm: 6 | - 1.8.7 7 | - 1.9.3 8 | - 2.0.0 9 | - 2.1.0 10 | script: bundle exec rake test 11 | env: 12 | - PUPPET_GEM_VERSION="~> 2.7.0" 13 | - PUPPET_GEM_VERSION="~> 3.2.0" 14 | - PUPPET_GEM_VERSION="~> 3.3.0" 15 | - PUPPET_GEM_VERSION="~> 3.4.0" 16 | - PUPPET_GEM_VERSION="~> 3.5.0" 17 | - PUPPET_GEM_VERSION="~> 3.6.0" 18 | - PUPPET_GEM_VERSION="~> 3.7.0" 19 | matrix: 20 | allow_failures: 21 | - rvm: 1.8.7 22 | exclude: 23 | - rvm: 1.9.3 24 | env: PUPPET_VERSION="~> 2.7.0" 25 | - rvm: 2.0.0 26 | env: PUPPET_VERSION="~> 2.7.0" 27 | - rvm: 2.1.0 28 | env: PUPPET_VERSION="~> 2.7.0" 29 | - rvm: 2.1.0 30 | env: PUPPET_VERSION="~> 3.2.0" 31 | - rvm: 2.1.0 32 | env: PUPPET_VERSION="~> 3.3.0" 33 | - rvm: 2.1.0 34 | env: PUPPET_VERSION="~> 3.4.0" 35 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 2.5.0 (2015-10-31) 2 | 3 | **Enhancements** 4 | - Support for [Grafana 2.5](http://grafana.org/blog/2015/10/28/Grafana-2-5-Released.html). This is just a version bump to reflect that Grafana 2.5 is now installed by default 5 | - [PR #58](https://github.com/bfraser/puppet-grafana/pull/58) Sort ```cfg``` keys so ```config.ini``` content is not updated every Puppet run 6 | 7 | **Fixes** 8 | - [Issue #52](https://github.com/bfraser/puppet-grafana/issues/52) Version logic moved to ```init.pp``` so overriding the version of Grafana to install works as intended 9 | 10 | **Behind The Scenes** 11 | 12 | - [PR #59](https://github.com/bfraser/puppet-grafana/pull/59) More specific version requirements in ```metadata.json``` due to use of ```contain``` function 13 | - [PR #61](https://github.com/bfraser/puppet-grafana/pull/61) Fixed typos in ```metadata.json``` 14 | 15 | # 2.1.0 (2015-08-07) 16 | 17 | **Enhancements** 18 | - Support for [Grafana 2.1](http://grafana.org/blog/2015/08/04/Grafana-2-1-Released.html) 19 | - [Issue #40](https://github.com/bfraser/puppet-grafana/issues/40) Support for [LDAP integration](http://docs.grafana.org/v2.1/installation/ldap/) 20 | - [PR #34](https://github.com/bfraser/puppet-grafana/pull/34) Support for 'repo' install method to install packages from [packagecloud](https://packagecloud.io/grafana) repositories 21 | - Addition of boolean parameter ```manage_package_repo``` to control whether the module will manage the package repository when using the 'repo' install method. See README.md for details 22 | - [PR #39](https://github.com/bfraser/puppet-grafana/pull/39) Ability to ensure a specific package version is installed when using the 'repo' install method 23 | 24 | **Fixes** 25 | - [Issue #37](https://github.com/bfraser/puppet-grafana/issues/37) Archive install method: check if user and service are already defined before attempting to define them 26 | - [Issue #42](https://github.com/bfraser/puppet-grafana/issues/42) Package versioning for RPM / yum systems 27 | - [Issue #45](https://github.com/bfraser/puppet-grafana/issues/45) Fix resource dependency issues when ```manage_package_repo``` is false 28 | 29 | **Behind The Scenes** 30 | - Use 40 character GPG key ID for packagecloud apt repository 31 | 32 | # 2.0.2 (2015-04-30) 33 | 34 | **Enhancements** 35 | - Support for Grafana 2.0. Users of Grafana 1.x should stick to version 1.x of the Puppet module 36 | - Support 'archive', 'docker' and 'package' install methods 37 | - Ability to supply a hash of parameters to the Docker container when using 'docker' install method 38 | - [PR #24](https://github.com/bfraser/puppet-grafana/pull/24) Ability to configure Grafana using configuration hash parameter ```cfg``` 39 | 40 | **Behind The Scenes** 41 | - Update module operatingsystem support, tags, Puppet requirements 42 | - Tests for 'archive' and 'package' install methods 43 | 44 | # 1.0.1 (2015-02-27) 45 | 46 | **Enhancements** 47 | - New parameter for Grafana admin password 48 | 49 | **Fixes** 50 | - Package install method now makes use of install_dir for config.js path 51 | 52 | **Behind The Scenes** 53 | - Add archive module to .fixtures.yml 54 | - Unquote booleans to make lint happy 55 | - Fix license identifier and unbounded dependencies in module metadata 56 | - Allow Travis to fail on Ruby 1.8.7 57 | - More Puppet versions are tested by Travis 58 | 59 | # 1.0.0 (2014-12-16) 60 | 61 | **Enhancements** 62 | - Add max_search_results parameter 63 | - Install Grafana 1.9.0 by default 64 | 65 | **Documentation** 66 | - Add download_url and install_method parameters to README 67 | 68 | **Behind The Scenes** 69 | - [Issue #6](https://github.com/bfraser/puppet-grafana/issues/6) Replace gini/archive dependency with camptocamp/archive 70 | - Addition of CHANGELOG 71 | - Style fixes 72 | - Removal of vagrant-wrapper gem 73 | - Fancy badges for build status 74 | 75 | # 0.2.2 (2014-10-27) 76 | 77 | **Enhancements** 78 | - Add default_route parameter to manage start dashboard 79 | 80 | **Fixes** 81 | - Symlink behavior 82 | 83 | **Behind The Scenes** 84 | - [Issue #9](https://github.com/bfraser/puppet-grafana/issues/9) Remove stdlib inclusion from manifest 85 | 86 | # 0.2.1 (2014-10-14) 87 | 88 | **Enhancements** 89 | - Support for multiple datasources 90 | - Install Grafana 1.8.1 by default 91 | 92 | **Behind The Scenes** 93 | - Added RSpec tests 94 | - Add stdlib as a module dependency 95 | - Add operating system compatibility 96 | 97 | # 0.1.3 (2014-07-03) 98 | 99 | **Enhancements** 100 | - Added support for InfluxDB 101 | 102 | # 0.1.2 (2014-06-30) 103 | 104 | First release on the Puppet Forge 105 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | This module has grown over time based on a range of contributions from 2 | people using it. If you follow these contributing guidelines your patch 3 | will likely make it into a release a little quicker. 4 | 5 | 6 | ## Contributing 7 | 8 | 1. Fork the repo. 9 | 10 | 2. Run the tests. We only take pull requests with passing tests, and 11 | it's great to know that you have a clean slate 12 | 13 | 3. Add a test for your change. Only refactoring and documentation 14 | changes require no new tests. If you are adding functionality 15 | or fixing a bug, please add a test. 16 | 17 | 4. Make the test pass. 18 | 19 | 5. Push to your fork and submit a pull request. 20 | 21 | 22 | ## Dependencies 23 | 24 | The testing and development tools have a bunch of dependencies, 25 | all managed by [bundler](http://bundler.io/) according to the 26 | [Puppet support matrix](http://docs.puppetlabs.com/guides/platforms.html#ruby-versions). 27 | 28 | By default the tests use a baseline version of Puppet. 29 | 30 | If you have Ruby 2.x or want a specific version of Puppet, 31 | you must set an environment variable such as: 32 | 33 | export PUPPET_VERSION="~> 3.2.0" 34 | 35 | Install the dependencies like so... 36 | 37 | bundle install 38 | 39 | ## Syntax and style 40 | 41 | The test suite will run [Puppet Lint](http://puppet-lint.com/) and 42 | [Puppet Syntax](https://github.com/gds-operations/puppet-syntax) to 43 | check various syntax and style things. You can run these locally with: 44 | 45 | bundle exec rake lint 46 | bundle exec rake syntax 47 | 48 | ## Running the unit tests 49 | 50 | The unit test suite covers most of the code, as mentioned above please 51 | add tests if you're adding new functionality. If you've not used 52 | [rspec-puppet](http://rspec-puppet.com/) before then feel free to ask 53 | about how best to test your new feature. Running the test suite is done 54 | with: 55 | 56 | bundle exec rake spec 57 | 58 | Note also you can run the syntax, style and unit tests in one go with: 59 | 60 | bundle exec rake test 61 | 62 | ## Integration tests 63 | 64 | The unit tests just check the code runs, not that it does exactly what 65 | we want on a real machine. For that we're using 66 | [beaker](https://github.com/puppetlabs/beaker). 67 | 68 | This fires up a new virtual machine (using vagrant) and runs a series of 69 | simple tests against it after applying the module. You can run this 70 | with: 71 | 72 | bundle exec rake acceptance 73 | 74 | This will run the tests on an Ubuntu 12.04 virtual machine. You can also 75 | run the integration tests against Centos 6.5 with. 76 | 77 | RS_SET=centos-64-x64 bundle exec rake acceptances 78 | 79 | If you don't want to have to recreate the virtual machine every time you 80 | can use `BEAKER_DESTROY=no` and `BEAKER_PROVISION=no`. On the first run you will 81 | at least need `BEAKER_PROVISION` set to yes (the default). The Vagrantfile 82 | for the created virtual machines will be in `.vagrant/beaker_vagrant_fies`. 83 | 84 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | bfraser 2 | rplessl 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | group :test do 4 | gem "rake" 5 | gem "puppet", ENV['PUPPET_VERSION'] || '~> 3.6.0' 6 | gem 'metadata-json-lint' 7 | gem "puppet-lint" 8 | gem "rspec-puppet", :git => 'https://github.com/rodjek/rspec-puppet.git' 9 | gem "puppet-syntax" 10 | gem "puppetlabs_spec_helper" 11 | gem "toml" 12 | end 13 | 14 | group :development do 15 | gem "travis" 16 | gem "travis-lint" 17 | gem "beaker" 18 | gem "beaker-rspec" 19 | gem "puppet-blacksmith" 20 | gem "guard-rake" 21 | end 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #grafana 2 | 3 | [![Puppet Forge](http://img.shields.io/puppetforge/v/bfraser/grafana.svg)](https://forge.puppetlabs.com/bfraser/grafana) 4 | [![Build Status](http://img.shields.io/travis/bfraser/puppet-grafana.svg)](http://travis-ci.org/bfraser/puppet-grafana) 5 | 6 | ####Table of Contents 7 | 8 | 1. [Overview](#overview) 9 | 2. [Module Description](#module-description) 10 | 3. [Setup](#setup) 11 | * [Beginning with Grafana](#beginning-with-grafana) 12 | 4. [Usage](#usage) 13 | * [Classes and Defined Types](#classes-and-defined-types) 14 | 5. [Limitations](#limitations) 15 | 6. [Copyright and License](#copyright-and-license) 16 | 17 | ##Overview 18 | 19 | This module installs Grafana, a dashboard and graph editor for Graphite, InfluxDB and OpenTSDB. 20 | 21 | ##Module Description 22 | 23 | Version 2.x of this module is designed to work with version 2.x of Grafana. If you would like to continue to use Grafana 1.x, please use version 1.x of this module. 24 | 25 | ##Setup 26 | 27 | This module will: 28 | 29 | * Install Grafana using your preferred method: package (default), Docker container, or tar archive 30 | * Allow you to override the version of Grafana to be installed, and / or the package source 31 | * Perform basic configuration of Grafana 32 | 33 | ###Beginning with Grafana 34 | 35 | To install Grafana with the default parameters: 36 | 37 | ```puppet 38 | class { 'grafana': } 39 | ``` 40 | 41 | This assumes that you want to install Grafana using the 'package' method. To establish customized parameters: 42 | 43 | ```puppet 44 | class { 'grafana': 45 | install_method => 'docker', 46 | } 47 | ``` 48 | 49 | ##Usage 50 | 51 | ###Classes and Defined Types 52 | 53 | ####Class: `grafana` 54 | 55 | The Grafana module's primary class, `grafana`, guides the basic setup of Grafana on your system. 56 | 57 | ```puppet 58 | class { 'grafana': } 59 | ``` 60 | **Parameters within `grafana`:** 61 | 62 | #####`archive_source` 63 | 64 | The download location of a tarball to use with the 'archive' install method. Defaults to the URL of the latest version of Grafana available at the time of module release. 65 | 66 | #####`cfg_location` 67 | 68 | Configures the location to which the Grafana configuration is written. The default location is '/etc/grafana/grafana.ini'. 69 | 70 | #####`cfg` 71 | 72 | Manages the Grafana configuration file. Grafana comes with its own default settings in a different configuration file (/opt/grafana/current/conf/defaults.ini), therefore this module does not supply any defaults. 73 | 74 | This parameter only accepts a hash as its value. Keys with hashes as values will generate sections, any other values are just plain values. The example below will result in... 75 | 76 | ```puppet 77 | class { 'grafana': 78 | cfg => { 79 | app_mode => 'production', 80 | server => { 81 | http_port => 8080, 82 | }, 83 | database => { 84 | type => 'sqlite3', 85 | host => '127.0.0.1:3306', 86 | name => 'grafana', 87 | user => 'root', 88 | password => '', 89 | }, 90 | users => { 91 | allow_sign_up => false, 92 | }, 93 | }, 94 | } 95 | ``` 96 | 97 | ...the following Grafana configuration: 98 | 99 | ```ini 100 | # This file is managed by Puppet, any changes will be overwritten 101 | 102 | app_mode = production 103 | 104 | [server] 105 | http_port = 8080 106 | 107 | [database] 108 | type = sqlite3 109 | host = 127.0.0.1:3306 110 | name = grafana 111 | user = root 112 | password = 113 | 114 | [users] 115 | allow_sign_up = false 116 | ``` 117 | 118 | Some minor notes: 119 | 120 | - If you want empty values, just use an empty string. 121 | - Keys that contains dots (like auth.google) need to be quoted. 122 | - The order of the keys in this hash is the same as they will be written to the configuration file. So settings that do not fall under a section will have to come before any sections in the hash. 123 | 124 | ####`ldap_cfg` 125 | 126 | #####TOML note 127 | This option **requires** the [toml](https://github.com/toml-lang/toml) gem. Either install the gem using puppet's native gem provider, [puppetserver_gem](https://forge.puppetlabs.com/puppetlabs/puppetserver_gem), [pe_gem](https://forge.puppetlabs.com/puppetlabs/pe_gem), [pe_puppetserver_gem](https://forge.puppetlabs.com/puppetlabs/pe_puppetserver_gem), or manually using one of the following: 128 | ``` 129 | # apply or puppet-master 130 | gem install toml 131 | # PE apply 132 | /opt/puppet/bin/gem install toml 133 | # AIO or PE puppetserver 134 | /opt/puppet/bin/puppetserver gem install toml 135 | ``` 136 | 137 | #####cfg note 138 | This option by itself is not sufficient to enable LDAP configuration as it must be enabled in the main configuration file. Enable it in cfg with: 139 | 140 | ``` 141 | 'auth.ldap' => { 142 | enabled => 'true', 143 | config_file => '/etc/grafana/ldap.toml', 144 | }, 145 | ``` 146 | 147 | ####Integer note 148 | Puppet may convert integers into strings while parsing the hash and converting into toml. This can be worked around by appending 0 to an integer. 149 | 150 | Example: 151 | ``` 152 | port => 636+0, 153 | ``` 154 | 155 | Manages the Grafana LDAP configuration file. This hash is directly translated into the corresponding TOML file, allowing for full flexibility in generating the configuration. 156 | 157 | See the [LDAP documentation](http://docs.grafana.org/v2.1/installation/ldap/) for more information. 158 | 159 | ####Example LDAP config 160 | 161 | ``` 162 | ldap_cfg => { 163 | servers => [ 164 | { host => 'ldapserver1.domain1.com', 165 | port => 636+0, 166 | use_ssl => true, 167 | search_filter => '(sAMAccountName=%s)', 168 | search_base_dns => [ 'dc=domain1,dc=com' ], 169 | bind_dn => 'user@domain1.com', 170 | bind_password => 'passwordhere', 171 | }, 172 | ], 173 | 'servers.attributes' => { 174 | name => 'givenName', 175 | surname => 'sn', 176 | username => 'sAMAccountName', 177 | member_of => 'memberOf', 178 | email => 'email', 179 | } 180 | }, 181 | ``` 182 | 183 | 184 | #####`container_cfg` 185 | 186 | Boolean to control whether a configuration file should be generated when using the 'docker' install method. If 'true', use the 'cfg' and 'cfg_location' parameters to control creation of the file. Defaults to false. 187 | 188 | #####`container_params` 189 | 190 | A hash of parameters to use when creating the Docker container. For use with the 'docker' install method. Refer to documentation of the 'docker::run' resource in the [garethr-docker](https://github.com/garethr/garethr-docker) module for details of available parameters. Defaults to: 191 | 192 | ```puppet 193 | container_params => { 194 | 'image' => 'grafana/grafana:latest', 195 | 'ports' => '3000:3000' 196 | } 197 | ``` 198 | 199 | #####`data_dir` 200 | 201 | The directory Grafana will use for storing its data. Defaults to '/var/lib/grafana'. 202 | 203 | #####`install_dir` 204 | 205 | The installation directory to be used with the 'archive' install method. Defaults to '/usr/share/grafana'. 206 | 207 | #####`install_method` 208 | 209 | Controls which method to use for installing Grafana. Valid options are: 'archive', 'docker', 'repo' and 'package'. The default is 'package'. If you wish to use the 'docker' installation method, you will need to include the 'docker' class in your node's manifest / profile. If you wish to use the 'repo' installation method, you can control whether the official Grafana repositories will be used. See `manage_package_repo` below for details. 210 | 211 | #####`manage_package_repo` 212 | 213 | Boolean. When using the 'repo' installation method, controls whether the official Grafana repositories are enabled on your host. If true, the official Grafana repositories will be enabled. If false, the module assumes you are managing your own package repository and will not set one up for you. Defaults to true. 214 | 215 | #####`package_name` 216 | 217 | The name of the package managed with the 'package' install method. Defaults to 'grafana'. 218 | 219 | #####`package_source` 220 | 221 | The download location of a package to be used with the 'package' install method. Defaults to the URL of the latest version of Grafana available at the time of module release. 222 | 223 | #####`rpm_iteration` 224 | 225 | Used when installing Grafana from package ('package' or 'repo' install methods) on Red Hat based systems. Defaults to '1'. It should not be necessary to change this in most cases. 226 | 227 | #####`service_name` 228 | 229 | The name of the service managed with the 'archive' and 'package' install methods. Defaults to 'grafana-server'. 230 | 231 | #####`version` 232 | 233 | The version of Grafana to install and manage. Defaults to the latest version of Grafana available at the time of module release. 234 | 235 | ##Advanced usage: 236 | 237 | The archive install method will create the user and a "command line" service by default. 238 | There are no extra parameters to manage user/service for archive. However, both check to see if they are defined before defining. This way you can create your own user and service with your own specifications. (sort of overriding) 239 | The service can be a bit tricky, in this example below, the class sensu_install::grafana::service creates a startup script and a service{'grafana-server':} 240 | 241 | Example: 242 | ```puppet 243 | user { 'grafana': 244 | ensure => present, 245 | uid => '1234', 246 | } 247 | -> 248 | class { 'grafana': 249 | install_method => 'archive', 250 | } 251 | 252 | include sensu_install::grafana::service 253 | 254 | # run your service after install/config but before grafana::service 255 | Class[::grafana::install] 256 | -> 257 | Class[sensu_install::grafana::service] 258 | -> 259 | Class[::grafana::service] 260 | 261 | ``` 262 | 263 | ####Custom Types and Providers 264 | 265 | The module includes two custom types: `grafana_dashboard` and `grafana_datasource` 266 | 267 | #####`grafana_dashboard` 268 | 269 | In order to use the dashboard resource, add the following to your manifest: 270 | 271 | ```puppet 272 | grafana_dashboard { 'example_dashboard': 273 | grafana_url => 'http://localhost:3000', 274 | grafana_user => 'admin', 275 | grafana_password => '5ecretPassw0rd', 276 | content => template('path/to/exported/file.json'), 277 | } 278 | ``` 279 | 280 | `content` must be valid JSON, and is parsed before imported. 281 | `grafana_user` and `grafana_password` are optional, and required when authentication is enabled in Grafana. 282 | 283 | #####`grafana_datasource` 284 | 285 | In order to use the datasource resource, add the following to your manifest: 286 | 287 | ```puppet 288 | grafana_datasource { 'influxdb': 289 | grafana_url => 'http://localhost:3000', 290 | grafana_user => 'admin', 291 | grafana_password => '5ecretPassw0rd', 292 | type => 'influxdb', 293 | url => 'http://localhost:8086', 294 | user => 'admin', 295 | password => '1nFlux5ecret', 296 | database => 'graphite', 297 | access_mode => 'proxy', 298 | is_default => true, 299 | json_data => template('path/to/additional/config.json'), 300 | } 301 | ``` 302 | 303 | Available types are: influxdb, elasticsearch, graphite, kairosdb, opentsdb, prometheus 304 | 305 | Access mode determines how Grafana connects to the datasource, either `direct` from the browser, or `proxy` to send requests via grafana. 306 | 307 | Authentication is optional, as is `database`; additional `json_data` can be provided to allow custom configuration options. 308 | 309 | 310 | ##Limitations 311 | 312 | This module has been tested on Ubuntu 14.04, using each of the 'archive', docker' and 'package' installation methods. Other configurations should work with minimal, if any, additional effort. 313 | 314 | ##Copyright and License 315 | 316 | Copyright (C) 2015 Bill Fraser 317 | 318 | Bill can be contacted at: fraser@pythian.com 319 | 320 | Licensed under the Apache License, Version 2.0 (the "License"); 321 | you may not use this file except in compliance with the License. 322 | You may obtain a copy of the License at 323 | 324 | http://www.apache.org/licenses/LICENSE-2.0 325 | 326 | Unless required by applicable law or agreed to in writing, software 327 | distributed under the License is distributed on an "AS IS" BASIS, 328 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 329 | See the License for the specific language governing permissions and 330 | limitations under the License. 331 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'puppetlabs_spec_helper/rake_tasks' 2 | require 'puppet-lint/tasks/puppet-lint' 3 | require 'puppet-syntax/tasks/puppet-syntax' 4 | 5 | # These two gems aren't always present, for instance 6 | # on Travis with --without development 7 | begin 8 | require 'puppet_blacksmith/rake_tasks' 9 | rescue LoadError 10 | end 11 | 12 | exclude_paths = [ 13 | "pkg/**/*", 14 | "vendor/**/*", 15 | "spec/**/*", 16 | ] 17 | 18 | Rake::Task[:lint].clear 19 | PuppetLint::RakeTask.new :lint do |config| 20 | # Pattern of files to ignore 21 | config.ignore_paths = exclude_paths 22 | 23 | # List of checks to disable 24 | config.disable_checks = [ 25 | '80chars', 26 | 'autoloader_layout', 27 | 'class_parameter_defaults', 28 | 'class_inherits_from_params_class' 29 | ] 30 | 31 | # Should the task fail if there were any warnings, defaults to false 32 | config.fail_on_warnings = true 33 | 34 | # Print out the context for the problem, defaults to false 35 | config.with_context = true 36 | 37 | # Format string for puppet-lint's output (see the puppet-lint help output 38 | # for details 39 | config.log_format = "%{path}:%{linenumber}:%{check}:%{KIND}:%{message}" 40 | 41 | # Compare module layout relative to the module root 42 | # config.relative = true 43 | end 44 | 45 | PuppetSyntax.exclude_paths = exclude_paths 46 | 47 | desc "Run acceptance tests" 48 | RSpec::Core::RakeTask.new(:acceptance) do |t| 49 | t.pattern = 'spec/acceptance' 50 | end 51 | 52 | desc "Run syntax, lint, and spec tests." 53 | task :test => [ 54 | :syntax, 55 | :lint, 56 | :spec, 57 | ] 58 | -------------------------------------------------------------------------------- /lib/puppet/provider/grafana.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may 4 | # not use this file except in compliance with the License. You may obtain 5 | # a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | # License for the specific language governing permissions and limitations 13 | # under the License. 14 | # 15 | require 'cgi' 16 | require 'json' 17 | require 'net/http' 18 | 19 | class Puppet::Provider::Grafana < Puppet::Provider 20 | # Helper methods 21 | def grafana_host 22 | unless @grafana_host 23 | @grafana_host = URI.parse(resource[:grafana_url]).host 24 | end 25 | @grafana_host 26 | end 27 | 28 | def grafana_port 29 | unless @grafana_port 30 | @grafana_port = URI.parse(resource[:grafana_url]).port 31 | end 32 | @grafana_port 33 | end 34 | 35 | def grafana_scheme 36 | unless @grafana_scheme 37 | @grafana_scheme = URI.parse(resource[:grafana_url]).scheme 38 | end 39 | @grafana_scheme 40 | end 41 | 42 | # Return a Net::HTTP::Response object 43 | def send_request(operation="GET", path="", data=nil, search_path={}) 44 | request = nil 45 | encoded_search = "" 46 | 47 | if URI.respond_to?(:encode_www_form) 48 | encoded_search = URI.encode_www_form(search_path) 49 | else 50 | # Ideally we would have use URI.encode_www_form but it isn't 51 | # available with Ruby 1.8.x that ships with CentOS 6.5. 52 | encoded_search = search_path.to_a.map do |x| 53 | x.map{|y| CGI.escape(y.to_s)}.join('=') 54 | end 55 | encoded_search = encoded_search.join('&') 56 | end 57 | uri = URI.parse("%s://%s:%d%s?%s" % [ 58 | self.grafana_scheme, self.grafana_host, self.grafana_port, 59 | path, encoded_search]) 60 | 61 | case operation.upcase 62 | when 'POST' 63 | request = Net::HTTP::Post.new(uri.request_uri) 64 | request.body = data.to_json() 65 | when 'PUT' 66 | request = Net::HTTP::Put.new(uri.request_uri) 67 | request.body = data.to_json() 68 | when 'GET' 69 | request = Net::HTTP::Get.new(uri.request_uri) 70 | when 'DELETE' 71 | request = Net::HTTP::Delete.new(uri.request_uri) 72 | else 73 | raise Puppet::Error, "Unsupported HTTP operation '%s'" % operation 74 | end 75 | 76 | request.content_type = 'application/json' 77 | if resource[:grafana_user] and resource[:grafana_user] 78 | request.basic_auth resource[:grafana_user], resource[:grafana_password] 79 | end 80 | 81 | return Net::HTTP.start(self.grafana_host, self.grafana_port, 82 | :use_ssl => self.grafana_scheme == 'https', 83 | :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http| 84 | http.request(request) 85 | end 86 | end 87 | end 88 | 89 | -------------------------------------------------------------------------------- /lib/puppet/provider/grafana_dashboard/grafana.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may 4 | # not use this file except in compliance with the License. You may obtain 5 | # a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | # License for the specific language governing permissions and limitations 13 | # under the License. 14 | require 'json' 15 | 16 | require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) 17 | 18 | # Note: this class doesn't implement the self.instances and self.prefetch 19 | # methods because the Grafana API doesn't allow to retrieve the dashboards and 20 | # all their properties in a single call. 21 | Puppet::Type.type(:grafana_dashboard).provide(:grafana, :parent => Puppet::Provider::Grafana) do 22 | desc "Support for Grafana dashboards stored into Grafana" 23 | 24 | defaultfor :kernel => 'Linux' 25 | 26 | # Return the list of dashboards 27 | def dashboards 28 | response = self.send_request('GET', '/api/search', nil, {:q => '', :starred => false}) 29 | if response.code != '200' 30 | fail("Fail to retrieve the dashboards (HTTP response: %s/%s)" % 31 | [response.code, response.body]) 32 | end 33 | 34 | begin 35 | JSON.parse(response.body) 36 | rescue JSON::ParserError 37 | fail("Fail to parse dashboards (HTTP response: %s/%s)" % 38 | [response_code, response.body]) 39 | end 40 | end 41 | 42 | # Return the dashboard matching with the resource's title 43 | def find_dashboard 44 | if not self.dashboards.find{ |x| x['title'] == resource[:title] } 45 | return 46 | end 47 | 48 | response = self.send_request('GET', '/api/dashboards/db/%s' % self.slug) 49 | if response.code != '200' 50 | fail("Fail to retrieve dashboard '%s' (HTTP response: %s/%s)" % 51 | [resource[:title], response.code, response.body]) 52 | end 53 | 54 | begin 55 | # Cache the dashboard's content 56 | @dashboard = JSON.parse(response.body)["dashboard"] 57 | rescue JSON::ParserError 58 | fail("Fail to parse dashboard '%s': %s" % 59 | [resource[:title], response.body]) 60 | end 61 | end 62 | 63 | def save_dashboard(dashboard) 64 | data = { 65 | :dashboard => dashboard.merge({ 66 | 'title' => resource[:title], 67 | 'id' => @dashboard ? @dashboard['id'] : nil, 68 | 'version' => @dashboard ? @dashboard['version'] + 1 : 0 69 | }), 70 | :overwrite => @dashboard != nil 71 | } 72 | 73 | response = self.send_request('POST', '/api/dashboards/db', data) 74 | if response.code != '200' 75 | fail("Fail to save dashboard '%s' (HTTP response: %s/%s)" % 76 | [resource[:name], response.code, response.body]) 77 | end 78 | end 79 | 80 | def slug 81 | resource[:title].downcase.gsub(/[^\w\- ]/, '').gsub(/ +/, '-') 82 | end 83 | 84 | def content 85 | @dashboard 86 | end 87 | 88 | def content=(value) 89 | self.save_dashboard(value) 90 | end 91 | 92 | def create 93 | self.save_dashboard(resource[:content]) 94 | end 95 | 96 | def destroy 97 | response = self.send_request('DELETE', '/api/dashboards/db/%s' % self.slug) 98 | 99 | if response.code != '200' 100 | raise Puppet::Error, "Failed to delete dashboard '%s' (HTTP "\ 101 | "response: %s/'%s')" % [resource[:title], response.code, 102 | response.body] 103 | end 104 | end 105 | 106 | def exists? 107 | self.find_dashboard() 108 | end 109 | end 110 | -------------------------------------------------------------------------------- /lib/puppet/provider/grafana_datasource/grafana.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may 4 | # not use this file except in compliance with the License. You may obtain 5 | # a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | # License for the specific language governing permissions and limitations 13 | # under the License. 14 | # 15 | require 'json' 16 | 17 | require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) 18 | 19 | Puppet::Type.type(:grafana_datasource).provide(:grafana, :parent => Puppet::Provider::Grafana) do 20 | desc "Support for Grafana datasources" 21 | 22 | defaultfor :kernel => 'Linux' 23 | 24 | def datasources 25 | response = self.send_request('GET', '/api/datasources') 26 | if response.code != '200' 27 | fail("Fail to retrieve datasources (HTTP response: %s/%s)" % 28 | [response.code, response.body]) 29 | end 30 | 31 | begin 32 | datasources = JSON.parse(response.body) 33 | 34 | datasources.collect{|x| x["id"]}.collect do |id| 35 | response = self.send_request('GET', '/api/datasources/%s' % id) 36 | if response.code != '200' 37 | fail("Fail to retrieve datasource %d (HTTP response: %s/%s)" % 38 | [id, response.code, response.body]) 39 | end 40 | 41 | datasource = JSON.parse(response.body) 42 | 43 | { 44 | :id => datasource["id"], 45 | :name => datasource["name"], 46 | :url => datasource["url"], 47 | :type => datasource["type"], 48 | :user => datasource["user"], 49 | :password => datasource["password"], 50 | :database => datasource["database"], 51 | :access_mode => datasource["access"], 52 | :is_default => datasource["isDefault"] ? :true : :false, 53 | :json_data => datasource["jsonData"] 54 | } 55 | end 56 | rescue JSON::ParserError 57 | fail("Fail to parse response: %s" % response.body) 58 | end 59 | end 60 | 61 | def datasource 62 | unless @datasource 63 | @datasource = self.datasources.find { |x| x[:name] == resource[:name] } 64 | end 65 | @datasource 66 | end 67 | 68 | def datasource=(value) 69 | @datasource = value 70 | end 71 | 72 | def type 73 | self.datasource[:type] 74 | end 75 | 76 | def type=(value) 77 | resource[:type] = value 78 | self.save_datasource() 79 | end 80 | 81 | def url 82 | self.datasource[:url] 83 | end 84 | 85 | def url=(value) 86 | resource[:url] = value 87 | self.save_datasource() 88 | end 89 | 90 | def access_mode 91 | self.datasource[:access_mode] 92 | end 93 | 94 | def access_mode=(value) 95 | self.resource[:access_mode] = value 96 | self.save_datasource() 97 | end 98 | 99 | def database 100 | self.datasource[:database] 101 | end 102 | 103 | def database=(value) 104 | resource[:database] = value 105 | self.save_datasource() 106 | end 107 | 108 | def user 109 | self.datasource[:user] 110 | end 111 | 112 | def user=(value) 113 | resource[:user] = value 114 | self.save_datasource() 115 | end 116 | 117 | def password 118 | self.datasource[:password] 119 | end 120 | 121 | def password=(value) 122 | resource[:password] = value 123 | self.save_datasource() 124 | end 125 | 126 | def is_default 127 | self.datasource[:is_default] 128 | end 129 | 130 | def is_default=(value) 131 | resource[:is_default] = value 132 | self.save_datasource() 133 | end 134 | 135 | def json_data 136 | self.datasource[:json_data] 137 | end 138 | 139 | def json_data=(value) 140 | resource[:json_data] = value 141 | self.save_datasource() 142 | end 143 | 144 | def save_datasource 145 | data = { 146 | :name => resource[:name], 147 | :type => resource[:type], 148 | :url => resource[:url], 149 | :access => resource[:access_mode], 150 | :database => resource[:database], 151 | :user => resource[:user], 152 | :password => resource[:password], 153 | :isDefault => (resource[:is_default] == :true), 154 | :jsonData => resource[:json_data], 155 | } 156 | 157 | if self.datasource.nil? 158 | response = self.send_request('POST', '/api/datasources', data) 159 | else 160 | data[:id] = self.datasource[:id] 161 | response = self.send_request('PUT', '/api/datasources/%s' % self.datasource[:id], data) 162 | end 163 | 164 | if response.code != '200' 165 | fail("Failed to create save '%s' (HTTP response: %s/'%s')" % 166 | [resource[:name], response.code, response.body]) 167 | end 168 | self.datasource = nil 169 | end 170 | 171 | def delete_datasource 172 | response = self.send_request('DELETE', '/api/datasources/%s' % self.datasource[:id]) 173 | 174 | if response.code != '200' 175 | fail("Failed to delete datasource '%s' (HTTP response: %s/'%s')" % 176 | [resource[:name], response.code, response.body]) 177 | end 178 | self.datasource = nil 179 | end 180 | 181 | def create 182 | self.save_datasource() 183 | end 184 | 185 | def destroy 186 | self.delete_datasource() 187 | end 188 | 189 | def exists? 190 | self.datasource 191 | end 192 | end 193 | -------------------------------------------------------------------------------- /lib/puppet/type/grafana_dashboard.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may 4 | # not use this file except in compliance with the License. You may obtain 5 | # a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | # License for the specific language governing permissions and limitations 13 | # under the License. 14 | require 'json' 15 | 16 | Puppet::Type.newtype(:grafana_dashboard) do 17 | @doc = "Manage dashboards in Grafana" 18 | 19 | ensurable 20 | 21 | newparam(:title, :namevar => true) do 22 | desc "The title of the dashboard." 23 | end 24 | 25 | newproperty(:content) do 26 | desc "The JSON representation of the dashboard." 27 | 28 | validate do |value| 29 | begin 30 | JSON.parse(value) 31 | rescue JSON::ParserError 32 | raise ArgumentError , "Invalid JSON string for content" 33 | end 34 | end 35 | 36 | munge do |value| 37 | JSON.parse(value) 38 | end 39 | 40 | def should_to_s(value) 41 | if value.length > 12 42 | "#{value.to_s.slice(0,12)}..." 43 | else 44 | value 45 | end 46 | end 47 | 48 | def is_to_s(value) 49 | should_to_s(value) 50 | end 51 | end 52 | 53 | newparam(:grafana_url) do 54 | desc "The URL of the Grafana server" 55 | defaultto "" 56 | 57 | validate do |value| 58 | unless value =~ /^https?:\/\// 59 | raise ArgumentError , "'%s' is not a valid URL" % value 60 | end 61 | end 62 | end 63 | 64 | newparam(:grafana_user) do 65 | desc "The username for the Grafana server (optional)" 66 | end 67 | 68 | newparam(:grafana_password) do 69 | desc "The password for the Grafana server (optional)" 70 | end 71 | 72 | validate do 73 | fail('content is required when ensure is present') if self[:ensure] == :present and self[:content].nil? 74 | end 75 | autorequire(:service) do 76 | 'grafana-server' 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /lib/puppet/type/grafana_datasource.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may 4 | # not use this file except in compliance with the License. You may obtain 5 | # a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | # License for the specific language governing permissions and limitations 13 | # under the License. 14 | # 15 | Puppet::Type.newtype(:grafana_datasource) do 16 | @doc = "Manage datasources in Grafana" 17 | 18 | ensurable 19 | 20 | newparam(:name, :namevar => true) do 21 | desc "The name of the datasource." 22 | end 23 | 24 | newparam(:grafana_url) do 25 | desc "The URL of the Grafana server" 26 | defaultto "" 27 | 28 | validate do |value| 29 | unless value =~ /^https?:\/\// 30 | raise ArgumentError , "'%s' is not a valid URL" % value 31 | end 32 | end 33 | end 34 | 35 | newparam(:grafana_user) do 36 | desc "The username for the Grafana server" 37 | end 38 | 39 | newparam(:grafana_password) do 40 | desc "The password for the Grafana server" 41 | end 42 | 43 | newproperty(:url) do 44 | desc "The URL of the datasource" 45 | 46 | validate do |value| 47 | unless value =~ /^https?:\/\// 48 | raise ArgumentError , "'%s' is not a valid URL" % value 49 | end 50 | end 51 | end 52 | 53 | newproperty(:type) do 54 | desc "The datasource type" 55 | newvalues(:influxdb, :elasticsearch, :graphite, :kairosdb, :opentsdb, :prometheus) 56 | end 57 | 58 | newproperty(:user) do 59 | desc "The username for the datasource (optional)" 60 | end 61 | 62 | newproperty(:password) do 63 | desc "The password for the datasource (optional)" 64 | end 65 | 66 | newproperty(:database) do 67 | desc "The name of the database (optional)" 68 | end 69 | 70 | newproperty(:access_mode) do 71 | desc "Whether the datasource is accessed directly or not by the clients" 72 | newvalues(:direct, :proxy) 73 | defaultto :direct 74 | end 75 | 76 | newproperty(:is_default) do 77 | desc "Whether the datasource is the default one" 78 | newvalues(:true, :false) 79 | defaultto :false 80 | end 81 | 82 | newproperty(:json_data) do 83 | desc "Additional JSON data to configure the datasource (optional)" 84 | 85 | validate do |value| 86 | unless value.nil? or value.is_a?(Hash) then 87 | raise ArgumentError , "json_data should be a Hash!" 88 | end 89 | end 90 | end 91 | autorequire(:service) do 92 | 'grafana-server' 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /manifests/config.pp: -------------------------------------------------------------------------------- 1 | # == Class grafana::config 2 | # 3 | # This class is called from grafana 4 | # 5 | class grafana::config { 6 | case $::grafana::install_method { 7 | 'docker': { 8 | if $::grafana::container_cfg { 9 | $cfg = $::grafana::cfg 10 | 11 | file { $::grafana::cfg_location: 12 | ensure => present, 13 | content => template('grafana/config.ini.erb'), 14 | } 15 | } 16 | } 17 | 'package','repo': { 18 | $cfg = $::grafana::cfg 19 | 20 | file { $::grafana::cfg_location: 21 | ensure => present, 22 | content => template('grafana/config.ini.erb'), 23 | } 24 | } 25 | 'archive': { 26 | $cfg = $::grafana::cfg 27 | 28 | file { "${::grafana::install_dir}/conf/custom.ini": 29 | ensure => present, 30 | content => template('grafana/config.ini.erb'), 31 | } 32 | } 33 | default: { 34 | fail("Installation method ${::grafana::install_method} not supported") 35 | } 36 | } 37 | 38 | if $::grafana::ldap_cfg { 39 | $ldap_cfg = $::grafana::ldap_cfg 40 | file { '/etc/grafana/ldap.toml': 41 | ensure => present, 42 | content => inline_template("<%= require 'toml'; TOML::Generator.new(@ldap_cfg).body %>\n"), 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /manifests/init.pp: -------------------------------------------------------------------------------- 1 | # == Class: grafana 2 | # 3 | # Installs and configures Grafana. 4 | # 5 | # === Parameters 6 | # [*archive_source*] 7 | # Download location of tarball to be used with the 'archive' install method. 8 | # Defaults to the URL of the latest version of Grafana available at the time of module release. 9 | # 10 | # [*container_cfg*] 11 | # Boolean. Determines whether a configuration file should be generated when using the 'docker' install method. 12 | # If true, use the `cfg` and `cfg_location` parameters to control creation of the file. 13 | # Defaults to false. 14 | # 15 | # [*container_params*] 16 | # Hash of parameters to use when creating the Docker container. For use with the 'docker' install method. 17 | # Refer to documentation of the `docker::run` resource in the `garethr-docker` module for details of available parameters. 18 | # Defaults to: 19 | # 20 | # container_params => { 21 | # 'image' => 'grafana/grafana:latest', 22 | # 'ports' => '3000' 23 | # } 24 | # 25 | # [*data_dir*] 26 | # The directory Grafana will use for storing its data. 27 | # Defaults to '/var/lib/grafana'. 28 | # 29 | # [*install_dir*] 30 | # Installation directory to be used with the 'archive' install method. 31 | # Defaults to '/usr/share/grafana'. 32 | # 33 | # [*install_method*] 34 | # Set to 'archive' to install Grafana using the tar archive. 35 | # Set to 'docker' to install Grafana using the official Docker container. 36 | # Set to 'package' to install Grafana using .deb or .rpm packages. 37 | # Set to 'repo' to install Grafana using an apt or yum repository. 38 | # Defaults to 'package'. 39 | # 40 | # [*manage_package_repo*] 41 | # If true this will setup the official grafana repositories on your host. Defaults to true. 42 | # 43 | # [*package_name*] 44 | # The name of the package managed with the 'package' install method. 45 | # Defaults to 'grafana'. 46 | # 47 | # [*package_source*] 48 | # Download location of package to be used with the 'package' install method. 49 | # Defaults to the URL of the latest version of Grafana available at the time of module release. 50 | # 51 | # [*service_name*] 52 | # The name of the service managed with the 'archive' and 'package' install methods. 53 | # Defaults to 'grafana-server'. 54 | # 55 | # [*version*] 56 | # The version of Grafana to install and manage. 57 | # Defaults to the latest version of Grafana available at the time of module release. 58 | # 59 | # [*repo_name*] 60 | # When using 'repo' install_method, the repo to look for packages in. 61 | # Set to 'stable' to install only stable versions 62 | # Set to 'testing' to install beta versions 63 | # Defaults to stable. 64 | # 65 | # === Examples 66 | # 67 | # class { '::grafana': 68 | # install_method => 'docker', 69 | # } 70 | # 71 | class grafana ( 72 | $archive_source = $::grafana::params::archive_source, 73 | $cfg_location = $::grafana::params::cfg_location, 74 | $cfg = $::grafana::params::cfg, 75 | $ldap_cfg = $::grafana::params::ldap_cfg, 76 | $container_cfg = $::grafana::params::container_cfg, 77 | $container_params = $::grafana::params::container_params, 78 | $data_dir = $::grafana::params::data_dir, 79 | $install_dir = $::grafana::params::install_dir, 80 | $install_method = $::grafana::params::install_method, 81 | $manage_package_repo = $::grafana::params::manage_package_repo, 82 | $package_name = $::grafana::params::package_name, 83 | $package_source = $::grafana::params::package_source, 84 | $repo_name = $::grafana::params::repo_name, 85 | $rpm_iteration = $::grafana::params::rpm_iteration, 86 | $service_name = $::grafana::params::service_name, 87 | $version = $::grafana::params::version 88 | ) inherits grafana::params { 89 | 90 | # validate parameters here 91 | if !is_hash($cfg) { 92 | fail('cfg parameter must be a hash') 93 | } 94 | 95 | class { 'grafana::install': } -> 96 | class { 'grafana::config': } ~> 97 | class { 'grafana::service': } 98 | 99 | contain 'grafana::install' 100 | contain 'grafana::service' 101 | 102 | #Class['grafana'] 103 | } 104 | -------------------------------------------------------------------------------- /manifests/install.pp: -------------------------------------------------------------------------------- 1 | # == Class grafana::install 2 | # 3 | class grafana::install { 4 | if $::grafana::archive_source != undef { 5 | $real_archive_source = $::grafana::archive_source 6 | } 7 | else { 8 | $real_archive_source = "https://grafanarel.s3.amazonaws.com/builds/grafana-${::grafana::version}.linux-x64.tar.gz" 9 | } 10 | 11 | if $::grafana::package_source != undef { 12 | $real_package_source = $::grafana::package_source 13 | } 14 | else { 15 | $real_package_source = $::osfamily ? { 16 | /(RedHat|Amazon)/ => "https://grafanarel.s3.amazonaws.com/builds/grafana-${::grafana::version}-${::grafana::rpm_iteration}.x86_64.rpm", 17 | 'Debian' => "https://grafanarel.s3.amazonaws.com/builds/grafana_${::grafana::version}_amd64.deb", 18 | default => $real_archive_source, 19 | } 20 | } 21 | 22 | case $::grafana::install_method { 23 | 'docker': { 24 | docker::image { 'grafana/grafana': 25 | image_tag => $::grafana::version, 26 | require => Class['docker'] 27 | } 28 | } 29 | 'package': { 30 | case $::osfamily { 31 | 'Debian': { 32 | package { 'libfontconfig1': 33 | ensure => present 34 | } 35 | 36 | wget::fetch { 'grafana': 37 | source => $real_package_source, 38 | destination => '/tmp/grafana.deb' 39 | } 40 | 41 | package { $::grafana::package_name: 42 | ensure => present, 43 | provider => 'dpkg', 44 | source => '/tmp/grafana.deb', 45 | require => [Wget::Fetch['grafana'],Package['libfontconfig1']] 46 | } 47 | } 48 | 'RedHat': { 49 | package { 'fontconfig': 50 | ensure => present 51 | } 52 | 53 | package { $::grafana::package_name: 54 | ensure => present, 55 | provider => 'rpm', 56 | source => $real_package_source, 57 | require => Package['fontconfig'] 58 | } 59 | } 60 | default: { 61 | fail("${::operatingsystem} not supported") 62 | } 63 | } 64 | } 65 | 'repo': { 66 | case $::osfamily { 67 | 'Debian': { 68 | package { 'libfontconfig1': 69 | ensure => present 70 | } 71 | 72 | if ( $::grafana::manage_package_repo ){ 73 | if !defined( Class['apt'] ) { 74 | class { 'apt': } 75 | } 76 | apt::source { 'grafana': 77 | location => "https://packagecloud.io/grafana/${::grafana::repo_name}/debian", 78 | release => 'wheezy', 79 | repos => 'main', 80 | key => { 81 | 'id' => '418A7F2FB0E1E6E7EABF6FE8C2E73424D59097AB', 82 | 'source' => 'https://packagecloud.io/gpg.key' 83 | }, 84 | before => Package[$::grafana::package_name], 85 | } 86 | Class['apt::update'] -> Package[$::grafana::package_name] 87 | } 88 | 89 | package { $::grafana::package_name: 90 | ensure => $::grafana::version, 91 | require => Package['libfontconfig1'] 92 | } 93 | } 94 | 'RedHat': { 95 | package { 'fontconfig': 96 | ensure => present 97 | } 98 | 99 | if ( $::grafana::manage_package_repo ){ 100 | yumrepo { 'grafana': 101 | descr => 'grafana repo', 102 | baseurl => 'https://packagecloud.io/grafana/stable/el/6/$basearch', 103 | gpgcheck => 1, 104 | gpgkey => 'https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana', 105 | enabled => 1, 106 | before => Package[$::grafana::package_name], 107 | } 108 | } 109 | 110 | package { $::grafana::package_name: 111 | ensure => "${::grafana::version}-${::grafana::rpm_iteration}", 112 | require => Package['fontconfig'] 113 | } 114 | } 115 | default: { 116 | fail("${::operatingsystem} not supported") 117 | } 118 | } 119 | } 120 | 'archive': { 121 | # create log directory /var/log/grafana (or parameterize) 122 | 123 | if !defined(User['grafana']){ 124 | user { 'grafana': 125 | ensure => present, 126 | home => $::grafana::install_dir 127 | } 128 | } 129 | 130 | file { $::grafana::install_dir: 131 | ensure => directory, 132 | group => 'grafana', 133 | owner => 'grafana', 134 | require => User['grafana'] 135 | } 136 | 137 | archive { '/tmp/grafana.tar.gz': 138 | ensure => present, 139 | extract => true, 140 | extract_command => 'tar xfz %s --strip-components=1', 141 | extract_path => $::grafana::install_dir, 142 | source => $real_archive_source, 143 | user => 'grafana', 144 | group => 'grafana', 145 | cleanup => true, 146 | require => File[$::grafana::install_dir] 147 | } 148 | 149 | } 150 | default: { 151 | fail("Installation method ${::grafana::install_method} not supported") 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /manifests/params.pp: -------------------------------------------------------------------------------- 1 | # == Class grafana::params 2 | # 3 | # This class is meant to be called from grafana 4 | # It sets variables according to platform 5 | # 6 | class grafana::params { 7 | $archive_source = undef 8 | $cfg_location = '/etc/grafana/grafana.ini' 9 | $cfg = {} 10 | $container_cfg = false 11 | $container_params = {} 12 | $data_dir = '/var/lib/grafana' 13 | $docker_image = 'grafana/grafana' 14 | $docker_ports = '3000:3000' 15 | $install_dir = '/usr/share/grafana' 16 | $install_method = 'package' 17 | $ldap_cfg = false 18 | $manage_package_repo = true 19 | $package_name = 'grafana' 20 | $package_source = undef 21 | $rpm_iteration = '1' 22 | $service_name = 'grafana-server' 23 | $version = '2.5.0' 24 | $repo_name = 'stable' 25 | } 26 | -------------------------------------------------------------------------------- /manifests/service.pp: -------------------------------------------------------------------------------- 1 | # == Class grafana::service 2 | # 3 | # This class is meant to be called from grafana 4 | # It ensure the service is running 5 | # 6 | class grafana::service { 7 | case $::grafana::install_method { 8 | 'docker': { 9 | $container = { 10 | 'grafana' => $::grafana::container_params 11 | } 12 | 13 | $defaults = { 14 | image => "${::grafana::params::docker_image}:${::grafana::version}", 15 | ports => $::grafana::params::docker_ports 16 | } 17 | 18 | create_resources(docker::run, $container, $defaults) 19 | } 20 | 'package','repo': { 21 | service { $::grafana::service_name: 22 | ensure => running, 23 | enable => true, 24 | subscribe => Package[$::grafana::package_name] 25 | } 26 | } 27 | 'archive': { 28 | $service_path = "${::grafana::install_dir}/bin/${::grafana::service_name}" 29 | $service_config = "${::grafana::install_dir}/conf/custom.ini" 30 | 31 | if !defined(Service[$::grafana::service_name]){ 32 | service { $::grafana::service_name: 33 | ensure => running, 34 | provider => base, 35 | binary => "su - grafana -c '${service_path} -config=${service_config} -homepath=${::grafana::install_dir} web &'", 36 | hasrestart => false, 37 | hasstatus => false, 38 | status => "ps -ef | grep ${::grafana::service_name} | grep -v grep" 39 | } 40 | } 41 | } 42 | default: { 43 | fail("Installation method ${::grafana::install_method} not supported") 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bfraser-grafana", 3 | "version": "2.5.0", 4 | "author": "bfraser", 5 | "summary": "This module provides Grafana, a dashboard and graph editor for Graphite and InfluxDB.", 6 | "license": "Apache-2.0", 7 | "source": "https://github.com/bfraser/puppet-grafana.git", 8 | "project_page": "https://github.com/bfraser/puppet-grafana", 9 | "issues_url": "https://github.com/bfraser/puppet-grafana/issues", 10 | "tags": [ 11 | "grafana", 12 | "graphite", 13 | "influxdb", 14 | "monitoring" 15 | ], 16 | "operatingsystem_support": [ 17 | { 18 | "operatingsystem": "RedHat", 19 | "operatingsystemrelease": [ "6.0" ] 20 | }, 21 | { 22 | "operatingsystem": "CentOS", 23 | "operatingsystemrelease": [ "6.0" ] 24 | }, 25 | { 26 | "operatingsystem": "Ubuntu", 27 | "operatingsystemrelease": [ "14.04" ] 28 | }, 29 | { 30 | "operatingsystem": "Debian", 31 | "operatingsystemrelease": [ "7.0" ] 32 | } 33 | ], 34 | "requirements": [ 35 | { 36 | "name": "pe", 37 | "version_requirement": ">= 3.2.0" 38 | }, 39 | { 40 | "name": "puppet", 41 | "version_requirement": ">= 3.4.0" 42 | } 43 | ], 44 | "dependencies": [ 45 | { 46 | "name": "puppet/archive", 47 | "version_requirement": ">= 0.5.1" 48 | }, 49 | { 50 | "name": "garethr/docker", 51 | "version_requirement": ">= 3.5.0 <6.0.0" 52 | }, 53 | { 54 | "name": "maestrodev/wget", 55 | "version_requirement": ">= 1.6.0 <2.0.0" 56 | }, 57 | { 58 | "name": "puppetlabs-stdlib", 59 | "version_requirement": ">=3.2.0 <5.0.0" 60 | }, 61 | { 62 | "name": "puppetlabs-apt", 63 | "version_requirement": ">=2.0.0 <3.0.0" 64 | } 65 | 66 | ] 67 | } 68 | -------------------------------------------------------------------------------- /spec/acceptance/class_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper_acceptance' 2 | 3 | describe 'grafana class' do 4 | 5 | context 'default parameters' do 6 | # Using puppet_apply as a helper 7 | it 'should work idempotently with no errors' do 8 | pp = <<-EOS 9 | class { 'grafana': } 10 | EOS 11 | 12 | # Run it twice and test for idempotency 13 | apply_manifest(pp, :catch_failures => true) 14 | apply_manifest(pp, :catch_changes => true) 15 | end 16 | 17 | describe package('grafana') do 18 | it { should be_installed } 19 | end 20 | 21 | describe service('grafana') do 22 | it { should be_enabled } 23 | it { should be_running } 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /spec/acceptance/nodesets/centos-64-x64.yml: -------------------------------------------------------------------------------- 1 | HOSTS: 2 | centos-64-x64: 3 | roles: 4 | - master 5 | platform: el-6-x86_64 6 | box : centos-64-x64-vbox4210-nocm 7 | box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box 8 | hypervisor : vagrant 9 | CONFIG: 10 | log_level: verbose 11 | type: foss 12 | -------------------------------------------------------------------------------- /spec/acceptance/nodesets/default.yml: -------------------------------------------------------------------------------- 1 | HOSTS: 2 | ubuntu-server-12042-x64: 3 | roles: 4 | - master 5 | platform: ubuntu-server-12.04-amd64 6 | box: ubuntu-server-12042-x64-vbox4210-nocm 7 | box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box 8 | hypervisor: vagrant 9 | 10 | CONFIG: 11 | log_level: verbose 12 | type: foss 13 | -------------------------------------------------------------------------------- /spec/acceptance/nodesets/ubuntu-server-12042-x64.yml: -------------------------------------------------------------------------------- 1 | HOSTS: 2 | ubuntu-server-12042-x64: 3 | roles: 4 | - master 5 | platform: ubuntu-server-12.04-amd64 6 | box: ubuntu-server-12042-x64-vbox4210-nocm 7 | box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box 8 | hypervisor: vagrant 9 | 10 | CONFIG: 11 | log_level: verbose 12 | type: foss 13 | -------------------------------------------------------------------------------- /spec/classes/coverage_spec.rb: -------------------------------------------------------------------------------- 1 | at_exit { RSpec::Puppet::Coverage.report! } 2 | -------------------------------------------------------------------------------- /spec/classes/grafana_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'grafana' do 4 | context 'supported operating systems' do 5 | ['Debian', 'RedHat'].each do |osfamily| 6 | describe "grafana class without any parameters on #{osfamily}" do 7 | let(:params) {{ }} 8 | let(:facts) {{ 9 | :osfamily => osfamily, 10 | }} 11 | 12 | it { should contain_class('grafana::params') } 13 | it { should contain_class('grafana::install').that_comes_before('grafana::config') } 14 | it { should contain_class('grafana::config') } 15 | it { should contain_class('grafana::service').that_subscribes_to('grafana::config') } 16 | 17 | it { should contain_service('grafana-server').with_ensure('running').with_enable(true) } 18 | it { should contain_package('grafana').with_ensure('present') } 19 | end 20 | end 21 | end 22 | 23 | context 'unsupported operating system' do 24 | describe 'grafana class without any parameters on Solaris/Nexenta' do 25 | let(:facts) {{ 26 | :osfamily => 'Solaris', 27 | :operatingsystem => 'Nexenta', 28 | }} 29 | 30 | it { expect { should contain_package('grafana') }.to raise_error(Puppet::Error, /Nexenta not supported/) } 31 | end 32 | end 33 | 34 | context 'package install method' do 35 | context 'debian' do 36 | let(:facts) {{ 37 | :osfamily => 'Debian' 38 | }} 39 | 40 | download_location = '/tmp/grafana.deb' 41 | 42 | describe 'use wget to fetch the package to a temporary location' do 43 | it { should contain_wget__fetch('grafana').with_destination(download_location) } 44 | it { should contain_wget__fetch('grafana').that_comes_before('Package[grafana]') } 45 | end 46 | 47 | describe 'install dependencies first' do 48 | it { should contain_package('libfontconfig1').with_ensure('present').that_comes_before('Package[grafana]') } 49 | end 50 | 51 | describe 'install the package' do 52 | it { should contain_package('grafana').with_provider('dpkg') } 53 | it { should contain_package('grafana').with_source(download_location) } 54 | end 55 | end 56 | 57 | context 'redhat' do 58 | let(:facts) {{ 59 | :osfamily => 'RedHat' 60 | }} 61 | 62 | describe 'install dependencies first' do 63 | it { should contain_package('fontconfig').with_ensure('present').that_comes_before('Package[grafana]') } 64 | end 65 | 66 | describe 'install the package' do 67 | it { should contain_package('grafana').with_provider('rpm') } 68 | end 69 | end 70 | end 71 | 72 | context 'repo install method' do 73 | let(:params) {{ 74 | :install_method => 'repo', 75 | :manage_package_repo => true 76 | }} 77 | 78 | context 'debian' do 79 | let(:facts) {{ 80 | :osfamily => 'Debian', 81 | :lsbdistid => 'Ubuntu' 82 | }} 83 | 84 | describe 'install apt repo dependencies first' do 85 | it { should contain_class('apt') } 86 | it { should contain_apt__source('grafana').with(:release => 'wheezy', :repos => 'main', :location => 'https://packagecloud.io/grafana/stable/debian') } 87 | it { should contain_apt__source('grafana').that_comes_before('Package[grafana]') } 88 | end 89 | 90 | describe 'install dependencies first' do 91 | it { should contain_package('libfontconfig1').with_ensure('present').that_comes_before('Package[grafana]') } 92 | end 93 | 94 | describe 'install the package' do 95 | it { should contain_package('grafana').with_ensure('2.5.0') } 96 | end 97 | end 98 | 99 | context 'redhat' do 100 | let(:facts) {{ 101 | :osfamily => 'RedHat' 102 | }} 103 | 104 | describe 'yum repo dependencies first' do 105 | it { should contain_yumrepo('grafana').with(:baseurl => 'https://packagecloud.io/grafana/stable/el/6/$basearch', :gpgkey => 'https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana', :enabled => 1) } 106 | it { should contain_yumrepo('grafana').that_comes_before('Package[grafana]') } 107 | end 108 | 109 | describe 'install dependencies first' do 110 | it { should contain_package('fontconfig').with_ensure('present').that_comes_before('Package[grafana]') } 111 | end 112 | 113 | describe 'install the package' do 114 | it { should contain_package('grafana').with_ensure('2.5.0-1') } 115 | end 116 | end 117 | end 118 | 119 | context 'repo install method without managing the package repo' do 120 | let(:params) {{ 121 | :install_method => 'repo', 122 | :manage_package_repo => false, 123 | :version => 'present' 124 | }} 125 | 126 | context 'debian' do 127 | let(:facts) {{ 128 | :osfamily => 'Debian', 129 | :lsbdistid => 'Ubuntu' 130 | }} 131 | 132 | it { should compile.with_all_deps } 133 | 134 | describe 'install dependencies first' do 135 | it { should contain_package('libfontconfig1').with_ensure('present').that_comes_before('Package[grafana]') } 136 | end 137 | 138 | describe 'install the package' do 139 | it { should contain_package('grafana').with_ensure('present') } 140 | end 141 | end 142 | end 143 | 144 | context 'archive install method' do 145 | let(:params) {{ 146 | :install_method => 'archive' 147 | }} 148 | 149 | install_dir = '/usr/share/grafana' 150 | service_config = '/usr/share/grafana/conf/custom.ini' 151 | archive_source = 'https://grafanarel.s3.amazonaws.com/builds/grafana-2.5.0.linux-x64.tar.gz' 152 | 153 | describe 'extract archive to install_dir' do 154 | it { should contain_archive('/tmp/grafana.tar.gz').with_ensure('present') } 155 | it { should contain_archive('/tmp/grafana.tar.gz').with_source(archive_source) } 156 | it { should contain_archive('/tmp/grafana.tar.gz').with_extract_path(install_dir) } 157 | end 158 | 159 | describe 'create grafana user' do 160 | it { should contain_user('grafana').with_ensure('present').with_home(install_dir) } 161 | it { should contain_user('grafana').that_comes_before('File[/usr/share/grafana]') } 162 | end 163 | 164 | describe 'manage install_dir' do 165 | it { should contain_file(install_dir).with_ensure('directory') } 166 | it { should contain_file(install_dir).with_group('grafana').with_owner('grafana') } 167 | end 168 | 169 | describe 'configure grafana' do 170 | it { should contain_file(service_config).with_ensure('present') } 171 | end 172 | 173 | describe 'run grafana as service' do 174 | it { should contain_service('grafana-server').with_ensure('running').with_provider('base') } 175 | it { should contain_service('grafana-server').with_hasrestart(false).with_hasstatus(false) } 176 | end 177 | 178 | context 'when user already defined' do 179 | let(:pre_condition) { 180 | 'user{"grafana": 181 | ensure => present, 182 | }' 183 | } 184 | describe 'do NOT create grafana user' do 185 | it { should_not contain_user('grafana').with_ensure('present').with_home(install_dir) } 186 | end 187 | end 188 | 189 | context 'when service already defined' do 190 | let(:pre_condition) { 191 | 'service{"grafana-server": 192 | ensure => running, 193 | hasrestart => true, 194 | hasstatus => true, 195 | }' 196 | } 197 | # let(:params) {{ :service_name => 'grafana-server'}} 198 | describe 'do NOT run service' do 199 | it { should_not contain_service('grafana-server').with_hasrestart(false).with_hasstatus(false) } 200 | end 201 | end 202 | end 203 | 204 | context 'invalid parameters' do 205 | context 'cfg' do 206 | let(:facts) {{ 207 | :osfamily => 'Debian', 208 | }} 209 | 210 | describe 'should raise an error when cfg parameter is not a hash' do 211 | let(:params) {{ 212 | :cfg => [], 213 | }} 214 | 215 | it { expect { should contain_package('grafana') }.to raise_error(Puppet::Error, /cfg parameter must be a hash/) } 216 | end 217 | 218 | describe 'should not raise an error when cfg parameter is a hash' do 219 | let(:params) {{ 220 | :cfg => {}, 221 | }} 222 | 223 | it { should contain_package('grafana') } 224 | end 225 | end 226 | end 227 | 228 | context 'configuration file' do 229 | let(:facts) {{ 230 | :osfamily => 'Debian', 231 | }} 232 | 233 | describe 'should not contain any configuration when cfg param is empty' do 234 | it { should contain_file('/etc/grafana/grafana.ini').with_content("# This file is managed by Puppet, any changes will be overwritten\n\n") } 235 | end 236 | 237 | describe 'should correctly transform cfg param entries to Grafana configuration' do 238 | let(:params) {{ 239 | :cfg => { 240 | 'app_mode' => 'production', 241 | 'section' => { 242 | 'string' => 'production', 243 | 'number' => 8080, 244 | 'boolean' => false, 245 | 'empty' => '', 246 | }, 247 | }, 248 | :ldap_cfg => { 249 | 'servers' => [ 250 | { 'host' => 'server1', 251 | 'use_ssl' => true, 252 | 'search_filter' => '(sAMAccountName=%s)', 253 | 'search_base_dns' => [ 'dc=domain1,dc=com' ], 254 | }, 255 | { 'host' => 'server2', 256 | 'use_ssl' => true, 257 | 'search_filter' => '(sAMAccountName=%s)', 258 | 'search_base_dns' => [ 'dc=domain2,dc=com' ], 259 | }, 260 | ], 261 | 'servers.attributes' => { 262 | 'name' => 'givenName', 263 | 'surname' => 'sn', 264 | 'username' => 'sAMAccountName', 265 | 'member_of' => 'memberOf', 266 | 'email' => 'email', 267 | } 268 | }, 269 | }} 270 | 271 | expected = "# This file is managed by Puppet, any changes will be overwritten\n\n"\ 272 | "app_mode = production\n\n"\ 273 | "[section]\n"\ 274 | "boolean = false\n"\ 275 | "empty = \n"\ 276 | "number = 8080\n"\ 277 | "string = production\n" 278 | 279 | it { should contain_file('/etc/grafana/grafana.ini').with_content(expected) } 280 | 281 | ldap_expected = "\n[[servers]]\n"\ 282 | "host = \"server1\"\n"\ 283 | "search_base_dns = [\"dc=domain1,dc=com\"]\n"\ 284 | "search_filter = \"(sAMAccountName=%s)\"\n"\ 285 | "use_ssl = true\n"\ 286 | "\n"\ 287 | "[[servers]]\n"\ 288 | "host = \"server2\"\n"\ 289 | "search_base_dns = [\"dc=domain2,dc=com\"]\n"\ 290 | "search_filter = \"(sAMAccountName=%s)\"\n"\ 291 | "use_ssl = true\n"\ 292 | "\n"\ 293 | "[servers.attributes]\n"\ 294 | "email = \"email\"\n"\ 295 | "member_of = \"memberOf\"\n"\ 296 | "name = \"givenName\"\n"\ 297 | "surname = \"sn\"\n"\ 298 | "username = \"sAMAccountName\"\n"\ 299 | "\n" 300 | 301 | it { should contain_file('/etc/grafana/ldap.toml').with_content(ldap_expected) } 302 | end 303 | end 304 | end 305 | -------------------------------------------------------------------------------- /spec/spec.opts: -------------------------------------------------------------------------------- 1 | --format 2 | s 3 | --colour 4 | --loadby 5 | mtime 6 | --backtrace 7 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'puppetlabs_spec_helper/module_spec_helper' 2 | -------------------------------------------------------------------------------- /spec/spec_helper_acceptance.rb: -------------------------------------------------------------------------------- 1 | require 'beaker-rspec/spec_helper' 2 | require 'beaker-rspec/helpers/serverspec' 3 | 4 | hosts.each do |host| 5 | # Install Puppet 6 | install_puppet 7 | end 8 | 9 | RSpec.configure do |c| 10 | # Project root 11 | proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..')) 12 | 13 | # Readable test descriptions 14 | c.formatter = :documentation 15 | 16 | # Configure all nodes in nodeset 17 | c.before :suite do 18 | # Install module and dependencies 19 | puppet_module_install(:source => proj_root, :module_name => 'grafana') 20 | hosts.each do |host| 21 | on host, puppet('module', 'install', 'puppetlabs-stdlib'), { :acceptable_exit_codes => [0,1] } 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /spec/unit/puppet/type/grafana_dashboard_type_spec.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | require 'spec_helper' 15 | 16 | describe Puppet::Type.type(:grafana_dashboard) do 17 | let(:gdashboard) { 18 | described_class.new :name => "foo", :grafana_url => "http://example.com/", :content => "{}", :ensure => :present 19 | } 20 | context "when setting parameters" do 21 | 22 | it "should fail if grafana_url isn't HTTP-based" do 23 | expect { 24 | described_class.new :name => "foo", :grafana_url => "example.com", :content => "{}", :ensure => :present 25 | }.to raise_error(Puppet::Error, /not a valid URL/) 26 | end 27 | 28 | it "should fail if content isn't provided" do 29 | expect { 30 | described_class.new :name => "foo", :grafana_url => "http://example.com", :ensure => :present 31 | }.to raise_error(Puppet::Error, /content is required/) 32 | end 33 | 34 | it "should fail if content isn't JSON" do 35 | expect { 36 | described_class.new :name => "foo", :grafana_url => "http://example.com/", :content => "{invalid", :ensure => :present 37 | }.to raise_error(Puppet::Error, /Invalid JSON/) 38 | end 39 | 40 | it "should accept valid parameters" do 41 | expect(gdashboard[:name]).to eq('foo') 42 | expect(gdashboard[:grafana_url]).to eq('http://example.com/') 43 | expect(gdashboard[:content]).to eq({}) 44 | end 45 | it "should autorequire the grafana-server for proper ordering" do 46 | catalog = Puppet::Resource::Catalog.new 47 | service = Puppet::Type.type(:service).new(:name => "grafana-server") 48 | catalog.add_resource service 49 | catalog.add_resource gdashboard 50 | 51 | relationship = gdashboard.autorequire.find do |rel| 52 | (rel.source.to_s == "Service[grafana-server]") and (rel.target.to_s == gdashboard.to_s) 53 | end 54 | expect(relationship).to be_a Puppet::Relationship 55 | end 56 | it "should not autorequire the service it is not managed" do 57 | catalog = Puppet::Resource::Catalog.new 58 | catalog.add_resource gdashboard 59 | expect(gdashboard.autorequire).to be_empty 60 | end 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /spec/unit/puppet/type/grafana_datasource_type_spec.rb: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Mirantis, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | require 'spec_helper' 15 | 16 | describe Puppet::Type.type(:grafana_datasource) do 17 | let(:gdatasource) { 18 | described_class.new :name => "foo", :grafana_url => "http://example.com", :url => 'http://influx.example.com' 19 | } 20 | context "when setting parameters" do 21 | 22 | it "should fail if grafana_url isn't HTTP-based" do 23 | expect { 24 | described_class.new :name => "foo", :grafana_url => "example.com", :content => "{}", :ensure => :present 25 | }.to raise_error(Puppet::Error, /not a valid URL/) 26 | end 27 | 28 | it "should fail if json_data isn't valid" do 29 | expect { 30 | described_class.new :name => "foo", :grafana_url => "http://example.com", :json_data => "invalid", :ensure => :present 31 | }.to raise_error(Puppet::Error, /json_data should be a Hash/) 32 | end 33 | 34 | it "should accept valid parameters" do 35 | expect(gdatasource[:name]).to eq('foo') 36 | expect(gdatasource[:grafana_url]).to eq('http://example.com') 37 | expect(gdatasource[:url]).to eq('http://influx.example.com') 38 | end 39 | it "should autorequire the grafana-server for proper ordering" do 40 | catalog = Puppet::Resource::Catalog.new 41 | service = Puppet::Type.type(:service).new(:name => "grafana-server") 42 | catalog.add_resource service 43 | catalog.add_resource gdatasource 44 | 45 | relationship = gdatasource.autorequire.find do |rel| 46 | (rel.source.to_s == "Service[grafana-server]") and (rel.target.to_s == gdatasource.to_s) 47 | end 48 | expect(relationship).to be_a Puppet::Relationship 49 | end 50 | it "should not autorequire the service it is not managed" do 51 | catalog = Puppet::Resource::Catalog.new 52 | catalog.add_resource gdatasource 53 | expect(gdatasource.autorequire).to be_empty 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /templates/config.ini.erb: -------------------------------------------------------------------------------- 1 | # This file is managed by Puppet, any changes will be overwritten 2 | 3 | <%- @cfg.keys.sort.each do |key| 4 | if @cfg[key].is_a?(Hash) -%> 5 | 6 | [<%= key %>] 7 | <%- @cfg[key].keys.sort.each do |valkey| -%> 8 | <%= valkey %> = <%= @cfg[key][valkey] %> 9 | <%- end -%> 10 | <%- else -%> 11 | <%= key %> = <%= @cfg[key] %> 12 | <%- end 13 | end -%> 14 | -------------------------------------------------------------------------------- /tests/init.pp: -------------------------------------------------------------------------------- 1 | # The baseline for module testing used by Puppet Labs is that each manifest 2 | # should have a corresponding test manifest that declares that class or defined 3 | # type. 4 | # 5 | # Tests are then run by using puppet apply --noop (to check for compilation 6 | # errors and view a log of events) or by fully applying the test in a virtual 7 | # environment (to compare the resulting system state to the desired state). 8 | # 9 | # Learn more about module testing here: 10 | # http://docs.puppetlabs.com/guides/tests_smoke.html 11 | # 12 | include grafana 13 | --------------------------------------------------------------------------------