npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dustul

v0.0.6

Published

CLI utility for rendering Dust templates as Consul values change

Downloads

9

Readme

Dustul

Introduction

Dustul is a Dust-based an alternative to consul-template. It allows you to subscribe to changes within Consul and render templates (using Dust template syntax) to disk as they occur. After rendering occurs, one or more commands can also be executed (e.g. service nginx restart, etc...).

Getting Started

Dustul is a command-line utility. As such, it should be installed globally via npm i -g dustul. Afterwards, getting started is a simple matter of creating a configuration file, as shown below.

Take note:

  • The file should export an array of configuration objects, allowing you to define one or more templates to be rendered.
  • Dustul relies on npm's consul package for interactions with Consul. For more information on the symantics that are used for defining subscriptions, see that project's documentation.
// config.js
module.exports = [
    {
        'config': {
        	/**
        	 * Consul options
        	 */
            'consul': {
                'host': '192.168.50.2',
                'port': 8500
            }
        },
        /**
         * The context object to be passed to Dust as Consul changes occur.
         * Here you can define context helpers, pre-defined data, etc...
         */
        'context': {
        },
        /**
         * Consul subscriptions are defined here. As changes occur, incoming data is processed
         * and assigned to the context object using the associated key. For example, our cities
         * subscription will be processed and stored at context.cities.
         */
        'watch': {
        	/**
        	 * Subscribe to changes within Consul's key / value store.
        	 */
            'cities': {
                'method': 'kv.get',
                'options': {
                    'key': 'config/env-app/cities',
                    'recurse': false
                },
                /**
                 * Optional callback. Allows you to manipulate incoming data before it is stored
                 * within the context object.
                 */
                'cb': (data) => {
                    return JSON.parse(data.Value);
                }
            },
            /**
             * Subscribe to a list of available Consul nodes.
             */
            'nodes': {
                'method': 'catalog.node.list'
            },
            /**
             * Subscribe to a list of available Consul services.
             */
            'services': {
                'method': 'catalog.service.list'
            },
            /**
             * Subscribe to a list of available Consul nodes for a specific service.
             */
            'consul_nodes': {
                'method': 'catalog.service.nodes',
                'options': {
                    'service': 'consul'
                }
            }
        },
        /**
         * This function is called just before Dust rendering occurs. Here you
         * can make final modifications to your context object.
         */
        'beforeRender': (consul, context) => {
            context.date = new Date();
        },
        /**
         * Whether or not to trim whitespace from the beginning and end of the rendered template
         * before saving to disk. Default: false
         */
        'trim': true,
        /**
         * An array of commands (and corresponding arguments) to be executed after rendering
         * has completed. Values for command, args, and options are passed to Node's process.spawn
         * method. Commands are run in order as a sequence.
         */
        'commands': [
            {
                'command': 'service',
                'args': ['nginx', 'restart'],
                'options': {}
            }
        ],
        /**
         * The path at which the rendered template will be saved. Relative paths
         * are considered relative to the location at which Dustul is executed. You'd be better
         * off sticking with absolute paths.
         */
        'destination': '/etc/nginx/sites-enabled/my-site.conf',
        /**
         * The Dust template
         */
        'template': `
            upstream my_upstream {
                {#consul_nodes}
                    server {Address};
                {/consul_nodes}
            }
            
            server {
                listen 80;
                location / {
                    proxy_pass http://my_upstream;
                }
            }
        `
    }
];

Next, call Dustul and provide it with the path to your configuration file, as shown below.

$ dustul subscribe -c ./config.js

Limitations & Desired Contributions

At the moment, Dustul only allows you to subscribe to changes within Consul's key value store. Short-term plans including adding support for subscribing to service and node changes, as well.

Related Resources