@@ -2,10 +2,15 @@ package macpool
22
33import (
44 "fmt"
5+ "strings"
56
7+ "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
68 maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
79 "github.com/redhat-developer/mapt/pkg/provider/aws"
10+ "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac"
11+ macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
812 "github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
13+ "github.com/redhat-developer/mapt/pkg/util"
914 "github.com/redhat-developer/mapt/pkg/util/logging"
1015)
1116
@@ -50,9 +55,11 @@ func houseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
5055}
5156
5257// Run serverless operation for house keeping
53- func (r * MacPoolRequestArgs ) scheduleHouseKeeper () error {
54- return serverless .Create (
58+ func scheduleHouseKeeper (ctx * pulumi.Context , r * MacPoolRequestArgs ) error {
59+ // First I need to create
60+ return serverless .Deploy (ctx ,
5561 & serverless.ServerlessArgs {
62+ Prefix : houseKeepingOperation ,
5663 ContainerName : fmt .Sprintf ("housekeeper-%s-%s-%s" ,
5764 r .PoolName ,
5865 r .Architecture ,
@@ -66,12 +73,17 @@ func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
6673 r .FixedLocation ),
6774 ScheduleType : & serverless .Repeat ,
6875 Schedulexpression : houseKeepingInterval ,
69- LogGroupName : fmt .Sprintf ("%s-%s-%s" ,
76+ LogGroupName : fmt .Sprintf ("%s-%s-%s-%s" ,
77+ houseKeepingOperation ,
7078 r .PoolName ,
7179 r .Architecture ,
7280 r .OSVersion )})
7381}
7482
83+ // func destroyScheduleHouseKeeper() error {
84+ // return serverless.Destroy(&houseKeepingOperation)
85+ // }
86+
7587func houseKeepingCommand (poolName , arch , osVersion string ,
7688 offeredCapacity , maxSize int ,
7789 fixedLocation bool ) string {
@@ -83,3 +95,89 @@ func houseKeepingCommand(poolName, arch, osVersion string,
8395 }
8496 return cmd
8597}
98+
99+ func (r * MacPoolRequestArgs ) addMachinesToPool (n int ) error {
100+ if err := validateBackedURL (); err != nil {
101+ return err
102+ }
103+ for i := 0 ; i < n ; i ++ {
104+ hr := r .fillHostRequest ()
105+ dh , err := macHost .CreatePoolDedicatedHost (hr )
106+ if err != nil {
107+ return err
108+ }
109+ mr := r .fillMacRequest ()
110+ if err = mr .CreateAvailableMacMachine (dh ); err != nil {
111+ return err
112+ }
113+ }
114+ return nil
115+ }
116+
117+ // format for remote backed url when creating the dedicated host
118+ // the backed url from param is used as base and the ID is appended as sub path
119+ func validateBackedURL () error {
120+ if strings .Contains (maptContext .BackedURL (), "file://" ) {
121+ return fmt .Errorf ("local backed url is not allowed for mac pool" )
122+ }
123+ return nil
124+ }
125+
126+ // transform pool request to host request
127+ // need if we need to expand the pool
128+ func (r * MacPoolRequestArgs ) fillHostRequest () * macHost.PoolMacDedicatedHostRequestArgs {
129+ return & macHost.PoolMacDedicatedHostRequestArgs {
130+ MacDedicatedHost : & macHost.MacDedicatedHostRequestArgs {
131+ Prefix : r .Prefix ,
132+ Architecture : r .Architecture ,
133+ FixedLocation : r .FixedLocation ,
134+ },
135+ PoolID : & macHost.PoolID {
136+ PoolName : r .PoolName ,
137+ Arch : r .Architecture ,
138+ OSVersion : r .OSVersion ,
139+ },
140+ BackedURL : fmt .Sprintf ("%s/%s" ,
141+ maptContext .BackedURL (),
142+ util .RandomID ("mapt" )),
143+ }
144+ }
145+
146+ // If we need less or equal than the max allowed on the pool we create all of them
147+ // if need are more than allowed we can create just the allowed
148+ func (r * MacPoolRequestArgs ) addCapacity (p * pool ) error {
149+ allowed := p .maxSize - p .offeredCapacity
150+ needed := p .offeredCapacity - p .currentOfferedCapacity ()
151+ if needed <= allowed {
152+ return r .addMachinesToPool (needed )
153+ }
154+ return r .addMachinesToPool (allowed )
155+ }
156+
157+ // If we need less or equal than the max allowed on the pool we create all of them
158+ // if need are more than allowed we can create just the allowed
159+ // TODO review allocation time is on the wrong order
160+ func (r * MacPoolRequestArgs ) destroyCapacity (p * pool ) error {
161+ machinesToDestroy := p .currentOfferedCapacity () - r .OfferedCapacity
162+ for i := 0 ; i < machinesToDestroy ; i ++ {
163+ m := p .destroyableMachines [i ]
164+ // TODO change this
165+ maptContext .SetProjectName (* m .ProjectName )
166+ if err := aws .DestroyStack (aws.DestroyStackRequest {
167+ Stackname : mac .StackMacMachine ,
168+ Region : * m .Region ,
169+ BackedURL : * m .BackedURL ,
170+ }); err != nil {
171+ return err
172+ }
173+ if err := aws .DestroyStack (aws.DestroyStackRequest {
174+ Stackname : mac .StackDedicatedHost ,
175+ // TODO check if needed to add region for backedURL
176+ Region : * m .Region ,
177+ BackedURL : * m .BackedURL ,
178+ }); err != nil {
179+ return err
180+ }
181+ }
182+ return nil
183+ }
0 commit comments