sonatype-nexus-community / nexus-repository-composer

Composer support for the Nexus Repository Manager (work in progress!)
Eclipse Public License 1.0
220 stars 87 forks source link

Creating composer repositories via Script API #34

Open SlytherinCz opened 5 years ago

SlytherinCz commented 5 years ago

I was trying to run a script that would create a typical triangle of composer repositories. I uploaded the following script

  "name":"composer",
  "type":"composer",
  "content":"repository.createComposerHosted('composer-hosted');
repository.createComposerProxy('composer-proxy','https://packagist.org'); repository.createComposerGroup('composer',['composer-proxy','composer-hosted'])"
}

If we choose type composer, no language engine is found, according to this message

{
  "name" : "composer",
  "result" : "Missing engine for language: composer"
}

If we change the type to something else, that exists, exception hints a missing implementation of No signature of method: org.sonatype.nexus.script.plugin.internal.provisioning.RepositoryApiImpl.createComposerHosted()

I would like to automate creating of all my repositories, one if which is composer. It is possible this functionality already exists, in which case documentation is all that's needed.

I sincerely hope this is something that is not out of your hands

thaiphv commented 5 years ago

Since this is a community plugin so it's not supported by the RepositoryAPI. But I think you could do something similar to https://github.com/sonatype-nexus-community/nexus-repository-apt/issues/66#issuecomment-402964722, which is also a community plugin.

rdvencioneck commented 5 years ago

you can use the following groovy scripts (they must be optimized to be just one, but that's a good start):

parsed_args = new JsonSlurper().parseText(args)

repositoryManager = repository.repositoryManager

existingRepository = repositoryManager.get(parsed_args.name)

if (existingRepository != null) {

newConfig = existingRepository.configuration.copy()
newConfig.attributes['storage']['writePolicy'] = parsed_args.write_policy.toUpperCase()
newConfig.attributes['storage']['strictContentTypeValidation'] = Boolean.valueOf(parsed_args.strict_content_validation)

repositoryManager.update(newConfig)

} else {

configuration = new Configuration(
        repositoryName: parsed_args.name,
        recipeName: 'composer-hosted',
        online: true,
        attributes: [
                storage: [
                        writePolicy: parsed_args.write_policy.toUpperCase(),
                        blobStoreName: parsed_args.blob_store,
                        strictContentTypeValidation: Boolean.valueOf(parsed_args.strict_content_validation)
                ]
        ]
)

repositoryManager.create(configuration)

}


- proxy

import groovy.json.JsonSlurper import org.sonatype.nexus.repository.config.Configuration

parsed_args = new JsonSlurper().parseText(args)

repositoryManager = repository.repositoryManager

authentication = parsed_args.remote_username == null ? null : [ type: 'username', username: parsed_args.remote_username, password: parsed_args.remote_password ]

existingRepository = repositoryManager.get(parsed_args.name)

if (existingRepository != null) {

newConfig = existingRepository.configuration.copy()
newConfig.attributes['proxy']['remoteUrl'] = parsed_args.remote_url
newConfig.attributes['httpclient']['authentication'] = authentication
newConfig.attributes['storage']['strictContentTypeValidation'] = Boolean.valueOf(parsed_args.strict_content_validation)

repositoryManager.update(newConfig)

} else {

configuration = new Configuration(
        repositoryName: parsed_args.name,
        recipeName: 'composer-proxy',
        online: true,
        attributes: [
                proxy  : [
                        remoteUrl: parsed_args.remote_url,
                        contentMaxAge: parsed_args.get('content_max_age', -1),
                        metadataMaxAge: parsed_args.get('metadata_max_age', 1440.0)
                ],
                httpclient: [
                        blocked: false,
                        autoBlock: true,
                        authentication: authentication,
                        connection: [
                                useTrustStore: false
                        ]
                ],
                storage: [
                        blobStoreName: parsed_args.blob_store,
                        strictContentTypeValidation: Boolean.valueOf(parsed_args.strict_content_validation)
                ],
                negativeCache: [
                        enabled: parsed_args.get("negative_cache_enabled", true),
                        timeToLive: parsed_args.get("negative_cache_ttl", 1440.0)
                ]
        ]
)

repositoryManager.create(configuration)

}


- group

import groovy.json.JsonSlurper import org.sonatype.nexus.repository.config.Configuration

parsed_args = new JsonSlurper().parseText(args)

repositoryManager = repository.repositoryManager

existingRepository = repositoryManager.get(parsed_args.name)

if (existingRepository != null) {

newConfig = existingRepository.configuration.copy()
newConfig.attributes['group']['memberNames'] = parsed_args.member_repos
newConfig.attributes['storage']['strictContentTypeValidation'] = Boolean.valueOf(parsed_args.strict_content_validation)

repositoryManager.update(newConfig)

} else {

configuration = new Configuration(
        repositoryName: parsed_args.name,
        recipeName: 'composer-group',
        online: true,
        attributes: [
                group  : [
                        memberNames: parsed_args.member_repos
                ],
                storage: [
                        blobStoreName: parsed_args.blob_store,
                        strictContentTypeValidation: Boolean.valueOf(parsed_args.strict_content_validation)
                ]
        ]
)

repositoryManager.create(configuration)

}


here is the curl for adding, for example, the hosted repo:

curl -X POST -H "Content-Type: text/plain" -d "{ \"name\": \"composer-hosted\", \"write_policy\": \"allow_once\", \"strict_content_validation\": false, \"blob_store\": \"default\"}" -u admin:admin123 http://localhost:8081/service/rest/v1/script/createrepo_composerhosted/run