Programmatically change the repository URL of a Hudson job - hudson

Is there a way to change the repository URL of a Hudson job using the Hudson CLI API?

There is no way to change the repository URL using the Hudson CLI. However there is a workaround that can be automated with little effort.
Workaround:
We can use cURL to download the config.xml of the job using the following command (note that in order to run cURL commands you have to setup cURL):
curl -X GET http://your-hudson-server/job/TheNameOfTheJob/config.xml -o localCopy.xml
The configuration file will contain something similar to this (depending on the Version Control used):
<scm-property>
<originalValue class="hudson.scm.SubversionSCM">
<locations>
<hudson.scm.SubversionSCM_-ModuleLocation>
<remote>https://your-repository</remote>
The value of the <remote> tag is the repository url (also check the credentials for the new repository).
There are several cURL ways to submit the modified version of config.xml back on the server. One way is:
curl -X POST http://your-hudson-server/job/TheNameOfTheJob/config.xml --data-binary "#newconfig.xml"

Related

How to use external JSON-file in Wget HTTP PUT while using Docker environment variables?

Current situation:
A Dockerfile that is based on an Ubuntu image, installs Wget and declares that a bashscript will run when a container is started.
A Docker container is started based on the image with the necessary environment variables in the command. These variables will be used inside the Wget command in the bashscript.
docker run -i -e ‘ENV_VARIABLE=VALUE’ [imagename]
The container runs the bashscript, containing a Wget HTTP PUT:
wget --method=PUT --body-data=”{\“key\”:\”${ENV_VARIABLE}\”}” ……
Desired situation:
The current situation works, but I don’t prefer this solution. This is because of the quote-escaping (\”) I have to use.
I tried to solve this by constructing the --body-data as below, with surrounding single quotes.
‘{“key”:”${ENV_VARIABLE}”}’
However, this will not set the ENV_VARIABLE since the payload is a full String now.
A more preferable solution would be to separate the JSON to a JSON-file, which I can refer to in the Wget call. This arises the following questions:
How to refer to the JSON-file? My best guess is to first copy the file on the build of the image into the image and then refer to it via a path in the Wget call, but then again, how do I refer to it?
If the above point is correct, will I still be able to refer to the Docker environment variables?

How to translate my cURL command into Chrome command?

I want to fire a POST request in command line, to post my image to a image searching site. At first, I tried cURL and get this command which works:
curl -i -X POST -F file=#search.png http://saucenao.com/search.php
It will post a file in FORM to the searching site and returns a HTML page result full with JavaScript which makes it hard to read in terminal. And it's also hard to preview online image in terminal.
Then I remember that I can open Chrome with arguments in command line, which I think may solve my problem. After some digging, I found Chrome switches, but seams it's just about Chrome starting flags (I'm not sure is this right, but I didn't find how to fire a post request like cURL do.)
So, can I use Chrome in command line to start it with a POST request just like my cURL command above?
There are a couple of things you could do.
You could write a script in JavaScript that will send the POST request and display the results inside the <body> element or the like;
You could keep the cURL command and use the -o (or --output) to save the resulting HTML in a file (but lose the -i switch, to avoid having the headers in the file), then open the file in Chrome or whichever browser you prefer. You could combine the two commands as a one-liner in any operating system. If you use Ubuntu, for example:
$ curl -o search.html -X POST -F file=#search.png http://saucenao.com/search.php && google-chrome search.html && rm search.html
According to this answer you could use bcat in order to avoid using a temporary file. Install it by apt-get install ruby-bcat and then just run
$ curl -X POST -F file=#search.png http://saucenao.com/search.php | bcat
I think the easier option is #2, but whichever you prefer.

Filtering out jenkins console output of a job

I'm quite new in Jenkins and I would like to filter out from the jenkins console output only the json output of my unix script run via a jenkins job
To simplify my scenario, I have a MyScript unix script that returns a json output. A jenkins job wraps the MyScript execution using a "Execute shell" build action.
When I run the jenkins job, MyScript is executed and the jenkins console output returns below output:
Started by remote host ...
Building remotely on ... in workspace ...
Set build name.
New build name is '#11-/products/software/myScript.py'
[ScriptWrapper] $ /bin/sh -xe /tmp/hudson9139846468482145951.sh
+ /products/software/myScript.py -t ...
{'ip': '...', 'host': '...'}
Set build name.
New build name is '#11-/products/software/myScript.py'
Variable with name 'BUILD_DISPLAY_NAME' already exists, ...
Finished: SUCCESS
From the above output I would like to filter out only the json output of my unix script that is "{'ip': '...', 'host': '...'}" .
That it is needed as we call the jenkins job via REST API and we need to get only the json output of the called unix script:
curl -s -k -u ... --request GET "https://<jenkins uri>/jenkins/view/ScriptWrapper/job/ScriptWrapper/19/consoleText"
We tried defining a parsing rules file but in this way we are able only to highlight some lines in the console output in the "Parsed Console Output" jenkins view.
In addition it seems that this "Parsed Console Output" is not accessible via rest api:
curl -s -k -u ... --request GET "https://<jenkins uri>/jenkins/view/ScriptWrapper/job/ScriptWrapper/19/parsed_console"
-> it doesn't work
Is there any way to filter out the jenkins console output?
We are also evaluating the possibility to use the Jenkins Groovy Postbuild Plugin. Do you think it can help ?
I thank you in advance for any suggestion.
If I understand the question correctly, you wish to generate clean output containing only the text you want?
If so, then I'd suggest you modify your shell script to output the desired text to a file, and then use either the "archive artifact" function in Jenkins to make the file content available, or the "html publisher" plugin to "publish" that file.
https://wiki.jenkins-ci.org/display/JENKINS/HTML+Publisher+Plugin
I third option could be to modify your shell script to output "magic cookies" as delimiters around the string you want.
That way you can fetch the entire console output using the REST API, and then easily filter out the text you want using a simple regex.

Enabling and Disabling Storage Plugin

I am running Apache Drill in Window 8.1 OS, having latest version of Drill (1.7).
I want to enable or disable storage plugin programatically (using C# code).
Is there any way to do so.?
You can update drill plugin via REST API.
I am taking MongoDB plugin as an example.
Enable
curl -X POST -H "Content-Type: application/json" -d '{"name":"mongoPlugin", "config":{"type":"mongo","enabled":true,"connection":"mongodb://localhost:27017/"}}' http://localhost:8047/storage/mongoPlugin.json
Change "enabled" to false to disable it.
Disable
curl -X POST -H "Content-Type: application/json" -d '{"name":"mongoPlugin", "config":{"type":"mongo","enabled":false,"connection":"mongodb://localhost:27017/"}}' http://localhost:8047/storage/mongoPlugin.json
Check drill docs for more details.
You already answered about creating plugins using C#. Just change payload as mentioned above.
For Enabling or Disabling Storage Plugin in Window Environment. First we have to download curl.exe file from Download Curl
. Set the path of curl.exe file in Environment Variable:-
Follow these steps:-
Download curl zip
Extract the contents (if you have downloaded the correct version you should find curl.exe)
Place curl.exe in a folder where you keep your software (e.g. D:\software\curl\curl.exe)
To run curl from the command line
a) Right-hand-click on "My Computer" icon
b) Select Properties
c) Click 'Advanced system settings' link
d) Go to tab [Advanced] - 'Environment Variables' button
e) Under System variable select 'Path' and Edit button
f) Add a semicolon followed by the path to where you placed your curl.exe (e.g. ;D:\software\curl).
Now Open Command Prompt and run following command:-
For Disabling Storage Plugin:-
curl http://localhost:8047/storage/DemoMySQl/enable/false
For Enabling:-
curl http://localhost:8047/storage/DemoMySQl/enable/true
Note:- DemoMySQl is storage plugin name.

Converting a Swagger YAML file to JSON from the command line

I'd like to convert a Swagger YAML file to JSON from the command line. The plan is to use this command line during a CI job. I've searched on google and found many solutions, but most of them use Python or Ruby, which I'd prefer not to use. For example: http://www.commandlinefu.com/commands/view/12218/convert-yaml-to-json
I'd like to do this without using Python or Ruby, and
I'd also like to be able to control the leading whitespace when formatting the JSON to match exactly the JSON that is output from Swagger's editor.swagger.io editor, when you choose File -> Download JSON
All this means is that I'd like the whitespace padding to be four spaces, like so:
{
"swagger": "2.0",
"info": {
"title": "API TITLE",
I haven't tried the Python method in the link above, but the Ruby method uses two space whitespace padding. Perhaps there is a way to control that, but I don't want to use Ruby or Python in this solution anyway.
I'm sure that there are many "correct" answers to this question. I am looking for the solution that is most elegant with the least number of dependencies. Ideally, a diff of the resulting JSON file against a JSON file generated by the editor.swagger.io should be empty.
I think that you are looking for the swagger-codegen (now OpenApi-generator) functionality:
Running
swagger-codegen generate -i swagger.yaml -l swagger
will out put a swagger.json in the same location.
Update For CI:
If you can install it on your build machine- good for you.
If you can't - the github page has a link to a docker image with a nodejs server is available (to convert using a curl command as suggested in a different answer).
Update For Docker:
If you use Docker, try swaggerapi/swagger-codegen-cli, there is an example for docker-compose that might help a few answers down by Fabian & ckeeney.
Update about OpenApi:
This question is about swagger, and a few years old. If you're just starting to use Swagger you should switch to OpenApi instead, and if you have existing swagger files, i suggest migrating.
Using yamljs:
yaml2json swagger.yaml -p -i4
The output from this command diff'd against the JSON output from editor.swagger.io produces an empty diff.
This is indeed what I'm looking for, but it brings in a huge dependency (node). I'm hoping for something even lighter, yet equally as elegant as this.
swagger-codegen cli interface
As Liel has already pointed out, you can run
swagger-codegen generate -i swagger.yaml -l swagger
Docker
If you use Docker, then I suggest you try swaggerapi/swagger-codegen-cli.
You can generate a json file using docker with the following command:
docker run -v ./docs:/docs swaggerapi/swagger-codegen-cli generate -i /docs/swagger.yaml -l swagger -o /docs
I like to setup a docker-compose.yml to "alias" this command for easy reuse:
version: "2"
services:
gen-swagger:
volumes:
- ./docs:/docs
image: swaggerapi/swagger-codegen-cli
command: generate -i /docs/swagger.yaml -l swagger -o /docs
And now I can just run docker-compose run gen-swagger
For version swagger-codegen 3.0.4
Use
swagger-codegen generate -i my_yaml.yaml -l openapi
to get a .json.
Another possibility to convert a swagger.yml file to swagger.json is a NPM package called swagger-cli.
npm install -g swagger-cli
Then you can convert a yml to json file:
swagger-cli bundle -o api-spec.json api-spec.yml
You can use the online swagger codegen project to do this:
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
\"spec\": {}
}" "https://generator.swagger.io/api/gen/clients/swagger-yaml"
Put the value of your swagger definition in the spec object. You'll get a link to download the converted & validated spec, in yaml format.
For options, take a look here:
http://generator.swagger.io/
I'd use https://openapi-generator.tech/
It's an npm install (I just used it locally npm install #openapitools/openapi-generator-cli) and then
npx #openapitools/openapi-generator-cli generate -i source.yaml -g openapi -o outputdir
For a gradle with Kotlin, i've wrote in my build.gradle.kts:
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import java.nio.file.Path
and then in a some task like compileJavacode for convertation:
val compileJava: Task by tasks.getting {
val openApiDir = "${rootProject.projectDir}/openapi"
val json: JsonNode? = ObjectMapper(YAMLFactory())
.readTree(Path.of("$openApiDir/openapi.yaml").toFile())
ObjectMapper().writerWithDefaultPrettyPrinter()
.writeValue(Path.of("$openApiDir/openapi.json").toFile(), json)
}