33
33
34
34
import java .io .File ;
35
35
import java .io .IOException ;
36
+ import java .io .InputStream ;
36
37
import java .nio .file .Path ;
37
38
import java .util .*;
39
+ import java .util .stream .Collectors ;
38
40
39
41
import static java .util .Collections .singletonList ;
40
42
@@ -92,7 +94,7 @@ private Infrastructure(Builder builder) {
92
94
93
95
public String deploy () {
94
96
uploadAssets ();
95
- LOG .debug ("Deploying '" + stackName + "' on account " + account );
97
+ LOG .info ("Deploying '" + stackName + "' on account " + account );
96
98
cfn .createStack (CreateStackRequest .builder ()
97
99
.stackName (stackName )
98
100
.templateBody (new Yaml ().dump (cfnTemplate ))
@@ -102,15 +104,15 @@ public String deploy() {
102
104
.build ());
103
105
WaiterResponse <DescribeStacksResponse > waiterResponse = cfn .waiter ().waitUntilStackCreateComplete (DescribeStacksRequest .builder ().stackName (stackName ).build ());
104
106
if (waiterResponse .matched ().response ().isPresent ()) {
105
- LOG .debug ("Stack " + waiterResponse .matched ().response ().get ().stacks ().get (0 ).stackName () + " successfully deployed" );
107
+ LOG .info ("Stack " + waiterResponse .matched ().response ().get ().stacks ().get (0 ).stackName () + " successfully deployed" );
106
108
} else {
107
109
throw new RuntimeException ("Failed to create stack" );
108
110
}
109
111
return functionName ;
110
112
}
111
113
112
114
public void destroy () {
113
- LOG .debug ("Deleting '" + stackName + "' on account " + account );
115
+ LOG .info ("Deleting '" + stackName + "' on account " + account );
114
116
cfn .deleteStack (DeleteStackRequest .builder ().stackName (stackName ).build ());
115
117
}
116
118
@@ -124,10 +126,32 @@ public static class Builder {
124
126
public String testName ;
125
127
private String stackName ;
126
128
private boolean tracing = false ;
127
- private JavaRuntime runtime = JavaRuntime . JAVA11 ;
129
+ private JavaRuntime runtime ;
128
130
private Map <String , String > environmentVariables = new HashMap <>();
129
131
private String idemPotencyTable ;
130
132
133
+ private Builder () {
134
+ getJavaRuntime ();
135
+ }
136
+
137
+ /**
138
+ * Retrieve the java runtime to use for the lambda function.
139
+ */
140
+ private void getJavaRuntime () {
141
+ String javaVersion = System .getenv ("JAVA_VERSION" ); // must be set in GitHub actions
142
+ if (javaVersion == null ) {
143
+ throw new IllegalArgumentException ("JAVA_VERSION is not set" );
144
+ }
145
+ if (javaVersion .startsWith ("8" )) {
146
+ runtime = JavaRuntime .JAVA8AL2 ;
147
+ } else if (javaVersion .startsWith ("11" )) {
148
+ runtime = JavaRuntime .JAVA11 ;
149
+ } else {
150
+ throw new IllegalArgumentException ("Unsupported Java version " + javaVersion );
151
+ }
152
+ LOG .debug ("Java Version set to {}, using runtime {}" , javaVersion , runtime .getRuntime ());
153
+ }
154
+
131
155
public Infrastructure build () {
132
156
Objects .requireNonNull (testName , "testName must not be null" );
133
157
@@ -153,11 +177,6 @@ public Builder tracing(boolean tracing) {
153
177
return this ;
154
178
}
155
179
156
- public Builder runtime (JavaRuntime runtime ) {
157
- this .runtime = runtime ;
158
- return this ;
159
- }
160
-
161
180
public Builder idempotencyTable (String tableName ) {
162
181
this .idemPotencyTable = tableName ;
163
182
return this ;
@@ -203,6 +222,7 @@ private Stack createStackWithLambda() {
203
222
204
223
functionName = stackName + "-function" ;
205
224
225
+ LOG .debug ("Building Lambda function with command " + packagingInstruction .stream ().collect (Collectors .joining (" " , "[" , "]" )));
206
226
Function function = Function .Builder
207
227
.create (stack , functionName )
208
228
.code (Code .fromAsset ("handlers/" , AssetOptions .builder ()
@@ -255,10 +275,10 @@ private void uploadAssets() {
255
275
}
256
276
ListObjectsV2Response objects = s3 .listObjectsV2 (ListObjectsV2Request .builder ().bucket (asset .bucketName ).build ());
257
277
if (objects .contents ().stream ().anyMatch (o -> o .key ().equals (objectKey ))) {
258
- System . out . println ("Asset already exists, skipping" );
278
+ LOG . debug ("Asset already exists, skipping" );
259
279
return ;
260
280
}
261
- System . out . println ("Uploading asset " + objectKey + " to " + asset .bucketName );
281
+ LOG . info ("Uploading asset " + objectKey + " to " + asset .bucketName );
262
282
s3 .putObject (PutObjectRequest .builder ().bucket (asset .bucketName ).key (objectKey ).build (), Path .of (cfnAssetDirectory , asset .assetPath ));
263
283
});
264
284
}
0 commit comments