-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Apr 21, 2020
0 parents
commit 637c576
Showing
7 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Dockerfile | ||
README.md | ||
*.pyc | ||
*.pyo | ||
*.pyd | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM python:alpine3.11 | ||
MAINTAINER Burak Kurt (https://github.com/kurtburak) | ||
|
||
COPY get-uaa-token.sh /app/ | ||
COPY requirements.txt /app/ | ||
COPY run.py /app/ | ||
WORKDIR /app | ||
RUN pip install --no-cache-dir -r requirements.txt --trusted-host pypi.org --trusted-host files.pythonhosted.org | ||
RUN apk update | ||
RUN apk add openssh | ||
RUN chmod +x ./run.py | ||
RUN mkdir -p /root/.ssh | ||
RUN touch /root/.ssh/known_hosts | ||
RUN chmod -R 600 /root/.ssh | ||
RUN chmod 700 /root/.ssh | ||
RUN chmod +x run.py | ||
RUN chmod +x *.sh | ||
|
||
ENTRYPOINT ["python", "-u"] | ||
CMD ["./run.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3' | ||
services: | ||
pcf-kkb-dr: | ||
image: test:1 | ||
env_file: | ||
- tanzu.env | ||
deploy: | ||
restart_policy: | ||
condition: any | ||
delay: 10s | ||
max_attempts: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/bash | ||
|
||
if [ -z $OPSMAN_IP ] || [ -z $OPSMAN_USER ] || [ -z $OPSMAN_PASSWORD ]; then | ||
echo "Tanzu ops manager informations are missing." | ||
echo "Be sure OPSMAN_IP, OPSMAN_USER and OPSMAN_PASSWORD variables are set." | ||
exit 1 | ||
fi | ||
|
||
if [ -z $SSH_USER ]; then | ||
SSH_USER=ubuntu | ||
fi | ||
|
||
KNOW_HOST=`grep $OPSMAN_IP /root/.ssh/known_hosts` | ||
if [ -z $KNOW_HOST ]; then | ||
ssh-keyscan $OPSMAN_IP > /root/.ssh/known_hosts | ||
fi | ||
|
||
if [ ! -f /root/.ssh/id_rsa ]; then | ||
echo $SSH_KEY > /root/.ssh/id_rsa | ||
sed -i 's/\\n/\n/g' /root/.ssh/id_rsa | ||
chmod 600 /root/.ssh/id_rsa | ||
fi | ||
|
||
ssh -l $SSH_USER $OPSMAN_IP 'bash -s' << 'ENDSSH' | ||
uaac target http://localhost:8080/uaa > /dev/null | ||
sec=`echo $OPSMAN_PASSWORD | base64 --decode` | ||
uaac token owner get opsman $OPSMAN_USER -s "" -p $sec > /dev/null | ||
token=$(uaac context | grep access_token | awk '{print $2}') | ||
echo $token | tr -d '\n' | ||
ENDSSH | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests==2.22.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# -*- coding: utf-8 -*- | ||
import requests | ||
import json | ||
import smtplib | ||
import time | ||
import datetime | ||
import subprocess | ||
import os | ||
import re | ||
|
||
try: | ||
os.chdir('/app') | ||
# Params | ||
opsman_ip = str(os.environ['OPSMAN_IP']) | ||
opsman_url = 'https://'+opsman_ip+'/api/v0/installations' | ||
slack_url = os.environ['SLACK_URL'] | ||
slack_headers = { | ||
'Content-type': 'application/json', | ||
} | ||
sleep_time = os.environ['API_REQUEST_CYCLE'] # seconds | ||
run_inform_period = os.environ['RUNNING_INFORM_PERIOD'] # times sleep_time | ||
|
||
# Get access-token | ||
access_token = '' | ||
headers = '' | ||
|
||
# Display info | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
disp='Ops Manager: '+str(opsman_ip) | ||
print(disp) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
disp='Access token: '+str(access_token) | ||
print(disp) | ||
|
||
last_inform_id = '' | ||
last_inform_status = '' | ||
counter = 0 | ||
while True: | ||
response = requests.get(opsman_url, headers=headers, verify=False) | ||
if response.status_code != 200: | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print(str(response.content)) | ||
f = open('error.log', 'w+') | ||
f.write(str(response.content)) | ||
f.close() | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
msg=("%s ERROR %s") % (str(datetime.datetime.utcnow), str(response.content)) | ||
print(msg) | ||
# Get access-token | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print('Getting new access token') | ||
cmd = '/app/get-uaa-token.sh' | ||
access_token = str(subprocess.check_output(['sh',cmd])) | ||
access_token = access_token.rstrip("\n\r") | ||
access_token = access_token.split('\'')[1] | ||
print('Access token: '+access_token) | ||
headers = { | ||
'Authorization': 'Bearer '+access_token, | ||
} | ||
continue | ||
|
||
resp_json = json.loads(response.content) | ||
last_run=resp_json['installations'][0] | ||
day_passed = datetime.datetime.today() - datetime.datetime.strptime(last_run['started_at'], "%Y-%m-%dT%H:%M:%S.%fZ") | ||
|
||
ins_log_url = opsman_url + '/' + str(last_run['id']) + '/logs' | ||
ins_log_resp = requests.get(ins_log_url , headers=headers, verify=False) | ||
ins_log = str(ins_log_resp.content) | ||
ins_log = ins_log.split('\\n') | ||
r = re.compile(r"^=====") | ||
ins_log_p1 = list(filter(r.match, ins_log))[-1] | ||
ins_log_sum = ins_log_p1 + '\n...\n' + ins_log[-3] + '\n' + ins_log[-2] + '\n' + ins_log[-1] | ||
ins_log_sum = re.sub(r"[^a-zA-Z0-9\n.,/()-]+", ' ',ins_log_sum) | ||
|
||
if last_run['status'] == 'running': | ||
sbj = 'Tanzu Update in Progress - '+opsman_ip | ||
msg=("""Installation RUNNING - %s | ||
Timestamp: %s | ||
Id: %s | ||
Started: %s | ||
Finished: %s | ||
Status: %s | ||
Last Log Summary: | ||
%s | ||
""") % (str(opsman_ip),str(datetime.datetime.utcnow()),last_run['id'],last_run['started_at'],last_run['finished_at'],last_run['status'],ins_log_sum) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print(msg) | ||
if last_run['status'] != last_inform_status: | ||
last_inform_status = last_run['status'] | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print('Sending slack notification.') | ||
slack_data = '{"text":"'+msg+'"}' | ||
r = requests.post(slack_url, headers=slack_headers, data=slack_data, verify=False) | ||
print(str(r.content)) | ||
elif counter % run_inform_period == 0: | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print('Sending slack notification.') | ||
slack_data = '{"text":"'+msg+'"}' | ||
r = requests.post(slack_url, headers=slack_headers, data=slack_data, verify=False) | ||
print(str(r.content)) | ||
elif last_run['status'] == 'failed' and day_passed.days < 1 : | ||
last_inform_status = last_run['status'] | ||
sbj = 'Tanzu Updadate Failed - '+opsman_ip | ||
msg=("""Installation FAILED - %s | ||
Id: %s | ||
Started: %s | ||
Finished: %s | ||
Status: %s | ||
Last Log Summary: | ||
%s | ||
""") % (str(opsman_ip),last_run['id'],last_run['started_at'],last_run['finished_at'],last_run['status'],ins_log_sum) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print(msg) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print("last_inform_id: "+str(last_inform_id)) | ||
print("last_run_id: "+str(last_run['id'])) | ||
if str(last_inform_id) != str(last_run['id']): | ||
last_inform_id = str(last_run['id']) | ||
print('Sending slack notification.') | ||
slack_data = '{"text":"'+msg+'"}' | ||
r = requests.post(slack_url, headers=slack_headers, data=slack_data, verify=False) | ||
print(str(r.content)) | ||
elif last_run['status'] == 'succeeded' and day_passed.days < 1 : | ||
last_inform_status = last_run['status'] | ||
sbj = 'Tanzu Updadate Succeeded - '+opsman_ip | ||
msg=("""Installation SUCCESS - %s | ||
Id: %s | ||
Started: %s | ||
Finished: %s | ||
Status: %s | ||
""") % (str(opsman_ip),last_run['id'],last_run['started_at'],last_run['finished_at'],last_run['status']) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print(msg) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print("last_inform_id: "+str(last_inform_id)) | ||
print("last_run_id: "+str(last_run['id'])) | ||
if str(last_inform_id) != str(last_run['id']): | ||
last_inform_id = str(last_run['id']) | ||
print('Sending slack notification.') | ||
slack_data = '{"text":"'+msg+'"}' | ||
r = requests.post(slack_url, headers=slack_headers, data=slack_data, verify=False) | ||
print(str(r.content)) | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print('Sleeping '+str(sleep_time)+' seconds...') | ||
time.sleep(sleep_time) | ||
counter += 1 | ||
|
||
except Exception as e: | ||
print(str(datetime.datetime.utcnow()), end=' -- ') | ||
print(e) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Tanzu Opsman variables | ||
OPSMAN_IP= | ||
OPSMAN_USER= | ||
OPSMAN_PASSWORD= | ||
SSH_USER=ubuntu | ||
SSH_KEY=-----BEGIN RSA PRIVATE KEY-----\n....\n-----END RSA PRIVATE KEY----- | ||
# Proxy Settings | ||
# Leave empty if unneceissary | ||
HTTP_PROXY= | ||
HTTPS_PROXY= | ||
NO_PROXY= | ||
# Notificaiton variables | ||
SLACK_URL= | ||
SMTP_SERVER= | ||
EMAIL_SENDER= | ||
EMAIL_RECEIVERS= | ||
# Program settings. Leave defaults | ||
API_REQUEST_CYCLE=60 | ||
RUNNING_INFORM_PERIOD=60 | ||
|
||
|
||
|
||
|