Skip to content

Commit 3713579

Browse files
committed
Merge pull request #5 from hellofresh/feature/new_manage
Feature/new manage
2 parents 1146284 + 6a723a9 commit 3713579

File tree

13 files changed

+297
-65
lines changed

13 files changed

+297
-65
lines changed

.kitchen.yml

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
---
2-
driver :
3-
# TRAVIS: local
4-
# NO TRAVIS: vagrant
5-
name : <%= if ENV['TRAVIS'] then 'localhost' else 'vagrant' end %>
2+
driver :
3+
name : <%= if ENV['TRAVIS'] then 'localhost' else 'vagrant' end %>
4+
box : "ubuntu/trusty64"
5+
customize :
6+
cpus : 2
7+
memory : 2048
68

79
provisioner:
8-
name : ansible_push
9-
verbose : "vvvv"
10-
ansible_config : "tests/ansible.cfg"
11-
idempotency_test : True
12-
# TRAVIS: True
13-
# NO TRAVIS: False
14-
sudo : <%= if ENV['TRAVIS'] then 'True' else 'False' end %>
10+
name : ansible_push
11+
verbose : "vvvv"
12+
ansible_config : "test/ansible.cfg"
13+
idempotency_test : True
14+
diff : True
15+
sudo : <%= if ENV['TRAVIS'] then 'True' else 'False' end %>
16+
raw_arguments : <%= if ENV['TRAVIS'] then '-c local' else '' end %>
17+
extra_vars : { 'kitchen_connection': '<%= if ENV["TRAVIS"] then "local" else "smart" end %>', 'kitchen_hosts': '<%= if ENV["TRAVIS"] then "localhost" else "all" end %>' }
1518

1619
platforms:
17-
# travistravis
18-
- name : <%= if ENV['TRAVIS'] then 'travis' else 'ubuntu-14.04' end %>
19-
provisioner :
20-
raw_arguments : <%= if ENV['TRAVIS'] then '-c local' else '' end %>
21-
# Should be safily ignored by Travis since connection local
22-
driver :
23-
box : ubuntu/trusty64
24-
customize :
25-
cpus : 2
26-
memory : 2048
20+
- name : "v1"
21+
provisioner :
22+
ansible_playbook_bin : "$(ansible-version path v1)ansible-playbook"
23+
24+
- name : "v2"
25+
provisioner :
26+
ansible_playbook_bin : "$(ansible-version path v2)ansible-playbook"
2727

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

33+
- name : manage
34+
provisioner :
35+
playbook : "test/integration/manage/manage.yml"

.travis.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22
language: python
33
python: "2.7"
44

5+
env:
6+
- KITCHEN_TEST="simple-v1"
7+
- KITCHEN_TEST="simple-v2"
8+
- KITCHEN_TEST="manage-v1"
9+
- KITCHEN_TEST="manage-v2"
10+
511
before_install:
612
# Make sure everything's up to date.
713
- sudo apt-get update -qq
814
- sudo apt-get install -qq python-apt python-pycurl git python-pip ruby ruby-dev build-essential autoconf
915
- gem install bundler
16+
# Get ansible install
17+
- curl -s https://raw.githubusercontent.com/AutomationWithAnsible/ansible-setup/master/example/travis-install -o /tmp/travis-ansible-setup.sh
1018

1119
install:
12-
- sudo pip install ansible
20+
- bash /tmp/travis-ansible-setup.sh
21+
- bundle install
1322

1423
script:
1524
- ansible --version
1625
- export TRAVIS=True
17-
- bundle install
18-
- bundle exec kitchen test travis
26+
- bundle exec kitchen test $KITCHEN_TEST
1927

2028
after_success:
2129
- echo "Success"

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
source 'https://rubygems.org'
22

3-
43
group :development do
54
gem 'test-kitchen'
65
gem 'kitchen-vagrant'
76
gem 'kitchen-ansiblepush'
87
gem 'kitchen-localhost'
8+
gem 'net-ssh', '~> 2.0'
99
end

defaults/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
---
2-
nginx_etc : "/etc/nginx/"
2+
nginx_etc : "/etc/nginx"
33
nginx_default_root : "/usr/share/nginx/html" # Will only work for ubuntu
44
nginx_max_clients : 512
55
nginx_install_repo : "ubuntu" # Options: ["nginx", "ubuntu"]
66
nginx_apt_state : "present" # Options: ["latest", "present"]
77

8+
# Should we control sites enabled
9+
nginx_manage_sites : False
10+
811
nginx_http_params :
912
sendfile : "on"
1013
tcp_nopush : "on"

tasks/common.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
notify:
5555
- check restart nginx
5656

57+
- include: "manage_sites.yml"
58+
when: nginx_manage_sites
59+
5760
- name: common | start the nginx service
5861
service:
5962
name=nginx

tasks/manage_sites.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
3+
- name: manage sites | Get current services
4+
shell: "ls *| sed 's/.conf//g'"
5+
changed_when: False
6+
args:
7+
chdir: "{{ nginx_etc }}/sites-enabled/"
8+
register: registered_nginx_sites
9+
10+
- name: manage sites | Check if any services are old
11+
set_fact:
12+
current_filenames: "{{ nginx_site | map(attribute='file_name') | list + ['default'] }}"
13+
14+
- name: manage sites | Check if any files needs to be removed
15+
set_fact:
16+
old_files: "{{ registered_nginx_sites.stdout_lines | difference(current_filenames) }}"
17+
18+
- name: manage sites | List of files need to removed
19+
debug:
20+
var=old_files
21+
22+
- name: manage sites | Ensure old services are removed
23+
file:
24+
path="{{ nginx_etc }}/sites-enabled/{{ item.0 }}{{ item.1 }}"
25+
state="absent"
26+
with_nested:
27+
- "{{ old_files }}"
28+
- [ "", ".conf"]
29+
notify:
30+
- reload nginx

test/ansible.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[defaults]
2+
roles_path=../:../../:/spec/
3+
# V2
4+
callback_whitelist = changes
5+
6+
ansible_managed=Ansible managed, Don't modify manually

test/integration/manage/manage.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
- name : Manage
3+
hosts : "all"
4+
gather_facts : True
5+
become : True
6+
connection : "{{ kitchen_connection | default('local') }}"
7+
pre_tasks:
8+
- name: update apt
9+
apt: update_cache=yes cache_valid_time=3600
10+
11+
- name: One time tasks for testing
12+
shell: "mkdir -p /etc/nginx/sites-enabled/ && touch /etc/nginx/sites-enabled/remove_me.conf && touch /tmp/creates"
13+
args:
14+
creates: /tmp/creates
15+
16+
- name: Create dir for content
17+
file:
18+
path="/nginx/{{ item }}"
19+
state=directory
20+
mode=0755
21+
with_items:
22+
- test2
23+
- test3
24+
25+
- name: Create content for test
26+
copy:
27+
content="Hello {{ item }}"
28+
dest="/nginx/{{ item }}/index.html"
29+
mode=0644
30+
with_items:
31+
- test2
32+
- test3
33+
34+
vars :
35+
nginx_manage_sites : True
36+
nginx_site :
37+
- file_name : "test2"
38+
blocks :
39+
- type : "server"
40+
index : "index.htm index.html"
41+
server_name : "localhost"
42+
listen : "1180"
43+
## Use josn
44+
location1 : {
45+
name: /,
46+
type: location,
47+
try_files: "$uri $uri/ =404",
48+
root: "/nginx/test2"
49+
}
50+
51+
- file_name : "test3"
52+
blocks :
53+
- type : "server"
54+
index : "index.htm index.html"
55+
server_name : "localhost"
56+
listen : "1280"
57+
## Use josn
58+
location1 : {
59+
name: /,
60+
type: location,
61+
try_files: "$uri $uri/ =404",
62+
root: "/nginx/test3"
63+
}
64+
65+
roles :
66+
- "ansible-nginx"
67+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'serverspec'
2+
3+
# Required by serverspec
4+
set :backend, :exec
5+
6+
describe 'Package Service' do
7+
describe package('nginx') do
8+
it { should be_installed }
9+
end
10+
11+
describe service('nginx') do
12+
it { should be_enabled }
13+
it { should be_running }
14+
end
15+
16+
describe port(1180) do
17+
it { should be_listening }
18+
end
19+
20+
describe port(1280) do
21+
it { should be_listening }
22+
end
23+
24+
end
25+
26+
describe 'NGinx sites-enabled' do
27+
28+
describe file('/etc/nginx/sites-enabled/default') do
29+
it { should be_symlink }
30+
it { should be_linked_to '/etc/nginx/sites-available/default' }
31+
end
32+
33+
describe file('/etc/nginx/sites-enabled/test2.conf') do
34+
it { should be_symlink }
35+
it { should be_linked_to '/etc/nginx/sites-available/test2.conf' }
36+
end
37+
38+
describe file('/etc/nginx/sites-enabled/test3.conf') do
39+
it { should be_symlink }
40+
it { should be_linked_to '/etc/nginx/sites-available/test3.conf' }
41+
end
42+
43+
describe file('/etc/nginx/sites-enabled/remove_me.conf') do
44+
it { should_not exist }
45+
end
46+
47+
end
48+
49+
describe 'NGinx http content' do
50+
51+
describe command "curl -s -L http://127.0.0.1:1180" do
52+
its(:exit_status) { should eq 0 }
53+
its(:stdout) { should match "Hello test2" }
54+
end
55+
56+
describe command "curl -s -L http://127.0.0.1:1280" do
57+
its(:exit_status) { should eq 0 }
58+
its(:stdout) { should match "Hello test3" }
59+
end
60+
61+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'serverspec'
2+
3+
# Required by serverspec
4+
set :backend, :exec
5+
6+
describe 'Package Service' do
7+
describe package('nginx') do
8+
it { should be_installed }
9+
end
10+
11+
describe service('nginx') do
12+
it { should be_enabled }
13+
it { should be_running }
14+
end
15+
16+
describe port(1080) do
17+
it { should be_listening }
18+
end
19+
20+
end
21+
22+
describe 'NGinx sites-enabled' do
23+
24+
describe file('/etc/nginx/sites-enabled/default') do
25+
it { should be_symlink }
26+
it { should be_linked_to '/etc/nginx/sites-available/default' }
27+
end
28+
29+
describe file('/etc/nginx/sites-enabled/test.conf') do
30+
it { should be_symlink }
31+
it { should be_linked_to '/etc/nginx/sites-available/test.conf' }
32+
end
33+
34+
end
35+
36+
describe 'NGinx http content' do
37+
38+
describe command "curl -s -L http://127.0.0.1:1080" do
39+
its(:exit_status) { should eq 0 }
40+
its(:stdout) { should match "Hello test1" }
41+
end
42+
end
43+

0 commit comments

Comments
 (0)