mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-11 10:29:39 +00:00
chore: Implement server installation logic on cubit layer
This commit is contained in:
parent
ef04b5bf57
commit
62c0030f8e
|
@ -6,6 +6,8 @@ import 'package:equatable/equatable.dart';
|
|||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
||||
import 'package:selfprivacy/logic/models/callback_dialogue_branching.dart';
|
||||
import 'package:selfprivacy/logic/models/launch_installation_data.dart';
|
||||
import 'package:selfprivacy/logic/providers/provider_settings.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_api_settings.dart';
|
||||
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
||||
|
@ -18,6 +20,7 @@ import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
|||
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_repository.dart';
|
||||
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||
import 'package:selfprivacy/ui/helpers/modals.dart';
|
||||
|
||||
export 'package:provider/provider.dart';
|
||||
|
||||
|
@ -239,14 +242,52 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
|
||||
void createServerAndSetDnsRecords() async {
|
||||
emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
|
||||
await repository.createServer(
|
||||
state.rootUser!,
|
||||
state.serverDomain!.domainName,
|
||||
state.dnsApiToken!,
|
||||
state.backblazeCredential!,
|
||||
onCancel: clearAppConfig,
|
||||
onSuccess: onCreateServerSuccess,
|
||||
|
||||
final installationData = LaunchInstallationData(
|
||||
rootUser: state.rootUser!,
|
||||
dnsApiToken: state.dnsApiToken!,
|
||||
dnsProviderType: state.serverDomain!.provider,
|
||||
domainName: state.serverDomain!.domainName,
|
||||
serverTypeId: state.serverTypeIdentificator!,
|
||||
errorCallback: clearAppConfig,
|
||||
successCallback: onCreateServerSuccess,
|
||||
);
|
||||
|
||||
final result =
|
||||
await ProvidersController.currentServerProvider!.launchInstallation(
|
||||
installationData,
|
||||
);
|
||||
|
||||
if (!result.success && result.data != null) {
|
||||
bool dialoguesResolved = false;
|
||||
CallbackDialogueBranching branching = result.data!;
|
||||
while (!dialoguesResolved) {
|
||||
showPopUpAlert(
|
||||
alertTitle: branching.title,
|
||||
description: branching.description,
|
||||
actionButtonTitle: branching.choices[1].title,
|
||||
actionButtonOnPressed: () async {
|
||||
final branchingResult = await branching.choices[1].callback!();
|
||||
if (branchingResult.data == null) {
|
||||
dialoguesResolved = true;
|
||||
return;
|
||||
}
|
||||
|
||||
branching = branchingResult.data!;
|
||||
},
|
||||
cancelButtonTitle: branching.choices[0].title,
|
||||
cancelButtonOnPressed: () async {
|
||||
final branchingResult = await branching.choices[0].callback!();
|
||||
if (branchingResult.data == null) {
|
||||
dialoguesResolved = true;
|
||||
return;
|
||||
}
|
||||
|
||||
branching = branchingResult.data!;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startServerIfDnsIsOkay({
|
||||
|
|
|
@ -25,6 +25,7 @@ import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
|||
import 'package:selfprivacy/logic/models/message.dart';
|
||||
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
||||
import 'package:selfprivacy/ui/helpers/modals.dart';
|
||||
import 'package:selfprivacy/utils/network_utils.dart';
|
||||
|
||||
|
@ -168,13 +169,15 @@ class ServerInstallationRepository {
|
|||
Future<ServerHostingDetails> startServer(
|
||||
final ServerHostingDetails server,
|
||||
) async {
|
||||
ServerHostingDetails serverDetails;
|
||||
final result = await ProvidersController.currentServerProvider!.powerOn(
|
||||
server.id,
|
||||
);
|
||||
|
||||
serverDetails = await ApiController.currentServerProviderApiFactory!
|
||||
.getServerProvider()
|
||||
.powerOn();
|
||||
if (result.success && result.data != null) {
|
||||
server.copyWith(startTime: result.data);
|
||||
}
|
||||
|
||||
return serverDetails;
|
||||
return server;
|
||||
}
|
||||
|
||||
Future<String?> getDomainId(final String token, final String domain) async {
|
||||
|
@ -239,108 +242,6 @@ class ServerInstallationRepository {
|
|||
return matches;
|
||||
}
|
||||
|
||||
Future<void> createServer(
|
||||
final User rootUser,
|
||||
final String domainName,
|
||||
final String cloudFlareKey,
|
||||
final BackblazeCredential backblazeCredential, {
|
||||
required final void Function() onCancel,
|
||||
required final Future<void> Function(ServerHostingDetails serverDetails)
|
||||
onSuccess,
|
||||
}) async {
|
||||
final ServerProviderApi api =
|
||||
ApiController.currentServerProviderApiFactory!.getServerProvider();
|
||||
|
||||
void showInstallationErrorPopUp() {
|
||||
showPopUpAlert(
|
||||
alertTitle: 'modals.unexpected_error'.tr(),
|
||||
description: 'modals.try_again'.tr(),
|
||||
actionButtonTitle: 'modals.yes'.tr(),
|
||||
actionButtonOnPressed: () async {
|
||||
ServerHostingDetails? serverDetails;
|
||||
try {
|
||||
final GenericResult createResult = await api.createServer(
|
||||
dnsProvider: getIt<ApiConfigModel>().dnsProvider!,
|
||||
dnsApiToken: cloudFlareKey,
|
||||
rootUser: rootUser,
|
||||
domainName: domainName,
|
||||
serverType: getIt<ApiConfigModel>().serverType!,
|
||||
);
|
||||
serverDetails = createResult.data;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
if (serverDetails == null) {
|
||||
print('Server is not initialized!');
|
||||
return;
|
||||
}
|
||||
await saveServerDetails(serverDetails);
|
||||
onSuccess(serverDetails);
|
||||
},
|
||||
cancelButtonOnPressed: onCancel,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
final GenericResult<ServerHostingDetails?> createServerResult =
|
||||
await api.createServer(
|
||||
dnsProvider: getIt<ApiConfigModel>().dnsProvider!,
|
||||
dnsApiToken: cloudFlareKey,
|
||||
rootUser: rootUser,
|
||||
domainName: domainName,
|
||||
serverType: getIt<ApiConfigModel>().serverType!,
|
||||
);
|
||||
|
||||
if (createServerResult.data == null) {
|
||||
const String e = 'Server is not initialized!';
|
||||
print(e);
|
||||
}
|
||||
|
||||
if (createServerResult.message == 'uniqueness_error') {
|
||||
showPopUpAlert(
|
||||
alertTitle: 'modals.already_exists'.tr(),
|
||||
description: 'modals.destroy_server'.tr(),
|
||||
actionButtonTitle: 'modals.yes'.tr(),
|
||||
actionButtonOnPressed: () async {
|
||||
await api.deleteServer(
|
||||
domainName: domainName,
|
||||
);
|
||||
|
||||
ServerHostingDetails? serverDetails;
|
||||
try {
|
||||
final GenericResult createResult = await api.createServer(
|
||||
dnsProvider: getIt<ApiConfigModel>().dnsProvider!,
|
||||
dnsApiToken: cloudFlareKey,
|
||||
rootUser: rootUser,
|
||||
domainName: domainName,
|
||||
serverType: getIt<ApiConfigModel>().serverType!,
|
||||
);
|
||||
serverDetails = createResult.data;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
if (serverDetails == null) {
|
||||
print('Server is not initialized!');
|
||||
return;
|
||||
}
|
||||
await saveServerDetails(serverDetails);
|
||||
onSuccess(serverDetails);
|
||||
},
|
||||
cancelButtonOnPressed: onCancel,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
saveServerDetails(createServerResult.data!);
|
||||
onSuccess(createServerResult.data!);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
showInstallationErrorPopUp();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> createDnsRecords(
|
||||
final ServerHostingDetails serverDetails,
|
||||
final ServerDomain domain, {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||
|
||||
class LaunchInstallationData {
|
||||
LaunchInstallationData({
|
||||
|
@ -9,7 +8,7 @@ class LaunchInstallationData {
|
|||
required this.dnsApiToken,
|
||||
required this.dnsProviderType,
|
||||
required this.domainName,
|
||||
required this.serverType,
|
||||
required this.serverTypeId,
|
||||
required this.errorCallback,
|
||||
required this.successCallback,
|
||||
});
|
||||
|
@ -18,7 +17,7 @@ class LaunchInstallationData {
|
|||
final String dnsApiToken;
|
||||
final String domainName;
|
||||
final DnsProviderType dnsProviderType;
|
||||
final ServerType serverType;
|
||||
final String serverTypeId;
|
||||
final Function() errorCallback;
|
||||
final Function(ServerHostingDetails details) successCallback;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import 'package:selfprivacy/logic/api_maps/generic_result.dart';
|
||||
import 'package:selfprivacy/logic/models/callback_dialogue_branching.dart';
|
||||
import 'package:selfprivacy/logic/models/launch_installation_data.dart';
|
||||
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||
|
||||
|
@ -11,6 +13,11 @@ abstract class ServerProvider {
|
|||
Future<GenericResult<List<ServerType>>> getServerTypes({
|
||||
required final ServerProviderLocation location,
|
||||
});
|
||||
Future<GenericResult<CallbackDialogueBranching?>> launchInstallation(
|
||||
final LaunchInstallationData installationData,
|
||||
);
|
||||
Future<GenericResult<DateTime?>> powerOn(final int serverId);
|
||||
Future<GenericResult<DateTime?>> restart(final int serverId);
|
||||
|
||||
GenericResult<bool> get success => GenericResult(success: true, data: true);
|
||||
}
|
||||
|
|
|
@ -359,6 +359,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
return GenericResult(data: metrics, success: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<GenericResult<DateTime?>> restart(final int serverId) async {
|
||||
DateTime? timestamp;
|
||||
final result = await _adapter.api().restart(serverId);
|
||||
|
@ -379,6 +380,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<GenericResult<DateTime?>> powerOn(final int serverId) async {
|
||||
DateTime? timestamp;
|
||||
final result = await _adapter.api().powerOn(serverId);
|
||||
|
@ -413,6 +415,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
return dnsProviderType;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<GenericResult<CallbackDialogueBranching?>> launchInstallation(
|
||||
final LaunchInstallationData installationData,
|
||||
) async {
|
||||
|
@ -449,7 +452,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
dnsApiToken: installationData.dnsApiToken,
|
||||
rootUser: installationData.rootUser,
|
||||
domainName: installationData.domainName,
|
||||
serverType: installationData.serverType.identifier,
|
||||
serverType: installationData.serverTypeId,
|
||||
dnsProviderType:
|
||||
dnsProviderToInfectName(installationData.dnsProviderType),
|
||||
hostName: hostname,
|
||||
|
|
Loading…
Reference in a new issue