First attempt at docker configuration

This commit is contained in:
Patrick McDonagh
2016-10-12 19:05:52 -05:00
parent 5e5cfde3cf
commit 069c9a9915
4 changed files with 152 additions and 0 deletions

29
Dockerfile Normal file
View File

@@ -0,0 +1,29 @@
FROM node:latest
RUN apt-get -y update && apt-get install -y apt-utils dialog vim
COPY mysql-install.sh /tmp/mysql-install.sh
RUN chmod +x /tmp/mysql-install.sh && /tmp/mysql-install.sh
RUN apt-get install -y python git-core wget
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python get-pip.py
RUN pip install requests
RUN git clone https://github.com/ruscito/pycomm.git
RUN cd pycomm && python setup.py install && cd ..
RUN mkdir /root/tag-logger
COPY init /root/tag-logger/init
COPY tag /root/tag-logger/tag
COPY www /root/tag-logger/www
COPY taglogger.py /root/tag-logger/taglogger.py
COPY startup.sh /root/startup.sh
RUN chmod +x /root/startup.sh
RUN npm install -g bower pm2 sails
RUN cd /root/tag-logger/www && npm install && bower install --allow-root && cd /
CMD '/root/startup.sh'

14
mysql-install.sh Normal file
View File

@@ -0,0 +1,14 @@
#!/bin/sh
DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server && apt-get clean
/usr/sbin/mysqld &
sleep 5
echo "mysqld_safe &" > /tmp/config && \
echo "mysqladmin --silent --wait=30 ping || exit 1" >> /tmp/config && \
echo "mysql -e 'GRANT ALL PRIVILEGES ON *.* TO \"root\"@\"%\" WITH GRANT OPTION;'" >> /tmp/config && \
bash /tmp/config && \
rm -f /tmp/config
echo "CREATE USER 'website'@'localhost' IDENTIFIED BY 'henrypump';GRANT ALL ON *.* TO 'website'@'localhost';CREATE USER 'admin'@'localhost' IDENTIFIED BY 'henrypump';GRANT ALL ON *.* to 'admin'@'localhost';CREATE USER 'admin'@'%' IDENTIFIED BY 'henrypump';GRANT ALL ON *.* to 'admin'@'%';FLUSH PRIVILEGES;CREATE DATABASE poconsole" | mysql

3
startup.sh Normal file
View File

@@ -0,0 +1,3 @@
service mysql start
sleep 5
node /root/tag-logger/www/app.js

106
taglogger.py Normal file
View File

@@ -0,0 +1,106 @@
#!/usr/bin/env python
'''
MySQL Tag Server
Created on April 7, 2016
@author: Patrick McDonagh
@description: Continuously loops through a list of tags to store values from a PLC into a MySQL database
'''
from tag.tag import Tag
import traceback
import time
import requests
import json
# DEFAULTS
web_address = "https://localhost:3000"
scan_rate = 30 # seconds
save_all = "test" # use True, False, or any string
plc_handshake_tags = {}
last_handshake_time = 0
tag_store = {}
device_types = {}
def main():
global web_address, scan_rate, save_all, tag_store, device_types, plc_handshake_tags, last_handshake_time
try:
# Get tags stored in database
get_tag_request_data = {'where': '{"tag_class": 5}'}
get_tag_request = requests.get('{}/tag'.format(web_address), params=get_tag_request_data, verify=False)
tags = json.loads(get_tag_request.text)
except Exception, e:
print("Error getting tags: {}".format(e))
time.sleep(10)
main()
try:
# Get tags stored in database
get_device_type_request = requests.get('{}/device_type'.format(web_address), verify=False)
device_types_json = json.loads(get_device_type_request.text)
for t in device_types_json:
device_types[t['id']] = t['dType']
except Exception, e:
print("Error getting tags: {}".format(e))
time.sleep(10)
main()
try:
sr_req_data = 'where={"parameter": "scan_rate"}'
sr_req = requests.get('{}/config?{}'.format(web_address, sr_req_data), verify=False)
sr_try = json.loads(sr_req.text)
if len(sr_try) > 0:
scan_rate = int(sr_try[0]['val'])
except Exception, e:
print("Error getting scan rate: {}".format(e))
print("I'll just use {} seconds as the scan rate...".format(scan_rate))
try:
sa_req_data = {"where": {"parameter": "save_all"}}
sa_req = requests.get('{}/config'.format(web_address), params=sa_req_data, verify=False)
sa_try = json.loads(sa_req.text)
if len(sa_try) > 0:
if sa_try[0]['val'].lower() == "true":
save_all = True
elif sa_try[0]['val'].lower() == "false":
save_all = False
except Exception, e:
print("Error getting save-all: {}".format(e))
print("I'll just use {} as the save-all parameter...".format(save_all))
try:
# Get tags stored in database
get_hs_request_data = {'where': '{"tag_class": 6}'}
get_hs_request = requests.get('{}/tag'.format(web_address), params=get_hs_request_data, verify=False)
hs_tags = json.loads(get_hs_request.text)
if len(hs_tags) > 0:
for hs in hs_tags:
plc_handshake_tags[hs['name']] = Tag(hs['name'], hs['tag'], hs['id'], hs['data_type'], hs['change_threshold'], hs['guarantee_sec'], mapFn=hs['map_function'], ip_address=hs['deviceID']['address'], device_type=device_types[hs['deviceID']['device_type']])
except Exception, e:
print("Error getting handshake tags: {}".format(e))
for t in tags:
# name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, device_type='CLX', ip_address='192.168.1.10'):
tag_store[t['name']] = Tag(t['name'], t['tag'], t['id'], t['data_type'], t['change_threshold'], t['guarantee_sec'], mapFn=t['map_function'], ip_address=t['deviceID']['address'], device_type=device_types[t['deviceID']['device_type']])
while True:
for tag in tag_store:
try:
tag_store[tag].read('test')
except:
print("ERROR EVALUATING {}".format(tag))
traceback.print_exc()
if plc_handshake_tags:
if time.time() - last_handshake_time > 30.0:
for hs_tag in plc_handshake_tags:
plc_handshake_tags[hs_tag].write(1)
print("Handshake with {} - {}".format(plc_handshake_tags[hs_tag].address, hs_tag))
last_handshake_time = time.time()
time.sleep(scan_rate)
if __name__ == '__main__':
main()