#!/usr/bin/env python import argparse import models.executor import requests from iexceptions import ExecuteException, \ MountDirNotFoundException, InspectDockerError from const import START, STOP import grpc import injure_pb2 import injure_pb2_grpc class FsExecutor(models.executor.Executor): def __init__(self): pass def common_runner(self, action, operation, **kwargs): methods = kwargs["methods"] if "methods" in kwargs and \ kwargs["methods"] else None if not methods and action != "faultall": return errno = kwargs["errno"] if "errno" in kwargs and \ kwargs["errno"] else None if not errno: errno = 0 random = kwargs["random"] if "random" in kwargs and \ kwargs["random"] else None if random == "False": random = False else: random = True pct = kwargs["pct"] if "pct" in kwargs and \ kwargs["pct"] else None if not pct: pct = 100 path = kwargs["path"] if "path" in kwargs and \ kwargs["path"] else None if not path: return delay = kwargs["delay"] if "delay" in kwargs and \ kwargs["delay"] else None if not delay: delay = 0 if errno == 0 and delay == 0 and operation == START: return container_ip = kwargs["container_ip"] if "container_ip" and \ kwargs["container_ip"] else None if not container_ip: return with grpc.insecure_channel(container_ip + ':65534') as channel: stub = injure_pb2_grpc.InjureStub(channel) if operation == START: if action == "fault": stub.SetFault(injure_pb2.Request(methods=methods.split( ","), errno=errno, random=random, pct=pct, path=path, delay=delay)) elif action == "faultall": stub.SetFaultAll(injure_pb2.Request( errno=errno, random=random, pct=pct, path=path, delay=delay)) elif operation == STOP: if action == "recover": stub.RecoverMethod(injure_pb2.Request( methods=methods.split(","))) elif action == "recoverall": stub.RecoverMethodAll(injure_pb2.Request()) def fault(self, operation, **kwargs): return self.common_runner("fault", operation, **kwargs) def faultall(self, operation, **kwargs): return self.common_runner("faultall", operation, **kwargs) def recover(self, operation, **kwargs): return self.common_runner("recover", operation, **kwargs) def recoverall(self, operation, **kwargs): return self.common_runner("recoverall", operation, **kwargs) if __name__ == "__main__": parser = argparse.ArgumentParser( description='FS Injection Simulation') parser.add_argument( 'operation', metavar='start/stop', help='operations') parser.add_argument( '-a', '--action', dest='action', metavar='fault/faultall/recover/recoverall', help='which action to take ') parser.add_argument( '-m', '--methods', dest='methods', help='which methods to operation') parser.add_argument( '-e', '--errno', dest='errno', type=int, help='which errno to inject') parser.add_argument( '-r', '--random', dest='random', help='random errno to inject') parser.add_argument( '-p', '--pct', dest='pct', type=int, help='which percent to inject') parser.add_argument( '-P', '--path', dest='path', help='which path to inject') parser.add_argument( '-d', '--delay', dest='delay', type=int, help='which delay to inject') parser.add_argument( '-c', '--container_ip', dest='container_ip', help='which container ip to operation ') args = parser.parse_args() executor = FsExecutor() try: getattr(executor, args.action)( args.operation, methods=args.methods, errno=args.errno, random=args.random, pct=args.pct, path=args.path, delay=args.delay, container_ip=args.container_ip) except (ExecuteException, MountDirNotFoundException, InspectDockerError) as e: print e._msg exit(1)