|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:celest_ast/celest_ast.dart' as ast; |
| 4 | +import 'package:celest_cli/src/sdk/sdk_finder.dart'; |
| 5 | +import 'package:celest_cloud_hub/src/database/cloud_hub_database.dart'; |
| 6 | +import 'package:celest_cloud_hub/src/deploy/fly/fly_deployment_engine.dart'; |
| 7 | +import 'package:celest_cloud_hub/src/services/service_mixin.dart'; |
| 8 | +import 'package:file/local.dart'; |
| 9 | +import 'package:http/http.dart' as http; |
| 10 | +import 'package:logging/logging.dart'; |
| 11 | +import 'package:process/process.dart'; |
| 12 | +import 'package:pub_semver/pub_semver.dart'; |
| 13 | + |
| 14 | +const fileSystem = LocalFileSystem(); |
| 15 | +const processManager = LocalProcessManager(); |
| 16 | + |
| 17 | +Future<void> main() async { |
| 18 | + Logger.root.level = Level.ALL; |
| 19 | + Logger.root.onRecord.listen((record) { |
| 20 | + print( |
| 21 | + '[${record.loggerName.split('.').last}] ${record.level}: ${record.message}', |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + const sdkFinder = DartSdkFinder(); |
| 26 | + final sdk = (await sdkFinder.findSdk()).sdk; |
| 27 | + |
| 28 | + const helloWorld = r''' |
| 29 | +import 'dart:io'; |
| 30 | +
|
| 31 | +Future<void> main() async { |
| 32 | + final port = int.parse(Platform.environment['PORT'] ?? '8080'); |
| 33 | + final server = await HttpServer.bind(InternetAddress.anyIPv4, port); |
| 34 | + print('Listening on http://localhost:$port'); |
| 35 | + await for (final request in server) { |
| 36 | + request.response.write('Hello, world!'); |
| 37 | + await request.response.close(); |
| 38 | + } |
| 39 | +} |
| 40 | +'''; |
| 41 | + final tmpDir = fileSystem.systemTempDirectory.createTempSync('celest_').path; |
| 42 | + await fileSystem |
| 43 | + .directory(tmpDir) |
| 44 | + .childFile('hello_world.dart') |
| 45 | + .writeAsString(helloWorld); |
| 46 | + |
| 47 | + print('Compiling server'); |
| 48 | + final res = await processManager.run([ |
| 49 | + sdk.dartAotRuntime, |
| 50 | + sdk.frontendServerAotSnapshot, |
| 51 | + '--sdk-root', |
| 52 | + sdk.sdkPath, |
| 53 | + '--platform', |
| 54 | + sdk.vmPlatformProductDill, |
| 55 | + '--aot', |
| 56 | + '--tfa', |
| 57 | + '--no-support-mirrors', |
| 58 | + '--link-platform', |
| 59 | + '--target=vm', |
| 60 | + '-Ddart.vm.product=true', |
| 61 | + '--output-dill=$tmpDir/celest.aot.dill', |
| 62 | + '$tmpDir/hello_world.dart', |
| 63 | + ]); |
| 64 | + if (res.exitCode != 0) { |
| 65 | + throw Exception('Failed to compile hello_world.dart:\n${res.stderr}'); |
| 66 | + } |
| 67 | + final bytes = await fileSystem.file('$tmpDir/celest.aot.dill').readAsBytes(); |
| 68 | + |
| 69 | + final db = CloudHubDatabase.memory(); |
| 70 | + |
| 71 | + print('Creating organization'); |
| 72 | + final orgId = typeId('org'); |
| 73 | + await db.organizationsDrift.createOrganization( |
| 74 | + id: orgId, |
| 75 | + organizationId: 'my-org', |
| 76 | + state: 'ACTIVE', |
| 77 | + displayName: 'My Organization', |
| 78 | + ); |
| 79 | + |
| 80 | + print('Creating project'); |
| 81 | + final projectId = typeId('prj'); |
| 82 | + await db.projectsDrift.createProject( |
| 83 | + id: projectId, |
| 84 | + parentType: 'Celest::Organization', |
| 85 | + parentId: orgId, |
| 86 | + projectId: 'my-project', |
| 87 | + state: 'ACTIVE', |
| 88 | + regions: '', |
| 89 | + ); |
| 90 | + |
| 91 | + print('Creating environment'); |
| 92 | + final environmentId = typeId('env'); |
| 93 | + final environment = |
| 94 | + (await db.projectEnvironmentsDrift.createProjectEnvironment( |
| 95 | + id: environmentId, |
| 96 | + parentType: 'Celest::Project', |
| 97 | + parentId: projectId, |
| 98 | + projectEnvironmentId: 'production', |
| 99 | + state: 'CREATING', |
| 100 | + )).first; |
| 101 | + final flyApiToken = Platform.environment['FLY_API_TOKEN']!; |
| 102 | + final deploymentEngine = FlyDeploymentEngine( |
| 103 | + db: db, |
| 104 | + flyApiToken: flyApiToken, |
| 105 | + projectAst: ast.ResolvedProject( |
| 106 | + projectId: 'my-project', |
| 107 | + environmentId: 'production', |
| 108 | + sdkConfig: ast.SdkConfiguration( |
| 109 | + celest: Version(1, 0, 9), |
| 110 | + dart: ast.Sdk(type: ast.SdkType.dart, version: sdk.version), |
| 111 | + ), |
| 112 | + ), |
| 113 | + kernelAsset: bytes, |
| 114 | + environment: environment, |
| 115 | + ); |
| 116 | + |
| 117 | + print('Deploying'); |
| 118 | + final state = await deploymentEngine.deploy(); |
| 119 | + deploymentEngine.close(); |
| 120 | + |
| 121 | + final uri = Uri.parse('https://${state.domainName}'); |
| 122 | + final response = await http.get(uri); |
| 123 | + if (response.statusCode != 200) { |
| 124 | + throw http.ClientException( |
| 125 | + 'Bad response: ${response.statusCode} ${response.body}', |
| 126 | + uri, |
| 127 | + ); |
| 128 | + } |
| 129 | + print(response.body); |
| 130 | +} |
0 commit comments