Skip to content

Commit 3d7e262

Browse files
committed
Power cycle the pi for testing timed out issue
1 parent 0bd8fcf commit 3d7e262

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

test/powercycle/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hosts

test/powercycle/control.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- name: Run test
2+
hosts: hosts
3+
gather_facts: false
4+
environment:
5+
PYTHONPATH: /home/pi/Repositories/python-build-hat:/home/pi/.local/lib/python3.9/site-packages/
6+
tasks:
7+
- name: Wait 300 seconds, but only start checking after 60 seconds
8+
wait_for_connection:
9+
delay: 60
10+
timeout: 300
11+
- name: Run experiment
12+
command: python3 /home/pi/Repositories/python-build-hat/test/firmware.py
13+
vars:
14+
ansible_command_timeout: 60 # timeout set to double time firmware.py takes to run
15+
- name: Find logs from experiment
16+
find:
17+
paths: /tmp
18+
patterns: "buildhat-*.log"
19+
register: log_files
20+
- name: Copy
21+
copy:
22+
remote_src: yes
23+
src: "{{item.path}}"
24+
dest: "/home/pi/logs/"
25+
loop: "{{log_files.files}}"
26+
- name: Shutdown machine
27+
command: systemctl poweroff
28+
when: shutdown is defined
29+
become: yes

test/powercycle/restart.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import json
2+
import os
3+
import time
4+
import urllib.request
5+
6+
from ansible import context
7+
from ansible.cli import CLI
8+
from ansible.executor.playbook_executor import PlaybookExecutor
9+
from ansible.inventory.manager import InventoryManager
10+
from ansible.module_utils.common.collections import ImmutableDict
11+
from ansible.parsing.dataloader import DataLoader
12+
from ansible.vars.manager import VariableManager
13+
14+
15+
def runPlaybook(yml, evars):
16+
loader = DataLoader()
17+
context.CLIARGS = ImmutableDict(tags={}, timeout=300, listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh',
18+
module_path=None, forks=100, private_key_file=None, ssh_common_args=None, ssh_extra_args=None,
19+
sftp_extra_args=None, scp_extra_args=None, become=False, become_method='sudo', become_user='root',
20+
verbosity=True, check=False, start_at_task=None, extra_vars=[{**{"ansible_python_interpreter": "/usr/bin/python3"}, **evars}])
21+
inventory = InventoryManager(loader=loader, sources=('hosts',))
22+
variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))
23+
pbex = PlaybookExecutor(playbooks=[yml], inventory=inventory, variable_manager=variable_manager, loader=loader, passwords={})
24+
results = pbex.run()
25+
if results != 0:
26+
raise RuntimeError("Playbook failed to run correctly")
27+
28+
def switch(plug, on):
29+
if on:
30+
state = "On"
31+
else:
32+
state = "Off"
33+
api_url = f"http://{plug}/cm?cmnd=Power%20{state}"
34+
request = urllib.request.Request(api_url)
35+
with urllib.request.urlopen(request) as response:
36+
data = json.loads(response.read().decode("utf-8"))
37+
if data['POWER'] != state.upper():
38+
raise Exception("Failed to set power", data['POWER'])
39+
40+
if __name__ == "__main__":
41+
42+
plug = os.getenv('TASMOTA')
43+
if plug is None:
44+
raise Exception("Need to set TASMOTA env variable")
45+
46+
COUNT = 3
47+
for i in range(COUNT):
48+
print(f"At {i}")
49+
if i < COUNT - 1:
50+
runPlaybook("control.yml", {"shutdown" : True})
51+
time.sleep(20)
52+
switch(plug, False)
53+
time.sleep(5)
54+
switch(plug, True)
55+
else:
56+
runPlaybook("control.yml", {})

0 commit comments

Comments
 (0)