Created
September 24, 2012 15:46
-
-
Save turtlebender/3776621 to your computer and use it in GitHub Desktop.
Updated chef data bag script
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
#!/usr/bin/env python | |
""" | |
_chef_profile_ | |
Generate a chef profile data bag | |
""" | |
import argparse | |
import crypt | |
import getpass | |
import json | |
import os | |
import pwd | |
import random | |
import signal | |
import sys | |
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
parser = argparse.ArgumentParser( | |
description='Build a chef profile json data bag') | |
parser.add_argument('--public-keys', | |
default = os.path.expanduser('~/.ssh/id_rsa.pub'), | |
help = "Comma separated list of public key files") | |
parser.add_argument('--user', default = getpass.getuser(), | |
help = "User Name") | |
parser.add_argument('--email', default = None, | |
help = "Email Address") | |
parser.add_argument('--name', default = None, | |
help = "Full Name") | |
parser.add_argument('--password', default = None, | |
help = "Password to use for chef accounts") | |
def grab_ssh_key(key_file): | |
""" | |
_grab_ssh_key_ | |
""" | |
with open(key_file, 'r') as handle: | |
key = handle.read() | |
return key | |
def build_pass(super_secret_passwd): | |
""" | |
Hash the password for both unix shadow and htpasswd | |
""" | |
chars = [] | |
for i in range(6): | |
chars.append(random.choice(ALPHABET)) | |
password = crypt.crypt(super_secret_passwd, | |
'$6${0}$'.format("".join(chars))) | |
chars = [] | |
for i in range(2): | |
chars.append(random.choice(ALPHABET)) | |
htpasswd = crypt.crypt(super_secret_passwd, | |
"".join(chars)) | |
return password, htpasswd | |
def main(): | |
""" | |
Run the User data bag generation tool | |
""" | |
args = parser.parse_args() | |
keys_list = [ x for x in args.public_keys.split(",") ] | |
ssh_keys_list = [ grab_ssh_key(x) for x in keys_list ] | |
name = args.name | |
if name is None: | |
name = raw_input('What is your full name? ') | |
email = args.email | |
if email is None: | |
email = raw_input('What is your email address? ') | |
password = args.password | |
if password is None: | |
password = getpass.getpass("Please enter your password: ") | |
password2 = getpass.getpass("Please confirm your password: ") | |
while password != password2: | |
password = getpass.getpass("Please enter your password: ") | |
password2 = getpass.getpass("Please confirm your password: ") | |
password_pair = build_pass(password) | |
chef_profile = { | |
"shell": "/bin/bash", | |
"nagios": { | |
"email": email | |
}, | |
"password": password_pair[0], | |
"ssh_keys": ssh_keys_list, | |
"comment": name, | |
"htpasswd": password_pair[1], | |
"id": args.user, | |
"groups": [ | |
"adm", | |
"sysadmin" | |
] | |
} | |
print json.dumps(chef_profile, indent=4) | |
if __name__ == '__main__': | |
def handleSigTERM(): | |
print "Exiting" | |
sys.exit(1) | |
signal.signal(signal.SIGTERM, handleSigTERM) | |
try: | |
main() | |
except KeyboardInterrupt: | |
print '' | |
print "Exiting" | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment