-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Azure Policy: Jenkinsfile for running azure policies #734
Merged
ebattat
merged 1 commit into
redhat-performance:main
from
athiruma:add_azure_jenkinsfile
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,2 @@ | ||
|
||
|
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,71 @@ | ||
def accounts_list = ['perfscale'] | ||
pipeline { | ||
agent { | ||
docker { | ||
label 'cloud-governance-worker' | ||
image 'quay.io/athiru/centos-stream8-podman:latest' | ||
args '-u root -v /etc/postfix/main.cf:/etc/postfix/main.cf --privileged' | ||
} | ||
} | ||
environment { | ||
POLICIES_IN_ACTION = '[]' | ||
AZURE_CLIENT_SECRET = credentials('cloud-governance-azure-client-secret') | ||
AZURE_TENANT_ID = credentials('cloud-governance-azure-tenant-id') | ||
AZURE_ACCOUNT_ID = credentials('cloud-governance-azure-account-id') | ||
AZURE_CLIENT_ID = credentials('cloud-governance-azure-client-id') | ||
ES_HOST = credentials('cloud-governance-es-host') | ||
ES_PORT = credentials('cloud-governance-es-port') | ||
LDAP_HOST_NAME = credentials('cloud-governance-ldap-host-name') | ||
contact2 = "[email protected]" | ||
} | ||
stages { | ||
stage('Checkout') { // Checkout (git clone ...) the projects repository | ||
steps { | ||
checkout scm | ||
} | ||
} | ||
stage('Initial Cleanup') { | ||
steps { | ||
sh '''if [[ "$(podman images -q quay.io/ebattat/cloud-governance 2> /dev/null)" != "" ]]; then podman rmi -f $(podman images -q quay.io/ebattat/cloud-governance 2> /dev/null); fi''' | ||
} | ||
} | ||
stage('Run Azure Policies') { | ||
steps { | ||
script { | ||
for (account in accounts_list ) { | ||
echo "Running for account ${account.toUpperCase()}" | ||
withCredentials([string(credentialsId: "${account}-azure-client-secret", variable: 'client_secret'), | ||
string(credentialsId: "${account}-azure-client-id", variable: 'client_id'), | ||
string(credentialsId: "${account}-azure-tenant-id", variable: 'tenant_id'), | ||
string(credentialsId: "${account}-azure-subscription-id", variable: 'subscription_id'), | ||
string(credentialsId: "${account}-azure-account-id", variable: 'account_id')]) { | ||
env.account_name = "Azure-${account}" | ||
sh 'python3 jenkins/clouds/azure/daily/policies/run_policies.py' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
stage('Finalize Cleanup') { | ||
steps { | ||
sh '''if [[ "$(podman images -q quay.io/ebattat/cloud-governance 2> /dev/null)" != "" ]]; then podman rmi -f $(podman images -q quay.io/ebattat/cloud-governance 2> /dev/null); fi''' | ||
deleteDir() | ||
} | ||
} | ||
} | ||
post { | ||
always { | ||
deleteDir() | ||
} | ||
failure { | ||
script { | ||
msg = "Build error for ${env.JOB_NAME} ${env.BUILD_NUMBER} (${env.BUILD_URL})" | ||
emailext body: """\ | ||
Jenkins job: ${env.BUILD_URL}\nSee the console output for more details: ${env.BUILD_URL}consoleFull\n\n | ||
""", | ||
subject: msg, | ||
to: "${contact2}" | ||
} | ||
} | ||
} | ||
} |
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,105 @@ | ||
import os | ||
from ast import literal_eval | ||
|
||
account_name = os.environ['account_name'] | ||
AZURE_CLIENT_SECRET = os.environ['client_secret'] | ||
AZURE_TENANT_ID = os.environ['tenant_id'] | ||
AZURE_ACCOUNT_ID = os.environ['account_id'] | ||
AZURE_CLIENT_ID = os.environ['client_id'] | ||
AZURE_SUBSCRIPTION_ID = os.environ['subscription_id'] | ||
days_to_delete_resource = os.environ.get('days_to_delete_resource', 7) | ||
LDAP_HOST_NAME = os.environ['LDAP_HOST_NAME'] | ||
LOGS = os.environ.get('LOGS', 'logs') | ||
ES_HOST = os.environ['ES_HOST'] | ||
ES_PORT = os.environ['ES_PORT'] | ||
CLOUD_GOVERNANCE = "quay.io/ebattat/cloud-governance:latest" | ||
|
||
|
||
def get_policies(file_type: str = '.py', exclude_policies: list = None): | ||
""" | ||
This method return a list of policies name without extension, that can filter by type | ||
@return: list of custodian policies name | ||
""" | ||
exclude_policies = [] if not exclude_policies else exclude_policies | ||
custodian_policies = [] | ||
root_folder = os.path.dirname( | ||
os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))) | ||
policies_path = os.path.join(root_folder, 'cloud_governance', 'policy', 'azure') | ||
for (_, _, filenames) in os.walk(policies_path): | ||
for filename in filenames: | ||
if not filename.startswith('__') and filename.endswith(file_type): | ||
if filename.split('.')[0] not in exclude_policies: | ||
if not file_type: | ||
custodian_policies.append(os.path.splitext(filename)[0]) | ||
elif file_type and file_type in filename: | ||
custodian_policies.append(os.path.splitext(filename)[0]) | ||
return custodian_policies | ||
|
||
|
||
GLOBAL_COST_POLICIES = ['cost_billing_reports'] | ||
available_policies = get_policies(exclude_policies=GLOBAL_COST_POLICIES) | ||
|
||
|
||
# # available_policies: Run policies in dry_run="yes" mode | ||
|
||
|
||
def run_cmd(cmd: str): | ||
""" | ||
This method runs the shell command | ||
:param cmd: | ||
:type cmd: | ||
:return: | ||
:rtype: | ||
""" | ||
os.system(cmd) | ||
|
||
|
||
def get_container_cmd(env_dict: dict): | ||
env_list = ' '.join(list(map(lambda item: f'-e {item[0]}="{item[1]}"', env_dict.items()))) | ||
container_name = "cloud-governance" | ||
container_run_cmd = f"""podman run --rm --name "{container_name}" --net="host" {env_list} {CLOUD_GOVERNANCE}""" | ||
return container_run_cmd | ||
|
||
|
||
policies_in_action = os.environ.get('POLICIES_IN_ACTION', []) | ||
if isinstance(policies_in_action, str): | ||
policies_in_action = literal_eval(policies_in_action) | ||
policies_not_action = list(set(available_policies) - set(policies_in_action)) | ||
|
||
|
||
container_env_dict = { | ||
"AZURE_CLIENT_SECRET": AZURE_CLIENT_SECRET, | ||
"AZURE_TENANT_ID": AZURE_TENANT_ID, | ||
"AZURE_ACCOUNT_ID": AZURE_ACCOUNT_ID, | ||
"AZURE_CLIENT_ID": AZURE_CLIENT_ID, | ||
"AZURE_SUBSCRIPTION_ID": AZURE_SUBSCRIPTION_ID, | ||
"account": account_name, | ||
"PUBLIC_CLOUD_NAME": "Azure", | ||
"dry_run": "yes", | ||
"LDAP_HOST_NAME": LDAP_HOST_NAME, | ||
"DAYS_TO_DELETE_RESOURCE": days_to_delete_resource, | ||
"es_host": ES_HOST, "es_port": ES_PORT, | ||
"MANAGER_EMAIL_ALERT": "False", "EMAIL_ALERT": "False", "log_level": "INFO", | ||
'DAYS_TO_TAKE_ACTION': days_to_delete_resource, | ||
} | ||
|
||
|
||
def run_policies(policies: list, dry_run: str = 'yes'): | ||
container_env_dict.update({}) | ||
for policy in policies: | ||
container_env_dict.update({'dry_run': dry_run, 'policy': policy}) | ||
container_cmd = get_container_cmd(container_env_dict) | ||
run_cmd(container_cmd) | ||
|
||
|
||
# Running the polices in dry_run=yes | ||
|
||
run_cmd(f"echo Running the cloud_governance policies with dry_run=yes") | ||
run_cmd(f"echo Polices list: {policies_not_action}") | ||
run_policies(policies=policies_not_action) | ||
|
||
# Running the polices in dry_run=no | ||
|
||
run_cmd('echo "Running the CloudGovernance policies with dry_run=no" ') | ||
run_cmd(f"echo Polices list: {policies_in_action}") | ||
run_policies(policies=policies_in_action, dry_run='no') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you run it and verify it works ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, Jenkins result