diff options
author | Richard Ipsum <richard.ipsum@codethink.co.uk> | 2015-03-18 16:56:30 +0000 |
---|---|---|
committer | Richard Ipsum <richard.ipsum@codethink.co.uk> | 2015-03-18 16:56:30 +0000 |
commit | bd14dd4f152c1423d3f37e0b1bd0b0355964b584 (patch) | |
tree | d270b2636c3454d900c35ab905fdecbde207f263 | |
parent | a0ba16844eb30f6a901e81b66845d0e2ef241f99 (diff) | |
parent | ae7b8b46e44a0a3130de4ab852a6a373337a3289 (diff) | |
download | definitions-bd14dd4f152c1423d3f37e0b1bd0b0355964b584.tar.gz |
Merge branch 'baserock/richardipsum/add-hosts-conf-ext-v3'
Reviewed by:
Richard Maw <richard.maw@gmail.com> (+2)
Pedro Alvarez <pedro.alvarez@codethink.co.uk> (v2) (+1)
-rwxr-xr-x | fstab.configure | 25 | ||||
-rwxr-xr-x | hosts.configure | 48 |
2 files changed, 55 insertions, 18 deletions
diff --git a/fstab.configure b/fstab.configure index 3bbc9102..b9154eee 100755 --- a/fstab.configure +++ b/fstab.configure @@ -1,5 +1,6 @@ -#!/usr/bin/python -# Copyright (C) 2013,2015 Codethink Limited +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright © 2013-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,21 +20,9 @@ import os import sys +import morphlib -def asciibetical(strings): +envvars = {k: v for (k, v) in os.environ.iteritems() if k.startswith('FSTAB_')} - def key(s): - return [ord(c) for c in s] - - return sorted(strings, key=key) - - -fstab_filename = os.path.join(sys.argv[1], 'etc', 'fstab') - -fstab_vars = asciibetical(x for x in os.environ if x.startswith('FSTAB_')) -with open(fstab_filename, 'a') as f: - for var in fstab_vars: - f.write('%s\n' % os.environ[var]) - -os.chown(fstab_filename, 0, 0) -os.chmod(fstab_filename, 0644) +conf_file = os.path.join(sys.argv[1], 'etc/fstab') +morphlib.util.write_from_dict(conf_file, envvars) diff --git a/hosts.configure b/hosts.configure new file mode 100755 index 00000000..6b068d04 --- /dev/null +++ b/hosts.configure @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright © 2015 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# =*= License: GPL-2 =*= + + +import os +import sys +import socket + +import morphlib + +def validate(var, line): + xs = line.split() + if len(xs) == 0: + raise morphlib.Error("`%s: %s': line is empty" % (var, line)) + + ip = xs[0] + hostnames = xs[1:] + + if len(hostnames) == 0: + raise morphlib.Error("`%s: %s': missing hostname" % (var, line)) + + family = socket.AF_INET6 if ':' in ip else socket.AF_INET + + try: + socket.inet_pton(family, ip) + except socket.error: + raise morphlib.Error("`%s: %s' invalid ip" % (var, ip)) + +envvars = {k: v for (k, v) in os.environ.iteritems() if k.startswith('HOSTS_')} + +conf_file = os.path.join(sys.argv[1], 'etc/hosts') +morphlib.util.write_from_dict(conf_file, envvars, validate) |