def populate_key(cloud, username, keyname, public_key, private_key=''): fingerprint = run_ssh_on_string(SSH_KEYGEN + " -lf %s", public_key).split( ' ')[1] logger.debug(fingerprint) try: insert_sshkey(cloud, username, public_key, fingerprint, keyname) try: send_to_host(username, public_key) print json.dumps([{ "keypair": { "public_key": public_key, "private_key": private_key, "user_id": "", "name": keyname, "fingerprint": fingerprint } }]) except: delete_sshkey(cloud, username, keyname) print '{"message": "Key pair \'%s\' could not be created.", "code": 409}' % keyname except IntegrityError: print '{"message": "Key pair \'%s\' already exists.", "code": 409}' % keyname
def main(): # args 1,2,3,4,5 are all used for gpg option = sys.argv[6] username = sys.argv[7] cloud = sys.argv[8] logger.debug(username) logger.debug(option) if option == 'create': logger.debug("there is no keyname") keyname = sys.argv[9] logger.debug("keyname %s", keyname) if len(sys.argv) > 10: password = sys.argv[10] public_key, private_key = generate_keypair(password=password) else: public_key, private_key = generate_keypair() populate_key(cloud, username, keyname, public_key, private_key) elif option == 'import': keyname = sys.argv[9] public_key = ' '.join(sys.argv[10:]) logger.debug(public_key) populate_key(cloud, username, keyname, public_key) elif option == 'delete': logger.debug("going to delete") keyname = sys.argv[9] logger.debug("cloud: %s, username: %s, keyname: %s", cloud, username, keyname) public_key = get_keypair(cloud, username, keyname)[0] logger.debug(public_key) try: send_to_host(username, public_key, method='DELETE') if not delete_sshkey(cloud, username, keyname): send_to_host(username, public_key) print '{"message": "Key pair \'%s\' could not be deleted.", "code": 409}' % keyname except: print '{"message": "Key pair \'%s\' could not be deleted.", "code": 409}' % keyname elif option == 'list': print json.dumps(get_keypairs(cloud, username))