amazon-web-services - 在使用 Kitchen 和 Chef 进行本地开发时,您如何模拟 OpsWorks 特定的服务/依赖项?

标签 amazon-web-services chef-infra aws-opsworks berkshelf test-kitchen

我正在围绕一些内置的 OpsWorks 食谱编写 Chef 包装器。我正在使用 Berkshelf 从他们的 github 存储库中克隆 OpsWorks 说明书。

这是我的 Berksfile:

source 'https://supermarket.getchef.com'

metadata

def opsworks_cookbook(name)
  cookbook name, github: 'aws/opsworks-cookbooks', branch: 'release-chef-11.10', rel: name
end

%w(dependencies scm_helper mod_php5_apache2 ssh_users opsworks_agent_monit
   opsworks_java gem_support opsworks_commons opsworks_initial_setup
   opsworks_nodejs opsworks_aws_flow_ruby
   deploy mysql memcached).each do |cb|
  opsworks_cookbook cb
end

我的元数据.rb:
depends 'deploy'
depends 'mysql'
depends 'memcached'

问题是,当我尝试覆盖依赖于 opsworks 的属性时关键在 node哈希,我得到一个:
NoMethodError
-------------
undefined method `[]=' for nil:NilClass

OpsWorks 有一大堆预配方依赖项,用于创建这些 key 并进行大量设置。我想找到一种方法来引入这些服务并针对我的 Kitchen 实例运行它们,或者以一种我可以实际测试我的食谱的方式模拟它们。

有没有办法做到这一点?

最佳答案

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

Ubuntu 14.04 快速入门

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

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

    堆栈文件
    {
      "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. 针对现有实例重新运行厨师来应用任何更改。你可以做一个 vagrant destroyvagrant up从头开始。

    关于amazon-web-services - 在使用 Kitchen 和 Chef 进行本地开发时,您如何模拟 OpsWorks 特定的服务/依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938335/

    相关文章:

    json - 删除 Lambda 支持的自定义资源的支持 Lambda 函数

    aws-cloudformation - 云信息+OpsWorks

    amazon-web-services - AWS Cognito/Amplify 返回空刷新 token

    amazon-web-services - terraform 中的 Redis 支持

    ruby - 在 ruby​​ 中访问文件 - 差异

    version - Chef Recipe 版本删除,或更新特定版本

    linux - 从 Linux 同步到 AWS S3 Bucket 时保留所有者和文件权限信息

    java - 如何与加密的 SQS 队列交互? KMS key 访问不起作用

    chef-infra - Chef 在用户的主目录中创建文件

    ruby-on-rails - 使用 unicorn 将 Rails 应用程序部署到 Opsworks 时出错