-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
236 lines (196 loc) · 8.42 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os
import time
import json
import boto3
from botocore import exceptions as botoexcept
import string
import random
import threading
import sys
import values
import shutil
from getpass import getpass
os.system("cls")
def strgen(size=34, chars=string.ascii_uppercase + string.digits + string.ascii_lowercase):
return ''.join(random.choice(chars) for _ in range(size))
class Spinner:
busy = False
delay = 0.1
@staticmethod
def spinning_cursor():
while 1:
for cursor in '|/-\\': yield cursor
def __init__(self, delay=None):
self.spinner_generator = self.spinning_cursor()
if delay and float(delay): self.delay = delay
def spinner_task(self):
while self.busy:
sys.stdout.write(next(self.spinner_generator))
sys.stdout.flush()
time.sleep(self.delay)
sys.stdout.write('\b')
sys.stdout.flush()
def __enter__(self):
self.busy = True
threading.Thread(target=self.spinner_task).start()
def __exit__(self, exception, value, tb):
self.busy = False
time.sleep(self.delay)
if exception is not None:
return False
print("\033[42mPlaybox CLI V1.0\033[0m")
while True:
cinput = input("$ ")
try:
command, args = cinput.split(" ", 1)
except ValueError:
command = cinput
if command.lower() == "ip":
try:
with open(f"boxes/{args}/box.json") as f:
boxdata = json.load(f)
ip = boxdata["ipadr"]
print(f"\033[42m{args}\033[0m IP address: {ip}")
except FileNotFoundError:
print(f"\033[91mBox '{args}' doesnt exist\033[0m")
except NameError:
print(f"\033[91minvalid number of arguments\033[0m")
elif command.lower() == "list":
print([name for name in os.listdir("boxes") if os.path.isdir(os.path.join("boxes", name))])
elif command.lower() == "session":
try:
with open(f"boxes/{args}/box.json") as file:
boxdata = json.load(file)
ip = boxdata["ipadr"]
ssh_session = f'ssh -o StrictHostKeyChecking=no -T -t -t -i "boxes/{args}/key.pem" ubuntu@{ip} "tmux attach-session -t server01"'
os.system(ssh_session)
except FileNotFoundError:
print(f"\033[91mBox '{args}' doesnt exist\033[0m")
except NameError:
print(f"\033[91minvalid number of arguments\033[0m")
elif command.lower() == "upload":
try:
BoxName, FilePath = args.split(" ", 1)
with open(f"boxes/{BoxName}/box.json") as file:
boxdata = json.load(file)
ip = boxdata["ipadr"]
scp_session = f'scp -o StrictHostKeyChecking=no -i "boxes/{BoxName}/key.pem" {FilePath} ubuntu@{ip}:/home/ubuntu/server01'
os.system(scp_session)
except FileNotFoundError:
print(f"\033[91mBox '{BoxName}' doesnt exist\033[0m")
continue
except ValueError:
print(f"\033[91mInvalid number of arguments\033[0m")
continue
except NameError:
print(f"\033[91mInvalid number of arguments\033[0m")
elif command.lower() == "kill":
BoxName = args
invalidboxname = os.path.isdir(f"boxes/{args}")
if invalidboxname == False:
print(f"\033[91mBox '{args}' doesnt exist\033[0m")
continue
YorN = input(f'\033[97;41mWARNING, YOUR ABOUT TO DELETE "{args}". KILLING A BOX WILL DELETE EVERYING AND SHUT DONW THE SERVER. ARE YOU SURE (Y/N)\033[0m ').lower()
if YorN == "n":
print("Canceling...")
continue
elif YorN == 'y':
session = boto3.session.Session()
ec2 = session.client(
service_name="ec2",
aws_access_key_id=values.aws_access_key_id,
aws_secret_access_key=values.aws_secret_access_key,
region_name=values.region_name
)
with open(f"boxes/{BoxName}/box.json", "r") as f:
boxdata = json.load(f)
InstanceId = boxdata["instanceid"]
try:
response = ec2.terminate_instances(
InstanceIds=[InstanceId]
)
except:
print("\033[91mError deleting. Deleteing box data anyways. You might have to delete the EC2 instance manually.\033[0m")
shutil.rmtree(f'boxes/{BoxName}')
print(f"The deed is done. {BoxName} has been fully deleted")
continue
else:
print("Recived invalid input. Canceling...")
continue
# Syntax: create <Box Name> <Instance Type> <Server Type>
# Box Name: Must be unique.
# Instance Type: Must be a ARM type architecture (i.e. t4g.large).
# Server Type: Must be ether Vanilla or Spigot. More coming soon.
elif command.lower() == "create":
try:
BoxName, InstanceType, ServerType = args.split(" ", 2)
except (NameError, ValueError) as e:
print(f"\033[91mInvalid number of arguments\033[0m")
continue
InvalidBoxName = os.path.isdir(f"boxes/{BoxName}")
if InvalidBoxName != False:
print(f"\033[91mBox '{BoxName}' already exists\033[0m")
continue
session = boto3.session.Session()
ec2 = session.client(
service_name="ec2",
aws_access_key_id=values.aws_access_key_id,
aws_secret_access_key=values.aws_secret_access_key,
region_name=values.region_name
)
keyname = strgen()
keyresponse = ec2.create_key_pair(
KeyName=keyname,
)
os.mkdir(f"boxes/{BoxName}")
with open(f"boxes/{BoxName}/key.pem", "w+") as keyfile:
keyfile.write(keyresponse['KeyMaterial'])
keyfile.close()
try:
instance = ec2.run_instances(
ImageId='ami-0acb327475c6fd498', # Replace with the desired AMI ID
InstanceType=InstanceType,
MinCount=1,
MaxCount=1,
KeyName=keyname,
NetworkInterfaces=[
{
'DeviceIndex': 0,
'AssociatePublicIpAddress': True,
}
]
)
except botoexcept.ClientError:
print(f"\033[91mInstance '{InstanceType}' doesn't exist\033[0m")
shutil.rmtree(f'boxes/{BoxName}')
continue
print("Waiting for instance to start running...")
with Spinner():
InstanceId = instance["Instances"][0]["InstanceId"]
state = "pending"
while state == "pending":
descinstance = ec2.describe_instances(
InstanceIds=[InstanceId]
)
time.sleep(2)
state = descinstance["Reservations"][0]["Instances"][0]["State"]["Name"]
time.sleep(15)
ip = descinstance["Reservations"][0]["Instances"][0]["PublicIpAddress"]
with open(f"boxes/{BoxName}/box.json", "w+") as boxdata:
boxdata.write('{ "ipadr": "' + ip + '", "instanceid": "' + InstanceId + '" }')
boxdata.close()
print("Successfully launched EC2 instance")
if ServerType.lower() == "vanilla":
upload_setup = f'scp -o StrictHostKeyChecking=no -i "boxes/{BoxName}/key.pem" scripts/vanillaserver.sh ubuntu@{ip}:/home/ubuntu'
ssh_session = f'ssh -o StrictHostKeyChecking=no -T -t -t -i "boxes/{BoxName}/key.pem" ubuntu@{ip} "sh vanillaserver.sh"'
elif ServerType.lower() == "spigot":
upload_setup = f'scp -o StrictHostKeyChecking=no -i "boxes/{BoxName}/key.pem" scripts/spigotserver.sh ubuntu@{ip}:/home/ubuntu'
ssh_session = f'ssh -o StrictHostKeyChecking=no -T -t -t -i "boxes/{BoxName}/key.pem" ubuntu@{ip} "sh spigotserver.sh"'
else:
print(f"\033[91m'{ServerType}' is a invalid server type\033[0m")
shutil.rmtree(f'boxes/{BoxName}')
continue
os.system(upload_setup)
os.system(ssh_session)
else:
print(f"\033[91m{command} is a invalid command\033[0m")