How do you mock specific OpsWorks services / dependencies when developing locally with a kitchen and a chef?

I write chef covers around some of the OpsWorks cookbooks. I use Berkshelf to clone OpsWorks cookbooks from my github relay.

This is my 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 

My metadata.rb:

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

The problem is that when I try to override attributes that depend on the opsworks key in the node hash, I get a:

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

OpsWorks has a whole bunch of pre-recipe dependencies that create these keys and do most of their settings. I would like to find a way to either use these services, or run them against my kitchen items, or make fun of them so that I can check my recipes.

Is there any way to do this?

+5
source share
2 answers

You will need to do this manually. You can add arbitrary attributes to your .kitchen.yml , just start the OpsWorks machine and register the values ​​you need and either use them directly or adapt them to workable test data.

0
source

I would highly recommend you check out Mike Gringh's blog post. Simplify the development of OpsWorks with Packer and its github opsworks-vm registry , which will help you make fun of the entire opsworks stack, including installing the opsworks agent, so you can also test application deployment recipes, several levels, multiple instances at the same time, etc. . This is very impressive.

Quick start on Ubuntu 14.04

NOTE. This cannot be done from the ubuntu virtual machine because the virtual box does not support the nested virtualization of 64-bit machines.

  • Install 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
  • Install 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
  • Install 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
  • Install 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
  • Build Mike Greiling opsworks-vm virtual camera image using Packer
    • mkdir ~/packer && cd ~/packer
    • git clone https://github.com/pixelcog/opsworks-vm.git
    • cd opsworks-vm
    • rake build install
    • This will install the new virtual vm box in ~ / .vagrant.d / boxes / ubuntu1404-opsworks /

To make fun of one instance of opsworks, create a new Vagrantfile as follows:

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

The path to the dna.json file is relative to the Vagrantfile and should contain any JSON data that you want to send to the OpsWorks chef.

For instance:

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

To mock multiple instances of opsworks and enable layers, see its AWS OpsWorks Getting Started Example , which includes stack.json below.

Vagrantfile (for multiple instances)

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

For those not familiar with the vagrant, just do vagrant up to start instance (s). You can then change your cooking locally, and any changes can be applied by re-launching the chef against existing instances using vagrant provision. You can do vagrant destroy and vagrant up to start from scratch.

+3
source

Source: https://habr.com/ru/post/1202991/


All Articles