Skip to content

Commit

Permalink
Merge pull request #5 from hellofresh/feature/new_manage
Browse files Browse the repository at this point in the history
Feature/new manage
  • Loading branch information
ahelal committed Mar 3, 2016
2 parents 1146284 + 6a723a9 commit 3713579
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 65 deletions.
52 changes: 27 additions & 25 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
---
driver :
# TRAVIS: local
# NO TRAVIS: vagrant
name : <%= if ENV['TRAVIS'] then 'localhost' else 'vagrant' end %>
driver :
name : <%= if ENV['TRAVIS'] then 'localhost' else 'vagrant' end %>
box : "ubuntu/trusty64"
customize :
cpus : 2
memory : 2048

provisioner:
name : ansible_push
verbose : "vvvv"
ansible_config : "tests/ansible.cfg"
idempotency_test : True
# TRAVIS: True
# NO TRAVIS: False
sudo : <%= if ENV['TRAVIS'] then 'True' else 'False' end %>
name : ansible_push
verbose : "vvvv"
ansible_config : "test/ansible.cfg"
idempotency_test : True
diff : True
sudo : <%= if ENV['TRAVIS'] then 'True' else 'False' end %>
raw_arguments : <%= if ENV['TRAVIS'] then '-c local' else '' end %>
extra_vars : { 'kitchen_connection': '<%= if ENV["TRAVIS"] then "local" else "smart" end %>', 'kitchen_hosts': '<%= if ENV["TRAVIS"] then "localhost" else "all" end %>' }

platforms:
# travistravis
- name : <%= if ENV['TRAVIS'] then 'travis' else 'ubuntu-14.04' end %>
provisioner :
raw_arguments : <%= if ENV['TRAVIS'] then '-c local' else '' end %>
# Should be safily ignored by Travis since connection local
driver :
box : ubuntu/trusty64
customize :
cpus : 2
memory : 2048
- name : "v1"
provisioner :
ansible_playbook_bin : "$(ansible-version path v1)ansible-playbook"

- name : "v2"
provisioner :
ansible_playbook_bin : "$(ansible-version path v2)ansible-playbook"

suites:
- name : simple
provisioner :
playbook : "tests/application/simple.yml"
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }
- name : simple
provisioner :
playbook : "test/integration/simple/simple.yml"

- name : manage
provisioner :
playbook : "test/integration/manage/manage.yml"
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
language: python
python: "2.7"

env:
- KITCHEN_TEST="simple-v1"
- KITCHEN_TEST="simple-v2"
- KITCHEN_TEST="manage-v1"
- KITCHEN_TEST="manage-v2"

before_install:
# Make sure everything's up to date.
- sudo apt-get update -qq
- sudo apt-get install -qq python-apt python-pycurl git python-pip ruby ruby-dev build-essential autoconf
- gem install bundler
# Get ansible install
- curl -s https://raw.githubusercontent.com/AutomationWithAnsible/ansible-setup/master/example/travis-install -o /tmp/travis-ansible-setup.sh

install:
- sudo pip install ansible
- bash /tmp/travis-ansible-setup.sh
- bundle install

script:
- ansible --version
- export TRAVIS=True
- bundle install
- bundle exec kitchen test travis
- bundle exec kitchen test $KITCHEN_TEST

after_success:
- echo "Success"
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'


group :development do
gem 'test-kitchen'
gem 'kitchen-vagrant'
gem 'kitchen-ansiblepush'
gem 'kitchen-localhost'
gem 'net-ssh', '~> 2.0'
end
5 changes: 4 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
---
nginx_etc : "/etc/nginx/"
nginx_etc : "/etc/nginx"
nginx_default_root : "/usr/share/nginx/html" # Will only work for ubuntu
nginx_max_clients : 512
nginx_install_repo : "ubuntu" # Options: ["nginx", "ubuntu"]
nginx_apt_state : "present" # Options: ["latest", "present"]

# Should we control sites enabled
nginx_manage_sites : False

nginx_http_params :
sendfile : "on"
tcp_nopush : "on"
Expand Down
3 changes: 3 additions & 0 deletions tasks/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
notify:
- check restart nginx

- include: "manage_sites.yml"
when: nginx_manage_sites

- name: common | start the nginx service
service:
name=nginx
Expand Down
30 changes: 30 additions & 0 deletions tasks/manage_sites.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---

- name: manage sites | Get current services
shell: "ls *| sed 's/.conf//g'"
changed_when: False
args:
chdir: "{{ nginx_etc }}/sites-enabled/"
register: registered_nginx_sites

- name: manage sites | Check if any services are old
set_fact:
current_filenames: "{{ nginx_site | map(attribute='file_name') | list + ['default'] }}"

- name: manage sites | Check if any files needs to be removed
set_fact:
old_files: "{{ registered_nginx_sites.stdout_lines | difference(current_filenames) }}"

- name: manage sites | List of files need to removed
debug:
var=old_files

- name: manage sites | Ensure old services are removed
file:
path="{{ nginx_etc }}/sites-enabled/{{ item.0 }}{{ item.1 }}"
state="absent"
with_nested:
- "{{ old_files }}"
- [ "", ".conf"]
notify:
- reload nginx
6 changes: 6 additions & 0 deletions test/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[defaults]
roles_path=../:../../:/spec/
# V2
callback_whitelist = changes

ansible_managed=Ansible managed, Don't modify manually
67 changes: 67 additions & 0 deletions test/integration/manage/manage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
- name : Manage
hosts : "all"
gather_facts : True
become : True
connection : "{{ kitchen_connection | default('local') }}"
pre_tasks:
- name: update apt
apt: update_cache=yes cache_valid_time=3600

- name: One time tasks for testing
shell: "mkdir -p /etc/nginx/sites-enabled/ && touch /etc/nginx/sites-enabled/remove_me.conf && touch /tmp/creates"
args:
creates: /tmp/creates

- name: Create dir for content
file:
path="/nginx/{{ item }}"
state=directory
mode=0755
with_items:
- test2
- test3

- name: Create content for test
copy:
content="Hello {{ item }}"
dest="/nginx/{{ item }}/index.html"
mode=0644
with_items:
- test2
- test3

vars :
nginx_manage_sites : True
nginx_site :
- file_name : "test2"
blocks :
- type : "server"
index : "index.htm index.html"
server_name : "localhost"
listen : "1180"
## Use josn
location1 : {
name: /,
type: location,
try_files: "$uri $uri/ =404",
root: "/nginx/test2"
}

- file_name : "test3"
blocks :
- type : "server"
index : "index.htm index.html"
server_name : "localhost"
listen : "1280"
## Use josn
location1 : {
name: /,
type: location,
try_files: "$uri $uri/ =404",
root: "/nginx/test3"
}

roles :
- "ansible-nginx"

61 changes: 61 additions & 0 deletions test/integration/manage/serverspec/nginx_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'serverspec'

# Required by serverspec
set :backend, :exec

describe 'Package Service' do
describe package('nginx') do
it { should be_installed }
end

describe service('nginx') do
it { should be_enabled }
it { should be_running }
end

describe port(1180) do
it { should be_listening }
end

describe port(1280) do
it { should be_listening }
end

end

describe 'NGinx sites-enabled' do

describe file('/etc/nginx/sites-enabled/default') do
it { should be_symlink }
it { should be_linked_to '/etc/nginx/sites-available/default' }
end

describe file('/etc/nginx/sites-enabled/test2.conf') do
it { should be_symlink }
it { should be_linked_to '/etc/nginx/sites-available/test2.conf' }
end

describe file('/etc/nginx/sites-enabled/test3.conf') do
it { should be_symlink }
it { should be_linked_to '/etc/nginx/sites-available/test3.conf' }
end

describe file('/etc/nginx/sites-enabled/remove_me.conf') do
it { should_not exist }
end

end

describe 'NGinx http content' do

describe command "curl -s -L http://127.0.0.1:1180" do
its(:exit_status) { should eq 0 }
its(:stdout) { should match "Hello test2" }
end

describe command "curl -s -L http://127.0.0.1:1280" do
its(:exit_status) { should eq 0 }
its(:stdout) { should match "Hello test3" }
end

end
43 changes: 43 additions & 0 deletions test/integration/simple/serverspec/nginx_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'serverspec'

# Required by serverspec
set :backend, :exec

describe 'Package Service' do
describe package('nginx') do
it { should be_installed }
end

describe service('nginx') do
it { should be_enabled }
it { should be_running }
end

describe port(1080) do
it { should be_listening }
end

end

describe 'NGinx sites-enabled' do

describe file('/etc/nginx/sites-enabled/default') do
it { should be_symlink }
it { should be_linked_to '/etc/nginx/sites-available/default' }
end

describe file('/etc/nginx/sites-enabled/test.conf') do
it { should be_symlink }
it { should be_linked_to '/etc/nginx/sites-available/test.conf' }
end

end

describe 'NGinx http content' do

describe command "curl -s -L http://127.0.0.1:1080" do
its(:exit_status) { should eq 0 }
its(:stdout) { should match "Hello test1" }
end
end

44 changes: 44 additions & 0 deletions test/integration/simple/simple.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name : Simple
hosts : "all"
gather_facts : True
become : True
connection : "{{ kitchen_connection | default('local') }}"
vars :
nginx_site :
- file_name : "test"
blocks :
- type : "server"
index : "index.htm index.html"
server_name : "localhost"
listen : "1080"
## Use josn
location1 : {
name: /,
type: location,
try_files: "$uri $uri/ /index.html",
root: "/nginx/test1/"
}
pre_tasks:
- name: update apt
apt: update_cache=yes cache_valid_time=3600

- name: Create dir for content
file:
path="/nginx/{{ item }}"
state=directory
mode=0755
with_items:
- test1

- name: Create content for test
copy:
content="Hello {{ item }}"
dest="/nginx/{{ item }}/index.html"
mode=0644
with_items:
- test1

roles :
- "ansible-nginx"

2 changes: 0 additions & 2 deletions tests/ansible.cfg

This file was deleted.

Loading

0 comments on commit 3713579

Please sign in to comment.