def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(ClientConfiguration2db, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.db_conf = DbConfiguration(configuration_files, configuration_values) self.config_js = os.path.join(directory, 'client', 'configuration.js') if os.path.exists(self.config_js): self.config = json.load(open(self.config_js)) else: self.config = {}
class ClientConfiguration2db(Upgrader): def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(ClientConfiguration2db, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.db_conf = DbConfiguration(configuration_files, configuration_values) self.config_js = os.path.join(directory, 'client', 'configuration.js') if os.path.exists(self.config_js): self.config = json.load(open(self.config_js)) else: self.config = {} @classmethod def get_priority(self): """ After database """ return 4 def check_updated(self): # In the future, this file will not exist. if not os.path.exists(self.config_js): return True print( " - There is a client/configuration.js file, which contains the generic configuration. Now that configuration is managed by the administration panel, and stored in the database. So all those values that you are using will be migrated to the database.") return False def upgrade(self): connection_url = self.db_conf.build_url() engine = create_engine(connection_url, echo=False, convert_unicode=True) Session = sessionmaker(bind=engine) session = Session() try: for key, value in six.iteritems(self.config): if key in ('development', 'host.entity.image.mobile', 'host.entity.image', 'sound.enabled', 'experiments.default_picture', 'host.entity.image.login', 'facebook.like.box.visible', 'create.account.visible'): continue new_property = model.DbClientProperties(key, value) session.add(new_property) session.commit() finally: session.close() os.remove(self.config_js) simple_server_config_filename = os.path.join('httpd', 'simple_server_config.py') lines = [] for line in open(simple_server_config_filename): if 'weblabclientadmin' not in line and 'configuration.js' not in line and 'webserver' not in line and 'client/images' not in line: lines.append(line) open(simple_server_config_filename, 'w').write(''.join(lines)) apache_weblab_generic_conf_filename = os.path.join('httpd', 'apache_weblab_generic.conf') lines = [] bad_pos = -1 recording = True for pos, line in enumerate(open(apache_weblab_generic_conf_filename)): if bad_pos >= 0: bad_pos -= 1 continue if 'LocationMatch (.*)configuration\.js' in line: bad_pos = 1 continue if 'configuration.js' in line or 'weblabclientadmin' in line or 'client/images' in line: continue if 'webserver' in line and '<Directory' in line: recording = False if not recording and '</Directory' in line: recording = True continue if recording: lines.append(line) open(apache_weblab_generic_conf_filename, 'w').write(''.join(lines))
class ConfigurationExperiments2db(Upgrader): def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(ConfigurationExperiments2db, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.db_conf = DbConfiguration(configuration_files, configuration_values) self.config_js = os.path.join(directory, 'client', 'configuration.js') if os.path.exists(self.config_js): self.config = json.load(open(self.config_js)) else: self.config = {} @classmethod def get_priority(self): """ After database """ return 1 def check_updated(self): # In the future, this file will not exist. if not os.path.exists(self.config_js): return True # But if it exists and it has an 'experiments' section, it has # not been upgraded. if 'experiments' in self.config: print( " - configuration.js contains experiments. The experiments in the configuration.js are going to be migrated to the database.") return False return True def upgrade(self): connection_url = self.db_conf.build_url() engine = create_engine(connection_url, echo=False, convert_unicode=True) Session = sessionmaker(bind=engine) session = Session() try: for client_id, experiments in self.config['experiments'].iteritems(): for experiment in experiments: experiment_category = experiment['experiment.category'] experiment_name = experiment['experiment.name'] db_category = session.query(model.DbExperimentCategory).filter_by(name=experiment_category).first() if not db_category: # Experiment not in database continue db_experiment = session.query(model.DbExperiment).filter_by(category=db_category, name=experiment_name).first() if not db_experiment: # Experiment not in database continue db_experiment.client = client_id for key in experiment: if key in ('experiment.category', 'experiment.name'): # Already processed continue existing_parameter = session.query(model.DbExperimentClientParameter).filter_by( parameter_name=key, experiment=db_experiment).first() if existing_parameter: # Already processed in the past continue value = experiment[key] if isinstance(value, basestring): param_type = 'string' elif isinstance(value, bool): param_type = 'bool' elif isinstance(value, float): param_type = 'floating' elif isinstance(value, int): param_type = 'integer' else: print("Error: invalid parameter type %s for key %s of experiment %s" % ( type(value), key, experiment_name)) continue param = model.DbExperimentClientParameter(db_experiment, key, param_type, unicode(experiment[key])) session.add(param) session.commit() finally: session.close() self.config.pop('experiments') json.dump(self.config, open(self.config_js, 'w'), indent=4)
def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(JSClientUpgrader, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.config_file = os.path.join(directory, 'httpd', 'simple_server_config.py') self.db_conf = DbConfiguration(configuration_files, configuration_values)
class JSClientUpgrader(Upgrader): def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(JSClientUpgrader, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.config_file = os.path.join(directory, 'httpd', 'simple_server_config.py') self.db_conf = DbConfiguration(configuration_files, configuration_values) @classmethod def get_priority(self): """ After database """ return 5 def check_updated(self): """ Check whether we need to apply this updater. :return: """ # We need to update if the simple_server_config.py has a Redirect from /weblab to /weblab/client (which was done for GWT # but which should not be done anymore). contents = open(self.config_file, "r").read() result = re.search(r"u'.*\$', u'redirect:.*/weblab/client'", contents) return result is None def upgrade(self): print("[Upgrader]: Starting update: Changing from GWT to new Non-GWT client") # UPGRADE ACTION 1: Old 'dummy' type experiments are now 'js' experiments. connection_url = self.db_conf.build_url() engine = create_engine(connection_url, echo=False, convert_unicode=True) Session = sessionmaker(bind=engine) session = Session() try: db_experiments = session.query(model.DbExperiment).filter_by(client='dummy').all() for db_exp in db_experiments: db_exp.client = 'js' # Add html.file db_param = model.DbExperimentClientParameter() db_param.experiment_id = db_exp.id db_param.parameter_name = 'html.file' db_param.value = 'nativelabs/dummy.html' db_param.parameter_type = 'string' session.add(db_exp) session.add(db_param) print("Experiment {0} had client 'dummy' and now has client 'js'".format(db_exp.name)) session.commit() finally: session.close() print("[Upgrader]: DONE: Changed Dummy experiments to type JS") # UPGRADE ACTION 2: JS experiments should have builtin set to true (all of them are builtin for now). session = Session() try: db_experiments = session.query(model.DbExperiment).filter_by(client='js').all() for db_exp in db_experiments: db_builtin_param = session.query(model.DbExperimentClientParameter).filter_by(experiment_id=db_exp.id, parameter_name='builtin').first() if db_builtin_param is None: db_builtin_param = model.DbExperimentClientParameter() db_builtin_param.experiment = db_exp db_builtin_param.parameter_name = 'builtin' db_builtin_param.parameter_type = 'bool' db_builtin_param.value = "True" session.add(db_builtin_param) session.commit() finally: session.close() print("[Upgrader]: DONE: All JS experiments now have a builtin parameter set to True") # UPGRADE ACTION 3: Call httpd-config-generate script. httpd_config_generate(self.directory) print("[Upgrader]: DONE: Called http-config-generate to upgrade Apache configuration files") print("[Upgrader]: UPGRADE FINISHED.")
def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(DatabaseUpgrader, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) db_conf = DbConfiguration(configuration_files, configuration_values) regular_url = db_conf.build_url() coord_url = db_conf.build_coord_url() self.upgrader = DbUpgrader(regular_url, coord_url)