-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from cernops/python3
Python3 support
- Loading branch information
Showing
4 changed files
with
25 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[TYPECHECK] | ||
ignored-classes=PuppetDBNodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# Daniel Fernandez Rodriguez <[email protected]> | ||
|
||
from argparse import ArgumentParser | ||
from collections import defaultdict | ||
from requests_kerberos import HTTPKerberosAuth | ||
|
@@ -11,7 +9,7 @@ | |
import sys | ||
|
||
|
||
class PuppetDBNodes(object): | ||
class PuppetDBNodes(): | ||
|
||
def __init__(self, args): | ||
for k, v in args.items(): | ||
|
@@ -41,15 +39,16 @@ def get_facts_puppetdb(self, apiurl, facts, hostgroup): | |
headers = {'Content-Type': 'application/json','Accept': 'application/json, version=2'} | ||
payload = {'query': query} | ||
|
||
logging.info("Getting facts from '%s', query: '%s'" % (url, query)) | ||
logging.info("Getting facts from '%s', query: '%s'", url, query) | ||
r = requests.get(url, params=payload, headers=headers, auth=HTTPKerberosAuth()) | ||
|
||
# pylint: disable=no-member | ||
if r.status_code == requests.codes.ok: | ||
logging.info("Request code: '%s'" % r.status_code) | ||
logging.info("Request code: '%s'", r.status_code) | ||
return json.loads(r.text) | ||
else: | ||
logging.error("The request failed with code '%s'" % r.status_code) | ||
return None | ||
|
||
logging.error("The request failed with code '%s'", r.status_code) | ||
return None | ||
|
||
|
||
def print_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist): | ||
|
@@ -63,16 +62,16 @@ def print_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist): | |
|
||
if raw_data != None: | ||
for entry in raw_data: | ||
data[entry['certname']] = dict(data[entry['certname']].items() + [(entry['name'], entry['value'])]) | ||
data[entry['certname']] = dict(list(data[entry['certname']].items()) + [(entry['name'], entry['value'])]) | ||
|
||
logging.info("Printing node list using standard output...") | ||
for node in data.keys(): | ||
print ('%s:'%node) | ||
print (" "*4 + "hostname: " + node) | ||
print (" "*4 + "username: " + sshuser) | ||
print('%s:'%node) | ||
print(" "*4 + "hostname: " + node) | ||
print(" "*4 + "username: " + sshuser) | ||
for fact in factlist: | ||
if data[node].has_key(fact): | ||
print (" "*4 + fact + ": " + str(data[node][fact]) ) | ||
if fact in data[node]: | ||
print(" "*4 + fact + ": " + str(data[node][fact]) ) | ||
logging.info("Node list printed successfully") | ||
|
||
else: | ||
|
@@ -91,21 +90,21 @@ def store_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist, filename): | |
|
||
if raw_data != None: | ||
for entry in raw_data: | ||
data[entry['certname']] = dict(data[entry['certname']].items() + [(entry['name'], entry['value'])]) | ||
data[entry['certname']] = dict(list(data[entry['certname']].items()) + [(entry['name'], entry['value'])]) | ||
|
||
logging.info("Saving node list in '%s'..." % filename) | ||
logging.info("Saving node list in '%s'...", filename) | ||
with open(filename, 'w') as file: | ||
for node in data.keys(): | ||
file.write('%s:\n'%node) | ||
file.write(" "*4 + "hostname: " + node + '\n') | ||
file.write(" "*4 + "username: " + sshuser +'\n') | ||
for fact in factlist: | ||
if data[node].has_key(fact): | ||
if fact in data[node]: | ||
file.write(" "*4 + fact + ": " + str(data[node][fact]) + '\n') | ||
logging.info('Node list saved successfully') | ||
|
||
# trick to avoid Rundeck complain when no output is printed out | ||
print "" | ||
print("") | ||
else: | ||
logging.error('Fact list empty. Check PuppetDB connection params') | ||
|
||
|
@@ -153,6 +152,6 @@ def main(): | |
if __name__ == "__main__": | ||
try: | ||
main() | ||
except Exception, e: | ||
except Exception as e: | ||
logging.error(e) | ||
sys.exit(-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
#yaml plugin metadata | ||
|
||
name: PuppetDB source | ||
version: 1.4.2 | ||
version: 2.0.0 | ||
rundeckPluginVersion: 1.0 | ||
author: Daniel Fernandez ([email protected]), David Moreno Garcia ([email protected]), Philippe Ganz (philippe.ganz@cern.ch), Nacho Barrientos Arias (nacho.barrientos@cern.ch) | ||
date: 2019/09/09 | ||
author: Daniel Fernandez, David Moreno Garcia, Philippe Ganz, Nacho Barrientos Arias (nacho.barrientos@cern.ch), Ignacio Coterillo Coz (ignacio.coterillo.coz@cern.ch) | ||
date: 2021/08/04 | ||
providers: | ||
- name: rundeck-puppetdb-nodes | ||
service: ResourceModelSource | ||
title: "PuppetDB source" | ||
description: Queries PuppetDB to retrieve node definitions from a specific Foreman hostgroup | ||
resource-format: resourceyaml | ||
plugin-type: script | ||
script-interpreter: /usr/bin/python | ||
script-interpreter: /usr/bin/python3 | ||
script-file: rundeck_puppetdb_nodes.py | ||
script-args: "-v --apiurl ${config.apiurl} --hostgroup ${config.hostgroup} --krbuser ${config.krbuser} --keytab ${config.keytab} | ||
--sshuser ${config.sshuser} --file ${config.file} --${config.mode} --factlist ${config.factlist}" | ||
|