[fix] put
This commit is contained in:
parent
af3636bcc1
commit
f1f8fd7042
|
|
@ -1,3 +1,6 @@
|
||||||
|
import os as _os
|
||||||
|
import pathlib as _pathlib
|
||||||
|
|
||||||
import helpers.misc as __misc
|
import helpers.misc as __misc
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -67,7 +70,14 @@ def sshconf_encode_bool(
|
||||||
return ("yes" if value else "no")
|
return ("yes" if value else "no")
|
||||||
|
|
||||||
|
|
||||||
def sshconf_encode_entry(name, entry, prefix):
|
def sshconf_encode_entry(name, entry, prefix, options = None):
|
||||||
|
options = (
|
||||||
|
{
|
||||||
|
"keys_directory": _os.path.join(_pathlib.Path.home(), ".ssh", "keys"),
|
||||||
|
}
|
||||||
|
|
|
||||||
|
(options or {})
|
||||||
|
)
|
||||||
lines = []
|
lines = []
|
||||||
lines.append(__misc.string_coin("Host {{prefix}}{{name}}", {"prefix": prefix, "name": name}))
|
lines.append(__misc.string_coin("Host {{prefix}}{{name}}", {"prefix": prefix, "name": name}))
|
||||||
if ("host" in entry):
|
if ("host" in entry):
|
||||||
|
|
@ -77,7 +87,7 @@ def sshconf_encode_entry(name, entry, prefix):
|
||||||
if ("username" in entry):
|
if ("username" in entry):
|
||||||
lines.append(__misc.string_coin("\tUser {{username}}", {"username": entry["username"]}))
|
lines.append(__misc.string_coin("\tUser {{username}}", {"username": entry["username"]}))
|
||||||
if ("key_name" in entry):
|
if ("key_name" in entry):
|
||||||
lines.append(__misc.string_coin("\tIdentityFile {{key_path}}", {"key_path": ("~/.ssh/keypairs/%s%s" % (prefix, entry["key_name"]))}))
|
lines.append(__misc.string_coin("\tIdentityFile {{key_path}}", {"key_path": _os.path.join(options["keys_directory"], ("%s%s.pub" % (prefix, entry["key_name"])))}))
|
||||||
if ("proxy_jump" in entry):
|
if ("proxy_jump" in entry):
|
||||||
lines.append(__misc.string_coin("\tProxyJump {{proxy_jump}}", {"proxy_jump": entry["proxy_jump"]}))
|
lines.append(__misc.string_coin("\tProxyJump {{proxy_jump}}", {"proxy_jump": entry["proxy_jump"]}))
|
||||||
if ("proxy_command" in entry):
|
if ("proxy_command" in entry):
|
||||||
|
|
@ -90,13 +100,23 @@ def sshconf_encode_entry(name, entry, prefix):
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def sshconf_encode(conf):
|
def sshconf_encode(conf, options = None):
|
||||||
|
options = (
|
||||||
|
{
|
||||||
|
"keys_directory": _os.path.join(_pathlib.Path.home(), ".ssh", "keys"),
|
||||||
|
}
|
||||||
|
|
|
||||||
|
(options or {})
|
||||||
|
)
|
||||||
return "\n".join(
|
return "\n".join(
|
||||||
map(
|
map(
|
||||||
lambda pair: sshconf_encode_entry(
|
lambda pair: sshconf_encode_entry(
|
||||||
pair[0],
|
pair[0],
|
||||||
pair[1],
|
pair[1],
|
||||||
conf["settings"]["prefix"]
|
conf["settings"]["prefix"],
|
||||||
|
{
|
||||||
|
"keys_directory": options["keys_directory"],
|
||||||
|
}
|
||||||
),
|
),
|
||||||
sorted(
|
sorted(
|
||||||
conf["entries"].items(),
|
conf["entries"].items(),
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import os as _os
|
||||||
import json as _json
|
import json as _json
|
||||||
import shutil as _shutil
|
import shutil as _shutil
|
||||||
import argparse as _argparse
|
import argparse as _argparse
|
||||||
|
import pathlib as _pathlib
|
||||||
|
|
||||||
import helpers.misc as __helpers_misc
|
import helpers.misc as __helpers_misc
|
||||||
import helpers.ssh as __helpers_ssh
|
import helpers.ssh as __helpers_ssh
|
||||||
|
|
@ -184,22 +185,27 @@ todo: backup (see old put script)
|
||||||
'''
|
'''
|
||||||
def action_put(
|
def action_put(
|
||||||
source_directory,
|
source_directory,
|
||||||
target_directory
|
target_directory,
|
||||||
|
target_file
|
||||||
):
|
):
|
||||||
sshconf = ""
|
sshconf = ""
|
||||||
|
keys_directory = _os.path.join(target_directory, "public_keys")
|
||||||
for group in _os.listdir(source_directory):
|
for group in _os.listdir(source_directory):
|
||||||
path_temp = _os.path.join(source_directory, group)
|
path_temp = _os.path.join(source_directory, group)
|
||||||
if (
|
if (
|
||||||
not
|
(not _os.path.isdir(path_temp))
|
||||||
|
or
|
||||||
(
|
(
|
||||||
_os.path.isdir(path_temp)
|
_os.path.isdir(path_temp)
|
||||||
and
|
and
|
||||||
|
(
|
||||||
(group == ".")
|
(group == ".")
|
||||||
and
|
or
|
||||||
(group == "..")
|
(group == "..")
|
||||||
and
|
or
|
||||||
(group == ".git")
|
(group == ".git")
|
||||||
)
|
)
|
||||||
|
)
|
||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
@ -208,17 +214,32 @@ def action_put(
|
||||||
conf_data = _json.loads(conf_content)
|
conf_data = _json.loads(conf_content)
|
||||||
# conf
|
# conf
|
||||||
if True:
|
if True:
|
||||||
sshconf = (sshconf + "\n" + __helpers_ssh.sshconf_encode(conf_data))
|
sshconf = (
|
||||||
|
"%s\n%s"
|
||||||
|
% (
|
||||||
|
sshconf,
|
||||||
|
__helpers_ssh.sshconf_encode(
|
||||||
|
conf_data,
|
||||||
|
{
|
||||||
|
"keys_directory": keys_directory,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
# public keys
|
# public keys
|
||||||
if True:
|
if True:
|
||||||
__helpers_misc.directory_create(target_directory)
|
__helpers_misc.directory_create(target_directory)
|
||||||
for name in _os.listdir(_os.path.join(source_directory, group, "keys")):
|
for name in _os.listdir(_os.path.join(source_directory, group, "keys")):
|
||||||
|
if (not name.endswith(".pub")):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
_os.makedirs(keys_directory, exist_ok = True)
|
||||||
_shutil.copy(
|
_shutil.copy(
|
||||||
_os.path.join(source_directory, group, "keys", name),
|
_os.path.join(source_directory, group, "keys", name),
|
||||||
_os.path.join(target_directory, "%s%s" % (conf_data["settings"]["prefix"], name, ))
|
_os.path.join(keys_directory, ("%s%s" % (conf_data["settings"]["prefix"], name, )))
|
||||||
)
|
)
|
||||||
# private keys
|
# private keys
|
||||||
if True:
|
if False:
|
||||||
keepass_db_path = _os.path.join(source_directory, "private_keys.kdbx")
|
keepass_db_path = _os.path.join(source_directory, "private_keys.kdbx")
|
||||||
_shutil.copy(
|
_shutil.copy(
|
||||||
keepass_db_path,
|
keepass_db_path,
|
||||||
|
|
@ -263,9 +284,16 @@ def main():
|
||||||
"-t",
|
"-t",
|
||||||
"--target-directory",
|
"--target-directory",
|
||||||
type = str,
|
type = str,
|
||||||
default = "~/.ssh",
|
default = _os.path.join(_pathlib.Path.home(), ".ssh"),
|
||||||
metavar = "<target-directory>",
|
metavar = "<target-directory>",
|
||||||
)
|
)
|
||||||
|
argument_parser.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--target-file",
|
||||||
|
type = str,
|
||||||
|
default = _os.path.join(_pathlib.Path.home(), ".ssh", "private_keys.kdbx"),
|
||||||
|
metavar = "<target-file>",
|
||||||
|
)
|
||||||
argument_parser.add_argument(
|
argument_parser.add_argument(
|
||||||
"-g",
|
"-g",
|
||||||
"--group",
|
"--group",
|
||||||
|
|
@ -310,6 +338,9 @@ def main():
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
elif (args.action == "key-generate"):
|
elif (args.action == "key-generate"):
|
||||||
|
if (args.name is None):
|
||||||
|
raise ValueError("name required")
|
||||||
|
else:
|
||||||
action_key_generate(
|
action_key_generate(
|
||||||
args.source_directory,
|
args.source_directory,
|
||||||
args.group,
|
args.group,
|
||||||
|
|
@ -324,7 +355,8 @@ def main():
|
||||||
elif (args.action == "put"):
|
elif (args.action == "put"):
|
||||||
action_put(
|
action_put(
|
||||||
args.source_directory,
|
args.source_directory,
|
||||||
args.target_directory
|
args.target_directory,
|
||||||
|
args.target_file
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError("invalid action: %s", args.action)
|
raise ValueError("invalid action: %s", args.action)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue