#!/usr/bin/python

# Copyright (C) 2015 Brent Baude <bbaude@redhat.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

''' oscap docker command '''

import argparse
from oscap_docker_python.oscap_docker_util import OscapScan
import docker
import sys
from requests import exceptions


class OscapDocker(object):
    ''' Generic class to call the scans '''
    def __init__(self):
        pass

    def set_args(self, args, unknown):
        '''
        Sets arguments for argparse into the oscapDocker class
        '''
        self.args = args
        self.unknown_args = unknown

    def cve_scan(self):
        ''' Wrapper function for container/image scanning '''
        OS = OscapScan()
        result = OS.scan_cve(self.args.scan_target, self.unknown_args)
        if result is not None:
            print(result)

    def scan(self):
        ''' Wrapper functiopn to scan with openscap'''
        OS = OscapScan()
        result = OS.scan(self.args.scan_target, self.unknown_args)
        if result is not None:
            print(result)


def ping_docker():
    ''' Simple check if the docker daemon is running '''
    # Class docker.Client was renamed to docker.APIClient in
    # python-docker-py 2.0.0.
    try:
        client = docker.APIClient()
    except AttributeError:
        client = docker.Client()
    client.ping()

if __name__ == '__main__':

    OD = OscapDocker()

    parser = argparse.ArgumentParser(description='oscap docker',
                                     epilog='See `man oscap` to learn \
                                     more about OSCAP-ARGUMENTS')
    subparser = parser.add_subparsers(help="commands")

    # Scan CVEs in image
    image_cve = subparser.add_parser('image-cve', help='Scan a docker image \
                                    for known vulnerabilities.')
    image_cve.set_defaults(func=OD.cve_scan)
    image_cve.add_argument('scan_target', help='Container or image to scan')

    # Scan an Image
    image = subparser.add_parser('image', help='Scan a docker image')
    image.add_argument('scan_target',
                       help='Container or image to scan')

    image.set_defaults(func=OD.scan)
    # Scan a container
    container = subparser.add_parser('container', help='Scan a running docker\
                                      container of given name.')
    container.add_argument('scan_target',
                           help='Container or image to scan')
    container.set_defaults(func=OD.scan)

    # Scan CVEs in container
    container_cve = subparser.add_parser('container-cve', help='Scan a \
                                         running container for known \
                                         vulnerabilities.')

    container_cve.set_defaults(func=OD.cve_scan)
    container_cve.add_argument('scan_target',
                               help='Container or image to scan')

    args, unknown = parser.parse_known_args()

    if "func" not in args:
        parser.print_help()
        sys.exit(2)

    try:
        ping_docker()

    except exceptions.ConnectionError:
        print("The docker daemon does not appear to be running")
        sys.exit(1)

    OD.set_args(args, unknown)
    args.func()
