How to Install RVM via Vagrant and Puppet - ruby-on-rails

I'm provisioning a new Vagrant box for Ruby on Rails development (using VirtualBox) and would like to add RVM + ruby 2.3.0 as part of the Vagrant provisioning process.
My Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/manifests"
puppet.module_path = "puppet/modules"
puppet.manifest_file = "development.pp"
end
end
And puppet/manifests/development.pp:
class requirements {
group { "puppet": ensure => "present", }
exec { "apt-update":
command => "/usr/bin/apt-get -y update"
}
package {
["mysql-client", "mysql-server", "libmysqlclient-dev"]:
ensure => installed, require => Exec['apt-update']
}
}
include requirements
This just installs mysql at the moment. I'd like to add RVM + install a default ruby (2.3.0 for example).
There is this guide:
http://blog.csanchez.org/2014/01/14/installing-rvm-and-multiple-ruby-versions-with-puppet/
Which uses this puppet module:
https://forge.puppetlabs.com/maestrodev/rvm
I don't have puppet installed, because when I try to run:
puppet module install maestrodev-rvm
I get "-bash: puppet: command not found".
Looking through the Puppet docs, it appears rather complicated to install/setup a Puppet client/server. This seems like overkill just to use the RVM puppet module. Also, designers on the team will be using this process so it needs to be as simple as possible.
Any help would be greatly appreciated.

I don't have puppet installed
Yes, you do - puppet is installed on your VM, otherwise you would not be able to provision mysql and other.
You probably run the command puppet module install maestrodev-rvm from your host, while this needs to be run on the VM.
There are different ways how people have the modules setup on the VM (librarian, some downloads all in module/ folder...) what I do is to create a shell provisioning which will install all the necessary modules.
In your vagrantfile add
config.vm.provision "shell", path: "puppet/script/install-puppet-modules.sh"
make sure this line is before your puppet provision - the install-puppet-modules.sh will be something like
#!/bin/bash
mkdir -p /etc/puppet/modules;
if [ ! -d /etc/puppet/modules/maestrodev/rvm ]; then
puppet module install maestrodev-rvm --version xxx
fi
I like to make a point to the version so if there's a new version of the module in the forge it might break, at least I know that version xxx has been tested.
So now you're able to add class { 'rvm': } and so on to install rvm and ruby in your puppet/manifests/development.pp file

Related

Make PowerShell recommendation like Linux-bash (E.g. docker)

I have windows 10 OS with WSL enabled and docker for windows installed.
When I type docker in PowerShell and hit tab, it suggests me with the corresponding folders and files in my working directory.
here AndroidStudioProjects is a directory in my working directory.
On the other hand,
When I type docker in WSL Ubuntu and hit tab, it suggests the available docker commands themselves. (My expected behavior)
I want PowerShell to also recommend like WSL ubuntu.
Presumably:
docker on WSL comes with tab-completion for POSIX-compatible shells such as bash, installed via the shell's initialization files.
no such support is provided for PowerShell, but there are third-party solutions - see below.
Installing PowerShell tab-completion for docker:
Install the DockerCompletion module from the PowerShell Gallery:
# Install the module in the scope of the current user.
Install-Module DockerCompletion -Scope CurrentUser
# Import the module into the session.
# Add this line to your $PROFILE file to make the tab-completion
# available in future sessions.
Import-Module DockerCompletion
Installing PowerShell tab-completion for all supported programs (CLIs):
The posh-cli meta-module - whose repo is here - offers a convenient way to automatically install tab-completion support for all locally installed CLIs for which application-specific tab-completion modules are available:
# Install the meta-module in the scope of the current user.
Install-Module posh-cli -Scope CurrentUser
# This looks for locally installed CLIs for which tab-completion
# modules are available, installs them, and adds
# Import-Module commands to your $PROFILE file.
Install-TabCompletion
See the README for more information.

Cant perform query in rails : may have been in progress in another thread when fork() was called

I am running under macOS Catalina version 10.15.1
When I run my rails console on my project and try to perform a query like User.first I get :
objc[57093]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.
objc[57093]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.
I followed this answer and added OBJC_DISABLE_INITIALIZE_FORK_SAFETY to my .zshrcfile which looks like that :
ZSH=$HOME/.oh-my-zsh
# You can change the theme with another one:
# https://github.com/robbyrussell/oh-my-zsh/wiki/themes
ZSH_THEME="robbyrussell"
# Useful oh-my-zsh plugins for Le Wagon bootcamps
plugins=(git gitfast zsh-autosuggestions last-working-dir zsh-syntax-highlighting common-aliases history-substring-search)
# Prevent Homebrew from reporting - https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Analytics.md
export HOMEBREW_NO_ANALYTICS=1
# Actually load Oh-My-Zsh
source "${ZSH}/oh-my-zsh.sh"
unalias rm # No interactive rm by default (brought by plugins/common-aliases)
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
# Load rbenv if installed (To manage your Ruby versions)
export PATH="${HOME}/.rbenv/bin:${PATH}"
type -a rbenv > /dev/null && eval "$(rbenv init -)"
# Load nvm if installed (To manage your Node versions)
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"
# Anaconda binaries (python, pip, conda, jupyter, pytest, pylint etc.)
export PATH="/anaconda3/bin:${HOME}/anaconda3/bin:${PATH}"
# Rails and Ruby uses the local `bin` folder to store binstubs.
# So instead of running `bin/rails` like the doc says, just run `rails`
# Same for `./node_modules/.bin` and nodejs
export PATH="./bin:./node_modules/.bin:${PATH}:/usr/local/sbin"
# Store your own aliases in the ~/.aliases file and load the here.
[[ -f "$HOME/.aliases" ]] && source "$HOME/.aliases"
# Encoding stuff for the terminal
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export EDITOR=atom
When I run in my terminal echo $OBJC_DISABLE_INITIALIZE_FORK_SAFETY I get YES, so I think the environment variable is correcly set... but that doesnt fix the issue.
How can I fix this problem ?
What worked for me, was disbaling Spring: export DISABLE_SPRING=true before starting your Rails console or server
I ran into this problem after I updated my Postgres database. I ended up updating to the latest ruby version and reinstalling all gems. After that everything worked again.
I tried the OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES suggestion as well, to no avail.

verify that an OS package is installed

I need to verify that an OS package is installed after deploying using capistrano (it's a rails project, in case it matters). I'd like to support the major linux distros and OS X. Fortunately, the name of the package is the same on all platforms.
I've thought adding a capistrano task, something like (untested code):
%w(yum apt-get brew).each do |manager|
path = `which #{manager}`.chomp
if path && path.size > 0
`#{path} install -y #{PKG}`
return
end
end
Inspired by this question.
Is there a better way?
I've thought checking uname, but it doesn't always have the distro, just "Linux". I also thought using lsb_release or listing files in /etc/*-release, but not all distros support it (e.g. centos).

Unable to run ruby script over ssh into ec2 instance

I'm working with ec2 instances and was trying to execute a ruby script on another instance after ssh to that instance.
I have a ruby script which updates configuration files, so i need to run that script as super user. when i run the script manually on that instance, sudo ruby recreate-532d01c.rb, the error that comes is
sudo: ruby: command not found
Running simple scripts with no root permissions works, eg.ruby file_1.rb.
Using rvmsudo in place of sudo executes the script with warning,
ubuntu#ip-10-0-0-111:~$ rvmsudo ruby recreate-82bb000012.rb
Warning: can not check `/etc/sudoers` for `secure_path`, falling back to call via `/usr/bin/env`, this breaks rules from `/etc/sudoers`. Run:
export rvmsudo_secure_path=1
to avoid the warning, put it in shell initialization file to make it persistent.
In case there is no `secure_path` in `/etc/sudoers`. Run:
export rvmsudo_secure_path=0
to avoid the warning, put it in shell initialization file to make it persistent.
I tried to execute the below command from rails console of one of the instance to test and it fails to recognize ruby as command
1.9.3-p545 :002 > system("ssh -i /home/ubuntu/.ssh/own_key.pem ubuntu#**.***.***.** ruby execute-52d.rb")
bash: ruby: command not found
I tried with possible solutions over web, but could not resolve the issue. I have the same configuration running for one of my old aws acount, this is a newly created account. Not sure if this could be issue in any way as currently ec2 instances fall under vpc by default and have some changes after dec 2013
Nothing to do with your VPC. So when you run your ruby script with sudo your environment that your user is using doesn't get set for Ruby.
Sounds like you may be using rvm and you probably set it up with a 'single user' config.
Try running as your user:
which ruby
and see where your ruby executable is located at. That's what you have to make sure that when your run your script as sudo it's available in the PATH.
Worst case you would have to reinstall rvm with multiuser config which should work when you run with sudo:
user$ \curl -sSL https://get.rvm.io | sudo bash -s stable

Installing Ruby on Rails on Windows via CYGWIN

I am attempting to follow instructions on this page:
http://www.agilereasoning.com/2011/05/25/ruby-on-rails-on-windows-7-using-cygwin/
I have been trying to install Rails with varying success first using the railsinstaller and I encounter difficulties like no vim and I couldn't copy and paste from the Windows command prompt so I install CYGWIN. It didn't download the files correctly so I downloaded them manually and some were hard to locate. I couldn't find the final package as a .tar file so I downloaded libxslt-devel-1.1.20-1.i386.rpm.
Right click the Cygwin shortcut and choose edit from the menu. Change the contents to >match:
1 #echo off
2 C:\cygwin\bin\rxvt -sr -sl 1500 -e C:\cygwin\bin\bash.exe --login -i
Do I have to put this as a command to run on the executable or inside the cygwin commmand prompt? When I try to do that I get this:
-bash: 'command': command not found
Lots of things wrong here.
You can copy/paste the command prompt with Edit->Mark or Edit->Paste
libxslt-devel-1.1.20-1.i386.rpm is a linux file.
C:\cygwin\bin\rxvt - why are you mesing around with rxvt?
I recommend you install the rubyinstaller + devkit and then do gem install rails. However don't expect to be happy with rails' performance on windows.
Ruby on Rails on Windows via CYGWINTry:
Install Vagrant
Virtual Box,
and Cygwin (or PuTTy, I am using Cygwin).
With this set, open Cygwin, go to your project folder, run vagrant init <box> (my box is hashicorp/precise64 - see others)
(you may also want to cfg your Vagrantfile?). All set, Run: vagrant up and vagrant ssh
Now you have a virtual machine (Ubuntu) running, and you can install rvm (recommended... so you can have different versions of Ruby), or go directly with ruby, rails, etc.. (sudo apt-get ruby -v x.x.x,etc)
- Vagrant "creates and configures lightweight, reproducible, and portable development environments".
- Cygwin helps with ssh issues, etc...
- Virtual Box manages the machine (Ubuntu, or other OS)
With this set, I have no problem at all running Ruby (on Rails) with Windows.

Resources