Search code examples
continuous-integrationnexusnexus3

nexus configure initial repositories non-interactively


I would like to create a docker for our nexus instance with the correct repositories, proxies etc already created.

Inspired by this question I started using the script API to configure my repositories. The repositories configured through this API don't work like the ones configured manually though (how sad; especially if you imagine the trouble I went through to get the configuration done with the non-documented script API...). I have already filed a bug therefore if you really want to know the details: https://issues.sonatype.org/browse/NEXUS-19891

Now my question: is there another way to configure the repositories non-interactively?

For jenkins it is possible to put some default configuration in /usr/share/jenkins/ref which will then be used only at the first startup; to give you an initial configuration. I was wondering if something similar exists for nexus? Or some other way that I don't know about?


Solution

  • I use python to do something similar to this:

    curl -X POST -u admin:admin123 --header 'Content-Type: application/json' http://localhost:8081/service/rest/v1/script -d '{"name":"test","type":"groovy","content":"repository.createYumProxy('\''test'\'', '\''http://repository:8080/'\'')"}'
    curl -X POST -u admin:admin123 --header "Content-Type: text/plain" 'http://127.0.0.1:8081/service/rest/v1/script/test/run'
    

    the exact script that I post (more readable here than with all those escaped quotes):

    repository.createYumProxy('{name}', '{url}');
    configuration = repository.repositoryManager.get('{name}').configuration.copy();
    configuration.attributes['proxy'] = [
        remoteUrl : "{url}",
        contentMaxAge : 0,
        metadataMaxAge : 0
    ]
    configuration.attributes['negativeCache'] = [
        timeToLive : 1.0
    ]
    repository.repositoryManager.update(configuration)
    

    The part that was missing in my case was the repositoryManager.update(). As quoted on the ticket:

    I think the important item(s) missing from your script is that you are not updating the repositoryManager with the new (copied) configuration (which causes the repository to stop/start and therefore reload config)