Skip to content
Snippets Groups Projects
Commit 12d638cd authored by shinedday's avatar shinedday
Browse files

Add config.json support as well as better network update

parent a6a135c3
Branches
Tags
No related merge requests found
...@@ -4,7 +4,8 @@ from os import path ...@@ -4,7 +4,8 @@ from os import path
from time import sleep from time import sleep
from tool.mqtt_client import MqttClient from tool.mqtt_client import MqttClient
from tool.ssh_client import SSHClient, Cmd, RemoteClient from tool.ssh_client import SSHClient, Cmd
from tool.remote_client import RemoteClient
from tool.update import VersionManager from tool.update import VersionManager
class Ihm(MqttClient, SSHClient): class Ihm(MqttClient, SSHClient):
...@@ -63,8 +64,16 @@ class Ihm(MqttClient, SSHClient): ...@@ -63,8 +64,16 @@ class Ihm(MqttClient, SSHClient):
# automatise la mise a jour de l'exerience sur les raspberry # automatise la mise a jour de l'exerience sur les raspberry
if cmd.lower() == "update": if cmd.lower() == "update":
r = RemoteClient("192.168.24.18", "pi", "raspberry") commands = [
updater = VersionManager(r) Cmd(
cmd="rm -r Desktop/mqtt_goyon/iotamak-core"
)
]
for i_client in range(len(self.clients)):
print("Hostname :", self.clients[i_client].hostname, " User :", self.clients[i_client].user)
self.run_cmd(i_client, commands)
updater = VersionManager()
updater.update() updater.update()
# charge une experience et verifie le format # charge une experience et verifie le format
......
import json import json
from tool.ssh_client import RemoteClient import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.remote_client import RemoteClient
def read_ssh(path):
def read_ssh(path):
with open(path, 'r', encoding='utf-8') as f: with open(path, 'r', encoding='utf-8') as f:
dict = json.load(f) dict = json.load(f)
...@@ -17,8 +20,9 @@ def read_ssh(path): ...@@ -17,8 +20,9 @@ def read_ssh(path):
)) ))
return res return res
def read_broker(path): def read_broker(path):
with open(path, 'r', encoding='utf-8') as f: with open(path, 'r', encoding='utf-8') as f:
dict = json.load(f) dict = json.load(f)
return dict.get("broker") return dict.get("broker")
\ No newline at end of file
{ {
"broker" : "192.168.1.1", "broker" : "192.168.24.209",
"clients_ssh" : [ "clients_ssh" : [
{ {
"hostname" : "192.168.1.1", "hostname" : "192.168.24.18",
"user" : "pi",
"password" : "raspberry"
},
{
"hostname" : "192.168.1.1",
"user" : "pi",
"password" : "raspberry"
},
{
"hostname" : "192.168.1.1",
"user" : "pi",
"password" : "raspberry"
},
{
"hostname" : "192.168.1.1",
"user" : "pi", "user" : "pi",
"password" : "raspberry" "password" : "raspberry"
} }
......
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from paho.mqtt.client import Client from paho.mqtt.client import Client
from tool.confi_reader import read_broker
class MqttClient: class MqttClient:
def __init__(self, client_id: str = None): def __init__(self, client_id: str = None):
self.client = Client(client_id=client_id) self.client = Client(client_id=client_id)
self.client.username_pw_set(username="goyon", password="mosquitto") self.client.username_pw_set(username="goyon", password="mosquitto")
self.client.connect("192.168.24.209", 1883, 60) self.client.connect(
read_broker(str(pathlib.Path(__file__).parent.resolve())+"/config.json"),
1883, 60
)
self.client.loop_start() self.client.loop_start()
def subscribe(self, topic, fun): def subscribe(self, topic, fun):
self.client.subscribe(topic) self.client.subscribe(topic)
self.client.message_callback_add(topic, fun) self.client.message_callback_add(topic, fun)
class RemoteClient:
def __init__(self, hostname, user, password):
self.hostname = hostname
self.user = user
self.password = password
...@@ -3,6 +3,11 @@ Tool class that implement basic interaction that help to finish processes ...@@ -3,6 +3,11 @@ Tool class that implement basic interaction that help to finish processes
""" """
from time import sleep from time import sleep
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.mqtt_client import MqttClient from tool.mqtt_client import MqttClient
......
import os
from pexpect import pxssh from pexpect import pxssh
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.confi_reader import read_ssh
class Cmd: class Cmd:
...@@ -9,21 +17,10 @@ class Cmd: ...@@ -9,21 +17,10 @@ class Cmd:
self.prefix = prefix self.prefix = prefix
class RemoteClient:
def __init__(self, hostname, user, password):
self.hostname = hostname
self.user = user
self.password = password
class SSHClient: class SSHClient:
def __init__(self): def __init__(self):
self.clients = [ self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve())+"/config.json")
RemoteClient("192.168.24.18", "pi", "raspberry")# ,
# RemoteClient("192.168.199.75", "pi", "raspberry")
]
def run_cmd(self, client: int, cmd: list): def run_cmd(self, client: int, cmd: list):
try: try:
......
import paramiko import paramiko
import os import os
from tool.ssh_client import RemoteClient import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.confi_reader import read_ssh
class VersionManager: class VersionManager:
def __init__(self, remote_client: RemoteClient): def __init__(self):
self.client = remote_client self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve())+"/config.json")
def update(self): def update(self):
# TODO: rm previous files # TODO: rm previous files
transport = paramiko.Transport((self.client.hostname, 22)) for client in self.clients:
transport.connect(username=self.client.user, password=self.client.password) transport = paramiko.Transport((client.hostname, 22))
sftp = MySFTPClient.from_transport(transport) transport.connect(username=client.user, password=client.password)
sftp.mkdir("./Desktop/mqtt_goyon/iotamak-core", ignore_existing=True) sftp = MySFTPClient.from_transport(transport)
sftp.put_dir("/mnt/d/work/stage m1/iotamak-core", "./Desktop/mqtt_goyon/iotamak-core") sftp.mkdir("./Desktop/mqtt_goyon/iotamak-core", ignore_existing=True)
sftp.close() sftp.put_dir("/mnt/d/work/stage m1/iotamak-core", "./Desktop/mqtt_goyon/iotamak-core")
sftp.close()
class MySFTPClient(paramiko.SFTPClient): class MySFTPClient(paramiko.SFTPClient):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment