4

注意:我没有任何实际管理服务器或在任何深度级别使用 Linux 的经验,因此我的知识和理解非常有限。从本质上讲,我正在即兴发挥。

有关完整的代码示例,请参见:https ://github.com/Integralist/Vagrant-Examples/tree/master/nodejs


这是一个两部分的问题:

  1. 无法挂载我的共享目录
  2. systemd服务不可用

我正在尝试创建一个启动 NodeJS 应用程序的服务,但它看起来systemctl在我安装的 Ubuntu 版本上不可用(https://vagrantcloud.com/ubuntu/trusty64)。

这是我的Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"

  # Working arround the "stdin: is not a tty" error, which appears when provisioning
  # config.ssh.pty = true

  config.vm.network :forwarded_port, guest: 80, host: 3000, auto_correct: true

  # We use Vagrant to create the new "web" group/owner for us
  # But we could have done this manually as part of our provisioning script
  #
  # useradd -mrU web
  # chown web /var/www
  # chgrp web /var/www
  # cd /var/www/
  # su web
  # git clone {code}
  config.vm.synced_folder "./", "/var/www", create: true, group: "web", owner: "web"

  config.vm.provision "shell" do |s|
    s.path = "provision/setup.sh"
  end
end

以下是setup.sh创建.service文件的配置脚本的内容:

su root

mkdir -p /var/www

cat << 'EOF' > /etc/systemd/system/our-node-app.service
  [Service]
  WorkingDirectory=/var/www
  ExecStart=/usr/bin/nodejs boot.js
  ExecReload=/bin/kill -HUP $MAINPID
  Restart=always
  StandardOutput=syslog
  StandardError=syslog
  SyslogIdentifier=some-identifier-here-typically-matching-workingdirectory
  User=web
  Group=web
  Environment='NODE_ENV=production'

  [Install]
  WantedBy=multi-user.target
EOF

但是当我运行时,vagrant up我得到以下错误输出:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: nodejs_default_1407743897168_39018
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 80 => 3000 (adapter 1)
    default: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /var/www => /Users/markmcdonnell/Box Sync/Library/Vagrant/nodejs

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u web`,gid=`getent group web | cut -d: -f3` var_www /var/www
mount -t vboxsf -o uid=`id -u web`,gid=`id -g web` var_www /var/www

所以我的第一个问题是我似乎无法挂载我的共享文件夹。

此外,最初在我的配置脚本中(在创建our-node-app.service文件之后)我将拥有以下内容:

systemctl enable our-node-app
systemctl start our-node-app
systemctl status our-node-app
journalctl -u node-sample # logs

如果我将其添加回我的配置脚本然后运行,vagrant provision --provision-with shell我将得到以下输出:

==> default: Running provisioner: shell...
    default: Running: /var/folders/n0/jlvkmj5n36vc0932b_1t0kxh0000gn/T/vagrant-shell20140811-58128-fa27fk.sh
==> default: stdin: is not a tty
==> default: /tmp/vagrant-shell: line 25: systemctl: command not found
==> default: /tmp/vagrant-shell: line 26: systemctl: command not found
==> default: /tmp/vagrant-shell: line 27: systemctl: command not found
==> default: /tmp/vagrant-shell: line 28: journalctl: command not found
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

chmod +x /tmp/vagrant-shell && /tmp/vagrant-shell

Stdout from the command:



Stderr from the command:

stdin: is not a tty
/tmp/vagrant-shell: line 25: systemctl: command not found
/tmp/vagrant-shell: line 26: systemctl: command not found
/tmp/vagrant-shell: line 27: systemctl: command not found
/tmp/vagrant-shell: line 28: journalctl: command not found

这是我发现systemctl命令不可用的问题的地方。

我还尝试修改配置脚本,而不是...

systemctl enable our-node-app
systemctl start our-node-app
systemctl status our-node-app
journalctl -u node-sample # logs

...我会用...

service our-node-app start
service --status-all | grep 'node'

这是因为我在某个地方读到了 Ubuntu 不支持的内容systemd,而是使用了一些名为upstart来启动其所有服务的东西。我当时假设我可以只使用另一个命令并保持脚本本身相同(似乎不是这种情况)。

但所做的所有更改都表明我的服务没有得到认可:

==> default: Running provisioner: shell...
    default: Running: /var/folders/n0/jlvkmj5n36vc0932b_1t0kxh0000gn/T/vagrant-shell20140811-58428-iot9kx.sh
==> default: stdin: is not a tty
==> default: our-node-app: unrecognized service
==> default:  [ ? ]  apport
==> default:  [ ? ]  console-setup
==> default:  [ ? ]  cryptdisks
==> default:  [ ? ]  cryptdisks-early
==> default:  [ ? ]  dns-clean
==> default:  [ ? ]  irqbalance
==> default:  [ ? ]  killprocs
==> default:  [ ? ]  kmod
==> default:  [ ? ]  networking
==> default:  [ ? ]  ondemand
==> default:  [ ? ]  open-vm-tools
==> default:  [ ? ]  pppd-dns
==> default:  [ ? ]  rc.local
==> default:  [ ? ]  screen-cleanup
==> default:  [ ? ]  sendsigs
==> default:  [ ? ]  umountfs
==> default:  [ ? ]  umountnfs.sh
==> default:  [ ? ]  umountroot
==> default:  [ ? ]  virtualbox-guest-x11
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

chmod +x /tmp/vagrant-shell && /tmp/vagrant-shell

Stdout from the command:



Stderr from the command:

stdin: is not a tty
our-node-app: unrecognized service
 [ ? ]  apport
 [ ? ]  console-setup
 [ ? ]  cryptdisks
 [ ? ]  cryptdisks-early
 [ ? ]  dns-clean
 [ ? ]  irqbalance
 [ ? ]  killprocs
 [ ? ]  kmod
 [ ? ]  networking
 [ ? ]  ondemand
 [ ? ]  open-vm-tools
 [ ? ]  pppd-dns
 [ ? ]  rc.local
 [ ? ]  screen-cleanup
 [ ? ]  sendsigs
 [ ? ]  umountfs
 [ ? ]  umountnfs.sh
 [ ? ]  umountroot
 [ ? ]  virtualbox-guest-x11

然后我发现Ubuntu 终究会转向这种systemd格式:但这是在 2014 年 2 月宣布的,所以我本以为最新的 Ubuntu 现在已经切换了(或者只是我是个白痴而不欣赏这样的改变需要多长时间)。

认为我必须使用这种 Upstart 格式,我已经开始阅读这个,但遗憾的是我无法弄清楚如何将我的systemd脚本转换为 Upstart 的格式。


这给我留下了一个问题:这里有没有其他人遇到过这个问题,如果有,他们是如何解决的(您是否切换到支持systemd或重写您的服务以使用 Upstart 格式的不同 Ubuntu 版本)?

您对如何将systemd脚本转换为 Upstart 格式有任何建议(或好的资源)吗?

对此主题的任何帮助将不胜感激;正如我在开始时提到的,我不是系统/服务器操作人员,所以我在这里做准备。

谢谢。

更新

我发现了这一点,似乎我误解了 和 之间的systemd区别。一个基于和改进的全新系统也是如此。init.dupstartsystemdinit.dupstart

链接到的文章解释了如何安装systemdupstart然后切换到systemd,但我在尝试安装 VM 时仍然遇到错误?

我已经更新了我的回购代码。

4

0 回答 0