44 "context"
55 "github.com/multycloud/multy/api/errors"
66 "github.com/multycloud/multy/api/util"
7+ "github.com/multycloud/multy/db"
78 "github.com/multycloud/multy/flags"
89 "github.com/multycloud/multy/resources"
910 "github.com/multycloud/multy/resources/output"
@@ -40,14 +41,14 @@ func NewDeploymentExecutor() DeploymentExecutor {
4041 return DeploymentExecutor {TfCmd : terraformCmd {}}
4142}
4243
43- func (d DeploymentExecutor ) Deploy (ctx context.Context , c * resources.MultyConfig , prev resources.Resource , curr resources.Resource ) (state * output. TfState , rollbackFn func (), err error ) {
44- tmpDir := GetTempDirForUser (false , c .GetUserId ())
45- encoded , err := d .EncodeAndStoreTfFile (ctx , c , prev , curr , false )
44+ func (d DeploymentExecutor ) Deploy (ctx context.Context , c * resources.MultyConfig , prev resources.Resource , curr resources.Resource ) (rollbackFn func (), err error ) {
45+ tmpDir := GetTempDirForUser (c .GetUserId ())
46+ encoded , err := d .EncodeAndStoreTfFile (ctx , c , prev , curr )
4647 if err != nil {
4748 return
4849 }
4950
50- err = d .MaybeInit (ctx , c .GetUserId (), false )
51+ err = d .MaybeInit (ctx , c .GetUserId ())
5152 if err != nil {
5253 return
5354 }
@@ -67,7 +68,7 @@ func (d DeploymentExecutor) Deploy(ctx context.Context, c *resources.MultyConfig
6768 log .Printf ("[ERROR] Rollback unsuccessful: %s\n " , err2 )
6869 return
6970 }
70- _ , err2 = d .EncodeAndStoreTfFile (ctx , originalC , curr , prev , false )
71+ _ , err2 = d .EncodeAndStoreTfFile (ctx , originalC , curr , prev )
7172 if err2 != nil {
7273 log .Printf ("[ERROR] Rollback unsuccessful: %s\n " , err2 )
7374 return
@@ -92,15 +93,10 @@ func (d DeploymentExecutor) Deploy(ctx context.Context, c *resources.MultyConfig
9293 return
9394 }
9495
95- state , err = d .GetState (ctx , c .GetUserId (), false )
96- if err != nil {
97- return state , rollbackFn , errors .InternalServerErrorWithMessage ("error parsing state" , err )
98- }
99-
10096 return
10197}
10298
103- func (d DeploymentExecutor ) EncodeAndStoreTfFile (ctx context.Context , c * resources.MultyConfig , prev resources.Resource , curr resources.Resource , readonly bool ) (EncodedResources , error ) {
99+ func (d DeploymentExecutor ) EncodeAndStoreTfFile (ctx context.Context , c * resources.MultyConfig , prev resources.Resource , curr resources.Resource ) (EncodedResources , error ) {
104100 credentials , err := util .ExtractCloudCredentials (ctx )
105101 if err != nil {
106102 return EncodedResources {}, err
@@ -118,7 +114,7 @@ func (d DeploymentExecutor) EncodeAndStoreTfFile(ctx context.Context, c *resourc
118114 // TODO: move this to a proper place
119115 hclOutput := tfBlock + encoded .HclString
120116
121- tmpDir := GetTempDirForUser (readonly , c .GetUserId ())
117+ tmpDir := GetTempDirForUser (c .GetUserId ())
122118 err = os .MkdirAll (tmpDir , os .ModeDir | (os .ModePerm & 0775 ))
123119 if err != nil {
124120 return EncodedResources {}, err
@@ -127,8 +123,8 @@ func (d DeploymentExecutor) EncodeAndStoreTfFile(ctx context.Context, c *resourc
127123 return encoded , err
128124}
129125
130- func (d DeploymentExecutor ) MaybeInit (ctx context.Context , userId string , readonly bool ) error {
131- tmpDir := GetTempDirForUser (readonly , userId )
126+ func (d DeploymentExecutor ) MaybeInit (ctx context.Context , userId string ) error {
127+ tmpDir := GetTempDirForUser (userId )
132128 _ , err := os .Stat (filepath .Join (tmpDir , tfDir ))
133129 if os .IsNotExist (err ) {
134130 start := time .Now ()
@@ -151,22 +147,26 @@ func (d DeploymentExecutor) MaybeInit(ctx context.Context, userId string, readon
151147 return nil
152148}
153149
154- func (d DeploymentExecutor ) GetState (ctx context.Context , userId string , readonly bool ) (* output.TfState , error ) {
155- tmpDir := GetTempDirForUser (readonly , userId )
156- return d .TfCmd .GetState (ctx , tmpDir )
150+ func (d DeploymentExecutor ) GetState (ctx context.Context , userId string , client db.TfStateReader ) (* output.TfState , error ) {
151+ return d .TfCmd .GetState (ctx , userId , client )
157152}
158153
159154func (d DeploymentExecutor ) RefreshState (ctx context.Context , userId string , c * resources.MultyConfig ) error {
160- _ , err := d .EncodeAndStoreTfFile (ctx , c , nil , nil , true )
155+ _ , err := d .EncodeAndStoreTfFile (ctx , c , nil , nil )
161156 if err != nil {
162157 return err
163158 }
164159
165- err = d .MaybeInit (ctx , userId , true )
160+ err = d .MaybeInit (ctx , userId )
166161 if err != nil {
167162 return err
168163 }
169164
165+ start := time .Now ()
166+ defer func () {
167+ log .Printf ("[DEBUG] refresh finished in %s" , time .Since (start ))
168+ }()
169+
170170 return d .refresh (ctx , userId )
171171}
172172
@@ -176,20 +176,16 @@ func (d DeploymentExecutor) refresh(ctx context.Context, userId string) error {
176176 log .Printf ("[DEBUG] refresh finished in %s" , time .Since (start ))
177177 }()
178178
179- tmpDir := GetTempDirForUser (true , userId )
179+ tmpDir := GetTempDirForUser (userId )
180180 return d .TfCmd .Refresh (ctx , tmpDir )
181181}
182182
183- func GetTempDirForUser (readonly bool , userId string ) string {
183+ func GetTempDirForUser (userId string ) string {
184184 tmpDir := filepath .Join (os .TempDir (), "multy" , userId )
185185
186186 if flags .Environment == flags .Local {
187187 tmpDir = filepath .Join (tmpDir , "local" )
188188 }
189189
190- if readonly {
191- tmpDir = filepath .Join (tmpDir , "readonly" )
192- }
193-
194190 return tmpDir
195191}
0 commit comments