Upgrade Rancher Service using REST API

I am trying to use the Rancher API to do automated deployments. Every time a new docker image is available, I want to upgrade the Service to use the new image version. Looking at the upgrade API, I can see in the inputs are:

HTTP/1.1 POST /v1/projects/1a8/services/1s6/?action=upgrade
Host: rancher.blah.com.au
Accept: application/json
Content-Type: application/json
Content-Length: 51

{
    "inServiceStrategy": null,
    "toServiceStrategy": null,
}

Please advise what are these input fields? All I want to do is update the following field and kick off the upgrade:

imageUuid: "docker:blah/blah:v0.1"

Thanks

1 Like

@yunspace for your use case, you would have to use “inServiceStrategy” for the upgrade, and:

  • fetch service.launchConfig field of your service-to-upgrade
  • update image field in the launchConfig fetched on the previous step, and pass it as enclosed field to inServiceStrategy parameter for the upgrade:
"inServiceStrategy": {
  "launchConfig": {
    "tty":true,
    "vcpu":1,
    "imageUuid":"docker:ubuntu:14.04.3"
    //...and other parameters of  your existing service's launch config
  }
}
3 Likes

Thanks, that answered my question. I will try it out

Hi, need a small help
how did u fetch the service.launchConfig of your corresponding service ?

@Sadish_Kumar - I simplified my life and I wrote a python script that does that - if that helps: https://github.com/etlweather/gaucho

3 Likes

If can help, I’m using this with jenkins: https://hub.docker.com/r/leen15/rancher-wrapper/

@Sadish_Kumar similar to @etlweather, I also have a script https://github.com/LittleBayDigital/little-builder/blob/master/bin/lib/_rancher.sh

Basically what it does is:

  1. Gets the service using the API ${RANCHER_API_URL}/projects/${environment}/services/${service}/
  2. Updates the updatedServiceStrategy
  3. Send a Update request

Actually I think @etlweather’s script is a lot nicer than my one. Use his! :sunglasses:

For automated deployment, in my case, upon tags in GitLab, I created a container which runs on Rancher using webhook to create the web API and upon HTTP requests, it runs commands using gaucho to upgrade the containers.

Then in GltLab CI, upon build with a tag, I simply use curl to call the HTTP endpoint to cause the service to be upgraded.

1 Like

hello

Is there a way, updating via rancher API, to keep previous configurations and only updating the docker image?

thanks in advance

No in 1.x, yes in 2.0.

1 Like

Hi Vincent,
Can you share how can we do that ( update an worklaod or servic to use different contianer image and keep other config as it is ) or direct us to some links where the porcess is menioned.

I try a easy approach.

  1. using rancher cli grep the service id
    ~/rancher ps | grep service | grep -w ${RANCHER_SERVICE_NAME}
  2. curl get api json with the serviceid from (1)
  3. from the json in (2) use jq get .launchConfig (if you have sidekick, please also get .secondaryLaunchConfigs i.e is array)
  4. create the request data json obj and pass in the data from (3)

    appendsecondaryLaunchConfigs=[ "${secondaryLaunchConfigs}" = null ] && echo "" || echo ",\"secondaryLaunchConfigs\": ${secondaryLaunchConfigs-null}"`

echo “{
"inServiceStrategy": {
"type": "inServiceUpgradeStrategy",
"batchSize": 1,
"intervalMillis": 2000,
"startFirst": false,
"launchConfig": ${launchConfig-null}
${appendsecondaryLaunchConfigs}
}
` }” | jq . > update-request-data.json

  1. the change the imageuuid in update-request-data.json

if not sidekick:

updatedServiceStrategy=cat update-request-data.json | jq ".inServiceStrategy.launchConfig.imageUuid=\"docker:${image}\""

is sidekick (you need to specify which sidekick if you have mutiple, if only one sidekickindex is 0):

updatedServiceStrategy=cat update-request-data.json | jq ".inServiceStrategy.secondaryLaunchConfigs[${sidekickindex}].imageUuid=\"docker:${image}\""

  1. send request to api with ?action=upgrade

curl --silent --write-out “Upgrade service - HTTP: %{http_code}\n” -u “${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}”
-X POST
-H ‘Accept: application/json’
-H ‘Content-Type: application/json’
-d “${updatedServiceStrategy}”
“${RANCHER_API_URL}/projects/${environment}/services/${serviceid}/?action=upgrade” -o upgrade.log

curl -u “64247E71B0B579E7A393:8NTg8FHftrcUfiU5kXUN37ARjgHvmgVfnNezfhHp” -X POST -H ‘Accept: application/json’ -H ‘Content-Type: application/json’ -d ‘{“inServiceStrategy”:{“launchConfig”:{“tty”:true, “vcpu”:1, “imageUuid”:“docker:tomcat:9.0-jre8”}}’ ‘http://10.10.1.8:8080/v2-beta/projects/1a7/services/1s88/?action=upgrade’

{“id”:“f0d4f155-58c8-4377-8f1a-13a959e4c284”,“type”:“error”,“links”:{},“actions”:{},“status”:400,“code”:“InvalidBodyContent”,“message”:“InvalidBodyContent”,“detail”:null,“baseType”:“error”}