如何建立起基於Vagrant的Open EDX的虛擬開發環境(二)

zlipper發表於2016-02-15
部分內容轉載從

Windows上使用Vagrant打造Laravel Homestead可協同跨平臺開發環境

大家對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


環境準備

  1. Git(非必需)
  2. PHP(非必需)
  3. Laravel(非必需)
  4. Composer(非必需)
  5. Vagrant
  6. 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 ... ...

相關文章