-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
592 additions
and
584 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from .kmt_os import get_kmt_os | ||
from .stacks import find_ssh_key | ||
from .tool import Exit, error, info | ||
|
||
|
||
class CommandRunner: | ||
def __init__(self, ctx, local, vm, remote_ip, remote_ssh_key, log_debug): | ||
self.local = local | ||
self.remote_ip = remote_ip | ||
self.remote_ssh_key = ssh_key_to_path(remote_ssh_key) | ||
self.vm = vm | ||
self.ctx = ctx | ||
self.log_debug = log_debug | ||
|
||
def run_cmd(self, cmd, allow_fail=False, verbose=False): | ||
log_debug = self.log_debug or verbose | ||
info(f"[+] {self.vm.ip} -> {cmd}") | ||
if self.vm.arch == "local": | ||
res = run_cmd_local(self.ctx, cmd, self.vm.ip, log_debug) | ||
else: | ||
res = run_cmd_remote(self.ctx, cmd, self.remote_ip, self.vm.ip, self.remote_ssh_key, log_debug) | ||
|
||
if not res.ok: | ||
error(f"[-] Failed: {self.vm.ip} -> {cmd}") | ||
if not allow_fail: | ||
print_failed(res.stderr) | ||
raise Exit("command failed") | ||
|
||
def copy_files(self, path, dest=None): | ||
if self.vm.arch == "local" and dest is None: | ||
self.ctx.run(f"cp {path} {get_kmt_os().shared_dir}") | ||
elif self.vm.arch == "local" and dest is not None: | ||
self.ctx.run( | ||
f"scp -o StrictHostKeyChecking=no -i /home/kernel-version-testing/ddvm_rsa {path} root@{self.vm.ip}:{dest}" | ||
) | ||
else: | ||
if self.remote_ssh_key == "" or self.remote_ip == "": | ||
raise Exit("remote ssh key and remote ip are required to run command on remote VMs") | ||
self.run_cmd( | ||
f"scp -o StrictHostKeyChecking=no -i {self.remote_ssh_key} {path} ubuntu@{self.remote_ip}:/opt/kernel-version-testing/", | ||
False, | ||
) | ||
|
||
def sync_source(self, source, target): | ||
sync_source(self.ctx, source, target, self.remote_ip, self.remote_ssh_key, self.vm.ip, self.vm.arch) | ||
|
||
|
||
def sync_source(ctx, source, target, instance_ip, ssh_key, ip, arch): | ||
kmt_dir = get_kmt_os().kmt_dir | ||
vm_copy = f"rsync -e \\\"ssh -o StrictHostKeyChecking=no -i {kmt_dir}/ddvm_rsa\\\" --chmod=F644 --chown=root:root -rt --exclude='.git*' --filter=':- .gitignore' ./ root@{ip}:{target}" | ||
if arch == "local": | ||
ctx.run( | ||
f"rsync -e \"ssh -o StrictHostKeyChecking=no -i {kmt_dir}/ddvm_rsa\" -p --chown=root:root -rt --exclude='.git*' --filter=':- .gitignore' {source} root@{ip}:{target}" | ||
) | ||
elif arch == "x86_64" or arch == "arm64": | ||
ctx.run( | ||
f"rsync -e \"ssh -o StrictHostKeyChecking=no -i {ssh_key}\" -p --chown=root:root -rt --exclude='.git*' --filter=':- .gitignore' {source} ubuntu@{instance_ip}:/home/ubuntu/datadog-agent" | ||
) | ||
ctx.run( | ||
f"ssh -i {ssh_key} -o StrictHostKeyChecking=no ubuntu@{instance_ip} \"cd /home/ubuntu/datadog-agent && {vm_copy}\"" | ||
) | ||
else: | ||
raise Exit(f"Unsupported arch {arch}") | ||
|
||
|
||
def run_cmd_local(ctx, cmd, ip, log_debug): | ||
return ctx.run( | ||
f"ssh -o StrictHostKeyChecking=no -i /home/kernel-version-testing/ddvm_rsa root@{ip} '{cmd}'", | ||
warn=True, | ||
hide=(not log_debug), | ||
) | ||
|
||
|
||
def run_cmd_remote(ctx, cmd, remote_ip, ip, ssh_key, log_debug): | ||
if ssh_key == "" or remote_ip == "": | ||
raise Exit("remote ssh key and remote ip are required to run command on remote VMs") | ||
return ctx.run( | ||
f"ssh -o StrictHostKeyChecking=no -i {ssh_key} ubuntu@{remote_ip} \"ssh -o StrictHostKeyChecking=no -i /home/kernel-version-testing/ddvm_rsa root@{ip} '{cmd}'\"", | ||
warn=True, | ||
hide=(not log_debug), | ||
) | ||
|
||
|
||
def print_failed(output): | ||
out = list() | ||
for line in output.split("\n"): | ||
out.append(f"\t{line}") | ||
error('\n'.join(out)) | ||
|
||
|
||
def ssh_key_to_path(ssh_key): | ||
ssh_key_path = "" | ||
if ssh_key != "": | ||
ssh_key.rstrip(".pem") | ||
ssh_key_path = find_ssh_key(ssh_key) | ||
|
||
return ssh_key_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
def compiler_built(ctx): | ||
res = ctx.run("docker images kmt:compile | grep -v REPOSITORY | grep kmt", warn=True) | ||
return res.ok | ||
|
||
|
||
def docker_exec(ctx, cmd, user="compiler", verbose=True, run_dir=None): | ||
if run_dir: | ||
cmd = f"cd {run_dir} && {cmd}" | ||
|
||
ctx.run(f"docker exec -u {user} -i kmt-compiler bash -c \"{cmd}\"", hide=(not verbose)) | ||
|
||
|
||
def start_compiler(ctx): | ||
if not compiler_built(ctx): | ||
build_compiler(ctx) | ||
|
||
if compiler_running(ctx): | ||
ctx.run("docker rm -f $(docker ps -aqf \"name=kmt-compiler\")") | ||
|
||
ctx.run( | ||
"docker run -d --restart always --name kmt-compiler --mount type=bind,source=./,target=/datadog-agent kmt:compile sleep \"infinity\"" | ||
) | ||
|
||
uid = ctx.run("getent passwd $USER | cut -d ':' -f 3").stdout.rstrip() | ||
gid = ctx.run("getent group $USER | cut -d ':' -f 3").stdout.rstrip() | ||
docker_exec(ctx, f"getent group {gid} || groupadd -f -g {gid} compiler", user="root") | ||
docker_exec(ctx, f"getent passwd {uid} || useradd -m -u {uid} -g {gid} compiler", user="root") | ||
docker_exec(ctx, f"chown {uid}:{gid} /datadog-agent && chown -R {uid}:{gid} /datadog-agent", user="root") | ||
docker_exec(ctx, "apt install sudo", user="root") | ||
docker_exec(ctx, "usermod -aG sudo compiler && echo 'compiler ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers", user="root") | ||
docker_exec(ctx, f"install -d -m 0777 -o {uid} -g {uid} /go", user="root") | ||
|
||
|
||
def compiler_running(ctx): | ||
res = ctx.run("docker ps -aqf \"name=kmt-compiler\"") | ||
if res.ok: | ||
return res.stdout.rstrip() != "" | ||
return False | ||
|
||
|
||
def build_compiler(ctx): | ||
ctx.run("docker rm -f $(docker ps -aqf \"name=kmt-compiler\")", warn=True, hide=True) | ||
ctx.run("docker image rm kmt:compile", warn=True, hide=True) | ||
ctx.run("cd ../datadog-agent-buildimages && docker build -f system-probe_x64/Dockerfile -t kmt:compile .") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAACFwAAAAdzc2gtcn | ||
NhAAAAAwEAAQAAAgEA7QX+iYc1CmxNdIbr6r0+kD+hvzX6IiSjMOmD9qL5R4MJw7/Kc01A | ||
e+JN7wrF7Mpj/HTC8Tv11TpMBBCBnJumps2reZgOhWLPFmwIoY1pbt+SkRAjOlmwSs8MWW | ||
wom1Rw45h6VtCW2TfiQKSsr6HeVJzXQeNwRApCO3mMDSDjJrGZft8Xnn054e9A70fEX3II | ||
Mi4CeTY1Y5Dy6E4MNumzgSiq/F6Ok+eyZBtR11tXT5MkL40U3dm8xMxW3sYg4G66Y4yVWA | ||
/AZJHa30IM18d0XDzXf5trCZP31NptP8nisVqB0hQJ5331NNJzQfhjm1u4v2BAowulALZv | ||
+FUPOGemYevKOl26vsPj6050E43t2wbKog/fbVyaTnjZjuhY+oyFsCohdmKYafx48E+80R | ||
G8/4H6vIaWalUG5X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keye1AAABAQDxMrYChpvPGmlzNMjzjJO7Kl3FXkgWspd90gybUz28pgcVF8 | ||
0zevOg+/TyAzQCHuGgQ0FyG7cGAqCqu3jmWQrvD9PDJGgbOb/K5qCfhPCCe0Tif6ql6PdB | ||
I1NRqxtiUlGs8yYIxWJs0zmFjJnuHkR/OJg3qYlzOYI4UCoySktnKfiisGCIDXF/q0EOrk | ||
gQXbLSmOfhCcrNp75GiJTJ9Bgc+V85NCLbL0aTTSBEMz74ONj4/z2rxf31o+2Dig3G2yrm | ||
ddjS2kRKkNxrq9lxOm9e288G6yT9s/YZaxSRX1bsoW4y88t1Zrod0luuFztMrFIu5nrm6K | ||
nH9Jkqmy7J4OcnAAABAQD7kbKw7iY1HQmYIhLIWc3TwTdbQQvxsl0X3mqmQghna9SPmyFc | ||
Jw3QZ5Db7zk6UhT3VEffeGjnLo80TKejCAVZNdu0dTe3PpHl7Xs1IRZajc+/DcVyMhJbEc | ||
Ku31pHRnozPTDrZxu+vyG5su5/G6/QKwX9O2/oFqVOnEtTqP8QQfIGVRG3i+ZuKQAcGHwG | ||
zFgWBFTT+4oJSC8pMAQQfY5rrUSFr3Zg/EBhk/XeBmIxo5iyOkZtpCHNuGbqNYteNNsM8y | ||
a1eAv3AZrgqk0eQl1XapooMMSY5mKjxJKscqthce9uvVnWPWVSI9moPKH6gaZ6336UhFzz | ||
3VDkRuwEid4dAAAAFnJvb3RAaXAtMTcyLTI5LTE4NS0yMjgBAgME | ||
-----END OPENSSH PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.