mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-25 18:26:36 +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/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.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/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/providers/provider_settings.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_api_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';
|
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/cubit/server_installation/server_installation_repository.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||||
|
import 'package:selfprivacy/ui/helpers/modals.dart';
|
||||||
|
|
||||||
export 'package:provider/provider.dart';
|
export 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
@ -239,14 +242,52 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
||||||
|
|
||||||
void createServerAndSetDnsRecords() async {
|
void createServerAndSetDnsRecords() async {
|
||||||
emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
|
emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
|
||||||
await repository.createServer(
|
|
||||||
state.rootUser!,
|
final installationData = LaunchInstallationData(
|
||||||
state.serverDomain!.domainName,
|
rootUser: state.rootUser!,
|
||||||
state.dnsApiToken!,
|
dnsApiToken: state.dnsApiToken!,
|
||||||
state.backblazeCredential!,
|
dnsProviderType: state.serverDomain!.provider,
|
||||||
onCancel: clearAppConfig,
|
domainName: state.serverDomain!.domainName,
|
||||||
onSuccess: onCreateServerSuccess,
|
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({
|
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/message.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_type.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/ui/helpers/modals.dart';
|
||||||
import 'package:selfprivacy/utils/network_utils.dart';
|
import 'package:selfprivacy/utils/network_utils.dart';
|
||||||
|
|
||||||
|
@ -168,13 +169,15 @@ class ServerInstallationRepository {
|
||||||
Future<ServerHostingDetails> startServer(
|
Future<ServerHostingDetails> startServer(
|
||||||
final ServerHostingDetails server,
|
final ServerHostingDetails server,
|
||||||
) async {
|
) async {
|
||||||
ServerHostingDetails serverDetails;
|
final result = await ProvidersController.currentServerProvider!.powerOn(
|
||||||
|
server.id,
|
||||||
|
);
|
||||||
|
|
||||||
serverDetails = await ApiController.currentServerProviderApiFactory!
|
if (result.success && result.data != null) {
|
||||||
.getServerProvider()
|
server.copyWith(startTime: result.data);
|
||||||
.powerOn();
|
}
|
||||||
|
|
||||||
return serverDetails;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> getDomainId(final String token, final String domain) async {
|
Future<String?> getDomainId(final String token, final String domain) async {
|
||||||
|
@ -239,108 +242,6 @@ class ServerInstallationRepository {
|
||||||
return matches;
|
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(
|
Future<bool> createDnsRecords(
|
||||||
final ServerHostingDetails serverDetails,
|
final ServerHostingDetails serverDetails,
|
||||||
final ServerDomain domain, {
|
final ServerDomain domain, {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
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';
|
|
||||||
|
|
||||||
class LaunchInstallationData {
|
class LaunchInstallationData {
|
||||||
LaunchInstallationData({
|
LaunchInstallationData({
|
||||||
|
@ -9,7 +8,7 @@ class LaunchInstallationData {
|
||||||
required this.dnsApiToken,
|
required this.dnsApiToken,
|
||||||
required this.dnsProviderType,
|
required this.dnsProviderType,
|
||||||
required this.domainName,
|
required this.domainName,
|
||||||
required this.serverType,
|
required this.serverTypeId,
|
||||||
required this.errorCallback,
|
required this.errorCallback,
|
||||||
required this.successCallback,
|
required this.successCallback,
|
||||||
});
|
});
|
||||||
|
@ -18,7 +17,7 @@ class LaunchInstallationData {
|
||||||
final String dnsApiToken;
|
final String dnsApiToken;
|
||||||
final String domainName;
|
final String domainName;
|
||||||
final DnsProviderType dnsProviderType;
|
final DnsProviderType dnsProviderType;
|
||||||
final ServerType serverType;
|
final String serverTypeId;
|
||||||
final Function() errorCallback;
|
final Function() errorCallback;
|
||||||
final Function(ServerHostingDetails details) successCallback;
|
final Function(ServerHostingDetails details) successCallback;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
import 'package:selfprivacy/logic/api_maps/generic_result.dart';
|
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_provider_location.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||||
|
|
||||||
|
@ -11,6 +13,11 @@ abstract class ServerProvider {
|
||||||
Future<GenericResult<List<ServerType>>> getServerTypes({
|
Future<GenericResult<List<ServerType>>> getServerTypes({
|
||||||
required final ServerProviderLocation location,
|
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);
|
GenericResult<bool> get success => GenericResult(success: true, data: true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,6 +359,7 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
return GenericResult(data: metrics, success: true);
|
return GenericResult(data: metrics, success: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
Future<GenericResult<DateTime?>> restart(final int serverId) async {
|
Future<GenericResult<DateTime?>> restart(final int serverId) async {
|
||||||
DateTime? timestamp;
|
DateTime? timestamp;
|
||||||
final result = await _adapter.api().restart(serverId);
|
final result = await _adapter.api().restart(serverId);
|
||||||
|
@ -379,6 +380,7 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
Future<GenericResult<DateTime?>> powerOn(final int serverId) async {
|
Future<GenericResult<DateTime?>> powerOn(final int serverId) async {
|
||||||
DateTime? timestamp;
|
DateTime? timestamp;
|
||||||
final result = await _adapter.api().powerOn(serverId);
|
final result = await _adapter.api().powerOn(serverId);
|
||||||
|
@ -413,6 +415,7 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
return dnsProviderType;
|
return dnsProviderType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
Future<GenericResult<CallbackDialogueBranching?>> launchInstallation(
|
Future<GenericResult<CallbackDialogueBranching?>> launchInstallation(
|
||||||
final LaunchInstallationData installationData,
|
final LaunchInstallationData installationData,
|
||||||
) async {
|
) async {
|
||||||
|
@ -449,7 +452,7 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
dnsApiToken: installationData.dnsApiToken,
|
dnsApiToken: installationData.dnsApiToken,
|
||||||
rootUser: installationData.rootUser,
|
rootUser: installationData.rootUser,
|
||||||
domainName: installationData.domainName,
|
domainName: installationData.domainName,
|
||||||
serverType: installationData.serverType.identifier,
|
serverType: installationData.serverTypeId,
|
||||||
dnsProviderType:
|
dnsProviderType:
|
||||||
dnsProviderToInfectName(installationData.dnsProviderType),
|
dnsProviderToInfectName(installationData.dnsProviderType),
|
||||||
hostName: hostname,
|
hostName: hostname,
|
||||||
|
|
Loading…
Reference in a new issue