-
Notifications
You must be signed in to change notification settings - Fork 23
add action to generate azure credentials #140
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
Merged
abikouo
merged 4 commits into
ansible-network:main
from
abikouo:ansible_test_providers_20240119
Jan 24, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| name: ansible-azure-test-provider | ||
| description: create file cloud-config-azure.ini in order to run ansible-test | ||
| inputs: | ||
| collection_path: | ||
| description: Path to the collection to create file in. | ||
| required: true | ||
| ansible_core_ci_key: | ||
| description: ansible core ci key | ||
| required: true | ||
| stage: | ||
| description: session stage | ||
| default: "prod" | ||
| session_id: | ||
| description: Azure session identifier | ||
| default: ${{ github.head_ref }} | ||
|
|
||
| outputs: | ||
| configuration_file: | ||
| description: Azure session details | ||
| value: ${{ inputs.collection_path }}/tests/integration/cloud-config-azure.ini | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Set up Python '3.9' | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: "3.9" | ||
|
|
||
| - name: install python required modules | ||
| run: pip install requests | ||
| shell: bash | ||
|
|
||
| - name: Generate SSH Key | ||
| run: ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa | ||
| shell: bash | ||
|
|
||
| - name: create Azure session file | ||
| run: | | ||
| python3 ${{ github.action_path }}/create_session.py | ||
| shell: bash | ||
| env: | ||
| ANSIBLE_CORE_CI_KEY: ${{ inputs.ansible_core_ci_key }} | ||
| ANSIBLE_CORE_CI_STAGE: "prod" | ||
| ANSIBLE_TEST_CLOUD_CONFIG_FILE: "${{ inputs.collection_path }}/tests/integration/cloud-config-azure.ini" |
69 changes: 69 additions & 0 deletions
69
.github/actions/ansible_azure_test_provider/create_session.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/python | ||
| """Script to request new azure session using ansible_core_ci_key.""" | ||
|
|
||
| import json | ||
| import os | ||
| import secrets | ||
| import sys | ||
|
|
||
| from pathlib import PosixPath | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Request new azure session credentials.""" | ||
| try: | ||
| ansible_core_ci_key = os.environ["ANSIBLE_CORE_CI_KEY"] | ||
| except KeyError: | ||
| sys.stderr.write("Missing mandatory environment variable ANSIBLE_CORE_CI_KEY.\n") | ||
| sys.exit(1) | ||
| ansible_core_ci_stage = os.environ.get("ANSIBLE_CORE_CI_STAGE") or "prod" | ||
| headers = {"Content-Type": "application/json"} | ||
| ansible_ssh_public_key_path = os.environ.get( | ||
| "ANSIBLE_TEST_SSH_PUBLIC_KEY_PATH" | ||
| ) or os.path.expanduser("~/.ssh/id_rsa.pub") | ||
|
|
||
| data = { | ||
| "config": { | ||
| "platform": "azure", | ||
| "version": "", | ||
| "architecture": "", | ||
| "public_key": PosixPath(ansible_ssh_public_key_path).read_text(encoding="utf-8"), | ||
| }, | ||
| "auth": { | ||
| "remote": { | ||
| "key": ansible_core_ci_key, | ||
| "nonce": None, | ||
| } | ||
| }, | ||
| } | ||
| session_id = "".join(secrets.choice("0123456789abcdef") for i in range(32)) | ||
| endpoint_url = ( | ||
| f"https://ansible-core-ci.testing.ansible.com/{ansible_core_ci_stage}/azure/{session_id}" | ||
| ) | ||
| response = requests.put(endpoint_url, data=json.dumps(data), headers=headers, timeout=30) | ||
| if response.status_code != 200: | ||
| sys.stderr.write( | ||
| f"HTTP Status Code error - Expected (200) Received ({response.status_code})" | ||
| ) | ||
| sys.exit(1) | ||
abikouo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # create ansible-test credential file | ||
| credentials = response.json().get("azure") | ||
| cloud_config_file = os.environ.get("ANSIBLE_TEST_CLOUD_CONFIG_FILE") or "cloud-config-azure.ini" | ||
| cloud_config_content = [ | ||
| "[default]", | ||
| f"AZURE_CLIENT_ID: {credentials.get('clientId')}", | ||
| f"AZURE_SECRET: {credentials.get('clientSecret')}", | ||
| f"AZURE_SUBSCRIPTION_ID: {credentials.get('subscriptionId')}", | ||
| f"AZURE_TENANT: {credentials.get('tenantId')}", | ||
| f"RESOURCE_GROUP: {credentials.get('resourceGroupNames')[0]}", | ||
| f"RESOURCE_GROUP_SECONDARY: {credentials.get('resourceGroupNames')[1]}", | ||
| ] | ||
| with open(cloud_config_file, mode="w", encoding="utf-8") as file_writer: | ||
| file_writer.write("\n".join(cloud_config_content)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.