036-win10搭建python的linux开发环境(pycharm+vagrant+virtualbox)

2,443 阅读4分钟

这是坚持技术写作计划(含翻译)的第36篇,定个小目标999,每周最少2篇。

本文以jumpserver为例,介绍如何在windows环境下进行jumpserver开发(jumpserver依赖的一些库,只有linux环境才能用),其实不局限于jumpserver,其他项目也适用(不限于python)。参考 打造跨平台一致性开发环境

python+vagrant+virtualbox系列文章

初始化环境

安装vagrant+virtualbox

参考 Vagrant系列(一)----win10搭建Vagrant+VirtualBox环境

下载centos镜像

可以使用境外服务器或者迅雷下载 https://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box

vagrant box add centos/7 \
    https://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box

## 或者

vagrant box add centos/7 /path/to/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box

下载jumpserver源代码并创建vagrant

如果下载速度慢,可以使用码云(gitee.com)自己创建个镜像项目,clone码云上的jumpserver

git clone --depth=1 https://github.com/jumpserver/jumpserver.git
cd jumpserver

在jumpserver下创建Vagrantfile

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

Vagrant.configure("2") do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box_check_update = false
  config.vm.box = "centos/7"
  config.vm.hostname = "jumpserver"
  config.vm.network "private_network", ip: "172.17.8.101"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "4096"
    vb.cpus = 2
    vb.name = "jumpserver"
  end

  config.vm.synced_folder ".", "/vagrant", type: "rsync",
    rsync__verbose: true,
    rsync__exclude: ['.git*', 'node_modules*','*.log','*.box','Vagrantfile']

  config.vm.provision "shell", inline: <<-SHELL
sudo sed -e "/mirrorlist/d" -e "s/#baseurl/baseurl/g" -e "s/mirror\.centos\.org/mirrors\.tuna\.tsinghua\.edu\.cn/g" -i /etc/yum.repos.d/CentOS-Base.repo
sudo yum makecache
sudo yum install -y epel-release

sudo yum install -y python36 python36-devel python36-pip \
		 libtiff-devel libjpeg-devel libzip-devel freetype-devel \
     lcms2-devel libwebp-devel tcl-devel tk-devel sshpass \
     openldap-devel mariadb-devel mysql-devel libffi-devel \
     openssh-clients telnet openldap-clients gcc

mkdir /home/vagrant/.pip
cat << EOF | sudo tee -a /home/vagrant/.pip/pip.conf
[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
use-mirrors = true
mirrors = https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
EOF

python3.6 -m venv /home/vagrant/venv
source /home/vagrant/venv/bin/activate
echo "source /home/vagrant/venv/bin/activate" >> /home/vagrant/.bash_profile
  SHELL
end

cd jumpserver
vagrant up
vagrant ssh

安装python3.6及配置pip源

如果使用我的Vargrantfile,已经自动配置阿里云源和清华源并且安装必要依赖包了,不需要重复配置

## 设置yum的清华源
sudo sed -e "/mirrorlist/d" -e "s/#baseurl/baseurl/g" -e "s/mirror\.centos\.org/mirrors\.tuna\.tsinghua\.edu\.cn/g" -i /etc/yum.repos.d/CentOS-Base.repo
sudo yum makecache
sudo yum install -y epel-release

## 安装依赖包
sudo yum install -y python36 python36-devel python36-pip \
		 libtiff-devel libjpeg-devel libzip-devel freetype-devel \
     lcms2-devel libwebp-devel tcl-devel tk-devel sshpass \
     openldap-devel mariadb-devel mysql-devel libffi-devel \
     openssh-clients telnet openldap-clients gcc

## 配置pip阿里云源
mkdir ~/.pip
cat << EOF | sudo tee -a ~/.pip/pip.conf
[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
use-mirrors = true
mirrors = https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
EOF

安装依赖

如果使用我的Vargrantfile,不需要配置python env了,已经配置好了

vagrant ssh
python3.6 -m venv /home/vagrant/venv
source /home/vagrant/venv/bin/activate

只需要执行这个即可

pip3 install -r /vagrant/requirements/requirements.txt

参考 安装文档

配置pycharm并启动jumpserver

配置pycharm

Ctrl+Alt+S打开设置

image.png

image.png

如果正常,会显示已经安装的pip包,如果是空,或者只有两个,那意味着python的env设置的不对。
image.png

image.png

image.png

使用 PyCharm专业版和vagrant进行同步开发

修改配置并初始化数据库

cd jumpserver
cp config_example.yml config.yml
## 修改config.yml的相关配置
vagrant ssh
source /home/vagrant/venv/bin/activate
cd /vagrant/
./utils/make_migrations.sh

启动项目

image.png

image.png

浏览器打开 http://172.17.8.101:8080/auth/login/?next=/

image.png

image.png

其他

已经提交PR,如果有需要的朋友, 可以在PR上投票,可以提高通过率added Vagrantfile to support windows dev#3036 (目前已合并到jumpserver repo中)

jumpserver virtualbox 已上传百度云盘 
链接:pan.baidu.com/s/1mr6xM7UV…  密码:rci6

2019-08-28 更新

如果遇到ansible无法执行的问题是因为上述方案只起了django 的server,没有启动celery

cd /path/to/host/jumpserver/
vagrant ssh
# 分别启动 celery和beat
/vagrant/jms start celery
/vagrant/jms start beat

至于为啥不直接 jms start all 而是使用pycharm启动server,因为可以debug jumpserver 。而用 jms start all 则不可以

参考资料