1- /* eslint-disable no-console */
21import { ElasticBeanstalkClient } from '@aws-sdk/client-elastic-beanstalk' ;
32import chalk from 'chalk' ;
4- import log from 'loglevel' ;
3+ import loglevel from 'loglevel' ;
54import { create } from './helpers/create-app-version' ;
65import { deploy } from './helpers/deploy-app-version-to-env' ;
76import { DBError , DBGroupDeployTriggerError , DBHealthinessCheckError } from './helpers/Errors' ;
87import { waitForGroupHealthiness } from './helpers/healthiness' ;
9- import { IDeployToGroupProps , IHealthCheckProps } from './helpers/Interfaces' ;
8+ import { IDeployToGroupProps , IHealthCheckProps , Logger } from './helpers/Interfaces' ;
109
1110const AWS_CLIENT_REQUEST_MAX_ATTEMPTS_DEFAULT = 10 ;
1211const DEFAULT_FORCE = false ;
@@ -34,7 +33,7 @@ function verifyPromisesSettled(results: PromiseSettledResult<void>[]) {
3433 * Application. For each of those unique Applications, we must create an App
3534 * Version to use for deployments.
3635 */
37- async function createAppVersionsForGroup ( client : ElasticBeanstalkClient , props : IDeployToGroupProps ) {
36+ async function createAppVersionsForGroup ( client : ElasticBeanstalkClient , props : IDeployToGroupProps , log : Logger ) {
3837 const appsWithCreatedVersions : string [ ] = [ ] ;
3938 const appVersionPromises : Promise < void > [ ] = [ ] ;
4039 log . info ( `Creating application versions for beanstalk group ${ props . group . name } ` ) ;
@@ -46,6 +45,7 @@ async function createAppVersionsForGroup(client: ElasticBeanstalkClient, props:
4645 version : props . group . versionProps ,
4746 appName : env . app ,
4847 dryRun : ! props . force ,
48+ log,
4949 } ) ,
5050 ) ;
5151 appsWithCreatedVersions . push ( env . app ) ;
@@ -56,14 +56,15 @@ async function createAppVersionsForGroup(client: ElasticBeanstalkClient, props:
5656 log . info ( chalk . green ( 'All needed application versions exist.' ) ) ;
5757}
5858
59- async function preDeployHealthcheck ( client : ElasticBeanstalkClient , props : IDeployToGroupProps ) {
59+ async function preDeployHealthcheck ( client : ElasticBeanstalkClient , props : IDeployToGroupProps , log : Logger ) {
6060 try {
6161 log . info ( chalk . blue ( 'Verifying environments are ready to receive deployment before initiating...' ) ) ;
6262 await waitForGroupHealthiness ( {
6363 client,
6464 group : props . group ,
6565 force : props . force ?? DEFAULT_FORCE ,
6666 checkVersion : false ,
67+ log,
6768 ...( props . preDeployHealthCheckProps ?? DEFAULT_HEALTH_CHECK_PROPS ) ,
6869 } ) ;
6970 } catch ( e ) {
@@ -74,14 +75,15 @@ async function preDeployHealthcheck(client: ElasticBeanstalkClient, props: IDepl
7475 }
7576}
7677
77- async function postDeployHealthcheck ( client : ElasticBeanstalkClient , props : IDeployToGroupProps ) {
78+ async function postDeployHealthcheck ( client : ElasticBeanstalkClient , props : IDeployToGroupProps , log : Logger ) {
7879 log . info ( chalk . blue ( 'Verifying environments successfully receive the deployment...this could take a while.' ) ) ;
7980 try {
8081 await waitForGroupHealthiness ( {
8182 client,
8283 group : props . group ,
8384 force : props . force ?? DEFAULT_FORCE ,
8485 checkVersion : true ,
86+ log,
8587 ...( props . postDeployHealthCheckProps ?? DEFAULT_HEALTH_CHECK_PROPS ) ,
8688 } ) ;
8789 } catch ( e ) {
@@ -103,7 +105,7 @@ async function postDeployHealthcheck(client: ElasticBeanstalkClient, props: IDep
103105 * For each Beanstalk Environment in the group, deploys the respective
104106 * Application Version.
105107 */
106- async function deployAppVersionsToGroup ( client : ElasticBeanstalkClient , props : IDeployToGroupProps ) {
108+ async function deployAppVersionsToGroup ( client : ElasticBeanstalkClient , props : IDeployToGroupProps , log : Logger ) {
107109 log . info ( `Asynchronously kicking off deployment to the ${ props . group . name } group of beanstalks.` ) ;
108110 const triggerErr = new DBGroupDeployTriggerError ( 'deployAppVersionsToGroup: ' , [ ] ) ;
109111 const force = props . force ?? DEFAULT_FORCE ;
@@ -114,6 +116,7 @@ async function deployAppVersionsToGroup(client: ElasticBeanstalkClient, props: I
114116 client,
115117 force,
116118 env,
119+ log,
117120 version : props . group . versionProps ,
118121 } ) ;
119122 } catch ( e ) {
@@ -126,19 +129,33 @@ async function deployAppVersionsToGroup(client: ElasticBeanstalkClient, props: I
126129 if ( triggerErr . errors . length !== 0 ) throw triggerErr ;
127130}
128131
132+ /**
133+ * Get AWS Credentials if provided
134+ */
135+ function getCredentials ( props : IDeployToGroupProps ) {
136+ if ( props . accessKeyId && props . secretAccessKey ) {
137+ return {
138+ accessKeyId : props . accessKeyId ,
139+ secretAccessKey : props . secretAccessKey ,
140+ } ;
141+ }
142+ return ;
143+ }
129144/**
130145 * Initializes the Beanstalk Client, creates the needed Application Versions,
131146 * and verifies the Beanstalk Environments in the group are ready to receive
132147 * the deployment.
133148 */
134- async function preDeploySteps ( props : IDeployToGroupProps ) : Promise < ElasticBeanstalkClient > {
149+ async function preDeploySteps ( props : IDeployToGroupProps , log : Logger ) : Promise < ElasticBeanstalkClient > {
135150 try {
151+ const credentials = getCredentials ( props ) ;
136152 const client = new ElasticBeanstalkClient ( {
137153 maxAttempts : AWS_CLIENT_REQUEST_MAX_ATTEMPTS_DEFAULT ,
138154 region : props . group . region ,
155+ credentials,
139156 } ) ;
140- await createAppVersionsForGroup ( client , props ) ;
141- await preDeployHealthcheck ( client , props ) ;
157+ await createAppVersionsForGroup ( client , props , log ) ;
158+ await preDeployHealthcheck ( client , props , log ) ;
142159 return client ;
143160 } catch ( e ) {
144161 log . error ( chalk . red ( e ) ) ;
@@ -155,17 +172,17 @@ async function preDeploySteps(props: IDeployToGroupProps): Promise<ElasticBeanst
155172 * Beanstalk Environment in the group, then verifies they reach a healthy state
156173 * and successfully land the expected version.
157174 */
158- async function deploySteps ( client : ElasticBeanstalkClient , props : IDeployToGroupProps ) {
175+ async function deploySteps ( client : ElasticBeanstalkClient , props : IDeployToGroupProps , log : Logger ) {
159176 let deployErrs : DBError = new DBError ( 'deploySteps(): ' , [ ] ) ;
160177 try {
161- await deployAppVersionsToGroup ( client , props ) ;
178+ await deployAppVersionsToGroup ( client , props , log ) ;
162179 } catch ( e ) {
163180 // We still want to see status of Beanstalks who did have deploy triggered
164181 deployErrs . errors . push ( e as Error ) ;
165182 }
166183
167184 try {
168- await postDeployHealthcheck ( client , props ) ;
185+ await postDeployHealthcheck ( client , props , log ) ;
169186 } catch ( e ) {
170187 log . error ( chalk . red ( e ) ) ;
171188 if ( e instanceof DBError ) {
@@ -183,10 +200,10 @@ async function deploySteps(client: ElasticBeanstalkClient, props: IDeployToGroup
183200 * Versions for their respective Beanstalk Applications, and then deploys
184201 * those versions to the Beanstalk Environments and verifies their health.
185202 */
186- export async function deployToGroup ( props : IDeployToGroupProps ) {
203+ export async function deployToGroup ( props : IDeployToGroupProps , log : Logger = loglevel ) {
187204 const group = props . group ;
188- log . setLevel ( props . logLevel ?? log . levels . INFO ) ;
205+ log . setLevel ( props . logLevel ?? 'info' ) ;
189206 log . info ( chalk . green ( 'Beginning deploy process for beanstalk group ' ) + chalk . blue ( group . name ) ) ;
190207
191- await deploySteps ( await preDeploySteps ( props ) , props ) ;
208+ await deploySteps ( await preDeploySteps ( props , log ) , props , log ) ;
192209}
0 commit comments