1#!/usr/bin/env python3
  2
  3"""Implementaion of role command."""
  4
  5from datetime import datetime
  6from datetime import timedelta
  7
  8from chart.common.args import ArgumentParser
  9from chart.users.role import RoleManager
 10from chart.events.event import Event
 11from chart.backend.activity import Activity
 12from chart.backend.job import JobStatus
 13from chart.backend.job import Job
 14from chart.project import settings
 15
 16def main():
 17    """Command line entry point."""
 18    parser = ArgumentParser()
 19    parser.add_argument('--list-users',
 20                        action='store_true')
 21    parser.add_argument('--list-roles', '--list', '-l',
 22                        action='store_true')
 23    # parser.add_argument('--show',
 24                        # help=('Show information on a user or role. Pass in username, name '
 25                              # 'or email address'))
 26    parser.add_argument('--test-event',
 27                        help='Specify class name of event to simulate')
 28    parser.add_argument('--test-report',
 29                        help='Specify activity name of report to test notifications')
 30    parser.add_argument('--test-job',
 31                        metavar='ACTIVITY',
 32                        help=('Simulate a job completing with a different state to the previous '
 33                              'time it ran'))
 34    parser.add_argument('--send-daemon',
 35                        help='Send a notification for a daemon change in state')
 36    parser.add_argument('--sid', '-s',
 37                        help='Spacecraft to simulate for --test-* options',
 38                        type=ArgumentParser.sid)
 39    parser.add_argument('--state',
 40                        help='New state when simulating job or daemon changes')
 41    parser.add_argument('--old-state',
 42                        help='Old state when simulating job or daemon changes')
 43    parser.add_argument('--job-id',
 44                        type=int,
 45                        help='Job ID of simulate job')
 46    parser.add_argument('--sendmail',
 47                        action='store_true',
 48                        help='Send emails')
 49    parser.add_argument('--sendmail-filter',
 50                       help=('when sending emails only send them if the email address matches this '
 51                             'wildcard match'))
 52    parser.add_argument('--log',
 53                        action='store_true',
 54                        help='For the test options, record the event to user log file')
 55    parser.add_argument('--start',
 56                       type=ArgumentParser.datetime,
 57                       help='For event and report test specify start time')
 58    parser.add_argument('--stop',
 59                       type=ArgumentParser.datetime,
 60                       help='For event and report test specify stop time')
 61    parser.add_argument('--cutoff_duration',
 62                        type=timedelta,
 63                        help=('Threshold for age of event, report or job before which alerts '
 64                              'will not be send'))
 65    args = parser.parse_args()
 66
 67    role_manager = RoleManager.instance()
 68
 69    if args.list_users:
 70        role_manager.list_users()
 71        parser.exit()
 72
 73    if args.list_roles:
 74        role_manager.list_roles()
 75        parser.exit()
 76
 77    if args.cutoff_duration:
 78        settings.EMAIL_CUTOFF = args.cutoff_duration
 79
 80    if args.test_event:
 81        ev = Event(args.test_event,
 82                   sid=args.sid,
 83                   start_time=datetime.utcnow() if args.start is None else args.start,
 84                   # instance_properties={'param1': 'one', 'param2': 'two'},
 85                   )
 86        role_manager.handle_event(ev,
 87                                  sendmails=args.sendmail,
 88                                  log=args.log,
 89                                  sendmail_filter=args.sendmail_filter)
 90        parser.exit()
 91
 92    if args.test_report:
 93        role_manager.handle_report(activity=Activity(args.test_report),
 94                                   sid=args.sid,
 95                                   start=datetime.utcnow() if args.start is None else args.start,
 96                                   stop=args.stop,
 97                                   sendmails=args.sendmail,
 98                                   log=args.log,
 99                                   sendmail_filter=args.sendmail_filter)
100        parser.exit()
101
102    if args.test_job:
103        if args.state is None:
104            parser.error('No --state set')
105
106        state = JobStatus[args.state.upper()]
107        if args.old_state:
108            old_state = JobStatus[args.old_state.upper()]
109            role_manager.handle_job_state_change(
110                activity=Activity(args.test_job),
111                sid=args.sid,
112                start=datetime.utcnow() if args.start is None else args.start,
113                stop=args.stop,
114                state=state,
115                old_state=old_state,
116                job_id=args.job_id,
117                sendmails=args.sendmail,
118                log=args.log,
119                sendmail_filter=args.sendmail_filter)
120
121        else:
122            role_manager.handle_job_state(
123                Job(sid=args.sid,
124                    sensing_start=datetime.utcnow() if args.start is None else args.start,
125                    sensing_stop=args.stop,
126                    activity=Activity(args.test_job),
127                    status=state),
128                sendmails=args.sendmail,
129                log=args.log,
130                sendmail_filter=args.sendmail_filter)
131
132        parser.exit()
133
134    if args.send_daemon:
135        if args.state is None:
136            parser.error('No new --state')
137
138        if args.old_state is None:
139            parser.error('No --old-state')
140
141        role_manager.handle_daemon_event(
142            daemon=args.send_daemon,
143            state=args.state,
144            old_state=args.old_state,
145            sendmails=args.sendmail,
146            sendmail_filter=args.sendmail_filter)
147
148        parser.exit()
149
150    parser.exit('No actions specified')
151
152if __name__ == '__main__':
153    main()