def _got_sshfp(ignored): retries = LISTEN_RETRIES while True: try: install_server(publichost, admin_privkey_path, monitor_pubkey, monitor_privkey_path, stdout, stderr) break except NotListeningError: retries -= 1 if retries <= 0: print >>stdout, "Timed out waiting for EC2 instance to listen for ssh connections." raise TimeoutError() print >>stdout, "Waiting another %d seconds..." % (LISTEN_POLL_TIME) time.sleep(LISTEN_POLL_TIME) continue furl = bounce_server(publichost, admin_privkey_path, privatehost, useraccesskeyid, usersecretkey, usertoken, producttoken, bucketname, oldsecrets, stdout, stderr, secretsfile) # XXX We'll have to ammend this: initialize_statmover_source(publichost, monitor_privkey_path, admin_privkey_path, sinkname_suffix, [instance.instance_id, 'SSEC2s']) # XXX We probably need to rethink this: append_record(FilePath(serverinfopath), instance.launch_time, instance.instance_id, publichost) print >>stderr, "Signup done." d4 = defer.succeed(None) if not oldsecrets: d4.addCallback(lambda ign: send_signup_confirmation(publichost, customer_name, customer_email, furl, customer_keyinfo, stdout, stderr) ) return d4
def when_done(ign): service_confirmed_fp = self.basefp.child(SERVICE_CONFIRMED_FILE) try: append_record(service_confirmed_fp, customer.subscription.id) except Exception: # The request really did succeed, we just failed to record that it did. Log the error # locally. traceback.print_tb(100, self._log)
def when_failed(): try: all_activationkeys.add(activationkey) append_record(signups_fp, 'failure', activationkey, productcode, name, email, publickey) except Exception: traceback.print_exc(100, stderr) request.write(FAILED_HTML) else: request.write(FAILED_HTML) finally: request.finish()
def test_append_record(self): mockfp = Mock() mockopen = mockfp.open mockhandle = mockfp.open.return_value mockwrite = mockhandle.write mockclose = mockhandle.close append_record(mockfp, *TESTTUPLE) mockopen.assert_called_with('a') writevector = mockwrite.call_args[0][0] self.failUnlessEqual(len(writevector.split(',')), 1 + len(TESTTUPLE)) mockclose.assert_called_with()
def test_append_record(self): mockfp = Mock() mockopen = mockfp.open mockhandle = mockfp.open.return_value mockwrite = mockhandle.write mockclose = mockhandle.close append_record(mockfp, *TESTTUPLE) mockopen.assert_called_with('a') writevector = mockwrite.call_args[0][0] self.failUnlessEqual(len(writevector.split(',')), 1+len(TESTTUPLE)) mockclose.assert_called_with()
def when_done(): try: successful_activationkeys.add(activationkey) all_activationkeys.add(activationkey) append_record(signups_fp, 'success', activationkey, productcode, name, email, publickey) except Exception: # The request really did succeed, we just failed to record that it did. Log the error locally. traceback.print_exc(100, stderr) request.write(SUCCEEDED_HTML) else: request.write(SUCCEEDED_HTML) finally: request.finish()
def render(self, request): print >>self.out, "Ooh, possible signup coming:", request.args activationkey = self.get_arg(request, 'ActivationKey') productcode = self.get_arg(request, 'ProductCode') append_record(self.basefp.child(DEVPAY_COMPLETIONS_FILE), activationkey, productcode) request.setResponseCode(200) if activationkey and productcode: return DEVPAY_RESPONSE_HAVE_CODES_HTML % {"activationkey": activationkey, "productcode": productcode, "productfullname": get_full_name(productcode, self.products)} else: return DEVPAY_RESPONSE_MISSING_CODE_HTML
def render(self, request): print >>self.out, "Yay, another potential customer:", request.args email = self.get_arg(request, 'Email') productname = self.get_arg(request, 'ProductName') productfullname = self.get_arg(request, 'ProductFullName') if productname in ACTIVE_PRODUCTS: signup_url = SIGNUP_BASE_URL + productname valid_email_template = 'valid_email_activeproduct.html' else: signup_url = None valid_email_template = 'valid_email_inactiveproduct.html' append_record(self.basefp.child(EMAILS_FILE), email, productname) request.setResponseCode(200) if email and VALID_EMAIL_RE.match(email): tmpl = env.get_template(valid_email_template) (start, _, rest) = tmpl.render(productname=productname, productfullname=productfullname).encode('utf-8').partition("MAGIC") request.write(start) d = defer.succeed(None) d.addCallback(lambda ign: send_autoreply(email, productfullname, signup_url)) def _sent(ign): request.write("We've just sent you an email to check that you can receive email from us.") request.write(rest) def _error(f): request.write("We weren't able to send email to the address you provided. This could be a problem on " "our end, but please check the address. We've recorded the address anyway so that we can " "try again manually.") request.write(rest) print >>self.out, str(f) d.addCallbacks(_sent, _error) d.addBoth(lambda ign: request.finish()) def _err(f): print >>self.out, str(f) d.addErrback(_err) return NOT_DONE_YET else: tmpl = env.get_template('invalid_email.html') return tmpl.render(productname=productname, productfullname=productfullname).encode('utf-8')
def render(self, request): # The expected HTTP method is a POST from the <form> in templates/subscription_signup.html. # render_POST is handled by the HandlerBase parent which calls this this method after logging # the request. # Get information needed to create the new stripe subscription to the S4 plan stripe_api_key, stripe_authorization_token, user_email = self.get_creation_parameters(request) try: # Invoke card charge by requesting subscription to recurring-payment plan. customer = self.create_customer(stripe_api_key, stripe_authorization_token, user_email) except RenderErrorDetailsForBrowser as e: tmpl = env.get_template('s4-subscription-form.html') return tmpl.render({"errorblock": e.details}).encode('utf-8', 'replace') # Log that a new subscription has been created (at stripe). subscriptions_fp = self.basefp.child(SUBSCRIPTIONS_FILE) append_record(subscriptions_fp, customer.subscription.id) # Initiate the provisioning service self.run_full_signup(customer, request) # Return the a page notifying the user that they've been charged and their service is being set up. tmpl = env.get_template('payment_verified.html') return tmpl.render({"productfullname": "Simple Secure Storage Service", "productname":"S4"}).encode('utf-8')
def main(stdin, flapp_stdout, flapp_stderr): append_record(flapp_stdout, "Automation script started.") parameters_json = stdin.read() (customer_email, customer_pgpinfo, customer_id, plan_id, subscription_id) = simplejson.loads(parameters_json) (abslogdir_fp, stripesecrets_log_fp, SSEC2secrets_log_fp, signup_log_fp) = create_log_filepaths(plan_id, customer_id, subscription_id) append_record(flapp_stdout, "Writing logs to %r." % (abslogdir_fp.path,)) stripesecrets_log_fp.setContent(parameters_json) SSEC2_secretsfile = SSEC2secrets_log_fp.open('a+') signup_logfile = signup_log_fp.open('a+') signup_stdout = LoggingStream(signup_logfile, '>') signup_stderr = LoggingStream(signup_logfile, '') sys.stdout = signup_stderr def errhandler(err): fh = flapp_stderr.open('a+') fh.write(repr(err)) fh.close() return err d = defer.succeed(None) d.addCallback(lambda ign: activate_subscribed_service(customer_email, customer_pgpinfo, customer_id, subscription_id, plan_id, signup_stdout, signup_stderr, SSEC2_secretsfile, signup_log_fp.path) ) d.addErrback(errhandler) d.addBoth(lambda ign: signup_logfile.close())
def _got_sshfp(ignored): retries = LISTEN_RETRIES while True: try: install_server(publichost, admin_privkey_path, monitor_pubkey, monitor_privkey_path, stdout, stderr) break except NotListeningError: retries -= 1 if retries <= 0: print >>stdout, "Timed out waiting for EC2 instance to listen for ssh connections." raise TimeoutError() print >>stdout, "Waiting another %d seconds..." % (LISTEN_POLL_TIME) time.sleep(LISTEN_POLL_TIME) continue furl = bounce_server(publichost, admin_privkey_path, privatehost, useraccesskeyid, usersecretkey, usertoken, producttoken, bucketname, oldsecrets, stdout, stderr, secretsfile) # Disabled for now. #initialize_statmover_source(publichost, monitor_privkey_path, admin_privkey_path, # sinkname_suffix, [instance.instance_id, 'SSEC2s']) # XXX We probably need to rethink this: append_record(FilePath(serverinfopath), instance.launch_time, instance.instance_id, publichost) print >>stderr, "Signup done." d4 = defer.succeed(None) if not oldsecrets: d4.addCallback(lambda ign: send_signup_confirmation(publichost, customer_name, customer_email, furl, customer_keyinfo, stdout, stderr) ) return d4
def render(self, request): print >>self.out, "Got activation request:", request.args name = self.get_arg(request, 'Name') email = self.get_arg(request, 'Email') activationkey = self.get_arg(request, 'ActivationKey') productcode = self.get_arg(request, 'ProductCode') publickey = self.get_arg(request, 'PublicKey') activation_requests_fp = self.basefp.child(ACTIVATION_REQUESTS_FILE) signups_fp = self.basefp.child(SIGNUPS_FILE) append_record(activation_requests_fp, activationkey, productcode, name, email, publickey) request.setResponseCode(200) if not (activationkey and productcode): return ACTIVATIONREQ_RESPONSE_MISSING_KEY_HTML elif activationkey in successful_activationkeys: return ACTIVATIONREQ_RESPONSE_ALREADY_SUCCEEDED_HTML elif activationkey in all_activationkeys: return ACTIVATIONREQ_RESPONSE_ALREADY_USED_HTML elif not name: return ACTIVATIONREQ_RESPONSE_MISSING_NAME_HTML % {"activationkey": activationkey, "productcode": productcode, "productfullname": get_full_name(productcode, self.products)} elif "%" in email or not "@" in email: return ACTIVATIONREQ_RESPONSE_MISSING_OR_INVALID_EMAIL_HTML % {"activationkey": activationkey, "productcode": productcode, "productfullname": get_full_name(productcode, self.products)} print >>self.out, "Yay! Someone signed up :-)" request.write(ACTIVATIONREQ_RESPONSE_NORMAL_HTML) # None of these fields can contain newlines because they would be quoted by get_arg. stdin = ("%s\n"*5) % (activationkey, productcode, name, email, publickey, ) stdout = RequestOutputStream(request, tee=self.out) stderr = self.out def when_done(): try: successful_activationkeys.add(activationkey) all_activationkeys.add(activationkey) append_record(signups_fp, 'success', activationkey, productcode, name, email, publickey) except Exception: # The request really did succeed, we just failed to record that it did. Log the error locally. traceback.print_exc(100, stderr) request.write(SUCCEEDED_HTML) else: request.write(SUCCEEDED_HTML) finally: request.finish() def when_failed(): try: all_activationkeys.add(activationkey) append_record(signups_fp, 'failure', activationkey, productcode, name, email, publickey) except Exception: traceback.print_exc(100, stderr) request.write(FAILED_HTML) else: request.write(FAILED_HTML) finally: request.finish() try: flappcommand.run(stdin, stdout, stderr, when_done, when_failed) except Exception: traceback.print_exc(100, stdout) when_failed() # http://twistedmatrix.com/documents/10.1.0/web/howto/web-in-60/asynchronous.html return NOT_DONE_YET
def write_serverinfo(pathtoserverinfo, remotepropstuplelist): serverinfofp = FilePath(pathtoserverinfo) serverinfofp.setContent("") for rpt in remotepropstuplelist: append_record(serverinfofp, rpt[0], rpt[1], pubIPextractor(rpt[2]))