Packer.io无法使用puppet provisioner:/ usr / bin / puppet:第3行:rvm:命令未找到

时间:2015-03-14 13:25:20

标签: rvm vagrant virtualbox puppet packer

我正在尝试使用Packer.io和Puppet构建一个Vagrant盒子文件。 我将此模板作为起点: https://github.com/puphpet/packer-templates/tree/master/centos-7-x86_64

我在shell配置程序之后添加了Puppet配置程序:

        {
            "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}}"
                }
            }
        }

当我开始构建像

这样的图像时
  

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

然后我收到此错误:

==> 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

如果我通过tty登录框,我可以将 rvm puppet 命令作为vagrant用户运行。 我做错了什么?

2 个答案:

答案 0 :(得分:2)

我正在尝试与你完全相同的路线:

  1. 使用相关脚本从此repo配置虚拟机。
  2. 使用puphpet.com配置中的puppet脚本,在packer中使用puppet-masterless provioner进一步配置vm。
  3. 仍在努力,尚未成功构建,但我可以分享以下内容:

    • puphpet/shell/install-puppet.sh检查第50行。因此puppet命令将触发rvm执行。
    • 在配置期间检查您的包装机输出。你的阅读内容如下:

      ...
      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
      ....
      
    • 显然,每个需要运行source /usr/local/rvm/scripts/rvm的用户都需要rvm命令。它被执行并设置为脚本puphpet/shell/install-ruby.sh中的bash配置文件。但是,这似乎不会影响packer的puppet masterless provisioning execute_command的上下文/范围。输出中行/usr/bin/puppet: line 3: rvm: command not found的原因。

    • 我目前的方法是template.json(包装工模板)中的以下配置,第二行和第三行将帮助您超越当前卡住的位置:

      {
          "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"
          }
      },
      

    请注意以下事项:

    1. 由于权限问题,可能因为流浪者用户而无法完成配置。在这种情况下,我们需要一种在sudo中运行source /usr/local/rvm/scripts/rvm并影响puppet供应命令范围的方法。
    2. puphpet.com输出脚本在其木偶脚本(例如/vagrant/puphpet第一行)中进行了puphpet/puppet/nodes/Apache.pp硬编码。因此,在执行puppet masterless之前,您可能需要向vm配置打包文件,以便在/vagrant/...中找到依赖关系。我packer.json对此表示赞同:

      {
          "type": "shell",
          "execute_command": "sudo bash '{{.Path}}'",
          "inline": [
              "mkdir /vagrant",
              "chown -R vagrant:vagrant /vagrant"
           ]
      },
      {
          "type": "file",
          "source": "./puphpet",
          "destination": "/vagrant"
      },
      
    3. Puppet将需要一些在puphpet/puppet/nodes/*.pp脚本中预期的Facter变量。请参阅上面的template.json

    4. 如上所述。在我这方面完整的木偶配置没有成功,但上面让我超越了你目前被卡住的地步。希望它有所帮助。


      <强>更新

      我替换了旧的执行命令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}}"
      

      使用新的

      "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}}\""
      

      这将确保puppet(rvm)以root身份运行并成功完成配置。

答案 1 :(得分:1)

作为我的其他答案的替代方案,我在此提供我的步骤和配置,以使此配置方案与打包器和放大器一起使用。 puphpet。

假设有以下内容:

  • ./:作为您自己的存储库的本地目录
  • ./ops/:一个目录ops,其中包含打包程序脚本和所需文件
  • ./ops/template.json:用于构建VM的打包程序模板
  • ./ops/template.json预计会有以下内容:
    • ./ops/packer-templates/this repo
    • 的克隆
    • ./ops/ubuntu-14.04.2-server-amd64.iso:您希望在vm中运行的ubuntu的iso
    • ./puphpet:遍历puphpet.com上的配置步骤的输出(因此这距离ops一级)

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
        }
    ]
}

发生的事情的叙述:

  • 使用用于构建puphpet box的脚本(第一个shell配置程序块)执行VM的基本配置
  • 在VM中创建目录/vagrant并设置vagrant用户
  • 的权限
  • 将本地存储库上传到/vagrant(重要,因为puphpet / puppet希望它存在于脚本中的该位置)
  • 上传后从/vagrant删除一些不需要的内容
  • 使用自定义execute_commandfacter配置
  • 启动puppet provisioner
  • 处理剩余的配置脚本。要使用exec一次/永久扩展,请启动一次/始终文件

注意:您可能需要在木偶供应商启动之前准备更多东西。例如。我需要一个目录,它将成为apache中vhost的docroot。使用shell配置来完成您自己的puphpet配置的模板。