2015年11月30日 星期一

AWS Opswork for windows , customize layer / recipes

AWS Opswork stack , 

  if you want to use windows , custom layer  only support windows 2012

required cookbook

  • aws Cookbook https://github.com/chef-cookbooks/aws
  • Windows Cookbook https://github.com/chef-cookbooks/windows
  •  java https://github.com/agileorbit-cookbooks/java

write your recipe

install java , setting custom json and set receipe to custom layer, "java"
{
  "java" : {
      "install_flavor": "windows", 
      "package_name":"Java SE Development Kit 8 Update 20 (64-bit)",
      "url" : "s3://backet/XXXX"
  }
}
install awscli for windows (I don't like powershell) default.rb
if kernel['machine'] =~ /x86_64/
  default['awscli']['url']          = "https://s3.amazonaws.com/aws-cli/AWSCLI64.msi"
else
  default['awscli']['url']          = "https://s3.amazonaws.com/aws-cli/AWSCLI32.msi"  
end

default['awscli']['checksum'] = nil
default['awscli']['package_name'] = "AWS Command Line Interface"
your_recipe.rb
pkg_checksum = node['awscli']['checksum']

windows_package node['awscli']['package_name'] do
  source node['awscli']['url']  
  checksum pkg_checksum if pkg_checksum 
  action :install
end
install .net3.5 feature for windows, default windows 2012 only .net4
Chef::Log.info("****** install .Net3.5 feature  ******")
windows_feature "NetFx3" do
  action :install
  all true
end  
install tomcat
pkg_checksum = node['tomcat']['checksum']

windows_package node['tomcat']['package_name'] do 
  source node['tomcat']['url']
  checksum pkg_checksum if pkg_checksum 
  action :install
end

windows_service node['piston']['tomcat']['service_name'] do
  action :configure_startup
  startup_type :automatic
end

Chef::Log.info("******Add firewall rule******")
#TO-DO: need to modify port as parameter
batch "Add firewall rule for Tomcat Server" do
  code <<-eoh data-blogger-escaped-action="allow" data-blogger-escaped-add="" data-blogger-escaped-advfirewall="" data-blogger-escaped-end="" data-blogger-escaped-eoh="" data-blogger-escaped-firewall="" data-blogger-escaped-localport="8080" data-blogger-escaped-name="Open Port 8080" data-blogger-escaped-netsh="" data-blogger-escaped-pre="" data-blogger-escaped-protocol="TCP" data-blogger-escaped-rule="" dir="in">

download app from s3 and unzip
package_file = "#{node['app']['deploy']['dist']}\\#{node['app']['package_name']}"

Chef::Log.info("****** fatch app zip from s3 dist******")  #ec2 with IAM role have s3 access policy , don't set access key
aws_s3_file package_file do
    if aws_access_key_id && aws_secret_access_key
      aws_access_key_id aws_access_key_id
      aws_secret_access_key aws_secret_access_key
    end
    
    bucket node['app']['deploy']['s3_bucket']
    remote_path node['app']['deploy']['s3_key']
    backup false
    action :create
end

deploy_dir = node['app']['deploy']['directory'] # tomcat folder

Chef::Log.info("****** unzip app to local folder path ******")
windows_zipfile deploy_dir do
  source package_file
  overwrite true
  action :unzip
end

沒有留言:

張貼留言