chore: Implement server installation on businness logic layer for hetzner

This commit is contained in:
NaiJi 2023-02-22 21:03:58 +04:00
parent 8da7341ccb
commit ef04b5bf57
3 changed files with 53 additions and 40 deletions

View file

@ -172,7 +172,7 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
await repository.saveServerType(serverType); await repository.saveServerType(serverType);
await ProvidersController.currentServerProvider! await ProvidersController.currentServerProvider!
.trySetServerLocation(serverType); .trySetServerLocation(serverType.identifier);
emit( emit(
(state as ServerInstallationNotFinished).copyWith( (state as ServerInstallationNotFinished).copyWith(
@ -223,20 +223,11 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
emit((state as ServerInstallationNotFinished).copyWith(rootUser: rootUser)); emit((state as ServerInstallationNotFinished).copyWith(rootUser: rootUser));
} }
void createServerAndSetDnsRecords() async { Future<void> onCreateServerSuccess(
final ServerInstallationNotFinished stateCopy = final ServerHostingDetails serverDetails,
state as ServerInstallationNotFinished; ) async {
void onCancel() => emit( await repository.saveServerDetails(serverDetails);
(state as ServerInstallationNotFinished).copyWith(isLoading: false), // TODO dns;
);
Future<void> onSuccess(final ServerHostingDetails serverDetails) async {
await repository.createDnsRecords(
serverDetails,
state.serverDomain!,
onCancel: onCancel,
);
emit( emit(
(state as ServerInstallationNotFinished).copyWith( (state as ServerInstallationNotFinished).copyWith(
isLoading: false, isLoading: false,
@ -246,19 +237,16 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
runDelayed(startServerIfDnsIsOkay, const Duration(seconds: 30), null); runDelayed(startServerIfDnsIsOkay, const Duration(seconds: 30), null);
} }
try { void createServerAndSetDnsRecords() async {
emit((state as ServerInstallationNotFinished).copyWith(isLoading: true)); emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
await repository.createServer( await repository.createServer(
state.rootUser!, state.rootUser!,
state.serverDomain!.domainName, state.serverDomain!.domainName,
state.dnsApiToken!, state.dnsApiToken!,
state.backblazeCredential!, state.backblazeCredential!,
onCancel: onCancel, onCancel: clearAppConfig,
onSuccess: onSuccess, onSuccess: onCreateServerSuccess,
); );
} catch (e) {
emit(stateCopy);
}
} }
void startServerIfDnsIsOkay({ void startServerIfDnsIsOkay({

View file

@ -1,3 +1,4 @@
import 'package:selfprivacy/logic/models/hive/server_details.dart';
import 'package:selfprivacy/logic/models/hive/server_domain.dart'; import 'package:selfprivacy/logic/models/hive/server_domain.dart';
import 'package:selfprivacy/logic/models/hive/user.dart'; import 'package:selfprivacy/logic/models/hive/user.dart';
import 'package:selfprivacy/logic/models/server_type.dart'; import 'package:selfprivacy/logic/models/server_type.dart';
@ -9,6 +10,8 @@ class LaunchInstallationData {
required this.dnsProviderType, required this.dnsProviderType,
required this.domainName, required this.domainName,
required this.serverType, required this.serverType,
required this.errorCallback,
required this.successCallback,
}); });
final User rootUser; final User rootUser;
@ -16,4 +19,6 @@ class LaunchInstallationData {
final String domainName; final String domainName;
final DnsProviderType dnsProviderType; final DnsProviderType dnsProviderType;
final ServerType serverType; final ServerType serverType;
final Function() errorCallback;
final Function(ServerHostingDetails details) successCallback;
} }

View file

@ -424,7 +424,7 @@ class HetznerServerProvider extends ServerProvider {
choices: [ choices: [
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.cancel'.tr(), title: 'basis.cancel'.tr(),
callback: null, callback: await installationData.errorCallback(),
), ),
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.try_again'.tr(), title: 'basis.try_again'.tr(),
@ -471,7 +471,7 @@ class HetznerServerProvider extends ServerProvider {
choices: [ choices: [
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.cancel'.tr(), title: 'basis.cancel'.tr(),
callback: null, callback: installationData.errorCallback(),
), ),
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.yes'.tr(), title: 'basis.yes'.tr(),
@ -498,7 +498,14 @@ class HetznerServerProvider extends ServerProvider {
choices: [ choices: [
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.cancel'.tr(), title: 'basis.cancel'.tr(),
callback: null, callback: () async {
final deletion = await deleteServer(hostname);
if (deletion.success) {
installationData.errorCallback();
}
return deletion;
},
), ),
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.try_again'.tr(), title: 'basis.try_again'.tr(),
@ -537,13 +544,23 @@ class HetznerServerProvider extends ServerProvider {
choices: [ choices: [
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.cancel'.tr(), title: 'basis.cancel'.tr(),
callback: null, callback: () async {
final deletion = await deleteServer(hostname);
if (deletion.success) {
installationData.errorCallback();
}
return deletion;
},
), ),
CallbackDialogueChoice( CallbackDialogueChoice(
title: 'basis.try_again'.tr(), title: 'basis.try_again'.tr(),
callback: () async { callback: () async {
await _adapter.api().deleteVolume(volume);
await Future.delayed(const Duration(seconds: 5));
final deletion = await deleteServer(hostname); final deletion = await deleteServer(hostname);
if (deletion.success) { if (deletion.success) {
await Future.delayed(const Duration(seconds: 5));
return launchInstallation(installationData); return launchInstallation(installationData);
} }
@ -559,6 +576,9 @@ class HetznerServerProvider extends ServerProvider {
code: volumeResult.code, code: volumeResult.code,
); );
} }
await installationData.successCallback(serverDetails);
return GenericResult(success: true, data: null);
} }
Future<GenericResult<CallbackDialogueBranching?>> deleteServer( Future<GenericResult<CallbackDialogueBranching?>> deleteServer(