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 則不能夠

參考資料

相關文章
相關標籤/搜索