forked from picoCTF/picoCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
65 lines (47 loc) · 1.44 KB
/
operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Low level deployment operations.
"""
from os import makedirs, path
from random import randint, Random
from signal import SIGKILL
from time import time
from spur import LocalShell
PROBLEMS_GROUP = "problems"
class TimeoutError(Exception):
"""
Exception dealing with executed commands that timeout.
"""
pass
def execute(cmd, timeout=600, **kwargs):
"""
Executes the given shell command
Args:
cmd: List of command arguments
timeout: maximum allotted time for the command
**kwargs: passes to LocalShell.spawn
Returns:
An execution result.
Raises:
NoSuchCommandError, RunProcessError, FileNotFoundError
"""
shell = LocalShell()
# It is unlikely that someone actually intends to supply
# a string based on how spur works.
if type(cmd) == str:
cmd = ["bash", "-c"] + [cmd]
process = shell.spawn(cmd, store_pid=True, **kwargs)
start_time = time()
while process.is_running():
delta_time = time() - start_time
if delta_time > timeout:
process.send_signal(SIGKILL)
raise TimeoutError(cmd, timeout)
return process.wait_for_result()
def create_user(username):
"""
Creates a user account with the given username
Args:
username: the username to create
"""
execute(["groupadd", "-f", PROBLEMS_GROUP])
execute(["useradd", "-G", PROBLEMS_GROUP, "-s", "/bin/bash", username])