Created
April 12, 2017 18:00
-
-
Save kimniche/8f322aff8c006235fdf6eb7293daed99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 : | |
| require "yaml" | |
| # Load our default config file, 'config/default.yaml' | |
| CONF = YAML.load(File.open("config/default.yaml", File::RDONLY).read) | |
| # Next, load local overrides from 'config/local.yaml' | |
| # If it doesn't exist, no worries. We'll just use the defaults | |
| if File.exists?("config/local.yaml") | |
| CONF.merge!(YAML.load(File.open("config/local.yaml", File::RDONLY).read)) | |
| end | |
| Vagrant.configure(2) do |config| | |
| #___VM CONFIG | |
| config.vm.box = "ubuntu/trusty64" | |
| config.vm.hostname = CONF['vm_name'] | |
| config.vm.provider "virtualbox" do |vb| | |
| # Display the VirtualBox GUI when booting the machine | |
| vb.gui = false | |
| # Customize the amount of memory on the VM: | |
| vb.memory = "2048" | |
| end | |
| # Set up source directory as a synced folder to support the use of an IDE on the host machine | |
| if CONF['sync_src_to_host'] == true | |
| config.vm.synced_folder CONF['host_go_src'], "/home/vagrant/go/src" | |
| end | |
| # Prevent annoying "stdin: not a tty" errors | |
| config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" | |
| #___NETWORK | |
| config.vm.network "public_network" | |
| # Open ports for all services declared in repos | |
| CONF['repos'].each do |repo| | |
| port = repo['port'] | |
| config.vm.network "forwarded_port", guest: port, host: port | |
| end | |
| #___SSH AND GITHUB | |
| # Enable SSH Forwarding | |
| config.ssh.forward_agent = true | |
| # If Windows, copy github_rsa | |
| if Vagrant::Util::Platform.windows? | |
| # You MUST have a ~/.ssh/github_rsa (GitHub specific) SSH key to copy to VM | |
| if File.exists?(File.join(Dir.home, ".ssh", "github_rsa")) | |
| # Read local machine's GitHub SSH Key (~/.ssh/github_rsa) | |
| github_ssh_key = File.read(File.join(Dir.home, ".ssh", "github_rsa")) | |
| # Copy it to VM as the /root/.ssh/id_rsa key | |
| config.vm.provision :shell, :inline => "echo 'Windows-specific: Copying local GitHub SSH Key to VM for provisioning...' && \ | |
| mkdir -p /root/.ssh && \ | |
| mkdir -p /home/vagrant/.ssh && \ | |
| echo '#{github_ssh_key}' > /root/.ssh/id_rsa && \ | |
| echo '#{github_ssh_key}' > /home/vagrant/.ssh/id_rsa && \ | |
| chmod 600 /root/.ssh/id_rsa && \ | |
| chmod 600 /home/vagrant/.ssh/id_rsa && \ | |
| chown vagrant:vagrant /home/vagrant/.ssh/id_rsa" | |
| else | |
| # Else, throw a Vagrant Error. Cannot successfully startup on Windows without a GitHub SSH Key! | |
| raise Vagrant::Errors::VagrantError, "\n\nERROR: GitHub SSH Key not found at ~/.ssh/github_rsa (required on Windows).\nYou can generate this key manually OR by installing GitHub for Windows (http://windows.github.com/)\n\n" | |
| end | |
| end | |
| config.vm.provision "shell", inline: <<-SHELL | |
| echo 'Defaults env_keep+=SSH_AUTH_SOCK' >> /etc/sudoers | |
| echo 'Defaults env_keep+=GOPATH' >> /etc/sudoers | |
| echo 'Defaults env_reset' >> /etc/sudoers | |
| SHELL | |
| #___APP PROVISIONING | |
| # Install dependencies, golang and environment variables | |
| config.vm.provision "shell", path: "./config/dependencies.sh", env: {"VERSION" => CONF['go_version']} | |
| config.vm.provision "shell", inline: <<-SHELL | |
| sudo -u vagrant -H bash -c 'git config --global url."[email protected]:".insteadOf "https://github.com/"' | |
| git config --global url."[email protected]:".insteadOf "https://github.com/" | |
| SHELL | |
| # Create upstart init scripts to run app as a service | |
| # Go get repos and checkout approriate branches | |
| config.vm.provision "file", | |
| source: "./config/upstart.tpl", | |
| destination: "~/upstart.tpl" | |
| config.vm.provision "file", | |
| source: "./config/service_manager.sh", | |
| destination: "/home/vagrant/service_manager.sh" | |
| CONF['repos'].each do |repo| | |
| service = repo['name'] | |
| branch = repo['branch'] | |
| config.vm.provision "shell" do |s| | |
| s.privileged = false, | |
| s.inline = "bash /home/vagrant/service_manager.sh $@", | |
| s.args = ["#{service}", "#{branch}"] | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment