UNPKG

@lxdhub/dbsync

Version:

Display, search and copy LXD-images using a web interface.

106 lines 5.19 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); const db_1 = require("@lxdhub/db"); const common_1 = require("@nestjs/common"); const aigle_1 = require("aigle"); const _ = require("lodash"); const architecture_service_1 = require("../architecture/architecture.service"); const image_service_1 = require("../image/image.service"); const lxd_1 = require("../lxd"); const operating_system_1 = require("../operating-system"); const typeorm_1 = require("typeorm"); const typeorm_2 = require("@nestjs/typeorm"); let OsArchService = class OsArchService { constructor(osArchRepository, architectureService, operatingSystemService, imageService, imageRepository, dbSyncSettings, lxdService) { this.osArchRepository = osArchRepository; this.architectureService = architectureService; this.operatingSystemService = operatingSystemService; this.imageService = imageService; this.imageRepository = imageRepository; this.dbSyncSettings = dbSyncSettings; this.lxdService = lxdService; this.logger = new common_1.Logger('OsArchService'); } remoteImageToDto(remoteImage) { return { architecture: this.architectureService.remoteImageToDto(remoteImage), operatingSystem: this.operatingSystemService.remoteImageToDto(remoteImage), remoteImage }; } getOsArchs(remoteImages) { return _.chain(remoteImages) .map(remoteImage => this.remoteImageToDto(remoteImage)) .uniqWith((a, b) => _.isEqual(a.architecture, b.architecture) && _.isEqual(a.operatingSystem, b.operatingSystem)) .value(); } async getOrCreate(remoteOsArch) { const os = await this.operatingSystemService.getOrCreate(remoteOsArch.operatingSystem); const osId = os.id; const arch = await this.architectureService.getOrCreate(remoteOsArch.architecture); const archId = arch.id; // Already exists let localOsArch = await this.osArchRepository .createQueryBuilder('osarch') .leftJoinAndSelect('osarch.architecture', 'arch') .leftJoinAndSelect('osarch.operatingSystem', 'os') .where('arch.id = :archId AND os.id = :osId', { archId, osId }) .getOne(); if (!localOsArch) { localOsArch = await this.create({ arch, os }); } const image = await this.imageService.getImage(remoteOsArch.remoteImage); image.osArchitecture = localOsArch; this.imageRepository.save(image); return localOsArch; } async create(osArch) { this.logger.log(`Adding OperatingSystem Architecture ` + `${osArch.arch.humanName} ${osArch.os.distribution}`); const localOsArch = new db_1.OperatingSystemArchitecture(); localOsArch.architecture = osArch.arch; localOsArch.operatingSystem = osArch.os; return this.osArchRepository.save(localOsArch); } async synchronize() { this.logger.log('-> Starting OperatingSystem-Architecture synchronization'); await aigle_1.default .resolve(this.dbSyncSettings.lxdhubConfig.remotes) .forEachSeries(async (remote) => { const images = await this.lxdService.getRemoteImages(remote); const osArchs = this.getOsArchs(images); return await aigle_1.default .resolve(osArchs) .forEachSeries(async (osArch) => await this.getOrCreate(osArch)); }); this.logger.log('-> Finished OperatingSystem-Architecture synchronization'); } }; OsArchService = __decorate([ common_1.Injectable(), __param(0, typeorm_2.InjectRepository(db_1.OperatingSystemArchitecture)), __param(4, typeorm_2.InjectRepository(db_1.Image)), __param(5, common_1.Inject('LXDHubDbSyncSettings')), __metadata("design:paramtypes", [typeorm_1.Repository, architecture_service_1.ArchitectureService, operating_system_1.OperatingSystemService, image_service_1.ImageService, typeorm_1.Repository, Object, lxd_1.LXDService]) ], OsArchService); exports.OsArchService = OsArchService; //# sourceMappingURL=os-arch.service.js.map