Skip to content

Commit

Permalink
Enable redeploying multiple workloads
Browse files Browse the repository at this point in the history
  • Loading branch information
th0th committed Oct 13, 2020
1 parent b8f4f26 commit f5d5f71
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
Binary file added hint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 14 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@

## Running

![Hint](hint.png)

### Running as a Github action

```yaml
- name: Update rancher deployment
uses: th0th/rancher-redeploy-workload@v0.5
uses: th0th/rancher-redeploy-workload@v0.6
env:
RANCHER_BEARER_TOKEN: ${{ secrets.RANCHER_BEARER_TOKEN }}
RANCHER_CLUSTER_ID: 'c-qxyky'
RANCHER_NAMESPACE: 'namespace'
RANCHER_PROJECT_ID: 'c-qyxkj:p-hn2z5'
RANCHER_URL: 'https://rancher.domain.tld'
RANCHER_WORKLOAD: 'workload'
RANCHER_PROJECT_ID: 'p-hm2z1'
RANCHER_URL: 'https://rancher.aperturescience.tld'
RANCHER_WORKLOADS: 'wheatley1,wheatley2'
```
### Running as a docker container
```shell script
$ docker run --rm -it \
-e RANCHER_BEARER_TOKEN="token-xgskl:n45p7tmd47t9lfzh7xl8rw6rvtrfzzxrtdr6qvjg27r4sjcxvzss7d" \
-e RANCHER_CLUSTER_ID="c-qxyky" \
-e RANCHER_NAMESPACE="namespace" \
-e RANCHER_PROJECT_ID="c-qyxkj:p-hn2z5" \
-e RANCHER_URL="https://rancher.domain.tld" \
-e RANCHER_WORKLOAD="workload" \
th0th/rancher-redeploy-workload:v0.5
-e RANCHER_PROJECT_ID="p-hm2z1" \
-e RANCHER_URL="https://rancher.aperturescience.tld" \
-e RANCHER_WORKLOADS="wheatley1,wheatley2" \
th0th/rancher-redeploy-workload:v0.6
```

## Shameless plug

I am an indie hacker and I am running an uptime monitoring and analytics platform called [WebGazer](https://www.webgazer.io). You might want to check it out if you are running an online business and want to notice the incidents before your customers.
I am an indie hacker and I am running an uptime monitoring and analytics platform called [WebGazer](https://www.webgazer.io). You might want to check it out if you are running an online business and want to notice the incidents before your customers.

## License

Copyright © 2020, Gökhan Sarı. Released under the [MIT License](LICENSE).
Copyright © 2020, Gökhan Sarı. Released under the [MIT License](LICENSE).
72 changes: 40 additions & 32 deletions redeploy_rancher_workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

import requests

logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
)
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO)

required_environment_variables: List[str] = [
'RANCHER_BEARER_TOKEN',
'RANCHER_CLUSTER_ID',
'RANCHER_NAMESPACE',
'RANCHER_PROJECT_ID',
'RANCHER_URL',
'RANCHER_WORKLOAD',
'RANCHER_WORKLOADS',
]

missing_environment_variables: List[str] = []
Expand All @@ -33,47 +31,57 @@
sys.exit(1)

rancher_bearer_token = os.environ['RANCHER_BEARER_TOKEN']
rancher_cluster_id = os.environ['RANCHER_CLUSTER_ID']
rancher_namespace = os.environ['RANCHER_NAMESPACE']
rancher_project_id = os.environ['RANCHER_PROJECT_ID']
rancher_url = os.environ['RANCHER_URL']
rancher_workload = os.environ['RANCHER_WORKLOAD']
rancher_workloads = os.environ['RANCHER_WORKLOADS']


def generate_workload_url(r_workload: str) -> str:
return (
'{rancher_url}/v3/project/{rancher_cluster_id}:{rancher_project_id}'
'/workloads/deployment:{rancher_namespace}:{rancher_workload}'
).format(
rancher_cluster_id=rancher_cluster_id,
rancher_namespace=rancher_namespace,
rancher_project_id=rancher_project_id,
rancher_url=rancher_url,
rancher_workload=r_workload,
)

url = '{rancher_url}/v3/project/{rancher_project_id}/workloads/deployment:{rancher_namespace}:{rancher_workload}'.format(
rancher_namespace=rancher_namespace,
rancher_project_id=rancher_project_id,
rancher_url=rancher_url,
rancher_workload=rancher_workload,
)

headers = {
'Authorization': 'Bearer {rancher_bearer_token}'.format(
rancher_bearer_token=rancher_bearer_token,
),
}

response_get = requests.get(
headers={
**headers
},
url=url,
)
for rancher_workload in rancher_workloads.split(','):
url = generate_workload_url(rancher_workload)

response_get.raise_for_status()
response_get = requests.get(
headers={
**headers
},
url=url,
)

workload = response_get.json()
response_get.raise_for_status()
workload = response_get.json()

workload['annotations']['cattle.io/timestamp'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
workload['annotations']['cattle.io/timestamp'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')

response_put = requests.put(
headers={
**headers,
},
json=workload,
url=url,
)
response_put = requests.put(
headers={
**headers,
},
json=workload,
url=url,
)

response_put.raise_for_status()
response_put.raise_for_status()

logging.info("Workload {rancher_workload} is successfully redeployed.".format(
rancher_workload=rancher_workload,
))
logging.info("Workload {rancher_workload} is successfully redeployed.".format(
rancher_workload=rancher_workload,
))

0 comments on commit f5d5f71

Please sign in to comment.