chetanmeh / oak-console-scripts

Miscellaneous scripts to be run via oak-run for AEM or Oak based setups
Apache License 2.0
10 stars 7 forks source link

groovy.lang.MissingPropertyException: No such property: osgi for class: groovysh_evaluate #3

Open royteeuwen opened 5 years ago

royteeuwen commented 5 years ago

Hey Chetan,

When executing one of your scripts (the changeNodeType.groovy), I get the following exception. Seeing as we use the oak-run.jar, it might be logical that there is no osgi? How can this be fixed

Thanks! Roy

royteeuwen commented 5 years ago

I think I have the answer: http://felix.apache.org/documentation/subprojects/apache-felix-script-console-plugin.html

Is it really necessary to have the entire osgi available to change the nodetype, or could this also be written without osgi based on pure oak-run.jar console based?

chetanmeh commented 5 years ago

Is it really necessary to have the entire osgi available to change the nodetype, or could this also be written without osgi based on pure oak-run.jar console based?

Technically yes. However nodetype changes logic is implemented mostly in the Jcr layer hence we need a repository instance. At oak-run level we mostly instantiate the NodeStore. So it would need some effort to instantiate the repo and then use that

royteeuwen commented 5 years ago

Ah ok got it, and I guess there isnt an example yet for doing this in the oak-run or doing it without starting up the entire Sling?

The reason I am asking this: We want to use the oak migration jar to migrate content from one instance to the other. But when doing this, we get a unique uuid exception, making the reindexing of the new content fail. So I would like to do the following:

royteeuwen commented 5 years ago

I got to a working solution:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import groovy.transform.CompileStatic
import org.apache.jackrabbit.oak.Oak
import org.apache.jackrabbit.oak.jcr.Jcr
import org.apache.jackrabbit.oak.spi.security.authentication.PreAuthenticatedLogin
import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal
import org.apache.jackrabbit.oak.spi.state.NodeStore

import javax.jcr.Node as JNode
import javax.jcr.Repository
import javax.jcr.Session as JSession
import javax.security.auth.Subject
import java.security.Principal
import java.security.PrivilegedExceptionAction

import static java.util.Collections.emptySet

@CompileStatic
class Constants {
    /**
     * Specify the path here whose nodetype needs to be changed
     */
    static String path = "/content/dam/we-retail/en/activities/biking/cycling_1.jpg/jcr:content/renditions/cq5dam.thumbnail.319.319.png/jcr:content"

    /**
     * Name of new nodetype
     */
    static String newNodeType = "oak:Resource"
}

@CompileStatic
class NodeTypeEditor {

    NodeStore nodeStore
    String path
    String newNodeType
    Repository repo
    JSession jSession

    def init() {
        repo = new Jcr(new Oak(nodeStore)).createRepository();
        jSession = createAdminSession(repo)
    }

    JSession createAdminSession(Repository repository) {
        Set<? extends Principal> principals = [{ 'admin' } as AdminPrincipal] as Set
        Subject subject = new Subject(true, principals, [PreAuthenticatedLogin.PRE_AUTHENTICATED] as HashSet, emptySet())
        return Subject.doAsPrivileged(subject,
                { repository.login(null, null) } as PrivilegedExceptionAction<JSession>,
                null)
    }

    def editNodeType() {
        init()
        try {
            JNode node = jSession.getNode(path)
            print "Existing node type ${node.getPrimaryNodeType().getName()}"
            node.setPrimaryType(newNodeType)
            def property = node.getProperty("jcr:uuid")
            property.remove()
            jSession.save()
            print "New nodetype. ${jSession.getNode(path).getPrimaryNodeType().getName()}"
        } finally {
            jSession?.logout()
        }
    }

    def print(String msg) {
        println msg
    }
}

new NodeTypeEditor(nodeStore: session.store, path: Constants.path, newNodeType: Constants.newNodeType).editNodeType()
chetanmeh commented 5 years ago

Should work. Just ensure you do not make any changes which impact indexes or blobs etc. For specific operation like this case it should be fine to do that

royteeuwen commented 5 years ago

Could you tell me why? Doesn't the oak-run trigger any index action? What I get when executing this script on any node is the following logs:

09:43:47.789 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/workflowDataLucene} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.793 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/slingeventJob} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.796 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/versionStoreIndex} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.802 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/commerceLucene} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.804 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/authorizables} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.805 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/cqProjectLucene} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.808 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/ntBaseLucene} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.812 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/cqTagLucene} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.
09:43:47.812 [main] WARN  o.a.j.oak.plugins.index.IndexUpdate - Missing provider for nrt/sync index: SegmentNodeBuilder{path=/oak:index/cqPageLucene} (rootState.async: null). Please note, it means that index data should be trusted only after this index is processed in an async indexing cycle.