@@ -2,13 +2,21 @@ import CloudRunnerLogger from './cloud-runner-logger';
22import BuildParameters from '../../../build-parameters' ;
33import CloudRunner from '../../cloud-runner' ;
44import Input from '../../../input' ;
5- import { DeleteObjectCommand , ListObjectsV2Command , PutObjectCommand , S3 } from '@aws-sdk/client-s3' ;
5+ import {
6+ CreateBucketCommand ,
7+ DeleteObjectCommand ,
8+ HeadBucketCommand ,
9+ ListObjectsV2Command ,
10+ PutObjectCommand ,
11+ S3 ,
12+ } from '@aws-sdk/client-s3' ;
13+ import { AwsClientFactory } from '../../providers/aws/aws-client-factory' ;
614export class SharedWorkspaceLocking {
715 private static _s3 : S3 ;
816 private static get s3 ( ) : S3 {
917 if ( ! SharedWorkspaceLocking . _s3 ) {
10- const region = Input . region || process . env . AWS_REGION || process . env . AWS_DEFAULT_REGION || 'us-east-1' ;
11- SharedWorkspaceLocking . _s3 = new S3 ( { region } ) ;
18+ // Use factory so LocalStack endpoint/path-style settings are honored
19+ SharedWorkspaceLocking . _s3 = AwsClientFactory . getS3 ( ) ;
1220 }
1321 return SharedWorkspaceLocking . _s3 ;
1422 }
@@ -24,7 +32,21 @@ export class SharedWorkspaceLocking {
2432 private static get workspacePrefix ( ) {
2533 return `locks/` ;
2634 }
35+ private static async ensureBucketExists ( ) : Promise < void > {
36+ const bucket = SharedWorkspaceLocking . bucket ;
37+ try {
38+ await SharedWorkspaceLocking . s3 . send ( new HeadBucketCommand ( { Bucket : bucket } ) ) ;
39+ } catch {
40+ const region = Input . region || process . env . AWS_REGION || process . env . AWS_DEFAULT_REGION || 'us-east-1' ;
41+ const createParams : any = { Bucket : bucket } ;
42+ if ( region && region !== 'us-east-1' ) {
43+ createParams . CreateBucketConfiguration = { LocationConstraint : region } ;
44+ }
45+ await SharedWorkspaceLocking . s3 . send ( new CreateBucketCommand ( createParams ) ) ;
46+ }
47+ }
2748 private static async listObjects ( prefix : string , bucket = SharedWorkspaceLocking . bucket ) : Promise < string [ ] > {
49+ await SharedWorkspaceLocking . ensureBucketExists ( ) ;
2850 if ( prefix !== '' && ! prefix . endsWith ( '/' ) ) {
2951 prefix += '/' ;
3052 }
@@ -241,6 +263,7 @@ export class SharedWorkspaceLocking {
241263 }
242264 const timestamp = Date . now ( ) ;
243265 const key = `${ SharedWorkspaceLocking . workspacePrefix } ${ buildParametersContext . cacheKey } /${ timestamp } _${ workspace } _workspace` ;
266+ await SharedWorkspaceLocking . ensureBucketExists ( ) ;
244267 await SharedWorkspaceLocking . s3 . send (
245268 new PutObjectCommand ( { Bucket : SharedWorkspaceLocking . bucket , Key : key , Body : '' } ) ,
246269 ) ;
@@ -268,6 +291,7 @@ export class SharedWorkspaceLocking {
268291 const key = `${ SharedWorkspaceLocking . workspacePrefix } ${
269292 buildParametersContext . cacheKey
270293 } /${ Date . now ( ) } _${ runId } _${ ending } _lock`;
294+ await SharedWorkspaceLocking . ensureBucketExists ( ) ;
271295 await SharedWorkspaceLocking . s3 . send (
272296 new PutObjectCommand ( { Bucket : SharedWorkspaceLocking . bucket , Key : key , Body : '' } ) ,
273297 ) ;
@@ -290,6 +314,7 @@ export class SharedWorkspaceLocking {
290314 runId : string ,
291315 buildParametersContext : BuildParameters ,
292316 ) : Promise < boolean > {
317+ await SharedWorkspaceLocking . ensureBucketExists ( ) ;
293318 const files = await SharedWorkspaceLocking . GetAllLocksForWorkspace ( workspace , buildParametersContext ) ;
294319 const file = files . find ( ( x ) => x . includes ( workspace ) && x . endsWith ( `_lock` ) && x . includes ( runId ) ) ;
295320 CloudRunnerLogger . log ( `All Locks ${ files } ${ workspace } ${ runId } ` ) ;
0 commit comments