forked from bodz1lla/aws-instance-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_run_instance.py
executable file
·93 lines (78 loc) · 2.75 KB
/
aws_run_instance.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
#!/usr/bin/env python3
import sys
import time
from flask import Flask, request
import json, boto3
import paramiko
import crypt
port = 8080
ami_id = 'ami-3e713f4d'
keyname = 'yourkey.pem'
instance_type = 't2.micro'
ec2 = boto3.resource('ec2')
def createvm(credentials):
data = {}
instance_id = ec2.create_instances(
ImageId = ami_id,
MinCount = 1,
MaxCount = 1,
KeyName = keyname,
InstanceType = instance_type,
)[0].id
instance = ec2.Instance(instance_id)
instance.wait_until_running()
data['instance_id'] = instance_id
data['instance_ip'] = instance.public_ip_address
return data
def adduser(connection,username,password,sudo=False):
shadow_password = crypt.crypt(password, crypt.mksalt(crypt.METHOD_SHA512))
print("Adding user...")
command = "sudo useradd -m " + username + " -p " + shadow_password
return connection.exec_command(command)
def connect(host):
i = 0
key = paramiko.RSAKey.from_private_key_file("~/.ssh/" + keyname)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
while True:
print("Trying to connect to {} ({})".format(host, i))
try:
ssh.connect(hostname = host, username = 'ubuntu', pkey = key)
print("Connected to {}".format(host))
print("Create a user ...".format(host))
adduser(ssh, "x1", "xyz123x", sudo=False)
break
except paramiko.AuthenticationException:
print("Authentication failed when connecting to {}".format(host))
sys.exit(1)
except:
print("Could not SSH to {}, waiting for it to start".format(host))
i += 1
time.sleep(5)
# If we could not connect within time limit
if i == 100:
print("Could not connect to {}. Giving up".format(host))
sys.exit(1)
def listinstance(self):
for instance in ec2.instances.all():
print instance.id, instance.state
return self.terminate()
def terminate(self):
for instance_id in sys.argv[1:]:
instance = ec2.Instance(instance_id)
response = instance.terminate()
print response
app = Flask(__name__)
@app.route('/createvm', methods=['POST'])
def main():
if not request.json or not 'username' in request.json or not 'password' in request.json:
abort(400)
credentials = {
'username': request.json['username'],
'password': request.json['password'],
}
vm = createvm(credentials)
c = connect(vm['instance_ip'])
return json.dumps(c)
if __name__ == "__main__":
app.run(port=port, debug=True)