From: garciadeblas Date: Tue, 29 Nov 2016 14:36:38 +0000 (+0100) Subject: Merge "Add a "dig" action" X-Git-Tag: v2.0.2~7^2~10 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=447b7a4e72b90ff00dd3b7552a40d7d70c118638;hp=e180c9bac1b679b17ed61b0c02228d4bbf247e3a;p=osm%2Fdevops.git Merge "Add a "dig" action" --- diff --git a/layers/netutils/actions.yaml b/layers/netutils/actions.yaml index 9995f777..5c99e21c 100644 --- a/layers/netutils/actions.yaml +++ b/layers/netutils/actions.yaml @@ -30,3 +30,18 @@ traceroute: type: string required: - destination +dig: + description: "DNS lookup" + params: + nsserver: + description: "The nameserver to lookup against." + type: string + host: + description: "The host to lookup" + type: string + type: + description: "The DNS record type to lookup" + type: string + required: + - host + diff --git a/layers/netutils/actions/dig b/layers/netutils/actions/dig new file mode 100755 index 00000000..736a4069 --- /dev/null +++ b/layers/netutils/actions/dig @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') + +from charms.reactive import main +from charms.reactive import set_state +from charmhelpers.core.hookenv import action_fail + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_state('actions.dig') + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/layers/netutils/reactive/layer_netutils.py b/layers/netutils/reactive/layer_netutils.py index 8f8db268..7043f190 100644 --- a/layers/netutils/reactive/layer_netutils.py +++ b/layers/netutils/reactive/layer_netutils.py @@ -22,6 +22,31 @@ def ready(): status_set('active', 'Ready!') set_flag('netutils.ready') +@when('actions.dig') +def dig(): + err = '' + try: + nsserver = action_get('nsserver') + host = action_get('host') + nstype = action_get('type') + cmd = "dig" + + if nsserver: + cmd += " @{}".format(nsserver) + if host: + cmd += " {}".format(host) + else: + action_fail('Hostname required.') + if nstype: + cmd += " -t {}".format(nstype) + + result, err = _run(cmd) + except: + action_fail('dig command failed:' + err) + else: + action_set({'outout': result}) + finally: + remove_flag('actions.dig') @when('actions.nmap') def nmap():