Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. module.exports = function(grunt) {
  2.  
  3. var fs = require('fs');
  4. var azureDeploy = require('azure-deploy');
  5.  
  6. var pathExists = function(path) {
  7. try {
  8. fs.accessSync(path, fs.F_OK);
  9. return true;
  10. } catch(e) {
  11. return false;
  12. }
  13. };
  14.  
  15. grunt.registerMultiTask('azureDeploy',
  16. "Deploys a directory to an Azure website.",
  17. function () {
  18.  
  19. var options = this.options({
  20. credential_file: '~/.azure_deploy.json'
  21. });
  22. var credFileExists = pathExists(options.credential_file);
  23.  
  24. grunt.log.writeln("Validating target data.");
  25. // 1. Make sure the user has the right directory
  26. if (typeof this.data.directory === 'undefined') {
  27. grunt.log.error("Directory isn't specified for this target!");
  28. return;
  29. }
  30. // test for directory existence
  31. if (!pathExists(this.data.directory)) {
  32. grunt.log.error("Directory " + this.data.directory + " isn't accessible.");
  33. return;
  34. }
  35. var targetDirectory = this.data.directory;
  36.  
  37. // 2. check for valid credentials
  38. var azureUsername = null;
  39. var azurePassword = null;
  40. if (credFileExists) {
  41. var creds = grunt.file.readJSON(options.credential_file)
  42. if (typeof creds.username !== 'undefined' &&
  43. typeof creds.password !== 'undefined') {
  44. azureUsername = creds.username
  45. azurePassword = creds.password
  46. } else {
  47. grunt.log.error("Couldn't get username and password from credential file.");
  48. return;
  49. }
  50. } else {
  51. grunt.log.error("I don't have a credential file. Please check that it exists.");
  52. return;
  53. }
  54.  
  55. // 3. Check for azure username
  56. if (typeof this.data.website_name === 'undefined') {
  57. grunt.log.error("Task target does not have a defined Azure website name. We need this!");
  58. return;
  59. } else {
  60. var azureWebsiteName = this.data.website_name;
  61. }
  62.  
  63. var deploymentManager = new azureDeploy.AzureWebSiteDeploymentManager(
  64. azureWebsiteName, azureUsername, azurePassword);
  65.  
  66. grunt.log.writeln("Starting Azure deployment...");
  67.  
  68. var done = this.async();
  69. deploymentManager.deploy(targetDirectory)
  70. .then(function() {
  71. grunt.log.ok("Deployment successful.");
  72. done();
  73. })
  74. .catch(function(e) {
  75. grunt.log.error('Deployment finished with errors.');
  76. grunt.log.writeln(e);
  77. done(false);
  78. });
  79. });
  80.  
  81.  
  82. // config
  83. grunt.initConfig({
  84. azureDeploy: {
  85. middleman: {
  86. options: {
  87. credential_file: './creds.json'
  88. },
  89. directory: './build',
  90. website_name: 'middleman-azure-cdn-test'
  91. }
  92. }
  93. });
  94.  
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement