Mercurial > repos > enis > gcp_batch_netcat
annotate gcp_batch_netcat.py @ 2:79160beab2a4 draft
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
author | enis |
---|---|
date | Tue, 15 Jul 2025 21:10:05 +0000 |
parents | 7852c0efafd7 |
children | 0ea626b10557 |
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 |
0
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
22 job_name = f'netcat-job-{uuid.uuid4()}' |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
23 |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
24 job_spec = { |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
25 "taskGroups": [ |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
26 { |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
27 "taskSpec": { |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
28 "runnables": [ |
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 "script": { |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
31 "text": f"nc -z -v {args.nfs_address} {args.port}" |
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 } |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
34 ] |
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 "taskCount": 1, |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
37 "parallelism": 1 |
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 ], |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
40 "logsPolicy": { |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
41 "destination": "CLOUD_LOGGING" |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
42 } |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
43 } |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
44 |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
45 job_spec_file = 'job.json' |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
46 with open(job_spec_file, 'w') as f: |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
47 json.dump(job_spec, f) |
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 command = [ |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
50 'gcloud', 'batch', 'jobs', 'submit', job_name, |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
51 '--location', args.region, |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
52 '--project', args.project, |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
53 '--config', job_spec_file, |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
54 '--format=text' |
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 |
2
79160beab2a4
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
0
diff
changeset
|
57 # Wait 4 minutes before submitting the job |
79160beab2a4
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
0
diff
changeset
|
58 time.sleep(240) |
79160beab2a4
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
0
diff
changeset
|
59 |
0
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
60 try: |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
61 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
|
62 with open(args.output, 'w') as f: |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
63 f.write("Job output:\n") |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
64 f.write(result.stdout) |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
65 f.write(result.stderr) |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
66 except subprocess.CalledProcessError as e: |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
67 with open(args.output, 'w') as f: |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
68 f.write("Error submitting job:\n") |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
69 f.write(e.stderr) |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
70 |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
71 if __name__ == '__main__': |
7852c0efafd7
planemo upload for repository https://github.com/afgane/gcp_batch_netcat
enis
parents:
diff
changeset
|
72 main() |