def restart_nagios():
    result = run('sudo /etc/nagios/check_config')
    if result.return_code != 0:
        utils.failure('Nagios config check failed, removing nagios config')
        run('rm -rf %(nagios_master_config_dir)s/*%(environment)s*' % env)
    else:
        run('sudo /etc/init.d/nagios3 restart')
def create_final_image(state):
    """Create docker image from container"""

    puts(blue("Building image"))

    # First need to cleanup container before we stop and commit it.
    # We execute most of the commands via ssh, until we actually remove ssh
    # itself and forcefully remove unnecessary system-level folders
    execute(cleanup_container)
    cont = state.container
    cont.exec_run('yum --assume-yes remove fipscheck fipscheck-lib openssh-server')
    cont.exec_run('rm -rf /var/log')
    cont.exec_run('rm -rf /var/lib/yum')

    conf = {'Cmd': ["/usr/bin/su", "-", "APP", "-c", "/home/APP/APP_rt/bin/ngamsServer -cfg /home/APP/APP/cfg/ngamsServer.conf -autoOnline -force -v 4"]}
    image_repo = docker_image_repository()

    try:
        cont.stop()
        cont.commit(repository=image_repo, tag='latest', conf=conf)
        success("Created Docker image %s:latest" % (image_repo,))
    except Exception as e:
        failure("Failed to build final image: %s" % (str(e)))
        raise
    finally:
        # Cleanup the docker environment from all our temporary stuff
        cont.remove()
def setup_container():
    """Create and prepare a docker container and let Fabric point at it"""

    from docker.client import DockerClient

    image = 'centos:centos7'
    container_name = 'APP_installation_target'
    cli = DockerClient.from_env(version='auto', timeout=10)

    # Create and start a container using the newly created stage1 image
    cont = cli.containers.run(image=image, name=container_name, remove=False, detach=True, tty=True)
    success("Created container %s from %s" % (container_name, image))

    # Find out container IP, prepare container for APP installation
    try:
        host_ip = cli.api.inspect_container(cont.id)['NetworkSettings']['IPAddress']

        info("Updating and installing OpenSSH server in container")
        cont.exec_run('yum -y update')
        cont.exec_run('yum -y install openssh-server sudo')
        cont.exec_run('yum clean all')

        info('Configuring OpenSSH to allow connections to container')
        add_public_ssh_key(cont)
        cont.exec_run('sed -i "s/#PermitRootLogin yes/PermitRootLogin yes/" /etc/ssh/sshd_config')
        cont.exec_run('sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config')
        cont.exec_run('ssh-keygen -A')
        cont.exec_run('chown root.root /root/.ssh/authorized_keys')
        cont.exec_run('chmod 600 /root/.ssh/authorized_keys')
        cont.exec_run('chmod 700 /root/.ssh')

        info('Starting OpenSSH deamon in container')
        cont.exec_run('/usr/sbin/sshd -D', detach=True)
    except:
        failure("Error while preparing container for APP installation, cleaning up...")
        cont.stop()
        cont.remove()
        raise

    # From now on we connect to root@host_ip using our SSH key
    env.hosts = host_ip
    env.user = '******'
    if 'key_filename' not in env and 'key' not in env:
        env.key_filename = os.path.expanduser("~/.ssh/id_rsa")

    # Make sure we can connect via SSH to the newly started container
    # We disable the known hosts check since docker containers created at
    # different times might end up having the same IP assigned to them, and the
    # ssh known hosts check will fail
    with settings(disable_known_hosts=True):
        execute(check_ssh)

    success('Container successfully setup! APP installation will start now')
    return DockerContainerState(cli, cont)
Exemple #4
0
    def processInput(self, testFilePath):

        try:
            self.socket = utils.SecuredSocket(self.host, self.port)
        except:
            print utils.failure("FAIL: cannot connect to ada_server socket")
            raise

        input = utils.readFile(testFilePath)
        self.socket.write(input)
        self.lastSocketOutput = self.socket.read()

        return True
Exemple #5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--filter", nargs='?', default=".*", const=".*",
                        help="filter module to run, support regex, example: module[12]/.*")
    # parser.add_argument("--dbstring", nargs='?', default=DB_CON_STRING, const=DB_CON_STRING,
    #                    help="database connection string")
    # parser.add_argument("--ports", nargs='+', default=PORT_DEFAULT_ARR,
    #                    help="set available hosts for option *.input[host abbr.], *.output[host abbr.]. "
    #                         "\nformat: [host abbr.]:[host port]"
    #                         "\nexample: ui:1234 ada:2345")
    parser.add_argument("--testsdir", nargs='?', default='./beeeon_tests',
                        help="set directory of tests")
    parser.add_argument("--segments", nargs='+', default=['ada', 'ui'],
                        help="register some segments(ui_server, AdaApp, ...), probably you want use this with --filter")
    args = parser.parse_args()

    # check filter regex validity
    try:
        re.compile(args.filter)
    except re.error:
        print "regex is not valid"
        sys.stdout.flush()
        return ERROR_PARAMS

    print 'START'
    sys.stdout.flush()

    os.chdir(args.testsdir)

    beeeOnTestRunner = beeeon_test_runner.testRunner(args.filter)
    beeeOnTestRunner.setTestSegments(args.segments)

    try:
        if (("ui" in args.segments) or ("ada" in args.segments)):
            beeeOnTestRunner.setBeeeOnSegmentTestfw(base_test_handler.BaseTestHandler())
        if("ui" in args.segments):
            beeeOnTestRunner.setBeeeOnSegmentTestfw(ui_test_handler.UiTestHandler())
        if("ada" in args.segments):
            beeeOnTestRunner.setBeeeOnSegmentTestfw(ada_test_handler.AdaTestHandler())
        if("adaapp" in args.segments):
            beeeOnTestRunner.setBeeeOnSegmentTestfw(adaapp_test_handler.AdaappTestHandler())
    except :
        print utils.failure("fail to instantiate segments testfw")
        sys.stdout.flush()
        return ERROR_RUNTIME
    '''
    place for register new BeeeOn Segment testfw
    '''

    return beeeOnTestRunner.run()
Exemple #6
0
    def __init__(self):
        self.name = "base tests"
        self.dbLastOutput = []
        try:
            self.dbConn = psycopg2.connect(DB_CON_STRING)
            self.dbCursor = self.dbConn.cursor()
        except:
            print utils.failure("FAIL: cannot connect database")
            raise

        self.supportedTests = {}
        self.supportedTests['sql'] = self.processDb
        self.supportedTests['sqlresult'] = self.processDbResult
        self.supportedTests['py'] = self.processPythonScript
        self.supportedTests['sleep'] = self.processSleep
Exemple #7
0
def intraday(event, context):
    logger.info('event : {event}'.format(event=event))
    path, query = validate_params(path=event.get('pathParameters'), query=event.get('queryStringParameters'))

    symbol = path.get('symbol')
    if not symbol:
        return failure(code=400, body='You should provide a symbol to your path parameters')

    logger.info('Getting stock intraday {symbol}'.format(symbol=symbol))

    try:
        result = Alphavantage().get_intraday(**path, **query)
        print(result)
        # if not result.get('Item'):
        #     return success(status_code=204, body=json.dumps(result.get('Item'), cls=DecimalEncoder))
    except Exception as e:
        return failure(body=e)
Exemple #8
0
def handler_error(error):
    if hasattr(error, 'name'):
        msg = error.name
        code = error.code
    else:
        msg = error.message
        code = 500

    return failure(msg, code)
Exemple #9
0
    def runSingleTestPart(self, testPartFile):
        testFile = os.path.basename(testPartFile)
        if("." not in testFile):
            print utils.failure("extension missing")
            sys.stdout.flush()
            return False

        fileName, fileExt = testFile.split(".")

        if("ignore" in fileExt):
            return True

        if(fileExt not in self.allTestsDict.keys()):
            print utils.failure("unknown extension: " + fileExt)
            sys.stdout.flush()
            return False

        return self.allTestsDict[fileExt](testPartFile)
Exemple #10
0
    def __init__(self):
        self.name = "ui-server tests"
        self.lastSocketOutput = ""
        self.host = HOST
        self.port = UI_PORT

        self.sessionMap = {}

        try:
            self.socket = utils.SecuredSocket(self.host, self.port)
        except:
            print utils.failure("FAIL: cannot connect to ui_server socket")
            raise


        self.supportedTests = {}
        self.supportedTests['input'] = self.processInput
        self.supportedTests['output'] = self.processOutput
        self.supportedTests['osyntax'] = self.processOutputSyntax
Exemple #11
0
def start_ngas_and_check_status(tgt_cfg):
    """
    Starts the ngamsDaemon process and checks that the server is up and running.
    Then it shuts down the server
    """

    # We sleep 2 here as it was found on Mac deployment to docker container that the
    # shell would exit before the ngasDaemon could detach, thus resulting in no startup.
    virtualenv('ngamsDaemon start -cfg {0} && sleep 2'.format(tgt_cfg))
    try:
        res = virtualenv('ngamsDaemon status -cfg {0}'.format(tgt_cfg),
                         warn_only=True)
        if res.failed:
            failure(
                "Couldn't contact NGAS server after starting it. "
                "Check log files under %s/log/ to find out what went wrong" %
                ngas_source_dir(),
                with_stars=False)
        else:
            success('NGAS server started correctly :)')
    finally:
        info("Shutting NGAS server down now")
        virtualenv("ngamsDaemon stop -cfg {0}".format(tgt_cfg))
Exemple #12
0
def main(args):
    try:
        check_args(args)
        profile = get_profile(args)
        validator_classes = get_validator_classes(profile)
    except ArgError:
        parser.print_usage()
        sys.exit(1)

    results_grid = {}
    results_list = []

    # Args are correct, lets now perform necessary steps
    for source_d in args.directories:
        if _is_verbose:
            utils.trace('TARGET', source_d)
        info = {
            'directory': source_d,
            'errors': 0,
            'successes': 0,
            'failures': 0,
            'skipped': 0,
            'messages': {
                'error': [],
                'skip': [],
                'fail': [],
                'feedback': [],
            },
        }
        results_list.append(info)
        for validator_class in validator_classes:

            # Check if validator class is actually a list or tuple that
            # consists of the name of the function, and keyword args
            if hasattr(validator_class, '__iter__'):
                name, kwargs = validator_class
                if hasattr(validators, name + '_validator'):
                    name = name + '_validator'
                validator_function = getattr(validators, name)
                validator_class = validator_function(**kwargs)

            validator = validator_class()
            name = validator.get_name()

            if _is_verbose:
                utils.trace('Validator', name)

            # Actually run the validator, catching exceptions to mark as
            # failure
            result = '.'
            try:
                validator.validate(source_d)
            except validators.ValidationUnableToCheckError as e:
                if _is_verbose:
                    utils.failure(source_d, name)
                info['skipped'] += 1
                info['messages']['skip'].append(str(e))
                result = '?'
            except validators.ValidationError as e:
                if _is_verbose:
                    utils.failure(source_d, name)
                info['failures'] += 1
                result = 'F'
                info['messages']['fail'].append(str(e))
            except Exception as e:
                if _is_verbose:
                    utils.failure(source_d, name)
                info['failures'] += 1
                info['errors'] += 1
                result = 'E'
                traceback.print_exc()
                info['messages']['error'].append(str(e))
            else:
                info['successes'] += 1
                feedback = validator.get_feedback()
                if feedback:
                    info['messages']['feedback'].append(feedback)
                if _is_verbose:
                    utils.success(source_d, name)

            # Set the result to results_grid
            key = format_matrix_labels(source_d, name)
            results_grid[key] = result

        if _is_verbose:
            if not info['failures']:
                utils.success(source_d, successes)
            else:
                utils.success(source_d, successes)
                utils.failure(source_d + ' failures:', failures)
                utils.failure(source_d + ' errors:', errors)
    print_report(results_grid, results_list)
Exemple #13
0
 def to_resp(self):
     return failure(self.msg, self.code)
Exemple #14
0
    def run(self):
        testModuleDirs = filter(os.path.isdir, os.listdir('.'))
        unitTestPassed = True
        # module tests
        for testModuleDir in testModuleDirs:

            isTesting = False
            for segment in self.segments:
                if re.match(segment + "_*", testModuleDir):
                    isTesting = True
                    break

            if not isTesting:
                continue

            testModuleFilter = "^" + self.filterRegex.split("/")[0]
            # skip if filter regex not match
            if not re.match(testModuleFilter, testModuleDir):
                continue
            print ""
            sys.stdout.flush()
            print "<",testModuleDir
            sys.stdout.flush()
            testDirs = next(os.walk(testModuleDir))[1]

            moduleTestsPassed = True
            # test cases
            for testDir in sorted(testDirs):
                if not re.match(self.filterRegex, testModuleDir+"/"+testDir):
                    continue
                if "ignore" in testDir:
                    print "  +", utils.info(testDir), utils.warning("IGNORE")
                    sys.stdout.flush()
                    continue

                print "  +", utils.info(testDir), ":",
                sys.stdout.flush()
                testPartFiles = sorted(os.listdir(testModuleDir + '/' + testDir))
                # print testPartFiles,
                singleTestPassed = True
                # test parts
                for testPartFile in sorted(testPartFiles):
                    # print "   +", testPartFile,
                    testPartPassed = self.runSingleTestPart(testModuleDir + '/' + testDir + '/' + testPartFile)
                    if(testPartPassed):
                        print testPartFile,
                        sys.stdout.flush()
                        pass
                    else:
                        singleTestPassed = False
                        print utils.failure(testPartFile) ,
                        sys.stdout.flush()
                        # dont break, we need to clean after test

                if(singleTestPassed):
                    print utils.success("OK")
                    sys.stdout.flush()
                else:
                    print utils.failure("FAIL")
                    sys.stdout.flush()
                    moduleTestsPassed = False
                    if(self.stopOnFail):
                        break

            if(moduleTestsPassed):
                print utils.success("> module "+ testModuleDir +" OK")
                sys.stdout.flush()
            else:
                print utils.failure("> module "+ testModuleDir +" FAIL")
                sys.stdout.flush()
                unitTestPassed = False
                if(self.stopOnFail):
                  break

        if(unitTestPassed):
            print utils.success("Tests OK")
            sys.stdout.flush()
            return SUCCESS
        else:
            print utils.failure("Tests FAIL")
            sys.stdout.flush()
            unitTestPassed = False
            return ERROR_TEST_FAIL

        return SUCCESS