Created
June 30, 2015 22:27
-
-
Save thewellington/92d84738f8a7d6e92390 to your computer and use it in GitHub Desktop.
Puppet Agent Reset
This file contains hidden or 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
#! /bin/env python | |
# puppet_agent_reset.py | |
# Collect info from the skytap JSON and save it | |
# | |
# Tasks: | |
# Get the default gateway | |
# Collect JSON from http://<gateway>/skytap | |
# Parse VM ID from "id" and configuration id from "configuration_url" | |
# Check certname in puppet.conf, set to "VMID<vmid>-ENV<config>" if different | |
# Delete puppet SSL info if hostname changed | |
# Check /skytapinfo file and update with above info if different | |
# If puppet conf changed, manually start puppt | |
import json | |
import sys | |
import os | |
import socket | |
import shutil | |
import os.path | |
try: | |
import configparser | |
except ImportError: | |
sys.stderr.write("You do not have the 'configparser' module installed. " | |
"Please see https://pypi.python.org/pypi/configparser " | |
"for more information.") | |
exit(1) | |
try: | |
import requests | |
except ImportError: | |
sys.stderr.write("You do not have the 'requests' module installed. " | |
"Please see http://docs.python-requests.org/en/latest/ " | |
"for more information.") | |
exit(1) | |
# Get the default gateway | |
file = os.popen("/sbin/ip route | awk '/default/ {print $3}'") | |
gateway = file.read().strip() | |
file.close() | |
# Quick test to see if the gateway actually looks like an IP address | |
try: | |
socket.inet_aton(gateway) | |
except socket.error: | |
sys.stderr.write("Could not determine the gateway.") | |
exit(1) | |
# Collect JSON from http://<gateway>/skytap | |
url = 'http://' + gateway + '/skytap' | |
json_data = requests.get(url) | |
if json_data.status_code != 200: | |
sys.stderr.write("Error received by JSON request. " | |
"Status code: " + json_data.status_code) | |
exit(1) | |
if json_data.headers['content-type'] != 'application/json': | |
sys.stderr.write("Returned text from server isn't a JSON. " | |
"Content-type: " + json_data.headers['content-type']) | |
exit(1) | |
data = json.loads(json_data.text) | |
vmid = data["id"] | |
configid = data["configuration_url"].split("/")[-1] | |
hostname = "vmid" + vmid + "-envid" + configid + ".dev.fulcrum.net" | |
# Check hostname | |
puppetconffile = '/etc/puppetlabs/puppet/puppet.conf' | |
puppetssldir = '/etc/puppetlabs/puppet/ssl' | |
puppetconf = open(puppetconffile, 'r') | |
parser = configparser.ConfigParser() | |
parser.read_file(puppetconf) | |
currenthost = parser["main"]["certname"] | |
if currenthost != hostname: | |
puppettmppath = '/tmp/puppet.conf' | |
puppettmp = open(puppettmppath, 'w+') | |
parser["main"]["certname"] = hostname | |
parser.write(puppettmp) | |
os.rename(puppettmppath, puppetconffile) | |
shutil.rmtree(puppetssldir) | |
os.mkdir(puppetssldir) | |
os.system('puppet agent --onetime') | |
# Update /skytapinfo file | |
skytapinfo = '/skytapinfo' | |
skyfile = open(skytapinfo, 'w+') | |
skyconfig = configparser.ConfigParser() | |
if os.path.isfile(skytapinfo) and os.path.getsize(skytapinfo) > 0: | |
skyconfig.read_file(skytapinfo) | |
if not skyconfig.has_section("SKYTAP"): | |
skyconfig.add_section("SKYTAP") | |
skyfilevm = '' | |
if (skyconfig.has_option("SKYTAP", "VMID")): | |
skyfilevm = skyconfig["SKYTAP"]["VMID"] | |
skyfileenv = '' | |
if (skyconfig.has_option("SKYTAP", "ENVIRONMENT")): | |
skyfileenv = skyconfig["SKYTAP"]["ENVIRONMENT"] | |
dirty = False | |
if (skyfilevm != vmid): | |
dirty = True | |
skyconfig["SKYTAP"]["VMID"] = str(vmid) | |
if (skyfileenv != configid): | |
dirty = True | |
skyconfig["SKYTAP"]["ENVIRONMENT"] = str(configid) | |
if dirty: | |
skyconfig.write(skyfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment