Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add default local repository for playbooks on user creation #78

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions server/src/core/startup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand All @@ -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,
};
1 change: 1 addition & 0 deletions server/src/data/database/model/Playbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const schema = new Schema<Playbook>(
uniqueQuickRef: {
type: Schema.Types.String,
unique: true,
sparse: true,
},
path: {
type: Schema.Types.String,
Expand Down
5 changes: 5 additions & 0 deletions server/src/data/database/repository/UserRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ async function updateLogsLevel(email: string, userLogsLevel: UserLogsLevel): Pro
await UsersModel.updateOne({ email: email }, { logsLevel: userLogsLevel }).exec();
}

async function findFirst(): Promise<User | null> {
return await UsersModel.findOne().lean().exec();
}

export default {
create,
findByEmailAndPassword,
Expand All @@ -50,4 +54,5 @@ export default {
resetApiKey,
updateLogsLevel,
findByApiKey,
findFirst,
};
4 changes: 3 additions & 1 deletion server/src/services/user/user.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -99,14 +100,15 @@ 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,
name: name,
role: Role.ADMIN,
avatar: avatar || '/avatars/squirrel.svg',
});
await createADefaultLocalUserRepository();
new SuccessResponse('Create first user').send(res);
});

Expand Down
2 changes: 1 addition & 1 deletion shared-lib/src/enums/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading