Beispiel #1
0
def send(name, text, newline=True):
    '''send [screen name], [text]
    Send the specified text to the screen console of the name specified.
    '''
    if newline:
        text = text + '\n'
    run('screen -S %s -p0 -X stuff \'%s\'' % (name, text))
Beispiel #2
0
def main(args):
    parser = argparse.ArgumentParser(
        description = 'libgxps regression tests',
        prog = 'gxps-regtest',
        usage = '%(prog)s [options ...] command [command-options ...] tests',
        add_help = False)
    parser.add_argument('-h', '--help',
                        action = HelpAction, nargs = 0)
    parser.add_argument('--help-command', metavar = 'COMMAND',
                        action = HelpAction,
                        help = 'Show help for a given command')
    parser.add_argument('-v', '--verbose',
                        action = 'store_true', dest = 'verbose', default = False,
                        help = 'Run in verbose mode')
    parser.add_argument('--tools-dir',
                        action = 'store', dest = 'tools_dir', default = os.path.abspath("../tools"),
                        help = 'Directory of gxps tools used for the tests')
    parser.add_argument('--skip', metavar = 'FILE',
                        action = 'store', dest = 'skipped_file',
                        help = 'File containing tests to skip')

    ns, args = parser.parse_known_args(args)
    if not args:
        parser.print_help()
        sys.exit(0)

    Config(vars(ns))
    try:
        commands.run(args)
    except commands.UnknownCommandError:
        sys.stderr.write("Unknown command: %s\n" % (args[0]))
        commands.print_help()
        sys.exit(1)
Beispiel #3
0
    def do_start(self, s):
        """start
        Starts the bukkit server.
        """
        if alive():
            print "Server already running."
            return

        self.ram.setupWorlds(0)

        runenv = os.path.join(self.env, "env")
        java = run("which java")
        startup = "%s %s -Xms%sm -Xmx%sm -jar craftbukkit.jar" % (
            java,
            config.get("Settings", "flags"),
            config.get("Settings", "memory_min"),
            config.get("Settings", "memory_max"),
        )
        screen = "screen -dmLS bukkit_server bash -c '%s'" % startup
        command = "cd %s;%s" % (runenv, screen)
        run(command)
        time.sleep(1)  # added to make sure alive can settle a little
        if alive():
            print "Server startup initiated."
        else:
            print "Server startup failed."
Beispiel #4
0
def new(name, command, logging=False):
    '''new [screen name], [shell command]
    Starts up a new screen console using the name and command specified.
    '''
    l = ''
    if logging: l = 'L'
    run('screen -d%smS %s bash -c \'%s\'' % (l, name, command))
Beispiel #5
0
def virusengine_set(state):
    '''
        state:on/off
       	return: Flase_msg/True
    '''

    noconf_msg = 'There is not config about virus on firewall!'
    virusconf_file = '/var/efw/virus/conf'
    cmd = '/usr/local/bin/setvirus.py'
    #cmd = '/usr/local/bin/setvirus.py'

    if not os.path.exists(virusconf_file):
        return noconf_file
    if state == 'off':
        state = '0'
    else:
        state = '1'
    try:
        with open(virusconf_file, 'r') as fd:
            line = fd.read()
            if not line.strip():
                return noconf_msg
        configs = line.split(',')
        configs[0] = state
        new_line = ','.join(configs)
        with open(virusconf_file, 'w') as fd:
            fd.write(new_line)
        run(cmd)
        os.system(cmd)
        return True
    except Exception, e:
        return e
Beispiel #6
0
def ipsengine_set(state):
    '''
        state: on/off
        return: False_msg/True
    '''

    noconf_msg = 'There is not config about ips on firewall!'
    ipsconf_file = '/var/efw/idps/engine/settings'
    cmd = '/usr/local/bin/restartips.py'

    if not os.path.exists(ipsconf_file):
        return noconf_file
    try:
        with open(ipsconf_file, 'r') as fd:
            line = fd.read()
            if not line.strip():
                return noconf_msg
        configs = line.split(',')
        configs[4] = state
        new_line = ','.join(configs)
        with open(ipsconf_file, 'w') as fd:
            fd.write(new_line)
        #sub = subprocess.Popen(cmd)
        #sub.wait()
        run(cmd)
        return True
    except Exception, e:
        return e
def test_raises_when_api_token_is_empty_string(capsys):
    ev = core_env_vars()
    ev["MERKELY_API_TOKEN"] = ""

    with dry_run(ev) as env, scoped_merkelypipe_json():
        with raises(ChangeError):
            run(External(env=env))
Beispiel #8
0
def new(name, command, logging=False):
    '''new [screen name], [shell command]
    Starts up a new screen console using the name and command specified.
    '''
    l = ''
    if logging: l = 'L'
    run('screen -d%smS %s bash -c \'%s\'' % (l, name, command))
Beispiel #9
0
def route_fromconfig():
    cmd = '/usr/local/bin/setrouting.py'
    try:
        run(cmd)
        return True
    except Exception, e:
        return e
Beispiel #10
0
def interface_stp_set_fromconfig():
    '''
        config file: /var/efw/stp/setting
    '''
    net_dict = {}
    enable_dict = {}
    cmd = '/usr/local/bin/stp_interface enable'
    netsetting = '/var/efw/ethernet/settings'
    enablesetting = '/var/efw/stp/setting'
    try:
        with open(netsetting, 'r') as net_file:
            netfile_lines = net_file.read().split('\n')
        with open(enablesetting, 'r') as enable_file:
            enablefile_lines = enable_file.read().split('\n')
        for line in netfile_lines:
            if '_DEV' in line:
                net_dict[line.split('_DEV')[0]] = line.split('=')[
                    1]  # eg: net_dict['GREEN'] = 'br0'
        for line in enablefile_lines:
            if '_ENABLE=' in line:
                enable_dict[line.split('_ENABLE')[0]] = line.split(
                    '=')[1].upper()  # eg: enable_dict['GREEN'] = 'ON'
        for color in net_dict:
            if color in enable_dict:
                this_cmd = cmd.replace('interface', net_dict[color])
                this_cmd = this_cmd.replace('enable', enable_dict[color])
                run(this_cmd)
        return True
    except Exception, e:
        return e
Beispiel #11
0
def console(command, wait=None, env='/opt/minecraft'):
    if not alive():
        print 'Server not Alive, could not send command'
        return None
    line = None
    if wait is not None:
        rex = re.compile(wait)
        lfname = os.path.join(env, 'env', 'server.log')
        logfile = open(lfname, 'r')
        size = os.stat(lfname)[6]
        logfile.seek(size)
    run('screen -S bukkit_server -p0 -X stuff \'%s\n\'' % command)
    if wait is not None:
        found = False
        while not found:
            where = logfile.tell()
            line = logfile.readline()
            if not line:
                time.sleep(0.1)
                logfile.seek(where)
            else:
                if len(rex.findall(line)) > 0:
                    found = True
        logfile.close()
    return line
Beispiel #12
0
def send(name, text, newline=True):
    '''send [screen name], [text]
    Send the specified text to the screen console of the name specified.
    '''
    if newline:
        text = text + '\n'
    run('screen -S %s -p0 -X stuff \'%s\'' % (name, text))
Beispiel #13
0
def command(method, *args):
    if method == 'task':
        commands.run(*args)
    elif method == 'dev_server':
        app.run_dev()
    else:
        print("unknown command {0}".format(method))
def test_raises_when_src_repo_root_does_not_exist(capsys):
    ev = new_log_approval_env()
    with dry_run(ev) as env:
        with raises(ChangeError) as exc:
            run(External(env=env))

    silent(capsys)
    assert str(exc.value) == "Error: Repository not found at /src/.git"
def test_raises_when_api_token_not_set(capsys):
    ev = core_env_vars()
    ev.pop("MERKELY_API_TOKEN")

    with dry_run(ev) as env:
        with ScopedFileCopier("/app/tests/data/Merkelypipe.json",
                              "/Merkelypipe.json"):
            with raises(ChangeError):
                run(External(env=env))
Beispiel #16
0
    def __call__(self, parser, namespace, values, option_string = None):
        if option_string == '--help-command':
            commands.run([values, '--help'])
            sys.exit(0)

        parser.print_help()
        commands.print_help()

        sys.exit(0)
Beispiel #17
0
def main():
    arguments = docopt(command_line)
    if arguments['run']:
        commands.run()
    elif arguments['rm']:
        commands.remove()
    elif arguments['rst']:
        commands.restart_solr()
    else:
        print "Error: No instruction passed"
Beispiel #18
0
def interface_stp_set(interface, state):
    '''
    '''
    cmd = '/usr/local/bin/stp_%s %s' % (interface, state.upper())
    if 'br' not in interface or len(interface) != 3:
        return 'the interface:%s is not a bridge interface, eg: br0,br1...' % interface
    try:
        run(cmd)
        return True
    except Exception, e:
        return e
Beispiel #19
0
def main():
    offset = None

    while True:
        notify()
        update = updates.get_oldest_update(updates.get_updates(offset))
        if not update:
            continue

        run(update)

        offset = updates.get_update_id(update) + 1
Beispiel #20
0
 def resize_tmpfs(self):
     '''resize_tmpfs
     Resizes the tmpfs mount to maintain the +10 percent space requirement
     '''
     world_path = os.path.join(self.env, 'env', self.name)
     if self.ramdisk:
         if self.automount:
             mountcmd = 'mount -o remount -o size=%sk %s' %\
                 (self._tmpfs_size(), world_path)
             if os.environ['USER'] != 'root':
                 mountcmd = 'sudo ' + mountcmd
             run(mountcmd)
Beispiel #21
0
 def _iptables(self, allow=False):
     if self.console():
         rule = '-m state --state NEW -m tcp -p tcp --dport %s' % \
                 self.console()
         if allow:
             run('sudo iptables -D INPUT %s -j REJECT' % rule)
             #print run('iptables -A INPUT %s -j ACCEPT')
             return True
         else:
             #print run('iptables -D INPUT %s -j ACCEPT')
             run('sudo iptables -A INPUT %s -j REJECT' % rule)
             return True
     return False
Beispiel #22
0
 def write_file(w_file, w_line):
     if not os.path.exists(w_file):
         return 'the vlan is not exists!'
     with open(w_file, 'r') as rf:
         lines = rf.read().split('\n')
     if w_line not in lines:
         return 'the vlan is not exists!'
     lines.remove(w_line)
     run("> %s" % w_file)
     for line in lines:
         cmd = 'echo %s >> %s' % (line, w_file)
         run(cmd)
     return True
Beispiel #23
0
def main(args):
    parser = argparse.ArgumentParser(
        description='Poppler regression tests',
        prog='poppler-regtest',
        usage='%(prog)s [options ...] command [command-options ...] tests',
        add_help=False)
    parser.add_argument('-h', '--help', action=HelpAction, nargs=0)
    parser.add_argument('--help-command',
                        metavar='COMMAND',
                        action=HelpAction,
                        help='Show help for a given command')
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        dest='verbose',
                        default=False,
                        help='Run in verbose mode')
    parser.add_argument('--utils-dir',
                        action='store',
                        dest='utils_dir',
                        default=os.path.abspath("../utils"),
                        help='Directory of poppler utils used for the tests')
    parser.add_argument(
        '-b',
        '--backends',
        action=ListAction,
        dest='backends',
        help='List of backends that will be used (separated by comma)')
    parser.add_argument('--skip',
                        metavar='FILE',
                        action='store',
                        dest='skipped_file',
                        help='File containing tests to skip')

    ns, args = parser.parse_known_args(args)
    if not args:
        parser.print_help()
        sys.exit(0)

    Config(vars(ns))
    try:
        commands.run(args)
    except commands.UnknownCommandError:
        sys.stderr.write("Unknown command: %s\n" % (args[0]))
        commands.print_help()
        sys.exit(1)
    except backends.UnknownBackendError as e:
        sys.stderr.write(str(e) + "\n")
        sys.stdout.write("Backends are: %s\n" % (", ".join(
            [backend.get_name() for backend in backends.get_all_backends()])))
        sys.exit(1)
Beispiel #24
0
def interface_link_set(interface, ip="", state=""):
    print state
    try:
        if ip != "":
            cmd = 'ip address add %s dev %s' % (ip, interface)
            run(cmd)
        if state != "":
            if state == 'off': state = 'down'
            if state == 'on': state = 'up'
            cmd = 'ip link set %s %s' % (interface, state)
            run(cmd)
        return True
    except Exception, e:
        return e
Beispiel #25
0
 def handle_line(self,line):
     if line.startswith("/"):
         try: command,args = whitespace.split(line[1:],1)
         except ValueError:
             command = line[1:]
             args = ()
         commands.run(command,self,args)
     else:
         if not self.sender.friends:
             print("You have no friends. :(")
         else:
             logging.debug("Sending to {} friends".format(len(self.sender.friends)))
             for addr in self.sender.friends:
                 self.sender.send(addr,line.encode('utf-8'))
Beispiel #26
0
def test_when_no_approvals_then_raises(mocker):
    mocked_get = mocker.patch('commands.control_deployment.http_get_json',
                              return_value=[])

    ev = new_control_deployment_env()

    with dry_run(ev) as env:
        with MockDockerFingerprinter(IMAGE_NAME, SHA256) as fingerprinter:
            external = External(env=env, docker_fingerprinter=fingerprinter)
            with raises(ChangeError):
                run(external)

    mocked_get.assert_called_once_with(
        f"https://{DOMAIN}/api/v1/projects/{OWNER}/{PIPELINE}/artifacts/{SHA256}/approvals/",
        "MY_SUPER_SECRET_API_TOKEN")
Beispiel #27
0
 def cleanup(self):
     '''cleanup
     Handles any housekeeping that may need to be done with the world as
     part of the server shutdown process.
     '''
     world_path = os.path.join(self.env, 'env', self.name)
     if self.ramdisk:
         self.rpsync()
         if self.automount:
             umountcmd = 'umount %s' % world_path
             if os.environ['USER'] != 'root':
                 umountcmd = 'sudo ' + umountcmd
             run(umountcmd)
             if os.path.ismounted(world_path):
                 raise Exception('Would UnMount Failed')
Beispiel #28
0
 def start(self):
     '''start
     Starts the Minecraft server and runs any per-world initialization.
     '''
     
     # First thing, we need to check to make sure the server isnt running,
     # we don't want to create any oddness as a result of running two
     # identically configured servers.
     if not self.running():
         
         # Next we will run the per-world initialization.  Normally this
         # shouldn't do anything, however if the worlds are stored on
         # ramdisks then there will be some useful setup stuff happening.
         self.prsync()
         
         # Now just to setup all the variables, determine which java we
         # will be running, and to build the bash command that we will send
         # to screen to startup the server.
         renv = os.path.join(self.env, 'env')
         java = run('which java')
         startup = 'cd %s;%s %s -Xms%sm -Xmx%sm -jar %s nogui' % (renv,
                                                            java,
                                                            self.java_args,
                                                            self.min_mem,
                                                            self.max_mem, 
                                                            self.binary)
         # And here we go, startup time!
         screen.new('mc_%s' % self.name, startup, self.logging)
    def __init__(self, load, ip=None):
        print "SERVER STARTING"
        self.numConnections = 0
        self.routingTable = None
        self.hashTable = None
        self.ip = run("ifconfig | awk '/inet addr/{print substr($2,6)}'").splitlines()[0]
        self.globalRange = None

        if ip:
            # tables = []
            # factory=WillowJoinFactory(tables)
            # reactor.connectTCP(ip, 1234, factory)
            # reactor.listenTCP(1234, self)
            # reactor.run()

            # while len(tables) != 2:
            #     pass
            # print "got tables!!"
            # # hashTable = tables[0]
            # # routingTable = tables[1]
            # print self.hashTable
            # print self.routingTable
            pass
        else:
            self.hashTable = WillowCuttingHashTable(load, 0, 5000)
            self.routingTable = RoutingTable()
            #get current ip address
            self.routingTable.addNode(self.ip, 0, 5000)
            self.globalRange = (self.hashTable.lowerBound, self.hashTable.upperBound)
    def __init__(self, load, ip=None):
        print "SERVER STARTING"
        self.numConnections = 0
        self.routingTable = None
        self.hashTable = None
        self.ip = run(
            "ifconfig | awk '/inet addr/{print substr($2,6)}'").splitlines()[0]
        self.globalRange = None

        if ip:
            # tables = []
            # factory=WillowJoinFactory(tables)
            # reactor.connectTCP(ip, 1234, factory)
            # reactor.listenTCP(1234, self)
            # reactor.run()

            # while len(tables) != 2:
            #     pass
            # print "got tables!!"
            # # hashTable = tables[0]
            # # routingTable = tables[1]
            # print self.hashTable
            # print self.routingTable
            pass
        else:
            self.hashTable = WillowCuttingHashTable(load, 0, 5000)
            self.routingTable = RoutingTable()
            #get current ip address
            self.routingTable.addNode(self.ip, 0, 5000)
            self.globalRange = (self.hashTable.lowerBound,
                                self.hashTable.upperBound)
Beispiel #31
0
def route_del(flags, destination, gateway, mask, iface=''):
    '''
        del a record
    '''
    try:
        dst = {
            'H': '-host ' + destination,
            'N': '-net ' + destination,
            'G': 'default'
        }
        gateway = 'gw %s' % gateway
        mask = 'netmask %s' % mask
        if iface:
            iface = 'dev %s' % iface
        if flags not in 'HNG':
            return 'the parameter "flags" is error'
        destination = dst[flags]
        cmd = 'route del %s %s %s %s' % (destination, gateway, mask, iface)
        status, output = run(cmd)
        if status == 0:
            return True
        else:
            return output
    except Exception, E:
        return e
Beispiel #32
0
 def handle_line(self, line):
     if line.startswith("/"):
         try:
             command, args = whitespace.split(line[1:], 1)
         except ValueError:
             command = line[1:]
             args = ()
         commands.run(command, self, args)
     else:
         if not self.sender.friends:
             print("You have no friends. :(")
         else:
             logging.debug("Sending to {} friends".format(
                 len(self.sender.friends)))
             for addr in self.sender.friends:
                 self.sender.send(addr, line.encode('utf-8'))
def stop_fogLight ( ):
    print "\nStopping Foglight"
    now =  datetime.now()
    print now.strftime("%d/%m/%Y %H:%M:%S")
    stop_command  = " /Quest_Software/Foglight/bin/fms -q " #option -q stop
    status, output =  run( stop_command )
    debug( status, output )
    times  = 1
    if output == 0:
        return True
    else:
        foglight_running = True

    while foglight_running:

    	 if not check_foglight_running():
	    foglight_runnig = False
            break;
         if times == MAX_TIMES:
            foglight_runnig = False
            stop_foglight_forced()
            break;
         times = times + 1 
         sleep(2)
    return True
 def GenKey():
     from commands import getstatusoutput as run
     s, o = run('uuidgen')
     if s == 0:
         return o
     else:
         raise ImportError("Missing uuid library (and can't fake it)")
Beispiel #35
0
 def start(self):
     '''start
     Starts the Minecraft server and runs any per-world initialization.
     '''
     
     # First thing, we need to check to make sure the server isnt running,
     # we don't want to create any oddness as a result of running two
     # identically configured servers.
     if not self.running():
         
         # Next we will run the per-world initialization.  Normally this
         # shouldn't do anything, however if the worlds are stored on
         # ramdisks then there will be some useful setup stuff happening.
         self.prsync()
         
         # Now just to setup all the variables, determine which java we
         # will be running, and to build the bash command that we will send
         # to screen to startup the server.
         renv = os.path.join(self.env, 'env')
         java = run('which java')
         startup = 'cd %s;%s %s -Xms%sm -Xmx%sm -jar %s nogui' % (renv,
                                                            java,
                                                            self.java_args,
                                                            self.min_mem,
                                                            self.max_mem, 
                                                            self.binary)
         # And here we go, startup time!
         screen.new('mc_%s' % self.name, startup, self.logging)
Beispiel #36
0
def route_add(flags, destination, gateway, mask, iface=''):
    '''
        添加一条路由
    '''
    print flags
    print destination
    print gateway
    print mask
    print iface
    try:
        dst = {
            'H': '-host ' + destination,
            'N': '-net ' + destination,
            'G': 'default'
        }
        gateway = 'gw %s' % gateway
        mask = 'netmask %s' % mask
        if iface:
            iface = 'dev %s' % iface
        if flags not in 'HNG':
            return 'the parameter "flags" is error'
        destination = dst[flags]
        cmd = 'route add %s %s %s %s' % (destination, gateway, mask, iface)
        status, output = run(cmd)
        if status == 0:
            return True
        else:
            return output
    except Exception, E:
        return e
Beispiel #37
0
    def __init__(self):
        cmd.Cmd.__init__(self)
        get_config()
        self.env = config.get("Settings", "environment")
        # Create any needed folders if they do not already exist.
        if not os.path.exists(self.env):
            os.makedirs(self.env)
        if not os.path.exists(os.path.join(self.env, "env")):
            os.makedirs(os.path.join(self.env, "env"))
        if not os.path.exists(os.path.join(self.env, "backup")):
            os.makedirs(os.path.join(self.env, "backup", "worlds"))
            os.makedirs(os.path.join(self.env, "backup", "snapshots"))

        self.ram = RamManager(self.env)

        # Check to see if java and screen is installed
        need_deps = False
        for package in ["java", "screen"]:
            output = run("which %s" % package)
            chk = re.compile(r"which:\sno\s%s" % package)
            if len(chk.findall(output)) > 0:
                need_deps = True
            if output == "":
                need_deps = True

            # If there were any issues, then we need to warn the user.
            if sys.platform != "win32":
                if need_deps:
                    print "".join(
                        [
                            "\nWARNING\n-------\n",
                            "Before you continue, please perform the following ",
                            "operations\nto install the needed software to get ",
                            "baskit working.  We\ndepend on this software in ",
                            "order to properly background and\nrun the bukkit ",
                            "server.  As it is expected for this\nscript to be ",
                            "run as a non-privileged user, we should not\nbe ",
                            "able to run these commands on our own.\n",
                        ]
                    )
                    if sys.platform == "linux2":
                        if run("which apt-get") == "/usr/bin/apt-get":
                            print "sudo apt-get -y install openjdk-6-jre screen\n"
                        elif run("which yum") == "/usr/bin/yum":
                            print "yum -y install java-1.6.0-openjdk screen\n"
                        else:
                            print "Please install java & screen.\n"
def test_required_env_vars(capsys, mocker):
    env = old_log_artifact_env()
    set_env_vars = {
        'CDB_ARTIFACT_GIT_URL': f"{BB}/{BB_ORG}/{BB_REPO}/commits/{COMMIT}",
        'CDB_ARTIFACT_GIT_COMMIT': COMMIT,
        'CDB_BUILD_NUMBER': BUILD_NUMBER,
        'CDB_CI_BUILD_URL':
        f'{BB}/{BB_ORG}/{BB_REPO}/addon/pipelines/home#!/results/{BUILD_NUMBER}',
        'CDB_ARTIFACT_SHA': SHA256,
    }
    with dry_run(env, set_env_vars):
        pipe = BitbucketPipe(pipe_metadata='/pipe.yml', schema=schema)
        mocker.patch('cdb.cdb_utils.calculate_sha_digest_for_docker_image',
                     return_value=SHA256)
        pipe.run()

    # extract data from approved cdb text file
    import inspect
    this_test = inspect.stack()[0].function
    approved = f"{APPROVAL_DIR}/{APPROVAL_FILE}.{this_test}.approved.txt"
    with open(approved) as file:
        old_approval = file.read()
    _old_blurb, old_method, old_payload, old_url = extract_blurb_method_payload_url(
        old_approval)

    expected_method = "Putting"
    expected_url = f"https://{DOMAIN}/api/v1/projects/{OWNER}/{PIPELINE}/artifacts/"
    expected_payload = {
        "build_url":
        f"{BB}/{BB_ORG}/{BB_REPO}/addon/pipelines/home#!/results/{BUILD_NUMBER}",
        "commit_url": f"{BB}/{BB_ORG}/{BB_REPO}/commits/{COMMIT}",
        "description": f"Created by build {BUILD_NUMBER}",
        "filename": IMAGE_NAME,
        "git_commit": COMMIT,
        "is_compliant": False,
        "sha256": SHA256,
    }

    # verify data from approved cdb text file
    assert old_payload == expected_payload
    assert old_method == expected_method
    assert old_url == expected_url

    # make merkely call
    ev = new_log_artifact_env()
    with dry_run(ev) as env:
        with MockDockerFingerprinter(IMAGE_NAME, SHA256) as fingerprinter:
            external = External(env=env, docker_fingerprinter=fingerprinter)
            method, url, payload = run(external)

    capsys_read(capsys)

    # CHANGE IN BEHAVIOUR
    expected_payload['user_data'] = {}

    # verify matching data
    assert method == expected_method
    assert url == expected_url
    assert payload == expected_payload
Beispiel #39
0
def exists(name):
    '''exists [screen name]
    Returns True/False wehther there is a screen of that name active.
    '''
    output = run('screen -wipe %s' % name)
    if output[:20] == 'There is a screen on':
        return True
    return False
Beispiel #40
0
 def init(self):
     '''init
     Initializes the world for the server to load.  This function mainly
     handles ramdisk setup like mounting the worlds and setting the disk
     sizing.
     '''
     world_path = os.path.join(self.env, 'env', self.name)
     if self.ramdisk:
         if self.automount:
             mountcmd = 'mount -t tmpfs -o size=%sk tmpfs %s' %\
                 (self._tmpfs_size(), world_path)
             if os.environ['USER'] != 'root':
                 mountcmd = 'sudo ' + mountcmd
             run(mountcmd)
             if not os.path.ismount(world_path):
                 raise Exception('World Mount Failed')
         self.prsync()
def check_foglight_running(): 
    command =  "/bin/ps auxx | /bin/grep -v grep | /bin/grep -v python |  /bin/grep -i FogLight | wc -l"
    status, output =  run( command )
    debug( status, output )
    if int (output) > 0:
        return True
    else:
        return False
Beispiel #42
0
def exists(name):
    '''exists [screen name]
    Returns True/False wehther there is a screen of that name active.
    '''
    output = run('screen -wipe %s' % name)
    if output[:20] == 'There is a screen on':
        return True
    return False
Beispiel #43
0
def test_docker_image(capsys, mocker):
    # make the cdb call
    env = {
        "CDB_ARTIFACT_DOCKER_IMAGE": IMAGE_NAME,
        "CDB_DESCRIPTION": DESCRIPTION,
        "CDB_ENVIRONMENT": ENVIRONMENT,
        "CDB_CI_BUILD_URL": CI_BUILD_URL,
        "CDB_USER_DATA": USER_DATA,
    }
    set_env_vars = {'CDB_ARTIFACT_SHA': SHA256}
    with dry_run(env, set_env_vars):
        mocker.patch('cdb.cdb_utils.calculate_sha_digest_for_docker_image',
                     return_value=SHA256)
        create_deployment("tests/integration/test-pipefile.json")
    verify_approval(capsys, ["out"])

    # extract data from approved cdb text file
    import inspect
    this_test = inspect.stack()[0].function
    approved = f"{APPROVAL_DIR}/{APPROVAL_FILE}.{this_test}.approved.txt"
    with open(approved) as file:
        old_approval = file.read()
    _old_blurb, old_method, old_payload, old_url = extract_blurb_method_payload_url(
        old_approval)

    expected_method = "Posting"
    expected_url = f"https://{DOMAIN}/api/v1/projects/{OWNER}/{NAME}/deployments/"
    expected_payload = {
        "artifact_sha256": SHA256,
        "build_url": CI_BUILD_URL,
        "description": DESCRIPTION,
        "environment": ENVIRONMENT,
        "user_data": {
            'status': 'deployed'
        },
    }

    # verify data from approved cdb text file
    assert old_method == expected_method
    assert old_url == expected_url
    assert old_payload == expected_payload

    # make merkely call
    protocol = "docker://"
    ev = create_new_deployment_env()
    ev["MERKELY_FINGERPRINT"] = f"{protocol}{IMAGE_NAME}"
    with dry_run(ev) as env:
        with MockDockerFingerprinter(IMAGE_NAME, SHA256) as fingerprinter:
            external = External(env=env, docker_fingerprinter=fingerprinter)
            method, url, payload = run(external)

    # verify matching data
    assert method == expected_method
    assert url == expected_url
    assert payload == expected_payload
    assert extract_blurb(capsys_read(capsys)) == [
        'MERKELY_COMMAND=log_deployment',
    ]
def test_bitbucket(capsys, mocker):
    # The original bitbucket code did not do a translation for put_evidence
    env = old_log_evidence_env()
    set_env_vars = {}
    with dry_run(env, set_env_vars):
        mocker.patch('cdb.cdb_utils.calculate_sha_digest_for_docker_image',
                     return_value=SHA256)
        put_evidence("tests/data/Merkelypipe.acme-roadrunner.json")

    verify_approval(capsys, ["out"])

    # extract data from approved cdb text file
    import inspect
    this_test = inspect.stack()[0].function
    approved = f"{APPROVAL_DIR}/{APPROVAL_FILE}.{this_test}.approved.txt"
    with open(approved) as file:
        old_approval = file.read()
    _old_blurb, old_method, old_payload, old_url = extract_blurb_method_payload_url(
        old_approval)

    expected_method = "Putting"
    expected_url = f"https://{DOMAIN}/api/v1/projects/{OWNER}/{PIPELINE}/artifacts/{SHA256}"
    expected_payload = {
        "contents": {
            "description":
            DESCRIPTION,
            "is_compliant":
            True,
            "url":
            f"https://{BB}/{BB_ORG}/{BB_REPO}/addon/pipelines/home#!/results/{BUILD_NUMBER}",
        },
        "evidence_type": EVIDENCE_TYPE
    }

    # verify data from approved cdb text file
    assert old_method == expected_method
    assert old_url == expected_url
    assert old_payload == expected_payload

    # make merkely call
    ev = new_log_evidence_env()
    with dry_run(ev) as env:
        with MockDockerFingerprinter(IMAGE_NAME, SHA256) as fingerprinter:
            external = External(env=env, docker_fingerprinter=fingerprinter)
            method, url, payload = run(external)

    # CHANGE IN BEHAVIOUR
    expected_payload['user_data'] = {}

    # verify matching data
    assert method == expected_method
    assert url == expected_url
    assert payload == expected_payload

    assert extract_blurb(capsys_read(capsys)) == [
        'MERKELY_COMMAND=log_evidence',
        'MERKELY_IS_COMPLIANT: True',
    ]
Beispiel #45
0
 def __init__(self):
     cmd.Cmd.__init__(self)
     get_config()
     self.env = config.get('Settings', 'environment')
     # Create any needed folders if they do not already exist.
     if not os.path.exists(self.env):
         os.makedirs(self.env)
     if not os.path.exists(os.path.join(self.env, 'env')):
         os.makedirs(os.path.join(self.env, 'env'))
     if not os.path.exists(os.path.join(self.env, 'backup')):
         os.makedirs(os.path.join(self.env, 'backup', 'worlds'))
         os.makedirs(os.path.join(self.env, 'backup', 'snapshots'))
         
     self.ram = RamManager(self.env)
         
 
     # Check to see if java and screen is installed
     need_deps = False
     for package in ['java', 'screen']:
         output = run('which %s' % package)
         chk = re.compile(r'which:\sno\s%s' % package)
         if len(chk.findall(output)) > 0:
             need_deps = True
         if output == '':
             need_deps = True
   
         # If there were any issues, then we need to warn the user.
         if sys.platform != 'win32':
             if need_deps:
                 print ''.join(['\nWARNING\n-------\n',
                                               'Before you continue, please perform the following ',
                                               'operations\nto install the needed software to get ',
                                               'baskit working.  We\ndepend on this software in ',
                                               'order to properly background and\nrun the bukkit ',
                                               'server.  As it is expected for this\nscript to be ',
                                               'run as a non-privileged user, we should not\nbe ',
                                               'able to run these commands on our own.\n'])
                 if sys.platform == 'linux2':
                     if run('which apt-get') == '/usr/bin/apt-get':
                         print 'sudo apt-get -y install openjdk-6-jre screen\n'
                     elif run('which yum') == '/usr/bin/yum' :
                         print 'yum -y install java-1.6.0-openjdk screen\n'
                     else:
                         print 'Please install java & screen.\n'
Beispiel #46
0
def handle_command(command_address, msg):
    auto_submitted = msg_get_header(msg, 'auto-submitted')
    if auto_submitted and auto_submitted.lower() != 'no':
        # Auto-submitted: header, https://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml
        print("Message appears to be automatically generated ({}), so ignoring it.".format(auto_submitted))
        return

    # Grab the address to which to respond and the subject
    reply_to = msg_get_response_address(msg)
    if reply_to is None:
        print("Failed to get an email address from the Reply-To, From, or Sender headers.")
        return
    subject = msg_get_header(msg, 'subject').replace('\n', '').replace('\r', '')
    print("Subject: " + subject)
    print("Responding to: " + reply_to)

    # Strip off any re:, fwd:, etc. (everything up to the last :, then trim whitespace)
    if ':' in subject:
        subject = subject[subject.rfind(':') + 1:]
    subject = subject.strip()

    try:
        cmd = get_signed_command(subject, reply_to)
    except ExpiredSignatureException:
        # TODO (maybe): Reply to the sender to tell them the signature was expired?  Or send a newly-signed message?
        print("Expired signature.")
        return
    except InvalidSignatureException:
        # Do nothing.
        print("Invalid signature.")
        return
    except NotSignedException:
        # If the subject isn't a signed command...
        # TODO (maybe): ... check if the reply_to is allowed to run the specific command with the given parameters...
        # ... and reply with a signed command for the recipient to send back (by replying).
        print("Signing command: {}".format(subject))
        response = send_response(
                source=command_address,
                destination=reply_to,
                subject='Re: {}'.format(sign(subject, reply_to)),
                body='To confirm and execute the command, please reply to this email.',
                )
        return

    # TODO (maybe): allow commands in body?
    
    # Execute the command.
    output = run(user=reply_to, cmd=cmd)

    # Reply with the output/response.
    response = send_response(
            source=command_address,
            destination=reply_to,
            subject='Re: {}'.format(subject),
            body='Output of "{}":\n\n{}'.format(cmd, output),
            )
def start_foglight():
    print "\nStarting Foglight"
    now =  datetime.now()
    print now.strftime("%d/%m/%Y %H:%M:%S")
    stop_command  = " /Quest_Software/Foglight/bin/fms -d " #option -d start as daemon
    status, output =  run( stop_command )
    debug( status, output )
    if int(status) == 0:
        return True 
    return False 
Beispiel #48
0
def getroute():
    '''
        获取路由详细信息
        Format:[ [Destination, Gateway, Genmask, Flags Metric Ref, Use Iface]... ]
    '''
    cmd = 'route -n'
    status, output = run(cmd)
    output_line = output.split('\n')[2:]  # 获取有用信息行
    return [[x for x in msg.split(' ') if x] for msg in output_line]
    '''
def run_command(cmd_name):
    try:
        if not cmd_name in commands.cmd_list.keys():
            raise
        return commands.run(cmd_name)

    except:
        agent_logger.log(logging.error,
                     "%s is not a registered command to run." % cmd_name)
        return None
def run_command(cmd_name):
    try:
        if not cmd_name in commands.cmd_list.keys():
            raise
        return commands.run(cmd_name)

    except:
        agent_logger.log(logging.error,
                         "%s is not a registered command to run." % cmd_name)
        return None
Beispiel #51
0
 def mountWorld(self, world, verbose=0):
     '''world - instance of RamWorld'''
     assert isinstance(world, RamWorld)
     if self.enable:
         # get disk size in mb
         size = int(run("du -m -s %s" % world.persistpath).split()[0])
         bumpsize = size / 10    # default to 10%
         if bumpsize < 10:       # but with a 10mb minimum
             bumpsize = 10
         target_mountsize = size  + bumpsize
         world.freewarn = bumpsize / 2   # warn at 50% of free space
         
         
         if self.autoMount:
             if not os.path.exists(world.rampath):
                 os.makedirs(world.rampath)
             # mount the ramdisk
             args = ["sudo"]
             if len(self.sudoPW) > 0:
                 args.append("-S")
             args.extend(["mount", "-t", "tmpfs", "none", world.rampath, "-o", "size=%sm" % target_mountsize])
             if verbose > 0:
                 print " ".join(args)
             if len(self.sudoPW) > 0:
                 xx = subprocess.Popen(args, stdin=subprocess.PIPE)
                 xx.communicate("%s\n" % self.sudoPW)
             else:
                 xx = subprocess.Popen(args)
             xx.wait()
         else:
             # Verify ramdisk is mounted
             if not os.path.ismount(world.rampath):
                 print "RAMDISK MOUNT ERROR:"
                 print "    No mounted volume found at %s" % world.rampath 
                 sys.exit(1)
             # Verify size of the ramdisk
             mountsize = int(run("df -B 1M %s" % world.rampath).split()[1])
             if mountsize < target_mountsize:
                 print "RAMDISK SIZE ERROR:"
                 print "   The ramdisk at %s" % world.rampath
                 print "   is %dmb but needs to be at least %dmb" % (mountsize, target_mountsize)
                 sys.exit(1)
def stop_foglight_forced ():
    print "\nStop forced"
    now =  datetime.now()
    print now.strftime("%d/%m/%Y %H:%M:%S")
    #Kids do not do this at home ...
    kill_command = "/bin/kill -9 `/bin/cat /Quest_Software/Foglight/state/.Foglight-fogserver.pid`"
    status, output =  run( kill_command )
    debug( status, output )
    if int(status) == 0:
        return True 
    return False 
Beispiel #53
0
 def do_start(self, s):
     '''start
     Starts the bukkit server.
     '''
     if alive():
         print 'Server already running.'
         return
     env = os.path.join(config.get('Settings', 'environment'), 'env')
     java = run('which java')
     startup = '%s %s -Xms%sm -Xmx%sm -jar craftbukkit.jar' % (java, 
                         config.get('Settings', 'flags'),
                         config.get('Settings', 'memory_min'),
                         config.get('Settings', 'memory_max'))
     screen = 'screen -dmLS bukkit_server bash -c \'%s\'' % startup
     command = 'cd %s;%s' % (env, screen)
     run(command)
     if alive():
         print 'Server startup initiated.'
     else:
         print 'Server startup failed.'
Beispiel #54
0
 def mergeWorlds(self, verbose=0):
     '''Called to save the data from the ramdisk worlds'''
     lockfile = open(os.path.join(self.env, "baskitlock"), "w")
     fcntl.flock(lockfile, fcntl.LOCK_EX)
     for world in self.worlds.itervalues():
         assert isinstance(world, RamWorld)  
         rsyncFolder(world.rampath, world.persistpath, verbose)
         freesize = int(run("df -B 1M %s | tail -1" % world.rampath).split()[3])
         if freesize < world.freewarn:
             print "WARNING: space is low on %s" % world.worldname 
     lockfile.close()
Beispiel #55
0
def rsyncFolder(sourcepath, destpath, verbose=0):
    '''rsyncFolder sourcepath destpath verbose
    verbose - if > 0 output rsync command
              if > 1 output files copied
    NOTE: current implementation is a non-destructive sync
          other code relies on this being non-destructive'''
    args = ["rsync", "-r", "-t", "-v", sourcepath + os.path.sep, destpath]
    if verbose > 0:
        print " ".join(args)
    output = run(" ".join(args))
    if verbose > 1:
        print output
Beispiel #56
0
def main(args):
    parser = argparse.ArgumentParser(
        description = 'Poppler regression tests',
        prog = 'poppler-regtest',
        usage = '%(prog)s [options ...] command [command-options ...] tests',
        add_help = False)
    parser.add_argument('-h', '--help',
                        action = HelpAction, nargs = 0)
    parser.add_argument('--help-command', metavar = 'COMMAND',
                        action = HelpAction,
                        help = 'Show help for a given command')
    parser.add_argument('-v', '--verbose',
                        action = 'store_true', dest = 'verbose', default = False,
                        help = 'Run in verbose mode')
    parser.add_argument('--utils-dir',
                        action = 'store', dest = 'utils_dir', default = os.path.abspath("../utils"),
                        help = 'Directory of poppler utils used for the tests')
    parser.add_argument('-b', '--backends',
                        action = ListAction, dest = 'backends',
                        help = 'List of backends that will be used (separated by comma)')
    parser.add_argument('--skip', metavar = 'FILE',
                        action = 'store', dest = 'skipped_file',
                        help = 'File containing tests to skip')

    ns, args = parser.parse_known_args(args)
    if not args:
        parser.print_help()
        sys.exit(0)

    Config(vars(ns))
    try:
        commands.run(args)
    except commands.UnknownCommandError:
        sys.stderr.write("Unknown command: %s\n" % (args[0]))
        commands.print_help()
        sys.exit(1)
    except backends.UnknownBackendError as e:
        sys.stderr.write(str(e) + "\n")
        sys.stdout.write("Backends are: %s\n" % (", ".join([backend.get_name() for backend in backends.get_all_backends()])))
        sys.exit(1)
Beispiel #57
0
 def _sync(self, from_path, to_path):
     '''Internal Function
     Convenience function for rsyncing.  Will use a lockfile with the
     world name to prevent multipe syncs from occuring at the same time to
     the same files.
     '''
     rsync_cmd = 'rsync -r -t %s/ %s/' % (from_path, to_path)
     world = os.path.split(from_path)[1]
     lockfile = os.path.join(self.env, 'persistent', '%s.lock' % world)
     
     # Wait for the lockfile to be released if one exists
     while os.path.exists(lockfile):
         time.sleep(0.1)
     
     # Create the lockfile
     lf = open(lockfile, 'w').close()
     
     # Run the Rsync
     run(rsync_cmd)
     
     # Remove the lockfile
     os.remove(lockfile)