Example #1
0
def download_key_file(cloudstore: DropboxCloudstore, key_file_path: str) -> None:
    """
    Downloads the ssh key file from Dropbox so that it can be used for this run.

    :param cloudstore:
    :param key_file_path:
    :return:
    """

    logger.info("Downloading file from Dropbox to path '%s'.." % key_file_path)
    cloudstore.download_file(fromfile='MinecraftKeyPair.pem', tofile=key_file_path)
    logger.info("Download finished.")
Example #2
0
def upload_key_file(cloudstore: DropboxCloudstore, key_file_path: str) -> None:
    """
    Uploads the ssh key file to Dropbox so that it will be available for download from Heroku.

    :param cloudstore:
    :param key_file_path:
    :return:
    """

    logger.info("Uploading file from path '%s' to Dropbox.." % key_file_path)
    with open(key_file_path, 'rb') as key_file:
        cloudstore.upload_file(file_bytes=key_file.read(), upload_path='MinecraftKeyPair.pem')
    logger.info("Upload finished.")
 def test_upload_delete(self):
     cloud_store = DropboxCloudstore(
         config=self.configuration.get_cloudstores()[0])
     # Upload file
     logger.info('Uploading file..')
     file_to_upload = open(
         os.path.join(self.test_data_path, self.file_name), 'rb').read()
     cloud_store.upload_file(file_to_upload, '/tests/' + self.file_name)
     # Check if it was uploaded
     self.assertIn(self.file_name, cloud_store.ls('/tests/').keys())
     # Delete it
     cloud_store.delete_file('/tests/' + self.file_name)
     # Check if it was deleted
     self.assertNotIn(self.file_name, cloud_store.ls('/tests/').keys())
 def test_connect(self):
     # Test the connection with the correct api key
     try:
         cloud_store_correct_key = DropboxCloudstore(
             config=self.configuration.get_cloudstores()[0])
         cloud_store_correct_key.ls()
     except BadInputError as e:
         logger.error('Error connecting with the correct credentials: %s',
                      e)
         self.fail('Error connecting with the correct credentials')
     else:
         logger.info('Connected with the correct credentials successfully.')
     # Test that the connection is failed with the wrong credentials
     with self.assertRaises(BadInputError):
         cloud_store_wrong_configuration = copy.deepcopy(
             self.configuration.get_cloudstores()[0])
         cloud_store_wrong_configuration['api_key'] = 'wrong_key'
         cloud_store_wrong_key = DropboxCloudstore(
             config=cloud_store_wrong_configuration)
         cloud_store_wrong_key.ls()
     logger.info(
         "Loading Dropbox with wrong credentials failed successfully.")
Example #5
0
def init_flask_server():
    global logger, config, sshClient, key, aws_config, mineserver_config, web_client_config, mineserver

    logger = logging.getLogger('Flask App')
    # Download key file from Dropbox if requested
    if 'cloudstore' in config.config_attributes:
        cloudstore_config = config.get_cloudstore_configs()[0]
        cloudstore = DropboxCloudstore(config=cloudstore_config)
        download_key_file(cloudstore=cloudstore,
                          key_file_path=mineserver_config['ssh_key_file_path'])
    # Setup paraminko ssh information
    logger.debug("Setting up paraminko ssh information")
    key = paramiko.RSAKey.from_private_key_file(mineserver_config['ssh_key_file_path'])
    sshClient = paramiko.SSHClient()
    sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # Initialize the Mineserver class
    logger.debug("Initializing the Mineserver class..")
    mineserver = Mineserver(instance_id=aws_config['instance_id'],
                            memory_allocation=mineserver_config['memory_allocation'])
    # Start the Flask app
    logger.debug("Starting the flask app..")
 def test_upload_download(self):
     cloud_store = DropboxCloudstore(
         config=self.configuration.get_cloudstores()[0])
     # Upload file
     logger.info('Uploading file..')
     file_to_upload = open(
         os.path.join(self.test_data_path, self.file_name), 'rb').read()
     cloud_store.upload_file(file_to_upload, '/tests/' + self.file_name)
     # Check if it was uploaded
     self.assertIn(self.file_name, cloud_store.ls('/tests/').keys())
     # Download it
     logger.info('Downloading file..')
     cloud_store.download_file(frompath='/tests/' + self.file_name,
                               tofile=os.path.join(self.test_data_path,
                                                   'actual_downloaded.txt'))
     # Compare contents of downloaded file with the original
     self.assertEqual(
         open(os.path.join(self.test_data_path, self.file_name),
              'rb').read(),
         open(os.path.join(self.test_data_path, 'actual_downloaded.txt'),
              'rb').read())
Example #7
0
def main():
    """
    :Example:
    python main.py -m run_mode_1
                   -c confs/template_conf.yml
                   -l logs/output.log
    """

    # Initializing
    args = _argparser()
    _setup_log(args.log, args.debug)
    logger.info("Starting in run mode: {0}".format(args.run_mode))
    # Load the configuration
    configuration = Configuration(config_src=args.config_file)
    # Init the Cloudstore
    cloud_store = DropboxCloudstore(config=configuration.get_cloudstores()[0])
    # Init the Datastore
    data_store = MySqlDatastore(**configuration.get_datastores()[0])
    # Init the Email App
    gmail_configuration = configuration.get_email_apps()[0]
    gmail_app = GmailEmailApp(config=configuration.get_email_apps()[0])

    # Mysql examples
    logger.info("\n\nMYSQL EXAMPLE\n-------------------------")
    logger.info("\n\nTables in current DB: {0}".format(
        list(data_store.show_tables())))
    logger.info("Creating Table: orders")
    table_schema = """ order_id INT(6) PRIMARY KEY,
                       order_type VARCHAR(30) NOT NULL,
                       location VARCHAR(30) NOT NULL """
    data_store.create_table(table='orders', schema=table_schema)
    logger.info("Tables in current DB:\n{0}".format(
        list(data_store.show_tables())))
    logger.info("Inserting into orders the values:\n(1 simple newyork)..")
    insert_data = {
        "order_id": 1,
        "order_type": "plain",
        "location": "new_york"
    }
    data_store.insert_into_table(table='orders', data=insert_data)
    logger.info("SELECT * FROM orders;\n{0}".format(
        data_store.select_from_table(table='orders')))
    logger.info("Deleting the inserted row from table orders..")
    data_store.delete_from_table(table='orders', where='order_id=1')
    logger.info("SELECT * FROM orders;\n{0}".format(
        data_store.select_from_table(table='orders')))
    logger.info("Dropping Table: orders")
    data_store.drop_table(table='orders')
    logger.info("Tables in current DB:\n{0}".format(
        list(data_store.show_tables())))

    # Dropbox examples
    logger.info("\n\nDROPBOX EXAMPLE\n-------------------------")
    logger.info("List of files in Dropbox /python_template:\n{0}".format(
        list(cloud_store.ls(path='/python_template').keys())))
    upload_path = "/python_template/file1.txt"
    file_content = "test file content"
    logger.info("Uploading file {file} with content:\n{content}".format(
        file=upload_path, content=file_content))
    cloud_store.upload_file(file_bytes=file_content.encode(),
                            upload_path=upload_path)
    logger.info("List of files in Dropbox /python_template:\n{0}".format(
        list(cloud_store.ls(path='/python_template').keys())))
    downloaded_file = cloud_store.download_file(frompath=upload_path)
    logger.info(
        "Downloaded file and its content is:\n{0}".format(downloaded_file))
    cloud_store.delete_file(file_path=upload_path)
    logger.info("Deleting file {file}..".format(file=upload_path))
    logger.info("List of files in Dropbox /python_template:\n{0}".format(
        list(cloud_store.ls(path='/python_template').keys())))

    # Gmail examples
    logger.info("\n\nGMAIL EXAMPLE\n-------------------------")
    subject = "Email example"
    body = "<h1>This is an html body example</h1><br><b>This goes to the html argument. " \
           "You can use the text argument for plain text.</b>"
    emails_list = [gmail_configuration['email_address']]
    attachments_paths = [os.path.join('data', 'sample_data.txt')]
    logger.info(
        "Sending email with `subject` = `{subject}`, `from,to,cc,bcc,reply_to` = `{email_addr}`, "
        "`html` = `{body}` and `attachments` = `{attachments}`".format(
            subject=subject,
            email_addr=emails_list[0],
            body=body,
            attachments=attachments_paths))
    gmail_app.send_email(subject=subject,
                         to=emails_list,
                         cc=emails_list,
                         bcc=emails_list,
                         html=body,
                         attachments=attachments_paths,
                         sender=emails_list[0],
                         reply_to=emails_list[0])
 def tearDownClass(cls):
     cloud_store = DropboxCloudstore(
         config=cls.configuration.get_cloudstores()[0])
     cloud_store.delete_file('/tests')
Example #9
0
    mineserver = Mineserver(instance_id=aws_config['instance_id'],
                            memory_allocation=mineserver_config['memory_allocation'])
    # Start the Flask app
    logger.debug("Starting the flask app..")


if __name__ == "__main__":
    args = _argparser()
    logger, config, aws_config, mineserver_config, web_client_config = setup_classes(config_file=args.config_file,
                                                                                     log=args.log,
                                                                                     debug=args.debug)
    if args.run_mode == 'run_flask':
        init_flask_server()
        app.run(debug=args.debug)
    elif args.run_mode == 'create_instance':
        logger = logging.getLogger('Create Instance')
        create_instance()
    elif args.run_mode == 'upload_key_file':
        logger = logging.getLogger('Upload Key File')
        cloudstore_config = config.get_cloudstore_configs()[0]
        cloudstore = DropboxCloudstore(config=cloudstore_config)
        upload_key_file(cloudstore=cloudstore,
                        key_file_path=mineserver_config['ssh_key_file_path'])
    else:
        raise argparse.ArgumentTypeError('Invalid run mode specified!')
else:
    logger, config, aws_config, mineserver_config, web_client_config = setup_classes(config_file=DEFAULT_CONFIG,
                                                                                     log=DEFAULT_LOG,
                                                                                     debug=False)
    init_flask_server()