def run_ssh(): try: crm_script.service('sshd', 'start') rc, _, _ = crm_script.sudo_call( ["mkdir", "-m", "700", "-p", "/root/.ssh"]) if rc != 0: crm_script.exit_fail("Failed to create /root/.ssh directory") keypath = None for key in ('id_rsa', 'id_dsa', 'id_ecdsa'): if os.path.exists(os.path.join('/root/.ssh', key)): keypath = os.path.join('/root/.ssh', key) break if not keypath: keypath = os.path.join('/root/.ssh', 'id_rsa') keygen = [ 'ssh-keygen', '-q', '-f', keypath, '-C', 'Cluster Internal', '-N', '' ] rc, out, err = crm_script.sudo_call(keygen) if rc != 0: crm_script.exit_fail("Failed to generate SSH key") _authorize_key(keypath) crm_script.exit_ok(True) except IOError, e: crm_script.exit_fail(str(e))
def _authorize_key(keypath): "add key to authorized_keys" pubkeypath = ''.join([keypath, '.pub']) if os.path.exists('/root/.ssh/authorized_keys'): pubkey = open(pubkeypath).read() if pubkey not in open('/root/.ssh/authorized_keys').read(): crm_script.sudo_call("cat %s >> /root/.ssh/authorized_keys" % (pubkeypath)) else: crm_script.sudo_call(["cp", pubkeypath, '/root/.ssh/authorized_keys'])
def gen_authkey(): if not os.path.isfile(COROSYNC_AUTH): rc, out, err = crm_script.sudo_call(['corosync-keygen', '-l']) if rc != 0: crm_script.exit_fail("Error generating key: %s" % (err)) elif stat.S_IMODE(os.stat(COROSYNC_AUTH)[stat.ST_MODE]) != stat.S_IRUSR: os.chmod(COROSYNC_AUTH, stat.S_IRUSR)
def run_ssh(): try: crm_script.service('sshd', 'start') rc, _, _ = crm_script.sudo_call(["mkdir", "-m", "700", "-p", "/root/.ssh"]) if rc != 0: crm_script.exit_fail("Failed to create /root/.ssh directory") keypath = None for key in ('id_rsa', 'id_dsa', 'id_ecdsa'): if os.path.exists(os.path.join('/root/.ssh', key)): keypath = os.path.join('/root/.ssh', key) break if not keypath: keypath = os.path.join('/root/.ssh', 'id_rsa') keygen = ['ssh-keygen', '-q', '-f', keypath, '-C', 'Cluster Internal', '-N', ''] rc, out, err = crm_script.sudo_call(keygen) if rc != 0: crm_script.exit_fail("Failed to generate SSH key") _authorize_key(keypath) crm_script.exit_ok(True) except IOError, e: crm_script.exit_fail(str(e))