Commit c3a54e9b authored by Olav Morken's avatar Olav Morken

Initial commit.

parents
#!/usr/bin/env python3
import argparse
import contextlib
import json
import requests
import settings
@contextlib.contextmanager
def checked_http(text):
print('{text}: '.format(text=text), end='')
try:
yield
print('OK')
except requests.exceptions.HTTPError as e:
print('Failed: ', e.response.status_code, e.response.text)
def clean_node(node):
headers = {
"Accept": 'pson',
"Content-Type": "text/pson",
}
cert = (settings.puppet_crt, settings.puppet_key)
puppet_url = "https://{}:{}/puppet-ca/v1/certificate_status/{}?environment=ignored".format(settings.puppet_ca_host, settings.puppet_ca_port, node)
with checked_http('Revoking certificate'):
r = requests.put(puppet_url, cert=cert, verify=settings.puppet_ca, data='{"desired_state":"revoked"}', headers=headers)
r.raise_for_status()
with checked_http('Deleting certificate'):
r = requests.delete(puppet_url, cert=cert, verify=settings.puppet_ca)
r.raise_for_status()
data = {
'command': 'deactivate node',
'version': 3,
'payload': {
'certname': node,
},
}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}
puppetdb_url = "https://{}:{}/pdb/cmd/v1".format(settings.puppetdb_host,
settings.puppetdb_port)
with checked_http('Deleting from PuppetDB'):
r = requests.post(puppetdb_url, cert=cert, verify=False, data=json.dumps(data), headers=headers)
r.raise_for_status()
def main():
parser = argparse.ArgumentParser(description='Delete puppet node')
parser.add_argument('node', help='Node to delete')
args = parser.parse_args()
clean_node(args.node)
if __name__ == '__main__':
main()
import subprocess
def _puppet_config(name):
output = subprocess.check_output(['puppet', 'config', 'print', name])
return str(output, 'utf-8').strip()
puppet_key = _puppet_config('hostprivkey')
puppet_crt = _puppet_config('hostcert')
puppet_ca = _puppet_config('localcacert')
puppet_ca_host = 'puppet-ca.uninett.no'
puppet_ca_port = 8140
puppetdb_host = 'puppetdb.uninett.no'
puppetdb_port = 8081
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment