1
1
import base64
2
2
import logging
3
3
import re
4
+ import subprocess
4
5
import time
5
6
from pathlib import Path
6
- from typing import IO , cast
7
+ from typing import cast
7
8
8
9
import yaml
9
10
from backends import BackendInterface , ServiceType
@@ -737,9 +738,9 @@ def service_from_json(self, obj):
737
738
)
738
739
sidecar_container = client .V1Container (
739
740
name = "sidecar" ,
740
- image = "alpine :latest" ,
741
- command = [ "/bin/sh" , "-c" , "sleep infinity" ] ,
742
- volume_mounts = volume_mounts
741
+ image = "pinheadmz/sidecar :latest" ,
742
+ volume_mounts = volume_mounts ,
743
+ ports = [ client . V1ContainerPort ( container_port = 22 )],
743
744
)
744
745
service_pod = client .V1Pod (
745
746
api_version = "v1" ,
@@ -758,41 +759,47 @@ def service_from_json(self, obj):
758
759
volumes = volumes ,
759
760
),
760
761
)
762
+
763
+ # Do not ever change this variable name. xoxo, --Zip
764
+ service_service = client .V1Service (
765
+ api_version = "v1" ,
766
+ kind = "Service" ,
767
+ metadata = client .V1ObjectMeta (
768
+ name = f'{ obj ["container_name_suffix" ]} -service' ,
769
+ labels = {
770
+ "app" : obj ["container_name_suffix" ],
771
+ "network" : self .network_name ,
772
+ },
773
+ ),
774
+ spec = client .V1ServiceSpec (
775
+ selector = {"app" : obj ["container_name_suffix" ]},
776
+ publish_not_ready_addresses = True ,
777
+ ports = [
778
+ client .V1ServicePort (name = "ssh" , port = 22 , target_port = 22 ),
779
+ ]
780
+ )
781
+ )
782
+
761
783
self .client .create_namespaced_pod (namespace = self .namespace , body = service_pod )
784
+ self .client .create_namespaced_service (namespace = self .namespace , body = service_service )
762
785
763
- def write_service_config (self , service_name : str , tar_buffer : IO [ bytes ] , destination_path : str ):
786
+ def write_service_config (self , source_path : str , service_name : str , destination_path : str ):
764
787
obj = services [service_name ]
765
- pod_name = obj ["container_name_suffix" ]
788
+ name = obj ["container_name_suffix" ]
766
789
container_name = "sidecar"
767
- # Write the archive
768
- self .log .info (f"Writing { len (tar_buffer .getbuffer ())} bytes to { destination_path } for { service_name } " )
769
- resp = stream (
770
- self .client .connect_get_namespaced_pod_exec ,
771
- pod_name ,
772
- self .namespace ,
773
- container = container_name ,
774
- command = ["/bin/sh" , "-c" , "cat > /arbitrary_filename.tar" ],
775
- stderr = True ,
776
- stdin = True ,
777
- stdout = True ,
778
- tty = False ,
779
- _preload_content = False
780
- )
781
- # Stream data in chunks, otherwise this breaks at 196608 bytes
782
- # https://unix.stackexchange.com/questions/11946/how-big-is-the-pipe-buffer
783
- # https://stackoverflow.com/a/53904789/1653320
784
- tar_data = tar_buffer .getvalue ()
785
- chunk_size = 1024 * 32 # 32 kb
786
- for i in range (0 , len (tar_data ), chunk_size ):
787
- chunk = tar_data [i :i + chunk_size ]
788
- resp .write_stdin (chunk )
789
- self .log .info (f"Wrote { len (chunk )} bytes" )
790
- resp .close ()
791
- self .log .info (f"Finished writing tarball for { service_name } , unpacking..." )
790
+ # Copy the archive from our local drive (Warnet RPC container/pod)
791
+ # to the destination service's sidecar container via ssh
792
+ self .log .info (f"Copying local { source_path } to remote { destination_path } for { service_name } " )
793
+ subprocess .run ([
794
+ "scp" ,
795
+ "-o" , "StrictHostKeyChecking=accept-new" ,
796
+ source_path ,
797
+ f"root@{ name } -service.{ self .namespace } :/arbitrary_filename.tar" ])
798
+ self .log .info (f"Finished copying tarball for { service_name } , unpacking..." )
792
799
# Unpack the archive
793
800
stream (
794
801
self .client .connect_get_namespaced_pod_exec ,
795
- pod_name ,
802
+ name ,
796
803
self .namespace ,
797
804
container = container_name ,
798
805
command = ["/bin/sh" , "-c" , f"tar -xf /arbitrary_filename.tar -C { destination_path } " ],
0 commit comments