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

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

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

python+vagrant+virtualbox系列文章
python

初始化环境

安装vagrant+virtualbox

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

下载centos镜像

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

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码云上的jumpservergit

git clone --depth=1 https://github.com/jumpserver/jumpserver.git
cd jumpserver
复制代码

在jumpserver下建立Vagrantfilegithub

# -*- 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,已经自动配置阿里云源和清华源而且安装必要依赖包了,不须要重复配置web

## 设置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了,已经配置好了sql

vagrant ssh
python3.6 -m venv /home/vagrant/venv
source /home/vagrant/venv/bin/activate
复制代码

只须要执行这个便可shell

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 则不能够

参考资料

相关文章
相关标签/搜索