Created
November 6, 2023 18:32
-
-
Save moshiurH/0e9eb9a534f73ccfb02f24c3f86a6f7e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
''' | |
PRE-REQUISITES: | |
======================= | |
1. Helm is installed. | |
2. helm repo add postgres-operator https://opensource.zalando.com/postgres-operator/charts/postgres-operator/ | |
3. helm repo add msrofficial https://registry.mirantis.com/charts/msr/msr | |
HOW TO RUN: | |
======================= | |
Change the `host` and `password` variables to that of your MSR instance. | |
Change the `n` variable if you want to create more data. | |
Run with `python3 populate_msr.yaml`. | |
''' | |
import json | |
import requests | |
import subprocess | |
from requests.auth import HTTPBasicAuth | |
from urllib3.exceptions import InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) | |
def get(url): | |
r = requests.get(url=url, auth=HTTPBasicAuth(admin, password), verify=False) | |
if r.status_code == 200 or r.status_code == 201 or r.status_code == 202 or r.status_code == 203: | |
return r, True | |
return r, False | |
def post(url, data): | |
r = requests.post(url=url, json=data, auth=HTTPBasicAuth(admin, password), verify=False) | |
# print(r.status_code) | |
# print(r.text) | |
if r.status_code == 200 or r.status_code == 201 or r.status_code == 202 or r.status_code == 203: | |
return r, True | |
return r, False | |
def put(url, data): | |
r = requests.put(url=url, json=data, headers={'Content-Type': 'application/json;charset=UTF-8'}, auth=HTTPBasicAuth(admin, password), verify=False) | |
if r.status_code == 200 or r.status_code == 201 or r.status_code == 202 or r.status_code == 203: | |
return r, True | |
return r, False | |
def post_binary(url, data): | |
r = requests.post(url=url, data=data, headers={'Content-Type': 'application/octet-stream'}, auth=HTTPBasicAuth(admin, password), verify=False) | |
if r.status_code == 200 or r.status_code == 201 or r.status_code == 202 or r.status_code == 203: | |
return r, True | |
return r, False | |
def patch(url, data): | |
r = requests.patch(url=url, json=data, headers={'Content-Type': 'application/json;charset=UTF-8'}, auth=HTTPBasicAuth(admin, password), verify=False) | |
if r.status_code == 200 or r.status_code == 201 or r.status_code == 202 or r.status_code == 203: | |
return r, True | |
return r, False | |
def push_image(source, dest): | |
if subprocess.call(['docker', 'pull', source]) != 0: quit() | |
if subprocess.call(['docker', 'tag', source, dest]) != 0: quit() | |
if subprocess.call(['docker', 'push', dest]) != 0: quit() | |
MSR_HELM_REPO = "msr" | |
POSTGRES_HELM_REPO = "postgres-operator" | |
n = "1" | |
new_user = "new-user-"+n | |
new_user_pass = new_user + "pass" | |
new_org = "new-org-"+n | |
new_team = new_org + "-team" | |
host = "a11cd6cfa93fe43af9a9c54cb7624835-1733437289.us-east-2.elb.amazonaws.com" | |
# host = "15.156.61.114" | |
admin = "admin" | |
password = "password" | |
# Update Settings to allow create repository on push | |
data = {'dtrHost': '', 'sso': False, 'createRepositoryOnPush': True, 'disableUpgrades': False, 'reportAnalytics': True, 'disableBackupWarning': False, 'clientCertAuthEnabled': False, 'auditAuthLogsEnabled': False, 'scanningEnabled': False, 'scanningSyncOnline': True, 'scanningDeadline': 360, 'scanningCVSSVersion': 3, 'scanningEnableAutoRecheck': False, 'jobHistoryCompactionEnabled': False, 'jobHistoryToKeep': 100, 'jobHistoryMaxAge': '', 'repoEventHistoryCompactionEnabled': False, 'repoEventHistoryToKeep': 100, 'repoEventHistoryMaxAge': '', 'disablePersistentCookies': False} | |
r, ok = post(url="https://"+host+"/api/v0/meta/settings", data=data) | |
if not ok: | |
print("1. Failed to Update Settings: statusCode=" + str(r.status_code), ", text: " + r.text); | |
quit() | |
else: | |
print("1. Updated Settings") | |
print("") | |
# Add some Global Enforcement Policies | |
data = {"enabled": True, "rules": [{"field": "tag", "operator": "sw", "values": ["mytag"]}]} | |
r, ok = post(url="https://"+host+"/api/v0/meta/settings/globalEnforcementPolicy/rules", data=data) | |
if not ok: | |
print("2. Failed to Update Global Enforcement Settings: statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("2. Updated Global Enforcement Settings") | |
print("") | |
# Create a new organization | |
r, ok = post(url="https://"+host+"/enzi/v0/accounts", data={"isOrg": True, "name": new_org}) | |
if not ok: | |
if r.json()["errors"][0]["code"] != "ACCOUNT_EXISTS": | |
print("3. Failed to Create Org '"+new_org+"': statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("3. Org '"+new_org+"' already exists") | |
else: | |
print("3. Created New Org '"+new_org+"'") | |
print("") | |
# Docker login | |
if subprocess.call(['docker', 'login', host, '-u', admin, '-p', password]) != 0: quit() | |
# Push admin/ubuntu:18.04 | |
img = "ubuntu:18.04" | |
push_image(source=img, dest=host+"/admin/"+img) | |
print("4. Pushed 'admin/"+img + "' image") | |
print("") | |
# Push admin/ubuntu:20.04 | |
img = "ubuntu:20.04" | |
push_image(source=img, dest=host+"/admin/"+img) | |
print("5. Pushed 'admin/"+img + "' image") | |
print("") | |
# Make admin/ubuntu repository public | |
r, ok = patch(url="https://"+host+"/api/v0/repositories/admin/ubuntu", data={"visibility": "public"}) | |
if not ok: | |
print("6. Failed to make 'admin/ubuntu' repository public: statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("6. Made 'admin/ubuntu' repository public") | |
print("") | |
# Create an enforcement policy in admin/ubuntu repository | |
r, ok = post(url="https://"+host+"/api/v0/repositories/admin/ubuntu/enforcementPolicies", data={"enabled": True, "rules": [{"field": "component.name", "operator": "eq", "values": ["mycomponent"]}]}) | |
if not ok: | |
print("7. Failed to make new Enforcement Policy for 'admin/ubuntu' repository: statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("7. Made new Enforcement Policy for 'admin/ubuntu' repository") | |
print("") | |
# Push myorg/postgres-operator:v1.7.1 | |
img = "postgres-operator:v1.7.1" | |
push_image(source='registry.opensource.zalan.do/acid/'+img, dest=host+'/'+new_org+'/'+img) | |
print("8. Pushed image '" + new_org+'/'+img + "'") | |
print("") | |
# Push myorg/postgres-operator/postgres-operator-1.7.1.tgz | |
repo = 'postgres-operator' | |
chart = 'postgres-operator-1.7.1.tgz' | |
if subprocess.call(['helm', 'pull', POSTGRES_HELM_REPO+'/postgres-operator', '--version=1.7.1']) != 0: quit() | |
with open('./'+chart, 'rb') as f: | |
data = f.read() | |
r, ok = post_binary(url='https://'+host+'/charts/api/'+new_org+'/'+repo+'/charts', data=data) | |
if not ok: | |
if r.json()["errors"][0]["code"] != "UNPROCESSABLE_ENTITY" and "already exists" not in r.json()["errors"][0]["detail"]: | |
print("9. Failed to Push Chart '"+new_org+"/"+chart+"': statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("9. Chart '"+chart+"' already exists in '"+new_org+"/"+repo+"' repository") | |
else: | |
print("9. Pushed Chart '"+new_org+"/"+chart+"'") | |
print("") | |
# Create msr organization | |
r, ok = post(url="https://"+host+"/enzi/v0/accounts", data={"isOrg": True, "name": "msr"}) | |
if not ok: | |
if r.json()["errors"][0]["code"] != "ACCOUNT_EXISTS": | |
print("10. Failed to Create User "+new_user+": statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("10. Organization 'msr' already exists") | |
else: | |
print("10. Created new organization 'msr'") | |
print("") | |
# Push msr/msr | |
img = "msr-api:3.0.7" | |
push_image(source='registry.mirantis.com/msr/'+img, dest=host+'/msr/msr:3.0.7') | |
print("11. Pushed image '" + new_org+"/msr/msr:3.0.7'") | |
print("") | |
# Push msr/msr/msr-1.0.7.tgz | |
repo = 'msr' | |
chart = 'msr-1.0.7.tgz' | |
if subprocess.call(['helm', 'pull', MSR_HELM_REPO+'/msr', '--version=1.0.7']) != 0: quit() | |
with open('./'+chart, 'rb') as f: | |
data = f.read() | |
r, ok = post_binary(url='https://'+host+'/charts/api/'+repo+'/'+repo+'/charts', data=data) | |
if not ok: | |
if r.json()["errors"][0]["code"] != "UNPROCESSABLE_ENTITY" and "already exists" not in r.json()["errors"][0]["detail"]: | |
print("12. Failed to Push Chart '"+new_org+"/"+chart+"': statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("12. Chart '"+chart+"' already exists in '"+repo+"/"+repo+"' repository") | |
else: | |
print("12. Pushed Chart '"+new_org+"/"+chart+"'") | |
print("") | |
# Create a new user | |
r, ok = post(url="https://"+host+"/enzi/v0/accounts", data={"isOrg": False, "isActive": True, "isAdmin": False, "name": new_user, "password": "msrpassword"}) | |
if not ok: | |
if r.json()["errors"][0]["code"] != "ACCOUNT_EXISTS": | |
print("13. Failed to Create User "+new_user+": statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("13. User '"+new_user+"' already exists") | |
else: | |
print("13. Created new user '"+new_user+"'") | |
print("") | |
# Add new_team to new_org | |
r, ok = post(url="https://"+host+"/enzi/v0/accounts/"+new_org+"/teams", data={"name": new_team, "type": "managed"}) | |
if not ok: | |
if r.json()["errors"][0]["code"] != "TEAM_EXISTS": | |
print("14. Failed to Add team '"+new_team+"' to org '"+new_org+"': statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("14. Team '"+new_team+"' already exists in '"+new_org+"' org") | |
else: | |
print("14. Added team '"+new_team+"' to org '"+new_org+"'") | |
print("") | |
# Add R/W permissions to new_org/repo for new_team | |
repo = 'postgres-operator' | |
r, ok = put(url="https://"+host+"/api/v0/repositories/"+new_org+"/"+repo+"/teamAccess/"+new_team, data={"accessLevel": "read-write"}) | |
if not ok: | |
print("15. Failed to add R/W permissions for '"+new_org+"/"+repo+"' to '"+new_team+"' team: statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("15. Added R/W permissions for for '"+new_org+"/"+repo+"' to '"+new_team+"' team") | |
print("") | |
# Get ID for new_team team | |
id = "" | |
r, ok = get(url="https://"+host+"/enzi/v0/accounts/"+new_org+"/teams/"+new_team) | |
if not ok: | |
print("16. Failed to get ID for '"+new_team+"' team: statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
id = r.json()["id"] | |
print("16. Got ID ('"+id+"') for '"+new_team+"' team") | |
print("") | |
# Add new_user as member in new_team: | |
r, ok = put(url="https://"+host+"/enzi/v0/accounts/"+new_org+"/teams/id:"+id+"/members/" + new_user, data={}) | |
if not ok: | |
print("17. Failed to add user '"+new_user+"' to '"+new_team+"' team: statusCode=" + str(r.status_code), ", text: " + r.text) | |
quit() | |
else: | |
print("17. Added user '"+new_user+"' to '"+new_team+"' team") | |
print("") | |
print("SUCCESSFULLY COMPLETED ALL STEPS") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment