I am not sure if I should use Puppet for this. I update and install through provision.sh.
My Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu32"
config.vm.provision :shell, path: './provision.sh'
config.vm.network "public_network"
end
provision.sh
apt-get update
apt-get -y install build-essential git-core python-software-properties nodejs
apt-get -y install vim
apt-get -y install curl
curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
Now I need to add the following to ~/.bashrc at the top. Or I can prepare a file .bashrc and replace it with ~/.bashrc
export RBENV_ROOT="${HOME}/.rbenv"
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
Then run source .bashrc
Then run following commands.
rbenv install 2.0.0-p247
rbenv rehash
gem install bundler
bundle
sudo apt-get install libpq-dev
gem install pg -v '0.15.0'
You can do this in your provision.sh script. Vagrant automatically shares the directory where your Vagrantfile lives with the guest VM as the /vagrant folder.
Create the .bashrc file as you want it, and put it in the same directory as your Vagrantfile. I would leave out the '.' and call it bashrc so you don't loose track of it.
Then you can add to your provision.sh script:
cp /vagrant/bashrc ~/.bashrc
source ~/.bashrc
Note: the bash provisioning runs as the root user, you'll have to modify this a bit if you want to use it as a non-root user.
cp /vagrant/bashrc /home/<username>/.bashrc
su - <username> -c "<command to run as user>"
Related
I'm using this vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
...bla bla bla bla bla...
config.vm.provision "shell", path: "provision/setup.sh"
end
Since I want to install Linuxbrew I have in my provision/setup.sh this code:
sudo apt-get update
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
# or maybe also this: (but nothing anyway):
# sudo git clone https://github.com/Linuxbrew/linuxbrew.git /home/vagrant/.linuxbrew
export PATH=$HOME/.linuxbrew/bin:$PATH
brew doctor
But I retrieve errors:
==> default: /tmp/vagrant-shell: line 35: brew: command not found
How to fix this?
There's an issue how you run your script - as you run with config.vm.provision "shell", path: "provision/setup.sh" vagrant will run it as root user and so you do not need sudo
however you should really run it as your user so do config.vm.provision "shell", path: "provision/setup.sh", privileged: false
also the export will not be saved for your future session so add it to .bashrc file something like echo PATH=$HOME/.linuxbrew/bin:$PATH >> .bashrc so the final script would look like
sudo apt-get update
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
echo PATH=$HOME/.linuxbrew/bin:$PATH >> ~/.bashrc
export PATH=$HOME/.linuxbrew/bin:$PATH
brew doctor
The export is needed if you run brew from the script but note that brew doctor will likely ends up with warning and do not return so you might end up seeing vagrant message as
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
and finally for the original error, #BMW gets all credit adding yes | to the command will default the enter key on the question
Let me guess. Add yes before the ruby command
yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
I'm using this vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
...bla bla bla bla bla...
config.vm.provision "shell", path: "provision/setup.sh", privileged: false
end
In my setup.sh I have:
sudo apt-get update
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
echo PATH=$HOME/.linuxbrew/bin:$PATH >> ~/.bashrc
export PATH=$HOME/.linuxbrew/bin:$PATH
brew doctor
then I have:
brew install rbenv ruby-build
brew update
brew upgrade
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
. ~/.bashrc
rbenv init
rbenv install 2.3.0
rbenv global 2.3.0
rbenv local 2.3.0
rbenv versions
gem install bundler
but I receive this error:
==> default: ERROR: While executing gem ... (Errno::EACCES) ==> default: Permission denied - /var/lib/gems`
How can I install "bundler" and other "gems" with my setup script?
It's not a good idea to run sudo in a script, but try sudoing the script itself with
$ sudo ./script.sh
and take all your 'sudos' out of your sh script.
Next change
config.vm.provision "shell", path: "provision/setup.sh", privileged: false
to
config.vm.provision "shell", path: "provision/setup.sh", privileged: true
This way sudo is only unlocked for the file, and it has control for whatever you are doing.
If this is a new file in the machine, you may need to use
chmod +x ./script.sh
for your os to execute it properly. From what I see, things should work okay unless you have accidentally provisioned your vm twice in which you may want to override it. You also can try running each command yourself to see if it is an isolated issue outside of vagrant.
edit:
The main issue is that the /var/lib/gems directory is own by root, so by whatever means get that under the sudo command.
edit: Massive clarity.
I'm trying to create a Dockerfile that configures my ruby environment. But it is failing when I try to run docker run -it
Dockerfile
FROM centos:6.6
# VOLUME /var/lib/mysql
# ENV HOME /root
# ENV PATH $HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH
# ENV SHELL /bin/bash
RUN yum update -y
RUN yum install git openssl-devel openssl readline-devel readline zlib-devel zlib libxml2-devel libxml2 libxslt-devel libxslt nginx tar gcc libaio libaio-devel -y
RUN rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.5.1-1.el6.x86_64.rpm
RUN yum install -y openssh-server
RUN mkdir -p /var/run/sshd
RUN adduser deploy -p Password1
RUN su - deploy
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN ./root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
RUN echo 'eval "$(rbenv init -)"' >> .bashrc
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile
RUN source $HOME/.bash_profile
ENV CONFIGURE_OPTS --disable-install-doc
RUN rbenv install 2.2.3
RUN rbenv global 2.2.3
RUN bash -l -c 'gem update --system'
RUN bash -l -c 'gem update'
RUN bash -l -c 'gem install nokogiri -- --use-system-libraries'
RUN bash -l -c 'gem install bundler rails-api --no-rdoc --no-ri'
RUN touch /etc/sysconfig/network
EXPOSE 3306
EXPOSE 22
EXPOSE 80
EXPOSE 3389
error
Error response from daemon: No command specified
Look at my Dockerfile:
FROM centos:6.6
RUN yum update -y
# Install the rbenv and Ruby dependencies
RUN yum install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev -y
RUN yum install git openssl-devel openssl readline-devel readline zlib-devel zlib libxml2-devel libxml2 libxslt-devel libxslt nginx tar gcc libaio libaio-devel -y
RUN rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.5.1-1.el6.x86_64.rpm
RUN yum install -y openssh-server
RUN mkdir -p /var/run/sshd
RUN adduser deploy -p Password1
RUN su - deploy
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN ./root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
RUN echo 'eval "$(rbenv init -)"' >> .bashrc
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile
RUN source $HOME/.bash_profile
ENV CONFIGURE_OPTS --disable-install-doc
RUN rbenv install 2.2.3
RUN rbenv global 2.2.3
RUN bash -l -c 'gem update --system'
RUN bash -l -c 'gem update'
RUN bash -l -c 'gem install nokogiri -- --use-system-libraries'
RUN bash -l -c 'gem install bundler rails-api --no-rdoc --no-ri'
RUN touch /etc/sysconfig/network
EXPOSE 3306
EXPOSE 22
EXPOSE 80
EXPOSE 3389
When running my docker container it knows rbenv global. But when ever it try to find ruby via ruby or which ruby or whereis ruby I get nothing. It also doesn't recognize rails-api or gem. What is going on?
Dockerfile
FROM centos:6.6
RUN yum update -y
RUN yum install git openssl-devel openssh-server sudo openssl readline-devel readline zlib-devel zlib libxml2-devel libxml2 libxslt-devel libxslt nginx tar gcc libaio libaio-devel -y
RUN rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.5.1-1.el6.x86_64.rpm
RUN sed -i -e "s/Defaults requiretty.*/ #Defaults requiretty/g" /etc/sudoers
RUN mkdir -p /var/run/sshd
RUN useradd -m -u 1000 -G wheel deploy && echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/wheel
USER deploy
RUN mkdir ~/dev
RUN git clone https://github.com/sstephenson/rbenv.git ~/.rbenv/
RUN git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
ENV PATH ~/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
RUN source ~/.bash_profile
ENV CONFIGURE_OPTS --disable-install-doc
RUN rbenv install 2.2.3
RUN rbenv global 2.2.3
RUN bash -l -c 'gem update --system'
RUN bash -l -c 'gem update'
RUN bash -l -c 'gem install nokogiri -- --use-system-libraries'
RUN bash -l -c 'gem install bundler rails-api --no-rdoc --no-ri'
COPY oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm /tmp/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
COPY oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm /tmp/oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
COPY oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm /tmp/oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
RUN sudo rpm -Uvh /tmp/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
RUN sudo rpm -Uvh /tmp/oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
RUN sudo rpm -Uvh /tmp/oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
RUN sudo touch /etc/sysconfig/network
RUN NLS_LANG=American_America.UTF8
ENV ORACLE_HOME=/usr/lib/oracle/12.1/client64
ENV LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib
EXPOSE 22
EXPOSE 3000
EXPOSE 5000
The issue at hand here is that every RUN statement in the Dockerfile happens in its own environment. The RUN source ~/.bash_profile will set $PATH to have /root/.rbenv/shims in it. But subsequent RUN will not have this environment set as they won't source .bash_profile since this isn't an interactive shell.
The easy way to solve this is to just add a ENV PATH $HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH somewhere before the ruby and gem commands. This is really the only bit you should need from the rbenv init shell script.
Have you tried rbenv rehash after installing the new Ruby version?
Your Dockerfile only uses one version of Ruby. Given your use case, I would recommend just using the official ruby docker images: https://hub.docker.com/_/ruby
I have a vagrant provisioning script which succeeds -- I can see the output from the logs and my dependencies are being installed, my directories and files are being created and copied over, etc. but when I vagrant ssh into the VM none of the folders, files, env variables, and installations are there.
Edit: git, curl, etc. work, but gvm, go, and $GOPATH etc do not, and my go directory does not exist
I'm confident the provisioning works correctly because I can run my web server from the script and confirm the application is being served.
Is this just the way Vagrant is set up? What's the point of vagrant ssh if so?
I'm running the default "hashicorp/precise32" box, Ubuntu 12.04, default provider.
Shell script
#! /bin/bash
echo "Provisioning virtual machine"
sudo apt-get update
echo "Installing Dependencies"
# Base dependencies: curl git
# Dependencies for gvm: make bison
sudo apt-get install curl git make bison -y 2> /dev/null
# Dependencies for add-apt-repository: python-software-properties software-properties-common
sudo apt-get install python-software-properties software-properties-common -y 2> /dev/null
# This allows us to get an updated version of git, which we need for gvm
sudo add-apt-repository ppa:git-core/ppa -y 2> /dev/null
sudo apt-get update
sudo apt-get install git -y 2> /dev/null
echo "Installing GVM"
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
source ~/.gvm/scripts/gvm
echo "Installing and configuring Go"
gvm install go1.4
gvm use go1.4 --default
mkdir -p ~/go/{bin,pkg,src}
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
echo "Installing Nginx"
sudo apt-get install nginx -y 2> /dev/null
Vagrantfile
`# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, path: "init.sh"
config.vm.network :forwarded_port, host: 4000, guest: 8080
end`
The Vagrant provisioning script is running as root, so ~ refers to /root instead of /home/vagrant.
Options to resolve are su -c "*your command*" vagrant or using absolute paths, which is probably the best approach.
Similar issue: Why is my Vagrant bootstrap file not modifying bash_login?
And more detail in my answer here: Vagrant - Rails Not Installed