0
votes

I'm trying to build a virtual environment with Virtualbox and Vagrant. The goal is to be able to access from pc browser to an app running on virtual machine 'server', that interacts with virtual machine 'db'.

I managed to create the virtual machines, and the project is ok. This is the directory structure:

├── MyProject
│   ├── environment
│   │   ├── Vagrantfile
│   │   ├── Manifest
│   │   │   ├── site.pp
│   │   ├── Modules
│   │   │   ├── Java
│   │   │   ├────── manifest
│   │   │   │        ├── init.pp
│   │   │   ├── Postgres
│   │   │   ├────── manifest
│   │   │   │        ├── init.pp
│   │   │   ├── Tomee
│   │   │   ├────── manifest
│   │   │   │        ├── init.pp
│   ├── Project
│   │   ├── postgres-driver 
│   │   ├── tomee.xml 
│   │   ├── app.war

I'm not sure that my approach to Puppet use is correct. For example I had to install Java8 on virtual machine 'web'. In Vagrant file:

config.vm.provision :shell do |shell|
    shell.inline = "mkdir -p /etc/puppet/modules;
                   puppet module install puppetlabs/apt"
 end
       config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "puppet/manifests"
    puppet.manifest_file = "site.pp"
    puppet.module_path = "puppet/modules"

And then in init.pp of Java folder:

class java {
    include apt

    apt::ppa { 'ppa:openjdk-r/ppa': 
      ensure => present, } 

    exec { 'apt-update':
      command => '/usr/bin/apt-get update',
      require => [
      Apt::Ppa['ppa:openjdk-r/ppa']
    ],
  }

    package { 'openjdk-8-jdk':
       require  => [
      Exec['apt-update'],
      Apt::Ppa['ppa:openjdk-r/ppa'],
    ],} }

Is this a valid way to use Puppet? Or should I avoid things like shell command in the Vagrantfile to install modules?

I'd like also to know how Puppet manages to download mysql from this init.pp file. There aren't puppet module installed.

class mysql {
  package { ['mysql-server']:
    ensure => present;
  }

  service { 'mysql':
    ensure  => running,
    require => Package['mysql-server'];
  }

  file { '/etc/mysql/my.cnf':
    source  => 'puppet:///modules/mysql/my.cnf',
    require => Package['mysql-server'],
    notify  => Service['mysql'];
  }

  exec { 'set-mysql-password':
    unless  => 'mysqladmin -uroot -proot status',
    command => "mysqladmin -uroot password root",
    path    => ['/bin', '/usr/bin'],
    require => Service['mysql'];
  }

  exec { 'load-dynamic-sql':
    command => 'mysql -u root -proot < /vagrant/sites/dynamic.sql',
    path    => ['/bin', '/usr/bin'],
    require => Exec['set-mysql-password'];
  }
}
1

1 Answers

0
votes

There are cupple of questions in one.

  1. Handling puppet dependencies or modules

There are probably multiple ways to handle it and I also use a shell script to provision the installation of the puppet modules - such script will look like

#!/bin/bash

mkdir -p /etc/puppet/modules;

if [ ! -d /etc/puppet/modules/puppetlabs-java ]; then
  puppet module install puppetlabs-java --version 1.4.1
fi

on top of your inline script, I check existence of puppet modules so if I need to run provision later, it will detect modules have been installed. Second, I stick with a specific module version; if you're not the module owner, you cannot be sure about the evolution, sticking with a version give you higher chance that the provisioning will still work in cuple of months, else module might have changed and you dont know why.

As mentioned there are other ways: you can download the modules directly from your host (but you need to make sure to download module dependency too); you can use librarian, vagrant has plugin to manage puppet librarian dependencies.

  1. Using puppet modules

You can write your own puppet module, ultimately you will need to write shell command but in case you plan to distribute the module you probably need to have test on the OS family and write different command depending on the OS.

I would still probably recommend using a dedicated module; in your case the puppetlabs/java does the job

  1. the mysql example

Your example is an example to setup and import a DB, not to installed the sql server, its probably in the mysql-server package