@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"fmt"
20
20
"os"
21
+ "sync"
21
22
"time"
22
23
23
24
"github.com/codefresh-io/cli-v2/pkg/log"
@@ -26,6 +27,7 @@ import (
26
27
"github.com/codefresh-io/cli-v2/pkg/util"
27
28
cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
28
29
eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
30
+ "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
29
31
30
32
"github.com/Masterminds/semver/v3"
31
33
appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
@@ -189,28 +191,35 @@ func NewRuntimeInstallCommand() *cobra.Command {
189
191
}
190
192
191
193
func RunRuntimeInstall (ctx context.Context , opts * RuntimeInstallOptions ) error {
192
- rt , err := runtime . Download ( opts . Version , opts . RuntimeName )
194
+ runtimes , err := cfConfig . NewClient (). V2 (). Runtime (). List ( ctx )
193
195
if err != nil {
194
- return fmt . Errorf ( "failed to download runtime definition: %w" , err )
196
+ return err
195
197
}
196
198
197
- server , err := util .CurrentServer ()
198
- if err != nil {
199
- return fmt .Errorf ("failed to get current server address: %w" , err )
199
+ for _ , rt := range runtimes {
200
+ if rt .Metadata .Name == opts .RuntimeName {
201
+ return fmt .Errorf ("failed to create runtime: %s. A runtime by this name already exists" , opts .RuntimeName )
202
+ }
200
203
}
201
204
202
- runtimeVersion := "v99.99.99"
203
- if rt . Spec . Version != nil { // in dev mode
204
- runtimeVersion = rt . Spec . Version . String ( )
205
+ rt , err := runtime . Download ( opts . Version , opts . RuntimeName )
206
+ if err != nil {
207
+ return fmt . Errorf ( "failed to download runtime definition: %w" , err )
205
208
}
206
209
207
- runtimeCreationResponse , err := cfConfig .NewClient ().V2 ().Runtime ().Create (ctx , opts .RuntimeName , server , runtimeVersion )
210
+ runtimeCreationResponse , err := cfConfig .NewClient ().V2 ().Runtime ().Create (ctx , opts .RuntimeName )
208
211
if err != nil {
209
212
return fmt .Errorf ("failed to create a new runtime: %w" , err )
210
213
}
211
214
212
215
opts .RuntimeToken = runtimeCreationResponse .NewAccessToken
213
216
217
+ server , err := util .CurrentServer ()
218
+ if err != nil {
219
+ return fmt .Errorf ("failed to get current server address: %w" , err )
220
+ }
221
+ rt .Spec .Cluster = server
222
+
214
223
log .G (ctx ).WithField ("version" , rt .Spec .Version ).Infof ("installing runtime '%s'" , opts .RuntimeName )
215
224
err = apcmd .RunRepoBootstrap (ctx , & apcmd.RepoBootstrapOptions {
216
225
AppSpecifier : rt .Spec .FullSpecifier (),
@@ -250,7 +259,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
250
259
}
251
260
}
252
261
253
- if err = createEventsReporter (ctx , opts .insCloneOpts , opts ); err != nil {
262
+ if err = createEventsReporter (ctx , opts .insCloneOpts , opts , rt ); err != nil {
254
263
return fmt .Errorf ("failed to create events-reporter: %w" , err )
255
264
}
256
265
@@ -271,10 +280,43 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
271
280
return fmt .Errorf ("failed to create `%s`: %w" , store .Get ().GitSourceName , err )
272
281
}
273
282
283
+ var wg sync.WaitGroup
284
+
285
+ wg .Add (1 )
286
+ go intervalCheckIsRuntimePersisted (15000 , ctx , opts .RuntimeName , & wg )
287
+ wg .Wait ()
288
+
274
289
log .G (ctx ).Infof ("done installing runtime '%s'" , opts .RuntimeName )
275
290
return nil
276
291
}
277
292
293
+ func intervalCheckIsRuntimePersisted (milliseconds int , ctx context.Context , runtimeName string , wg * sync.WaitGroup ) {
294
+ interval := time .Duration (milliseconds ) * time .Millisecond
295
+ ticker := time .NewTicker (interval )
296
+ var err error
297
+
298
+ for retries := 20 ; retries > 0 ; <- ticker .C {
299
+ fmt .Println ("waiting for the runtime installation to complete..." )
300
+ var runtimes []model.Runtime
301
+ runtimes , err = cfConfig .NewClient ().V2 ().Runtime ().List (ctx )
302
+ if err != nil {
303
+ continue
304
+ }
305
+
306
+ for _ , rt := range runtimes {
307
+ if rt .Metadata .Name == runtimeName {
308
+ wg .Done ()
309
+ ticker .Stop ()
310
+ return
311
+ }
312
+ }
313
+
314
+ retries --
315
+ }
316
+
317
+ panic (fmt .Errorf ("failed to complete the runtime installation due to error: %w" , err ))
318
+ }
319
+
278
320
func NewRuntimeListCommand () * cobra.Command {
279
321
cmd := & cobra.Command {
280
322
Use : "list [runtime_name]" ,
@@ -534,7 +576,7 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
534
576
return err
535
577
}
536
578
537
- func createEventsReporter (ctx context.Context , cloneOpts * git.CloneOptions , opts * RuntimeInstallOptions ) error {
579
+ func createEventsReporter (ctx context.Context , cloneOpts * git.CloneOptions , opts * RuntimeInstallOptions , rt * runtime. Runtime ) error {
538
580
runtimeTokenSecret , err := getRuntimeTokenSecret (opts .RuntimeName , opts .RuntimeToken )
539
581
if err != nil {
540
582
return fmt .Errorf ("failed to create codefresh token secret: %w" , err )
@@ -564,7 +606,7 @@ func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts
564
606
return err
565
607
}
566
608
567
- if err := updateProject (repofs , opts .RuntimeName ); err != nil {
609
+ if err := updateProject (repofs , opts .RuntimeName , rt ); err != nil {
568
610
return err
569
611
}
570
612
@@ -616,7 +658,7 @@ func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, op
616
658
return err
617
659
}
618
660
619
- func updateProject (repofs fs.FS , runtimeName string ) error {
661
+ func updateProject (repofs fs.FS , runtimeName string , rt * runtime. Runtime ) error {
620
662
projPath := repofs .Join (apstore .Default .ProjectsDir , runtimeName + ".yaml" )
621
663
project , appset , err := getProjectInfoFromFile (repofs , projPath )
622
664
if err != nil {
@@ -627,7 +669,14 @@ func updateProject(repofs fs.FS, runtimeName string) error {
627
669
project .ObjectMeta .Labels = make (map [string ]string )
628
670
}
629
671
672
+ runtimeVersion := "v99.99.99"
673
+ if rt .Spec .Version != nil { // in dev mode
674
+ runtimeVersion = rt .Spec .Version .String ()
675
+ }
676
+
630
677
project .ObjectMeta .Labels [store .Get ().LabelKeyCFType ] = store .Get ().CFRuntimeType
678
+ project .ObjectMeta .Labels [store .Get ().LabelKeyRuntimeVersion ] = runtimeVersion
679
+
631
680
return repofs .WriteYamls (projPath , project , appset )
632
681
}
633
682
0 commit comments