-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangetf.py
98 lines (78 loc) · 3.86 KB
/
changetf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# This file takes input from a flask app, and modifies the Terraform main.tf files in both the NMapLab and PacketLab resource groups,
# and deletes the terraform.tfstate files located in those folders in order to spool up new instances of resource groups
import os
import time
import subprocess
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/api/submit', methods=['POST'])
def get_data_from_react():
data = request.get_json()
clone_instances(data["data"])
return data
if __name__ == '__main__':
app.run()
def clone_instances(data_from_site):
numStudents = data_from_site
resourceGroupNum = 1
cwd = os.getcwd()
for x in range(numStudents):
#############################################################################
# CHANGE THIS PATH TO YOUR PACKETLAB 'main.tf' DIRECTORY #
#############################################################################
with open(cwd + r'\PacketLab\main.tf', 'r+') as f: #r+ does the work of rw
lines = f.readlines()
for i, line in enumerate(lines):
if line.strip().startswith('name'):
x = line.split('"')
if int(resourceGroupNum) == 1:
x[1] += str(resourceGroupNum)
else:
newx = x[1].replace(str(int(resourceGroupNum - 1)), str(resourceGroupNum))
x[1] = newx
resourceGroupNum = int(resourceGroupNum) + 1
newline = '"'.join(x)
lines[i] = newline
f.seek(0)
for line in lines:
f.write(line)
f.close()
#############################################################################
# CHANGE THIS PATH TO YOUR PACKETLAB DIRECTORY #
#############################################################################
os.chdir(cwd + r'\PacketLab')
os.system("terraform init")
os.system("terraform apply -auto-approve")
os.system("del /f terraform.tfstate")
os.system("del /f terraform.tfstate.backup")
resourceGroupNum = 1
#############################################################################
# CHANGE THIS PATH TO YOUR MOODLEDEV 'main.tf' DIRECTORY #
#############################################################################
with open(cwd + r'\NMapLab\main.tf', 'r+') as f: #r+ does the work of rw
lines = f.readlines()
for i, line in enumerate(lines):
if line.strip().startswith('name'):
x = line.split('"')
if int(resourceGroupNum) == 1:
x[1] += str(resourceGroupNum)
else:
newx = x[1].replace(str(int(resourceGroupNum - 1)), str(resourceGroupNum))
x[1] = newx
resourceGroupNum = int(resourceGroupNum) + 1
newline = '"'.join(x)
lines[i] = newline
f.seek(0)
for line in lines:
f.write(line)
f.close()
#############################################################################
# CHANGE THIS PATH TO YOUR NMAPLAB DIRECTORY #
#############################################################################
os.chdir(cwd + r'\NMapLab')
#os.system("terraform init")
#os.system("terraform apply -auto-approve")
#os.system("del /f terraform.tfstate")
#os.system("del /f terraform.tfstate.backup")