经过几年尝试不同的事情,我们终于让 PyCharm 与我们的 ckan/Vagrant VM 环境一起工作。
我们多次尝试让 PyCharm 的本地实例与我们的访客 Vagrant VM 对话,但这对于开发人员来说似乎非常脆弱和困惑。
然后我们团队中有人说“我们为什么不在 Vagrant VM 上安装 PyCharm,然后使用 x11 转发来查看用户界面?”
尝试一下似乎是个好主意,而且对我们来说效果很好。我们获得了 IDE 和调试启动的所有好处。耶!
这就是我们的做法。您将必须了解您的特定环境并更改您在下面看到的内容以适应您正在做的事情。您还需要注意自己的 PyCharm 许可!
VagrantFile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
# apache port
config.vm.network "forwarded_port", guest: 5000, host: 5000, auto_correct: false
# database port
config.vm.network "forwarded_port", guest: 5432, host: 65432, auto_correct: false
# solr port
config.vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct: false
# For PyCharm
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
# Install PyCharm IDE via shell script
config.vm.provision "shell", path: "install_pycharm.sh"
end
install_pycharm.sh
#!/bin/sh -e
set -x
cd /tmp
wget https://download.jetbrains.com/python/pycharm-professional-2017.3.1.tar.gz
tar -xvzf pycharm-professional-2017.3.1.tar.gz -C /tmp/
cp -r /tmp/pycharm-2017.3.1 /opt/pycharm
ln -s /opt/pycharm/bin/pycharm.sh /usr/local/bin/pycharm
ln -s /opt/pycharm/bin/inspect.sh /usr/local/bin/inspect
yum -y groupinstall "X Window System"
# Copy .Xauthority file from vagrant to ckan home
cp /vagrant/.Xauthority /usr/lib/ckan
chown ckan:ckan /usr/lib/ckan/.Xauthority
echo "==========================================="
echo "PyCharm will be installed in your Vagrant instance, but...."
echo "now you must run some manual steps to get PyCharm working:"
echo ""
echo " ssh into vagrant"
echo " This ssh will create your /home/vagrant/.Xauthority file, which you need to see the PyCharm GUI."
echo " Then you will need to copy this .Xauthority file to the ckan user to run PyCharm as ckan."
echo ""
echo " sudo cp /home/vagrant/.Xauthority /usr/lib/ckan"
echo " sudo chown ckan:ckan /usr/lib/ckan/.Xauthority"
echo " Set the DISPLAY variable for ckan user as it is set for vagrant when you do echo $DISPLAY: "
echo " export DISPLAY=localhost:11.0 "
echo " To start PyCharm run"
echo " pycharm "