Skip to content

Commit

Permalink
Updated backup/restore scripts. (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov authored Sep 1, 2020
1 parent 32c3e46 commit bde6021
Show file tree
Hide file tree
Showing 21 changed files with 991 additions and 48 deletions.
363 changes: 320 additions & 43 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"postinstall": "npm rebuild node-sass"
},
"devDependencies": {
"@azure/storage-blob": "12.1.2",
"@types/chai": "^4.2.12",
"@types/google-maps": "^3.2.2",
"@types/knockout": "^3.4.68",
Expand Down Expand Up @@ -61,11 +62,11 @@
"webpack-merge": "^5.1.2"
},
"dependencies": {
"@paperbits/azure": "0.1.313",
"@paperbits/common": "0.1.313",
"@paperbits/core": "0.1.313",
"@paperbits/prosemirror": "0.1.313",
"@paperbits/styles": "0.1.313",
"@paperbits/azure": "0.1.314",
"@paperbits/common": "0.1.314",
"@paperbits/core": "0.1.314",
"@paperbits/prosemirror": "0.1.314",
"@paperbits/styles": "0.1.314",
"@webcomponents/custom-elements": "1.4.2",
"@webcomponents/shadydom": "^1.7.4",
"adal-vanilla": "^1.0.18",
Expand Down
7 changes: 7 additions & 0 deletions scripts.v2/capture.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@REM Capture the content of an API Management portal into dest_folder - incl. pages, layouts, configuration, etc. but excluding media files

set management_endpoint="< service name >.management.azure-api.net"
set access_token="SharedAccessSignature ..."
set dest_folder="../dist/snapshot"

node ./capture %management_endpoint% %access_token% %dest_folder%
55 changes: 55 additions & 0 deletions scripts.v2/capture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require("fs");
const path = require("path");
const { request, downloadBlobs, getStorageSasTokenOrThrow } = require("./utils");
const managementApiEndpoint = process.argv[2];
const managementApiAccessToken = process.argv[3];
const destinationFolder = process.argv[4];


async function getContentTypes() {
const data = await request("GET", `https://${managementApiEndpoint}/subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/00000/contentTypes?api-version=2019-12-01`, managementApiAccessToken);
const contentTypes = data.value.map(x => x.id.replace("\/contentTypes\/", ""));

return contentTypes;
}

async function getContentItems(contentType) {
const data = await request("GET", `https://${managementApiEndpoint}/subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/00000/contentTypes/${contentType}/contentItems?api-version=2019-12-01`, managementApiAccessToken);
const contentItems = data.value;

return contentItems;
}

async function captureJson() {
const result = {};
const contentTypes = await getContentTypes();

for (const contentType of contentTypes) {
const contentItems = await getContentItems(contentType);

contentItems.forEach(contentItem => {
result[contentItem.id] = contentItem;
delete contentItem.id;
});
}

await fs.promises.mkdir(path.resolve(destinationFolder), { recursive: true });

fs.writeFileSync(`${destinationFolder}/data.json`, JSON.stringify(result));
}

async function capture() {
const blobStorageUrl = await getStorageSasTokenOrThrow(managementApiEndpoint, managementApiAccessToken);
const localMediaFolder = `./${destinationFolder}/media`;

await captureJson();
await downloadBlobs(blobStorageUrl, localMediaFolder);
}

capture()
.then(() => {
console.log("DONE");
})
.catch(error => {
console.log(error);
});
6 changes: 6 additions & 0 deletions scripts.v2/cleanup.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@REM Delete the content of an API Management portal - incl. pages, layouts, configuration, media files, etc.

set management_endpoint="< service name >.management.azure-api.net"
set access_token="SharedAccessSignature ..."

node ./cleanup %management_endpoint% %access_token%
45 changes: 45 additions & 0 deletions scripts.v2/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { request, deleteBlobs, getStorageSasTokenOrThrow } = require("./utils");
const managementApiEndpoint = process.argv[2];
const managementApiAccessToken = process.argv[3];


async function getContentTypes() {
const data = await request("GET", `https://${managementApiEndpoint}/subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/00000/contentTypes?api-version=2019-12-01`, managementApiAccessToken);
const contentTypes = data.value.map(x => x.id.replace("\/contentTypes\/", ""));

return contentTypes;
}

async function getContentItems(contentType) {
const data = await request("GET", `https://${managementApiEndpoint}/subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/00000/contentTypes/${contentType}/contentItems?api-version=2019-12-01`, managementApiAccessToken);
const contentItems = data.value;

return contentItems;
}

async function deleteContent() {
const contentTypes = await getContentTypes();

for (const contentType of contentTypes) {
const contentItems = await getContentItems(contentType);

for (const contentItem of contentItems) {
await request("DELETE", `https://${managementApiEndpoint}//subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/00000/${contentItem.id}?api-version=2019-12-01`, managementApiAccessToken);
}
}
}

async function cleanup() {
const blobStorageUrl = await getStorageSasTokenOrThrow(managementApiEndpoint, managementApiAccessToken);

await deleteContent();
await deleteBlobs(blobStorageUrl);
}

cleanup()
.then(() => {
console.log("DONE");
})
.catch(error => {
console.log(error);
});
61 changes: 61 additions & 0 deletions scripts.v2/configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const fs = require('fs'),
path = require('path'),
configDesignFile = path.join(__dirname, '\\..\\src\\config.design.json'),
configPublishFile = path.join(__dirname, '\\..\\src\\config.publish.json'),
configRuntimeFile = path.join(__dirname, '\\..\\src\\config.runtime.json');

const managementEndpoint = process.argv[2];
const apimSasAccessTokenValue = process.argv[3];
const backendUrlValue = process.argv[4];
const apimServiceNameValue = process.argv[5];
const apimServiceUrlValue = `https://${managementEndpoint}/subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/${apimServiceNameValue}`;

const apimServiceParameter = "managementApiUrl";
const apimSasAccessTokenParameter = "managementApiAccessToken";
const backendUrlParameter = "backendUrl";

fs.readFile(configDesignFile, { encoding: 'utf-8' }, function (err, data) {
if (!err) {
const obj = JSON.parse(data);
obj[apimServiceParameter] = apimServiceUrlValue;
obj[apimSasAccessTokenParameter] = apimSasAccessTokenValue;
obj[backendUrlParameter] = backendUrlValue;
fs.writeFile(configDesignFile, JSON.stringify(obj, null, 4), function (errWrite) {
if (errWrite) {
return console.log(errWrite);
}
});
} else {
console.log(err);
}
});

fs.readFile(configPublishFile, { encoding: 'utf-8' }, function (err, data) {
if (!err) {
const obj = JSON.parse(data);
obj[apimServiceParameter] = apimServiceUrlValue;
obj[apimSasAccessTokenParameter] = apimSasAccessTokenValue;
fs.writeFile(configPublishFile, JSON.stringify(obj, null, 4), function (errWrite) {
if (errWrite) {
return console.log(errWrite);
}
});
} else {
console.log(err);
}
});

fs.readFile(configRuntimeFile, { encoding: 'utf-8' }, function (err, data) {
if (!err) {
const obj = JSON.parse(data);
obj[apimServiceParameter] = apimServiceUrlValue;
obj[backendUrlParameter] = backendUrlValue;
fs.writeFile(configRuntimeFile, JSON.stringify(obj, null, 4), function (errWrite) {
if (errWrite) {
return console.log(errWrite);
}
});
} else {
console.log(err);
}
});
1 change: 1 addition & 0 deletions scripts.v2/data.json

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions scripts.v2/deploy-default.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@REM Reprovision an existing API Management portal deployment - clean all the content, autogenerate new content, upload it, publish the portal, and host it

cd ..
call npm install
cd scripts

set apimServiceName="<service-name>"
set management_endpoint="<service-name>.management.azure-api.net/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.ApiManagement/service/<service-name>"
set access_token="SharedAccessSignature integration&..."
set portalUrl="https://portalstorage.../"
set backendUrl="https://<service-name>.developer.azure-api.net"

set source_folder="../dist/snapshot"

node ./cleanup %management_endpoint% %access_token%
node ./configure %management_endpoint% %access_token% %backendUrl% %apimServiceName%
node ./generate %management_endpoint% %access_token% %source_folder%

cd ..

@REM Run the publishing step and upload the generated portal to a Storage Account for hosting

call npm run publish
call az storage blob upload-batch --source dist/website --destination $web --connection-string %storage_connection_string%
explorer %portalUrl%
7 changes: 7 additions & 0 deletions scripts.v2/generate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@REM Generate and provision default content of an API Management portal - incl. pages, layouts, configuration, media files, etc.

set management_endpoint="< service name >.management.azure-api.net"
set access_token="SharedAccessSignature ..."
set source_folder="../dist/snapshot"

node ./generate %management_endpoint% %access_token% %source_folder%
36 changes: 36 additions & 0 deletions scripts.v2/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require("fs");
const { request, uploadBlobs, getStorageSasTokenOrThrow } = require("./utils");
const managementApiEndpoint = process.argv[2]
const managementApiAccessToken = process.argv[3]
const sourceFolder = process.argv[4];


async function generateJson() {
const data = fs.readFileSync(`${sourceFolder}/data.json`);
const dataObj = JSON.parse(data);
const keys = Object.keys(dataObj);

for (const key of keys) {
await request(
"PUT",
`https://${managementApiEndpoint}/subscriptions/00000/resourceGroups/00000/providers/Microsoft.ApiManagement/service/00000/${key}?api-version=2019-12-01`,
managementApiAccessToken,
JSON.stringify(dataObj[key]));
}
}

async function generate() {
const blobStorageUrl = await getStorageSasTokenOrThrow(managementApiEndpoint, managementApiAccessToken);
const localMediaFolder = `./${sourceFolder}/media`;

await generateJson();
await uploadBlobs(blobStorageUrl, localMediaFolder);
}

generate()
.then(() => {
console.log("DONE");
})
.catch(error => {
console.log(error);
});
8 changes: 8 additions & 0 deletions scripts.v2/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generate and provision default content of an API Management portal - incl. pages, layouts, configuration, media files, etc.

export management_endpoint="< service name >.management.azure-api.net"
export access_token="SharedAccessSignature ..."
export source_folder="../dist/snapshot"

# make sure to double quote the $access_token variable so it handles the space correctly
node ./generate $management_endpoint "$access_token" $data_file
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bde6021

Please sign in to comment.