Esempio n. 1
0
def _checks():
    """Requirements"""

    if os.environ.get('USER_FILE'):
        print 'ERROR: USER_FILE environment variable must not be set.'
        sys.exit()
    if os.path.exists('/host/{0}/users.cfg'.format(VPC)):
        os.rename('/host/{0}/users.cfg'.format(VPC), '/host/{0}/users.cfg.register.bak'.format(VPC))
    check_email_template()
Esempio n. 2
0
def _checks():
    """Requirements"""

    if os.environ.get('USER_FILE'):
        print 'ERROR: USER_FILE environment variable must not be set.'
        sys.exit()
    if os.path.exists('/host/{0}/users.cfg'.format(VPC)):
        os.rename('/host/{0}/users.cfg'.format(VPC),
                  '/host/{0}/users.cfg.register.bak'.format(VPC))
    check_email_template()
Esempio n. 3
0
File: ses.py Progetto: Vizuri/train
def email_credentials():
    """Email all user information and credentials listed in USER_FILE"""

    check_email_template()
    SES_REGION = check_ses_region('SES_REGION')
    SES_FROM_EMAIL = check_env('SES_FROM_EMAIL')
    SES_FROM_NAME = check_env('SES_FROM_NAME', ' ')

    conn = boto.ses.connect_to_region(SES_REGION,
                                      aws_access_key_id=AWS_ACCESS_KEY_ID,
                                      aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    print 'Emailing user information and credentials ...'
    mail = imp.load_source('mail', EMAIL_TEMPLATE)
    with open(USER_FILE) as file:
        for line in file:
            # defaults
            instances = ''
            mail_body = mail.body
            username = line.split(',')[0].strip()

            # message
            msg = MIMEMultipart()
            msg.preamble = 'Multipart message.\n'
            msg['Subject'] = mail.subject
            msg['From'] = '{0} <{1}>'.format(SES_FROM_NAME, SES_FROM_EMAIL).strip()
            msg['To'] = line.split(',')[1].strip()

            # *.pem and *.ppk files
            msg.attach(prep_file('/host/{0}/users/{1}/{1}-{0}.pem'.format(VPC, username), username))
            msg.attach(prep_file('/host/{0}/users/{1}/{1}-{0}.ppk'.format(VPC, username), username))

            # instance information
            mail_body += "\n---\n\nAWS instances:\n"

            files = [f for f in os.listdir('/host/{0}/users/{1}/'.format(VPC, username)) if f.endswith('.txt')]
            for textfile in files:
                with open('/host/{0}/users/{1}/{2}'.format(VPC, username, textfile)) as f:
                    for line in f:
                        if line.startswith('AWS'):
                            continue
                        else:
                            instances += line

            mail_body += instances
            msg.attach(MIMEText(mail_body))

            try:
                result = conn.send_raw_email(msg.as_string(), source=msg['From'], destinations=msg['To'])
                print "Welcome email sent to: '{0}' <{1}> ...".format(username, msg['To'])
            except:
                print 'Error sending email to: {0}'.format(username, msg['To'])
                raise
Esempio n. 4
0
def email_credentials(conn):
    """Email all user information and credentials listed in USER_FILE"""

    if not os.environ.get("MAILGUN_KEY") or not os.environ.get("MAILGUN_DOMAIN"):
        print "\nERROR: Required environment variable 'MAILGUN_KEY' or 'MAILGUN_DOMAIN' not set!\n"
        sys.exit()
    else:
        MAILGUN_KEY = check_env('MAILGUN_KEY')
        MAILGUN_DOMAIN = check_env('MAILGUN_DOMAIN')

    check_email_template()

    print 'Emailing user information and credentials ...'

    mail = imp.load_source('mail', EMAIL_TEMPLATE)
    with open(USER_FILE) as file:
        for line in file:
            instances = ''
            username = line.split(',')[0].strip()
            email = line.split(',')[1].strip()

            # instances
            files = [f for f in os.listdir('/host/{0}/users/{1}/'.format(VPC, username)) if f.endswith('.txt')]
            target = open('/host/{0}/users/{1}/instances.txt'.format(VPC, username), 'w')
            target.truncate()

            for textfile in files:
                with open('/host/{0}/users/{1}/{2}'.format(VPC, username, textfile)) as f:
                    instances += f.read()
                    instances += '\n'

            target.write(instances)
            target.close()

            try:
                result = requests.post(
                    "https://api.mailgun.net/v3/{0}/messages".format(MAILGUN_DOMAIN),
                    auth = ("api", MAILGUN_KEY),
                    files = [ ("attachment", open ('/host/{0}/users/{1}/{1}-{0}.pem'.format(VPC, username))),
                              ("attachment", open ('/host/{0}/users/{1}/{1}-{0}.ppk'.format(VPC, username))),
                              ("attachment", open ('/host/{0}/users/{1}/instances.txt'.format(VPC, username))) ],
                    data = { 'from': "{0} <{1}>".format(mail.from_name, mail.from_email),
                             'to': email,
                             'subject': mail.subject,
                             'text': mail.text})

                print "Welcome email sent to: '{0}' <{1}> ...".format(username, email)

            except requests.exceptions.ConnectionError as e:
                print 'An error occurred'
                raise
Esempio n. 5
0
def email_credentials(conn):
    """Email all user information and credentials listed in USER_FILE"""

    if not os.environ.get("MANDRILL_KEY"):
        print "\nERROR: Required environment variable 'MANDRILL_KEY' not set!\n"
        sys.exit()
    else:
        mandrill_client = mandrill.Mandrill(os.environ.get('MANDRILL_KEY'))

    check_email_template()

    print 'Emailing user information and credentials ...'

    mail = imp.load_source('mail', EMAIL_TEMPLATE)
    with open(USER_FILE) as file:
        for line in file:
            instances = ''
            username = line.split(',')[0].strip()
            email = line.split(',')[1].strip()

            # keyfile
            with open ('/host/{0}/users/{1}/{1}-{0}.pem'.format(VPC, username), "r") as f:
                keyfile = base64.b64encode(f.read())
            # ppkfile
            with open ('/host/{0}/users/{1}/{1}-{0}.ppk'.format(VPC, username), "r") as f:
                ppkfile = base64.b64encode(f.read())

            # instances
            files = [f for f in os.listdir('/host/{0}/users/{1}/'.format(VPC, username)) if f.endswith('.txt')]
            for textfile in files:
                with open('/host/{0}/users/{1}/{2}'.format(VPC, username, textfile)) as f:
                    instances += f.read()
                    instances += '\n'

            try:
                message = {
                    'from_email': mail.from_email,
                    'from_name': mail.from_name,
                    'to': [{'email': email, 'name': username, 'type': 'to'}],
                    'subject': mail.subject,
                    'text': mail.text,
                    'attachments': [
                        {
                            "type": "text/plain",
                            "name": "{0}.pem".format(username),
                            "content": keyfile
                        },
                        {
                            "type": "text/plain",
                            "name": "{0}.ppk".format(username),
                            "content": ppkfile
                        },
                        {
                            "type": "text/plain",
                            "name": "instances.txt",
                            "content": base64.b64encode(instances)
                        }
                    ],
                }

                result = mandrill_client.messages.send(message=message)
                print "Welcome email sent to: '{0}' <{1}> ...".format(username, email)

            except mandrill.Error, e:
                print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
                raise
Esempio n. 6
0
def email_credentials():
    """Email all user information and credentials listed in USER_FILE"""

    check_email_template()
    SES_REGION = check_ses_region('SES_REGION')
    SES_FROM_EMAIL = check_env('SES_FROM_EMAIL')
    SES_FROM_NAME = check_env('SES_FROM_NAME', ' ')

    conn = boto.ses.connect_to_region(
        SES_REGION,
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    print 'Emailing user information and credentials ...'
    mail = imp.load_source('mail', EMAIL_TEMPLATE)
    with open(USER_FILE) as file:
        for line in file:
            # defaults
            instances = ''
            mail_body = mail.body
            username = line.split(',')[0].strip()

            # message
            msg = MIMEMultipart()
            msg.preamble = 'Multipart message.\n'
            msg['Subject'] = mail.subject
            msg['From'] = '{0} <{1}>'.format(SES_FROM_NAME,
                                             SES_FROM_EMAIL).strip()
            msg['To'] = line.split(',')[1].strip()

            # *.pem and *.ppk files
            msg.attach(
                prep_file(
                    '/host/{0}/users/{1}/{1}-{0}.pem'.format(VPC, username),
                    username))
            msg.attach(
                prep_file(
                    '/host/{0}/users/{1}/{1}-{0}.ppk'.format(VPC, username),
                    username))

            # instance information
            mail_body += "\n---\n\nAWS instances:\n"

            files = [
                f for f in os.listdir('/host/{0}/users/{1}/'.format(
                    VPC, username)) if f.endswith('.txt')
            ]
            for textfile in files:
                with open('/host/{0}/users/{1}/{2}'.format(
                        VPC, username, textfile)) as f:
                    for line in f:
                        if line.startswith('AWS'):
                            continue
                        else:
                            instances += line

            mail_body += instances
            msg.attach(MIMEText(mail_body))

            try:
                result = conn.send_raw_email(msg.as_string(),
                                             source=msg['From'],
                                             destinations=msg['To'])
                print "Welcome email sent to: '{0}' <{1}> ...".format(
                    username, msg['To'])
            except:
                print 'Error sending email to: {0}'.format(username, msg['To'])
                raise
Esempio n. 7
0
def email_credentials(conn):
    """Email all user information and credentials listed in USER_FILE"""

    if not os.environ.get("MANDRILL_KEY"):
        print "\nERROR: Required environment variable 'MANDRILL_KEY' not set!\n"
        sys.exit()
    else:
        mandrill_client = mandrill.Mandrill(os.environ.get('MANDRILL_KEY'))

    check_email_template()

    print 'Emailing user information and credentials ...'

    mail = imp.load_source('mail', EMAIL_TEMPLATE)
    with open(USER_FILE) as file:
        for line in file:
            instances = ''
            username = line.split(',')[0].strip()
            email = line.split(',')[1].strip()

            # keyfile
            with open('/host/{0}/users/{1}/{1}-{0}.pem'.format(VPC, username),
                      "r") as f:
                keyfile = base64.b64encode(f.read())
            # ppkfile
            with open('/host/{0}/users/{1}/{1}-{0}.ppk'.format(VPC, username),
                      "r") as f:
                ppkfile = base64.b64encode(f.read())

            # instances
            files = [
                f for f in os.listdir('/host/{0}/users/{1}/'.format(
                    VPC, username)) if f.endswith('.txt')
            ]
            for textfile in files:
                with open('/host/{0}/users/{1}/{2}'.format(
                        VPC, username, textfile)) as f:
                    instances += f.read()
                    instances += '\n'

            try:
                message = {
                    'from_email':
                    mail.from_email,
                    'from_name':
                    mail.from_name,
                    'to': [{
                        'email': email,
                        'name': username,
                        'type': 'to'
                    }],
                    'subject':
                    mail.subject,
                    'text':
                    mail.text,
                    'attachments': [{
                        "type": "text/plain",
                        "name": "{0}.pem".format(username),
                        "content": keyfile
                    }, {
                        "type": "text/plain",
                        "name": "{0}.ppk".format(username),
                        "content": ppkfile
                    }, {
                        "type": "text/plain",
                        "name": "instances.txt",
                        "content": base64.b64encode(instances)
                    }],
                }

                result = mandrill_client.messages.send(message=message)
                print "Welcome email sent to: '{0}' <{1}> ...".format(
                    username, email)

            except mandrill.Error, e:
                print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
                raise