Skip to content

Commit f73d35c

Browse files
authored
chore(cli): Pass etag to backend during deploy (#353)
1 parent 7a8a479 commit f73d35c

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

apps/cli/lib/src/frontend/celest_frontend.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import 'package:celest_cli/src/utils/process.dart';
3333
import 'package:celest_cli/src/utils/recase.dart';
3434
import 'package:celest_cli/src/utils/run.dart';
3535
import 'package:celest_cloud/src/proto.dart' as pb;
36+
import 'package:crypto/crypto.dart';
3637
import 'package:logging/logging.dart';
3738
import 'package:mason_logger/mason_logger.dart' show Progress;
3839
import 'package:stream_transform/stream_transform.dart';
@@ -1002,7 +1003,7 @@ final class CelestFrontend {
10021003
return cloudEnvironment;
10031004
}
10041005

1005-
Future<Uint8List> _tarGzDirectory(String path) async {
1006+
Future<(Uint8List, String)> _tarGzDirectory(String path) async {
10061007
final encoder = TarFileEncoder();
10071008
final tarFile = fileSystem.file(
10081009
p.join(projectPaths.projectCacheDir, '${p.basename(path)}.tar'),
@@ -1013,7 +1014,8 @@ final class CelestFrontend {
10131014
filename: tarFile.path,
10141015
);
10151016
final tarStream = tarFile.openRead();
1016-
return collectBytes(tarStream.transform(gzip.encoder));
1017+
final bytes = await collectBytes(tarStream.transform(gzip.encoder));
1018+
return (bytes, md5.convert(bytes).toString());
10171019
}
10181020

10191021
Future<(ast.ResolvedProject, Uri)> _deployProject({
@@ -1026,26 +1028,26 @@ final class CelestFrontend {
10261028
resolvedProject: resolvedProject,
10271029
environmentId: environmentId,
10281030
);
1029-
final (kernelBytes, flutterAssetBytes) = (
1030-
output.outputDill,
1031-
await switch (resolvedProject.sdkConfig.targetSdk) {
1032-
ast.SdkType.flutter => _tarGzDirectory(
1033-
p.join(projectPaths.buildDir, 'flutter_assets'),
1034-
),
1035-
_ => Future.value(Uint8List(0)),
1036-
},
1037-
);
1031+
final (flutterAssetBytes, flutterAssetsEtag) =
1032+
switch (resolvedProject.sdkConfig.targetSdk) {
1033+
ast.SdkType.flutter => await _tarGzDirectory(
1034+
p.join(projectPaths.buildDir, 'flutter_assets'),
1035+
),
1036+
_ => (Uint8List(0), ''),
1037+
};
10381038
final assets = [
10391039
pb.ProjectAsset(
10401040
type: output.type,
10411041
filename: '${p.basenameWithoutExtension(output.outputDillPath)}.gz',
1042-
inline: gzip.encode(kernelBytes),
1042+
inline: gzip.encode(output.outputDill),
1043+
etag: output.outputDillDigest.toString(),
10431044
),
10441045
if (resolvedProject.sdkConfig.targetSdk == ast.SdkType.flutter)
10451046
pb.ProjectAsset(
10461047
type: pb.ProjectAsset_Type.FLUTTER_ASSETS,
10471048
filename: 'flutter_assets.tar.gz',
10481049
inline: flutterAssetBytes,
1050+
etag: flutterAssetsEtag,
10491051
),
10501052
];
10511053
final operation = cloud.projects.environments.deploy(

examples/gemini/celest/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
# Celest
55
**/.env
6+
build/

services/celest_cloud_hub/lib/src/deploy/fly/fly_deployment_engine.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef ProjectAsset =
3434
pb.ProjectAsset_Type type,
3535
String filename,
3636
Uint8List inline,
37-
String etag,
37+
String? etag,
3838
});
3939

4040
final class FlyDeploymentEngine {
@@ -263,8 +263,10 @@ final class FlyDeploymentEngine {
263263

264264
// Deploy using `flyctl deploy`
265265
await _withTempDirectory((dir) async {
266-
if (kernelAsset.etag != md5.convert(kernelAsset.inline).toString()) {
267-
throw GrpcError.failedPrecondition('Kernel asset has been modified');
266+
if (kernelAsset.etag case final etag?) {
267+
if (etag != md5.convert(kernelAsset.inline).toString()) {
268+
throw GrpcError.failedPrecondition('Kernel asset has been modified');
269+
}
268270
}
269271

270272
// Write the kernel file to disk
@@ -281,11 +283,12 @@ final class FlyDeploymentEngine {
281283
await kernelFile.writeAsBytes(kernelAssetData);
282284

283285
if (flutterAssetsBundle case final flutterAssetsBundle?) {
284-
if (flutterAssetsBundle.etag !=
285-
md5.convert(flutterAssetsBundle.inline).toString()) {
286-
throw GrpcError.failedPrecondition(
287-
'Flutter assets bundle has been modified',
288-
);
286+
if (flutterAssetsBundle.etag case final etag?) {
287+
if (etag != md5.convert(flutterAssetsBundle.inline).toString()) {
288+
throw GrpcError.failedPrecondition(
289+
'Flutter assets bundle has been modified',
290+
);
291+
}
289292
}
290293
// Write the Flutter assets bundle to disk
291294
final flutterAssetsFile = dir.childFile(flutterAssetsBundle.filename);

services/celest_cloud_hub/lib/src/services/project_environments_service.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,7 @@ final class ProjectEnvironmentsService extends ProjectEnvironmentsServiceBase
453453
if (kernelAsset == null) {
454454
throw GrpcError.invalidArgument('Missing Dart kernel asset');
455455
}
456-
if (!kernelAsset.hasEtag()) {
457-
throw GrpcError.invalidArgument('Missing ETag for kernel asset');
458-
}
459456
final flutterAssetsBundle = assets[pb.ProjectAsset_Type.FLUTTER_ASSETS];
460-
if (flutterAssetsBundle != null && !flutterAssetsBundle.hasEtag()) {
461-
throw GrpcError.invalidArgument('Missing ETag for Flutter asset');
462-
}
463457

464458
final operationId = typeId('op');
465459
final operation =
@@ -481,14 +475,14 @@ final class ProjectEnvironmentsService extends ProjectEnvironmentsServiceBase
481475
filename: kernelAsset.filename,
482476
inline: Uint8List.fromList(kernelAsset.inline).asUnmodifiableView(),
483477
type: kernelAsset.type,
484-
etag: kernelAsset.etag,
478+
etag: kernelAsset.hasEtag() ? kernelAsset.etag : null,
485479
),
486480
flutterAssetsBundle: switch (flutterAssetsBundle) {
487481
final asset? => (
488482
filename: asset.filename,
489483
inline: Uint8List.fromList(asset.inline).asUnmodifiableView(),
490484
type: asset.type,
491-
etag: asset.etag,
485+
etag: asset.hasEtag() ? asset.etag : null,
492486
),
493487
_ => null,
494488
},

0 commit comments

Comments
 (0)