Saturday, November 18, 2017

cookbook for kubernetes master

Attributes

#

# Cookbook Name:: kubernetes
# Attributes:: default
#
# Copyright 2016, whitestar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
default['kubernetes']['version'] = '1.7.8'
default['kubernetes']['fullversion'] = '1.7.8-1'
default['kubernetes']['domainname'] = 'xx.xx.xx.xx'
default['kubernetes']['yum_repo'] = {
  'baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64',
  'gpgcheck' => '1',
  'repo_gpgcheck' => '1',
  'gpgkey' => 'https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg',

}
===========================================================

metadata.rb

name 'kubernetes'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'All Rights Reserved'
description 'Installs/Configures kubernetes'
long_description 'Installs/Configures kubernetes'
version '0.2.0'
chef_version '>= 12.1' if respond_to?(:chef_version)
==========================================================
recipes

#
# Cookbook:: kubernetes
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.


template '/etc/yum.repos.d/kubernetes.repo' do
  source  'etc/yum.repos.d/kubernetes.repo'
  owner 'root'
  group 'root'
  mode '0644'
end
template '/opt/clusterrole.yml' do
  source  'opt/clusterrole.yml'
  owner 'root'
  group 'root'
  mode '0644'
end

bash 'systemctl_daemon-reload' do
  code <<-EOH
    systemctl daemon-reload
  EOH
  action :nothing
end
execute "disable selinux - running" do
      command "/usr/sbin/setenforce 0"

end

execute 'Disable selinux' do
  command "sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"
end
[
    "kubeadm",
  ].each {|pkg|
    package pkg do
========================================================================

/xxxxxx/xxxxxxxxx/src/xxxxxxx/cookbooks/kubernetes/templates/default/etc/systemd/system/docker.service.d

cat override.conf 

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd overlay 

============================================================
/xxxxxx/xxxxxxx/xxxx/xxxxxx/cookbooks/kubernetes/templates/default/opt

cat clusterrole.yml 
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  labels:
    k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard
  namespace: kube-system
========================================================================

cookbook for monitoring-script to know the ecs-agent connectivity status

Berksfile

# frozen_string_literal: true
source 'https://supermarket.chef.io'


metadata

Attributes

#
# Cookbook Name:: checkagentconnect
# Attributes:: default

default['CLUSTER']['ENV'] = 'Dev'
Metadata.rb

name 'checkagentconnect'
maintainer 'The Authors'
maintainer_email 'kaushikvhm@gmail.com'
license 'All Rights Reserved'
description 'Installs/Configures checkagentconnect'
long_description 'Installs/Configures checkagentconnect'
version '0.1.0'
chef_version '>= 12.1' if respond_to?(:chef_version)

recipes

#
# Cookbook:: checkagentconnect
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.


template '/tmp/get-pip.py' do
source 'tmp/get-pip.py.erb'
mode '0755'
end

bash 'Install python pip and awscli' do

  code <<-EOH
       python /tmp/get-pip.py
       pip install awscli
    EOH
  not_if { ::File.exist?('/tmp/get-pip.py') }
end

template '/tmp/AgentStatusWithInstance.sh' do
source 'tmp/AgentStatusWithInstance.sh.erb'
mode '0755'
end
cron 'AgentNotification' do
hour '*'
minute '*/1'
command "/tmp/AgentStatusWithInstance.sh"
#user centos
end
======================================================================

Templates 

#!/bin/bash
getLocalIpaddress=`hostname -I`
arrayIpaddress=($getLocalIpaddress)
validIpaddress=${arrayIpaddress[0]}
#echo $validIpaddress
aws ec2 describe-instances >> /tmp/jsontxt
var=$(jq --arg validIpaddress "$validIpaddress" '.Reservations[].Instances[]|select(.PrivateIpAddress==$validIpaddress)|.InstanceId' jsontxt)
var2=$(echo $var|tr -d '"')
#echo $var2
ECS_CLUSTER=<%= node['CLUSTER']['ENV']%>
CONTAINERS_ID=$(aws ecs list-container-instances --cluster $ECS_CLUSTER --output text --query 'containerInstanceArns')
DATE=$(date +%Y-%m-%d-%H:%M)
for container in $CONTAINERS_ID
do
STATUS=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output json --query 'containerInstances[0].agentConnected')
CHECK_INSTANCE_ID=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].ec2InstanceId')
#echo "check instance $CHECK_INSTANCE_ID"
if [ "$var2"  = "$CHECK_INSTANCE_ID" ]
then
if [ $STATUS == "true" ]
then
#echo $validIpaddress
#echo $var2
Agentversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.agentVersion')
echo $Agentversion
dockerversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.dockerVersion')
#echo $dockerversion
tags=$(jq --arg instanceid "$var2" '.Reservations[].Instances[]|select(.InstanceId==$instanceid)|.Tags[]|select(.Key=="Name")|.Value' /tmp/jsontxt)
#echo $tags
echo -e "Ipaddress=$validIpaddress\n Agentversion=$Agentversion\n dockerversion=$dockerversion\n tags=$tags\n" | mailx -s "ECS-Agent is not connected with the following host" xxxxxxxxxxxx@mail.com
fi
=====================================================================


Get container logs on to the host machine using the VOLUMES

ecs register-task-definition --family xxxx-xxxx-xxxxapp --volumes "[{"name": "xxxxx-xxxx-appname","host": { "sourcePath": "/opt/logs/appname" } }]" --container-definitions "[{"name":"appname_container","image":"xxxxx.dkr.ecr.us-east-1.amazonaws.com/appname:${buildNumber}","cpu": 10,"memory":300,"workingDirectory": "/opt/appname","essential": true,"environment": [{"name": "NODE_ENV","value": "xxxx"}],"mountPoints": [ {"sourceVolume": "applicationname","containerPath": "/opt/applicationname/logs","readOnly": false} ],"portMappings": [ {"hostPort": 8080,"containerPort": 8080,"protocol": "tcp" } ],"command": ["node","bin/www"]}]" --network-mode "bridge".


VOLUME:

--volumes "[{"name": "xxxxx-xxxx-appname","host": { "sourcePath": "/opt/logs/appname" } }]"


Container path for logs (mounted to volume in the ecs-taskdefintion in the ECS)

"mountPoints": [ {"sourceVolume": "applicationname","containerPath": "/opt/applicationname/logs","readOnly": false} ]



ECS-Agent connectivity status and notify the agent connectivity through mail

#!/bin/bash
getLocalIpaddress=`hostname -I`
arrayIpaddress=($getLocalIpaddress)
validIpaddress=${arrayIpaddress[0]}
#echo $validIpaddress
aws ec2 describe-instances >> jsontxt
var=$(jq --arg validIpaddress "$validIpaddress" '.Reservations[].Instances[]|select(.PrivateIpAddress==$validIpaddress)|.InstanceId' jsontxt)
var2=$(echo $var|tr -d '"')
#echo $var2
ECS_CLUSTER="Dev"
CONTAINERS_ID=$(aws ecs list-container-instances --cluster $ECS_CLUSTER --output text --query 'containerInstanceArns')
DATE=$(date +%Y-%m-%d-%H:%M)
for container in $CONTAINERS_ID
do
STATUS=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output json --query 'containerInstances[0].agentConnected')
CHECK_INSTANCE_ID=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].ec2InstanceId')
if [ "$var2"  = "$CHECK_INSTANCE_ID" ]
then
if [ $STATUS == "true" ]
then
echo $validIpaddress
echo $var2
Agentversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.agentVersion')
echo $Agentversion   
dockerversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.dockerVersion')
echo $dockerversion
tags=$(jq --arg instanceid "$var2" '.Reservations[].Instances[]|select(.InstanceId==$instanceid)|.Tags[]|select(.Key=="Name")|.Value' jsontxt)
echo $tags
echo -e "Ipaddress=$validIpaddress\n Agentversion=$Agentversion\n dockerversion=$dockerversion\n tags=$tags\n" | mailx -s "This is the subject" xxxxxxxxxx@gmail.com
#uuencode /home/centos/bodyattach.txt #/home/centos/bodyattach.txt | cat #/home/centos/bodyattach.txt | mailx -s "subject" -a #/home/centos/bodyattach.txt xxxxxxxx@gmail.com
fi
fi
done

rm jsontxt

Sunday, October 1, 2017

Example of chef cookbook

1)ec2-container-service(ecs)
2)ecs-exited-containers 
3)ec2-container-service_logfiles_on_host
4)container notification script
5)Nginx-container
6)docker-ce
7)Netscalercpx


============================================================



ec2-container-service(ecs)/recipes/

cat aws-linux.rb 
#
# Cookbook Name:: ec2-container-service
# Recipe:: aws-linux
#
# Copyright (c) 2016 The Authors, All Rights Reserved.

# Install ecs-init
package 'ecs-init' do
  action :install
end

# Create ecs dir
directory '/etc/ecs' do
  owner 'root'
  group 'root'
  mode '0755'
  action :create
end

template '/etc/ecs/ecs.config' do
  source 'ecs.config.erb'
  owner 'root'
  group 'root'
  mode '0644'
  variables(cluster: node['ecs']['cluster'])
end

service 'ecs' do
  supports status: false, restart: false, reload: false
  action [:start, :enable]
end


cat default.rb 
#
# Cookbook Name:: ec2-container-service
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.

# require docker 1.11.2 or greater

# if amazon linux, use amazon linux recipe, otherwise use centos recipe

cat docker-image.rb 
#
# Cookbook Name:: ec2-container-service
# Recipe:: docker-image
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
# require 'pry'

# if Chef::Config[:solo]
  aws_creds = { :aws_access_key => 'xxxxxxxxxxxxxxxxxxx', :aws_secret_key => 'xxxxxxxxxxxxxxxxxxxxxxxxx' }
  appuser =  'centos'
  appgroup = 'docker'
  user 'centos' do
    gid 'docker'
  end

bash 'Enable IAM roles for tasks' do
  user 'root'
  cwd '/tmp'
  code <<-EOH
    sysctl -w net.ipv4.conf.all.route_localnet=1
    iptables -t nat -A PREROUTING -p tcp -d xxxxxxxxxxxx --dport 80 -j DNAT --to-destination localhost:51679
    iptables -t nat -A OUTPUT -d xxxxxxxxx -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 51679
  EOH
end

# Pull container amazon/amazon-ecs-agent:latest
docker_image 'amazon/amazon-ecs-agent' do
   tag 'latest'
  read_timeout 300
  write_timeout 300
   action :pull
#   notifies :redeploy, 'docker_container[ecs-agent]'
end

# Create image paths
directory '/var/log/ecs' do
  owner appuser
  group appgroup
  mode '0755'
  action :create
end

directory '/var/lib/ecs' do
  owner appuser
  group appgroup
  mode '0755'
  action :create
end

directory '/var/lib/ecs/data' do
  owner appuser
  group appgroup
  mode '0755'
  action :create
end


# start docker container
docker_container 'ecs-agent' do
  repo 'amazon/amazon-ecs-agent'
  restart_policy 'always'  
  network_mode 'host'
  read_timeout 300
  write_timeout 300
  detach false
  volumes ['/var/run/docker.sock:/var/run/docker.sock', '/var/log/ecs/:/log', '/var/lib/ecs/data:/data']
  env ["ECS_CLUSTER=#{node["ecs"]["cluster"]}",
       "ECS_LOGFILE=#{node["ecs"]["log_file"]}",
       "ECS_LOGLEVEL=#{node["ecs"]["log_level"]}",
       "ECS_DATADIR=#{node["ecs"]["data_dir"]}",
       "ECS_ENABLE_TASK_IAM_ROLE=#{node["ecs"]["enable_task_iam_role_network_host"]}",
       "ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=#{node["ecs"]["enable_task_iam_role_network_host"]}",
       "ECS_NUM_IMAGES_DELETE_PER_CYCLE=#{node["ecs"]["num_images_delete_per_cycle"]}",
       "AWS_ACCESS_KEY_ID=#{aws_creds[:aws_access_key]}",
       "AWS_SECRET_ACCESS_KEY=#{aws_creds[:aws_secret_key]}",
       "AWS_DEFAULT_REGION=#{node["ec2"]["placement_availability_zone"][0...-1]}"
  ]
  tag 'latest'
  action :run

end

cat .kitchen.aws.yml
---
driver:
  name: ec2
  aws_ssh_key_id: xxxxxxxxxxxxxxxxxxxxxx
  security_group_ids: ["sg-xxxxxxxxxx"]
  region: us-singapore-1
  availability_zone: t
  require_chef_omnibus: true
  subnet_id: subnet-xxxxxxxxxxx
  instance_type: t2.supersmall
  associate_public_ip: true
  interface: private
  tags:
    Name: kitchen-kaushik
    application_name: testing
  block_device_mappings: 
    - device_name: /dev/sda1
      ebs:
        volume_size: 20
        delete_on_termination: true  

transport:
  # username: centos
  ssh_key: ~/.ssh/xxxxxxxxxx.pem
  connection_timeout: 10
  connection_retries: 5

verifier:
  name: inspec

provisioner:
  name: chef_solo
  roles_path: ../../roles
  data_bags_path: ../../data_bags
  environments_path: ../../environments
  cookbook_path: ../../cookbooks
  always_update_cookbooks: true

platforms:
  - name: centos-7

suites:
  - name: default
    run_list:
      - recipe[ecs::docker-image]
    verifier:
      inspec_tests:
        - test/smoke/default

    attributes:

cat Berksfile
source 'https://supermarket.chef.io'

metadata
cookbook 'docker', '~> 2.9.7'

cookbook 'docker-grid'

cat attributes/attributes.rb 
default['ecs']['cluster']
default['ecs']['log_file'] = '/log/ecs-agent.log'
default['ecs']['log_level'] = 'info'
default['ecs']['data_dir'] = '/data'
default['ecs']['enable_task_iam_role'] = 'true'
default['ecs']['enable_task_iam_role_network_host'] = 'true'

default['ecs']['num_images_delete_per_cycle'] = '10'

cat metadata.rb 
name 'ec2-container-service'
maintainer 'legend'
maintainer_email 'legend@gmail.com'
license 'all_rights'
description 'Installs/Configures ecs'
long_description 'Installs/Configures ecs'
version '0.1.2'

depends 'docker', '~> 2.0'
# If you upload to Supermarket you should set this so your cookbook
# gets a `View Issues` link
# issues_url 'https://github.com/<insert_org_here>/ecs/issues' if respond_to?(:issues_url)

# If you upload to Supermarket you should set this so your cookbook
# gets a `View Source` link
# source_url 'https://github.com/<insert_org_here>/ecs' if respond_to?(:source_url)


cat templates/ecs.config.erb 
ECS_CLUSTER=<% @cluster %>
ECS_LOGFILE=<% @logfile %>
ECS_LOGLEVEL=<% @loglevel %>
ECS_DATADIR=<% @data_dir %>
ECS_ENABLE_TASK_IAM_ROLE=<% @iam_role_enable %>

ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=<% @iam_role_nethost %>

======================================================================



ecs-exited-containers 

cat recipes/ecs-remove-containers.rb 
#
# Cookbook:: ec2-container-service-exit-containers
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.

# bash 'Remove Exited and Dead containers' do
#   code <<-EOH
#     sudo docker ps -a | grep Exited | cut -d ' ' -f 1 | xargs sudo docker rm
#   EOH
#   only_if  "test ! -z $(sudo docker ps -a | grep Exited | cut -d ' ' -f 1 )"
# end
template '/tmp/remove_exited_container.sh' do 
source 'remove_exited_container.sh.erb'
mode '0755'
end

cron 'remove_exited_containers' do
hour '*'
minute '*/5'
command "/tmp/remove_exited_container.sh"
end


ecs-exit-containers/templates/default }-> cat remove_exited_container.sh.erb 

#!/bin/bash



 docker ps -a | grep -iE 'exited|dead' | cut -d ' ' -f 1 | xargs docker rm -f -v



cat metadata.rb 
name 'ec2-container-service-exit-containers'
maintainer 'legend'
maintainer_email 'legend@gmail.com'
license 'All Rights Reserved'
description 'Installs/Configures ecs-exit-containers'
long_description 'Installs/Configures ecs-exit-containers'
version '0.1.10'
chef_version '>= 12.1' if respond_to?(:chef_version)

# The `issues_url` points to the location where issues for this cookbook are
# tracked.  A `View Issues` link will be displayed on this cookbook's page when
# uploaded to a Supermarket.
#
# issues_url 'https://github.com/<insert_org_here>/ecs-exit-containers/issues'

# The `source_url` points to the development repository for this cookbook.  A
# `View Source` link will be displayed on this cookbook's page when uploaded to
# a Supermarket.
#

source_url 'https://bitbucket.org/xxxxxxxxxxxxx/ec2-container-service-exit-containers'




=====================================================================



ec2-container-service_logfiles_on_host }-> cat Berksfile 
# frozen_string_literal: true
source 'https://supermarket.chef.io'


metadata







cat recipes/container_logfiles.rb 

#

# Cookbook:: ec2-container-services_logfiles_on_host

# Recipe:: default

#

# Copyright:: 2017, The Authors, All Rights Reserved.



template '/tmp/ec2-container-container-service_logfiles_onhost.sh' do

source 'container_logfiles_onhost.sh.erb'

mode '0755'
end

directory '/var/log/applicationname' do 
owner 'root' 
group 'root' 
mode '0755' 
action :create 
end

#directory '/var/log/applicationname' do
#owner 'root' 
#group 'root' 
#mode '0755' 
#action :create 
#end


cat templates/default/ec2-container-service_logfiles_onhost.sh.erb 
#!/bin/bash
container=`docker ps | grep "docker-applicationname" | cut -d' ' -f 1` 
#sudo chown owner. /var/log/docker-applicationame

docker exec -it $container sh -c "cat /rm/applicationname/logs/app.log" > /var/log/applicationname/$(date +"%Y_%m_%d_%I_%M_%p").log


======================================================================


container notification script:



 cat Berksfile
# frozen_string_literal: true
source 'https://supermarket.chef.io'


metadata

cat recipes/docker-container-notification.rb 
#
# Cookbook:: mailx
# Recipe:: default
#
# Copyright:: 2021, The Authors, All Rights Reserved.

%w( mailx sharutils ).each do |pkg|
package pkg
end

service 'postfix' do
action [ :enable,:start ]
end

template '/tmp/mailx_script.sh' do 
source 'mailx_script.sh.erb'
mode '0755'
end

cron 'mailx_notification' do
hour '*'
minute '*/2'
command "/tmp/mailx_script.sh"
#user centos 

end

templates/default }-> cat mailx_script.sh.erb 
#!/bin/bash
docker ps -a  --format "{{.ID}} {{.Names}}" -f status=exited > /tmp/a
   for APPID in `cat /tmp/a | awk '{print $1}'`
     do
    docker logs -t "$APPID"  > /tmp/"$APPID"_log.txt;
    done
   for i in `cat /tmp/a | awk '{ print $1 }'`
     do
       j=`cat /tmp/a | grep $i |awk '{print $2}'`
       uuencode /tmp/"$i"_log.txt /tmp/"$i"_log.txt|cat "/tmp/"$i"_log.txt" | mailx -s "$j" -a /tmp/"$i"_log.txt legend@gmail.com
     done

===================================================================

Nginx-container



cat Berksfile
source 'https://supermarket.chef.io'
cookbook 'docker', '~> 2.16.0'


metadata


/nginx-container }-> cat metadata.rb 

depends 'docker', '~> 2.0'



cookbooks/nginx-container/files/default/dockerimages/nginx

cat Dockerfile 
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
# Remove host checking 
RUN echo "Host http://bitbucket.org\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config 
RUN git clone https://xxxxxxxxxxxxxxx@bitbucket.org/repoowner/xxxxxxxxxxxxxxxxxx.git /opt/xxxxxxxxxxx/


nginx.conf

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size  1024;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;



/cookbooks/nginx-container/recipes 

#
# Cookbook Name:: nginx-c
# Recipe:: default
#
# Copyright (c) 2017 The Authors, All Rights Reserved.
docker_service 'default' do
  action [:create, :start]
end

directory '/tmp/dockerimages' do
  owner 'root'
  group 'root'
  mode '0770'
  action :create
end

remote_directory "/tmp/dockerimages" do
  files_mode '0770'
  files_owner 'root'
  mode '0770'
  owner 'root'
  source "dockerimages"
end

docker_image 'nginx' do
  source '/tmp/dockerimages/nginx/'
  tag 'nginx-v0.1'
  action :build_if_missing
  read_timeout 6000
  write_timeout 6000
end

docker_container 'nginx-proxy-container' do
  repo 'nginx'
  tag 'nginx-v0.1'
  restart_policy 'unless-stopped'
  tty true
  command "/bin/bash"
  open_stdin true
  action :run
end

.kitchen.yml
---
driver:
  name: vagrant

provisioner:
  name: chef_zero
  # You may wish to disable always updating cookbooks in CI or other testing environments.
  # For example:
  #   always_update_cookbooks: <%= !ENV['CI'] %>
  always_update_cookbooks: true

verifier:
  name: inspec

platforms:
  - name: centos-7.2

suites:
  - name: default
    run_list:
      - recipe[nginx-c::default]
    verifier:
      inspec_tests:
        - test/recipes

    attributes:

=========================================================
ntp-centos7

cookbooks/ntp-centos7/attributes }-> cat default.rb 
default['ntp']['service'] = 'ntpd'
default['ntp']['conffile'] = '/etc/ntp.conf'
default['ntp']['varlibdir'] = '/var/lib/ntp'
default['ntp']['statsdir'] = '/var/log/ntpstats/'
default['ntp']['packages'] = %w(ntp)
default['ntp']['conf_owner'] = 'root'
default['ntp']['conf_group'] = 'root'
default['ntp']['var_owner'] = 'ntp'
default['ntp']['var_group'] = 'ntp'
#default['ntp']['packages'] = %w(ntp ntpdate) if node['platform_version'].to_i >= 7

default['ntp']['leapfile'] = '/etc/ntp.leapseconds'


cookbooks/ntp-centos7/files/default }-> cat ntp.leapseconds 
#
# In the following text, the symbol '#' introduces
# a comment, which continues from that symbol until
# the end of the line. A plain comment line has a
# whitespace character following the comment indicator.
# There are also special comment lines defined below.
# A special comment will always have a non-whitespace
# character in column 2.
#
# A blank line should be ignored.
#
# The following table shows the corrections that must
# be applied to compute International Atomic Time (TAI)
# from the Coordinated Universal Time (UTC) values that
# are transmitted by almost all time services.
#
# The first column shows an epoch as a number of seconds
# since 1900.0 and the second column shows the number of
# seconds that must be added to UTC to compute TAI for
# any timestamp at or after that epoch. The value on
# each line is valid from the indicated initial instant
# until the epoch given on the next one or indefinitely
# into the future if there is no next line.
# (The comment on each line shows the representation of
# the corresponding initial epoch in the usual
# day-month-year format. The epoch always begins at
# 00:00:00 UTC on the indicated day. See Note 5 below.)
#
# Important notes:
#
# 1. Coordinated Universal Time (UTC) is often referred to
# as Greenwich Mean Time (GMT). The GMT time scale is no
# longer used, and the use of GMT to designate UTC is
# discouraged.
#
# 2. The UTC time scale is realized by many national
# laboratories and timing centers. Each laboratory
# identifies its realization with its name: Thus
# UTC(NIST), UTC(USNO), etc. The differences among
# these different realizations are typically on the
# order of a few nanoseconds (i.e., 0.000 000 00x s)
# and can be ignored for many purposes. These differences
# are tabulated in Circular T, which is published monthly
# by the International Bureau of Weights and Measures
# (BIPM). See www.bipm.fr for more information.
#
# 3. The current defintion of the relationship between UTC
# and TAI dates from 1 January 1972. A number of different
# time scales were in use before than epoch, and it can be
# quite difficult to compute precise timestamps and time
# intervals in those "prehistoric" days. For more information,
# consult:
#
# The Explanatory Supplement to the Astronomical
# Ephemeris.
# or
# Terry Quinn, "The BIPM and the Accurate Measurement
# of Time," Proc. of the IEEE, Vol. 79, pp. 894-905,
# July, 1991.
#
# 4.  The insertion of leap seconds into UTC is currently the
# responsibility of the International Earth Rotation Service,
# which is located at the Paris Observatory:
#
# Central Bureau of IERS
# 61, Avenue de l'Observatoire
# 75014 Paris, France.
#
# Leap seconds are announced by the IERS in its Bulletin C
#
# See hpiers.obspm.fr or www.iers.org for more details.
#
# All national laboratories and timing centers use the
# data from the BIPM and the IERS to construct their
# local realizations of UTC.
#
# Although the definition also includes the possibility
# of dropping seconds ("negative" leap seconds), this has
# never been done and is unlikely to be necessary in the
# foreseeable future.
#
# 5. If your system keeps time as the number of seconds since
# some epoch (e.g., NTP timestamps), then the algorithm for
# assigning a UTC time stamp to an event that happens during a positive
# leap second is not well defined. The official name of that leap
# second is 23:59:60, but there is no way of representing that time
# in these systems.
# Many systems of this type effectively stop the system clock for
# one second during the leap second and use a time that is equivalent
# to 23:59:59 UTC twice. For these systems, the corresponding TAI
# timestamp would be obtained by advancing to the next entry in the
# following table when the time equivalent to 23:59:59 UTC
# is used for the second time. Thus the leap second which
# occurred on 30 June 1972 at 23:59:59 UTC would have TAI
# timestamps computed as follows:
#
# ...
# 30 June 1972 23:59:59 (2287785599, first time): TAI= UTC + 10 seconds
# 30 June 1972 23:59:60 (2287785599,second time): TAI= UTC + 11 seconds
# 1  July 1972 00:00:00 (2287785600) TAI= UTC + 11 seconds
# ...
#
# If your system realizes the leap second by repeating 00:00:00 UTC twice
# (this is possible but not usual), then the advance to the next entry
# in the table must occur the second time that a time equivlent to
# 00:00:00 UTC is used. Thus, using the same example as above:
#
# ...
#       30 June 1972 23:59:59 (2287785599): TAI= UTC + 10 seconds
#       30 June 1972 23:59:60 (2287785600, first time): TAI= UTC + 10 seconds
#       1  July 1972 00:00:00 (2287785600,second time): TAI= UTC + 11 seconds
# ...
#
# in both cases the use of timestamps based on TAI produces a smooth
# time scale with no discontinuity in the time interval.
#
# This complexity would not be needed for negative leap seconds (if they
# are ever used). The UTC time would skip 23:59:59 and advance from
# 23:59:58 to 00:00:00 in that case.  The TAI offset would decrease by
# 1 second at the same instant.  This is a much easier situation to deal
# with, since the difficulty of unambiguously representing the epoch
# during the leap second does not arise.
#
# Questions or comments to:
# Jeff Prillaman
# Time Service Department
# US Naval Observatory
# Washington, DC
# jeffrey.prillaman@usno.navy.mil
#
# Last Update of leap second values:  18 Apr 2017
#
# The following line shows this last update date in NTP timestamp
# format. This is the date on which the most recent change to
# the leap second data was added to the file. This line can
# be identified by the unique pair of characters in the first two
# columns as shown below.
#
#$ 3701462400
#
# The data in this file will be updated periodically as new leap
# seconds are announced. In addition to being entered on the line
# above, the update time (in NTP format) will be added to the basic
# file name leap-seconds to form the name leap-seconds.<NTP TIME>.
# In addition, the generic name leap-seconds.list will always point to
# the most recent version of the file.
#
# This update procedure will be performed only when a new leap second
# is announced.
#
# The following entry specifies the expiration date of the data
# in this file in units of seconds since 1900.0.  This expiration date
# will be changed at least twice per year whether or not a new leap
# second is announced. These semi-annual changes will be made no
# later than 1 June and 1 December of each year to indicate what
# action (if any) is to be taken on 30 June and 31 December,
# respectively. (These are the customary effective dates for new
# leap seconds.) This expiration date will be identified by a
# unique pair of characters in columns 1 and 2 as shown below.
# In the unlikely event that a leap second is announced with an
# effective date other than 30 June or 31 December, then this
# file will be edited to include that leap second as soon as it is
# announced or at least one month before the effective date
# (whichever is later).
# If an announcement by the IERS specifies that no leap second is
# scheduled, then only the expiration date of the file will
# be advanced to show that the information in the file is still
# current -- the update time stamp, the data and the name of the file
# will not change.
#
# Updated through IERS Bulletin C 53
# File expires on:  1 Dec 2017
#
#@ 3721075200
#
2272060800 10 # 1 Jan 1972
2287785600 11 # 1 Jul 1972
2303683200 12 # 1 Jan 1973
2335219200 13 # 1 Jan 1974
2366755200 14 # 1 Jan 1975
2398291200 15 # 1 Jan 1976
2429913600 16 # 1 Jan 1977
2461449600 17 # 1 Jan 1978
2492985600 18 # 1 Jan 1979
2524521600 19 # 1 Jan 1980
2571782400 20 # 1 Jul 1981
2603318400 21 # 1 Jul 1982
2634854400 22 # 1 Jul 1983
2698012800 23 # 1 Jul 1985
2776982400 24 # 1 Jan 1988
2840140800 25 # 1 Jan 1990
2871676800 26 # 1 Jan 1991
2918937600 27 # 1 Jul 1992
2950473600 28 # 1 Jul 1993
2982009600 29 # 1 Jul 1994
3029443200 30 # 1 Jan 1996
3076704000 31 # 1 Jul 1997
3124137600 32 # 1 Jan 1999
3345062400 33 # 1 Jan 2006
3439756800 34 # 1 Jan 2009
3550089600 35 # 1 Jul 2012
3644697600 36 # 1 Jul 2015
3692217600 37 # 1 Jan 2017
#
# the following special comment contains the
# hash value of the data in this file computed
# use the secure hash algorithm as specified
# by FIPS 180-1. See the files in ~/sha for
# the details of how this hash value is
# computed. Note that the hash computation
# ignores comments and whitespace characters
# in data lines. It includes the NTP values
# of both the last modification time and the
# expiration time of the file, but not the
# white space on those lines.
# the hash line is also ignored in the
# computation.
#
#h 3f004255 91f969f7 252361e5 27aa6754 eb6b7c72
#


cookbooks/ntp-centos7/recipes }-> cat centos07.rb 
#
# Cookbook Name:: ntp-systemd
# Recipe:: default
#
# Copyright (c) 2017 The Authors, All Rights Reserved.
node['ntp']['packages'].each do |pkg|
package pkg
end


template node['ntp']['conffile'] do
  source 'ntp.conf.erb'
  owner node['ntp']['conf_owner']
  group node['ntp']['conf_group']
  mode '0644'

end

[node['ntp']['varlibdir'], node['ntp']['statsdir']].each do |ntpdir|
  directory ntpdir do
    owner node['ntp']['var_owner']
    group node['ntp']['var_group']
    mode '0755'
  end
end

cookbook_file node['ntp']['leapfile'] do
  owner node['ntp']['conf_owner']
  group node['ntp']['conf_group']
  mode '0644'
  source 'ntp.leapseconds'
  notifies :restart, "service[#{node['ntp']['service']}]"
end


service node['ntp']['service'] do
  supports status: true, restart: true
  action [:enable, :start]
end

execute 'Change the timezone to NewYork for EDT' do
  command "timedatectl set-timezone America/New_York"
  only_if "test -z $( date | grep -i EDT | cut -d ' ' -f 6 )"
end


cookbooks/ntp-centos7/templates/default }-> cat ntp.conf.erb Generated by Chef
# Local modifications will be overwritten.
tinker panic 0 allan 1500 dispersion 15 step 0.128 stepout 900
statsdir /var/log/ntpstats/
leapfile /etc/ntp.leapseconds
driftfile /var/lib/ntp/ntp.drift

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable



disable monitor


server 0.pool.ntp.org iburst minpoll 6 maxpoll 10
restrict 0.pool.ntp.org nomodify notrap noquery
server 1.pool.ntp.org iburst minpoll 6 maxpoll 10
restrict 1.pool.ntp.org nomodify notrap noquery
server 2.pool.ntp.org iburst minpoll 6 maxpoll 10
restrict 2.pool.ntp.org nomodify notrap noquery
server 3.pool.ntp.org iburst minpoll 6 maxpoll 10
restrict 3.pool.ntp.org nomodify notrap noquery

restrict default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict -6 default kod notrap nomodify nopeer noquery
restrict -6 ::1
=========================================================
docker-ce

vi attributes/default.rb



# Cookbook Name:: docker-ce
# Attributes:: default
# Copyright 2016, whitestar
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and
# limitations under the License.
#
default['docker-grid']['CE']['version_on_centos'] = '17.09.0.ce-1'
default['docker-grid']['CE']['version_on_ubuntu'] = '17.09.0-ce'
default['docker-grid']['yum_repo'] = {
  'baseurl' => 'https://download.docker.com/linux/centos/7/x86_64/stable/',
  'gpgcheck' => '1',
  'gpgkey' => 'https://download.docker.com/linux/centos/gpg',
}
default['docker-grid']['CE']['version'] = node.value_for_platform(
  ['centos', 'redhat'] => {
    'default' => "#{node['docker-grid']['CE']['version_on_centos']}.el#{node['platform_version'].to_i}.#{node['platform']}",
  },
  'ubuntu' => {
    'default' => "#{node['docker-grid']['CE']['version_on_ubuntu']}~#{node['lsb']['codename']}",
  }
)
default['docker-grid']['CE']['storage-driver_on_centos'] = 'overlay'
default['docker-grid']['CE']['storage-driver_on_ubuntu'] = 'aufs'

default['docker-grid']['CE']['storage-driver'] = node.value_for_platform(

ash 'systemctl_daemon-reload' do
  code <<-EOH
    systemctl daemon-reload
  EOH
  action :nothing
en
if node['docker-grid']['CE']['storage-driver'] == 'overlay'
  bash 'load_overlayfs_kernel_module' do
    code <<-EOH
      modprobe overlay
    EOH
    not_if 'lsmod | grep overlay'
  end
 template '/etc/modules-load.d/overlay.conf' do
    source  'etc/modules-load.d/overlay.conf'
    owner 'root'
    group 'root'
=====
recipes/default.rb
# https://dcos.io/docs/1.8/administration/installing/custom/system-requirements/
bash 'systemctl_daemon-reload' do
  code <<-EOH
    systemctl daemon-reload
  EOH
  action :nothing
end

if node['docker-grid']['CE']['storage-driver'] == 'overlay'
  bash 'load_overlayfs_kernel_module' do
    code <<-EOH
      modprobe overlay
    EOH
    not_if 'lsmod | grep overlay'
  end
 template '/etc/modules-load.d/overlay.conf' do
    source  'etc/modules-load.d/overlay.conf'
    owner 'root'
    group 'root'
    mode '0644'
  end
end
log 'reboot_message' do
  message 'Please reboot this machine because of kernel boot option modified.'
  level :warn
  action :nothing
end
userns_remap = node['docker-grid']['CE']['userns-remap']
if !userns_remap.nil? && !userns_remap.empty?
  unless Gem::Version.create(node['docker-grid']['CE']['version'].tr('~', '-')) >= Gem::Version.create('17.06')
    # tr('~', '-') for Ubuntu.
    Chef::Application.fatal!('Docker version must be 1.10 or later for userns-remap.')  # and exit.
  end
  # By default user namespace feature is inactive in RHEL family (>= 7.2).
  if node['platform_family'] == 'rhel'
    unless Gem::Version.create(node['platform_version']) >= Gem::Version.create('7.2')
      Chef::Application.fatal!('Platform version must be 7.2 or later for kernel user namespace feature.')  # and exit.
    end

    bash 'enable_user_namespace_feature_of_kernerl' do
      code <<-"EOH"
        grubby --args='user_namespace.enable=1' --update-kernel=/boot/vmlinuz-#{node['kernel']['release']}
      EOH
      not_if "grubby --info=/boot/vmlinuz-#{node['kernel']['release']} | grep 'user_namespace.enable=1'"
      notifies :write, 'log[reboot_message]'
    end
  end

  subid_files = [
    '/etc/subuid',
    '/etc/subgid',
  ]

  subid_files.each {|subid_file|
    file subid_file do
      owner 'root'
      group 'root'
      mode '0644'
      action :touch
      not_if { File.exist?(subid_file) }
    end
  }

  this_recipe = self
  remap_user = userns_remap == 'default' ? 'dockremap' : userns_remap
  ruby_block 'adds_subid_entries' do
    action :run
    not_if "cat /etc/subuid | grep #{remap_user}"
    not_if "cat /etc/subgid | grep #{remap_user}"
    notifies :restart, 'service[docker]'
    block do
      subid_files.each {|subid_file|
        max_start_id = 100_000
        offset = 0
        already_exist = false

        begin
          File.open(subid_file) {|file|
            file.each_line {|line|
              entry = line.split(':')
              if entry[0] == remap_user
                already_exist = true
                break
              end
              if entry[1].to_i >= max_start_id
                max_start_id = entry[1].to_i
                offset = entry[2].to_i
              end
            }
          }

          if already_exist
            this_recipe.log "#{remap_user} already exists in #{subid_file}"
          else
            File.open(subid_file, 'a') {|file|
              entry_str = "#{remap_user}:#{max_start_id + offset}:65536"
              this_recipe.log "#{remap_user} (#{entry_str}) is added in #{subid_file}"
              file.puts entry_str
            }
          end
        rescue IOError => e
          puts e
        end
      }
    end
  end
end
directory '/etc/systemd/system/docker.service.d' do
  owner 'root'
  group 'root'
  mode '0755'
  action :create
end

template '/etc/systemd/system/docker.service.d/override.conf' do
  source  'etc/systemd/system/docker.service.d/override.conf'
  owner 'root'
  group 'root'
  mode '0644'
  notifies :run, 'bash[systemctl_daemon-reload]'
  notifies :restart, 'service[docker]'
end
case node['platform_family']
when 'rhel'
 [
     'yum-utils', 
     'device-mapper-persistent-data',
  'lvm2'
  
  ].each {|pkg|
    package pkg do
      action :install
    end
  }
  execute "docker-ce repository adding" do
       command "yum-config-manager --add-repo  https://download.docker.com/linux/centos/docker-ce.repo"
  end
  execute " enable docker-ce-edge" do
       command "yum-config-manager --enable docker-ce-edge"
  end
  [
    
    'docker-ce'
   
  ].each {|pkg|
    package pkg do
      action :install
      version node['docker-grid']['CE']['version']
      #options '--enablerepo=dockerrepo'
    end
  }
execute " disable docker-ce-edge" do
       command "yum-config-manager --disable docker-ce-edge"
  end
when 'debian'


end
service 'docker' do
  action [:start, :enable]
end


 cat .kitchen.aws.yml 
---
driver:
  name: ec2
  aws_ssh_key_id: xxxxxxxxxxxx
  security_group_ids: ["sg-xxxxxxxxxxxx”]
  region: us-xxxxxxxxx
  availability_zone: x
  require_chef_omnibus: true
  subnet_id: subnet-xxxxxxxx
  instance_type: t2.small
  associate_public_ip: true
  interface: private
  tags:
    Name: test-docker-cce-demo
    application_name: chef-testing
  block_device_mappings: 
    - device_name: /dev/sda1
      ebs:
        volume_size: 20
        delete_on_termination: true  

transport:
  # username: centos
  ssh_key: ~/.ssh/xxxxxxxxxxxxxxxxxx.pem
  connection_timeout: 10
  connection_retries: 5

verifier:
  name: inspec

provisioner:
  name: chef_solo
  roles_path: ../../roles
  data_bags_path: ../../data_bags
  environments_path: ../../environments
  cookbook_path: ../../cookbooks
  always_update_cookbooks: true

platforms:
  - name: centos-7

suites:
  - name: default
    run_list:
        - recipe[docker-ce::default]
    verifier:
      inspec_tests:
        - test/smoke/default
    attributes:

============================================================

Netscalercpx

#
# Cookbook Name::netscaler 
# Recipe:: default
#
# Copyright (c) 2017 The Authors, All Rights Reserved.
docker_service 'default' do
  action [:create, :start]
end

docker_image 'store/citrix/netscalercpx' do
  tag '11.1-53.11'
  action :pull
  read_timeout 6000
  write_timeout 6000
end

directory '/var/cpx' do
  owner 'root'
  group 'root'
  mode '0755'
  action :create
end

docker_container 'mycpx' do
  repo 'store/citrix/netscalercpx'
  tag '11.1-53.11'
  restart_policy 'unless-stopped'
  volumes '/var/cpx:/cpx'
  env ['EULA=yes']
  privileged true
  ulimits [
    { 'Name' => 'core', 'Soft' => -1, 'Hard' => -1}
  ]
  publish_all_ports true
  tty true
  command "/bin/sh -c 'bash -C '/var/netscaler/bins/docker_startup.sh''"
  # open_stdin true
  action :run
end

#bash 'running the netscalercpx conatiner' do 
#code 'docker run -dt -p 22 -p 80 -p 161/udp --privileged=true -e EULA=yes --name mycpx -v /var/cpx:/cpx --ulimit core=-1 store/citrix/netscalercpx:11.1-53.11' 
#only_if "test -z $(docker ps | grep mycpx | cut -d ' ' -f 1)" 
#end

============================================================