Skip to content

Commit 68d031e

Browse files
committed
chore: aws mac pool added struct for running release remotely
Signed-off-by: Adrian Riobo <[email protected]>
1 parent 2967fe4 commit 68d031e

File tree

16 files changed

+298
-67
lines changed

16 files changed

+298
-67
lines changed

pkg/provider/aws/action/fedora/fedora.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
249249
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
250250
c.GetHostIP(!r.Airgap))
251251
if len(r.Timeout) > 0 {
252-
if err = serverless.OneTimeDelayedTask(ctx,
252+
if err = serverless.OneTimeDelayedTask(
253+
ctx,
254+
fmt.Sprintf("fedora-timeout-%s", maptContext.RunID()),
253255
r.region, r.Prefix,
254256
awsFedoraDedicatedID,
255257
fmt.Sprintf("aws %s destroy --project-name %s --backed-url %s --serverless",

pkg/provider/aws/action/mac-pool/housekeeper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
7+
"github.com/redhat-developer/mapt/pkg/provider/aws"
78
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
89
"github.com/redhat-developer/mapt/pkg/util/logging"
910
)
@@ -14,7 +15,7 @@ import (
1415
func houseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
1516
// Create mapt Context, this is a special case where we need change the context
1617
// based on the operation
17-
if err := maptContext.Init(ctx); err != nil {
18+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
1819
return err
1920
}
2021

@@ -52,6 +53,10 @@ func houseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
5253
func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
5354
return serverless.Create(
5455
&serverless.ServerlessArgs{
56+
ContainerName: fmt.Sprintf("housekeeper-%s-%s-%s",
57+
r.PoolName,
58+
r.Architecture,
59+
r.OSVersion),
5560
Command: houseKeepingCommand(
5661
r.PoolName,
5762
r.Architecture,

pkg/provider/aws/action/mac-pool/mac-pool.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ func Create(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
3434
if err := r.scheduleHouseKeeper(); err != nil {
3535
return err
3636
}
37-
if err := r.createRequestTaskSpec(); err != nil {
37+
if err := requestTaskSpec(r); err != nil {
38+
return err
39+
}
40+
if err := releaseTaskSpec(
41+
r.PoolName,
42+
r.Architecture,
43+
r.OSVersion); err != nil {
3844
return err
3945
}
4046
return r.requestReleaserAccount()
@@ -68,6 +74,10 @@ func Request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
6874
}
6975

7076
func Release(ctx *maptContext.ContextArgs, hostID string) error {
77+
// If remote run through serverless
78+
if ctx.Remote {
79+
return releaseRemote(ctx, hostID)
80+
}
7181
return macUtil.Release(ctx, hostID)
7282
}
7383

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package macpool
2+
3+
import (
4+
"fmt"
5+
6+
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
7+
"github.com/redhat-developer/mapt/pkg/provider/aws"
8+
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
9+
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
10+
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
11+
"github.com/redhat-developer/mapt/pkg/util/logging"
12+
)
13+
14+
func releaseRemote(ctx *maptContext.ContextArgs, hostID string) error {
15+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
16+
return err
17+
}
18+
// Get host as context will be fullfilled with info coming from the tags on the host
19+
host, err := data.GetDedicatedHost(hostID)
20+
if err != nil {
21+
return err
22+
}
23+
hi := macHost.GetHostInformation(*host)
24+
tARN, err := serverlessTaskARN(
25+
*hi.PoolName,
26+
*hi.Arch,
27+
*hi.OSVersion,
28+
releaseOperation)
29+
if err != nil {
30+
return err
31+
}
32+
logging.Debugf("Got ARN for task spec %s", *tARN)
33+
return fmt.Errorf("not implemented yet")
34+
}
35+
36+
func releaseTaskSpec(poolName, arch, osVersion string) error {
37+
name := serverlessName(
38+
poolName,
39+
arch,
40+
osVersion, releaseOperation)
41+
return serverless.Create(
42+
&serverless.ServerlessArgs{
43+
ContainerName: name,
44+
Command: releaseCommand,
45+
LogGroupName: name,
46+
Tags: serverlessTags(
47+
poolName,
48+
arch,
49+
osVersion,
50+
releaseOperation)})
51+
}

pkg/provider/aws/action/mac-pool/request.go

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@ package macpool
22

33
import (
44
"fmt"
5-
"strings"
65

76
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
8-
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
9-
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
10-
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
7+
"github.com/redhat-developer/mapt/pkg/provider/aws"
118
macMachine "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/machine"
129
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
1310
"github.com/redhat-developer/mapt/pkg/provider/aws/services/tag"
11+
"github.com/redhat-developer/mapt/pkg/util/logging"
1412
)
1513

1614
func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
17-
// If remote run through serverless
18-
if maptContext.IsRemote() {
19-
return requestRemote(ctx, r)
20-
}
2115
// First get full info on the pool and the next machine for request
2216
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
2317
if err != nil {
@@ -31,16 +25,15 @@ func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
3125
// Create mapt Context
3226
ctx.ProjectName = *hi.ProjectName
3327
ctx.BackedURL = *hi.BackedURL
34-
if err := maptContext.Init(ctx); err != nil {
28+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
3529
return err
3630
}
3731

3832
mr := macMachine.Request{
39-
Prefix: *hi.Prefix,
40-
Version: *hi.OSVersion,
41-
Architecture: *hi.Arch,
42-
SetupGHActionsRunner: r.SetupGHActionsRunner,
43-
Timeout: r.Timeout,
33+
Prefix: *hi.Prefix,
34+
Version: *hi.OSVersion,
35+
Architecture: *hi.Arch,
36+
Timeout: r.Timeout,
4437
}
4538

4639
// TODO here we would change based on the integration-mode requested
@@ -58,62 +51,46 @@ func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
5851
}
5952

6053
func requestRemote(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
61-
if err := maptContext.Init(ctx); err != nil {
54+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
6255
return err
6356
}
64-
rARNs, err := data.GetResourcesMatchingTags(
65-
data.ResourceTypeECS,
66-
requestTags(
67-
r.PoolName,
68-
r.Architecture,
69-
r.OSVersion))
57+
tARN, err := serverlessTaskARN(r.PoolName,
58+
r.Architecture,
59+
r.OSVersion,
60+
requestOperation)
7061
if err != nil {
7162
return err
7263
}
73-
if len(rARNs) > 1 {
74-
return fmt.Errorf(
75-
"should be only one task spec matching tags. Found %s",
76-
strings.Join(rARNs, ","))
77-
}
78-
// We got the arn value for the task
64+
logging.Debugf("Got ARN for task spec %s", *tARN)
7965
return fmt.Errorf("not implemented yet")
8066
}
8167

8268
// Run serverless operation request
8369
// check how we will call it from the request?
8470
// may add tags and find or add arn to stack?
85-
func (r *MacPoolRequestArgs) createRequestTaskSpec() error {
71+
func requestTaskSpec(r *MacPoolRequestArgs) error {
72+
name := serverlessName(
73+
r.PoolName,
74+
r.Architecture,
75+
r.OSVersion,
76+
requestOperation)
8677
return serverless.Create(
8778
&serverless.ServerlessArgs{
79+
ContainerName: name,
8880
Command: requestCommand(
8981
r.PoolName,
9082
r.Architecture,
9183
r.OSVersion),
92-
LogGroupName: fmt.Sprintf("%s-%s-%s-request",
93-
r.PoolName,
94-
r.Architecture,
95-
r.OSVersion),
96-
Tags: requestTags(
84+
LogGroupName: name,
85+
Tags: serverlessTags(
9786
r.PoolName,
9887
r.Architecture,
99-
r.OSVersion)})
88+
r.OSVersion,
89+
requestOperation)})
10090
}
10191

10292
func requestCommand(poolName, arch, osVersion string) string {
10393
cmd := fmt.Sprintf(requestCommandRegex,
10494
poolName, arch, osVersion)
10595
return cmd
10696
}
107-
108-
// Return the map of tags wich should identify unique
109-
// resquest operation spec for a pool
110-
func requestTags(poolName, arch, osVersion string) (m map[string]string) {
111-
poolID := macHost.PoolID{
112-
PoolName: poolName,
113-
Arch: arch,
114-
OSVersion: osVersion,
115-
}
116-
m = poolID.AsTags()
117-
m[macConstants.TagKeyPoolOperationName] = requestOperation
118-
return
119-
}

pkg/provider/aws/action/mac-pool/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const (
1010
// https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html#eb-rate-expressions
1111
houseKeepingInterval = "27 minutes"
1212

13-
requestOperation = "request"
13+
requestOperation = "mac-pool-request"
1414
requestCommandRegex = "aws mac-pool request --name %s --arch %s --version %s --serverless "
1515
// requestTimeoutParam = "--timeout "
1616
// itCirrusPWTokenParam = "--it-cirrus-pw-token "
1717
// itCirrusPWLabelsParam = "--it-cirrus-pw-labels "
18+
releaseOperation = "mac-pool-release"
19+
releaseCommand = "aws mac-pool release --serverless "
1820
)
1921

2022
type MacPoolRequestArgs struct {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package macpool
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
8+
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
9+
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
10+
)
11+
12+
// this is a business identificator to assing to resources related to the serverless management
13+
func serverlessName(poolName, arch, osVersion, operation string) string {
14+
return fmt.Sprintf("%s-%s-%s-%s",
15+
operation,
16+
poolName,
17+
arch,
18+
osVersion)
19+
}
20+
21+
func serverlessTaskARN(poolName, arch, osVersion, operation string) (*string, error) {
22+
rARNs, err := data.GetResourcesMatchingTags(
23+
data.ResourceTypeECS,
24+
serverlessTags(
25+
poolName,
26+
arch,
27+
osVersion,
28+
operation))
29+
if err != nil {
30+
return nil, err
31+
}
32+
if len(rARNs) > 1 {
33+
return nil, fmt.Errorf(
34+
"should be only one task spec matching tags. Found %s",
35+
strings.Join(rARNs, ","))
36+
}
37+
return &rARNs[0], nil
38+
39+
}
40+
41+
// Return the map of tags wich should identify unique
42+
// resquest operation spec for a pool
43+
func serverlessTags(poolName, arch, osVersion, operation string) (m map[string]string) {
44+
poolID := macHost.PoolID{
45+
PoolName: poolName,
46+
Arch: arch,
47+
OSVersion: osVersion,
48+
}
49+
m = poolID.AsTags()
50+
m[macConstants.TagKeyPoolOperationName] = operation
51+
return
52+
}

pkg/provider/aws/action/rhel/rhel.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
245245
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
246246
c.GetHostIP(!r.Airgap))
247247
if len(r.Timeout) > 0 {
248-
if err = serverless.OneTimeDelayedTask(ctx,
248+
if err = serverless.OneTimeDelayedTask(
249+
ctx,
250+
fmt.Sprintf("rhel-timeout-%s", maptContext.RunID()),
249251
r.region, r.Prefix,
250252
awsRHELDedicatedID,
251253
fmt.Sprintf("aws %s destroy --project-name %s --backed-url %s --serverless",

pkg/provider/aws/action/windows/windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
280280
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
281281
c.GetHostIP(!r.Airgap))
282282
if len(r.Timeout) > 0 {
283-
if err = serverless.OneTimeDelayedTask(ctx,
283+
if err = serverless.OneTimeDelayedTask(
284+
ctx,
285+
fmt.Sprintf("windows-timeout-%s", maptContext.RunID()),
284286
r.region, r.Prefix,
285287
awsWindowsDedicatedID,
286288
fmt.Sprintf("aws %s destroy --project-name %s --backed-url %s --serverless",

pkg/provider/aws/modules/mac/host/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func GetMatchingHostsInStateInformation(matchingTags map[string]string, state *e
5555
}
5656
var r []*mac.HostInformation
5757
for _, dh := range hosts {
58-
if state == nil || (state != nil && dh.State == *state) {
58+
if state == nil || dh.State == *state {
5959
r = append(r, GetHostInformation(dh))
6060
}
6161
}
@@ -83,6 +83,7 @@ func GetHostInformation(h ec2Types.Host) *mac.HostInformation {
8383
RunID: getTagValue(h.Tags, maptContext.TagKeyRunID),
8484
Region: &region,
8585
Host: &h,
86+
PoolName: getTagValue(h.Tags, macConstants.TagKeyPoolName),
8687
}
8788
}
8889

0 commit comments

Comments
 (0)