I have following
- A repository which contains a code(python) to be executed
- A VM at Google Cloud (debian/ubuntu)
- A scheduler + cloud function to start VM every hour
What I require
- I want to run the code present in repository which will be pulled every hour and runs 15-25 mins and finally after its execution the machine shuts down.
What I already tried:
Using Crontab in machine.
- used
@reboot run_my_script && shutdown -h now
- Once this is implemented I am not able to access VM anymore because at every start the vm runs the script and shut itself down.
This method not works Example what if my code runs to error or my system requires new dependency
Syncing Cloud scheduler and Crontab
- used
5 * * * * run_my_script && shutdown -h now
- Cloud scheduler is set to start my vm at every hour exactly at hour:00:00
- Crontab starts running the code at hour:05:00 and shutdown after completion (5 minutes to start vm)
This method is running fine But I require a more robust solution(Industry accepted way)
Code Snippets
VM
Name
test_vm
Crontab
5 * * * * sh /home/danish_bansal/workflow.sh
workflow.sh:
#!/bin/sh
sudo rm -r repoName/ || true
sudo git clone https://<token>@github.com/Repo/repoName.git
cd repoName
/usr/bin/python3 Script.py
sudo shutdown -h now
Cloud Scheduler
At 0 * * * * calls cloud function
Cloud Function
from googleapiclient import discovery
def startInstance(r):
service = discovery.build('compute', 'v1')
print('VM Instance starting')
# Project ID for this request.
project = 'project-name'
# The name of the zone for this request.
zone = 'us-central1-a'
# Name of the instance resource to start.
instance = 'test-vm'
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()
print(response,'VM Instance started')