Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example StarCluster plugin that tags all instances based on tags specified in the config
# This is an example config that assumes tagger.py is either in
# $HOME/.starcluster/plugins or lives somewhere in your $PYTHONPATH
[plugin tagger]
setup_class = tagger.TaggerPlugin
# add as many key=value pairs as you like separated by ','
tags = 'mykey=myvalue, mykey2=myvalue2'
[cluster default]
...
plugins = tagger
# Install this file to $HOME/.starcluster/plugins/tagger.py or somewhere on your $PYTHONPATH
from starcluster.clustersetup import ClusterSetup
from starcluster.logger import log
class TaggerPlugin(ClusterSetup):
def __init__(self, tags):
self.tags = [t.strip() for t in tags.split(',')]
self.tags = dict([t.split('=') for t in self.tags)
def run(self, nodes, master, user, user_shell, volumes):
log.info("Tagging all nodes...")
for tag in self.tags:
val = self.tags.get(tag)
log.info("Applying tag - %s: %s" % (tag, val))
for node in nodes:
node.add_tag(tag, val)

There is a typo on line 8 (missing closing bracket):

self.tags = dict([t.split('=') for t in self.tags)

should be

self.tags = dict([t.split('=') for t in self.tags])

This plugin adds tags to all nodes started when you start the cluster, but won't add tags to any nodes added later with addnode or via the load balancer. To fix that, you need to have something more like the code below. The other thing to note is that the code doesn't strip quotes, so the config file syntax example is incorrect: you need

tags = mykey1=myvalue1 mykey2=myvalue2

(i.e no quotes around the key/value pairs)

# Install this file to $HOME/.starcluster/plugins/tagger.py or somewhere on your $PYTHONPATH
# See https://gist.github.com/jtriley/3305302
from starcluster.clustersetup import ClusterSetup
from starcluster.logger import log

class TaggerPlugin(ClusterSetup):
    def __init__(self, tags):
        self.tags = [t.strip() for t in tags.split(',')]
        self.tags = dict([t.split('=') for t in self.tags])

    def run(self, nodes, master, user, user_shell, volumes):
        log.info("Tagging all nodes...")
        for tag in self.tags:
            val = self.tags.get(tag)
            log.info("Applying tag - %s: %s" % (tag, val))
            for node in nodes:
                node.add_tag(tag, val)

    def on_add_node(self, node, nodes, master, user, user_shell, volumes):
        log.info("Tagging nodes %s" % node)
        for tag in self.tags:
            val = self.tags.get(tag)
            log.info("Applying tag - %s: %s" % (tag, val))
        node.add_tag(tag, val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment