Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snap support #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
src/config.py
src/config.pyc
parts
prime
snap/.snapcraft
stage
*.snap
33 changes: 33 additions & 0 deletions snap/hooks/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh
set -e

# CONFIG FILE PATH
CONFIG_FILE=$SNAP_DATA/config

# CONFIG OPTIONS: username, port, something-else
OPTIONS="apisecret apiendpoint domain subdomains ttl ifconfig"

## add or replace an option inside the config file. Create the file if doesn't exist
refresh_opt_in_config() {
opt=$1
value="$2"
replace_line="$opt=$value"
if [ ! -e $CONFIG_FILE ]; then
echo "[settings]" > $CONFIG_FILE
fi
if $(grep -q "$opt=" $CONFIG_FILE 2> /dev/null); then
sed "s/^$opt=.*/$replace_line/" $CONFIG_FILE 2>/dev/null >${CONFIG_FILE}.new
mv -f ${CONFIG_FILE}.new $CONFIG_FILE 2>/dev/null
else
echo $replace_line >> $CONFIG_FILE
fi
}

# Iterate through the config options array
for opt in $OPTIONS
do
# Use snapctl to get the value registered by the snap set command
echo "Configuration changed '$opt': $(snapctl get $opt)"
refresh_opt_in_config $opt $(snapctl get $opt)
done

36 changes: 36 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: gandi-live-dns
version: git
summary: GANDI dynamic DNS updater
description: |
This is a simple dynamic DNS updater for the Gandi registrar. It uses their
LiveDNS REST API to update the zone file for a subdomain of a domain to point
at the external IPv4 address of the computer it has been run from.

It has been developed on Debian 8 Jessie and tested on Debian 9 Stretch
GNU/Linux using Python 2.7.

With the new v5 Website, Gandi has also launched a new REST API which makes it
easier to communicate via bash/curl or python/requests.

grade: stable
confinement: strict

parts:
gandi-live-dns:
plugin: nil
stage-packages:
- unzip
- python-requests
- python-args
- python-simplejson
override-build: |
cp -v src/snap.config.py src/config.py
mv -v src/gandi-live-dns.py \
src/config.py \
$SNAPCRAFT_PART_INSTALL
snapcraftctl build

apps:
gandi-live-dns:
command: python $SNAP/gandi-live-dns.py
plugs: [network]
68 changes: 68 additions & 0 deletions src/snap.config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python
# encoding: utf-8

import ConfigParser
from ConfigParser import NoOptionError
import os

snap = os.environ['SNAP_NAME']
config_file = os.path.join(os.environ['SNAP_DATA'], 'config')

def error_msg(key):
return ("No `{key}` value set, you need to configure this snap using " \
"`snap set {snap} {key}=<value>`".format(key=key, snap=snap))

if not os.path.exists(config_file):
raise Exception("No configuration file found at {}".format(config_file))

try:
config = ConfigParser.ConfigParser()
config.readfp(open(config_file))
except:
raise Exception("Impossible to open config file {}".format(config_file))

def get_setting(key):
try:
s = config.get('settings', key)
if not len(s):
raise NoOptionError('settings', key)
except NoOptionError as e:
raise e
return s

try:
api_secret = get_setting('apisecret')
except NoOptionError as e:
print(error_msg("apisecret"))
print('''
Get your API key
Start by retrieving your API Key from the "Security" section in new Account admin panel to be able to make authenticated requests to the API.
https://account.gandi.net/
''')
raise e

try:
api_endpoint = get_setting('apiendpoint')
except NoOptionError as e:
api_endpoint = 'https://dns.api.gandi.net/api/v5'

try:
domain = config.get("settings", "domain")
except NoOptionError as e:
print(error_msg("domain"))
raise e

try:
subdomains = get_setting('subdomains').split(',')
except NoOptionError as e:
subdomains = ''

try:
ttl = get_setting('ttl')
except NoOptionError as e:
ttl = '300'

try:
ifconfig = get_setting('ifconfig')
except NoOptionError as e:
ifconfig = 'http://ipv4.myexternalip.com/raw'