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

时间:2014-09-19 16:20:51

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

我正在为一些内置的OpsWorks烹饪书编写Chef包装器。我正在使用Berkshelf从他们的github repo克隆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

我的metadata.rb:

depends 'deploy'
depends 'mysql'
depends 'memcached'

问题是,当我尝试覆盖依赖于opsworks哈希中的node键的属性时,我得到了:

NoMethodError
-------------
undefined method `[]=' for nil:NilClass

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

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:3)

我强烈建议您查看Mike Greiling的博客文章Simplify OpsWorks Development With Packer和他的github repo 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虚拟框图像
    1. mkdir ~/packer && cd ~/packer
    2. git clone https://github.com/pixelcog/opsworks-vm.git
    3. cd opsworks-vm
    4. rake build install
    5. 这会将新的虚拟机vm安装到〜/ .vagrant.d / boxes / ubuntu1404-opsworks /
  6. 要模拟单个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" Examplestack.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 up来启动实例。然后,您可以在本地修改您的食谱,并通过使用vagrant provision.对现有实例重新运行厨师来应用任何更改您可以执行vagrant destroyvagrant up从头开始

答案 1 :(得分:0)

您必须手动执行此操作。您可以向.kitchen.yml添加任意属性,只需使用OpsWorks计算机并记录您需要的值,直接使用它们或将其调整为可用的测试数据。

相关问题