简体   繁体   English

如何判断我是否在Vagrant主持人中?

[英]How to tell if I am inside a Vagrant host?

Whats a bulletproof way to determine if I am running inside a vagrant machine? 什么是防弹方式来确定我是否在流浪汉机器内运行?

Guest OS is Debian Linux, though if there are indicators per-os that would be great to have documented as well. 客户操作系统是Debian Linux,不过如果有指标per-os也很好记录。

AFAIK, there isn't a way outside of your own customizations. AFAIK,除了您自己的自定义之外没有办法。 One idea that comes to mind is to touch a file that you then reference from your apps. 想到的一个想法是触摸您随后从应用程序引用的文件。 Add something like config.vm.provision "shell", inline: "touch /etc/is_vagrant_vm" to the bottom of your Vagrantfile and base your conditionals around the existence of that file. 添加类似config.vm.provision "shell", inline: "touch /etc/is_vagrant_vm"东西config.vm.provision "shell", inline: "touch /etc/is_vagrant_vm"到你的Vagrantfile的底部,并根据该文件的存在来定位你的条件。

Provisioning a file that you can check the existence of seems like the most reliable way to handle this. 配置一个可以检查存在的文件似乎是处理此问题的最可靠方法。

Another option if you don't want to write files to the box would be to check for the existence of the vagrant user itself: 如果您不想将文件写入框中,另一个选项是检查vagrant用户本身是否存在:

grep -q '^vagrant:' /etc/passwd && echo 'Vagrant environment: true'

This is not foolproof as others have indicated, and it is possible (although uncommon) to have a vagrant box that uses a different user to connect as. 这并非像其他人所指出的那样万无一失,并且有可能(尽管不常见)有一个使用不同用户连接的流浪盒。

Checking the user's existence also would not work reliably if you have machines in your environment with a user account called vagrant that are not actually vagrant boxes, but that would also be fairly uncommon. 如果您的环境中的计算机上有一个名为vagrant的用户帐户实际上并不是流浪盒,那么检查用户的存在也无法可靠地工作,但这也是相当不常见的。

I don't know if there is any bulletproof way for this, but one thing I often do is to config my vagrant environment's shell UI to be different from the shell UI of my host machine. 我不知道是否有任何防弹方式,但我经常做的一件事是将我的流浪者环境的shell UI配置为与我的主机的shell UI不同。 This way I can tell the difference at first glance. 这样我乍一看就可以区分出来。 It also helps if you want to distinguish among multiple vagrant environments. 如果您想要区分多个流浪者环境,它也会有所帮助。

To customize the shell UI, oh-my-zsh will come in handy. 要自定义shell UI, oh-my-zsh会派上用场。

Assuming you don't run a vagrant instance in your vagrant instance: 假设你没有在你的vagrant实例中运行vagrant实例:

if which vagrant > /dev/null 2>&1; then
    echo "This is most likely the host"
fi

你可以使用facter

facter | grep vagrant

This is how I was able to answer the question with a very small chunk of bash. 这就是我用一小块bash来回答这个问题的方法。 The -e flag checks if the file exists. -e标志检查文件是否存在。 Change provision to a file path that makes sense for your build. 更改provision ,以有意义为您构建一个文件路径。

#!/usr/bin/env bash

user=$USER

provision=/vagrant/Vagrantfile
if [ -e $provision ]
then
  echo "Vagrantfile found..."
  echo "Setting $user to vagrant..."
  user=vagrant
fi

# Example usage
cd /home/$user

Tested on "centos/7" 测试“centos / 7”

In vagrant your are in control of which IP you are running against, so you could change it to for example 192.168.0.10 在vagrant中,您可以控制运行的IP,因此可以将其更改为例如192.168.0.10

Then: 然后:

curl localhost:8080/health => in local e.g spring-boot

curl 192.168.0.10:8080/health => in vagrant

NOTE: /health assumes spring-boot but you could also use your own implementation of a /health endpoint as well 注意:/ health假定是spring-boot,但您也可以使用自己的/ health端点实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM