def create_keypair()

in playbooks/roles/cyclecloud/files/configure.py [0:0]


def create_keypair(username, public_key=None):
    user_home = "/home/{}".format(username)
    if not os.path.isdir(user_home+"/.ssh"):
        _catch_sys_error(["mkdir", "-p", user_home+"/.ssh"])
    public_key_file  = user_home+"/.ssh/id_rsa.pub"
    if not os.path.exists(public_key_file):
        if public_key:
            with open(public_key_file, 'w') as pubkeyfile:
                pubkeyfile.write(public_key)
                pubkeyfile.write("\n")
        else:
            _catch_sys_error(["ssh-keygen", "-f", user_home+"/.ssh/id_rsa", "-N", ""])
            with open(public_key_file, 'r') as pubkeyfile:
                public_key = pubkeyfile.read()

    authorized_key_file = user_home+"/.ssh/authorized_keys"
    authorized_keys = ""
    if os.path.exists(authorized_key_file):
        with open(authorized_key_file, 'r') as authkeyfile:
            authorized_keys = authkeyfile.read()
    if public_key not in authorized_keys:
        with open(authorized_key_file, 'w') as authkeyfile:
            authkeyfile.write(public_key)
            authkeyfile.write("\n")
    _catch_sys_error(["chown", "-R", username + ":" + username, user_home])
    return public_key