diff --git a/server/src/core/startup/index.ts b/server/src/core/startup/index.ts index faf55737..61b380fb 100644 --- a/server/src/core/startup/index.ts +++ b/server/src/core/startup/index.ts @@ -3,10 +3,12 @@ import { getFromCache } from '../../data/cache'; import initRedisValues from '../../data/cache/defaults'; import { PlaybookModel } from '../../data/database/model/Playbook'; import PlaybooksRepositoryRepo from '../../data/database/repository/PlaybooksRepositoryRepo'; +import UserRepo from '../../data/database/repository/UserRepo'; import Crons from '../../integrations/crons'; import WatcherEngine from '../../integrations/docker/core/WatcherEngine'; import providerConf from '../../integrations/docker/registries/providers/provider.conf'; import PlaybooksRepositoryEngine from '../../integrations/playbooks-repository/PlaybooksRepositoryEngine'; +import Shell from '../../integrations/shell'; import logger from '../../logger'; import ContainerRegistryUseCases from '../../use-cases/ContainerRegistryUseCases'; import DeviceAuthUseCases from '../../use-cases/DeviceAuthUseCases'; @@ -44,6 +46,7 @@ async function init() { if (version !== SettingsKeys.DefaultValue.SCHEME_VERSION) { await migrate(); + await createADefaultLocalUserRepository(); logger.warn(`[CONFIGURATION] - Scheme version differed, starting writing updates`); await initRedisValues(); void setAnsibleVersion(); @@ -64,6 +67,23 @@ async function migrate() { } } +export async function createADefaultLocalUserRepository() { + const user = await UserRepo.findFirst(); + if (user) { + const userPlaybooksRepository = { + name: user?.email.trim().split('@')[0] || 'user-default', + enabled: true, + type: Playbooks.PlaybooksRepositoryType.LOCAL, + directory: '/playbooks/00000000-0000-0000-0000-000000000002', + uuid: '00000000-0000-0000-0000-000000000002', + }; + await PlaybooksRepositoryRepo.updateOrCreate(userPlaybooksRepository); + try { + Shell.FileSystemManager.createDirectory(userPlaybooksRepository.directory); + } catch (error: any) {} + } +} + export default { init, }; diff --git a/server/src/data/database/model/Playbook.ts b/server/src/data/database/model/Playbook.ts index 333e63e8..9756cfe3 100644 --- a/server/src/data/database/model/Playbook.ts +++ b/server/src/data/database/model/Playbook.ts @@ -31,6 +31,7 @@ const schema = new Schema( uniqueQuickRef: { type: Schema.Types.String, unique: true, + sparse: true, }, path: { type: Schema.Types.String, diff --git a/server/src/data/database/repository/UserRepo.ts b/server/src/data/database/repository/UserRepo.ts index 2e7090f8..877f1c61 100644 --- a/server/src/data/database/repository/UserRepo.ts +++ b/server/src/data/database/repository/UserRepo.ts @@ -42,6 +42,10 @@ async function updateLogsLevel(email: string, userLogsLevel: UserLogsLevel): Pro await UsersModel.updateOne({ email: email }, { logsLevel: userLogsLevel }).exec(); } +async function findFirst(): Promise { + return await UsersModel.findOne().lean().exec(); +} + export default { create, findByEmailAndPassword, @@ -50,4 +54,5 @@ export default { resetApiKey, updateLogsLevel, findByApiKey, + findFirst, }; diff --git a/server/src/services/user/user.ts b/server/src/services/user/user.ts index 44e6cc4f..6148a630 100644 --- a/server/src/services/user/user.ts +++ b/server/src/services/user/user.ts @@ -1,6 +1,7 @@ import { SettingsKeys } from 'ssm-shared-lib'; import { AuthFailureError } from '../../core/api/ApiError'; import { SuccessResponse } from '../../core/api/ApiResponse'; +import { createADefaultLocalUserRepository } from '../../core/startup'; import { getAnsibleVersion } from '../../core/system/version'; import { Role } from '../../data/database/model/User'; import UserRepo from '../../data/database/repository/UserRepo'; @@ -99,7 +100,7 @@ export const createFirstUser = asyncHandler(async (req, res) => { if (hasUser) { throw new AuthFailureError('Your instance already has a user, you must first connect'); } - + // TODO: move to use cases await UserRepo.create({ email: email, password: password, @@ -107,6 +108,7 @@ export const createFirstUser = asyncHandler(async (req, res) => { role: Role.ADMIN, avatar: avatar || '/avatars/squirrel.svg', }); + await createADefaultLocalUserRepository(); new SuccessResponse('Create first user').send(res); }); diff --git a/shared-lib/src/enums/settings.ts b/shared-lib/src/enums/settings.ts index cf4e7c84..40176db9 100644 --- a/shared-lib/src/enums/settings.ts +++ b/shared-lib/src/enums/settings.ts @@ -15,7 +15,7 @@ export enum AnsibleReservedExtraVarsKeys { } export enum DefaultValue { - SCHEME_VERSION = '6', + SCHEME_VERSION = '7', SERVER_LOG_RETENTION_IN_DAYS = '30', CONSIDER_DEVICE_OFFLINE_AFTER_IN_MINUTES = '3', CONSIDER_PERFORMANCE_GOOD_MEM_IF_GREATER = '10',