6
6
"encoding/hex"
7
7
"fmt"
8
8
"os"
9
- "sync"
10
9
"time"
11
10
12
11
"go.opentelemetry.io/otel/trace"
@@ -20,11 +19,8 @@ import (
20
19
"github.com/ctfer-io/chall-manager/global"
21
20
errs "github.com/ctfer-io/chall-manager/pkg/errors"
22
21
"github.com/ctfer-io/chall-manager/pkg/fs"
23
- "github.com/ctfer-io/chall-manager/pkg/iac"
24
- "github.com/ctfer-io/chall-manager/pkg/identity"
25
22
"github.com/ctfer-io/chall-manager/pkg/lock"
26
23
"github.com/ctfer-io/chall-manager/pkg/scenario"
27
- "github.com/pkg/errors"
28
24
)
29
25
30
26
func (store * Store ) CreateChallenge (ctx context.Context , req * CreateChallengeRequest ) (* Challenge , error ) {
@@ -129,68 +125,8 @@ func (store *Store) CreateChallenge(ctx context.Context, req *CreateChallengeReq
129
125
130
126
// 6. Spin up instances if pool is configured. Lock is acquired at challenge level
131
127
// hence don't need to be held too.
132
- wg := sync.WaitGroup {}
133
- wg .Add (int (req .Min ))
134
- cerr := make (chan error , req .Min )
135
128
for range req .Min {
136
- go func () {
137
- defer wg .Done ()
138
-
139
- id := identity .New ()
140
- stack , err := iac .NewStack (ctx , id , fschall )
141
- if err != nil {
142
- cerr <- errors .Wrap (err , "building new stack" )
143
- return
144
- }
145
-
146
- if err := iac .Additional (ctx , stack , fschall .Additional , req .Additional ); err != nil {
147
- cerr <- errors .Wrap (err , "configuring additionals on stack" )
148
- return
149
- }
150
-
151
- sr , err := stack .Up (ctx )
152
- if err != nil {
153
- cerr <- errors .Wrap (err , "stack up" )
154
- return
155
- }
156
-
157
- now := time .Now ()
158
- fsist := & fs.Instance {
159
- Identity : id ,
160
- ChallengeID : req .Id ,
161
- Since : now ,
162
- LastRenew : now ,
163
- Until : common .ComputeUntil (fschall .Until , fschall .Timeout ),
164
- Additional : req .Additional ,
165
- }
166
- if err := iac .Extract (ctx , stack , sr , fsist ); err != nil {
167
- cerr <- errors .Wrap (err , "extracting stack info" )
168
- return
169
- }
170
-
171
- if err := fsist .Save (); err != nil {
172
- cerr <- errors .Wrap (err , "exporting instance information to filesystem" )
173
- return
174
- }
175
-
176
- logger .Info (ctx , "instance created successfully" )
177
- common .InstancesUDCounter ().Add (ctx , 1 )
178
- }()
179
- }
180
- wg .Wait ()
181
- close (cerr )
182
-
183
- var merr error
184
- for err := range cerr {
185
- merr = multierr .Append (merr , err )
186
- }
187
- if merr != nil {
188
- // TODO retry if any problem ? -> resiliency, sagas, cleaner API for challenge/instances mamagement
189
- err := & errs.ErrInternal {Sub : merr }
190
- logger .Error (ctx , "pooling instances" ,
191
- zap .Error (err ),
192
- )
193
- return nil , errs .ErrInternalNoSub
129
+ go instance .SpinUp (ctx , req .Id )
194
130
}
195
131
196
132
// 7. Save challenge on filesystem, and respond to API call
0 commit comments