Sfoglia il codice sorgente

chore: add logs to git repo pull

Nicolas Meienberger 1 anno fa
parent
commit
e5f1a94c08

+ 5 - 0
packages/cli/src/executors/app/app.executors.ts

@@ -110,11 +110,16 @@ export class AppExecutors {
         await copyDataDir(appId);
       }
 
+      await execAsync(`chmod -R a+rwx ${path.join(appDataDirPath)}`).catch(() => {
+        this.logger.error(`Error setting permissions for app ${appId}`);
+      });
+
       // run docker-compose up
       this.logger.info(`Running docker-compose up for app ${appId}`);
       await compose(appId, 'up -d');
 
       this.logger.info(`Docker-compose up for app ${appId} finished`);
+
       return { success: true, message: `App ${appId} installed successfully` };
     } catch (err) {
       return this.handleAppError(err);

+ 14 - 3
packages/cli/src/executors/repo/repo.executors.ts

@@ -75,9 +75,20 @@ export class RepoExecutors {
 
       this.logger.info(`Pulling repo ${repoUrl} to ${repoPath}`);
 
-      await execAsync(`git config --global --add safe.directory ${repoPath}`);
-      await execAsync(`git -C ${repoPath} reset --hard`);
-      const { stdout, stderr } = await execAsync(`git -C ${repoPath} pull`);
+      await execAsync(`git config --global --add safe.directory ${repoPath}`).then(({ stdout, stderr }) => {
+        this.logger.info('------------------ git config --global --add safe.directory ------------------');
+        this.logger.error(`stderr: ${stderr}`);
+        this.logger.info(`stdout: ${stdout}`);
+      });
+
+      // reset hard
+      await execAsync(`git -C ${repoPath} reset --hard`).then(({ stdout, stderr }) => {
+        this.logger.info(`------------------ git -C ${repoPath} reset --hard ------------------`);
+        this.logger.error(`stderr: ${stderr}`);
+        this.logger.info(`stdout: ${stdout}`);
+      });
+
+      const { stderr, stdout } = await execAsync(`git -C ${repoPath} pull`);
 
       if (stderr) {
         this.logger.error(`Error pulling repo ${repoUrl}: ${stderr}`);

+ 18 - 44
packages/cli/src/executors/system/system.executors.ts

@@ -12,6 +12,8 @@ import { Stream } from 'stream';
 import { promisify } from 'util';
 import dotenv from 'dotenv';
 import { SystemEvent } from '@runtipi/shared';
+import { killOtherWorkers } from 'src/services/watcher/watcher';
+import chalk from 'chalk';
 import { AppExecutors } from '../app/app.executors';
 import { copySystemFiles, generateSystemEnvFile, generateTlsCertificates } from './system.helpers';
 import { TerminalSpinner } from '@/utils/logger/terminal-spinner';
@@ -93,7 +95,7 @@ export class SystemExecutors {
 
     const filesAndFolders = [
       path.join(rootFolderHost, 'apps'),
-      // path.join(rootFolderHost, 'app-data'),
+      path.join(rootFolderHost, 'app-data'),
       path.join(rootFolderHost, 'logs'),
       path.join(rootFolderHost, 'media'),
       path.join(rootFolderHost, 'repos'),
@@ -104,20 +106,14 @@ export class SystemExecutors {
       path.join(rootFolderHost, 'VERSION'),
     ];
 
-    const { uid, gid } = getUserIds();
-
     // Give permission to read and write to all files and folders for the current user
     await Promise.all(
       filesAndFolders.map(async (fileOrFolder) => {
         if (await pathExists(fileOrFolder)) {
-          await execAsync(`sudo chown -R ${uid}:${gid} ${fileOrFolder}`);
-
-          await execAsync(`sudo chmod -R 750 ${fileOrFolder}`);
+          await execAsync(`sudo chmod -R a+rwx ${fileOrFolder}`);
         }
       }),
     );
-
-    await execAsync(`sudo chmod -R a+rwx ${path.join(rootFolderHost, 'app-data')}`);
   };
 
   public systemInfo = async () => {
@@ -181,33 +177,25 @@ export class SystemExecutors {
    * This method will start Tipi.
    * It will copy the system files, generate the system env file, pull the images and start the containers.
    */
-  public start = async (sudo = true) => {
+  public start = async () => {
     const spinner = new TerminalSpinner('Starting Tipi...');
     try {
-      if (sudo) {
-        await this.ensureFilePermissions(this.rootFolder);
-      } else {
-        console.log(
-          boxen("You are running in sudoless mode. While tipi should work as expected, you'll probably face folder permission issues with the apps you install and you'll need to manually fix them.", {
-            title: '⛔️ Sudoless mode',
-            titleAlignment: 'center',
-            padding: 1,
-            borderStyle: 'double',
-            borderColor: 'red',
-            margin: { top: 1 },
-          }),
-        );
+      const { isSudo } = getUserIds();
+
+      if (!isSudo) {
+        console.log(chalk.red('Tipi needs to run as root to start. Use sudo ./runtipi-cli start'));
+        throw new Error('Tipi needs to run as root to start. Use sudo ./runtipi-cli start');
       }
 
+      await this.ensureFilePermissions(this.rootFolder);
+
       spinner.start();
       spinner.setMessage('Copying system files...');
       await copySystemFiles();
 
       spinner.done('System files copied');
 
-      if (sudo) {
-        await this.ensureFilePermissions(this.rootFolder, false);
-      }
+      await this.ensureFilePermissions(this.rootFolder, false);
 
       spinner.setMessage('Generating system env file...');
       spinner.start();
@@ -249,16 +237,10 @@ export class SystemExecutors {
       const out = fs.openSync('./logs/watcher.log', 'a');
       const err = fs.openSync('./logs/watcher.log', 'a');
 
-      if (sudo) {
-        // Dummy sudo call to ask for password
-        await execAsync('sudo echo "Dummy sudo call"');
-        const subprocess = spawn('sudo', ['./runtipi-cli', 'watch'], { cwd: this.rootFolder, stdio: ['inherit', out, err] });
+      await killOtherWorkers();
 
-        subprocess.unref();
-      } else {
-        const subprocess = spawn('./runtipi-cli', [process.argv[1] as string, 'watch'], { cwd: this.rootFolder, detached: true, stdio: ['ignore', out, err] });
-        subprocess.unref();
-      }
+      const subprocess = spawn('./runtipi-cli', [process.argv[1] as string, 'watch'], { cwd: this.rootFolder, detached: true, stdio: ['ignore', out, err] });
+      subprocess.unref();
 
       spinner.done('Watcher started');
 
@@ -337,7 +319,7 @@ export class SystemExecutors {
    * runtipi-cli binary with the new one.
    * @param {string} target
    */
-  public update = async (target: string, sudo = true) => {
+  public update = async (target: string) => {
     const spinner = new TerminalSpinner('Evaluating target version...');
     try {
       spinner.start();
@@ -427,15 +409,7 @@ export class SystemExecutors {
       // eslint-disable-next-line no-promise-executor-return
       await new Promise((resolve) => setTimeout(resolve, 3000));
 
-      const args = [process.argv[1] as string, 'start'];
-
-      const { isSudo } = getUserIds();
-
-      if (!sudo && !isSudo) {
-        args.push('--no-sudo');
-      }
-
-      const childProcess = spawn('./runtipi-cli', args);
+      const childProcess = spawn('./runtipi-cli', [process.argv[1] as string, 'start']);
 
       childProcess.stdout.on('data', (data) => {
         process.stdout.write(data);

+ 2 - 2
packages/cli/src/index.ts

@@ -22,9 +22,9 @@ const main = async () => {
     .description('Start tipi')
     .option('--no-permissions', 'Skip permissions check')
     .option('--no-sudo', 'Skip sudo usage')
-    .action(async (options) => {
+    .action(async () => {
       const systemExecutors = new SystemExecutors();
-      await systemExecutors.start(options.sudo);
+      await systemExecutors.start();
     });
 
   program

+ 6 - 15
packages/cli/src/services/watcher/watcher.ts

@@ -9,7 +9,7 @@ import { getUserIds } from '@/utils/environment/user';
 const execAsync = promisify(exec);
 
 const runCommand = async (jobData: unknown) => {
-  const { gid, uid, isSudo } = getUserIds();
+  const { gid, uid } = getUserIds();
   console.log(`Running command with uid ${uid} and gid ${gid}`);
 
   const { installApp, startApp, stopApp, uninstallApp, updateApp, regenerateAppEnv } = new AppExecutors();
@@ -69,28 +69,21 @@ const runCommand = async (jobData: unknown) => {
     }
 
     if (data.command === 'update') {
-      if (!isSudo) {
-        ({ success, message } = await update(data.version, false));
-      } else {
-        ({ success, message } = await update(data.version, true));
-      }
+      ({ success, message } = await update(data.version));
     }
   }
 
   return { success, message };
 };
 
-const killOtherWorkers = async () => {
+export const killOtherWorkers = async () => {
   const { stdout } = await execAsync('ps aux | grep "index.js watch" | grep -v grep | awk \'{print $2}\'');
+  const { stdout: stdoutInherit } = await execAsync('ps aux | grep "runtipi-cli watch" | grep -v grep | awk \'{print $2}\'');
 
   const pids = stdout.split('\n').filter((pid: string) => pid !== '');
+  const pidsInherit = stdoutInherit.split('\n').filter((pid: string) => pid !== '');
 
-  pids.forEach((pid) => {
-    if (pid === process.pid.toString()) {
-      console.log('Skipping killing current worker');
-      return;
-    }
-
+  pids.concat(pidsInherit).forEach((pid) => {
     console.log(`Killing worker with pid ${pid}`);
     try {
       process.kill(Number(pid));
@@ -104,8 +97,6 @@ const killOtherWorkers = async () => {
  * Start the worker for the events queue
  */
 export const startWorker = async () => {
-  await killOtherWorkers();
-
   const worker = new Worker(
     'events',
     async (job) => {