From 97c4735129c297b66d7781a6c4cc1bdf281a4f79 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Wed, 12 Feb 2020 09:36:25 +1100
Subject: [PATCH] Move afsmon to mirror-update.opendev.org

This migrates the afsmon script from puppet deploying on
mirror-update.openstack.org to ansible deploying on
mirror-update.opendev.org.

There is nothing particularly special and this just a straight install
with some minor dependencies.  Since we have log publishing running on
the opendev.org server, we publish the update logs alongside the
others.

Change-Id: Ifa3b4d59f8d0fc23a4492e50348bab30766d5779
---
 .../manifests/mirror_update.pp                | 39 +-----------------
 playbooks/roles/afsmon/README.rst             |  8 ++++
 playbooks/roles/afsmon/files/afsmon.cfg       | 17 ++++++++
 playbooks/roles/afsmon/tasks/main.yaml        | 41 +++++++++++++++++++
 .../mirror-update/files/publish-mirror-logs   |  1 +
 playbooks/roles/mirror-update/tasks/main.yaml |  4 ++
 testinfra/test_mirror-update.py               |  7 ++++
 7 files changed, 80 insertions(+), 37 deletions(-)
 create mode 100644 playbooks/roles/afsmon/README.rst
 create mode 100644 playbooks/roles/afsmon/files/afsmon.cfg
 create mode 100644 playbooks/roles/afsmon/tasks/main.yaml

diff --git a/modules/openstack_project/manifests/mirror_update.pp b/modules/openstack_project/manifests/mirror_update.pp
index af23539ee0..9ec3393929 100644
--- a/modules/openstack_project/manifests/mirror_update.pp
+++ b/modules/openstack_project/manifests/mirror_update.pp
@@ -458,47 +458,12 @@ class openstack_project::mirror_update (
   }
 
   # AFS Monitoring
-  file { '/etc/afsmon.cfg':
-    ensure => present,
-    content => template('openstack_project/mirror-update/afsmon.cfg.erb'),
-    replace => true,
-  }
-
-  vcsrepo { '/opt/afsmon':
-    ensure   => latest,
-    provider => git,
-    revision => 'master',
-    source   => 'https://opendev.org/opendev/afsmon',
-  }
-
-  python::virtualenv { '/usr/afsmon-env':
-    ensure     => present,
-    owner      => 'root',
-    group      => 'root',
-    timeout    => 0,
-    # puppet-python 1.9.4 wants to guess we want "virtualenv-3", which
-    # we don't.  Fixed in later versions.
-    virtualenv => 'virtualenv',
-    version    => 3,
-  }
-
-  exec { 'install_afsmon' :
-    command     => '/usr/afsmon-env/bin/pip install --upgrade /opt/afsmon',
-    path        => '/usr/local/bin:/usr/bin:/bin',
-    refreshonly => true,
-    subscribe   => Vcsrepo['/opt/afsmon'],
-    require     => Python::Virtualenv['/usr/afsmon-env'],
-  }
-
+  # NOTE(ianw) 2020-02 : moved to mirror-update.opendev.org and ansible
   cron { 'afsmon':
     minute      => [0, 30],
     command     => '/usr/afsmon-env/bin/afsmon statsd >> /var/log/afsmon.log 2>&1',
     environment => 'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',
-    require     => [
-                    Python::Virtualenv['/usr/afsmon-env'],
-                    Exec['install_afsmon'],
-                    File['/etc/afsmon.cfg'],
-                   ],
+    ensure      => absent,
   }
 
 }
diff --git a/playbooks/roles/afsmon/README.rst b/playbooks/roles/afsmon/README.rst
new file mode 100644
index 0000000000..cd4be6904c
--- /dev/null
+++ b/playbooks/roles/afsmon/README.rst
@@ -0,0 +1,8 @@
+afsmon
+
+Install the afsmon tool and related bits and pieces for periodic
+monitoring of AFS volumes.  This role is really only intended to be
+run on the `mirror-update` host as we only need one instance of it
+running.
+
+**Role Variables**
diff --git a/playbooks/roles/afsmon/files/afsmon.cfg b/playbooks/roles/afsmon/files/afsmon.cfg
new file mode 100644
index 0000000000..524986f6df
--- /dev/null
+++ b/playbooks/roles/afsmon/files/afsmon.cfg
@@ -0,0 +1,17 @@
+[main]
+# Enable debugging output
+debug = True
+
+# If specified, all fileservers in this cell will be queried
+cell = openstack.org
+
+# You can specify a specific list of fileservers.  This is appended to
+# the cell fileservers if present, otherwise is the canonical list
+#fileservers = afs01.dfw.openstack.org
+#         fileserver02.afs.company.com
+#         fileserver03.afs.company.com
+
+# Options for remote statsd host if required
+[statsd]
+host = graphite.opendev.org
+port = 8125
diff --git a/playbooks/roles/afsmon/tasks/main.yaml b/playbooks/roles/afsmon/tasks/main.yaml
new file mode 100644
index 0000000000..b8d4a8745a
--- /dev/null
+++ b/playbooks/roles/afsmon/tasks/main.yaml
@@ -0,0 +1,41 @@
+- name: Ensure afsmon log directory
+  file:
+    path: '/var/log/afsmon'
+    state: directory
+    owner: root
+    group: root
+    mode: 0755
+
+- name: Install dependency packages
+  package:
+    name:
+      - python3-pbr
+      - python3-statsd
+      - python3-prettytable
+    state: present
+
+- name: Install afsmon
+  pip:
+    name: afsmon
+    state: present
+    executable: pip3
+
+- name: Install configuration file
+  copy:
+    src: afsmon.cfg
+    dest: '/etc/'
+    mode: '0644'
+
+- name: Install afsmon cron job
+  cron:
+    name: 'afsmon'
+    state: present
+    job: '/usr/local/bin/afsmon statsd >> /var/log/afsmon/afsmon.log 2>&1'
+    hour: '*'
+    minute: '30'
+
+- name: Install logrotate rules
+  include_role:
+    name: logrotate
+  vars:
+    logrotate_file_name: '/var/log/afsmon/afsmon.log'
\ No newline at end of file
diff --git a/playbooks/roles/mirror-update/files/publish-mirror-logs b/playbooks/roles/mirror-update/files/publish-mirror-logs
index adbfe5c4f2..d25a1f8ac2 100755
--- a/playbooks/roles/mirror-update/files/publish-mirror-logs
+++ b/playbooks/roles/mirror-update/files/publish-mirror-logs
@@ -20,6 +20,7 @@ RSYNC="rsync -avz --no-perms --no-owner --no-group"
 
 $K5START $RSYNC /var/log/rsync-mirrors $DEST
 $K5START $RSYNC /var/log/afs-release $DEST
+$K5START $RSYNC /var/log/afsmon $DEST
 
 # NOTE(ianw) : r/w volume as this is infrequently accessed; thus no
 # replications and no "vos release" etc required.
diff --git a/playbooks/roles/mirror-update/tasks/main.yaml b/playbooks/roles/mirror-update/tasks/main.yaml
index f95df72b42..59e027c7bd 100644
--- a/playbooks/roles/mirror-update/tasks/main.yaml
+++ b/playbooks/roles/mirror-update/tasks/main.yaml
@@ -37,4 +37,8 @@
   include_role:
     name: afs-release
 
+- name: Setup AFS monitoring script
+  include_role:
+    name: afsmon
+
 # TODO: reprepro and other mirror components
diff --git a/testinfra/test_mirror-update.py b/testinfra/test_mirror-update.py
index 4fd5740bbe..5fa315a5cf 100644
--- a/testinfra/test_mirror-update.py
+++ b/testinfra/test_mirror-update.py
@@ -63,3 +63,10 @@ def test_afs_release_script_run(host):
     cmd = host.run('/opt/afs-release/release-volumes.py '
                    '--debug --skip-release')
     assert cmd.succeeded
+
+def test_afsmon_installed(host):
+    f = host.file('/usr/local/bin/afsmon')
+    assert f.exists
+
+    f = host.file('/etc/afsmon.cfg')
+    assert f.exists