Skip to content

Commit ca9e9d1

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

File tree

18 files changed

+379
-77
lines changed

18 files changed

+379
-77
lines changed

pkg/integrations/cirrus/persistentworker.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (args *PersistentWorkerArgs) GetUserDataValues() *integrations.UserDataValu
4343
CliURL: downloadURL(),
4444
Name: pwa.Name,
4545
Token: pwa.Token,
46-
Labels: getLabelsAsString(),
46+
Labels: GetLabelsAsString(),
4747
Port: cirrusPort,
4848
}
4949
}
@@ -74,18 +74,8 @@ func GetToken() string {
7474
"")
7575
}
7676

77-
// platform: darwin, linux, windows
78-
// arch: amd64, arm64
79-
func downloadURL() string {
80-
url := fmt.Sprintf(baseURL, version, *pwa.Platform, *pwa.Arch)
81-
if pwa.Platform == &Windows {
82-
url = fmt.Sprintf("%s.exe", url)
83-
}
84-
return url
85-
}
86-
8777
// Get labels in format
88-
func getLabelsAsString() string {
78+
func GetLabelsAsString() string {
8979
return util.IfNillable(pwa != nil,
9080
func() string {
9181
if len(pwa.Labels) > 0 {
@@ -101,3 +91,13 @@ func getLabelsAsString() string {
10191
},
10292
"")
10393
}
94+
95+
// platform: darwin, linux, windows
96+
// arch: amd64, arm64
97+
func downloadURL() string {
98+
url := fmt.Sprintf(baseURL, version, *pwa.Platform, *pwa.Arch)
99+
if pwa.Platform == &Windows {
100+
url = fmt.Sprintf("%s.exe", url)
101+
}
102+
return url
103+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
255255
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
256256
c.GetHostIP(!r.Airgap))
257257
if len(r.Timeout) > 0 {
258-
if err = serverless.OneTimeDelayedTask(ctx,
258+
if err = serverless.OneTimeDelayedTask(
259+
ctx,
260+
fmt.Sprintf("fedora-timeout-%s", maptContext.RunID()),
259261
r.region, r.Prefix,
260262
awsFedoraDedicatedID,
261263
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package macpool
2+
3+
import (
4+
"os"
5+
"strings"
6+
7+
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
8+
"github.com/redhat-developer/mapt/pkg/provider/aws"
9+
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
10+
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
11+
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
12+
"github.com/redhat-developer/mapt/pkg/provider/aws/services/ecs"
13+
"github.com/redhat-developer/mapt/pkg/util/logging"
14+
)
15+
16+
func releaseRemote(ctx *maptContext.ContextArgs, hostID string) error {
17+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
18+
return err
19+
}
20+
// Get host as context will be fullfilled with info coming from the tags on the host
21+
host, err := data.GetDedicatedHost(hostID)
22+
if err != nil {
23+
return err
24+
}
25+
hi := macHost.GetHostInformation(*host)
26+
tARN, err := serverlessTaskARN(
27+
*hi.PoolName,
28+
*hi.Arch,
29+
*hi.OSVersion,
30+
releaseOperation)
31+
if err != nil {
32+
return err
33+
}
34+
logging.Debugf("Got ARN for task spec %s", *tARN)
35+
// How to handle the region...coming from create operation we are always using "us-east-1"
36+
defaultRegion := "us-east-1"
37+
containerName := releaseTaskContainerName(*hi.PoolName, *hi.Arch, *hi.OSVersion)
38+
// just pass the params
39+
cmd := strings.Join(os.Args[1:], " ")
40+
return ecs.RunTaskWithCommand(&defaultRegion, tARN,
41+
&serverless.MaptServerlessClusterName, &containerName, &cmd)
42+
}
43+
44+
func releaseTaskSpec(poolName, arch, osVersion string) error {
45+
name := releaseTaskContainerName(poolName, arch, osVersion)
46+
return serverless.Create(
47+
&serverless.ServerlessArgs{
48+
ContainerName: name,
49+
Command: releaseCommand,
50+
LogGroupName: name,
51+
Tags: serverlessTags(
52+
poolName,
53+
arch,
54+
osVersion,
55+
releaseOperation)})
56+
}
57+
58+
func releaseTaskContainerName(poolName, arch, osVersion string) string {
59+
return serverlessName(poolName, arch, osVersion, releaseOperation)
60+
}

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

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ package macpool
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67

78
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"
9+
"github.com/redhat-developer/mapt/pkg/provider/aws"
1110
macMachine "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/machine"
1211
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
12+
"github.com/redhat-developer/mapt/pkg/provider/aws/services/ecs"
1313
"github.com/redhat-developer/mapt/pkg/provider/aws/services/tag"
14+
"github.com/redhat-developer/mapt/pkg/util/logging"
1415
)
1516

1617
func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
17-
// If remote run through serverless
18-
if maptContext.IsRemote() {
19-
return requestRemote(ctx, r)
20-
}
2118
// First get full info on the pool and the next machine for request
2219
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
2320
if err != nil {
@@ -31,16 +28,15 @@ func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
3128
// Create mapt Context
3229
ctx.ProjectName = *hi.ProjectName
3330
ctx.BackedURL = *hi.BackedURL
34-
if err := maptContext.Init(ctx); err != nil {
31+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
3532
return err
3633
}
3734

3835
mr := macMachine.Request{
39-
Prefix: *hi.Prefix,
40-
Version: *hi.OSVersion,
41-
Architecture: *hi.Arch,
42-
SetupGHActionsRunner: r.SetupGHActionsRunner,
43-
Timeout: r.Timeout,
36+
Prefix: *hi.Prefix,
37+
Version: *hi.OSVersion,
38+
Architecture: *hi.Arch,
39+
Timeout: r.Timeout,
4440
}
4541

4642
// TODO here we would change based on the integration-mode requested
@@ -58,45 +54,47 @@ func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
5854
}
5955

6056
func requestRemote(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
61-
if err := maptContext.Init(ctx); err != nil {
57+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
6258
return err
6359
}
64-
rARNs, err := data.GetResourcesMatchingTags(
65-
data.ResourceTypeECS,
66-
requestTags(
67-
r.PoolName,
68-
r.Architecture,
69-
r.OSVersion))
60+
tARN, err := serverlessTaskARN(r.PoolName,
61+
r.Architecture,
62+
r.OSVersion,
63+
requestOperation)
7064
if err != nil {
7165
return err
7266
}
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
79-
return fmt.Errorf("not implemented yet")
67+
logging.Debugf("Got ARN for task spec %s", *tARN)
68+
// How to handle the region...coming from create operation we are always using "us-east-1"
69+
defaultRegion := "us-east-1"
70+
containerName := requestTaskContainerName(r.PoolName, r.Architecture, r.OSVersion)
71+
// just pass the params
72+
cmd := strings.Join(os.Args[1:], " ")
73+
return ecs.RunTaskWithCommand(&defaultRegion, tARN,
74+
&serverless.MaptServerlessClusterName, &containerName, &cmd)
8075
}
8176

8277
// Run serverless operation request
8378
// check how we will call it from the request?
8479
// may add tags and find or add arn to stack?
85-
func (r *MacPoolRequestArgs) createRequestTaskSpec() error {
80+
func requestTaskSpec(r *MacPoolRequestArgs) error {
81+
name := requestTaskContainerName(
82+
r.PoolName,
83+
r.Architecture,
84+
r.OSVersion)
8685
return serverless.Create(
8786
&serverless.ServerlessArgs{
87+
ContainerName: name,
8888
Command: requestCommand(
8989
r.PoolName,
9090
r.Architecture,
9191
r.OSVersion),
92-
LogGroupName: fmt.Sprintf("%s-%s-%s-request",
93-
r.PoolName,
94-
r.Architecture,
95-
r.OSVersion),
96-
Tags: requestTags(
92+
LogGroupName: name,
93+
Tags: serverlessTags(
9794
r.PoolName,
9895
r.Architecture,
99-
r.OSVersion)})
96+
r.OSVersion,
97+
requestOperation)})
10098
}
10199

102100
func requestCommand(poolName, arch, osVersion string) string {
@@ -105,15 +103,6 @@ func requestCommand(poolName, arch, osVersion string) string {
105103
return cmd
106104
}
107105

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
106+
func requestTaskContainerName(poolName, arch, osVersion string) string {
107+
return serverlessName(poolName, arch, osVersion, requestOperation)
119108
}

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
@@ -246,7 +246,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
246246
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
247247
c.GetHostIP(!r.Airgap))
248248
if len(r.Timeout) > 0 {
249-
if err = serverless.OneTimeDelayedTask(ctx,
249+
if err = serverless.OneTimeDelayedTask(
250+
ctx,
251+
fmt.Sprintf("rhel-timeout-%s", maptContext.RunID()),
250252
r.region, r.Prefix,
251253
awsRHELDedicatedID,
252254
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
@@ -281,7 +281,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
281281
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
282282
c.GetHostIP(!r.Airgap))
283283
if len(r.Timeout) > 0 {
284-
if err = serverless.OneTimeDelayedTask(ctx,
284+
if err = serverless.OneTimeDelayedTask(
285+
ctx,
286+
fmt.Sprintf("windows-timeout-%s", maptContext.RunID()),
285287
r.region, r.Prefix,
286288
awsWindowsDedicatedID,
287289
fmt.Sprintf("aws %s destroy --project-name %s --backed-url %s --serverless",

0 commit comments

Comments
 (0)