Implement lxc container ssh logic
parent
18908d3f64
commit
6fe9fbfc3c
|
@ -5,16 +5,18 @@ import ipaddress
|
||||||
lxd_client = pylxd.client.Client()
|
lxd_client = pylxd.client.Client()
|
||||||
|
|
||||||
|
|
||||||
def create_instance(container_name: str):
|
def create_instance(container_name: str, instance_password: str):
|
||||||
config = {'name': container_name, 'source':
|
config = {'name': container_name, 'source':
|
||||||
{'type': 'image', "mode": "pull", "server": "https://cloud-images.ubuntu.com/daily", "protocol": "simplestreams",
|
{'type': 'image', "mode": "pull", "server": "https://cloud-images.ubuntu.com/daily", "protocol": "simplestreams",
|
||||||
'alias': 'lts/amd64'}, 'config': {'security.nesting': 'true'}}
|
'alias': 'lts/amd64'}, 'config': {'security.nesting': 'true'}}
|
||||||
|
|
||||||
instance = lxd_client.instances.create(config, wait=True)
|
instance = lxd_client.instances.create(config, wait=True)
|
||||||
instance.start(wait=True)
|
instance.start(wait=True)
|
||||||
|
|
||||||
while type(ipaddress.ip_address(instance.state().network['eth0']['addresses'][0]['address'])) != ipaddress.IPv4Address:
|
while type(ipaddress.ip_address(instance.state().network['eth0']['addresses'][0]['address'])) != ipaddress.IPv4Address:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
setup_ssh(container_name, instance_password)
|
||||||
|
|
||||||
return instance.state().network['eth0']['addresses'][0]
|
return instance.state().network['eth0']['addresses'][0]
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,8 +28,20 @@ def destroy_instance(container_name: str):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def execute_command(container_name: str, command: str):
|
def execute_command(container_name: str, command: list, stdin_payload=None):
|
||||||
instance = lxd_client.instances.get(container_name)
|
instance = lxd_client.instances.get(container_name)
|
||||||
result_tuple = instance.execute([command])
|
result_tuple = instance.execute(command, stdin_payload=stdin_payload)
|
||||||
|
|
||||||
return result_tuple
|
return result_tuple
|
||||||
|
|
||||||
|
|
||||||
|
def setup_ssh(container_name: str, instance_password: str):
|
||||||
|
execute_command(container_name,
|
||||||
|
["sed", "-i", "s/PasswordAuthentication no/PasswordAuthentication yes/", "/etc/ssh/sshd_config"])
|
||||||
|
execute_command(container_name,
|
||||||
|
["sed", "-i", "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/",
|
||||||
|
"/etc/ssh/sshd_config"])
|
||||||
|
execute_command(container_name, ["systemctl", "restart", "sshd"])
|
||||||
|
execute_command(container_name, ["passwd", "root"], stdin_payload=f"{instance_password}\n{instance_password}")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
1
main.py
1
main.py
|
@ -1,6 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import paramiko
|
import paramiko
|
||||||
import sshim_patch as sshim
|
import sshim_patch as sshim
|
||||||
import lxd_interface
|
import lxd_interface
|
||||||
|
|
|
@ -8,8 +8,7 @@ import lxd_interface
|
||||||
import threading
|
import threading
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logging.basicConfig(level='DEBUG')
|
logger = logging.getLogger(__name__)
|
||||||
logger = logging.getLogger()
|
|
||||||
|
|
||||||
|
|
||||||
def expect(self, line, echo=True) -> str:
|
def expect(self, line, echo=True) -> str:
|
||||||
|
@ -68,7 +67,7 @@ def check_auth_none(self, username):
|
||||||
|
|
||||||
|
|
||||||
def check_auth_password(self, username, password):
|
def check_auth_password(self, username, password):
|
||||||
print(os.environ["ssh-username"], os.environ["ssh-password"])
|
logger.debug(os.environ["ssh-username"])
|
||||||
if username == os.environ["ssh-username"] and password == os.environ["ssh-password"]:
|
if username == os.environ["ssh-username"] and password == os.environ["ssh-password"]:
|
||||||
return paramiko.AUTH_SUCCESSFUL
|
return paramiko.AUTH_SUCCESSFUL
|
||||||
return paramiko.AUTH_FAILED
|
return paramiko.AUTH_FAILED
|
||||||
|
@ -89,7 +88,7 @@ class Runner(threading.Thread):
|
||||||
self.channel.settimeout(None)
|
self.channel.settimeout(None)
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
vm_ip = lxd_interface.create_instance(self.instance_name)
|
vm_ip = lxd_interface.create_instance(self.instance_name, self.instance_password)
|
||||||
|
|
||||||
with paramiko.SSHClient() as ssh_client:
|
with paramiko.SSHClient() as ssh_client:
|
||||||
ssh_client.connect(vm_ip, username='root', passphrase=self.instance_password)
|
ssh_client.connect(vm_ip, username='root', passphrase=self.instance_password)
|
||||||
|
|
Loading…
Reference in New Issue