Last active
April 28, 2019 09:36
-
-
Save larryli/2416426685236041393ff402cf66be51 to your computer and use it in GitHub Desktop.
ESP-IDF Vagrantfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
class VagrantPlugins::ProviderVirtualBox::Config < Vagrant.plugin("2", :config) | |
def update_customizations(customizations) | |
@customizations = customizations | |
end | |
end | |
class VagrantPlugins::ProviderVirtualBox::Action::Customize | |
alias_method :original_call, :call | |
def call(env) | |
machine = env[:machine] | |
config = machine.provider_config | |
driver = machine.provider.driver | |
uuid = driver.instance_eval { @uuid } | |
if uuid != nil | |
lines = driver.execute('showvminfo', uuid, '--machinereadable', retryable: true).split("\n") | |
filters = {} | |
lines.each do |line| | |
if matcher = /^USBFilterVendorId(\d+)="(.+?)"$/.match(line) | |
id = matcher[1].to_i | |
vendor_id = matcher[2].to_s | |
filters[id] ||= {} | |
filters[id][:vendor_id] = vendor_id | |
elsif matcher = /^USBFilterProductId(\d+)="(.+?)"$/.match(line) | |
id = matcher[1].to_i | |
product_id = matcher[2].to_s | |
filters[id] ||= {} | |
filters[id][:product_id] = product_id | |
end | |
end | |
config.update_customizations(config.customizations.reject { |_, command| filter_exists(filters, command) }) | |
end | |
original_call(env) | |
end | |
def filter_exists(filters, command) | |
if command.size > 6 && command[0] == 'usbfilter' && command[1] == 'add' | |
vendor_id = product_id = false | |
i = 2 | |
while i < command.size - 1 do | |
if command[i] == '--vendorid' | |
i += 1 | |
vendor_id = command[i] | |
elsif command[i] == '--productid' | |
i += 1 | |
product_id = command[i] | |
end | |
i += 1 | |
end | |
if vendor_id != false && product_id != false | |
filters.each do |_, filter| | |
if filter[:vendor_id] == vendor_id && filter[:product_id] == product_id | |
return true | |
end | |
end | |
end | |
end | |
return false | |
end | |
end | |
Vagrant.configure("2") do |config| | |
config.vm.box = "ubuntu/bionic64" | |
# Disable automatic box update checking. If you disable this, then | |
# boxes will only be checked for updates when the user runs | |
# `vagrant box outdated`. This is not recommended. | |
# config.vm.box_check_update = false | |
# Provider-specific configuration so you can fine-tune various | |
# backing providers for Vagrant. These expose provider-specific options. | |
# Example for VirtualBox: | |
# | |
config.vm.provider "virtualbox" do |vb| | |
# # Customize the amount of memory on the VM: | |
# vb.memory = "1024" | |
# Enable USB | |
vb.customize ['modifyvm', :id, '--usb', 'on'] | |
# Please use your esp device id, run `VBoxManage.exe list usbhost` list devices | |
vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'ESP', '--vendorid', '10c4', '--productid', 'ea60'] | |
# Disable log | |
vb.customize ['modifyvm', :id, '--uartmode1', 'disconnected'] | |
end | |
# Enable provisioning with a shell script. Additional provisioners such as | |
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the | |
# documentation for more information about their specific syntax and use. | |
config.vm.provision "shell", inline: <<-SHELL | |
GCC_VERSION='xtensa-esp32-elf-gcc (crosstool-NG crosstool-ng-1.22.0-80-g6c4433a) 5.2.0' | |
IDF_VERSION='v3.2' | |
apt-get -qq update && apt-get -qq upgrade -y | |
apt-get -qq install -y linux-image-extra-virtual | |
apt-get -qq install -y gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing | |
if [ ! -d /opt/local/espressif/ ]; then | |
mkdir -p /opt/local/espressif/ | |
fi | |
gccVersion=`/opt/local/espressif/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc --version | head -n 1` | |
if [ "$gccVersion" != "$GCC_VERSION" ]; then | |
wget -qO- https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar xz -C /opt/local/espressif/ | |
fi | |
if [ ! -d /opt/local/espressif/esp-idf/ ]; then | |
git clone -q -b $IDF_VERSION --recursive https://github.com/espressif/esp-idf.git /opt/local/espressif/esp-idf | |
else | |
idfVersion=`cd /opt/local/espressif/esp-idf/; git describe --exact-match` | |
if [ "$idfVersion" != "$IDF_VERSION" ]; then | |
cd /opt/local/espressif/esp-idf/; git fetch && git checkout $IDF_VERSION && git submodule update --init --recursive | |
fi | |
fi | |
chown -R vagrant: /opt/local/espressif/ | |
usermod -a -G dialout vagrant | |
SHELL | |
config.vm.provision 'shell', privileged: false, inline: <<-SHELL | |
python -m pip install -q --user -r /opt/local/espressif/esp-idf/requirements.txt | |
grep -q 'IDF_PATH' /home/vagrant/.profile || echo "export IDF_PATH=\"/opt/local/espressif/esp-idf\"\nexport PATH=\"/opt/local/espressif/xtensa-esp32-elf/bin:${PATH}\"\ncd /vagrant" >> /home/vagrant/.profile | |
SHELL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment