Skip to content

Instantly share code, notes, and snippets.

@jonfreeland
Last active August 29, 2015 14:06
Show Gist options
  • Save jonfreeland/f5ddae89289b082bfb2f to your computer and use it in GitHub Desktop.
Save jonfreeland/f5ddae89289b082bfb2f to your computer and use it in GitHub Desktop.
Copy connection strings and app settings between Azure web sites.
var fs = require('fs'),
webSiteManagement = require('azure-mgmt-website'),
argv = require('yargs')
.usage('Usage: $0 --subscriptionId [guid] --pem [path] --sourceWebSpace [string] --sourceSiteName [string] --targetWebSpace [string] --targetWebSite [string]')
.demand(['subscriptionId', 'pem', 'sourceSiteName', 'targetSiteName'])
.describe('pem', 'azure account cert export')
.describe('sourceWebSpace', 'exclude to see list of web spaces')
.describe('targetWebSpace', 'exclude to see list of web spaces')
.argv;
var ignoredAppSettings = [
'WEBSITE_HTTPLOGGING_RETENTION_DAYS',
'WEBSITE_HTTPLOGGING_CONTAINER_URL',
'DIAGNOSTICS_AZURETABLESASURL',
'DIAGNOSTICS_AZUREBLOBCONTAINERSASURL',
'DIAGNOSTICS_AZUREBLOBRETENTIONINDAYS'
];
var subId = argv.subscriptionId;
var pem = argv.pem;
var wsMgmtClient = webSiteManagement.createWebSiteManagementClient(webSiteManagement.createCertificateCloudCredentials({
subscriptionId: subId,
pem: fs.readFileSync(pem).toString()
}));
if (!argv.sourceWebSpace || !argv.targetWebSpace) {
// Get all the available webspaces under a subscription.
wsMgmtClient.webSpaces.list(function (err, result) {
if (err) {
console.error(err);
} else {
console.log('You did not provide --sourceWebSpace or --targetWebSpace, so here is a list...');
console.info(result);
return;
}
});
} else {
var sourceWebSpace = argv.sourceWebSpace;
var sourceSiteName = argv.sourceSiteName;
var targetWebSpace = argv.targetWebSpace;
var targetSiteName = argv.targetSiteName;
wsMgmtClient.webSites.getConfiguration(sourceWebSpace, sourceSiteName, function (err, resultSource) {
if (err) {
console.error(err);
} else {
console.info('App settings for ' + sourceSiteName + ':');
console.info(resultSource.appSettings);
console.log('');
console.info('Connection strings for ' + sourceSiteName + ':');
console.info(resultSource.connectionStrings);
console.log('');
console.log('');
wsMgmtClient.webSites.getConfiguration(targetWebSpace, targetSiteName, function (err, resultTarget) {
if (err) {
console.error(err);
} else {
var filteredAppSettings = filterAppSettings(resultSource.appSettings, ignoredAppSettings);
console.log('Updating ' + resultSource.connectionStrings.length + ' connection strings and ' + Object.keys(filteredAppSettings).length + '/' + Object.keys(resultSource.appSettings).length + ' app settings for ' + targetSiteName + '...');
wsMgmtClient.webSites.updateConfiguration(targetWebSpace, targetSiteName, { appSettings: filteredAppSettings, connectionStrings: resultSource.connectionStrings }, function (err, resultTarget) {
if (err) {
console.error(err);
} else {
console.info('Successfully updated app settings and connection strings for ' + targetSiteName + '!');
}
});
}
});
}
});
}
function filterAppSettings(appSettings, ignores) {
var newObj = {};
for (var key in appSettings) {
//copy the setting if not ignored
if (ignores.indexOf(key) == -1) {
newObj[key] = appSettings[key];
}
}
return newObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment