@@ -23,14 +23,52 @@ type terraformCmd struct {
2323 retries int
2424}
2525
26+ var (
27+ checkpoints = []string {
28+ "init" ,
29+ "import" ,
30+ "apply:import" ,
31+ "commit" ,
32+ "apps" ,
33+ "prune:cloud" ,
34+ "prune:mgmt" ,
35+ }
36+
37+ priorities = map [string ]int {}
38+ )
39+
40+ func init () {
41+ for i , c := range checkpoints {
42+ priorities [c ] = i
43+ }
44+ }
45+
46+ func (ctx * Context ) runCheckpoint (current , checkpoint string , fn func () error ) error {
47+ if current == "" || priorities [checkpoint ] > priorities [current ] {
48+ err := fn ()
49+ if err == nil {
50+ ctx .Manifest .Checkpoint = checkpoint
51+ }
52+ return err
53+ }
54+
55+ utils .Highlight ("Skipping checkpoint %s, ran up to %s previously\n " , checkpoint , current )
56+
57+ return nil
58+ }
59+
2660func (ctx * Context ) Deploy (commit func () error ) error {
2761 if err := ctx .Provider .CreateBucket (); err != nil {
2862 return err
2963 }
3064
31- if err := runAll ([]terraformCmd {
32- {dir : "./terraform/mgmt" , cmd : "init" , args : []string {"-upgrade" }},
33- {dir : "./terraform/mgmt" , cmd : "apply" , args : []string {"-auto-approve" }, retries : 1 },
65+ defer ctx .Manifest .Flush ()
66+
67+ if err := ctx .runCheckpoint (ctx .Manifest .Checkpoint , "init" , func () error {
68+ return runAll ([]terraformCmd {
69+ {dir : "./terraform/mgmt" , cmd : "init" , args : []string {"-upgrade" }},
70+ {dir : "./terraform/mgmt" , cmd : "apply" , args : []string {"-auto-approve" }, retries : 1 },
71+ })
3472 }); err != nil {
3573 return err
3674 }
@@ -41,10 +79,19 @@ func (ctx *Context) Deploy(commit func() error) error {
4179 return err
4280 }
4381
44- if err := runAll ([]terraformCmd {
45- {dir : "./terraform/mgmt" , cmd : "init" , args : []string {"-upgrade" }},
46- {dir : "./terraform/mgmt" , cmd : "import" , args : []string {"plural_cluster.mgmt" , * ctx .ImportCluster }},
47- {dir : "./terraform/mgmt" , cmd : "apply" , args : []string {"-auto-approve" }, retries : 1 },
82+ if err := ctx .runCheckpoint (ctx .Manifest .Checkpoint , "import" , func () error {
83+ return runAll ([]terraformCmd {
84+ {dir : "./terraform/mgmt" , cmd : "init" , args : []string {"-upgrade" }},
85+ {dir : "./terraform/mgmt" , cmd : "import" , args : []string {"plural_cluster.mgmt" , * ctx .ImportCluster }},
86+ })
87+ }); err != nil {
88+ return err
89+ }
90+
91+ if err := ctx .runCheckpoint (ctx .Manifest .Checkpoint , "apply:import" , func () error {
92+ return runAll ([]terraformCmd {
93+ {dir : "./terraform/mgmt" , cmd : "apply" , args : []string {"-auto-approve" }, retries : 1 },
94+ })
4895 }); err != nil {
4996 return err
5097 }
@@ -73,16 +120,19 @@ func (ctx *Context) Deploy(commit func() error) error {
73120 }
74121 }
75122
76- if err := commit (); err != nil {
123+ if err := ctx .runCheckpoint (ctx .Manifest .Checkpoint , "commit" , func () error {
124+ utils .Highlight ("\n Setting up gitops management, first lets commit the changes made up to this point...\n \n " )
125+ return commit ()
126+ }); err != nil {
77127 return err
78128 }
79129
80- utils . Highlight ( " \n Setting up gitops management... \n " )
81-
82- if err := runAll ([] terraformCmd {
83- {dir : "./terraform/mgmt " , cmd : "apply " , args : []string {"-auto-approve " }},
84- {dir : "./terraform/apps" , cmd : "init " , args : []string {"-upgrade" } },
85- { dir : "./terraform/apps" , cmd : "apply" , args : [] string { "-auto-approve" }, retries : 1 },
130+ if err := ctx . runCheckpoint ( ctx . Manifest . Checkpoint , "apps" , func () error {
131+ return runAll ([] terraformCmd {
132+ { dir : "./terraform/mgmt" , cmd : "apply" , args : [] string { "-auto-approve" }},
133+ {dir : "./terraform/apps " , cmd : "init " , args : []string {"-upgrade " }},
134+ {dir : "./terraform/apps" , cmd : "apply " , args : []string {"-auto-approve" }, retries : 1 },
135+ })
86136 }); err != nil {
87137 return err
88138 }
0 commit comments