示例#1
0
    def _download_links_write(self):
        """ Write a file to static/ajax/<random number> containing all
            Download Links
        """
        files = []
        for filename in os.listdir(self.BinDir):
            size = os.path.getsize(os.path.join(self.BinDir, filename))
            files.append({'name': filename, 'size': size})
        r = json.dumps(sorted(files))

        # Write info to file
        static_ajax_file = os.path.join(abspath(request.folder), "static",
                                        "ajax", self.Rand)
        mkutils.write_file(static_ajax_file, str(r))
示例#2
0
    def _download_links_write(self):
        """ Write a file to static/ajax/<random number> containing all
            Download Links
        """
        files = []
        for filename in os.listdir(self.BinDir):
            size = os.path.getsize(os.path.join(self.BinDir, filename))
            files.append({'name': filename, 'size': size})
        r = json.dumps(sorted(files))

        # Write info to file
        static_ajax_file = os.path.join(
            abspath(request.folder), "static", "ajax", self.Rand
        )
        mkutils.write_file(static_ajax_file, str(r))
示例#3
0
    def _build(self):
        status = 3
        out = ''
        settings_summary_json = {}
        logger.info('Build started, ID: %s, Target: %s' %
                    (self.Id, self.Target))
        if self._createdirectories():
            if not self.Noconf:
                self._createconfig()

            # write summary to output directory
            settings_summary_json = self._summary_json()
            self._summary_json_write(settings_summary_json)

            # handle files in <meshkit>/files
            mkfilesdir = os.path.join(request.folder, "files")
            if os.path.exists(mkfilesdir):
                mkutils.cptree(mkfilesdir, self.FilesDir)
                logger.info('Copied files from %s to %s.' %
                            (mkfilesdir, self.FilesDir))

            # handle files/ in imagebuilder
            ibfilesdir = os.path.join(config.buildroots_dir, self.Target,
                                      "files")
            if os.path.exists(ibfilesdir):
                mkutils.cptree(ibfilesdir, self.FilesDir)

            # handle community files (custom files uploaded by community)
            if config.communitysupport and config.communityfiles_dir:
                cfilesdir = os.path.join(config.communityfiles_dir,
                                         self.Community, "files")
                logger.info('Copied files from %s to %s' %
                            (cfilesdir, self.FilesDir))
                if os.path.exists(cfilesdir):
                    mkutils.cptree(cfilesdir, self.FilesDir)
                    if self.Community == 'weimar':
                        filesdir = "%s/etc/init.d/apply_profile.code" % \
                            self.FilesDir
                        with open(filesdir) as source:
                            data = source.read()
                            data1 = data.replace("#SIM_ARG1=\"olympia\"",
                                                 "SIM_ARG1=\"ffweimar\"")
                            data2 = data1.replace("#SIM_ARG2=\"adhoc\"",
                                                  "SIM_ARG2=\"hybrid\"")
                            logger.info("node number: " + self.Nodenumber)
                            data3 = data2.replace(
                                "#SIM_ARG3=2", "SIM_ARG3=" + self.Nodenumber)
                            source.seek(0)
                            source.write(data3)
                            source.close()

            # Handle uploaded archive
            if self.Upload:
                uploaded_file = os.path.join(request.folder, "uploads",
                                             self.Upload)
                if os.access(uploaded_file, os.R_OK):
                    logger.info('extracting %s' % uploaded_file)
                    pu_ret = processupload.extract(uploaded_file,
                                                   self.FilesDir)
                    if pu_ret:
                        logger.warning(str(pu_ret))
                    # delete uploaded file
                    try:
                        os.remove(uploaded_file)
                        logger.debug('Deleted %s' % uploaded_file)
                    except:
                        logger.warning('Could not delete %s' % uploaded_file)

            # Copy community profile
            if config.communitysupport and self.Community:
                communityprofile = os.path.join(config.profiles,
                                                'profile_' + self.Community)
                if not os.path.exists(communityprofile):
                    logger.warning('The communityfile %s does not exist!' %
                                   communityprofile)
                else:
                    logger.debug('Copied %s to %s' %
                                 (communityprofile, self.FilesDirConfig))
                    shutil.copy(communityprofile, self.FilesDirConfig)

            # construct the commandline
            path = os.path.join(config.buildroots_dir, self.Target)
            cmdline = ["make", "image"]
            if len(self.Profile) > 0:
                cmdline.append("PROFILE=%s" % self.Profile)
            if self.Pkgs:
                cmdline.append("PACKAGES=%s" % self.Pkgs)
            if len(os.listdir(self.FilesDir)) > 0:
                cmdline.append("FILES=%s" % self.FilesDir)
            if len(self.BinDir) > 0:
                cmdline.append("BIN_DIR=%s" % self.BinDir)

            proc = subprocess.Popen(cmdline,
                                    cwd=path,
                                    stdout=subprocess.PIPE,
                                    shell=False,
                                    stderr=subprocess.STDOUT)

            out, _ = proc.communicate()
            ret = proc.returncode

            self._download_links_write()
            if ret != 0:
                if ret < 0:
                    logger.critical('make was killed by signal %s', str(ret))
                else:
                    logger.critical('make failed with return code %s',
                                    str(ret))
                status = 2
            else:
                status = 0

            # write build log
            build_log_file = os.path.join(self.BinDir, "build.log")
            mkutils.write_file(build_log_file, out)

        return status, out, settings_summary_json
示例#4
0
    def _createconfig(self):
        """ Write uci config file for meshwizard and meshkit to
            FilesDirConfig/meshwizard
        """
        def add_section(type, name):
            self.mkconfig += "config %s '%s'\n" % (type, name)

        def add_option(option, value):
            self.mkconfig += "\toption %s '%s'\n" % (option, value)

        def add_list(option, value):
            self.mkconfig += "\tlist %s '%s'\n" % (option, value)

        # section system
        add_section('system', 'system')
        add_option('hostname', self.Hostname)
        add_option('conloglevel', '8')
        add_option('cronloglevel', '9')
        add_option('latitude', self.Latitude)
        add_option('longitude', self.Longitude)
        add_option('location', self.Location)
        self.mkconfig += "\n"

        # ssh pubkeys
        if self.Pubkeys and len(self.Pubkeys) > 0:
            add_section('system', 'ssh')
            for k in self.Pubkeys:
                add_list('pubkey', k)
            self.mkconfig += "\n"

        # section community
        add_section('public', 'community')
        add_option('name', self.Community)
        self.mkconfig += "\n"

        # section contact
        add_section('public', 'contact')
        add_option('nickname', self.Nickname)
        add_option('name', self.Name)
        add_list('homepage', self.Homepage)
        add_option('mail', self.Email)
        add_option('phone', self.Phone)
        add_option('note', self.Note)
        self.mkconfig += "\n"

        # section luci_main
        add_section('core', 'luci_main')
        add_option('mediaurlbase', "/luci-static/%s" % self.Theme)
        self.mkconfig += "\n"

        # section netconfig
        add_section('netconfig', 'netconfig')
        wifi_counter = 0
        for row_wifi in self.rows_wifi:
            counter = str(wifi_counter)
            add_option("IB_wifi%s_config" % counter, '1')

            if row_wifi.ipv4addr:
                add_option("IB_wifi%s_ip4addr" % counter, row_wifi.ipv4addr)

            if row_wifi.chan:
                add_option("IB_wifi%s_channel" % counter, row_wifi.chan)

            if row_wifi.dhcp:
                add_option("IB_wifi%s_dhcp" % counter, row_wifi.dhcp and "1"
                           or "0")

            if row_wifi.dhcprange:
                add_option("IB_wifi%s_dhcprange" % counter, row_wifi.dhcprange)

            if row_wifi.vap:
                add_option("IB_wifi%s_vap" % counter, row_wifi.vap and "1"
                           or "0")

            if row_wifi.ipv6addr and self.Ipv6 and \
               self.Ipv6_config == 'static':
                add_option("IB_wifi%s_ip6addr" % counter, row_wifi.wifiipv6)

            if row_wifi.ipv6ra and (self.Ipv6 and
                                    (self.Ipv6_config == 'auto-ipv6-random'
                                     or self.Ipv6_config == 'auto-ipv6-fromv4'
                                     or self.Ipv6_config == 'static')):

                add_option("IB_wifi%s_ipv6ra" % counter,
                           row_wifi.ipv6ra and "1" or "0")

            if self.Wanproto == 'olsr' and self.Wanipv4addr is not None:
                add_option('wan_config', '1')
                add_option('wan_proto', self.Wanproto)
                add_option('wan_ip4addr', self.Wanipv4addr)
                add_option('wan_netmask', self.Wannetmask)

            if self.Lanproto == 'olsr' and self.Lanipv4addr is not None:
                add_option('lan_config', '1')
                add_option('lan_proto', self.Lanproto)
                add_option('lan_dhcp', self.Landhcp)
                add_option('lan_dhcprange', self.Landhcprange)
                add_option('lan_ip4addr', self.Lanipv4addr)
                add_option('lan_netmask', self.Lannetmask)

        self.mkconfig += "\n"

        # section netconfig/wan for dhcp/static
        if (self.Wanproto == 'static' and self.Wanipv4addr is not None) or \
           self.Wanproto == 'dhcp':
            add_section('netconfig', 'wan')
            add_option('proto', self.Wanproto)

            if self.Wanproto == 'static':
                add_option('ip4addr', self.Wanipv4addr)
                add_option('netmask', self.Wannetmask)

                if self.Wanproto == 'static':
                    if self.Wangateway is not None:
                        add_option('gateway', self.Wangateway)

                    if self.Wandns is not None:
                        add_option('dns', self.Wandns)

            if self.Wan_allow_ssh:
                add_option('allowssh', '1')

            if self.Wan_allow_web:
                add_option('allowweb', '1')

            self.mkconfig += "\n"

        # section Lan - static
        if self.Lanproto == 'static' and self.Lanipv4addr and self.Lannetmask:
            add_section('netconfig', 'lan')
            add_option('proto', self.Lanproto)
            add_option('ip4addr', self.Lanipv4addr)
            add_option('netmask', self.Lannetmask)
            self.mkconfig += "\n"

        # Section ipv6
        add_section("defaults", "ipv6")
        add_option("enabled", self.Ipv6 and '1' or '0')
        if self.Ipv6 and self.Ipv6_config:
            add_option("config", self.Ipv6_config)
        self.mkconfig += "\n"

        # Section general
        add_section('general', 'general')
        add_option('sharenet', self.Sharenet)
        add_option('localrestrict', self.Localrestrict)
        self.mkconfig += "\n"

        # Section qos
        if self.Wan_qos == 1:
            add_section('qos', 'wan')
            add_option('down', self.Wan_qos_down)
            add_option('up', self.Wan_qos_up)
            self.mkconfig += "\n"

        # Write config to etc/config/meshwizard
        mw_conf_file = os.path.join(self.FilesDirConfig, 'meshwizard')
        mkutils.write_file(mw_conf_file, self.mkconfig)

        # Make meshwizard start at bootime
        filedir = os.path.join(request.folder, "private", "files")
        initfile = os.path.join(filedir, 'wizard.init')
        shutil.copy(initfile, os.path.join(self.FilesDirInit, 'wizard'))
        try:
            os.symlink('/etc/init.d/wizard',
                       os.path.join(self.FilesDirRc, 'S99wizard'))
        except OSError as e:
            if e.errno == errno.EEXIST:
                pass

        # Write /etc/config/meshkit containing current configuration
        self.mkconfig = ''
        add_section('meshkit', 'update')
        if self.Profile:
            add_option('profile', self.Profile)

        add_option('target', self.Target)
        add_option('url', self.Url)
        self.mkconfig += "\n"

        # if a password_hash is available then write it to the meshkit config
        # too
        if self.password_hash:
            add_section('defaults', 'auth')
            add_option('password_hash', self.password_hash)

        # write meshkit config file
        mk_conf_file = os.path.join(self.FilesDirConfig, 'meshkit')
        mkutils.write_file(mk_conf_file, self.mkconfig)
示例#5
0
 def _summary_json_write(self, jsondata):
     summary_file = os.path.join(self.BinDir, "summary.json")
     logger.debug('Writing summary to %s' % summary_file)
     mkutils.write_file(summary_file, jsondata)
示例#6
0
    def _build(self):
        status = 3
        out = ''
        settings_summary_json = {}
        logger.info(
            'Build started, ID: %s, Target: %s' %
            (self.Id, self.Target))
        if self._createdirectories():
            if not self.Noconf:
                self._createconfig()

            # write summary to output directory
            settings_summary_json = self._summary_json()
            self._summary_json_write(settings_summary_json)

            # handle files in <meshkit>/files
            mkfilesdir = os.path.join(request.folder, "files")
            if os.path.exists(mkfilesdir):
                mkutils.cptree(mkfilesdir, self.FilesDir)
                logger.info(
                    'Copied files from %s to %s.' %
                    (mkfilesdir, self.FilesDir))

            # handle files/ in imagebuilder
            ibfilesdir = os.path.join(
                config.buildroots_dir,
                self.Target,
                "files")
            if os.path.exists(ibfilesdir):
                mkutils.cptree(ibfilesdir, self.FilesDir)

            # handle community files (custom files uploaded by community)
            if config.communitysupport and config.communityfiles_dir:
                cfilesdir = os.path.join(
                    config.communityfiles_dir,
                    self.Community,
                    "files")
                logger.info(
                    'Copied files from %s to %s' %
                    (cfilesdir, self.FilesDir))
                if os.path.exists(cfilesdir):
                    mkutils.cptree(cfilesdir, self.FilesDir)
                    if self.Community == 'weimar':
                        filesdir = "%s/etc/init.d/apply_profile.code" % \
                            self.FilesDir
                        with open(filesdir) as source:
                            data = source.read()
                            data1 = data.replace(
                                "#SIM_ARG1=\"olympia\"",
                                "SIM_ARG1=\"ffweimar\"")
                            data2 = data1.replace(
                                "#SIM_ARG2=\"adhoc\"",
                                "SIM_ARG2=\"hybrid\"")
                            logger.info("node number: " + self.Nodenumber)
                            data3 = data2.replace(
                                "#SIM_ARG3=2",
                                "SIM_ARG3=" +
                                self.Nodenumber)
                            source.seek(0)
                            source.write(data3)
                            source.close()

            # Handle uploaded archive
            if self.Upload:
                uploaded_file = os.path.join(
                    request.folder,
                    "uploads",
                    self.Upload)
                if os.access(uploaded_file, os.R_OK):
                    logger.info('extracting %s' % uploaded_file)
                    pu_ret = processupload.extract(
                        uploaded_file, self.FilesDir)
                    if pu_ret:
                        logger.warning(str(pu_ret))
                    # delete uploaded file
                    try:
                        os.remove(uploaded_file)
                        logger.debug('Deleted %s' % uploaded_file)
                    except:
                        logger.warning('Could not delete %s' % uploaded_file)

            # Copy community profile
            if config.communitysupport and self.Community:
                communityprofile = os.path.join(
                    config.profiles,
                    'profile_' + self.Community)
                if not os.path.exists(communityprofile):
                    logger.warning(
                        'The communityfile %s does not exist!' %
                        communityprofile)
                else:
                    logger.debug(
                        'Copied %s to %s' %
                        (communityprofile, self.FilesDirConfig))
                    shutil.copy(communityprofile, self.FilesDirConfig)

            # construct the commandline
            path = os.path.join(config.buildroots_dir, self.Target)
            cmdline = ["make", "image"]
            if len(self.Profile) > 0:
                cmdline.append("PROFILE=%s" % self.Profile)
            if self.Pkgs:
                cmdline.append("PACKAGES=%s" % self.Pkgs)
            if len(os.listdir(self.FilesDir)) > 0:
                cmdline.append("FILES=%s" % self.FilesDir)
            if len(self.BinDir) > 0:
                cmdline.append("BIN_DIR=%s" % self.BinDir)

            proc = subprocess.Popen(
                cmdline,
                cwd=path,
                stdout=subprocess.PIPE,
                shell=False,
                stderr=subprocess.STDOUT
            )

            out, _ = proc.communicate()
            ret = proc.returncode

            self._download_links_write()
            if ret != 0:
                if ret < 0:
                    logger.critical('make was killed by signal %s', str(ret))
                else:
                    logger.critical(
                        'make failed with return code %s',
                        str(ret))
                status = 2
            else:
                status = 0

            # write build log
            build_log_file = os.path.join(self.BinDir, "build.log")
            mkutils.write_file(build_log_file, out)

        return status, out, settings_summary_json
示例#7
0
    def _createconfig(self):
        """ Write uci config file for meshwizard and meshkit to
            FilesDirConfig/meshwizard
        """

        def add_section(type, name):
            self.mkconfig += "config %s '%s'\n" % (type, name)

        def add_option(option, value):
            self.mkconfig += "\toption %s '%s'\n" % (option, value)

        def add_list(option, value):
            self.mkconfig += "\tlist %s '%s'\n" % (option, value)

        # section system
        add_section('system', 'system')
        add_option('hostname', self.Hostname)
        add_option('conloglevel', '8')
        add_option('cronloglevel', '9')
        add_option('latitude', self.Latitude)
        add_option('longitude', self.Longitude)
        add_option('location', self.Location)
        self.mkconfig += "\n"

        # ssh pubkeys
        if self.Pubkeys and len(self.Pubkeys) > 0:
            add_section('system', 'ssh')
            for k in self.Pubkeys:
                add_list('pubkey', k)
            self.mkconfig += "\n"

        # section community
        add_section('public', 'community')
        add_option('name', self.Community)
        self.mkconfig += "\n"

        # section contact
        add_section('public', 'contact')
        add_option('nickname', self.Nickname)
        add_option('name', self.Name)
        add_list('homepage', self.Homepage)
        add_option('mail', self.Email)
        add_option('phone', self.Phone)
        add_option('note', self.Note)
        self.mkconfig += "\n"

        # section luci_main
        add_section('core', 'luci_main')
        add_option('mediaurlbase', "/luci-static/%s" % self.Theme)
        self.mkconfig += "\n"

        # section netconfig
        add_section('netconfig', 'netconfig')
        wifi_counter = 0
        for row_wifi in self.rows_wifi:
            counter = str(wifi_counter)
            add_option("IB_wifi%s_config" % counter, '1')

            if row_wifi.ipv4addr:
                add_option("IB_wifi%s_ip4addr" % counter, row_wifi.ipv4addr)

            if row_wifi.chan:
                add_option("IB_wifi%s_channel" % counter, row_wifi.chan)

            if row_wifi.dhcp:
                add_option(
                    "IB_wifi%s_dhcp" % counter,
                    row_wifi.dhcp and "1" or "0"
                )

            if row_wifi.dhcprange:
                add_option("IB_wifi%s_dhcprange" % counter, row_wifi.dhcprange)

            if row_wifi.vap:
                add_option(
                    "IB_wifi%s_vap" % counter, row_wifi.vap and "1" or "0"
                )

            if row_wifi.ipv6addr and self.Ipv6 and \
               self.Ipv6_config == 'static':
                add_option("IB_wifi%s_ip6addr" % counter, row_wifi.wifiipv6)

            if row_wifi.ipv6ra and (
                self.Ipv6 and (self.Ipv6_config == 'auto-ipv6-random' or
                               self.Ipv6_config == 'auto-ipv6-fromv4' or
                               self.Ipv6_config == 'static')):

                add_option(
                    "IB_wifi%s_ipv6ra" % counter,
                    row_wifi.ipv6ra and "1" or "0"
                )

            if self.Wanproto == 'olsr' and self.Wanipv4addr is not None:
                add_option('wan_config', '1')
                add_option('wan_proto', self.Wanproto)
                add_option('wan_ip4addr', self.Wanipv4addr)
                add_option('wan_netmask', self.Wannetmask)

            if self.Lanproto == 'olsr' and self.Lanipv4addr is not None:
                add_option('lan_config', '1')
                add_option('lan_proto', self.Lanproto)
                add_option('lan_dhcp', self.Landhcp)
                add_option('lan_dhcprange', self.Landhcprange)
                add_option('lan_ip4addr', self.Lanipv4addr)
                add_option('lan_netmask', self.Lannetmask)

        self.mkconfig += "\n"

        # section netconfig/wan for dhcp/static
        if (self.Wanproto == 'static' and self.Wanipv4addr is not None) or \
           self.Wanproto == 'dhcp':
            add_section('netconfig', 'wan')
            add_option('proto', self.Wanproto)

            if self.Wanproto == 'static':
                add_option('ip4addr', self.Wanipv4addr)
                add_option('netmask', self.Wannetmask)

                if self.Wanproto == 'static':
                    if self.Wangateway is not None:
                        add_option('gateway', self.Wangateway)

                    if self.Wandns is not None:
                        add_option('dns', self.Wandns)

            if self.Wan_allow_ssh:
                add_option('allowssh', '1')

            if self.Wan_allow_web:
                add_option('allowweb', '1')

            self.mkconfig += "\n"

        # section Lan - static
        if self.Lanproto == 'static' and self.Lanipv4addr and self.Lannetmask:
            add_section('netconfig', 'lan')
            add_option('proto', self.Lanproto)
            add_option('ip4addr', self.Lanipv4addr)
            add_option('netmask', self.Lannetmask)
            self.mkconfig += "\n"

        # Section ipv6
        add_section("defaults", "ipv6")
        add_option("enabled", self.Ipv6 and '1' or '0')
        if self.Ipv6 and self.Ipv6_config:
            add_option("config", self.Ipv6_config)
        self.mkconfig += "\n"

        # Section general
        add_section('general', 'general')
        add_option('sharenet', self.Sharenet)
        add_option('localrestrict', self.Localrestrict)
        self.mkconfig += "\n"

        # Section qos
        if self.Wan_qos == 1:
            add_section('qos', 'wan')
            add_option('down', self.Wan_qos_down)
            add_option('up', self.Wan_qos_up)
            self.mkconfig += "\n"

        # Write config to etc/config/meshwizard
        mw_conf_file = os.path.join(self.FilesDirConfig, 'meshwizard')
        mkutils.write_file(mw_conf_file, self.mkconfig)

        # Make meshwizard start at bootime
        filedir = os.path.join(request.folder, "private", "files")
        initfile = os.path.join(filedir, 'wizard.init')
        shutil.copy(initfile, os.path.join(self.FilesDirInit, 'wizard'))
        try:
            os.symlink(
                '/etc/init.d/wizard',
                os.path.join(
                    self.FilesDirRc,
                    'S99wizard'))
        except OSError as e:
            if e.errno == errno.EEXIST:
                pass

        # Write /etc/config/meshkit containing current configuration
        self.mkconfig = ''
        add_section('meshkit', 'update')
        if self.Profile:
            add_option('profile', self.Profile)

        add_option('target', self.Target)
        add_option('url', self.Url)
        self.mkconfig += "\n"

        # if a password_hash is available then write it to the meshkit config
        # too
        if self.password_hash:
            add_section('defaults', 'auth')
            add_option('password_hash', self.password_hash)

        # write meshkit config file
        mk_conf_file = os.path.join(self.FilesDirConfig, 'meshkit')
        mkutils.write_file(mk_conf_file, self.mkconfig)
示例#8
0
 def _summary_json_write(self, jsondata):
     summary_file = os.path.join(self.BinDir, "summary.json")
     logger.debug('Writing summary to %s' % summary_file)
     mkutils.write_file(summary_file, jsondata)