示例#1
0
    def construct(self):
        # This is the working dir by now.
        sys.path.insert(0, '')
        c = self.master_config

        self.import_statements()
        reusing = c.Global.reuse_files
        if reusing:
            try:
                self.load_config_from_json()
            except (AssertionError, IOError):
                reusing = False
        # check again, because reusing may have failed:
        if reusing:
            pass
        elif c.Global.secure:
            keyfile = os.path.join(c.Global.security_dir, c.Global.exec_key)
            key = str(uuid.uuid4())
            with open(keyfile, 'w') as f:
                f.write(key)
            os.chmod(keyfile, stat.S_IRUSR | stat.S_IWUSR)
            c.SessionFactory.exec_key = key
        else:
            c.SessionFactory.exec_key = ''
            key = None

        try:
            self.factory = ControllerFactory(config=c, logname=self.log.name)
            self.start_logging()
            self.factory.construct()
        except:
            self.log.error("Couldn't construct the Controller", exc_info=True)
            self.exit(1)

        if not reusing:
            # save to new json config files
            f = self.factory
            cdict = {
                'exec_key': key,
                'ssh': c.Global.sshserver,
                'url':
                "%s://%s:%s" % (f.client_transport, f.client_ip, f.regport),
                'location': c.Global.location
            }
            self.save_connection_dict('ipcontroller-client.json', cdict)
            edict = cdict
            edict['url'] = "%s://%s:%s" % (
                (f.client_transport, f.client_ip, f.regport))
            self.save_connection_dict('ipcontroller-engine.json', edict)