Skip to content

Instantly share code, notes, and snippets.

@jthurteau
Last active February 14, 2019 18:04
Show Gist options
  • Save jthurteau/993cc51edb5c7788ae41fb7a348dcec1 to your computer and use it in GitHub Desktop.
Save jthurteau/993cc51edb5c7788ae41fb7a348dcec1 to your computer and use it in GitHub Desktop.
Base Vagrant/Puppet Setup
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'resolv'
realm_mode = true
realm_key = 'KEY_HERE'
rhel_org = 'YOUR_ORG'
app_name = 'APP_NAME'
puppet_stack = 'apache-python-postgres'
vagrant_guest_path = '/vagrant'
manifests_path = 'puppet/manifests'
local_dynamic_manifest = 'local-dev.pp'
ldm_path = "#{manifests_path}/#{local_dynamic_manifest}"
ldm_token = 'puppet made this'
ppp = ['global', (realm_mode ? 'realm' : 'nomad')]
puppet_stack.split('-').each { |a| ppp.push(a) }
ppp.push(app_name)
ppp.push('override')
begin
puppet_facts = YAML.load_file('.puppet_facts')
rescue SystemCallError => e
print " Warning: puppet facts not available (this is a critical error on up, reload, provision, or resume)\n\r"
end
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = realm_mode ? "generic/rhel7" : "centos/7"
# give me a name
config.vm.define (app_name + (realm_mode ? '-realm' : '-nomad') + '-lib-ncsu') do
end
# 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
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 8001, host: 8081, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder ".", vagrant_guest_path, owner: 'vagrant', group: 'vagrant', type: 'virtualbox'
# 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|
# Display the VirtualBox GUI when booting the machine
vb.gui = false
vb.memory = "1024"
end
# View the documentation for the provider you are using for more
# information on available options.
#TODO is all this really needed for realm?
fqdn = Socket.gethostbyname(Socket.gethostname).first.split('.')
hostname = fqdn.shift.downcase
domain = fqdn.join('.').downcase
public_ips = Socket.ip_address_list.reject{|i| !i.ipv4? || i.ipv4_loopback? || i.ipv4_multicast? || i.ipv4_private? }
ip = public_ips.any? ? public_ips.first&.ip_address : nil
realfqdn = ip ? (Resolv.getname ip).split('.') : nil
realhostname = realfqdn ? realfqdn.shift.downcase : hostname
realdomain = realfqdn ? realfqdn.join('.').downcase : domain
config.trigger.before [:up, :provision, :reload, :resume] do |trigger|
trigger.info = "Checking, are Strings Attached?"
trigger.ruby do |env, machine|
if (!puppet_facts)
print " Error: Unable to load puppet facts. Exiting...\n\r"
exit(1)
end
if !File.file?(ldm_path)
ldm_file = File.new(ldm_path, 'w+')
ldm_file.write("# #{ldm_token}")
else
ldm_file = File.new(ldm_path, 'a+')
ldm_file.rewind
if (!ldm_file.readline.start_with?("# #{ldm_token}"))
ldm_file.close
ldm_file = nil
print " proceeding with manually written #{local_dynamic_manifest} \n\r"
end
end
if (ldm_file)
print " buiding #{local_dynamic_manifest} \n\r"
ldm_file.truncate(ldm_file.pos + 1)
ppp.each do |pp|
if (File.file?("#{manifests_path}/#{pp}.pp"))
print " adding \"#{pp}\" manifest to #{local_dynamic_manifest}\n\r"
ldm_file.write("\n\r# from #{pp}.pp \n\r");
ldm_file.write(File.read("#{manifests_path}/#{pp}.pp"))
else
print " no \"#{pp}\" manifest available for #{local_dynamic_manifest}\n\r"
end
end
ldm_file.close
end
end
end
config.trigger.before [:up, :provision, :reload] do |trigger|
trigger.info = "Harvesting Host FQDN"
trigger.ruby do |env, machine|
if (!ip)
Socket.ip_address_list.map{ |i| print(i.inspect + "\n\r")}
print(" No public IPv4 detected, known IPs: \n\r");
end
print " host fqdn detected as #{realhostname} + #{realdomain}" + (ip ? " (#{ip})" : '') + "\n\r"
end
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
hostnamectl set-hostname #{realhostname}
SHELL
if (realm_mode)
config.vm.provision :shell, inline: <<-SHELL
if [ ! -e /etc/hosts ] || ! grep -q "needed for realm-puppet management" /etc/hosts
then
echo " " >> /etc/hosts
echo "## The line below is needed for realm-puppet management" >> /etc/hosts
echo "#{ip} #{realhostname}.#{realdomain} #{realhostname}" >> /etc/hosts
echo " " >> /etc/hosts
fi
yum install http://rhn200cap.unity.ncsu.edu/pub/katello-ca-consumer-latest.noarch.rpm -y
if [[ $(subscription-manager identity) =~ "org ID: #{rhel_org}" ]]
then
echo "RHEL already registered"
else
subscription-manager register --org="#{rhel_org}" --activationkey="#{realm_key}"
fi
SHELL
end
config.vm.provision :shell, inline: <<-SHELL
# rpm -ivh https://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm
rpm -Uvh https://yum.puppetlabs.com/puppet5/puppet5-release-el-7.noarch.rpm
yum groups install "Development Tools" -y
yum install nano epel-release puppet -y
yum update -y
if [ ! -e /usr/bin/puppet ]
then
echo "linking puppet binary..."
ln -s /opt/puppetlabs/bin/puppet /usr/bin/puppet
fi
# # puppet 3.8 compat "supported"
# puppet module install puppetlabs-postgresql --version 4.9.0
# puppet module install puppetlabs-apache --version 1.11.1
# puppet module install puppetlabs-mysql --version 3.11.0
# # puppet 3.8 compat approved
# puppet module install puppet-python --version 2.2.2
# puppet 5.5 compat supported
puppet module install puppetlabs-postgresql --version 5.12.0
puppet module install puppetlabs-apache --version 4.0.0
puppet module install puppetlabs-mysql --version 8.0.0
# puppet 5.5 compat approved
puppet module install puppet-python --version 2.2.2
puppet config set strict_variables true --section main
SHELL
config.vm.provision :puppet do |puppet|
puppet.manifests_path = manifests_path
puppet.manifest_file = local_dynamic_manifest
puppet.options = "--verbose --debug"
puppet.facter = puppet_facts
end
config.vm.provision :shell do |s|
s.inline = <<-SHELL
if [ ! -e ~/.bashrc ] || ! grep -q "poor detection of vagrant windows host" ~/.bashrc
then
echo " " >> ~/.bashrc
echo "## The (2) lines below fix poor detection of vagrant windows host" >> ~/.bashrc
echo "stty sane" >> ~/.bashrc
echo "export TERM=linux" >> ~/.bashrc
echo " " >> ~/.bashrc
fi
SHELL
s.privileged = false
end
config.vm.provision "puppet-reset", type: :shell, run: 'never' do |s|
s.inline = <<-SHELL
puppet module uninstall puppetlabs-postgresql
puppet module uninstall puppetlabs-apache
puppet module uninstall puppetlabs-mysql
puppet module uninstall puppet-python
SHELL
end
config.vm.provision "reminders", type: :shell, run: 'always' do |s|
s.inline = <<-SHELL
echo To activate post-startup tasks on the guest VM use: vagrant up --provision-with start
SHELL
end
config.vm.provision "chill", type: :shell, run: 'never' do |s|
s.inline = <<-SHELL
echo Toning down SELinux...
setenforce Permissive
systemctl stop firewalld.service
SHELL
end
config.vm.provision "start", type: :shell, run: 'never' do |s|
s.inline = <<-SHELL
echo Starting Services...
systemctl start httpd.service
SHELL
end
config.vm.provision "stop", type: :shell, run: 'never' do |s|
s.inline = <<-SHELL
echo Stopping Services...
systemctl stop httpd.service
SHELL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment