[关闭]
@daduizhang 2021-03-24T02:07:52.000000Z 字数 10609 阅读 812

vagrant

Linux


vagrant 虚拟开发环境,和线上环境保持一致,已预装git, vim, python, go, docker, mysql 等, 并完成以下配置:

开箱即用,轻松避免开发环境和生产环境不一致的问题。
使用vagrant的好处:
- 业界流行的开发方式
- 避免折腾环境问题,提升开发效率
- 用windows和mac的用户可以在享受原生系统的易用性的同时,享受使用linux的快感。
- 占用资源少,和原生linux速度相当
- 与host系统隔离,vm被搞坏了可以随时删掉重建,还可以创建多个虚拟环境。系统崩了,不用担心重要文件丢失。
- 方便本地调试,端口无限制,软件安装无需申请权限
- 避免在一台机器上测试通过,在另一台机器上无法通过的问题

使用方式

  1. 到“钉钉-企业文件-常用软件”中相应的 virtualbox,vagrant并安装
  2. 创建 ubuntu-16.04 环境目录
  3. 到“钉钉-企业文件-常用软件” 中下载 ubuntu-16.04.box
  4. 执行命令
  1. vagrant box add ubuntu-16.04 ./ubuntu-16.04.box
  2. vagrant init ubuntu-16.04

此时会在目录下生成配置文件 Vagrantfile,修改以下配置:

  1. # 添加私有IP,和host机通信
  2. config.vm.network "private_network", ip: "192.168.33.10"
  3. # 映射文件夹,可自行修改
  4. config.vm.synced_folder "../workspace", "/home/vagrant/workspace"
  5. config.vm.provider "virtualbox" do |vb|
  6. # Display the VirtualBox GUI when booting the machine
  7. vb.gui = false
  8. # Customize the amount of memory on the VM:
  9. vb.memory = "2048"
  10. vb.cpus = "2"
  11. vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
  12. end

继续执行以下命令即可

  1. vagrant up # 启动虚拟机
  2. vagrant ssh # ssh到虚拟机
  3. vagrant halt # 关闭虚拟机,通常情况下,无需关闭

详细使用方法可参考 https://segmentfault.com/a/1190000008729625

----------------------------------

用户可运行以下 Vagrantfile 自行编译生成 ubuntu-16.04-v1.0.0.box

  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # All Vagrant configuration is done below. The "2" in Vagrant.configure
  4. # configures the configuration version (we support older styles for
  5. # backwards compatibility). Please don't change it unless you know what
  6. # you're doing.
  7. Vagrant.configure("2") do |config|
  8. # The most common configuration options are documented and commented below.
  9. # For a complete reference, please see the online documentation at
  10. # https://docs.vagrantup.com.
  11. # Every Vagrant development environment requires a box. You can search for
  12. # boxes at https://vagrantcloud.com/search.
  13. config.vm.box = "ubuntu/xenial64"
  14. config.vm.hostname = "shannonai.local"
  15. # Disable automatic box update checking. If you disable this, then
  16. # boxes will only be checked for updates when the user runs
  17. # `vagrant box outdated`. This is not recommended.
  18. config.vm.box_check_update = false
  19. # Create a forwarded port mapping which allows access to a specific port
  20. # within the machine from a port on the host machine. In the example below,
  21. # accessing "localhost:8080" will access port 80 on the guest machine.
  22. # NOTE: This will enable public access to the opened port
  23. # config.vm.network "forwarded_port", guest: 80, host: 8080
  24. # Create a forwarded port mapping which allows access to a specific port
  25. # within the machine from a port on the host machine and only allow access
  26. # via 127.0.0.1 to disable public access
  27. # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  28. # Create a private network, which allows host-only access to the machine
  29. # using a specific IP.
  30. config.vm.network "private_network", ip: "192.168.33.10"
  31. # Create a public network, which generally matched to bridged network.
  32. # Bridged networks make the machine appear as another physical device on
  33. # your network.
  34. # config.vm.network "public_network"
  35. # Share an additional folder to the guest VM. The first argument is
  36. # the path on the host to the actual folder. The second argument is
  37. # the path on the guest to mount the folder. And the optional third
  38. # argument is a set of non-required options.
  39. config.vm.synced_folder "/Users/shibo/shannon_workspace", "/home/vagrant/workspace"
  40. # Provider-specific configuration so you can fine-tune various
  41. # backing providers for Vagrant. These expose provider-specific options.
  42. # Example for VirtualBox:
  43. #
  44. config.vm.provider "virtualbox" do |vb|
  45. # Display the VirtualBox GUI when booting the machine
  46. vb.gui = false
  47. # Customize the amount of memory on the VM:
  48. vb.memory = "2048"
  49. end
  50. #
  51. # View the documentation for the provider you are using for more
  52. # information on available options.
  53. # Enable provisioning with a shell script. Additional provisioners such as
  54. # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  55. # documentation for more information about their specific syntax and use.
  56. config.vm.provision "shell", inline: <<-SHELL
  57. set -ex
  58. export DEBIAN_FRONTEND=noninteractive
  59. USER_HOME=/home/vagrant
  60. CONDA_HOME=$USER_HOME/software/conda
  61. GO_HOME=$USER_HOME/software
  62. PYTHON_VERSION=3.6.6
  63. # use aliyun source
  64. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.list
  65. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list
  66. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list
  67. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse' >> /etc/apt/sources.list
  68. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list
  69. echo 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' >> /etc/apt/sources.list
  70. echo 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list
  71. echo 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list
  72. echo 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse' >> /etc/apt/sources.list
  73. # install package
  74. apt-get -yqq update
  75. # install mysql and allow remote access, default password of root user is 123123
  76. echo "mysql-server mysql-server/root_password password 123123" | debconf-set-selections
  77. echo "mysql-server mysql-server/root_password_again password 123123" | debconf-set-selections
  78. apt-get install -yqq --no-install-recommends mysql-server mysql-client
  79. sed -i 's/^bind-address/# bind-address/' /etc/mysql/mysql.conf.d/mysqld.cnf
  80. mysql -u root -p123123 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123123' WITH GRANT OPTION; FLUSH PRIVILEGES;"
  81. sed -i '31i collation-server = utf8_unicode_ci' /etc/mysql/mysql.conf.d/mysqld.cnf
  82. sed -i '32i character-set-server = utf8' /etc/mysql/mysql.conf.d/mysqld.cnf
  83. sed -i '33i skip-character-set-client-handshake' /etc/mysql/mysql.conf.d/mysqld.cnf
  84. systemctl restart mysql
  85. # install common packages
  86. apt-get install -yqq --no-install-recommends gcc build-essential apt-utils rsync netcat procps libssl-dev
  87. apt-get install -yqq --no-install-recommends vim htop git tree pv libmysqlclient-dev language-pack-zh-hans ack-grep curl
  88. # set git
  89. git config --system push.default simple
  90. git config --system alias.co checkout
  91. git config --system alias.br branch
  92. git config --system alias.ci commit
  93. git config --system alias.st status
  94. git config --system core.editor "vim"
  95. git config --system alias.unstage 'reset HEAD --'
  96. git config --system alias.lg 'log --graph --abbrev-commit --decorate --format=format:"%C(cyan)%h%C(reset) - %C(white)%s%C(reset) %C(yellow)%d%C(reset) %C(dim white) - %anC(reset) %C(dim green)(%c i)%C(reset)" --all'
  97. # set locales
  98. grep -q -F 'LANG=zh_CN.UTF-8' /etc/environment || echo 'LANG=zh_CN.UTF-8' >> /etc/environment
  99. grep -q -F 'LC_ALL=zh_CN.UTF-8' /etc/environment || echo 'LC_ALL=zh_CN.UTF-8' >> /etc/environment
  100. grep -q -F 'LANGUAGE=zh_CN.UTF-8' /etc/environment || echo 'LANGUAGE=zh_CN.UTF-8' >> /etc/environment
  101. # install golang
  102. mkdir -p $GO_HOME
  103. if [ ! -d $GO_HOME/go ]; then
  104. curl -sSL -o /tmp/golang.tar.gz https://code.aliyun.com/k9kdqvbb/files/raw/master/go1.10.3.linux-amd64.tar.gz
  105. tar -C $GO_HOME -xzf /tmp/golang.tar.gz
  106. fi
  107. grep -q -F "export GOROOT=$GO_HOME/go" $USER_HOME/.profile || echo "export GOROOT=$GO_HOME/go" >> $USER_HOME/.profile
  108. grep -q -F 'export PATH=$GOROOT/bin:$PATH' $USER_HOME/.profile || echo 'export PATH=$GOROOT/bin:$PATH' >> $USER_HOME/.profile
  109. grep -q -F "export GOPATH=~/workspace/go" $USER_HOME/.profile || echo "export GOPATH=~/workspace/go" >> $USER_HOME/.profile
  110. grep -q -F 'export PATH=$GOPATH/bin:$PATH' $USER_HOME/.profile || echo 'export PATH=$GOPATH/bin:$PATH' >> $USER_HOME/.profile
  111. chown -R vagrant: $GO_HOME
  112. rm -rf /tmp/golang.tar.gz
  113. # install conda
  114. if [ ! -d $CONDA_HOME ]; then
  115. curl -sS -o /tmp/conda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
  116. bash /tmp/conda.sh -b -p $CONDA_HOME
  117. $CONDA_HOME/bin/conda install -yq python==$PYTHON_VERSION
  118. rm -rf /tmp/conda.sh
  119. fi
  120. grep -q -F "export CONDA_HOME=$CONDA_HOME" $USER_HOME/.profile || echo "export CONDA_HOME=$CONDA_HOME" >> $USER_HOME/.profile
  121. grep -q -F 'export PATH=$CONDA_HOME/bin:$PATH' $USER_HOME/.profile || echo 'export PATH=$CONDA_HOME/bin:$PATH' >> $USER_HOME/.profile
  122. mkdir -p $USER_HOME/.pip
  123. mkdir -p /root/.pip
  124. echo '[global]' | tee $USER_HOME/.pip/pip.conf /root/.pip/pip.conf
  125. echo 'trusted-host = mirrors.aliyun.com' | tee --append $USER_HOME/.pip/pip.conf /root/.pip/pip.conf
  126. echo 'index-url = https://mirrors.aliyun.com/pypi/simple' | tee --append $USER_HOME/.pip/pip.conf /root/.pip/pip.conf
  127. chown -R vagrant: $USER_HOME/.pip
  128. $CONDA_HOME/bin/pip install --no-cache-dir --default-timeout=120 ipython glances mycli tqdm pymysql mysqlclient fabric argparse pyyaml
  129. chown -R vagrant: $CONDA_HOME
  130. # install docker-ce
  131. apt-get install -yqq --no-install-recommends apt-transport-https ca-certificates software-properties-common
  132. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
  133. add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  134. apt-get -yqq update
  135. apt-get install -yqq --no-install-recommends docker-ce=18.03.1~ce-0~ubuntu
  136. usermod -a -G docker vagrant
  137. chmod 666 /var/run/docker.sock
  138. # aliyun speed up docker image download
  139. mkdir -p /etc/docker
  140. echo "{" > /etc/docker/daemon.json
  141. echo ' "registry-mirrors": ["https://ht6aappv.mirror.aliyuncs.com"]' >> /etc/docker/daemon.json
  142. echo "}" >> /etc/docker/daemon.json
  143. systemctl daemon-reload
  144. systemctl restart docker
  145. # install docker-compose
  146. DOCKER_COMPOSE=/usr/local/bin/docker-compose
  147. if [ ! -f $DOCKER_COMPOSE ]; then
  148. curl -sSL -o $DOCKER_COMPOSE https://code.aliyun.com/k9kdqvbb/files/raw/master/docker-compose-Linux-x86_64
  149. chmod +x $DOCKER_COMPOSE
  150. fi
  151. # clean
  152. apt-get autoremove -yqq --purge
  153. apt-get clean && apt-get autoclean
  154. # set bashrc
  155. grep -q -F "HISTSIZE=1000" $USER_HOME/.bashrc || sed -i 's/^HISTSIZE=1000/HISTSIZE=100000/' $USER_HOME/.bashrc
  156. grep -q -F "HISTFILESIZE=2000" $USER_HOME/.bashrc || sed -i 's/^HISTFILESIZE=2000/HISTFILESIZE=200000/' $USER_HOME/.bashrc
  157. # set prefix sensitive command prompt
  158. echo '"\e[A": history-search-backward' > $USER_HOME/.inputrc
  159. echo '"\e[B": history-search-forward' >> $USER_HOME/.inputrc
  160. chown vagrant: $USER_HOME/.inputrc
  161. # config vimrc
  162. echo 'filetype plugin indent on' > $USER_HOME/.vimrc
  163. echo 'set nocompatible' >> $USER_HOME/.vimrc
  164. echo 'filetype off' >> $USER_HOME/.vimrc
  165. echo 'set mouse-=a' >> $USER_HOME/.vimrc
  166. echo 'set fileencodings=utf-8' >> $USER_HOME/.vimrc
  167. echo 'set termencoding=utf-8' >> $USER_HOME/.vimrc
  168. echo 'set fileencoding=utf-8' >> $USER_HOME/.vimrc
  169. echo 'syntax on' >> $USER_HOME/.vimrc
  170. echo 'set nu' >> $USER_HOME/.vimrc
  171. echo 'set tabstop=4' >> $USER_HOME/.vimrc
  172. echo 'set shiftwidth=4' >> $USER_HOME/.vimrc
  173. echo 'set expandtab' >> $USER_HOME/.vimrc
  174. echo 'set cindent' >> $USER_HOME/.vimrc
  175. echo 'set autoindent' >> $USER_HOME/.vimrc
  176. echo 'set showmatch' >> $USER_HOME/.vimrc
  177. echo 'set cursorline' >> $USER_HOME/.vimrc
  178. echo 'set cursorcolumn' >> $USER_HOME/.vimrc
  179. echo 'set incsearch' >> $USER_HOME/.vimrc
  180. echo 'set hlsearch' >> $USER_HOME/.vimrc
  181. echo 'set ignorecase smartcase' >> $USER_HOME/.vimrc
  182. echo 'set nowrapscan' >> $USER_HOME/.vimrc
  183. echo 'set laststatus=2' >> $USER_HOME/.vimrc
  184. echo 'set nostartofline' >> $USER_HOME/.vimrc
  185. echo 'set ruler' >> $USER_HOME/.vimrc
  186. echo ':color desert' >> $USER_HOME/.vimrc
  187. chown vagrant: $USER_HOME/.vimrc
  188. SHELL
  189. end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注