chore: Implement server installation on businness logic layer for hetzner

pull/213/head
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,42 +223,30 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
emit((state as ServerInstallationNotFinished).copyWith(rootUser: rootUser)); emit((state as ServerInstallationNotFinished).copyWith(rootUser: rootUser));
} }
Future<void> onCreateServerSuccess(
final ServerHostingDetails serverDetails,
) async {
await repository.saveServerDetails(serverDetails);
// TODO dns;
emit(
(state as ServerInstallationNotFinished).copyWith(
isLoading: false,
serverDetails: serverDetails,
),
);
runDelayed(startServerIfDnsIsOkay, const Duration(seconds: 30), null);
}
void createServerAndSetDnsRecords() async { void createServerAndSetDnsRecords() async {
final ServerInstallationNotFinished stateCopy = emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
state as ServerInstallationNotFinished; await repository.createServer(
void onCancel() => emit( state.rootUser!,
(state as ServerInstallationNotFinished).copyWith(isLoading: false), state.serverDomain!.domainName,
); state.dnsApiToken!,
state.backblazeCredential!,
Future<void> onSuccess(final ServerHostingDetails serverDetails) async { onCancel: clearAppConfig,
await repository.createDnsRecords( onSuccess: onCreateServerSuccess,
serverDetails, );
state.serverDomain!,
onCancel: onCancel,
);
emit(
(state as ServerInstallationNotFinished).copyWith(
isLoading: false,
serverDetails: serverDetails,
),
);
runDelayed(startServerIfDnsIsOkay, const Duration(seconds: 30), null);
}
try {
emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
await repository.createServer(
state.rootUser!,
state.serverDomain!.domainName,
state.dnsApiToken!,
state.backblazeCredential!,
onCancel: onCancel,
onSuccess: onSuccess,
);
} 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(