Compare commits

...

11 commits
v1.0.0 ... main

6 changed files with 105 additions and 25 deletions

29
install.sh Executable file
View file

@ -0,0 +1,29 @@
#!/bin/bash
destination="/usr/local/bin"
while [ $# -gt 0 ]
do
case "$1" in
"--help"|"-h"|"-?")
usage
exit 0
;;
"--destination"|"-d")
shift
destination="${1}"
shift
;;
*)
echo "Ignoring unknwon parameter '${1}'"
shift
;;
esac
done
if [ ! -e "${HOME}/.config/ovh_dns_ensure.conf" ]; then
touch "${HOME}/.config/ovh_dns_ensure.conf"
fi
chmod go-rwx "${HOME}/.config/ovh_dns_ensure.conf"
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
sed "s#__src_folder__#${script_dir}#g" "${script_dir}/wrapper.sh" > "${destination}/ovh_dns_ensure.sh"
chmod +x "${destination}/ovh_dns_ensure.sh"

View file

@ -1,9 +1,10 @@
#!/bin/bash #!/bin/bash
if [ ! -d "$(dirname "${0}")/.venv" ]; then script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
python -m venv "$(dirname "${0}")/.venv" if [ ! -d "${script_dir}/.venv" ]; then
python -m venv "$script_dir/.venv"
fi fi
# shellcheck disable=1091 # shellcheck disable=1091
source "$(dirname "${0}")/.venv/bin/activate" source "$script_dir/.venv/bin/activate"
pip install -r "$(dirname "${0}")/requirements.txt" > /dev/null pip install -r "$script_dir/requirements.txt" > /dev/null
pip install "$(dirname "${0}")/" > /dev/null pip install "$script_dir/" > /dev/null
ovh_dns_ensure.py "${@}" ovh_dns_ensure.py "${@}"

0
ovh_dns_ensure/__init__.py Normal file → Executable file
View file

68
ovh_dns_ensure/ovh_dns_ensure.py Normal file → Executable file
View file

@ -68,45 +68,62 @@ class OvhDnsEnsure:
) )
self._log.debug(result) self._log.debug(result)
elif self.current_state == 'different': elif self.current_state == 'different':
count = 0 if not self.config['allow_multiple']:
for record in self.records: for record in self.records:
if count > 0:
if not self.config['dummy']: if not self.config['dummy']:
self._log.debug( self._log.debug(
'Deleting record %s', 'Deleting record %s',
record record
) )
result = self.ovh.delete(f"/domain/zone/{self.config['zone']}/record/{record}") result = self.ovh.delete(
self._log.debug(result) f"/domain/zone/{self.config['zone']}/record/{record}"
else:
self._log.debug(
'Changing record %s',
record
)
if not self.config['dummy']:
result = self.ovh.put(f"/domain/zone/{self.config['zone']}/record/{record}",
subDomain = self.config['subdomain'],
target = self.config['target'],
ttl = self.config['ttl'],
) )
self._log.debug(result) self._log.debug(result)
count += 1 else:
self._log.debug(
"Not deleting existing records since we were called with --allow-multiple."
)
self._log.debug(
"Creating new record..."
)
if not self.config['dummy']:
result = self.ovh.post(f"/domain/zone/{self.config['zone']}/record/",
subDomain = self.config['subdomain'],
fieldType = self.config['type'],
target = self.config['target'],
ttl = self.config['ttl'],
)
self._log.debug(result)
def _get_current_state(self): def _get_current_state(self):
params = { params = {
"fieldType": self.config['type'], # "fieldType": self.config['type'],
"subDomain": self.config['subdomain'] "subDomain": self.config['subdomain']
} }
self._log.debug(
"Getting all records with subdomain '%s'...",
# self.config['type'],
self.config['subdomain']
)
self.records = self.ovh.get(f"/domain/zone/{self.config['zone']}/record", **params) self.records = self.ovh.get(f"/domain/zone/{self.config['zone']}/record", **params)
self.current_state = 'absent' self.current_state = 'absent'
for record in self.records: for record in self.records:
self._log.debug(
"Checking record: %s...",
record
)
data = self.ovh.get(f"/domain/zone/{self.config['zone']}/record/{record}") data = self.ovh.get(f"/domain/zone/{self.config['zone']}/record/{record}")
if data['target'] == self.config['target'] and data['ttl'] == self.config['ttl']: if data['target'] == self.config['target'] and data['ttl'] == self.config['ttl']:
self.current_state = 'same' self.current_state = 'same'
self._log.debug('A record with the same type, TTL and target exists. %s', data) self._log.debug(
'A record with the same type, TTL and target exists. %s',
data
)
if self.current_state == 'absent' and len(self.records) == 0: if self.current_state == 'absent' and len(self.records) == 0:
self._log.debug("Doesn't exist a record with that type and target") self._log.debug(
"Doesn't exist a record with that type and target"
)
elif self.current_state == 'absent' and len(self.records) > 0: elif self.current_state == 'absent' and len(self.records) > 0:
self._log.debug( self._log.debug(
"There are %s records with same type but different target or TTL", "There are %s records with same type but different target or TTL",
@ -200,7 +217,10 @@ class OvhDnsEnsure:
required=True, required=True,
default='A', default='A',
type=click.Choice( type=click.Choice(
["A", "AAAA", "CAA", "CNAME", "DKIM", "DMARC", "DNAME", "LOC", "MX", "NAPTR", "NS", "PTR", "SPF", "SRV", "SSHFP", "TLSA", "TXT"], [
"A", "AAAA", "CAA", "CNAME", "DKIM",
"DMARC", "DNAME", "LOC", "MX", "NAPTR",
"NS", "PTR", "SPF", "SRV", "SSHFP", "TLSA", "TXT"],
case_sensitive=False, case_sensitive=False,
), ),
help='DNS record type' help='DNS record type'
@ -261,6 +281,14 @@ class OvhDnsEnsure:
), ),
help='OVH Server to use.' help='OVH Server to use.'
) )
@click.option(
'--allow-multiple',
'-m',
is_flag=True,
default=False,
required=True,
help='OVH Consumer key. Better use the configuration file for safety.'
)
@click_config_file.configuration_option() @click_config_file.configuration_option()
def __main__(**kwargs): def __main__(**kwargs):
return OvhDnsEnsure(**kwargs) return OvhDnsEnsure(**kwargs)

0
setup.py Normal file → Executable file
View file

22
wrapper.sh Executable file
View file

@ -0,0 +1,22 @@
#!/bin/bash
if [ -z "${HOME}" ]; then
if [ "$(whoami)" == "root" ]; then
HOME="/root"
else
HOME=$(grep "$(whoami)" /etc/passwd | awk 'BEGIN {FS=":"} {print($6)}')
fi
fi
CONFIG_FILE="${HOME}/.config/ovh_dns_ensure.conf"
cd "__src_folder__" || exit 1
if [ -r "${CONFIG_FILE}" ]; then
perms=$(stat -c %A "${CONFIG_FILE}")
if [ "${perms:4:6}" != '------' ]; then
echo "Permissions too open for config file '${CONFIG_FILE}' ($perms). Remove all permissions to group and others."
exit 1
fi
config=(--config "${CONFIG_FILE}")
else
config=()
fi
"__src_folder__/ovh_dns_ensure.sh" "${config[@]}" "${@}"