Ejemplo n.º 1
0
    def finish_handle(self):

        filepath = os.path.join(settings.FILEPATH, 'corehq', 'pillows',
                                'mappings', 'user_mapping.py')
        userpillow = UserPillow(create_index=False)

        #current index
        #check current index
        aliased_indices = userpillow.check_alias()

        current_index = userpillow.es_index

        #regenerate the mapping dict
        m = DEFAULT_MAPPING_WRAPPER

        user_special_types = {
            "domain": type_exact_match_string("domain", dual=True),
            "username": type_exact_match_string("username", dual=True),
            "user_data": {
                "dynamic": True,
                "type": "object"
            },
        }
        user_special_types["username"]["fields"]["exact"][
            "include_in_all"] = False
        user_special_types["username"]["fields"]["username"][
            "analyzer"] = "simple"
        m['properties'] = dynamic.set_properties(
            self.doc_class, custom_types=user_special_types)
        m['_meta'][
            'comment'] = "Autogenerated [%s] mapping from ptop_generate_mapping %s" % (
                self.doc_class_str, datetime.utcnow().strftime('%m/%d/%Y'))
        m['_all'] = {"analyzer": "simple"}

        userpillow.default_mapping = m
        if hasattr(userpillow, '_calc_meta'):
            delattr(userpillow, '_calc_meta')
        output = []
        output.append('USER_INDEX="%s_%s"' %
                      (userpillow.es_index_prefix, userpillow.calc_meta()))
        output.append('USER_MAPPING=%s' % pprint.pformat(m))
        newcalc_index = "%s_%s" % (userpillow.es_index_prefix,
                                   userpillow.calc_meta())
        print "Writing new user index and mapping: %s" % output[0]
        with open(filepath, 'w') as outfile:
            outfile.write('\n'.join(output))

        if newcalc_index not in aliased_indices and newcalc_index != current_index:
            sys.stderr.write(
                "\n\tWarning, current index %s is not aliased at the moment\n"
                % current_index)
            sys.stderr.write("\tCurrent live aliased index: %s\n\n" %
                             (','.join(aliased_indices)))

        sys.stderr.write("File written to %s\n" % filepath)
Ejemplo n.º 2
0
    def handle(self, *args, **options):
        source_uri = getattr(settings, 'PRODUCTION_COUCHDB_URI', None)
        target_uri = XFormInstance.get_db().uri
        if source_uri is None:
            print "\n\tNo production URI to replicate from, we're done here.\n"
            print "\n\tNo settings.PRODUCTION_COUCHDB_URI has been set\n"
            sys.exit()

        input_query = options['query_string']

        if not input_query:
            print "\tRunning default query for user.is_superuser"
            query_string = BASE_QUERY

        else:
            query_string = input_query

        print "\n\tRunning user query: %s" % query_string

        user_pillow = UserPillow()
        user_es = user_pillow.get_es()

        doc_ids = [
            res['_id'] for res in get_query_results(user_es, query_string)
        ]

        do_replicate = options['makeitso']
        repl_params = {'doc_ids': doc_ids}

        if 'cancel' in args:
            repl_params['cancel'] = True
            print "\n\tSending a replication cancel notification to server"
        else:
            print "\n\tStarting staging replication from prod"

        if do_replicate:
            server = CommCareUser.get_db().server
            server.replicate(source_uri, target_uri, **repl_params)
            AuditEvent.audit_command()
        else:
            print "\n\tReplication dry run with params: %s" % repl_params