Skip to content
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

Update apiview prod to staging sync job to use managed identity #8673

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eng/scripts/python/apiview-syncdb/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
azure-cosmos
azure-storage-blob
azure-storage-blob
azure-identity
27 changes: 12 additions & 15 deletions eng/scripts/python/apiview-syncdb/sync_cosmosdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import traceback
from ast import literal_eval
from azure.cosmos import CosmosClient
from azure.identity import AzurePowerShellCredential
from azure.storage.blob import BlobServiceClient

logging.getLogger().setLevel(logging.INFO)
Expand All @@ -25,13 +26,14 @@
BACKUP_CONTAINER = "backups"
BLOB_NAME_PATTERN ="cosmos/{0}/{1}"

# Create a AzurePowerShellCredential()
credentials = AzurePowerShellCredential()
praveenkuttappan marked this conversation as resolved.
Show resolved Hide resolved

def restore_data_from_backup(backup_storage_url, dest_url, db_name):

def restore_data_from_backup(connection_string, dest_url, dest_key, db_name):

dest_db_client = get_db_client(dest_url, dest_key, db_name)
dest_db_client = get_db_client(dest_url, db_name)

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_service_client = BlobServiceClient(backup_storage_url, credential = credentials)
container_client = blob_service_client.get_container_client(BACKUP_CONTAINER)
for cosmos_container_name in COSMOS_CONTAINERS:
# Load source records from backup file
Expand Down Expand Up @@ -68,10 +70,10 @@ def get_backup_contents(container_client, blob_name):


# Create cosmosdb clients
def get_db_client(dest_url, dest_key, db_name):
def get_db_client(dest_url, db_name):

# Create cosmosdb client for destination db
dest_cosmos_client = CosmosClient(dest_url, credential=dest_key)
dest_cosmos_client = CosmosClient(dest_url, credential=credentials)
if not dest_cosmos_client:
logging.error("Failed to create cosmos client for destination db")
exit(1)
Expand All @@ -83,7 +85,7 @@ def get_db_client(dest_url, dest_key, db_name):
dest_db_client = dest_cosmos_client.get_database_client(db_name)
logging.info("Created database clients")
except:
logging.error("Failed to create databae client using CosmosClient")
logging.error("Failed to create database client using CosmosClient")
traceback.print_exc()
exit(1)
return dest_db_client
Expand Down Expand Up @@ -122,20 +124,15 @@ def fetch_records(container_client, container_name):
)

parser.add_argument(
"--backup-connection-string",
"--backup-storage-url",
required=True,
help=("Connection string to backup storage account"),
help=("URL to backup storage account"),
)
parser.add_argument(
"--dest-url",
required=True,
help=("URL to destination cosmosdb"),
)
parser.add_argument(
"--dest-key",
required=True,
help=("Destination cosmosdb account key"),
)
parser.add_argument(
"--db-name",
required=True,
Expand All @@ -145,4 +142,4 @@ def fetch_records(container_client, container_name):
args = parser.parse_args()

logging.info("Syncing database..")
restore_data_from_backup(args.backup_connection_string, args.dest_url, args.dest_key, args.db_name)
restore_data_from_backup(args.backup_storage_url, args.dest_url, args.db_name)
24 changes: 16 additions & 8 deletions src/dotnet/APIView/apiview-sync-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ stages:
- stage: 'Main'
jobs:
- job: 'BlobCopy'
displayName: 'Sync API reviews'
pool:
name: azsdk-pool-mms-win-2022-general
vmImage: windows-2022
Expand Down Expand Up @@ -40,13 +41,20 @@ stages:
env:
AZCOPY_AUTO_LOGIN_TYPE: 'PSCRED'

- task: UsePythonVersion@0
displayName: 'Use Python 3.6'
inputs:
versionSpec: 3.6
- template: /eng/pipelines/templates/steps/use-python-version.yml
parameters:
versionSpec: '3.10'

- script: |
cd $(Build.SourcesDirectory)/eng/scripts/python/apiview-syncdb/
python -m pip install -r requirements.txt
python ./sync_cosmosdb.py --dest-url $(apiview-staging-cosmos-url) --dest-key $(apiview-staging-cosmos-key) --db-name $(apiview-cosmosdb-name) --backup-connection-string $(apiview-cosmos-backup-connection)
- task: AzurePowerShell@5
displayName: Sync CosmosDB
inputs:
azureSubscription: 'Azure SDK Engineering System'
ScriptType: 'InlineScript'
azurePowerShellVersion: LatestVersion
workingDirectory: $(Build.BinariesDirectory)
pwsh: true
Inline: |
cd $(Build.SourcesDirectory)/eng/scripts/python/apiview-syncdb/
python -m pip install -r requirements.txt
python ./sync_cosmosdb.py --dest-url $(apiview-staging-cosmos-url) --db-name $(apiview-cosmosdb-name) --backup-storage-url $(apiview-cosmos-backup-url)
praveenkuttappan marked this conversation as resolved.
Show resolved Hide resolved
exit $LASTEXITCODE
praveenkuttappan marked this conversation as resolved.
Show resolved Hide resolved