【问题标题】:How are people testing opsworks cookbooks?人们如何测试 opsworks 食谱?
【发布时间】:2014-05-01 18:05:08
【问题描述】:

我在 nginx 上使用我目前使用 Capistrano 的乘客,对铁路应用程序的动态配置实例进行了相当复杂的部署。我正在尝试自动化 Opsworks 上的整个过程,从创建堆栈、层、实例和动态应用程序部署。现有的 Opsworks 食谱都无法满足我的需求,因此我开始编写自己的定制食谱。我正在尝试使用 test-kitchen 和 vagrant 来测试它们,但是 Opsworks 食谱有太多的自定义依赖项无法在本地使用。因此,似乎我要么通过仅使用我自己的不依赖于 Opsworks 食谱的食谱来重新发明轮子,要么尝试在 AWS 上测试我的食谱,这既慢又昂贵。

是否有人在 Opsworks 上进行任何自定义食谱开发,如果有,您如何对其进行测试?您是否在使用任何社区食谱?如果有的话,他们在 Opsworks 上工作有多么痛苦?

【问题讨论】:

    标签: chef-infra aws-opsworks


    【解决方案1】:

    Opsworks 现在支持 Chef 11.10 和 Berkshelf (http://berkshelf.com/) 来管理依赖项,这使得使用自定义说明书如此。很多。更容易。

    我对 Test Kitchen (https://github.com/test-kitchen/test-kitchen) 和 Vagrant & Chef Solo 进行本地测试非常满意。

    此外,您可以从堆栈设置页面在 Opsworks 堆栈上手动运行特定配方。看看这里:http://docs.aws.amazon.com/opsworks/latest/userguide/workingcookbook-manual.html

    您还可以在 Opsworks 部署期间使用一些自定义 JSON 运行说明书测试:http://docs.aws.amazon.com/opsworks/latest/userguide/troubleshoot-debug-test.html

    【讨论】:

    • 这听起来不错,但是您如何测试依赖于 OpsWorks 说明书的自定义说明书?到目前为止,我能想到的唯一方法是将它们作为子模块包含在 Berksfile 中,并在某些特定条件下进行。
    【解决方案2】:

    我们将 Opsworks 与许多自定义食谱和 vagrant 一起使用。虽然测试范围远非完美,但它运行得非常顺利,也适应了社区食谱。我们的食谱库是公开的:https://github.com/till/easybib-cookbooks

    这个 repo 的有趣之处在于:

    浏览我们的食谱应该会为您的问题提供更多提示。我们用 Travis 测试我们的说明书,根本不测试 opsworks 说明书。尽管我必须指出,我目前正在那里研究如何将它们集成到我们的测试运行中,因为没有它们的提供者,测试我们的部署说明书几乎是不可能的。

    【讨论】:

    • 第一个和第二个链接出现错误 404
    【解决方案3】:

    此页面上有很多有用的建议,但我强烈建议大家查看 Mike Greiling 的博客文章 Simplify OpsWorks Development With Packer 和他的 github 存储库 opsworks-vm 这可以帮助您模拟整个 opsworks 堆栈,包括安装的 opsworks 代理,因此您还可以同时测试应用部署配方、多个层、多个实例等。这是非常令人印象深刻的。

    Ubuntu 14.04 快速入门

    注意:这不能在 ubuntu 虚拟机上完成,因为 virtualbox 不支持 64 位机器的嵌套虚拟化。

    1. 安装ChefDK
      1. mkdir /tmp/packages && cd /tmp/packages
      2. wget https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chefdk_0.8.1-1_amd64.deb
      3. sudo dpkg -i chefdk_0.8.0-1_amd64.deb
      4. cd /opt/chefdk/
      5. chef verify
      6. which ruby
      7. echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile && source ~/.bash_profile
    2. 安装VirtualBox
      1. echo 'deb http://download.virtualbox.org/virtualbox/debian vivid contrib' > /etc/apt/sources.list.d/virtualbox.list
      2. wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
      3. sudo apt-get update -qqy
      4. sudo apt-get install virtualbox-5.0 dkms
    3. 安装Vagrant
      1. cd /tmp/packages
      2. wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.4_x86_64.deb
      3. sudo dpkg -i vagrant_1.7.4_x86_64.deb
      4. vagrant plugin install vagrant-berkshelf
      5. vagrant plugin install vagrant-omnibus
      6. vagrant plugin list
    4. 安装Packer
      1. mkdir /opt/packer && cd /opt/packer
      2. wget https://dl.bintray.com/mitchellh/packer/packer_0.8.6_linux_amd64.zip
      3. unzip packer_0.8.6_linux_amd64.zip
      4. echo 'PATH=$PATH:/opt/packer' >> ~/.bash_profile && source ~/.bash_profile
    5. 使用 Packer 构建 Mike Greiling 的 opsworks-vm virtualbox 映像
      1. mkdir ~/packer && cd ~/packer
      2. git clone https://github.com/pixelcog/opsworks-vm.git
      3. cd opsworks-vm
      4. rake build install
      5. 这会将新的 virtualbox vm 安装到 ~/.vagrant.d/boxes/ubuntu1404-opsworks/

    要模拟单个 opsworks 实例,请创建一个新的 Vagrantfile,如下所示:

    Vagrant.configure("2") do |config|
      config.vm.box = "ubuntu1404-opsworks"
      config.vm.provision :opsworks, type: 'shell', args: 'path/to/dna.json'
    end
    

    dna.json 文件路径是相对于 Vagrantfile 设置的,并且应该包含您希望发送给 OpsWorks Chef 的任何 JSON 数据。

    例如:

    {
      "deploy": {
        "my-app": {
          "application_type": "php",
          "scm": {
            "scm_type": "git",
            "repository": "path/to/my-app"
          }
        }
      },
      "opsworks_custom_cookbooks": {
        "enabled": true,
        "scm": {
          "repository": "path/to/my-cookbooks"
        },
        "recipes": [
          "recipe[opsworks_initial_setup]",
          "recipe[dependencies]",
          "recipe[mod_php5_apache2]",
          "recipe[deploy::default]",
          "recipe[deploy::php]",
          "recipe[my_custom_cookbook::configure]"
        ]
      }
    }
    

    要模拟多个 opsworks 实例并包含层,请参阅他的 AWS OpsWorks "Getting Started" Example,其中包括下面的 stack.json

    Vagrantfile(用于多个实例)

    Vagrant.configure("2") do |config|
    
      config.vm.box = "ubuntu1404-opsworks"
    
      # Create the php-app layer
      config.vm.define "app" do |layer|
    
        layer.vm.provision "opsworks", type:"shell", args:[
          'ops/dna/stack.json',
          'ops/dna/php-app.json'
        ]
    
        # Forward port 80 so we can see our work
        layer.vm.network "forwarded_port", guest: 80, host: 8080
        layer.vm.network "private_network", ip: "10.10.10.10"
      end
    
      # Create the db-master layer
      config.vm.define "db" do |layer|
    
        layer.vm.provision "opsworks", type:"shell", args:[
          'ops/dna/stack.json',
          'ops/dna/db-master.json'
        ]
    
        layer.vm.network "private_network", ip: "10.10.10.20"
      end
    end
    

    stack.json

    {
      "opsworks": {
        "layers": {
          "php-app": {
            "instances": {
              "php-app1": {"private-ip": "10.10.10.10"}
            }
          },
          "db-master": {
            "instances": {
              "db-master1": {"private-ip": "10.10.10.20"}
            }
          }
        }
      },
      "deploy": {
        "simple-php": {
          "application_type": "php",
          "document_root": "web",
          "scm": {
            "scm_type": "git",
            "repository": "dev/simple-php"
          },
          "memcached": {},
          "database": {
            "host": "10.10.10.20",
            "database": "simple-php",
            "username": "root",
            "password": "correcthorsebatterystaple",
            "reconnect": true
          }
        }
      },
      "mysql": {
        "server_root_password": "correcthorsebatterystaple",
        "tunable": {"innodb_buffer_pool_size": "256M"}
      },
      "opsworks_custom_cookbooks": {
        "enabled": true,
        "scm": {
          "repository": "ops/cookbooks"
        }
      }
    }
    

    对于那些不熟悉 vagrant 的人,您只需执行 vagrant up 即可启动实例。然后,您可以在本地修改您的食谱,并且可以通过使用 vagrant provision. 对现有实例重新运行 chef 来应用任何更改。您可以执行 vagrant destroyvagrant up 从头开始​​。

    【讨论】:

      【解决方案4】:

      Opsworks 食谱建立在他们的自定义盒子上,因此设置一个本地 VM,您可以在其中与您的食谱一起运行他们的食谱是一个挑战,

      幸运的是,这个项目重新创建了他们的 Ubuntu 盒子。 https://github.com/wwestenbrink/vagrant-opsworks

      在此基础上,我建立了一个本地 vagrant 环境,运行 opsworks 食谱和我自己的食谱。 https://github.com/erupenkman/opsworks-example

      有了这个,您实际上可以在部署前在几乎相同的本地环境中进行测试。

      【讨论】:

      • 如果 Windows Server 也有同样的功能,那当然是件好事。目前测试食谱相当痛苦。
      【解决方案5】:

      查看 Travis-ci 和 sous-chef,您可能会发现这适用于测试食谱等,并且他们也有适用于 opsworks 的部署配置。

      https://github.com/michaelklishin/sous-chef http://docs.travis-ci.com/user/deployment/opsworks/

      【讨论】:

        【解决方案6】:

        我能够使用 Chef+Kitchen+Vagrant 解决这个问题

        这是我的包装:https://github.com/elitechance/opsworks-cookbooks-wrapper

        下面是测试方法

        git 克隆https://github.com/elitechance/opsworks-cookbooks-wrapper.git

        git 克隆https://github.com/aws/opsworks-cookbooks.git

        cd opsworks-cookbooks-wrapper

        cp .kitchen.sample.yml .kitchen.yml

        cp Berksfile.sample Berksfile

        cp metadata.sample.rb metadata.rb

        最后一条命令将模拟 OpsWorks NodeJs 设置配方,详情请参阅 .kitchen.sample.yml

        $ chef exec kitchen setup

        已知错误

        第一次运行$ chef exec kitchen setup,你会遇到错误提示:

        运行处理程序:[2016-04-08T17:08:34+00:00] 错误:运行异常处理程序 运行处理程序完成 [2016-04-08T17:08:34+00:00] 错误:异常处理程序完成 Chef客户端失败。 43 个资源在 01 分 02 秒内更新 [2016-04-08T17:08:34+00:00] 致命:堆栈跟踪转储到 /tmp/kitchen/cache/chef-stacktrace.out [2016-04-08T17:08:34 +00:00] 致命:如果您提交错误报告,请提供 stacktrace.out 文件的内容 [2016-04-08T17:08:34+00:00] 错误:远程文件的 ruby​​_blockFallback [/tmp/rubygems-2.2 .2.tgz] 有一个错误:NoMethodError: remote_file/tmp/rubygems-2.2.2.tgz 有一个错误:NoMethodError: undefined method `to_sym' for [:create]:Array [2016-04-08T17:08:34 +00:00] 致命:Chef::Exceptions::ChildConvergeError:Chef 运行进程未成功退出(退出代码 1)

        再次运行:

        $ chef exec kitchen setup

        【讨论】:

          猜你喜欢
          • 2016-04-26
          • 1970-01-01
          • 2014-11-25
          • 2015-12-19
          • 1970-01-01
          • 2019-03-06
          • 1970-01-01
          • 1970-01-01
          • 2017-08-25
          相关资源
          最近更新 更多