flomesh-io / ztm

ZTM (Zero Trust Mesh) is a privacy-first open-source decentralized network software based on HTTP/2 tunnels. Experience boundless connectivity and mesh the globe!
Apache License 2.0
326 stars 27 forks source link

Run `ztm start` didn't start the service #27

Closed Hou-Xiaoxuan closed 3 months ago

Hou-Xiaoxuan commented 4 months ago

Run ztm start didn't report any errors, but service didn't start.

Check service status:

> sudo systemctl status ztm-agent.service 
× ztm-agent.service - ztm agent service
     Loaded: bad-setting (Reason: Unit ztm-agent.service has a bad unit file setting.)
     Active: failed (Result: exit-code) since Sun 2024-06-02 23:46:56 CST; 16min ago
   Duration: 3ms
   Main PID: 24445 (code=exited, status=203/EXEC)

Jun 02 23:46:56 HXX-LENOVO systemd[1]: ztm-agent.service: Scheduled restart job, restart counter is at 5.
Jun 02 23:46:56 HXX-LENOVO systemd[1]: ztm-agent.service: Start request repeated too quickly.
Jun 02 23:46:56 HXX-LENOVO systemd[1]: ztm-agent.service: Failed with result 'exit-code'.
Jun 02 23:46:56 HXX-LENOVO systemd[1]: Failed to start ztm agent service.
Jun 03 00:02:30 HXX-LENOVO systemd[1]: /etc/systemd/system/ztm-agent.service:6: Empty path in command line: ''' run agent --listen '127.0.0.1:7777' --database '/root/ztm.db''
Jun 03 00:02:30 HXX-LENOVO systemd[1]: ztm-agent.service: Unit configuration has fatal error, unit will not be started.

And check /etc/systemd/system/ztm-agent.service found:

[Service]
ExecStart='' run agent --listen '127.0.0.1:7777' --database '/root/ztm.db'

The relevant code is on line 405 of agent/main.js.

function startServiceLinux(name, args, optsChanged) {
  var program = os.abspath(pipy.exec(['which', pipy.argv[0]]).toString().trim())
  var user = pipy.exec('whoami').toString().trim()
  var opts = args.map(
    arg => arg.startsWith('-') ? arg : `'${arg}'`
  ).join(' ')
  var filename = `/etc/systemd/system/ztm-${name}.service`
  var logdir = `/var/log/ztm`
  if (optsChanged || !os.stat(filename)) {
    os.write(filename, stripIndentation(`
      [Unit]
      Description = ztm ${name} service
      After = network.target

      [Service]
      ExecStart='${program}' run ${name} ${opts}
      ExecStop=/usr/bin/kill $MAINPID
      Restart=on-failure
      User=${user}
      LimitNOFILE=655360
      StandardOutput=append:${logdir}/${name}.log

      [Install]
      WantedBy = multi-user.target
    `))
    os.mkdir(logdir, { recursive: true })
    pipy.exec(`systemctl daemon-reload`)
  }
  pipy.exec(`systemctl restart ztm-${name}`)
}

There are two reasons for this error.

First, some linux system didn't have which command. Such as my Archlinux WSL.

In addition, even with whitch, ztm downloads from release will not be added to path, and the documentation and readme do not mention this.

Hou-Xiaoxuan commented 4 months ago

BTW, the code does not appear to be compatible with macOS, and running 'sudo ztm agent' on macOS results in an error。

sudo ./bin/ztm start agent --listen 0.0.0.0:7777 --database ./agent.db
Password:
ztm: cannot open file: /var/root/Library/LaunchAgents/io.flomesh.ztm.agent.plist

os info:

OS: macOS 14.5 23F79 arm64 
Host: MacBookPro17,1 
Kernel: 23.5.0 
CPU: Apple M1 
GPU: Apple M1 
genedna commented 4 months ago

@Hou-Xiaoxuan ,

May be try ztm run agent --listen 0.0.0.0:7777 --database ./agent.db ?

Hou-Xiaoxuan commented 4 months ago

@Hou-Xiaoxuan ,

May be try ztm run agent --listen 0.0.0.0:7777 --database ./agent.db ?

Yes, the run command is perfectly fine. I just want to report a bug with the ztm start command.

keveinliu commented 4 months ago
```fish
ztm: cannot open file: /var/root/Library/LaunchAgents/io.flomesh.ztm.agent.plist

Yes, we haven't adopt system service on MacOS. BTW, this bug report has been accepted. We'll handle it asap. Thanks!

keveinliu commented 3 months ago

@Hou-Xiaoxuan this issue has been fixed.