部分内容转载从
大家对VMware
或者VirtualBox
一定不会陌生,虚拟化的好处自然深入人心,而现在我们可以通过Vagrant
搭建一套类似Laravel Homestead
完整开发环境,这样极大的减少了架设开发环境的时间,同时还支持在Windows/Mac/Linux
不同平台上分享定制包,统一团队之间的开发环境提高工作效率,而Docker
的出现也让未来更值得期待。
用Vagrant为自己打造一个奇妙的跨平台开发环境
阅读原文 - http://wsgzao.github.io/post/vagrant/
扩展阅读
Vagrant - https://www.vagrantup.com/
Laravel Homestead - http://laravel.com/docs/5.1/homestead
在windows下进行linux开发:利用Vagrant+virtualbox - http://blog.star7th.com/2015/06/1538.html
在 Mac/win7 下上使用 Vagrant 打造本地开发环境 - http://segmentfault.com/a/1190000002645737
环境准备
- Git(非必需)
- PHP(非必需)
- Laravel(非必需)
- Composer(非必需)
- Vagrant
- VirtualBox
如果大家有需要devstack的box请参阅 如何建立起基于Vagrant的Open EDX的虚拟开发环境(一),上传到csdn的种子下载
1-4 不介绍了(参考原文)
安装Vagrant(vagrant_1.8.1.msi)
1.下载Vagrant
https://www.vagrantup.com/downloads.html
安装VirtualBox(VirtualBox-5.0.14-105127-Win)
BIOS里面开启CPU硬件虚拟化支持VT(Virtualization Technology)
1.下载VirtualBox
https://www.virtualbox.org/wiki/Downloads
2.导入镜像(下面的环节比较复杂,之前没有用过vagrant,没想到还比较曲折)
设置VirtualBox目录并拷贝镜像: vagrant-images_20151221-dogwood-devstack-rc2.box
3.命令初始化Vagrant
create the Vagrant instance:
mkdir devstack
cd devstack
curl -L https://raw.githubusercontent.com/edx/configuration/master/vagrant/release/devstack/Vagrantfile > Vagrantfile
vagrant plugin install vagrant-vbguest
vagrant up
下载Vagrantfile到box目录,文件内容如下:
Vagrant.require_version ">= 1.6.5"
unless Vagrant.has_plugin?("vagrant-vbguest")
raise "Please install the vagrant-vbguest plugin by running `vagrant plugin install vagrant-vbguest`"
end
VAGRANTFILE_API_VERSION = "2"
MEMORY = 4096
CPU_COUNT = 2
$script = <<SCRIPT
if [ ! -d /edx/app/edx_ansible ]; then
echo "Error: Base box is missing provisioning scripts." 1>&2
exit 1
fi
OPENEDX_RELEASE=$1
export PYTHONUNBUFFERED=1
source /edx/app/edx_ansible/venvs/edx_ansible/bin/activate
cd /edx/app/edx_ansible/edx_ansible/playbooks
# Did we specify an openedx release?
if [ -n "$OPENEDX_RELEASE" ]; then
EXTRA_VARS="-e edx_platform_version=$OPENEDX_RELEASE \
-e certs_version=$OPENEDX_RELEASE \
-e forum_version=$OPENEDX_RELEASE \
-e xqueue_version=$OPENEDX_RELEASE \
"
CONFIG_VER=$OPENEDX_RELEASE
# Need to ensure that the configuration repo is updated
# The vagrant-devstack.yml playbook will also do this, but only
# after loading the playbooks into memory. If these are out of date,
# this can cause problems (e.g. looking for templates that no longer exist).
/edx/bin/update configuration $CONFIG_VER
else
CONFIG_VER="master"
fi
ansible-playbook -i localhost, -c local run_role.yml -e role=edx_ansible -e configuration_version=$CONFIG_VER $EXTRA_VARS
ansible-playbook -i localhost, -c local vagrant-devstack.yml -e configuration_version=$CONFIG_VER $EXTRA_VARS
SCRIPT
MOUNT_DIRS = {
:edx_platform => {:repo => "edx-platform", :local => "/edx/app/edxapp/edx-platform", :owner => "edxapp"},
:themes => {:repo => "themes", :local => "/edx/app/edxapp/themes", :owner => "edxapp"},
:forum => {:repo => "cs_comments_service", :local => "/edx/app/forum/cs_comments_service", :owner => "forum"},
:ecommerce => {:repo => "ecommerce", :local => "/edx/app/ecommerce/ecommerce", :owner => "ecommerce"},
:ecommerce_worker => {:repo => "ecommerce-worker", :local => "/edx/app/ecommerce_worker/ecommerce_worker", :owner => "ecommerce_worker"},
:programs => {:repo => "programs", :local => "/edx/app/programs/programs", :owner => "programs"},
# This src directory won't have useful permissions. You can set them from the
# vagrant user in the guest OS. "sudo chmod 0777 /edx/src" is useful.
:src => {:repo => "src", :local => "/edx/src", :owner => "root"},
}
if ENV['VAGRANT_MOUNT_BASE']
MOUNT_DIRS.each { |k, v| MOUNT_DIRS[k][:repo] = ENV['VAGRANT_MOUNT_BASE'] + "/" + MOUNT_DIRS[k][:repo] }
end
# map the name of the git branch that we use for a release
# to a name and a file path, which are used for retrieving
# a Vagrant box from the internet.
openedx_releases = {
"named-release/dogwood.rc" => {
:name => "dogwood-devstack-rc2", :file => "20151221-dogwood-devstack-rc2.box",
},
"named-release/dogwood" => {
:name => "dogwood-devstack-rc2", :file => "20151221-dogwood-devstack-rc2.box",
},
# Cypress is deprecated and unsupported
# "named-release/cypress" => {
# :name => "cypress-devstack", :file => "cypress-devstack.box",
# },
# Birch is deprecated and unsupported
# "named-release/birch.2" => {
# :name => "birch-devstack-2", :file => "birch-2-devstack.box",
# },
}
openedx_releases.default = {
:name => "devstack-nightly-20160125", :file => "20160125-devstack-from-nightly.box", ###实例化时居然找这个文件,而不是上面指定的 20151221-dogwood-devstack-rc2.box,后面改了,但是又出现新的问题,看来官方的文档考虑问题也不全面!
}
rel = ENV['OPENEDX_RELEASE']
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Creates an edX devstack VM from an official release
config.vm.box = openedx_releases[rel][:name]
config.vm.box_url = "http://files.edx.org/vagrant-images/#{openedx_releases[rel][:file]}"
config.vm.network :private_network, ip: "192.168.33.10"
# If you want to run the box but don't need network ports, set VAGRANT_NO_PORTS=1.
# This is useful if you want to run more than one box at once.
if not ENV['VAGRANT_NO_PORTS']
config.vm.network :forwarded_port, guest: 8000, host: 8000 # LMS
config.vm.network :forwarded_port, guest: 8001, host: 8001 # Studio
config.vm.network :forwarded_port, guest: 8002, host: 8002 # Ecommerce
config.vm.network :forwarded_port, guest: 8003, host: 8003 # LMS for Bok Choy
config.vm.network :forwarded_port, guest: 8031, host: 8031 # Studio for Bok Choy
config.vm.network :forwarded_port, guest: 8120, host: 8120 # edX Notes Service
config.vm.network :forwarded_port, guest: 8765, host: 8765
config.vm.network :forwarded_port, guest: 9200, host: 9200 # Elasticsearch
config.vm.network :forwarded_port, guest: 18080, host: 18080 # Forums
config.vm.network :forwarded_port, guest: 8100, host: 8100 # Analytics Data API
config.vm.network :forwarded_port, guest: 8110, host: 8110 # Insights
config.vm.network :forwarded_port, guest: 9876, host: 9876 # ORA2 Karma tests
config.vm.network :forwarded_port, guest: 50070, host: 50070 # HDFS Admin UI
config.vm.network :forwarded_port, guest: 8088, host: 8088 # Hadoop Resource Manager
end
config.ssh.insert_key = true
config.vm.synced_folder ".", "/vagrant", disabled: true
# Enable X11 forwarding so we can interact with GUI applications
if ENV['VAGRANT_X11']
config.ssh.forward_x11 = true
end
if ENV['VAGRANT_USE_VBOXFS'] == 'true'
MOUNT_DIRS.each { |k, v|
config.vm.synced_folder v[:repo], v[:local], create: true, owner: v[:owner], group: "www-data"
}
else
MOUNT_DIRS.each { |k, v|
config.vm.synced_folder v[:repo], v[:local], create: true, nfs: true
}
end
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", MEMORY.to_s]
vb.customize ["modifyvm", :id, "--cpus", CPU_COUNT.to_s]
# Allow DNS to work for Ubuntu 12.10 host
# http://askubuntu.com/questions/238040/how-do-i-fix-name-service-for-vagrant-client
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
# Use vagrant-vbguest plugin to make sure Guest Additions are in sync
config.vbguest.auto_reboot = true
config.vbguest.auto_update = true
# Assume that the base box has the edx_ansible role installed
# We can then tell the Vagrant instance to update itself.
config.vm.provision "shell", inline: $script, args: rel
end
vagrant-images_20151221-dogwood-devstack-rc2.box 文件名改为 20151221-dogwood-devstack-rc2.box
$vagrant up
There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
a syntax error.
Path: D:/2/edx/dogwood/devstack/Vagrantfile
Line number: 0
Message: RuntimeError: Please install the vagrant-vbguest plugin by running `vagrant plugin install vagrant-vbguest`
报错了,执行:
$vagrant plugin install vagrant-vbguest
无法执行,报错,需要gem。。。。。但是新版的vagrant不支持gem了
$ vagrant plugin install vagrant-vbguest bundle config disable_multisource true
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:
An error occurred while installing childprocess (0.5.9), and Bundler cannot continue.
Make sure that `gem install childprocess -v '0.5.9'` succeeds before bundling.
Warning: this Gemfile contains multiple primary sources. Using `source` more than once without a block is a security risk, and may result in installing unexpected gems. To resolve this warning, use a block to indicate which gems should come from the secondary source. To upgrade this warning to an error, run `bundle config disable_multisource true`.Gem::RemoteFetcher::FetchError: Errno::ECONNABORTED: An established connection was aborted by the software in your host machine. - SSL_connect (https://rubygems.org/gems/childprocess-0.5.9.gem)
上网搜了一下,解决方案是利用vagrant目录下一个与原来gem兼容的bat来做:
$C:\HashiCorp\Vagrant\embedded\bin\gem.bat install vagrant-vbguest
Successfully installed log4r-1.1.10
Successfully installed micromachine-1.1.0
Successfully installed vagrant-vbguest-0.11.0
Parsing documentation for log4r-1.1.10
Installing ri documentation for log4r-1.1.10
Parsing documentation for micromachine-1.1.0
Installing ri documentation for micromachine-1.1.0
Parsing documentation for vagrant-vbguest-0.11.0
Installing ri documentation for vagrant-vbguest-0.11.0
Done installing documentation for log4r, micromachine, vagrant-vbguest after 6 seconds
3 gems installed
再执行
$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.11.0)'!
终于OK了!!
$ vagrant box add ./20151221-dogwood-devstack-rc2.box
==> box: Box file was not detected as metadata. Adding it directly...
A name is required when adding a box file directly. Please pass
the `--name` parameter to `vagrant box add`. See
`vagrant box add -h` for more help.
########这里还有错误,以前没有用过vagrant,语法不对,需要为这个box起个名!
google了一个答案(浪费了半天时间):
|
You can point to the folder where vagrant and copy the box file to same location. Then after you may run as follows
#vagrant box add my-box name-of-the-box.box
$vagrant box add dogwood-devstack-rc2 ./20151221-dogwood-devstack-rc2.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dogwood-devstack-rc2' (v0) for provider:
box: Unpacking necessary files from: file://D:/2/edx/dogwood/devstack/20151221-dogwood-devstack-rc2.box
box:
==> box: Successfully added box 'dogwood-devstack-rc2' (v0) for 'virtualbox'!
#######################接着执行
vagrant init my-box
vagrant up
Just to check status
vagrant status
|
http://stackoverflow.com/questions/22065698/how-to-add-a-downloaded-box-file-to-vagrant
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'dogwood-devstack-rc2'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: devstack_default_1455519035857_32344
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8088 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 8088, host: 1234
Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.
#### 端口被占用???还是让他自己初始化一个吧(由于使用了从git下载的vagrantfile,导致了某些端口的冲突,删掉这个文件吧,否则init不了)
$ vagrant init dogwood-devstack-rc2
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims: 4.2.0
VBoxService inside the vm claims: 5.0.10
Going on, assuming VBoxService is correct...
GuestAdditions versions on your host (5.0.14) and guest (5.0.10) do not match.
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims: 4.2.0
VBoxService inside the vm claims: 5.0.10
Going on, assuming VBoxService is correct...
stdin: is not a tty
Reading package lists...
Building dependency tree...
Reading state information...
linux-headers-3.2.0-23-generic is already the newest version.
The following packages will be upgraded:
dkms
1 upgraded, 0 newly installed, 0 to remove and 168 not upgraded.
Need to get 73.3 kB of archives.
After this operation, 3,072 B of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main dkms all 2.2.0.3-1ubuntu3.6 [73.3 kB]
dpkg-preconfigure: unable to re-open stdin: No such file or directory
Fetched 73.3 kB in 1s (38.0 kB/s)
(Reading database ... 106259 files and directories currently installed.)
Preparing to replace dkms 2.2.0.3-1ubuntu3 (using .../dkms_2.2.0.3-1ubuntu3.6_all.deb) ...
Unpacking replacement dkms ...
Processing triggers for man-db ...
Setting up dkms (2.2.0.3-1ubuntu3.6) ...
Copy iso file C:\Program Files/Oracle/VirtualBox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
stdin: is not a tty
mount: warning: /mnt seems to be mounted read-only.
Installing Virtualbox Guest Additions 5.0.14 - guest version is 5.0.10
stdin: is not a tty
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.14 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.10 of VirtualBox Guest Additions...
Stopping VirtualBox Additions ...done.
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
Starting the VirtualBox Guest Additions ...done.
Installing the Window System drivers
Installing X.Org Server 1.11 modules ...done.
Setting up the Window System to use the Guest Additions ...done.
You may need to restart the the Window System (or just restart the guest system)
to enable the Guest Additions.
Installing graphics libraries and desktop services components ...done.
An error occurred during installation of VirtualBox Guest Additions 5.0.14. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
stdin: is not a tty
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => D:/2/EDX/dogwood/devstack
[D:\2\edx\dogwood\devstack]$ vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
###############终于起来了!!!唉
$ vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:
Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/yury/.vagrant.d/boxes/dogwood-devstack-rc2/0/virtualbox/vagrant_private_key
$ssh vagrant@127.0.0.1 2222
####用上面给出的密钥登录系统
Connecting to 127.0.0.1:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
New release '14.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine.
Last login: Tue Dec 22 02:47:44 2015 from 10.0.2.2
登录成功!!!启动lms和studio服务需要切换至edxapp用户,先通过命令设置一个口令
vagrant$sudo passwd edxapp
::: 输入口令
vagrant$su edxapp
edxapp$paver lms --fast #启动lms,在浏览器登录(如果之前设置了端口映射,则可以用127.0.0.1:8000浏览页面,否则需要访问预先设置(修改Vagrantfile中相应的值)的ip:8000来访问
Django version 1.8.7, using settings 'lms.envs.devstack'
Starting development server at http://0.0.0.0:8000/
edxapp$paver studio --fast
Django version 1.8.7, using settings 'cms.envs.devstack'
Starting development server at http://0.0.0.0:8001/
至此,基于vagrant的devstack设置成功,后面还会继续介绍如何进行详细的功能配置,以及与主流的开发环境结合的技巧,
to be continued ... ...