2
votes

I'm trying to build a Vagrant box file using Packer.io and Puppet. I have this template as a starting point: https://github.com/puphpet/packer-templates/tree/master/centos-7-x86_64

I added the Puppet provisioner after the shell provisioner:

        {
            "type": "puppet-masterless",
            "manifest_file": "../../puphpet/puppet/site.pp",
            "manifest_dir": "../../puphpet/puppet/nodes",
            "module_paths": [
                "../../puphpet/puppet/modules"
            ],
            "override": {
                "virtualbox-iso": {
                    "execute_command": "echo 'vagrant' | {{.FacterVars}}{{if .Sudo}} sudo -S -E bash {{end}}/usr/bin/puppet apply --verbose --modulepath='{{.ModulePath}}' {{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}} {{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}} --detailed-exitcodes {{.ManifestFile}}"
                }
            }
        }

When I start building the image like

packer-io build -only=virtualbox-iso template.json

Then I get this error:

==> virtualbox-iso: Provisioning with Puppet...
    virtualbox-iso: Creating Puppet staging directory...
    virtualbox-iso: Uploading manifest directory from: ../../puphpet/puppet/nodes
    virtualbox-iso: Uploading local modules from: ../../puphpet/puppet/modules
    virtualbox-iso: Uploading manifests...
    virtualbox-iso:
    virtualbox-iso: Running Puppet: echo 'vagrant' |  sudo -S -E bash /usr/bin/puppet apply --verbose --modulepath='/tmp/packer-puppet-masterless/module-0'  --manifestdir='/tmp/packer-puppet-masterless/manifests'  --detailed-exitcodes /tmp/packer-puppet-masterless/manifests/site.pp
    virtualbox-iso: /usr/bin/puppet: line 3: rvm: command not found
==> virtualbox-iso: Unregistering and deleting virtual machine...
==> virtualbox-iso: Deleting output directory...
Build 'virtualbox-iso' errored: Puppet exited with a non-zero exit status: 127

If I log in into the box via tty, I can run both rvm and puppet commands as vagrant user. What did I do wrong?

2

2 Answers

2
votes

I am trying out the exact same route as you are:

  1. Use relevant scripts for provisioning the vm from this repo.
  2. Use the puppet scripts from a puphpet.com configuration to further provision the vm using puppet-masterless provioner in packer.

Still working on it, not a successful build yet, but I can share the following:

  • Inspect line 50 from puphpet/shell/install-puppet.sh. So the puppet command will trigger rvm to be executed.
  • Inspect your packer output during provisioning. Your read something along the lines of:

    ...
    Creating alias default for ruby-1.9.3-p551
    
    To start using RVM you need to run `source /usr/local/rvm/scripts/rvm` in all
    your open shell windows, in rare cases you need to reopen all shell windows.
    Cleaning up rvm archives
    ....
    
  • Apparently the command source /usr/local/rvm/scripts/rvm is needed for each user that needs to run rvm. It is executed and set to bash profiles in the script puphpet/shell/install-ruby.sh. However, this does not seem to affect the context/scope of the puppet masterless provisioning execute_command of packer. Reason for the line /usr/bin/puppet: line 3: rvm: command not found in your output.

  • My current way forward is the following configuration in template.json (packer template), the second and third line will help get beyond the point where you are stuck currently:

    {
        "type": "puppet-masterless",
        "prevent_sudo": true,
        "execute_command": "{{if .Sudo}}sudo -E {{end}}bash -c \"source /usr/local/rvm/scripts/rvm; {{.FacterVars}} puppet apply --verbose --parser future --modulepath='{{.ModulePath}}' {{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}} {{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}} --detailed-exitcodes {{.ManifestFile}}\"",
        "manifest_file": "./puphpet/puppet/site.pp",
        "manifest_dir": "./puphpet/puppet",
        "hiera_config_path": "./puphpet/puppet/hiera.yaml",
        "module_paths": [
            "./puphpet/puppet/modules"
        ],
        "facter": {
            "ssh_username": "vagrant",
            "provisioner_type": "virtualbox",
            "vm_target_key": "vagrantfile-local"
        }
    },
    

Note the following things:

  1. Probably running puppet as vagrant user will not complete provisioning due to permission issues. In that case we need a way to run source /usr/local/rvm/scripts/rvm in a sudo and affect the scope of the puppet provisioning command.
  2. The puphpet.com output scripts have /vagrant/puphpet hardcoded in their puppet scripts (e.g. puphpet/puppet/nodes/Apache.pp first line). So you might require a packer file provisioning to your vm before you execute puppet masterless, in order for it to find the dependencies in /vagrant/.... My packer.json conf for this:

    {
        "type": "shell",
        "execute_command": "sudo bash '{{.Path}}'",
        "inline": [
            "mkdir /vagrant",
            "chown -R vagrant:vagrant /vagrant"
         ]
    },
    {
        "type": "file",
        "source": "./puphpet",
        "destination": "/vagrant"
    },
    
  3. Puppet will need some Facter variables as they are expected in the puphpet/puppet/nodes/*.pp scripts. Refer to my template.json above.

As said. No success in a complete puppet provisioning yet on my side, but the above got me beyond the point where you are stuck currently. Hope it helps.


Update:

I replaced my old execute command for puppet provisioner

"execute_command": "source /usr/local/rvm/scripts/rvm && {{.FacterVars}}{{if .Sudo}} sudo -E{{end}} puppet apply --verbose --parser future --modulepath='{{.ModulePath}}' {{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}} {{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}} --detailed-exitcodes {{.ManifestFile}}"

with a new one

"execute_command": "{{if .Sudo}}sudo -E {{end}}bash -c \"source /usr/local/rvm/scripts/rvm; {{.FacterVars}} puppet apply --verbose --parser future --modulepath='{{.ModulePath}}' {{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}} {{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}} --detailed-exitcodes {{.ManifestFile}}\""

This will ensure puppet (rvm) is running as root and finishes provisioning successfully.

1
votes

As an alternative to my other answer, I hereby provide my steps and configuration to get this provisioning scenario working with packer & puphpet.

Assuming the following to be in place:

  • ./: a local directory acting as your own repository being
  • ./ops/: a directory ops inside which holds packer scripts and required files
  • ./ops/template.json: the packer template used to build the VM
  • ./ops/template.json expects the following is in place:
    • ./ops/packer-templates/: a clone of this repo
    • ./ops/ubuntu-14.04.2-server-amd64.iso: the iso for the ubuntu you want to have running in your vm
    • ./puphpet: the output of walking through the configuration steps on puphpet.com (so this is one level up from ops)

The contents of template.json:

{
    "variables": {
        "ssh_name": "vagrant",
        "ssh_pass": "vagrant",
        "local_packer_templates_dir": "./packer-templates/ubuntu-14.04-x86_64",
        "local_puphput_dir": "../puphpet",
        "local_repo_dir": "../",
        "repo_upload_dir": "/vagrant"
    },
    "builders": [
        {
            "name": "ubuntu-14.04.amd64.virtualbox",
            "type": "virtualbox-iso",
            "headless": false,
            "boot_command": [
                "<esc><esc><enter><wait>",
                "/install/vmlinuz noapic preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ",
                "debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
                "hostname={{ .Name }} ",
                "fb=false debconf/frontend=noninteractive ",
                "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false ",
                "initrd=/install/initrd.gz -- <enter>"
            ],
            "boot_wait": "10s",
            "disk_size": 20480,
            "guest_os_type": "Ubuntu_64",
            "http_directory": "{{user `local_packer_templates_dir`}}/http",
            "iso_checksum": "83aabd8dcf1e8f469f3c72fff2375195",
            "iso_checksum_type": "md5",
            "iso_url": "./ubuntu-14.04.2-server-amd64.iso",
            "ssh_username": "{{user `ssh_name`}}",
            "ssh_password": "{{user `ssh_pass`}}",
            "ssh_port": 22,
            "ssh_wait_timeout": "10000s",
            "shutdown_command": "echo '/sbin/halt -h -p' > shutdown.sh; echo '{{user `ssh_pass`}}'|sudo -S bash 'shutdown.sh'",
            "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
            "virtualbox_version_file": ".vbox_version",
            "vboxmanage": [
                ["modifyvm", "{{.Name}}", "--memory", "2048"],
                ["modifyvm", "{{.Name}}", "--cpus", "4"]
            ]
        }
    ],
    "provisioners": [
        {
            "type": "shell",
            "execute_command": "echo '{{user `ssh_pass`}}'|sudo -S bash '{{.Path}}'",
            "scripts": [
                "{{user `local_packer_templates_dir`}}/scripts/base.sh",
                "{{user `local_packer_templates_dir`}}/scripts/virtualbox.sh",
                "{{user `local_packer_templates_dir`}}/scripts/vagrant.sh",
                "{{user `local_packer_templates_dir`}}/scripts/puphpet.sh",
                "{{user `local_packer_templates_dir`}}/scripts/cleanup.sh",
                "{{user `local_packer_templates_dir`}}/scripts/zerodisk.sh"
            ]
        },
        {
            "type": "shell",
            "execute_command": "sudo bash '{{.Path}}'",
            "inline": [
                "mkdir {{user `repo_upload_dir`}}",
                "chown -R vagrant:vagrant {{user `repo_upload_dir`}}"
             ]
        },
        {
            "type": "file",
            "source": "{{user `local_repo_dir`}}",
            "destination": "{{user `repo_upload_dir`}}"
        },
        {
            "type": "shell",
            "execute_command": "sudo bash '{{.Path}}'",
            "inline": [
                "rm -fR {{user `repo_upload_dir`}}/.vagrant",
                "rm -fR {{user `repo_upload_dir`}}/ops"
             ]
        },
        {
            "type": "puppet-masterless",
            "execute_command": "{{if .Sudo}}sudo -E {{end}}bash -c \"source /usr/local/rvm/scripts/rvm; {{.FacterVars}} puppet apply --verbose --parser future --modulepath='{{.ModulePath}}' {{if ne .HieraConfigPath \"\"}}--hiera_config='{{.HieraConfigPath}}' {{end}} {{if ne .ManifestDir \"\"}}--manifestdir='{{.ManifestDir}}' {{end}} --detailed-exitcodes {{.ManifestFile}}\"",
            "manifest_file": "{{user `local_puphput_dir`}}/puppet/site.pp",
            "manifest_dir": "{{user `local_puphput_dir`}}/puppet",
            "hiera_config_path": "{{user `local_puphput_dir`}}/puppet/hiera.yaml",
            "module_paths": [
                "{{user `local_puphput_dir`}}/puppet/modules"
            ],
            "facter": {
                "ssh_username": "{{user `ssh_name`}}",
                "provisioner_type": "virtualbox",
                "vm_target_key": "vagrantfile-local"
            }
        },
        {
            "type": "shell",
            "execute_command": "sudo bash '{{.Path}}'",
            "inline": [
                 "echo '{{user `repo_upload_dir`}}/puphpet' > '/.puphpet-stuff/vagrant-core-folder.txt'",
                 "sudo bash {{user `repo_upload_dir`}}/puphpet/shell/important-notices.sh"
             ]
        }
    ],
    "post-processors": [
        {
          "type": "vagrant",
          "output": "./build/{{.BuildName}}.box",
          "compression_level": 9
        }
    ]
}

Narration of what happens:

  • execute the basic provisioning of the VM using the scripts that are used to build puphpet boxes (first shell provisioner block)
  • create a directory /vagrant in the VM and set permissions for vagrant user
  • upload local repository to /vagrant (important as puphpet/puppet expects it to exist at that location in its scripts)
  • remove some unneeded stuff from /vagrant after upload
  • start puppet provisioner with custom execute_command and facter configuration
  • process the remaining provisioning scripts. To be extended with exec once/always, start once/always files

Note: you might need to prepare some more things before the puppet provisioner kicks off. E.g. I need a directory in place that will be the docroot of a vhost in apache. Use shell provisioning to complete the template for your own puphpet configuration.