From 6a7a01f5c695de57f408578ce74e3ace154ca4f2 Mon Sep 17 00:00:00 2001 From: krattan406 <30876207+krattan406@users.noreply.github.com> Date: Wed, 14 Feb 2018 13:01:46 -0800 Subject: [PATCH 1/4] Add files via upload --- 2_Tomcat/cookbooks/tomcat-chef/Berksfile | 4 + 2_Tomcat/cookbooks/tomcat-chef/LICENSE | 3 + 2_Tomcat/cookbooks/tomcat-chef/README.md | 4 + 2_Tomcat/cookbooks/tomcat-chef/chefignore | 107 ++++++++++++++++++ 2_Tomcat/cookbooks/tomcat-chef/metadata.rb | 20 ++++ .../cookbooks/tomcat-chef/recipes/default.rb | 61 ++++++++++ .../cookbooks/tomcat-chef/spec/spec_helper.rb | 3 + .../spec/unit/recipes/default_spec.rb | 35 ++++++ .../templates/default/tomcat.service.erb | 26 +++++ .../tomcat-chef/templates/tomcat.service.erb | 26 +++++ .../test/smoke/default/default_test.rb | 18 +++ 11 files changed, 307 insertions(+) create mode 100644 2_Tomcat/cookbooks/tomcat-chef/Berksfile create mode 100644 2_Tomcat/cookbooks/tomcat-chef/LICENSE create mode 100644 2_Tomcat/cookbooks/tomcat-chef/README.md create mode 100644 2_Tomcat/cookbooks/tomcat-chef/chefignore create mode 100644 2_Tomcat/cookbooks/tomcat-chef/metadata.rb create mode 100644 2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb create mode 100644 2_Tomcat/cookbooks/tomcat-chef/spec/spec_helper.rb create mode 100644 2_Tomcat/cookbooks/tomcat-chef/spec/unit/recipes/default_spec.rb create mode 100644 2_Tomcat/cookbooks/tomcat-chef/templates/default/tomcat.service.erb create mode 100644 2_Tomcat/cookbooks/tomcat-chef/templates/tomcat.service.erb create mode 100644 2_Tomcat/cookbooks/tomcat-chef/test/smoke/default/default_test.rb diff --git a/2_Tomcat/cookbooks/tomcat-chef/Berksfile b/2_Tomcat/cookbooks/tomcat-chef/Berksfile new file mode 100644 index 0000000..0656a99 --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/Berksfile @@ -0,0 +1,4 @@ +# frozen_string_literal: true +source 'https://supermarket.chef.io' + +metadata diff --git a/2_Tomcat/cookbooks/tomcat-chef/LICENSE b/2_Tomcat/cookbooks/tomcat-chef/LICENSE new file mode 100644 index 0000000..a27e740 --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/LICENSE @@ -0,0 +1,3 @@ +Copyright 2018 The Authors + +All rights reserved, do not redistribute. diff --git a/2_Tomcat/cookbooks/tomcat-chef/README.md b/2_Tomcat/cookbooks/tomcat-chef/README.md new file mode 100644 index 0000000..174cbb7 --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/README.md @@ -0,0 +1,4 @@ +# tomcat-chef + +TODO: Enter the cookbook description here. + diff --git a/2_Tomcat/cookbooks/tomcat-chef/chefignore b/2_Tomcat/cookbooks/tomcat-chef/chefignore new file mode 100644 index 0000000..38e7379 --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/chefignore @@ -0,0 +1,107 @@ +# Put files/directories that should be ignored in this file when uploading +# to a chef-server or supermarket. +# Lines that start with '# ' are comments. + +# OS generated files # +###################### +.DS_Store +Icon? +nohup.out +ehthumbs.db +Thumbs.db + +# SASS # +######## +.sass-cache + +# EDITORS # +########### +\#* +.#* +*~ +*.sw[a-z] +*.bak +REVISION +TAGS* +tmtags +*_flymake.* +*_flymake +*.tmproj +.project +.settings +mkmf.log + +## COMPILED ## +############## +a.out +*.o +*.pyc +*.so +*.com +*.class +*.dll +*.exe +*/rdoc/ + +# Testing # +########### +.watchr +.rspec +spec/* +spec/fixtures/* +test/* +features/* +examples/* +Guardfile +Procfile +.kitchen* +.rubocop.yml +spec/* +Rakefile +.travis.yml +.foodcritic +.codeclimate.yml + +# SCM # +####### +.git +*/.git +.gitignore +.gitmodules +.gitconfig +.gitattributes +.svn +*/.bzr/* +*/.hg/* +*/.svn/* + +# Berkshelf # +############# +Berksfile +Berksfile.lock +cookbooks/* +tmp + +# Policyfile # +############## +Policyfile.rb +Policyfile.lock.json + +# Cookbooks # +############# +CONTRIBUTING* +CHANGELOG* +TESTING* +MAINTAINERS.toml + +# Strainer # +############ +Colanderfile +Strainerfile +.colander +.strainer + +# Vagrant # +########### +.vagrant +Vagrantfile diff --git a/2_Tomcat/cookbooks/tomcat-chef/metadata.rb b/2_Tomcat/cookbooks/tomcat-chef/metadata.rb new file mode 100644 index 0000000..150037c --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/metadata.rb @@ -0,0 +1,20 @@ +name 'tomcat-chef' +maintainer 'The Authors' +maintainer_email 'you@example.com' +license '' +description 'Installs/Configures tomcat-chef' +long_description 'Installs/Configures tomcat-chef' +version '0.1.0' +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//tomcat-chef/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://github.com//tomcat-chef' diff --git a/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb b/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb new file mode 100644 index 0000000..eb27480 --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb @@ -0,0 +1,61 @@ +package 'java-1.7.0-openjdk-devel' + +group 'tomcat' + +user 'tomcat' do + manage_home false + shell '/bin/nologin' + group 'tomcat' + home '/opt/tomcat' +end + + +remote_file 'apache-tomcat-8.5.20.tar.gz' do + source 'https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz' +end + +directory '/opt/tomcat' do + mode '0700' + group 'tomcat' +end + +execute 'tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1' + + +execute "manage permissions conf" do + command "chgrp -R tomcat /opt/tomcat/conf && chmod g+r /opt/tomcat/conf/*" +end + + + +directory '/opt/tomcat/conf' do + mode '0070' +end + + +execute 'chmod -R g+r /opt/tomcat/conf/*' + +execute 'chown some files: work, temp, logs' do + command 'chown -R tomcat /opt/tomcat/work /opt/tomcat/temp /opt/tomcat/logs' +end + + +template '/etc/systemd/system/tomcat.service' do + source 'tomcat.service.erb' +end + +execute 'systemctl daemon-reload' + +service 'tomcat' do + action [:start, :enable] +end + + + + + + + + + + diff --git a/2_Tomcat/cookbooks/tomcat-chef/spec/spec_helper.rb b/2_Tomcat/cookbooks/tomcat-chef/spec/spec_helper.rb new file mode 100644 index 0000000..052d78a --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/spec/spec_helper.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true +require 'chefspec' +require 'chefspec/berkshelf' diff --git a/2_Tomcat/cookbooks/tomcat-chef/spec/unit/recipes/default_spec.rb b/2_Tomcat/cookbooks/tomcat-chef/spec/unit/recipes/default_spec.rb new file mode 100644 index 0000000..1cd4208 --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/spec/unit/recipes/default_spec.rb @@ -0,0 +1,35 @@ +# +# Cookbook:: tomcat-chef +# Spec:: default +# +# Copyright:: 2018, The Authors, All Rights Reserved. + +require 'spec_helper' + +describe 'tomcat-chef::default' do + context 'When all attributes are default, on Ubuntu 16.04' do + let(:chef_run) do + # for a complete list of available platforms and versions see: + # https://github.com/customink/fauxhai/blob/master/PLATFORMS.md + runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04') + runner.converge(described_recipe) + end + + it 'converges successfully' do + expect { chef_run }.to_not raise_error + end + end + + context 'When all attributes are default, on CentOS 7.4.1708' do + let(:chef_run) do + # for a complete list of available platforms and versions see: + # https://github.com/customink/fauxhai/blob/master/PLATFORMS.md + runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '7.4.1708') + runner.converge(described_recipe) + end + + it 'converges successfully' do + expect { chef_run }.to_not raise_error + end + end +end diff --git a/2_Tomcat/cookbooks/tomcat-chef/templates/default/tomcat.service.erb b/2_Tomcat/cookbooks/tomcat-chef/templates/default/tomcat.service.erb new file mode 100644 index 0000000..0528c6a --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/templates/default/tomcat.service.erb @@ -0,0 +1,26 @@ +# Systemd unit file for tomcat +[Unit] +Description=Apache Tomcat Web Application Container +After=syslog.target network.target + +[Service] +Type=forking + +Environment=JAVA_HOME=/usr/lib/jvm/jre +Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid +Environment=CATALINA_HOME=/opt/tomcat +Environment=CATALINA_BASE=/opt/tomcat +Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' +Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' + +ExecStart=/opt/tomcat/bin/startup.sh +ExecStop=/bin/kill -15 $MAINPID + +User=tomcat +Group=tomcat +UMask=0007 +RestartSec=10 +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/2_Tomcat/cookbooks/tomcat-chef/templates/tomcat.service.erb b/2_Tomcat/cookbooks/tomcat-chef/templates/tomcat.service.erb new file mode 100644 index 0000000..0528c6a --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/templates/tomcat.service.erb @@ -0,0 +1,26 @@ +# Systemd unit file for tomcat +[Unit] +Description=Apache Tomcat Web Application Container +After=syslog.target network.target + +[Service] +Type=forking + +Environment=JAVA_HOME=/usr/lib/jvm/jre +Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid +Environment=CATALINA_HOME=/opt/tomcat +Environment=CATALINA_BASE=/opt/tomcat +Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' +Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' + +ExecStart=/opt/tomcat/bin/startup.sh +ExecStop=/bin/kill -15 $MAINPID + +User=tomcat +Group=tomcat +UMask=0007 +RestartSec=10 +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/2_Tomcat/cookbooks/tomcat-chef/test/smoke/default/default_test.rb b/2_Tomcat/cookbooks/tomcat-chef/test/smoke/default/default_test.rb new file mode 100644 index 0000000..abf8d4d --- /dev/null +++ b/2_Tomcat/cookbooks/tomcat-chef/test/smoke/default/default_test.rb @@ -0,0 +1,18 @@ +# # encoding: utf-8 + +# Inspec test for recipe tomcat-chef::default + +# The Inspec reference, with examples and extensive documentation, can be +# found at http://inspec.io/docs/reference/resources/ + +unless os.windows? + # This is an example test, replace with your own test. + describe user('root'), :skip do + it { should exist } + end +end + +# This is an example test, replace it with your own test. +describe port(80), :skip do + it { should_not be_listening } +end From baefbb31c62228b7c0755cd53f3ef7115544df19 Mon Sep 17 00:00:00 2001 From: krattan406 <30876207+krattan406@users.noreply.github.com> Date: Wed, 14 Feb 2018 14:40:36 -0800 Subject: [PATCH 2/4] Update README.md --- 2_Tomcat/README.md | 92 +++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/2_Tomcat/README.md b/2_Tomcat/README.md index ad172c8..86d725f 100644 --- a/2_Tomcat/README.md +++ b/2_Tomcat/README.md @@ -1,45 +1,53 @@ -# Install and Configure Apache Tomcat +# Usage + +Assumption: You have a virtualbox with centOs7 and ChefDk 2.4.17 + +1) Login your virtualbox as super user from terminal. +2) Copy the cookbook directory to a base directory +3) change working directory to /cookbooks +4) Run the tomcat installation cookbook: + sudo chef-client --local-mode --runlist 'recipe[tomcat-chef]' + + Result - + [2018-02-14T17:11:39-05:00] WARN: No config file found or specified on command line, using command line options. + Starting Chef Client, version 13.6.4 + resolving cookbooks for run list: ["tomcat-chef"] + Synchronizing Cookbooks: + - tomcat-chef (0.1.0) + Installing Cookbook Gems: + Compiling Cookbooks... + Converging 13 resources + Recipe: tomcat-chef::default + * yum_package[java-1.7.0-open +  -- etc - Truncated + + +## Test + +root@osboxes cookbooks]# curl localhost/ + +Result: +If tomcat has been sucessfully installed and started using the cookbook then output of above curl is : + + + +

hello world

+ + + +## References +* https://docs.chef.io/resource_template.html +* https://docs.chef.io/resource_chef_client.html +* https://docs.chef.io/resource_remote_file.html +* https://docs.chef.io/resource_rpm_package.html +* https://docs.chef.io/resource_cookbook_file.html +* https://docs.chef.io/resource_service.html +* https://docs.chef.io/resource_chef_client.html +* https://www.youtube.com/watch?v=FOYc_SGWE-0 +* https://learn.chef.io/modules/learn-the-basics/rhel/virtualbox/make-your-recipe-more-manageable#/ +* https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7 +* https://files.fosswire.com/2007/08/fwunixref.pdf +* https://linux.die.net -The [Apache Tomcat®](http://tomcat.apache.org/) software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. The Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket specifications are developed under the Java Community Process. -## Goal -Use Chef to successfully install and configure tomcat on a RHEL7-based target system. - -## Success Criteria - -You should be prepared and able to demonstrate the following: - -* Your Chef cookbook successfully executes on your target node without errors -* Your Chef cookbook is portable and can be run by Chef to validate your work. Please include any instructions or assumptions needed to successfully execute your cookbook. -* You can interact with the default tomcat site in a browser or successfully `curl localhost` on the target system -* You can run `chef-client` multiple times without failures -* Your GitHub.com source code repository shows the history of your work - -You should be able to explain the following: - -* Steps taken to achieve the end result -* Build and test process of Chef code -* Tools and resources used in the process - ->Note: You are NOT required to use Chef Server for this exercise, but you may if that is your preference. - -## Instructions - -* Translate the Tomcat installation instructions from `INSTRUCTIONS.rb` into Chef code that completes the installation and configuration -* Use the Chef [Resources Reference](https://docs.chef.io/resources.html) to find the most appropriate Chef resources to use for each task -* Once you feel you have met the success criteria outlined above, send a link to your GitHub.com repo to the person coordinating these workshops on your behalf -* Provide instructions for us to run your cookbook so that we can test your work. - -There are a couple of ways that you can write, test and run your cookbook. - -* Write and test your cookbook locally using Test Kitchen via Vagrant + Virtual Box, or the cloud platform of your choice. - * Steps for this option are outlined [here](https://learn.chef.io/tutorials/local-development/) -* Develop directly on your RHEL-based virtual machine - * Write your cookbook in vim, nano or emacs, and run `chef-client` in `--local-mode` - * The ChefDK or Chef Client must be installed on the VM first - -## Suggested Resources - -* Use the [Chef Documentation](http://docs.chef.io) to identify and use resources that will help you model the desired state of your infrastructure. -* [Tomcat Installation Instructions](https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7) From ea682a7efac114b58e5980b241ff88890191fcb2 Mon Sep 17 00:00:00 2001 From: krattan406 <30876207+krattan406@users.noreply.github.com> Date: Wed, 14 Feb 2018 14:41:03 -0800 Subject: [PATCH 3/4] Update README.md --- 2_Tomcat/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_Tomcat/README.md b/2_Tomcat/README.md index 86d725f..37f7844 100644 --- a/2_Tomcat/README.md +++ b/2_Tomcat/README.md @@ -4,7 +4,7 @@ Assumption: You have a virtualbox with centOs7 and ChefDk 2.4.17 1) Login your virtualbox as super user from terminal. 2) Copy the cookbook directory to a base directory -3) change working directory to /cookbooks +3) Change working directory to /cookbooks 4) Run the tomcat installation cookbook: sudo chef-client --local-mode --runlist 'recipe[tomcat-chef]' From 705e50616c380fcde5e00181306e988531e67df4 Mon Sep 17 00:00:00 2001 From: krattan406 <30876207+krattan406@users.noreply.github.com> Date: Fri, 16 Feb 2018 18:25:18 -0800 Subject: [PATCH 4/4] Fixed issue with tomcat startup.sh permissions --- .../cookbooks/tomcat-chef/recipes/default.rb | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb b/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb index eb27480..0374cc1 100644 --- a/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb +++ b/2_Tomcat/cookbooks/tomcat-chef/recipes/default.rb @@ -1,3 +1,4 @@ + package 'java-1.7.0-openjdk-devel' group 'tomcat' @@ -15,8 +16,8 @@ end directory '/opt/tomcat' do - mode '0700' - group 'tomcat' + mode '0777' + group 'tomcat' end execute 'tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1' @@ -26,12 +27,17 @@ command "chgrp -R tomcat /opt/tomcat/conf && chmod g+r /opt/tomcat/conf/*" end - - directory '/opt/tomcat/conf' do - mode '0070' + mode '0777' end +execute "manage permissions bin" do + command "chgrp -R tomcat /opt/tomcat/bin && chmod g+r /opt/tomcat/bin" +end + +directory '/opt/tomcat/bin' do + mode '0777' +end execute 'chmod -R g+r /opt/tomcat/conf/*' @@ -53,9 +59,3 @@ - - - - - -