mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 09:16:54 +00:00
chore: Adapt MetricsRepository to new ProvidersControllers model
This commit is contained in:
parent
62c0030f8e
commit
97e9e9d9cb
|
@ -1,23 +1,16 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/volume_provider.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/staging_options.dart';
|
||||
import 'package:selfprivacy/logic/models/disk_size.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/models/metrics.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||
import 'package:selfprivacy/logic/models/server_metadata.dart';
|
||||
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
||||
import 'package:selfprivacy/utils/extensions/string_extensions.dart';
|
||||
import 'package:selfprivacy/utils/network_utils.dart';
|
||||
import 'package:selfprivacy/utils/password_generator.dart';
|
||||
|
||||
|
@ -353,10 +346,10 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
required final String serverType,
|
||||
}) async {
|
||||
final String stagingAcme = StagingOptions.stagingAcme ? 'true' : 'false';
|
||||
|
||||
Response? serverCreateResponse;
|
||||
DioError? hetznerError;
|
||||
bool success = false;
|
||||
|
||||
final Dio client = await getClient();
|
||||
try {
|
||||
final Map<String, Object> data = {
|
||||
|
@ -405,94 +398,6 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
);
|
||||
}
|
||||
|
||||
Future<GenericResult<ServerHostingDetails?>> skldfjalkdsjflkasd({
|
||||
required final String dnsApiToken,
|
||||
required final User rootUser,
|
||||
required final String domainName,
|
||||
required final ServerVolume volume,
|
||||
required final String serverType,
|
||||
required final DnsProviderType dnsProvider,
|
||||
}) async {
|
||||
final String dbPassword = StringGenerators.dbPassword();
|
||||
final int volumeId = volume.id;
|
||||
|
||||
final String apiToken = StringGenerators.apiToken();
|
||||
final String hostname = getHostnameFromDomain(domainName);
|
||||
const String infectBranch = 'providers/hetzner';
|
||||
final String stagingAcme = StagingOptions.stagingAcme ? 'true' : 'false';
|
||||
final String base64Password =
|
||||
base64.encode(utf8.encode(rootUser.password ?? 'PASS'));
|
||||
final String dnsProviderType = dnsProviderToInfectName(dnsProvider);
|
||||
|
||||
Response? serverCreateResponse;
|
||||
ServerHostingDetails? serverDetails;
|
||||
DioError? hetznerError;
|
||||
bool success = false;
|
||||
final Dio client = await getClient();
|
||||
try {
|
||||
final Map<String, Object> data = {
|
||||
'name': hostname,
|
||||
'server_type': serverType,
|
||||
'start_after_create': false,
|
||||
'image': 'ubuntu-20.04',
|
||||
'volumes': [volumeId],
|
||||
'networks': [],
|
||||
'user_data': '#cloud-config\n'
|
||||
'runcmd:\n'
|
||||
'- curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/$infectBranch/nixos-infect | '
|
||||
"STAGING_ACME='$stagingAcme' PROVIDER=$infectProviderName DNS_PROVIDER_TYPE=$dnsProviderType "
|
||||
"NIX_CHANNEL=nixos-21.05 DOMAIN='$domainName' LUSER='${rootUser.login}' ENCODED_PASSWORD='$base64Password' "
|
||||
'CF_TOKEN=$dnsApiToken DB_PASSWORD=$dbPassword API_TOKEN=$apiToken HOSTNAME=$hostname bash 2>&1 | '
|
||||
'tee /tmp/infect.log',
|
||||
'labels': {},
|
||||
'automount': true,
|
||||
'location': region!,
|
||||
};
|
||||
print('Decoded data: $data');
|
||||
|
||||
serverCreateResponse = await client.post(
|
||||
'/servers',
|
||||
data: data,
|
||||
);
|
||||
print(serverCreateResponse.data);
|
||||
serverDetails = ServerHostingDetails(
|
||||
id: serverCreateResponse.data['server']['id'],
|
||||
ip4: serverCreateResponse.data['server']['public_net']['ipv4']['ip'],
|
||||
createTime: DateTime.now(),
|
||||
volume: volume,
|
||||
apiToken: apiToken,
|
||||
provider: ServerProviderType.hetzner,
|
||||
);
|
||||
success = true;
|
||||
} on DioError catch (e) {
|
||||
print(e);
|
||||
hetznerError = e;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
await Future.delayed(const Duration(seconds: 10));
|
||||
await deleteVolume(volume);
|
||||
}
|
||||
|
||||
String? apiResultMessage = serverCreateResponse?.statusMessage;
|
||||
if (hetznerError != null &&
|
||||
hetznerError.response!.data['error']['code'] == 'uniqueness_error') {
|
||||
apiResultMessage = 'uniqueness_error';
|
||||
}
|
||||
|
||||
return GenericResult(
|
||||
data: serverDetails,
|
||||
success: success && hetznerError == null,
|
||||
code: serverCreateResponse?.statusCode ??
|
||||
hetznerError?.response?.statusCode,
|
||||
message: apiResultMessage,
|
||||
);
|
||||
}
|
||||
|
||||
Future<GenericResult<bool>> deleteServer({
|
||||
required final String domainName,
|
||||
}) async {
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||
|
||||
import 'package:selfprivacy/logic/cubit/metrics/metrics_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/metrics.dart';
|
||||
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
||||
|
||||
class MetricsLoadException implements Exception {
|
||||
MetricsLoadException(this.message);
|
||||
|
@ -12,8 +11,7 @@ class MetricsLoadException implements Exception {
|
|||
|
||||
class MetricsRepository {
|
||||
Future<MetricsLoaded> getMetrics(final Period period) async {
|
||||
final providerApiFactory = ApiController.currentServerProviderApiFactory;
|
||||
if (providerApiFactory == null) {
|
||||
if (ProvidersController.currentServerProvider == null) {
|
||||
throw MetricsLoadException('Server Provider data is null');
|
||||
}
|
||||
|
||||
|
@ -33,20 +31,19 @@ class MetricsRepository {
|
|||
}
|
||||
|
||||
final serverId = getIt<ApiConfigModel>().serverDetails!.id;
|
||||
final ServerMetrics? metrics =
|
||||
await providerApiFactory.getServerProvider().getMetrics(
|
||||
final result = await ProvidersController.currentServerProvider!.getMetrics(
|
||||
serverId,
|
||||
start,
|
||||
end,
|
||||
);
|
||||
|
||||
if (metrics == null) {
|
||||
if (result.data == null || !result.success) {
|
||||
throw MetricsLoadException('Metrics data is null');
|
||||
}
|
||||
|
||||
return MetricsLoaded(
|
||||
period: period,
|
||||
metrics: metrics,
|
||||
metrics: result.data!,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
Future<List<ServerType>> fetchAvailableTypesByLocation(
|
||||
final ServerProviderLocation location,
|
||||
) async {
|
||||
if (ApiController.currentServerProviderApiFactory == null) {
|
||||
if (ProvidersController.currentServerProvider == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@ -754,6 +754,7 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
void clearAppConfig() {
|
||||
closeTimer();
|
||||
ApiController.clearProviderApiFactories();
|
||||
ProvidersController.clearProviders();
|
||||
repository.clearAppConfig();
|
||||
emit(const ServerInstallationEmpty());
|
||||
}
|
||||
|
|
|
@ -332,15 +332,24 @@ class ServerInstallationRepository {
|
|||
return api.isHttpServerWorking();
|
||||
}
|
||||
|
||||
Future<ServerHostingDetails> restart() async =>
|
||||
ApiController.currentServerProviderApiFactory!
|
||||
.getServerProvider()
|
||||
.restart();
|
||||
Future<ServerHostingDetails> restart() async {
|
||||
final server = getIt<ApiConfigModel>().serverDetails!;
|
||||
|
||||
Future<ServerHostingDetails> powerOn() async =>
|
||||
ApiController.currentServerProviderApiFactory!
|
||||
.getServerProvider()
|
||||
.powerOn();
|
||||
final result = await ProvidersController.currentServerProvider!.restart(
|
||||
server.id,
|
||||
);
|
||||
|
||||
if (result.success && result.data != null) {
|
||||
server.copyWith(startTime: result.data);
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
Future<ServerHostingDetails> powerOn() async {
|
||||
final server = getIt<ApiConfigModel>().serverDetails!;
|
||||
return startServer(server);
|
||||
}
|
||||
|
||||
Future<ServerRecoveryCapabilities> getRecoveryCapabilities(
|
||||
final ServerDomain serverDomain,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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/metrics.dart';
|
||||
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
||||
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||
|
||||
|
@ -18,6 +19,11 @@ abstract class ServerProvider {
|
|||
);
|
||||
Future<GenericResult<DateTime?>> powerOn(final int serverId);
|
||||
Future<GenericResult<DateTime?>> restart(final int serverId);
|
||||
Future<GenericResult<ServerMetrics?>> getMetrics(
|
||||
final int serverId,
|
||||
final DateTime start,
|
||||
final DateTime end,
|
||||
);
|
||||
|
||||
GenericResult<bool> get success => GenericResult(success: true, data: true);
|
||||
}
|
||||
|
|
|
@ -289,6 +289,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
return GenericResult(success: true, data: metadata);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<GenericResult<ServerMetrics?>> getMetrics(
|
||||
final int serverId,
|
||||
final DateTime start,
|
||||
|
|
Loading…
Reference in a new issue