Skip to content

Commit

Permalink
Merge pull request #8 from cernops/python3
Browse files Browse the repository at this point in the history
Python3 support
  • Loading branch information
icot authored Aug 4, 2021
2 parents febb341 + 0d21843 commit 7a510a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[TYPECHECK]
ignored-classes=PuppetDBNodes
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Parameters
Requirements
------------
* The plugin requires Rundeck version 2.0 or higher.
* Python 3.
* python-requests v1.1.0-4 or higher
* python-requests-kerberos v0.5 ([important!!](https://bugzilla.redhat.com/show_bug.cgi?id=1169296)) or higher

Expand Down
37 changes: 18 additions & 19 deletions rundeck-puppetdb-nodes-plugin/contents/rundeck_puppetdb_nodes.py
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
Expand All @@ -11,7 +9,7 @@
import sys


class PuppetDBNodes(object):
class PuppetDBNodes():

def __init__(self, args):
for k, v in args.items():
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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')

Expand Down Expand Up @@ -153,6 +152,6 @@ def main():
if __name__ == "__main__":
try:
main()
except Exception, e:
except Exception as e:
logging.error(e)
sys.exit(-1)
8 changes: 4 additions & 4 deletions rundeck-puppetdb-nodes-plugin/plugin.yaml
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}"
Expand Down

0 comments on commit 7a510a3

Please sign in to comment.