Skip to content

Commit

Permalink
Merge pull request sekassel-research#5 from sekassel-research/feat/ra…
Browse files Browse the repository at this point in the history
…ncher-2

major: Rancher 2 support
  • Loading branch information
KosrenDQ authored Mar 1, 2021
2 parents 2973f72 + 88fd4bc commit b4d32c7
Show file tree
Hide file tree
Showing 1,753 changed files with 6,535 additions and 157,024 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 2
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
!node_modules
!node_modules
.idea/
58 changes: 42 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
# rancher-update

This action helps by updating a service in the rancher 1.6.x environment.
This action helps by updating a service in the Rancher 2 environment with kubernetes.

# Examples

## Update service

```yaml
on:
on:
push:
branches:
- master

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: sekassel-research/actions-rancher-update@2.0.0
with:
rancher_url: https://rancher.test.de
rancher_token: ${{ secrets.RANCHER_TOKEN }}
cluster_id: ${{ secrets.CLUSTER_ID }}
project_id: ${{ secrets.PROJECT_ID }}
namespace: ${{ secrets.NAMESPACE }}
deployment: ${{ secrets.DEPLOYMENT }}
docker_image: sekassel-research/test-image:latest
```
# Backwards compatibility
If you want to use this extension for `Rancher 1.6.x`, you need to use the following version `sekassel-research/actions-rancher-update@1.1.4`

## Example

```yaml
on:
push:
branches:
- master
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: sekassel-research/actions-rancher-update@1.1.4
with:
rancher_url: https://rancher.test.de
rancher_access: ${{ secrets.RANCHER_ACCESS }}
rancher_key: ${{ secrets.RANCHER_KEY }}
project_id: 1a5
stack_name: test-stack
service_name: test-service
docker_image: sekassel-research/test-image:latest

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: sekassel-research/actions-rancher-update@1.1.4
with:
rancher_url: https://rancher.test.de
rancher_access: ${{ secrets.RANCHER_ACCESS }}
rancher_key: ${{ secrets.RANCHER_KEY }}
project_id: 1a5
stack_name: test-stack
service_name: test-service
docker_image: sekassel-research/test-image:latest
```
27 changes: 12 additions & 15 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ name: Rancher Update Service
branding:
color: blue
icon: arrow-up-circle
description: An action for updating a service in the rancher 1.6.x environment
description: An action for updating a service in the Rancher 2.x environment
inputs:
rancher_url:
description: Rancher instance url
description: Rancher instance URL
required: true
rancher_access:
description: Rancher access rancher
rancher_token:
description: Rancher API access token
required: true
rancher_key:
description: Rancher api key
cluster_id:
description: Rancher/Kubernetes Cluster ID
required: true
project_id:
description: Id of the used rancher project
description: Rancher Project ID
required: true
stack_name:
description: Name of the stack which is associated with the service
namespace:
description: Rancher/Kubernetes Namespace
required: true
service_name:
description: Name of the service which should be updated
deployment:
description: Deployment/Service within the Namespace
required: true
docker_image:
description: Name of the docker image which is used for updating
description: Docker image
required: true
output:
result:
description: Boolean which indicates if the action was successful or not

runs:
using: node12
Expand Down
102 changes: 36 additions & 66 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,45 @@
const core = require('@actions/core');
const request = require('request-promise-native');
import core from '@actions/core';
import axios from 'axios';

process.on('unhandledRejection', handleError);
main().catch(handleError);

const sleep = (sec) => new Promise((resolve) => setTimeout(resolve, sec * 1000));
const waitForState = async (waitFor, rancherApi, id) => {
let retry = 10;
let state = '';
while (state !== waitFor && retry > 0) {
state = (await rancherApi.get(`/services/${id}`)).state;
retry--;
await sleep(5);
}

if (retry === 0) {
throw new Error(`Maximum retries exceeded waiting for state ${waitFor}`);
}
}

async function main() {
const RANCHER_URL = core.getInput('rancher_url', { required: true });
const RANCHER_ACCESS = core.getInput('rancher_access', { required: true });
const RANCHER_KEY = core.getInput('rancher_key', { required: true });
const PROJECT_ID = core.getInput('project_id', { required: true });
const STACK_NAME = core.getInput('stack_name', { required: true });
const SERVICE_NAME = core.getInput('service_name', { required: true });
const DOCKER_IMAGE = core.getInput('docker_image', { required: true });

const rancherApi = request.defaults({
baseUrl: `${RANCHER_URL}/v2-beta/projects/${PROJECT_ID}`,
auth: {
user: RANCHER_ACCESS,
pass: RANCHER_KEY
const rancherUrl = core.getInput('rancher_url', {required: true});
const rancherToken = core.getInput('rancher_token', {required: true});
const clusterId = core.getInput('cluster_id', {required: true});
const projectId = core.getInput('project_id', {required: true});
const namespace = core.getInput('namespace', {required: true});
const deployment = core.getInput('deployment', {required: true});
const dockerImage = core.getInput('docker_image', {required: true});

await axios.patch(
`${rancherUrl}/k8s/clusters/${clusterId}/api/apps/v1/namespaces/${namespace}/deployments/${deployment}`,
[
{
op: 'replace',
path: '/spec/template/spec/containers/0/image',
value: dockerImage,
},
],
{
headers: {
'Content-Type': 'application/json-patch+json',
'Authorization': 'Bearer ' + rancherToken,
},
},
json: true
});

let success = false;
// Check the stack
const stack = await rancherApi.get(`/stacks?name=${STACK_NAME}`);
if (!stack || !stack.data[0]) {
throw new Error('Could not find stack name. Check the stack_name input. Deploy failed!');
}
const stackId = stack.data[0].id;

// Check the service
const service = await rancherApi.get(`/services?name=${SERVICE_NAME}&stackId=${stackId}`);
if (!service || !service.data[0]) {
throw new Error('Could not find service name. Check the service_name input. Deploy failed!');
}
const { id, launchConfig } = service.data[0];
launchConfig.imageUuid = `docker:${DOCKER_IMAGE}`;

// Upgrade
const body = {
inServiceStrategy: {
launchConfig
}
};
await rancherApi.post(`/service/${id}?action=upgrade`, { body });
console.log('Waiting for upgrade ...');
await waitForState('upgraded', rancherApi, id);

// Finish upgrade
await rancherApi.post(`/service/${id}?action=finishupgrade`);
console.log('Waiting for service starting ...');
await waitForState('active', rancherApi, id);

console.log('Service is running, upgrade successful');
core.setOutput('result', success);
);

await axios.post(
`${rancherUrl}/v3/project/${clusterId}:${projectId}/workloads/deployment:${namespace}:${deployment}?action=redeploy`,
{},
{
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + rancherToken,
},
},
);
}

function handleError(err) {
Expand Down
15 changes: 0 additions & 15 deletions node_modules/.bin/sshpk-conv

This file was deleted.

7 changes: 0 additions & 7 deletions node_modules/.bin/sshpk-conv.cmd

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/sshpk-sign

This file was deleted.

7 changes: 0 additions & 7 deletions node_modules/.bin/sshpk-sign.cmd

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/sshpk-verify

This file was deleted.

7 changes: 0 additions & 7 deletions node_modules/.bin/sshpk-verify.cmd

This file was deleted.

15 changes: 0 additions & 15 deletions node_modules/.bin/uuid

This file was deleted.

7 changes: 0 additions & 7 deletions node_modules/.bin/uuid.cmd

This file was deleted.

Loading

0 comments on commit b4d32c7

Please sign in to comment.