Compare commits

...

3 Commits

Author SHA1 Message Date
Sebastian Lohff 2aa243d2d2 Allow extra routes to be added to an interface
Routes can be specified in the format of cidr-gateway,
e.g. 10.0.0.0/8-10.0.0.1.
2019-03-28 00:44:20 +01:00
Sebastian Lohff 158c71c1cc Remove cloud-init after first run by default 2019-03-27 23:57:57 +01:00
Sebastian Lohff 4092a9a519 Add thin README 2019-03-27 23:25:13 +01:00
2 changed files with 23 additions and 4 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# genconfdrv
Generate a config drive for cloud-init. Requires `genisoimage` to be installed.
Currently only tested with Debian and `.deb`-based Linux distributions.

View File

@ -36,7 +36,7 @@ class ConfigDrive:
def set_hostname(self, hostname): def set_hostname(self, hostname):
self._hostname = hostname self._hostname = hostname
def conf_network(self, interface, address=None, gateway=None): def conf_network(self, interface, address=None, gateway=None, *extra_routes):
if not address and gateway: if not address and gateway:
raise ValueError("You cannot define a gateway, but supply no address") raise ValueError("You cannot define a gateway, but supply no address")
@ -66,6 +66,16 @@ class ConfigDrive:
if gateway: if gateway:
self._interfaces.append(" gateway %s" % str(ipaddress.ip_address(gateway))) self._interfaces.append(" gateway %s" % str(ipaddress.ip_address(gateway)))
for routedef in extra_routes:
if "-" not in routedef:
raise ValueError("Route {} is missing a gateway separated by a -".format(routedef))
route, gw = routedef.split('-')
if "/" not in route:
raise ValueError("Route {} is not a subnet".format(route))
route = ipaddress.ip_interface(route)
gw = ipaddress.ip_address(gw)
self._interfaces.append(" up ip route add {} via {}".format(route, gw))
def conf_resolve(self, resolvers): def conf_resolve(self, resolvers):
if not self._hostname: if not self._hostname:
raise ValueError("Please set a hostname before calling this function") raise ValueError("Please set a hostname before calling this function")
@ -263,12 +273,16 @@ def main():
parser.add_argument("-o", "--output", required=True, help="Path to write iso to") parser.add_argument("-o", "--output", required=True, help="Path to write iso to")
parser.add_argument("-n", "--nameservers", "--ns", default=["1.1.1.1", "8.8.8.8"], nargs="+", help="Nameservers") parser.add_argument("-n", "--nameservers", "--ns", default=["1.1.1.1", "8.8.8.8"], nargs="+", help="Nameservers")
parser.add_argument("-i", "--networks", "--net", default=[], nargs="+", parser.add_argument("-i", "--networks", "--net", default=[], nargs="+",
help="Specify all networks, in format of interface[:address:[gateway]]. " help="Specify all networks, in format of interface[:address[:gateway[:route-gateway[:...]]]]. "
"Both : and ; can be used as delimiter (but only one per net config). " "Both : and ; can be used as delimiter (but only one per net config). "
"Address MUST be a network in CIDR notation") "Address MUST be a network in CIDR notation. Additional "
"routes can be added in the form of cidr-gateway, e.g. "
"10.0.0.0/8-10.0.0.1")
parser.add_argument("-u", "--disable-upgrades", action="store_true", default=False) parser.add_argument("-u", "--disable-upgrades", action="store_true", default=False)
parser.add_argument("-v", "--verbose", action="store_true", default=False) parser.add_argument("-v", "--verbose", action="store_true", default=False)
parser.add_argument("--no-debian-cleanup", "--ndc", action="store_true", default=False) parser.add_argument("--no-debian-cleanup", "--ndc", action="store_true", default=False)
parser.add_argument("--no-remove-cloud-init", action="store_true", default=False,
help="Do not purge cloud-init from system after execution")
parser.add_argument("--set-root-password", "--srp", default=None) parser.add_argument("--set-root-password", "--srp", default=None)
parser.add_argument("-a", "--add-user", default=[], nargs="+", parser.add_argument("-a", "--add-user", default=[], nargs="+",
help="Add users, format is username:key?:sudo?:gecos?:password?, " help="Add users, format is username:key?:sudo?:gecos?:password?, "
@ -308,7 +322,6 @@ def main():
"rm /etc/apt/sources.list.d/*", True) "rm /etc/apt/sources.list.d/*", True)
cfgdrv.add_command("sed -rni '/^([^#]|## template)/p' " cfgdrv.add_command("sed -rni '/^([^#]|## template)/p' "
"/etc/resolv.conf /etc/cloud/templates/resolv.conf.tmpl", True) "/etc/resolv.conf /etc/cloud/templates/resolv.conf.tmpl", True)
if args.set_root_password: if args.set_root_password:
cfgdrv.set_password("root", args.set_root_password) cfgdrv.set_password("root", args.set_root_password)
@ -340,6 +353,9 @@ def main():
password = user[4] password = user[4]
cfgdrv.add_user(user[0], keys, sudo=sudo, gecos=gecos, password=password) cfgdrv.add_user(user[0], keys, sudo=sudo, gecos=gecos, password=password)
if not args.no_remove_cloud_init:
cfgdrv.add_command("apt remove -y cloud-init")
if args.output: if args.output:
cfgdrv.write_drive(args.output, args.format) cfgdrv.write_drive(args.output, args.format)
finally: finally: