All files / src/core/services systemctl.ts

25.53% Statements 12/47
40% Branches 4/10
20% Functions 3/15
22.22% Lines 10/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x 1x 1x 1x   1x 1x   1x                                                                                               1x                 12x           2x                                                                                                        
import { Injectable } from '@rxdi/core';
import { exec } from 'child_process';
import { writeFile } from 'fs';
import { promisify } from 'util';
 
import { Environment } from '../../app.constants';
const defaultRunnerName = 'runner';
@Injectable()
export class SystemctlService {
  async init() {
    const service = Environment.GRAPHQL_SYSTEM_SERVICE_NAME;
    const service_description =
      Environment.GRAPHQL_SYSTEM_SERVICE_DESCRIPTION;
    const executableBinary =
      Environment.GRAPHQL_SYSTEM_SERVICE_EXECUTABLE;
 
    try {
      await this.install(
        service_description,
        service,
        executableBinary,
      );
    } catch (e) {
      console.error(e);
    }
    try {
      await this.reload();
    } catch (e) {
      console.error(e);
    }
    try {
      await this.enable(service);
    } catch (e) {
      console.error(e);
    }
 
    try {
      await this.stop(service);
    } catch (e) {
      console.error(e);
    }
    try {
      await this.start(service);
    } catch (e) {
      console.error(e);
    }
  }
 
  async enable(name: string) {
    await promisify(exec)(`systemctl enable ${name}`);
  }
 
  generateConfig(
    description = 'Graphql Runner',
    executable = 'runner-linux',
  ) {
    return `
[Unit]
Description=${description}
 
[Service]
ExecStart=${process.cwd()}/${executable}
${Object.entries(Environment)
  .filter(
    ([key, value]) =>
      ![
        'GRAPHQL_SYSTEM_SERVICE',
        'GRAPHQL_SYSTEM_SERVICE_NAME',
        'GRAPHQL_SYSTEM_SERVICE_DESCRIPTION',
      ].includes(key) && !!value,
  )
  .map(([key, value]) => `Environment="${key}=${value}"`)
  .join('\n')}
Restart=always
RestartSec=3
 
[Install]
WantedBy=multi-user.target
    `;
  }
 
  async install(
    description = 'Graphql Runner',
    name = defaultRunnerName,
    executable = 'runner-linux',
  ) {
    await promisify(writeFile)(
      `/etc/systemd/system/${name}.service`,
      this.generateConfig(description, executable),
      { encoding: 'utf-8' },
    );
  }
  async reload() {
    const data = await promisify(exec)(
      `systemctl daemon-reload`,
    );
    console.log(data);
  }
  async start(name = defaultRunnerName) {
    const data = await promisify(exec)(
      `systemctl start ${name}`,
    );
    console.log(data);
  }
 
  async restart() {
    await this.stop();
    await this.reload();
    await this.start();
  }
  async stop(name = defaultRunnerName) {
    const data = await promisify(exec)(
      `systemctl stop ${name}`,
    );
    console.log(data);
  }
  async status(name = defaultRunnerName) {
    const data = await promisify(exec)(
      `systemctl status ${name}`,
    );
    console.log(data);
  }
}