annotate gcp_batch_netcat.py @ 3:0ea626b10557 draft

planemo upload for repository https://github.com/afgane/gcp_batch_netcat
author enis
date Mon, 21 Jul 2025 15:13:09 +0000
parents 79160beab2a4
children 2ff4a39ea41b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
1
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
2 import json
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
3 import subprocess
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
4 import argparse
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
5 import uuid
2
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
6 import time
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
7 import os
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
8
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
9 def main():
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
10 parser = argparse.ArgumentParser()
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
11 parser.add_argument('--nfs_address', required=True)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
12 parser.add_argument('--output', required=True)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
13 parser.add_argument('--project', required=True)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
14 parser.add_argument('--region', required=True)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
15 parser.add_argument('--port', default='2049')
2
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
16 parser.add_argument('--service_account_key', required=True)
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
17 args = parser.parse_args()
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
18
2
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
19 # Set up authentication using the service account key
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
20 os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = args.service_account_key
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
21
3
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
22 # Ensure gcloud uses a writable config directory
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
23 os.environ['CLOUDSDK_CONFIG'] = '/tmp/gcloud-config'
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
24
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
25 # Create the temp config directory if it doesn't exist
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
26 os.makedirs('/tmp/gcloud-config', exist_ok=True)
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
27
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
28 job_name = f'netcat-job-{uuid.uuid4()}'
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
29
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
30 job_spec = {
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
31 "taskGroups": [
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
32 {
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
33 "taskSpec": {
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
34 "runnables": [
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
35 {
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
36 "script": {
3
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
37 "text": f"/usr/bin/nc -z -v {args.nfs_address} {args.port}"
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
38 }
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
39 }
3
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
40 ],
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
41 "computeResource": {
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
42 "cpuMilli": 1000,
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
43 "memoryMib": 1024
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
44 },
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
45 "environment": {
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
46 "imageUri": "afgane/gcp-batch-netcat:0.1.0"
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
47 }
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
48 },
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
49 "taskCount": 1,
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
50 "parallelism": 1
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
51 }
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
52 ],
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
53 "logsPolicy": {
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
54 "destination": "CLOUD_LOGGING"
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
55 }
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
56 }
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
57
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
58 job_spec_file = 'job.json'
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
59 with open(job_spec_file, 'w') as f:
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
60 json.dump(job_spec, f)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
61
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
62 command = [
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
63 'gcloud', 'batch', 'jobs', 'submit', job_name,
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
64 '--location', args.region,
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
65 '--project', args.project,
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
66 '--config', job_spec_file,
3
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
67 '--format=text',
0ea626b10557 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 2
diff changeset
68 '--verbosity=debug'
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
69 ]
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
70
2
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
71 # Wait 4 minutes before submitting the job
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
72 time.sleep(240)
79160beab2a4 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents: 0
diff changeset
73
0
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
74 try:
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
75 result = subprocess.run(command, capture_output=True, text=True, check=True)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
76 with open(args.output, 'w') as f:
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
77 f.write("Job output:\n")
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
78 f.write(result.stdout)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
79 f.write(result.stderr)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
80 except subprocess.CalledProcessError as e:
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
81 with open(args.output, 'w') as f:
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
82 f.write("Error submitting job:\n")
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
83 f.write(e.stderr)
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
84
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
85 if __name__ == '__main__':
7852c0efafd7 planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff changeset
86 main()