Skip to content

Commit d4ed2e8

Browse files
committed
Automate RSA key handle with dev.py compose script, let nabla.init service copy the key to persistent ssh volume
1 parent e2ab948 commit d4ed2e8

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

docker/compose/ci/stages/dev/init/compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ services:
2626
- type: bind
2727
source: ../../../../
2828
target: ${THIS_PROJECT_DOCKER_BIND_DIRECTORY}
29-
entrypoint: ["ncpfmp.bat", "nbl.ci.dev.init"]
29+
entrypoint: ["ncpfmp.bat", "nbl.ci.dev.init", "--key", "/key"]
3030

3131
networks:
3232
nabla.network:

docker/dev.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ def parseInputArguments():
1616
def main():
1717
try:
1818
args = parseInputArguments()
19-
os.path.dirname(os.path.abspath(__file__))
20-
21-
os.chdir(os.path.dirname(os.path.abspath(__file__)))
22-
19+
20+
os.chdir(os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "compose/ci/stages/dev")))
21+
2322
key = args.ssh
2423
platform = args.platform
2524
arch = args.arch
@@ -35,24 +34,18 @@ def main():
3534

3635
if subprocess.call(["docker", "volume", "inspect", "ssh"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) != 0:
3736
subprocess.run(["docker", "volume", "create", "ssh"], check=True) # create ssh volume if not present
38-
39-
# TODO: Unix/Macos when needed
40-
#subprocess.call(f"docker rm -f dev.ssh.intermediate", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
41-
#subprocess.run(f"docker run -d -v ssh:C:\\volume-mount-point --name dev.ssh.intermediate artifactory.devsh.eu/nabla/windows/base:latest", check=True) # create intermediate container
42-
#subprocess.run(f"docker start dev.ssh.intermediate", check=True) # start intermediate container
43-
#subprocess.run(f"docker cp {key} dev.ssh.intermediate:C:\\volume-mount-point", check=True) # copy ssh key to ssh volume
44-
#subprocess.call(f"docker rm -f dev.ssh.intermediate", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
45-
46-
os.chdir("./compose/ci/stages/dev")
47-
37+
4838
compose = [
4939
"docker", "compose",
5040
"-f", f"./compose.{platform}.{arch}.yml",
5141
"--env-file", "../.env/platform/windows/.env"
5242
]
5343

54-
subprocess.run(compose + ["up", "--build"], check=True) # compose up pipeline
55-
subprocess.run(compose + ["down"], check=True) # compose down pipeline
44+
subprocess.run(compose + ["build"], check=True)
45+
subprocess.run(compose + ["create", "--force-recreate"], check=True)
46+
subprocess.run(compose + ["cp", key, "nabla.init:key"], check=True)
47+
subprocess.run(compose + ["up"], check=True)
48+
subprocess.run(compose + ["down"], check=True)
5649

5750
except subprocess.CalledProcessError as e:
5851
print(f"Subprocess failed with exit code {e.returncode}")

docker/scripts/nbl/ci/dev/init.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import os, subprocess, sys, argparse
1+
import os, subprocess, sys, argparse, shutil
22

33

44
def parseInputArguments():
55
parser = argparse.ArgumentParser(description="Nabla CI Pipeline nbl.ci.dev.init Framework Module")
66

7+
parser.add_argument("--key", help="RSA input key file", type=str, required=True)
8+
79
args = parser.parse_args()
810

911
return args
@@ -15,20 +17,28 @@ def init():
1517
if not THIS_PROJECT_SSH_DIRECTORY:
1618
raise ValueError("THIS_PROJECT_SSH_DIRECTORY environment variables doesn't exist!")
1719

18-
key = os.path.normpath(os.path.join(THIS_PROJECT_SSH_DIRECTORY, "id_rsa"))
19-
20-
# TODO: Unix/MacOS when needed
21-
subprocess.run(f"icacls.exe {key} /reset", check=True)
22-
subprocess.run(f"icacls.exe {key} /GRANT:R ContainerAdministrator:(R)", check=True)
23-
subprocess.run(f"icacls.exe {key} /inheritance:r", check=True)
20+
args = parseInputArguments()
2421

2522
try:
23+
inputKey = args.key
24+
targetKey = os.path.normpath(os.path.join(THIS_PROJECT_SSH_DIRECTORY, "id_rsa"))
25+
26+
# TODO: Unix/MacOS when needed
27+
subprocess.run(f"icacls.exe {targetKey} /reset", check=False)
28+
29+
shutil.copy(inputKey, targetKey)
30+
print(f"Copied \"{inputKey}\" to \"{targetKey}\"")
31+
32+
subprocess.run(f"icacls.exe {targetKey} /GRANT:R ContainerAdministrator:(R)", check=True)
33+
subprocess.run(f"icacls.exe {targetKey} /inheritance:r", check=True)
34+
2635
subprocess.run("ssh -o StrictHostKeyChecking=no -T [email protected]", check=True)
2736
except subprocess.CalledProcessError as e:
2837
if not (e.returncode == 0 or e.returncode == 1):
2938
raise ValueError("Could not authenticate with provided rsa key, exiting...")
39+
except FileNotFoundError:
40+
raise ValueError(f"Input key file \"{inputKey}\" not found")
3041

31-
3242
def main():
3343
try:
3444
init()

0 commit comments

Comments
 (0)