Using ApMan

This notebook demonstrates how to use apman from code. Typically, the apman_run command line script is used to execute packages - for an example of this, please refer to 'CLI Example.ipynb'

Import apman, and set up logging to see outputs


In [1]:
from apman import packagemanager as pm
import logging

In [2]:
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

Show an example apman config file, and script


In [3]:
package_config_path = '../tests/test_configs/error/config'

In [4]:
with open(package_config_path, 'r') as fin:
    print fin.read()


{
    "id":"test",
    "description": "test",
    "timeout": 3,
    "command": "python script.py"
}

Load the package config as an apman package object


In [5]:
package = pm.PackageManager('../tests/test_configs/error/config')

Execute the package


In [6]:
package.run_package()


DEBUG:root:Logging package start to DB.
ERROR:root:Cannot log package start to ApMan DB Log.
DEBUG:root:Executing package command.
DEBUG:root:Logging package end to DB.
ERROR:root:Cannot log package end to ApMan DB Log.

Get the package results, and print them


In [7]:
summary, detail = package.get_notification_text()

In [8]:
print(summary)


Package (test) failed (runtime error)

In [9]:
print(detail)


ID: test
Timeout: 3
Start: 2015-10-29 17:37:40.334000
End: 2015-10-29 17:37:40.459000
Result: failed (runtime error)
Stdout: 
Stderr: Traceback (most recent call last):
  File "script.py", line 3, in <module>
    x = 1/0
ZeroDivisionError: integer division or modulo by zero

Send a notification email to the apman admins and package owners


In [10]:
package.send_notification_email()


DEBUG:root:Sending notification emails.
INFO:root:Notification emails sent.