user:kluong:feature_idea_-_staging_environment

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
user:kluong:feature_idea_-_staging_environment [2021/10/21 13:48]
kluong
user:kluong:feature_idea_-_staging_environment [2021/10/23 17:41]
kluong [Gateway - testing changes]
Line 182: Line 182:
 **Note** - be sure to take care when using sudo! This gives you access to do a lot of things to the existing system, including removing system files that you normally wouldn'​t be able to. **Note** - be sure to take care when using sudo! This gives you access to do a lot of things to the existing system, including removing system files that you normally wouldn'​t be able to.
  
-+To check the users, use the ''​%%\du%%''​ command in postgres:
- +
-=== Populating the tables === +
- +
-Okay, now the database and user are setup, but we'll also need to setup the tables for the database. There'​s a schema file in the control_tower repo under the db/ folder that will let you do this. +
- +
-Make sure the current directory you're in is the control_tower repo. Here's the command you can run as the postgres user: +
- +
-<​code>​ +
-psql -U control_tower_staging -d control_tower_staging -f db/​multi-table.sql +
-</​code>​ +
- +
-After this is done, this is all the database configuration you'll need to do. There are some additional gateway changes described below. +
- +
-You can check that the tables were created properly by running the following command: +
- +
-<​code>​ +
-psql -U control_tower_staging -d control_tower_staging -c "SHOW TABLES"​ +
-</​code>​ +
- +
-To check the users, use the ''​%%\du+%%''​ command in postgres:+
  
 <​code>​ <​code>​
Line 216: Line 196:
 </​code>​ </​code>​
  
-To check the databases, use the `\lcommand in postgres:+To check the databases, use the ''​%%\l%%'' ​command in postgres:
  
 <​code>​ <​code>​
Line 241: Line 221:
  
  
-==== Gateway ​Changes ​====+ 
 +=== Populating the tables === 
 + 
 +Okay, now the database and user are setup, but we'll also need to setup the tables for the database. There'​s a schema file in the control_tower repo under the db/ folder that will let you do this. 
 + 
 +Make sure the current directory you're in is the control_tower repo. Here's the command you can run as the postgres user: 
 + 
 +<​code>​ 
 +psql -U control_tower_staging -d control_tower_staging -f db/​multi-table.sql 
 +</​code>​ 
 + 
 +After this is done, this is all the database configuration you'll need to do. There are some additional gateway changes described below. 
 + 
 +You can check that the tables were created properly by running the following command: 
 + 
 +<​code>​ 
 +psql -U control_tower_staging -d control_tower_staging -c "​SELECT * FROM pg_catalog.pg_tables WHERE schemaname='​public'"​ 
 +</​code>​ 
 + 
 + 
 + 
 +==== Gateway ​Change - ability to pass in a database URI ====
  
 I mentioned earlier we'll have to change the way the gateway works. This is the way the current code connects to the database: I mentioned earlier we'll have to change the way the gateway works. This is the way the current code connects to the database:
Line 260: Line 261:
 </​code>  ​ </​code>  ​
  
-We can also use a URI instead of passing in the arguments in the way we did previously. A shell script that calls the gateway would look something like this:+We can also use a URI instead of passing in the arguments in the way we did previously. A shell script that calls the gateway would look something like this for staging:
  
 <​code>​ <​code>​
 #!/bin/bash #!/bin/bash
  
-GATEWAY_DB_URI="​postgresql://​control_tower_staging@localhost/​control_tower_staging"​ python gateway.py+# setup the python env 
 +source ./​env/​bin/​activate  
 + 
 +export ​GATEWAY_DB_URI="​postgresql://​control_tower_staging@localhost/​control_tower_staging"​ 
 +python gateway.py ​/​dev/​serial/​by-id/​usb-FTDI_FT231X_USB_UART_DN01DS3L-if00-port0
 </​code>​ </​code>​
  
-To get this to work in production, you would have to modify the run_production.sh script, and also update the repository ​in production.+You'd need to see some RX frames on the tty device to test this properly though, since the gateway doesn'​t reach out to the database unless it sees a message to decode.  
 + 
 +To get this to work in production, you would have to modify the run_production.sh script, and also update the repository production. 
 + 
 + 
 +==== Gateway - testing changes ==== 
 +  
 +Options for testing 
 + 
 +  * Deploy to production - see what happens 
 +  * Deploy to staging - see what happens 
 +  * Setup an personal network (on your laptop, or maybe on the server) 
 +  * Connect the gateway to a "​fake"​ xbee virtually, using a pty and another script 
 +  * Mock out the serial device completely in python 
 + 
 +It's not necessarily practical to have hardware to test with all of the time and often hardware might not be available anyways. Luckily, with software we can work around this.  
 + 
 +**Note: development for the gateway should generally be done in a linux-based environment.** 
 + 
 +You can make a "​fake"​ xbee network using the following python script: 
 + 
 +<​code>​ 
 +import os 
 +import errno 
 +import time 
 + 
 +def symlink_force(target,​ link_name):​ 
 +    try: 
 +        os.symlink(target,​ link_name) 
 +    except OSError as e: 
 +        if e.errno == errno.EEXIST:​ 
 +            os.remove(link_name) 
 +            os.symlink(target,​ link_name) 
 +        else: 
 +            raise e 
 + 
 +def valid_packets():​ 
 +    packets = {} 
 +    packets['​heartbeat'​] = "​\x7e\x00\x16\x90\x00\x7d\x33\xa2\x00\x40\xe6\x4b\x5e\x03\xfd\x01\x00\x00\xff\xff\xf0\xfa\x23\x00\x2b\x02\xb2"​ 
 +    packets['​apple'​] = "​\x7e\x00\x22\x90\x00\x7d\x33\xa2\x00\x40\x9f\x27\xa7\x29\x6c\x01\x01\x00\xff\xff\x80\x6f\x69\x3d\x06\x0f\x71\x7d\x33\x5a\x8a\x01\x00\x76\x01\x22\x00\x6e\x09\x55"​ 
 +    packets['​cranberry'​] = "​\x7e\x00\x22\x90\x00\x7d\x33\xa2\x00\x41\x25\xe5\x88\x0c\x83\x01\x02\x00\xff\xff\x7c\xf3\x05\x00\xba\x0f\x5c\x08\x05\x00\x20\x73\x3b\x00\xdd\x8b\x01\x00\x7a"​ 
 +    packets['​dragonfruit'​] = "​\x7e\x00\x24\x90\x00\x7d\x33\xa2\x00\x40\xe6\x72\x7d\x5e\x30\x18\x01\x03\x00\xff\xff\x30\xc8\x07\x00\x6b\x0d\xf4\x00\x06\x00\x00\x00\xb6\x72\x37\x00\xfe\x8b\x01\x00\x00"​ 
 +    packets['​snapdragon'​] = "​\x7e\x00\x22\x90\x00\x7d\x33\xa2\x00\x40\xa3\x53\x7d\x5e\x20\x9c\x01\x04\x00\xff\xff\x12\xe4\x49\x00\xca\x0d\x44\x0c\x2c\x31\x01\x00\x2f\x01\x34\x00\x64\x00\xbb"​ 
 + 
 +    return packets 
 + 
 +master_fd, slave_fd = os.openpty() 
 +symlink_force(os.ttyname(slave_fd),​ '/​tmp/​fakexbee'​) 
 + 
 + 
 +packets = valid_packets() 
 +while True: 
 +    for key in packets: 
 +        os.write(master_fd,​ bytes(packets[key])) 
 +        time.sleep(1) 
 + 
 +</​code>​ 
 + 
  • user/kluong/feature_idea_-_staging_environment.txt
  • Last modified: 2021/10/23 17:42
  • by kluong