def generate_image(d): """ Generates an image accoording to given configuration. """ logging.debug(repr(d)) if d['imagebuilder'] not in IMAGEBUILDERS: raise Exception("Invalid imagebuilder specified!") x = OpenWrtConfig() x.setUUID(d['uuid']) x.setOpenwrtVersion(d['openwrt_ver']) x.setArch(d['arch']) x.setPortLayout(d['port_layout']) x.setWifiIface(d['iface_wifi'], d['driver'], d['channel']) x.setWifiAnt(d['rx_ant'], d['tx_ant']) x.setLanIface(d['iface_lan']) x.setNodeType("adhoc") x.setPassword(d['root_pass']) x.setHostname(d['hostname']) x.setIp(d['ip']) x.setSSID(d['ssid']) # Add WAN interface and all subnets if d['wan_dhcp']: x.addInterface("wan", d['iface_wan'], init = True) else: x.addInterface("wan", d['iface_wan'], d['wan_ip'], d['wan_cidr'], d['wan_gw'], init = True) for subnet in d['subnets']: x.addSubnet(str(subnet['iface']), str(subnet['network']), subnet['cidr'], subnet['dhcp'], True) x.setCaptivePortal(d['captive_portal']) if d['vpn']: x.setVpn(d['vpn_username'], d['vpn_password'], d['vpn_mac'], d['vpn_limit_down'], d['vpn_limit_up']) if d['lan_wifi_bridge']: x.enableLanWifiBridge() if d['lan_wan_switch']: x.switchWanToLan() # Add optional packages for package in d['opt_pkg']: x.addPackage(package) # Cleanup stuff from previous builds os.chdir(WORKDIR) os.system("rm -rf build/files/*") os.system("rm -rf build/%s/bin/*" % d['imagebuilder']) os.mkdir("build/files/etc") x.generate("build/files/etc") if d['only_config']: # Just pack configuration and send it prefix = hashlib.md5(os.urandom(32)).hexdigest()[0:16] tempfile = os.path.join(DESTINATION, prefix + "-config.zip") zip = ZipFile(tempfile, 'w', ZIP_DEFLATED) os.chdir('build/files') for root, dirs, files in os.walk("etc"): for file in files: zip.write(os.path.join(root, file)) zip.close() # Generate checksum f = open(tempfile, 'r') checksum = hashlib.md5(f.read()) f.close() # We can take just first 22 characters as checksums are fixed size and we can reconstruct it filechecksum = urlsafe_b64encode(checksum.digest())[:22] checksum = checksum.hexdigest() result = "%s-%s-config-%s.zip" % (d['hostname'], d['router_name'], filechecksum) destination = os.path.join(DESTINATION, result) os.rename(tempfile, destination) # Send an e-mail t = loader.get_template('generator/email_config.txt') c = Context({ 'hostname' : d['hostname'], 'ip' : d['ip'], 'username' : d['vpn_username'], 'config' : result, 'checksum' : checksum, 'network' : { 'name' : settings.NETWORK_NAME, 'home' : settings.NETWORK_HOME, 'contact' : settings.NETWORK_CONTACT, 'description' : getattr(settings, 'NETWORK_DESCRIPTION', None) }, 'images_bindist_url' : getattr(settings, 'IMAGES_BINDIST_URL', None) }) send_mail( settings.EMAIL_SUBJECT_PREFIX + (_("Configuration for %s/%s") % (d['hostname'], d['ip'])), t.render(c), settings.EMAIL_IMAGE_GENERATOR_SENDER, [d['email']], fail_silently = False ) else: # Generate full image x.build("build/%s" % d['imagebuilder']) # Read image version try: f = open(glob('%s/build/%s/build_dir/target-*/root-*/etc/version' % (WORKDIR, d['imagebuilder']))[0], 'r') version = f.read().strip() version = re.sub(r'\W+', '_', version) version = re.sub(r'_+', '_', version) f.close() except: version = 'unknown' # Get resulting image files = [] for file, type in d['imagefiles']: file = str(file) source = "%s/build/%s/bin/%s" % (WORKDIR, d['imagebuilder'], file) f = open(source, 'r') checksum = hashlib.md5(f.read()) f.close() # We can take just first 22 characters as checksums are fixed size and we can reconstruct it filechecksum = urlsafe_b64encode(checksum.digest())[:22] checksum = checksum.hexdigest() ext = os.path.splitext(file)[1] router_name = d['router_name'].replace('-', '') result = "%s-%s-%s%s-%s%s" % (d['hostname'], router_name, version, ("-%s" % type if type else "-all"), filechecksum, ext) destination = os.path.join(DESTINATION, result) os.rename(source, destination) files.append({ 'name' : result, 'checksum' : checksum }) # Send an e-mail t = loader.get_template('generator/email.txt') c = Context({ 'hostname' : d['hostname'], 'ip' : d['ip'], 'username' : d['vpn_username'], 'files' : files, 'network' : { 'name' : settings.NETWORK_NAME, 'home' : settings.NETWORK_HOME, 'contact' : settings.NETWORK_CONTACT, 'description' : getattr(settings, 'NETWORK_DESCRIPTION', None) }, 'images_bindist_url' : getattr(settings, 'IMAGES_BINDIST_URL', None) }) send_mail( settings.EMAIL_SUBJECT_PREFIX + (_("Router images for %s/%s") % (d['hostname'], d['ip'])), t.render(c), settings.EMAIL_IMAGE_GENERATOR_SENDER, [d['email']], fail_silently = False )
dest='no_build', help='Just generate configuration - do not build an image.') parser.add_option('--imagebuilder-dir', dest='imagebuilder_dir', default='./imagebuilder', help='Set OpenWRT imagebuilder directory.') (options, args) = parser.parse_args() options = options.__dict__ # Cleanup stuff from previous builds os.system("rm -rf files/*") os.system("rm -rf imagebuilder/bin/*") os.mkdir("files/etc") x = OpenWrtConfig() for key in ('iface', 'driver', 'password', 'hostname', 'ip', 'layout'): if not options[key]: print "Error: You have to specify --interface, --driver, --password, --hostname and --ip!" exit() if options['layout'] not in portLayouts: print "Error: Port layout '%s' is not supported!" % options['layout'] exit() x.setOpenwrtVersion(options['openwrt_version']) x.setArch(options['arch']) x.setPortLayout(options['layout']) x.setWifiIface(options['iface'], options['driver'], int(options['channel'])) x.setWifiAnt(4, 4)
parser.add_option('--vpn-password', dest = 'vpn_password', help = 'Specifies the assigned VPN password.') parser.add_option('--no-build', action = 'store_true', dest = 'no_build', help = 'Just generate configuration - do not build an image.') parser.add_option('--imagebuilder-dir', dest = 'imagebuilder_dir', default = './imagebuilder', help = 'Set OpenWRT imagebuilder directory.') (options, args) = parser.parse_args() options = options.__dict__ # Cleanup stuff from previous builds os.system("rm -rf files/*") os.system("rm -rf imagebuilder/bin/*") os.mkdir("files/etc") x = OpenWrtConfig() for key in ('iface', 'driver', 'password', 'hostname', 'ip', 'layout'): if not options[key]: print "Error: You have to specify --interface, --driver, --password, --hostname and --ip!" exit() if options['layout'] not in portLayouts: print "Error: Port layout '%s' is not supported!" % options['layout'] exit() x.setOpenwrtVersion(options['openwrt_version']) x.setArch(options['arch']) x.setPortLayout(options['layout']) x.setWifiIface(options['iface'], options['driver'], int(options['channel'])) x.setWifiAnt(4, 4)
parser.add_option( "--no-build", action="store_true", dest="no_build", help="Just generate configuration - do not build an image." ) parser.add_option( "--imagebuilder-dir", dest="imagebuilder_dir", default="./imagebuilder", help="Set OpenWRT imagebuilder directory." ) (options, args) = parser.parse_args() options = options.__dict__ # Cleanup stuff from previous builds os.system("rm -rf files/*") os.system("rm -rf imagebuilder/bin/*") os.mkdir("files/etc") x = OpenWrtConfig() for key in ("iface", "driver", "password", "hostname", "ip", "layout"): if not options[key]: print "Error: You have to specify --interface, --driver, --password, --hostname and --ip!" exit() if options["layout"] not in portLayouts: print "Error: Port layout '%s' is not supported!" % options["layout"] exit() x.setOpenwrtVersion(options["openwrt_version"]) x.setArch(options["arch"]) x.setPortLayout(options["layout"]) x.setWifiIface(options["iface"], options["driver"], int(options["channel"])) x.setWifiAnt(4, 4)