def __init__(self, server_type = "Base"): ''' Initial setup for all servers. This is called from the server's init function.''' # Establish AWS connections self.ec2_conn = AWS.get_ec2_connection() self.cf_conn = AWS.get_cf_connection() self.route53_conn = AWS.get_route53_connection() self.elb_conn = AWS.get_elb_connection() # Get HCFG2 configuration self._server_type = server_type self._hcfg2_configuration = HCFG2.get_server_configuration(server_type) print server_type print self._hcfg2_configuration self._default_puppetmaster_ip = self._hcfg2_configuration['default_puppetmaster_ip'] self._domain = self._hcfg2_configuration['domain'] self._default_instance_type = self._hcfg2_configuration['default_instance_type'] self._template_location = self._hcfg2_configuration['template_location'] self._ip_range = self._hcfg2_configuration['ip_range'] self._security_groups = self._hcfg2_configuration['security_groups'] # Set up server details self._set_puppetmaster() self._get_host_number() self._assign_subnet() self._get_instance_type() print "Base Server bootstrapping done"
def view_databases(): rds = AWS.get_rds_connection() results = rds.get_all_dbinstances() for inst in results: print dir(inst) print "Database: " + inst.id print "\tStatus: " + inst.status host, port = inst.endpoint print "\tEndpoint: {0}:{1}".format(host, port) print "\tHCS user: " + inst.master_username
def convert_and_save(file, people_name, relation): starter = file.find(',') image_data = file[starter + 1:] image_data = bytes(image_data, encoding="ascii") im = Image.open(BytesIO(base64.b64decode(image_data))) im = im.convert('RGB') img_name = str(time.time()).replace('.', '') + '.jpg' dir_name = os.path.join(APP_ROOT, 'images', img_name) im.save(dir_name) data = { 'name': people_name, 'relation': relation, 'image_bucket_key': img_name } thread = Thread(target=AWS.put_item_db(tablename='people', data=data)) thread.start() thread2 = Thread( target=AWS.put_item_bucket(sourcename=dir_name, targetname=img_name)) thread2.start()
def restart_database(): rds = AWS.get_rds_connection() results = rds.get_all_dbinstances() RDS._hcs_header() for i in range(len(results)): print "[{0}] {1} | {2}".format(i, results[i].id, results[i].status) item = int(raw_input("\nWhich database do you want to restart? Pick an invalid number to escape: ")) if 0 <= item and item < len(results): dbid = results[item].id print "Restarting {0}...".format(dbid) # restart the server rds.reboot_dbinstance(dbid) print "Waiting for database to come online..." while rds.get_all_dbinstances(instance_id=dbid)[0].status == "rebooting": # throttle how often we poll, or AWS will kick us out time.sleep(2) print "Database is online!" else: print "Terminated RDS connection."
def pi2aws_process_image(img): t = time.time() print(str(pi2aws_process_image.__name__)) name = str(time.time()).replace('.', '') + '.jpg' dir_name = os.path.join(APP_ROOT, 'images', name) cv2.imwrite(dir_name, img) AWS.put_item_bucket(sourcename=dir_name, targetname=name) people = AWS.get_item__db('people') keys = [item['image_bucket_key'] for item in people] print(keys) matched_key = AWS.compare_faces_among_all(unknown_key=name, known_keys=keys) print('match: ' + str(matched_key)) if matched_key is None: person_name = 'unknown' else: # for person in people: # if person['image_bucket_key'] == matched_key: # person_name = person['name'] # break person_name = next( (item['name'] for item in people if item['image_bucket_key'] == matched_key), False) local_url = r'http://' + PUBLIC_IP + ':' + str(PORT) + r'/upload/' + name data = { 'name': person_name, 'url': local_url, 'timestamp': str(datetime.datetime.now()), } print(data) AWS.put_item_db(tablename='status', data=data) print('time elapsed: ' + str(time.time() - t))
def main(): parser = argparse.ArgumentParser(description='Constructor') parser.add_argument('--init', action="store_true", default=False) parser.add_argument('--env', type=str, help='Environment to launch in.', default='dev') args = parser.parse_args() aws = AWS(args.env) if args.init: aws.run_with_shell('configure') groups = aws.run('ec2', 'describe-security-groups')['SecurityGroups'] groups = [item for item in groups if item['GroupName'] == 'web-server'] if len(groups) == 0: aws.run('ec2', 'create-security-group', '--group-name', 'web-server', '--description', 'Generic web server with SSH access') aws.run('ec2', 'authorize-security-group-ingress', '--group-name', 'web-server', '--protocol', 'tcp', '--port', '22', '--cidr', '0.0.0.0/0') aws.run('ec2', 'authorize-security-group-ingress', '--group-name', 'web-server', '--protocol', 'tcp', '--port', '80', '--cidr', '0.0.0.0/0') aws.run('ec2', 'authorize-security-group-ingress', '--group-name', 'web-server', '--protocol', 'tcp', '--port', '443', '--cidr', '0.0.0.0/0') subprocess.call('mkdir -p ~/.constructor', shell=True) subprocess.call( 'aws ec2 create-key-pair --key-name dev --query \'KeyMaterial\' --output text --profile dev > ~/.constructor/' + args.env + '.pem', shell=True) subprocess.call('chmod 400 ~/.constructor/' + args.env + '.pem', shell=True) if os.path.isfile(os.path.expanduser('~/.constructor/id_rsa')): subprocess.call( 'ssh-keygen -f ~/.constructor/id_rsa -t rsa -P \'\'', shell=True) print "Run: cat ~/.constructor/id_rsa.pub | pbcopy" print "And add your key to your git hosting." else: infrastructure = Infrastructure(environment=args.env) blueprint = imp.load_source('blueprint', 'blueprint.py') blueprint.build(infrastructure)
def __init__(self,region=None): AWS.__init__(self, region=region) self.ec2 = boto3.client('ec2')
def main(): parser = argparse.ArgumentParser(description='Constructor') parser.add_argument('--init', action="store_true", default=False) parser.add_argument('--env', type=str, help='Environment to launch in.', default='dev') args = parser.parse_args() aws = AWS(args.env) if args.init: aws.run_with_shell('configure') groups = aws.run('ec2', 'describe-security-groups')['SecurityGroups'] groups = [item for item in groups if item['GroupName'] == 'web-server'] if len(groups) == 0: aws.run( 'ec2', 'create-security-group', '--group-name', 'web-server', '--description', 'Generic web server with SSH access' ) aws.run( 'ec2', 'authorize-security-group-ingress', '--group-name', 'web-server', '--protocol', 'tcp', '--port', '22', '--cidr', '0.0.0.0/0' ) aws.run( 'ec2', 'authorize-security-group-ingress', '--group-name', 'web-server', '--protocol', 'tcp', '--port', '80', '--cidr', '0.0.0.0/0' ) aws.run( 'ec2', 'authorize-security-group-ingress', '--group-name', 'web-server', '--protocol', 'tcp', '--port', '443', '--cidr', '0.0.0.0/0' ) subprocess.call('mkdir -p ~/.constructor', shell=True) subprocess.call( 'aws ec2 create-key-pair --key-name dev --query \'KeyMaterial\' --output text --profile dev > ~/.constructor/' + args.env + '.pem', shell=True ) subprocess.call('chmod 400 ~/.constructor/' + args.env + '.pem', shell=True) if os.path.isfile(os.path.expanduser('~/.constructor/id_rsa')): subprocess.call('ssh-keygen -f ~/.constructor/id_rsa -t rsa -P \'\'', shell=True) print "Run: cat ~/.constructor/id_rsa.pub | pbcopy" print "And add your key to your git hosting." else: infrastructure = Infrastructure(environment=args.env) blueprint = imp.load_source('blueprint', 'blueprint.py') blueprint.build(infrastructure)
def __init__(self, region=None): AWS.__init__(self, region=region) self.s3 = boto3.client('s3')
POINTS_PER_HIT = 1 POINTS_PER_KILL = 10 RULES = { "shot_damage": SHOT_DAMAGE, "lives": LIVES, "respawn": 30, "fire_rate": FIRE_RATE, "mode": MODE, "points_per_hit": POINTS_PER_HIT, "points_per_kill": POINTS_PER_KILL } aws_client = AWS( "server", "a213qaaw8pshxz.iot.us-east-1.amazonaws.com", "/home/steve/Desktop/AWS_Certs/RootCA", "/home/steve/Desktop/AWS_Certs/fc21dd6d85-private.pem.key", "/home/steve/Desktop/AWS_Certs/fc21dd6d85-certificate.pem.crt") Game_Players = Players() def json_clean(jsonMessage): if jsonMessage.endswith('\x00'): return jsonMessage[:-1] else: return jsonMessage def register_player(client, userdata, message): message.payload = json_clean(message.payload)
from AWS import AWS import time testClient = AWS( "server", "a213qaaw8pshxz.iot.us-east-1.amazonaws.com", "/home/steve/Desktop/AWS_Certs/RootCA", "/home/steve/Desktop/AWS_Certs/fc21dd6d85-private.pem.key", "/home/steve/Desktop/AWS_Certs/fc21dd6d85-certificate.pem.crt") test = {'a': 1, 'b': 2, 'c': 3} while True: testClient.AWS_Publish("$aws/things/SPSTM/test", test) time.sleep(2)
import sys, os from AWS import AWS conn = AWS.get_ec2_connection() c = 'f' instance = sys.argv[1] for i in ['vol-67e00719','vol-49e00737','vol-3ee00740','vol-0ee00770','vol-f0e0078e']: conn.attach_volume(i, instance, '/dev/sd{0}'.format(c)) c = chr(ord(c) + 1)
def status(): return jsonify(AWS.get_item__db('status'))