11/* eslint-disable max-len */
2+ const fs = require ( 'fs' ) ;
3+
24const Command = require ( '../../Command' ) ;
35const runnerRoot = require ( '../root/runner.cmd' ) ;
46const inquirer = require ( 'inquirer' ) ;
@@ -36,13 +38,14 @@ const {
3638 INSTALLATION_DEFAULTS ,
3739} = require ( './helper' ) ;
3840const InstallationPlan = require ( './InstallationPlan' ) ;
41+ const { produceVenonaKeys } = require ( './key-helper' ) ;
3942const { array } = require ( 'yargs' ) ;
4043
4144const defaultDockerRegistry = 'quay.io' ;
4245const handleError = createErrorHandler ( `\nIf you had any issues with the installation please report them at: ${ colors . blue ( 'https://github.com/codefresh-io/cli/issues/new' ) } ` ) ;
4346
4447async function isNewAccount ( ) {
45- const [ pipelines , err ] = await to ( sdk . pipelines . list ( { } ) ) ;
48+ const [ pipelines , err ] = await to ( sdk . pipelines . list ( { } ) ) ;
4649 if ( ! err && _ . isArray ( _ . get ( pipelines , 'docs' ) ) ) {
4750 return ! pipelines . docs . length ;
4851 }
@@ -61,6 +64,7 @@ function printInstallationOptionsSummary({
6164 appProxy,
6265 appProxyHost,
6366 dryRun,
67+ shouldUseHelm,
6468} ) {
6569 let summary = `\n${ colors . green ( 'Installation options summary:' ) }
6670 1. Kubernetes Context: ${ colors . cyan ( kubeContextName ) }
@@ -78,6 +82,9 @@ function printInstallationOptionsSummary({
7882 if ( dryRun ) {
7983 summary += '**** running in dry-run mode ****' ;
8084 }
85+ if ( shouldUseHelm ) {
86+ summary += '**** running in helm values generation mode ****' ;
87+ }
8188 console . log ( summary ) ;
8289}
8390
@@ -104,6 +111,10 @@ const initCmd = new Command({
104111 . option ( 'url' , {
105112 describe : 'Codefresh system custom url' ,
106113 } )
114+ . option ( 'generate-helm-values-file' , {
115+ describe : 'Path to the new generated helm values file' ,
116+ type : 'string' ,
117+ } )
107118 . option ( 'kube-context-name' , {
108119 describe : 'Name of the Kubernetes context on which runner should be installed [$CF_ARG_KUBE_CONTEXT_NAME]' ,
109120 } )
@@ -246,6 +257,7 @@ const initCmd = new Command({
246257 const {
247258 'kube-node-selector' : kubeNodeSelector ,
248259 'build-node-selector' : buildNodeSelector ,
260+ 'generate-helm-values-file' : helmValuesFile ,
249261 tolerations,
250262 'kube-config-path' : kubeConfigPath ,
251263 'storage-class-name' : storageClassName ,
@@ -274,6 +286,9 @@ const initCmd = new Command({
274286 'dry-run' : dryRun ,
275287 'bypass-download' : bypassDownload
276288 } = _argv ;
289+
290+ const shouldUseHelm = ! ! helmValuesFile ;
291+
277292 let {
278293 'kube-context-name' : kubeContextName ,
279294 'kube-namespace' : kubeNamespace ,
@@ -303,6 +318,10 @@ const initCmd = new Command({
303318 httpsProxy = httpsProxy || detectedProxyVars . httpsProxy ;
304319 noProxy = noProxy || detectedProxyVars . noProxy ;
305320
321+ if ( shouldUseHelm ) {
322+ shouldExecutePipeline = false ;
323+ }
324+
306325 if ( noQuestions ) {
307326 // use defaults
308327 kubeContextName = kubeContextName || getKubeContext ( kubeConfigPath ) ;
@@ -420,6 +439,7 @@ const initCmd = new Command({
420439 appProxy,
421440 appProxyHost,
422441 dryRun,
442+ shouldUseHelm,
423443 } ) ;
424444
425445 if ( token ) {
@@ -538,6 +558,7 @@ const initCmd = new Command({
538558 } ,
539559 installationEvent : installationProgress . events . AGENT_INSTALLED ,
540560 executeOnDryRun : true ,
561+ condition : ! shouldUseHelm ,
541562 } ) ;
542563
543564 // generate new runtime name
@@ -709,6 +730,7 @@ const initCmd = new Command({
709730 } ,
710731 installationEvent : installationProgress . events . RUNTIME_INSTALLED ,
711732 executeOnDryRun : true ,
733+ condition : ! shouldUseHelm ,
712734 } ) ;
713735
714736 installationPlan . addStep ( {
@@ -733,7 +755,7 @@ const initCmd = new Command({
733755 installationPlan . addContext ( 'appProxyIP' , `${ appProxyUrl } ` ) ;
734756 } ,
735757 installationEvent : installationProgress . events . APP_PROXY_INSTALLED ,
736- condition : ! ! appProxy ,
758+ condition : ! ! appProxy && ! shouldUseHelm ,
737759 executeOnDryRun : true ,
738760 } ) ;
739761
@@ -751,7 +773,13 @@ const initCmd = new Command({
751773 await sdk . runtimeEnvs . update ( { name : reName } , _ . merge ( re , body ) ) ;
752774 console . log ( `Runtime environment "${ colors . cyan ( reName ) } " has been updated with the app proxy` ) ;
753775 } ,
754- condition : async ( ) => installationPlan . getContext ( 'appProxyIP' ) ,
776+ condition : async ( ) => {
777+ if ( shouldUseHelm ) {
778+ return false ;
779+ }
780+
781+ return installationPlan . getContext ( 'appProxyIP' ) ;
782+ } ,
755783 } ) ;
756784
757785 // update agent with new runtime
@@ -802,6 +830,7 @@ const initCmd = new Command({
802830 } ,
803831 installationEvent : installationProgress . events . RUNNER_INSTALLED ,
804832 executeOnDryRun : true ,
833+ condition : ! shouldUseHelm ,
805834 } ) ;
806835
807836 // install monitoring
@@ -829,6 +858,10 @@ const initCmd = new Command({
829858 installationEvent : installationProgress . events . MONITOR_INSTALLED ,
830859 executeOnDryRun : true ,
831860 condition : async ( ) => {
861+ if ( shouldUseHelm ) {
862+ return false ;
863+ }
864+
832865 if ( ! installMonitor ) {
833866 return false ;
834867 }
@@ -842,10 +875,47 @@ const initCmd = new Command({
842875 } ,
843876 } ) ;
844877
878+ // helm value files if its enabled
879+ installationPlan . addStep ( {
880+ name : 'generate helm value files' ,
881+ func : async ( ) => {
882+ const runtimeNameContext = installationPlan . getContext ( 'runtimeName' ) ;
883+ const agent = installationPlan . getContext ( 'agent' ) ;
884+
885+ const keys = await produceVenonaKeys (
886+ _ . get ( sdk , 'config.context.token' ) ,
887+ kubeNamespace ,
888+ ) ;
889+
890+ const global = {
891+ namespace : kubeNamespace ,
892+ codefreshHost : sdk . config . context . url ,
893+ agentToken : agent . token ,
894+ agentId : agent . id ,
895+ agentName : agent . name ,
896+ accountId : agent . account ,
897+ runtimeName : runtimeNameContext ,
898+ keys,
899+ } ;
900+
901+ const content = JSON . stringify ( { global } , null , 4 ) ;
902+
903+ fs . writeFileSync (
904+ helmValuesFile ,
905+ content ,
906+ {
907+ encoding : 'utf8' ,
908+ } ,
909+ ) ;
910+ } ,
911+ condition : shouldUseHelm ,
912+ } ) ;
913+
845914 // Post Installation
846915 if ( shouldExecutePipeline ) {
847916 const pipelines = await sdk . pipelines . list ( { id : `${ INSTALLATION_DEFAULTS . PROJECT_NAME } /${ INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } ` } ) ;
848917 const testPipelineExists = ! ! _ . get ( pipelines , 'docs.length' ) ;
918+
849919 if ( ! testPipelineExists ) {
850920 installationPlan . addStep ( {
851921 name : 'create test pipeline' ,
@@ -876,6 +946,7 @@ const initCmd = new Command({
876946 exitOnError : false ,
877947 } ) ;
878948 }
949+
879950 installationPlan . addStep ( {
880951 name : 'execute test pipeline' ,
881952 func : async ( ) => {
@@ -891,9 +962,12 @@ const initCmd = new Command({
891962
892963 await installationPlan . execute ( ) ;
893964
894- console . log ( colors . green ( '\nRunner Status:' ) ) ;
895- await getAgents . handler ( { } ) ;
896- console . log ( '' ) ;
965+ if ( ! shouldUseHelm ) {
966+ console . log ( colors . green ( '\nRunner Status:' ) ) ;
967+ await getAgents . handler ( { } ) ;
968+ console . log ( '' ) ;
969+ }
970+
897971 if ( installMonitor ) {
898972 console . log ( `Go to ${ colors . blue ( 'https://g.codefresh.io/kubernetes/services/' ) } to view your cluster in Codefresh dashbaord` ) ;
899973 }
0 commit comments