From 5024bfa3ea19cbcd0b89886b67e0adfde0945c12 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Thu, 7 Feb 2019 22:27:01 +0100 Subject: [PATCH] fix --- .kitchen.yml | 5 ++ files/sensu-agent.service | 16 ++++ tasks/agent.yml | 4 + tasks/build_agent.yml | 45 ++++++++++- tasks/main.yml | 1 + test/integration/build/default.yml | 18 +++++ .../build/serverspec/default_spec.rb | 78 +++++++++++++++++++ 7 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 files/sensu-agent.service create mode 100644 test/integration/build/default.yml create mode 100644 test/integration/build/serverspec/default_spec.rb diff --git a/.kitchen.yml b/.kitchen.yml index 7a16e14..e07f844 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -25,3 +25,8 @@ platforms: suites: - name: default + - name: build + provisioner: + extra_vars: + sensu_build: yes + sensu_build_go_version: 1.11 diff --git a/files/sensu-agent.service b/files/sensu-agent.service new file mode 100644 index 0000000..ea4d946 --- /dev/null +++ b/files/sensu-agent.service @@ -0,0 +1,16 @@ +[Unit] +Description=The Sensu Agent process. +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +User=sensu +Group=sensu +LimitNOFILE=65535 +ExecStart=/usr/local/bin/sensu-agent start +Restart=always +WorkingDirectory=/ + +[Install] +WantedBy=multi-user.target diff --git a/tasks/agent.yml b/tasks/agent.yml index 29aad07..d6a81fd 100644 --- a/tasks/agent.yml +++ b/tasks/agent.yml @@ -12,8 +12,12 @@ retries: 3 delay: 1 until: result is success + when: not sensu_build tags: sensu +- import_tasks: build_agent.yml + when: sensu_build + - name: copy agent configuration file copy: content: '{{ sensu_agent_full_config|to_nice_yaml }}' diff --git a/tasks/build_agent.yml b/tasks/build_agent.yml index 40b6e52..9d4b2ed 100644 --- a/tasks/build_agent.yml +++ b/tasks/build_agent.yml @@ -15,7 +15,7 @@ repo: https://github.com/sensu/sensu-go/ version: '{{ sensu_build_version }}' dest: /usr/local/src/sensu-go - when: git.changed or not bin.stat.exists + register: git tags: sensu - name: install dependencies @@ -41,7 +41,7 @@ - build - '-o' - /usr/local/bin/sensu-agent - - main.go + - ./cmd/sensu-agent/ environment: GOPATH: /usr/local/src/go GOBIN: /usr/local/src/go/bin @@ -55,3 +55,44 @@ group: root mode: 0755 tags: go + +- name: create configuration folder + file: + path: /etc/sensu + owner: root + group: root + mode: 0755 + state: directory + tags: go + +- name: create sensu group + group: + name: sensu + gid: 998 + tags: go + +- name: create sensu user + user: + name: sensu + group: sensu + uid: 996 + home: /opt/sensu + shell: /bin/false + comment: Sensu Monitoring Framework + tags: go + +- name: copy systemd service + copy: + src: sensu-agent.service + dest: /etc/systemd/system/sensu-agent.service + owner: root + group: root + mode: 0644 + register: systemd + tags: go + +- name: reload systemd + systemd: + daemon_reload: yes + when: systemd.changed + tags: go diff --git a/tasks/main.yml b/tasks/main.yml index ec35f0e..408c39e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,5 +1,6 @@ --- - import_tasks: repository.yml + when: not sensu_build - import_tasks: agent.yml when: sensu_agent diff --git a/test/integration/build/default.yml b/test/integration/build/default.yml new file mode 100644 index 0000000..05309f3 --- /dev/null +++ b/test/integration/build/default.yml @@ -0,0 +1,18 @@ +--- +- hosts: default + connection: local + vars: + sensu_agent_labels: + test: coucou + hello: + warning: 30 + critical: 50 + + roles: + - ansible-role-sensu + + pre_tasks: + - name: add repo backports + apt_repository: + repo: 'deb http://httpredir.debian.org/debian {{ansible_distribution_release}}-backports main' + state: present diff --git a/test/integration/build/serverspec/default_spec.rb b/test/integration/build/serverspec/default_spec.rb new file mode 100644 index 0000000..805481c --- /dev/null +++ b/test/integration/build/serverspec/default_spec.rb @@ -0,0 +1,78 @@ +require 'serverspec' + +set :backend, :exec + +puts +puts '================================' +puts %x(ansible --version) +puts '================================' + +%w[ + sensu-go-agent + sensu-go-cli + sensu-go-backend + monitoring-plugins-basic +].each do |package| + describe package(package) do + it { should be_installed } + end +end + +%w[ + sensu-agent + sensu-backend +].each do |service| + describe service(service) do + it { should be_enabled } + it { should be_running } + it { should be_running.under('systemd') } + end +end + +[3000, 8080, 8081].each do |port| + describe port(port) do + it { should be_listening.with('tcp6') } + end +end + +describe command( + 'sensuctl configure -n --password "P@ssw0rd!" ' \ + '--url http://127.0.0.1:8080 --username admin --format tabular' +) do + its(:exit_status) { should eq 0 } +end + +describe command('sensuctl namespace list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match 'supernamespace' } +end + +describe command('sensuctl user list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/johndoe.*\s+devops,users\s+.*true/) } +end + +describe command('sensuctl asset list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) } +end + +describe command('sensuctl handler list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/mail.*\s+pipe\s+.*echo test \| mail -s coucou\s+/) } +end + +describe command('sensuctl filter list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/state_changed.*\s+allow\s+event\.check\.occurrences == 1/) } +end + +describe command('sensuctl cluster-role list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/view.*\s+1/) } +end + +describe command('sensuctl check list') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/ping.*\s+ping -c 1 127.0.0.1\s+60\s+.*\s+linux\s+/) } +end