Add id
authorPeng Li <seudut@gmail.com>
Fri, 22 Jun 2018 05:57:28 +0000 (13:57 +0800)
committerPeng Li <seudut@gmail.com>
Fri, 22 Jun 2018 05:57:28 +0000 (13:57 +0800)
mqtt-client.py
mqtt-daemon.py
suanzi-support.service [new file with mode: 0644]

index 98215fc..cbc7cd4 100644 (file)
@@ -4,40 +4,25 @@ from subprocess import call, Popen, PIPE, STDOUT
 import paho.mqtt.client as mqtt
 import argparse
 
-ID = 'rpdzkj'
-MQTT_SERVER = 'mqtt.suanzi.zi'
+MQTT_SERVER = 'mqtt.suanzi.ai'
 MQTT_PORT = 1883
 
-SSH_SERVER = 'autossh.suanzi.ai'
-PORT_RANGE = (20000, 30000)
-USER = 'autossh'
-PASSWORD = 'hard2guess'
-#
-#def run_ssh (port):
-#    pass
-#
-#
-#class MyClient(mqtt.Client):
-#    def __init__(self):
-#
-#
-#
-def on_connect(client, userdata, flags, rc):
-    client.subscribe("rpdzkj-response")
-    print("Connected with result code "+str(rc))
-    client.publish('rpdzkj-request', 33)
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Choose an avaiable port and run autossh.')
+    parser.add_argument('-i', '--id', dest='id', help='the unique id of remote machine, such as rpdzkj-080027a6f8dc, where the rpdzkj is the user, and 080027a6f8dc is the mac addres', required=True)
+    args = parser.parse_args()
+    id = args.id
 
-def on_message(client, userdata, msg):
-    print(msg.topic+" "+str(msg.payload))
-    if msg.topic == 'rpdzkj-rsponse':
-        print str(msg.payload)
+    def on_connect(client, userdata, flags, rc):
+        client.subscribe(id + "-response")
+        print("Connected with result code "+str(rc))
+        client.publish(id + '-request', 0)
+
+    def on_message(client, userdata, msg):
+        print(msg.topic+" "+str(msg.payload))
+        if msg.topic == id + '-response':
+            print 'port is', str(msg.payload)
 
-if __name__ == '__main__':
-#    parser = argparse.ArgumentParser(description='Choose an avaiable port and run autossh.')
-#    parser.add_argument('-i', '--id', dest='id', help='board unique id, rpdzkj|firefly', required=True)
-#    args = parser.parse_args()
-#    id = args.id
-#
     client = mqtt.Client()
     client.on_connect = on_connect
     client.on_message = on_message
index a06914a..41ed9fc 100644 (file)
@@ -7,16 +7,16 @@
 from subprocess import call, Popen, PIPE, STDOUT
 import paho.mqtt.client as mqtt
 import shlex
+import os
 import random
 from uuid import getnode as get_mac
 
-ID = 'rpdzkj'
-MQTT_SERVER = 'mqtt.suanzi.zi'
+MQTT_SERVER = 'mqtt.suanzi.ai'
 MQTT_PORT = 1883
 ALIVE_TIME = 60 * 5
 
 SSH_SERVER = 'autossh.suanzi.ai'
-PORT_RANGE = (20000, 20100)
+PORT_RANGE = (20000, 30000)
 USER = 'autossh'
 PASSWORD = 'hard2guess'
 
@@ -41,23 +41,33 @@ def exec_ssh(port):
     print command
     return call(shlex.split(command), shell=False)
 
+
 def on_connect(client, userdata, flags, rc):
-    client.subscribe("rpdzkj-request")
+    #client.subscribe("rpdzkj-request")
+    client.subscribe(client.id + '-request')
     print("Connected with result code "+str(rc))
 
 def on_message(client, userdata, msg):
     print(msg.topic+" "+str(msg.payload))
-    if msg.topic == 'rpdzkj-request':
+    if msg.topic == client.id + '-request':
         print str(msg.payload)
         port = getAvailablePort(SSH_SERVER, PORT_RANGE)
         if exec_ssh(port) == 0:
-            client.publish('rpdzkj-response', port)
+            client.publish(client.id + '-response', port)
         else:
             raise Exception ('run ssh failed')
 
+class MyClient(mqtt.Client):
+    def __init__(self, id):
+        mqtt.Client.__init__(self)
+        self.id = id
+        self.on_connect = on_connect
+        self.on_message = on_message
+
 if __name__ == '__main__':
-    client = mqtt.Client()
-    client.on_connect = on_connect
-    client.on_message = on_message
+    mac = hex(get_mac())
+    id = os.getenv('USER') + '-' + '{:0>12}'.format(mac[2:13])
+    print 'id is: ' , id
+    client = MyClient(id)
     client.connect(MQTT_SERVER, MQTT_PORT, 60)
     client.loop_forever()
diff --git a/suanzi-support.service b/suanzi-support.service
new file mode 100644 (file)
index 0000000..16531f3
--- /dev/null
@@ -0,0 +1,16 @@
+[Unit]
+Description= suanzi support daemon
+After=network-online.target
+
+[Service]
+User=debian
+Type=simple
+ExecStart=/usr/local/bin/autossh-wrapper.py -u 'autossh' autossh.suanzi.ai
+ExecStop=/usr/bin/pkill -P $MAINPID
+KillMode=process
+Restart=always
+RestartSec=60
+
+[Install]
+WantedBy=multi-user.target
+