Guest User

Untitled

a guest
Nov 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. "use strict";
  2.  
  3. const util = require('util');
  4. const AWS = require("aws-sdk");
  5. /**
  6. * Deploy env
  7. * @param event
  8. * @param context
  9. * @param callback
  10. * Example of params
  11. {
  12. "region": "eu-central-1",
  13. "applicationName": "DealsClickoutLinkManager",
  14. "environmentName": "deals-clickout-manager-prod",
  15. "versionRegex": "deals_clickout_link_manager_prod"
  16. }
  17. */
  18. module.exports.deploy = (event, context, callback) => {
  19.  
  20. console.log("EVENT", inspect(event));
  21. console.log("CONTEXT", inspect(context));
  22.  
  23. let pipeLineRegion = process.env.AWS_REGION;
  24. let PIPE_LINE = new AWS.CodePipeline({
  25. region: pipeLineRegion
  26. });
  27. let job = event["CodePipeline.job"];
  28. let config = job.data.actionConfiguration.configuration;
  29. let params;
  30.  
  31. try {
  32. params = JSON.parse(config.UserParameters);
  33. } catch (e) {
  34. let parsingError = new Error("Error parsing json");
  35. return wrap(
  36. PIPE_LINE.putJobFailureResult.bind(PIPE_LINE),
  37. {
  38. jobId: job.id,
  39. failureDetails: {
  40. type: "JobFailed",
  41. message: parsingError.toString()
  42. }
  43. }
  44. ).then(data => {
  45. console.log("NOTIFY-PIPE-LINE-ERROR-RESULT", inspect(data));
  46. return callback(parsingError);
  47. });
  48. }
  49.  
  50. console.log("PIPELINE-REGION", pipeLineRegion);
  51.  
  52. let ApplicationName = params.applicationName;
  53. let EnvironmentName = params.environmentName;
  54. let PROD_ENV = new RegExp(params.versionRegex);
  55. let EBS = new AWS.ElasticBeanstalk({
  56. region: params.region
  57. });
  58.  
  59. console.log("PARAMS", params, params.region);
  60.  
  61. wrap(
  62. EBS.describeApplicationVersions.bind(EBS),
  63. {
  64. ApplicationName: ApplicationName
  65. }
  66. )
  67. .then(data => {
  68. console.log("VERSIONS", inspect(data));
  69. let filtered = data.ApplicationVersions.filter(item => PROD_ENV.test(item.VersionLabel));
  70. let item = filtered.shift();
  71. console.log("VERSION", inspect(item));
  72. if (item && item.Status && item.Status.toLocaleLowerCase() != "processed") {
  73. return item;
  74. } else if (!!item) {
  75. throw new Error("Version is already deployed: " + item.VersionLabel);
  76. }
  77. throw new Error("No version item found");
  78. })
  79. .then(version => {
  80. return wrap(
  81. EBS.describeEnvironments.bind(EBS),
  82. {
  83. ApplicationName: ApplicationName
  84. }
  85. )
  86. .then(envs => {
  87. console.log("Environments", inspect(envs));
  88. let item = envs.Environments.find(item => item.EnvironmentName === EnvironmentName);
  89. if (!item) {
  90. throw new Error("EnvironmentName " + EnvironmentName + " is not present in " + ApplicationName);
  91. } else if (item.VersionLabel === version.VersionLabel) {
  92. throw new Error("Application version " + version.VersionLabel + " already deployed");
  93. }
  94. return version;
  95. });
  96. })
  97. .then(version => {
  98. let data = {
  99. ApplicationName: ApplicationName,
  100. EnvironmentName: EnvironmentName,
  101. VersionLabel: version.VersionLabel
  102. };
  103. console.log("UPDATE-ENV", inspect(data));
  104. return wrap(EBS.updateEnvironment.bind(EBS), data);
  105. })
  106. .then(result => {
  107. console.log("DEPLOYING", inspect(result));
  108. if (job.id !== null) {
  109. console.log("NOTIFY-PIPE-LINE", inspect({
  110. jobId: job.id
  111. }));
  112. return wrap(
  113. PIPE_LINE.putJobSuccessResult.bind(PIPE_LINE),
  114. {
  115. jobId: job.id
  116. }
  117. ).then(data => {
  118. console.log("NOTIFY-PIPE-LINE-RESULT", inspect(data));
  119. return callback(null, "Started deployment of " + result.VersionLabel + " and putJobSuccessResult");
  120. });
  121. } else {
  122. return callback(null, "Started deployment of " + result.VersionLabel);
  123. }
  124. })
  125. .catch(error => {
  126. console.log("ERROR", error);
  127. if (job.id !== null) {
  128. console.log("NOTIFY-PIPE-LINE-ERROR", inspect({
  129. jobId: job.id,
  130. failureDetails: {
  131. type: "JobFailed",
  132. message: error.toString()
  133. }
  134. }));
  135. return wrap(
  136. PIPE_LINE.putJobFailureResult.bind(PIPE_LINE),
  137. {
  138. jobId: job.id,
  139. failureDetails: {
  140. type: "JobFailed",
  141. message: error.toString()
  142. }
  143. }
  144. ).then(data => {
  145. console.log("NOTIFY-PIPE-LINE-ERROR-RESULT", inspect(data));
  146. return callback(error);
  147. });
  148. }
  149. return callback(error);
  150. })
  151. .catch(error => callback(error));
  152. };
  153. /**
  154. * Inspect
  155. * @param data
  156. * @returns {*}
  157. */
  158. function inspect(data) {
  159. return util.inspect(data, {colors: false, depth: 10});
  160. }
  161. /**
  162. * Wrap functions to promise
  163. * @param execute
  164. * @param params
  165. * @returns {Promise}
  166. */
  167. function wrap(execute, params) {
  168. return new Promise(function (resolve, reject) {
  169. execute(params, (error, data) => {
  170. if (error) {
  171. reject(error);
  172. } else {
  173. resolve(data);
  174. }
  175. })
  176. })
  177. }
Add Comment
Please, Sign In to add comment