Vagrant执行命令行用户级别

时间:2017-07-14 22:38:10

标签: vagrant ubuntu-16.04

在使用vagrant构建VM时,您是否可以帮助我在用户级执行命令?具体来说,我试图更新"启动器图标"在Ubuntu的屏幕左侧。在下面的vagrantfile中,我在内联shell和内联脚本中尝试了这一点。如果您能提供任何帮助,我将不胜感激。

$script = <<SCRIPT
    echo "Configuring Launcher Icons..."
    gsettings set com.canonical.Unity.Launcher favorites "['application://org.gnome.Nautilus.desktop', 'application://chromium-browser.desktop', 'unity://running-apps', 'application://terminator.desktop', 'unity://expo-icon', 'unity://devices']"
    service lightdm restart
SCRIPT

Vagrant.configure(2) do |config|
    config.vm.box = "box-cutter/ubuntu1604-desktop"

    config.vm.provider "virtualbox" do |vb|
        # Display the VirtualBox GUI when booting the machine
        vb.gui = true
        vb.name = "UpdateLauncherIcons"

        # Customize the amount of memory on the VM:
        vb.memory = "4096"
    end

    config.vm.provision "shell", inline: <<-SHELL
        # Update OS
        apt-get update
        apt-get upgrade
        apt-get --yes install git
        apt-get --yes install terminator
        apt-get --yes install chromium-browser

        gsettings set com.canonical.Unity.Launcher favorites "['application://org.gnome.Nautilus.desktop', 'application://chromium-browser.desktop', 'unity://running-apps', 'application://terminator.desktop', 'unity://expo-icon', 'unity://devices']"
        service lightdm restart

    SHELL

    config.vm.provision "shell", inline: $script

end

注意,vagrant确实在执行内联脚本时提供错误消息:

==> default: Running provisioner: shell...
default: Running: inline script
==> default: Configuring Launcher Icons...
==> default:
==> default: (process:6221): dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY

进一步测试确认以下命令在终端窗口内的客户机中执行时运行良好。同样,挑战是让这个命令从主机上执行的vagrantfile开始工作。

gsettings set com.canonical.Unity.Launcher favorites "['application://org.gnome.Nautilus.desktop', 'application://chromium-browser.desktop', 'unity://running-apps', 'application://terminator.desktop', 'unity://expo-icon', 'unity://devices']"

1 个答案:

答案 0 :(得分:0)

Haven长期使用Ubuntu Desktop Vagrant盒子,但我们团队项目中的类似设置使用&#34; privileged:false&#34;对于shell配置程序:

config.vm.provision "shell", privileged: false, path: "scripts/setup-desktop.sh"
config.vm.provision "shell", privileged: false, path: "scripts/configure-desktop.sh"

以及设置脚本设置自动登录

echo "autologin-user=vagrant" | sudo tee -a /etc/lightdm/lightdm.conf

# restart desktop
sudo service lightdm restart

下一个配置脚本然后使用一些自动启动功能来运行它

#!/bin/bash

#modify shortcuts on ubuntu cause that are used in eclipse
cat <<AUTOSTART_SHORTCUTS | sudo tee /home/vagrant/bin/modify-shortcuts
#!/bin/sh
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-down []
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-up []
#Disable Resize window (Alt + F8)
gsettings set org.gnome.desktop.wm.keybindings begin-resize []
#Disable move window (Alt + F7)
gsettings set org.gnome.desktop.wm.keybindings begin-move []

AUTOSTART_SHORTCUTS
sudo chmod 777 /home/vagrant/bin/modify-shortcuts

cat <<GSETTINGS | sudo tee /home/vagrant/bin/setup-unity
#!/bin/sh
# set favourites in launcher
gsettings set  com.canonical.Unity.Launcher favorites "['application://nautilus.desktop', 'application://gnome-terminal.desktop', 'application://firefox.desktop', 'application://chromium-browser.desktop', 'application://gvim.desktop', 'application://IDEA.desktop']"

# set german keyboard
gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'de'), ('xkb', 'us')]"

#Disabling screensaver
#gsettings set org.gnome.desktop.screensaver idle-activation-enabled false
gsettings set org.gnome.desktop.session idle-delay 0

# disable screensaver locking desktop
gsettings set org.gnome.desktop.screensaver lock-enabled false

# cleanup
sudo rm -f /home/vagrant/.config/autostart/setup-unity.desktop
GSETTINGS
sudo chmod 777 /home/vagrant/bin/setup-unity

mkdir -p .config/autostart

cat <<AUTOSTART_SHORTCUTS_DT | sudo tee /home/vagrant/.config/autostart/modify-shortcuts.desktop
[Desktop Entry]
Type=Application
Exec=/home/vagrant/bin/modify-shortcuts
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=modify-shortcuts
Name=modify-shortcuts
Comment[en_US]=
Comment=
AUTOSTART_SHORTCUTS_DT
cat <<GSETTINGS_DT | sudo tee /home/vagrant/.config/autostart/setup-unity.desktop
[Desktop Entry]
Type=Application
Exec=/home/vagrant/bin/setup-unity
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=setup-unity
Name=setup-unity
Comment[en_US]=
Comment=setup-unity
GSETTINGS_DT

# restart desktop
sudo service lightdm restart