#!/usr/bin/python3

import amulet

d = amulet.Deployment(series='trusty')

d.add('benchmark-collector')
d.add('mysql')
d.add('mediawiki')

# Set mysql memory for running under the local provider
d.configure('mysql', {'dataset-size': '512M'})

d.relate('mediawiki:db', 'mysql:db')
d.relate('benchmark-collector:benchmark', 'mediawiki:juju-info')
d.relate('benchmark-collector:benchmark', 'mysql:juju-info')


try:
    d.setup(timeout=3600)
    d.sentry.wait()
except amulet.helpers.TimeoutError:
    amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")
except:
    raise

# Once everything is deployed, configure collection.
mediawiki = d.sentry['mediawiki'][0]
mysql = d.sentry['mysql'][0]

# TODO: Do one run to get the file and parse it.
# Need to get the db configuration from mediawiki
dbhost, code = mediawiki.run("cat /etc/mediawiki/LocalSettings.php \
        | grep wgDBserver | awk '{printf $3}'")
dbname, code = mediawiki.run("cat /etc/mediawiki/LocalSettings.php \
        | grep wgDBname | awk '{printf $3}'")
dbuser, code = mediawiki.run("cat /etc/mediawiki/LocalSettings.php \
        | grep wgDBuser | awk '{printf $3}'")
dbpasswd, code = mediawiki.run("cat /etc/mediawiki/LocalSettings.php \
        | grep wgDBpassword | awk '{printf $3}'")

# $wgDBserver         = "10.0.3.72";
# $wgDBname           = "mediawiki";
# $wgDBuser           = "thaedouyahlahdi";
# $wgDBpassword       = "laemaiteekaezae";

d.configure('benchmark-collector', {
    'plugins': 'cpu,memory,disk,dbi,apache',
    'extra-config': """
        <Plugin disk>
            Disk "sda"
            IgnoreSelected false
        </Plugin>
        <Plugin dbi>
            <Database "customers_db">
                Driver "mysql"
                DriverOption "host" "{0}"
                DriverOption "username" "{1}"
                DriverOption "password" "{2}"
                DriverOption "dbname" "{3}"
                SelectDB "{3}"
                Query "select count(*) from logging"
            </Database>
        </Plugin>
        <Plugin apache>
            <Instance "mediawiki">
                URL "http://localhost/server-status?auto"
                Server "apache"
            </Instance>
        </Plugin>
    """.format(mysql.info['public-address'], dbhost, dbuser, dbpasswd, dbname)
})

d.expose('benchmark-collector')


# # test we can access over http
# page = requests.get('http://{}'.format(unit.info['public-address']))
# page.raise_for_status()


# Now you can use d.sentry.unit[UNIT] to address each of the units and perform
# more in-depth steps. There are three test statuses: amulet.PASS, amulet.FAIL,
# and amulet.SKIP - these can be triggered with amulet.raise_status(). Each
# d.sentry.unit[] has the following methods:
# - .info - An array of the information of that unit from Juju
# - .file(PATH) - Get the details of a file on that unit
# - .file_contents(PATH) - Get plain text output of PATH file from that unit
# - .directory(PATH) - Get details of directory
# - .directory_contents(PATH) - List files and folders in PATH on that unit
# - .relation(relation, service:rel) - Get relation data from return service
