Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Programmatically creating a repo via the script API #66

Closed
womanderful opened this issue Jun 19, 2018 · 6 comments
Closed

Programmatically creating a repo via the script API #66

womanderful opened this issue Jun 19, 2018 · 6 comments

Comments

@womanderful
Copy link

Hi!

We're trying to create an APT repo via curl (ultimately from jenkins). In https://github.com/sonatype/nexus-public/blob/master/plugins/nexus-script-plugin/src/main/java/org/sonatype/nexus/script/plugin/RepositoryApi.java we can see a number of built-in methods for creating different types of repos. Does the APT plugin add a new method for this? We tried the following:

# create script
curl -u user:pass -X POST -H "Content-Type: application/json" --data '{"name":"foo","type":"groovy","content":"repository.createAptHosted('\''test'\'','\''myblob'\'')"}' http://myhost:8081/service/rest/v1/script
# run it
curl -u user:pass -X POST -H 'Content-Type: text/plain' -H 'Accept: application/json' 'http://myhost:8081/service/rest/v1/script/foo/run'
{
  "name" : "foo",
  "result" : "javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.sonatype.nexus.script.plugin.internal.provisioning.RepositoryApiImpl.createAptHosted() is applicable for argument types: (java.lang.String, java.lang.String) values: [test, deb_packages]\nPossible solutions: createNpmHosted(java.lang.String, java.lang.String), createHosted(java.lang.String, java.lang.String), createNpmHosted(java.lang.String), createNpmHosted(java.lang.String, java.lang.String, boolean), createRawHosted(java.lang.String, java.lang.String), createYumHosted(java.lang.String, java.lang.String)"
}

The error suggests some alternatives but "createAptHosted" is not among them, which makes me think that this is a dead end. Or are we missing something?

We're using Nexus Repository Manager OSS 3.11.0-01 and nexus-repository-apt 1.0.7.

Thank you,

@su-ar
Copy link

su-ar commented Jun 25, 2018

Can we put something in the json that does something similar to the groovy script "internal/provisioning/RepositoryApiImpl.groovy"? So emulate a "createAptHosted" in the json we upload using "RepositoryApiImpl.groovy" as a reference.

The "content" section of the json is in groovy. "RepositoryApiImpl.groovy" seems to provide the groovy implementations of the create functions for the other repo types.

@mpoindexter
Copy link
Contributor

I think these convenience methods are only for built-in repository types, so I wouldn't expect there to be a createAptHosted method. Think you'd have to implement the logic yourself in your script.

@Alan01252
Copy link

Alan01252 commented Jul 6, 2018

For others that are attempting this here's what worked for me:

Grab provision.sh and addUpdateScript.groovy from the below repository:

https://github.com/sonatype/nexus-book-examples/tree/book-examples-3.2/scripting/complex-script

( note I had to add @Grab('org.codehaus.groovy:groovy-backports-compat23:2.4.5') ) to addUpdateScript.groovy

Create a groovy script for adding apt-repo

/**
 * This script automatically configures nexus to have a self hosted apt repository and a proxy repository
 * for ubuntu 16.04
 */

import org.sonatype.nexus.repository.config.Configuration
import org.sonatype.nexus.blobstore.api.BlobStoreManager
import org.sonatype.nexus.repository.storage.WritePolicy

keypair =  """
-----BEGIN PGP PRIVATE KEY BLOCK-----
SOME PRIVATE KEY GOES HERE
-----END PGP PRIVATE KEY BLOCK-----
"""

/**
 * Self hosted repository
 */
hostedConfiguration = new Configuration(
        repositoryName: "voxygen-apt-hosted-xenial",
        recipeName: "apt-hosted",
        online: true,
        attributes: [
                storage: [
                        blobStoreName              : BlobStoreManager.DEFAULT_BLOBSTORE_NAME,
                        writePolicy                : WritePolicy.ALLOW,
                        strictContentTypeValidation: true
                ] as Map,
                apt: [
                        distribution : 'xenial',
                        flat : false
                ] as Map,
                aptSigning: [
                        keypair:  keypair.trim(),
                        passphrase: 'voxygen'
                ] as Map,
                aptHosted: [
                        assetHistoryLimit: null
                ] as Map
        ] as Map
)


repository.createRepository(hostedConfiguration)

/**
 * Proxy Repository
 */


proxyConfiguration = new Configuration(
        repositoryName: "voxygen-apt-proxy-xenial",
        recipeName: "apt-proxy",
        online: true,
        attributes: [
                storage: [
                        blobStoreName              : BlobStoreManager.DEFAULT_BLOBSTORE_NAME,
                        writePolicy                : WritePolicy.ALLOW,
                        strictContentTypeValidation: true
                ] as Map,
                apt: [
                        distribution : 'xenial',
                        flat : false
                ] as Map,
                httpclient   : [
                        connection: [
                                blocked  : false,
                                autoBlock: true
                        ] as Map
                ] as Map,
                proxy: [
                        remoteUrl: 'http://gb.archive.ubuntu.com/ubuntu/',
                        contentMaxAge: 0,
                        metaDataMaxAge: 0
                ] as Map,
                negativeCache: [
                        enabled   : true,
                        timeToLive: 1440
                ] as Map,
        ] as Map
)

repository.createRepository(proxyConfiguration)

@hmkhitaryan
Copy link

hmkhitaryan commented Jan 8, 2019

@Alan01252 ,
OK, I understand all the code above, but what to do then. Is there a way to only run script, but not add and then run? because adding script and then running causes another issue, we must remove that script after running, otherwise we will get duplication exception from nexus API. So please anybody help me on finding a better way of only running, but not adding then running the specified script.

@Alan01252
Copy link

HI, @hmkhitaryan Did you have any luck? I'm not entirely sure I understand what you're asking but happy to help if I can.

@hmkhitaryan
Copy link

hmkhitaryan commented Jan 9, 2019

Dear @Alan01252 , the thing is so, I can create, delete repositories programatically, but every time I send "http://localhost:8081/service/rest/v1/script/{name}" request, I have to send this request then
"http://localhost:8081/service/rest/v1/script/{name}/run" for running the script, and then to send DELETE "http://localhost:8081/service/rest/v1/script/{name}" to avoid duplication exception. now I want to implement such logic for only to run script, but not add and then run. I have heard that it exists such solution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants