@@ -141,20 +141,11 @@ const internalCertificate = {
141141 } ) ;
142142 } )
143143 . then ( ( in_use_result ) => {
144- // 3. Generate the LE config
145- return internalNginx . generateLetsEncryptRequestConfig ( certificate )
146- . then ( internalNginx . reload )
147- . then ( ( ) => {
144+ // Is CloudFlare, no config needed, so skip 3 and 5.
145+ if ( data . meta . cloudflare_use ) {
146+ return internalNginx . reload ( ) . then ( ( ) => {
148147 // 4. Request cert
149- if ( data . meta . cloudflare_use ) {
150- return internalCertificate . requestLetsEncryptCloudFlareDnsSsl ( certificate , data . meta . cloudflare_token ) ;
151- } else {
152- return internalCertificate . requestLetsEncryptSsl ( certificate ) ;
153- }
154- } )
155- . then ( ( ) => {
156- // 5. Remove LE config
157- return internalNginx . deleteLetsEncryptRequestConfig ( certificate ) ;
148+ return internalCertificate . requestLetsEncryptCloudFlareDnsSsl ( certificate , data . meta . cloudflare_token ) ;
158149 } )
159150 . then ( internalNginx . reload )
160151 . then ( ( ) => {
@@ -166,15 +157,44 @@ const internalCertificate = {
166157 } )
167158 . catch ( ( err ) => {
168159 // In the event of failure, revert things and throw err back
169- return internalNginx . deleteLetsEncryptRequestConfig ( certificate )
170- . then ( ( ) => {
171- return internalCertificate . enableInUseHosts ( in_use_result ) ;
172- } )
160+ return internalCertificate . enableInUseHosts ( in_use_result )
173161 . then ( internalNginx . reload )
174162 . then ( ( ) => {
175163 throw err ;
176164 } ) ;
177165 } ) ;
166+ } else {
167+ // 3. Generate the LE config
168+ return internalNginx . generateLetsEncryptRequestConfig ( certificate )
169+ . then ( internalNginx . reload )
170+ . then ( ( ) => {
171+ // 4. Request cert
172+ return internalCertificate . requestLetsEncryptSsl ( certificate ) ;
173+ } )
174+ . then ( ( ) => {
175+ // 5. Remove LE config
176+ return internalNginx . deleteLetsEncryptRequestConfig ( certificate ) ;
177+ } )
178+ . then ( internalNginx . reload )
179+ . then ( ( ) => {
180+ // 6. Re-instate previously disabled hosts
181+ return internalCertificate . enableInUseHosts ( in_use_result ) ;
182+ } )
183+ . then ( ( ) => {
184+ return certificate ;
185+ } )
186+ . catch ( ( err ) => {
187+ // In the event of failure, revert things and throw err back
188+ return internalNginx . deleteLetsEncryptRequestConfig ( certificate )
189+ . then ( ( ) => {
190+ return internalCertificate . enableInUseHosts ( in_use_result ) ;
191+ } )
192+ . then ( internalNginx . reload )
193+ . then ( ( ) => {
194+ throw err ;
195+ } ) ;
196+ } ) ;
197+ }
178198 } )
179199 . then ( ( ) => {
180200 // At this point, the letsencrypt cert should exist on disk.
@@ -763,26 +783,25 @@ const internalCertificate = {
763783 let tokenLoc = '~/cloudflare-token' ;
764784 let storeKey = 'echo "dns_cloudflare_api_token = ' + apiToken + '" > ' + tokenLoc ;
765785
766- let cmd = certbot_command + ' certonly --non-interactive ' +
786+ let cmd =
787+ storeKey + " && " +
788+ certbot_command + ' certonly --non-interactive ' +
767789 '--cert-name "npm-' + certificate . id + '" ' +
768790 '--agree-tos ' +
769791 '--email "' + certificate . meta . letsencrypt_email + '" ' +
770792 '--domains "' + certificate . domain_names . join ( ',' ) + '" ' +
771- '--dns-cloudflare --dns-cloudflare-credentials ' + tokenLoc + ' ' +
772- ( le_staging ? '--staging' : '' ) ;
793+ '--dns-cloudflare --dns-cloudflare-credentials ' + tokenLoc +
794+ ( le_staging ? ' --staging' : '' )
795+ + ' && rm ' + tokenLoc ;
773796
774797 if ( debug_mode ) {
775798 logger . info ( 'Command:' , cmd ) ;
776799 }
777800
778- return utils . exec ( storeKey ) . then ( ( result ) => {
779- utils . exec ( cmd ) . then ( ( result ) => {
780- utils . exec ( 'rm ' + tokenLoc ) . then ( result => {
781- logger . success ( result ) ;
782- return result ;
783- } ) ;
801+ return utils . exec ( cmd ) . then ( ( result ) => {
802+ logger . info ( result ) ;
803+ return result ;
784804 } ) ;
785- } ) ;
786805 } ,
787806
788807
0 commit comments