def instance(): """ Creates an EC2 instance from an Ubuntu AMI and configures it as a Django server with nginx + gunicorn """ # Record the starting time and print a starting message start_time = time.time() print(_green("Started...")) # Use boto to create an EC2 instance env.host_string = _create_ec2_instance() print(_green("Waiting 60 seconds for server to boot...")) time.sleep(60) # Configure the instance that was just created for item in tasks.configure_instance: try: print(_yellow(item['message'])) except KeyError: pass globals()["_" + item['action']](item['params']) # Print out the final runtime and the public dns of the new instance end_time = time.time() print(_green("Runtime: %f minutes" % ((end_time - start_time) / 60))) print(_green("\nPLEASE ADD ADDRESS THIS TO YOUR ")), print(_yellow("project_conf.py")), print(_green(" FILE UNDER ")), print(_yellow("fabconf['EC2_INSTANCES'] : ")), print(_green(env.host_string))
def server(): """ Creates an EC2 Instance """ print(_yellow("Creating EC2 instance...")) conn = boto.ec2.connect_to_region( AWS_REGION, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY ) image = conn.get_all_images(AWS_AMIS) group = conn.get_all_security_groups(groupnames=[AWS_SECURITY])[0] reservation = image[0].run( 1, 1, key_name=AWS_KEYPAIR, security_groups=AWS_SECURITY, instance_type=AWS_INSTANCE_TYPE ) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name": INSTANCE_NAME_TAG}) while instance.state == u"pending": print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name)) return instance.public_dns_name
def reload_nginx(): """ Reloads the nginx config files and restarts nginx """ start = check_hosts() print(_yellow("Reloading the nginx config files...")) # Stop old nginx process sudo("service nginx stop") # Load the nginx config files print(_yellow("Configuring nginx")) put(_r("%(FAB_CONFIG_PATH)s/templates/nginx.conf"), _r("/home/%(SERVER_USERNAME)s/nginx.conf")) sudo("mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old") sudo(_r("mv /home/%(SERVER_USERNAME)s/nginx.conf /etc/nginx/nginx.conf")) sudo("chown root:root /etc/nginx/nginx.conf") _put_template({"template": "%(FAB_CONFIG_PATH)s/templates/nginx-app-proxy", "destination": "/home/%(SERVER_USERNAME)s/%(PROJECT_NAME)s"}) sudo("rm -rf /etc/nginx/sites-enabled/default") sudo(_r("mv /home/%(SERVER_USERNAME)s/%(PROJECT_NAME)s /etc/nginx/sites-available/%(PROJECT_NAME)s")) sudo(_r("chown root:root /etc/nginx/sites-available/%(PROJECT_NAME)s")) print(_yellow("Restarting nginx")) sudo("/etc/init.d/nginx restart") time_diff = time.time() - start print(_yellow("Finished reloading nginx in %.2fs" % time_diff))
def launch_instance(): print(_green("Launching instance of %s..." % env.ec2_amis[0])) conn = boto.ec2.connect_to_region(env.ec2_region) reservation = conn.run_instances( image_id=env.ec2_amis[0], key_name=env.ec2_keypair, security_groups=env.ec2_secgroups, instance_type=env.ec2_instancetype, user_data=env.ec2_userdata) instance = reservation.instances[0] while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(15) instance.update() while not instance.public_dns_name: print(_yellow("Waiting for Public DNS")) time.sleep(15) instance.update() print(_green("Public DNS: %s" % instance.public_dns_name)) print(_green("Public IP address: %s" % instance.ip_address)) print(_green("Instance state: %s" % instance.state)) print(_green("Instance ID: %s" % instance.id)) print(_green("Waiting 30 seconds for instance to boot...")) time.sleep(30) return instance
def _run_task(task, start_message, finished_message): """ Tasks a task from tasks.py and runs through the commands on the server """ # Get the hosts and record the start time env.hosts = fabconf['EC2_INSTANCES'] start = time.time() # Check if any hosts exist if env.hosts == []: print( "There are EC2 instances defined in project_conf.py, please add some instances and try again" ) print("or run 'fab spawn_instance' to create an instance") return # Print the starting message print(_yellow(start_message)) # Run the task items for item in task: try: print(_yellow(item['message'])) except KeyError: pass globals()["_" + item['action']](item['params']) # Print the final message and the elapsed time print(_yellow("%s in %.2fs" % (finished_message, time.time() - start)))
def reload_nginx(): """ Reloads the nginx config files and restarts nginx """ start = check_hosts() print(_yellow("Reloading the nginx config files...")) # Stop old nginx process sudo("service nginx stop") # Load the nginx config files print(_yellow("Configuring nginx")) put(_r("%(FAB_CONFIG_PATH)s/templates/nginx.conf"), _r("/home/%(SERVER_USERNAME)s/nginx.conf")) sudo("mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old") sudo(_r("mv /home/%(SERVER_USERNAME)s/nginx.conf /etc/nginx/nginx.conf")) sudo("chown root:root /etc/nginx/nginx.conf") _put_template({ "template": "%(FAB_CONFIG_PATH)s/templates/nginx-app-proxy", "destination": "/home/%(SERVER_USERNAME)s/%(PROJECT_NAME)s" }) sudo("rm -rf /etc/nginx/sites-enabled/default") sudo( _r("mv /home/%(SERVER_USERNAME)s/%(PROJECT_NAME)s /etc/nginx/sites-available/%(PROJECT_NAME)s" )) sudo(_r("chown root:root /etc/nginx/sites-available/%(PROJECT_NAME)s")) print(_yellow("Restarting nginx")) sudo("/etc/init.d/nginx restart") time_diff = time.time() - start print(_yellow("Finished reloading nginx in %.2fs" % time_diff))
def create_server(): """ Creates EC2 Instance """ print(_green("Started...")) print(_yellow("...Creating EC2 instance...")) conn = boto.ec2.connect_to_region(settings.EC2_REGION, aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY) image = conn.get_image(settings.EC2_AMI) reservation = image.run(1, 1, key_name=settings.EC2_KEY_PAIR, security_groups={settings.EC2_SECURITY}, instance_type=settings.EC2_INSTANCE_TYPE) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name":settings.EC2_TAG}) while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name)) return instance.public_dns_name
def execute(self): if not boolify(self._config.get('enable_executescript_command', True)): return self._abort( "This command has been disabled by your administrator.") script = self._job['options'][0] if 'options' in self._job and len( self._job['options']) > 0 else None module_name = self._job['options'][1] if 'options' in self._job and len( self._job['options']) > 1 else None execution_strategy = self._job['options'][ 2] if 'options' in self._job and len( self._job['options']) > 2 else None if execution_strategy == 'single': # option[3] is a single Host IP fabric_execution_strategy = None safe_deployment_strategy = None single_host_ip = self._job['options'][ 3] if 'options' in self._job and len( self._job['options']) > 3 else None else: # option[2] is fabric type, option[3] might be Safe deploy group param fabric_execution_strategy = execution_strategy safe_deployment_strategy = self._job['options'][ 3] if 'options' in self._job and len( self._job['options']) > 3 else None single_host_ip = None try: log(_green("STATE: Started"), self._log_file) try: if not script or not script.strip(): return self._abort("No valid script provided") script_data = b64decode_utf8(script) allowed_shebang = ('#!/bin/bash', '#! /bin/bash', '#!/bin/sh', '#! /bin/sh') if not script_data.startswith(allowed_shebang): return self._abort( "No valid shell script provided (shebang missing)") except: return self._abort("No valid script provided") if single_host_ip: log( _yellow("Executing script on a single host: %s" % single_host_ip), self._log_file) self._exec_script_single_host(script_data, module_name, single_host_ip) else: log(_yellow("Executing script on every running instance"), self._log_file) self._exec_script(script_data, module_name, fabric_execution_strategy, safe_deployment_strategy) self._worker.update_status( "done", message=self._get_notification_message_done()) log(_green("STATE: End"), self._log_file) except Exception as e: self._worker.update_status( "failed", message=self._get_notification_message_failed(e)) log(_red("STATE: End"), self._log_file)
def _create_ec2_instance(): """ Creates EC2 Instance """ print(_yellow("Creating instance")) conn = boto.ec2.connect_to_region( ec2_region, aws_access_key_id=fabconf['AWS_ACCESS_KEY'], aws_secret_access_key=fabconf['AWS_SECRET_KEY']) image = conn.get_all_images(ec2_amis) reservation = image[0].run(1, 1, ec2_keypair, ec2_secgroups, instance_type=ec2_instancetype) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name": fabconf['INSTANCE_NAME_TAG']}) while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name)) return instance.public_dns_name
def stop_instance(name): """ Stop all servers with the given name """ print(_green("Started stopping {}...".format(name))) conn = connect_to_ec2() filters = {"tag:Name": name} for reservation in conn.get_all_instances(filters=filters): for instance in reservation.instances: if "stopped" in str(instance._state): print "instance {} is already stopped".format(instance.id) continue if "terminated" in str(instance._state): print "instance {} has been terminated".format(instance.id) continue else: print instance._state print (instance.id, instance.tags['Name']) if raw_input("stop? (y/n) ").lower() == "y": print(_yellow("Stopping {}".format(instance.id))) conn.stop_instances(instance_ids=[instance.id]) while instance.state != u'stopped': print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() if raw_input("Delete from ssh/config? (y/n) ").lower() == "y": remove_ssh_config(name)
def _create_ec2_instance(): """ Creates EC2 Instance """ print(_yellow("Creating instance")) conn = boto.ec2.connect_to_region(ec2_region, aws_access_key_id=fabconf['AWS_ACCESS_KEY'], aws_secret_access_key=fabconf['AWS_SECRET_KEY']) image = conn.get_all_images(ec2_amis) reservation = image[0].run(1, 1, ec2_keypair, ec2_secgroups, instance_type=ec2_instancetype) this_instance = reservation.instances[0] conn.create_tags([this_instance.id], {"Name": fabconf['INSTANCE_NAME_TAG']}) while this_instance.state == u'pending': print(_yellow("Instance state: %s" % this_instance.state)) time.sleep(10) this_instance.update() print(_green("Instance state: %s" % this_instance.state)) print(_green("Public dns: %s" % this_instance.public_dns_name)) return this_instance.public_dns_name
def create_server(): """ Creates EC2 Instance """ print(_green("Started...")) print(_yellow("...Creating EC2 instance...")) conn = boto.ec2.connect_to_region( settings.EC2_REGION, aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY) image = conn.get_image(settings.EC2_AMI) reservation = image.run(1, 1, key_name=settings.EC2_KEY_PAIR, security_groups={settings.EC2_SECURITY}, instance_type=settings.EC2_INSTANCE_TYPE) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name": settings.EC2_TAG}) while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name)) return instance.public_dns_name
def _run_task(task, start_message, finished_message): """ Tasks a task from tasks.py and runs through the commands on the server """ # Get the hosts and record the start time env.hosts = fabconf['EC2_INSTANCES'] start = time.time() # Check if any hosts exist if env.hosts == []: print("There are EC2 instances defined in project_conf.py, please add some instances and try again") print("or run 'fab spawn_instance' to create an instance") return # Print the starting message print(_yellow(start_message)) # Run the task items for item in task: try: print(_yellow(item['message'])) except KeyError: pass globals()["_" + item['action']](item['params']) # Print the final message and the elapsed time print(_yellow("%s in %.2fs" % (finished_message, time.time() - start)))
def launch_instance(count): """ Provisions ec2 instance(s). For eg, to launch 3 instances you would run: fab launch_instance:3 """ print(_green("Started........")) print(_yellow("...............Creating EC2 instance(s)...")) conn = boto.connect_ec2(aws_access_key_id="XXXXX", aws_secret_access_key="XXXXXX") image = conn.get_all_images("ami-4b814f22") reservation = image[0].run(count, count, key_name="fabric", security_groups=['default'], instance_type="m1.small", user_data=get_script()) instance_list = reservation.instances print instance_list for instance in instance_list: conn.create_tags([instance.id], {"Name":"jetty"}) while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(5) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name))
def _create_server(): """ Creates EC2 Instance """ print(_yellow("Creating instance")) conn = boto.connect_ec2(ec2_key, ec2_secret) assert ec2_amis is not None image = conn.get_all_images(ec2_amis) reservation = image[0].run(1, 1, ec2_keypair, ec2_secgroups, instance_type=ec2_instancetype) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name": fabconf['INSTANCE_NAME_TAG']}) conn.create_tags([instance.id], {"Env": env.environment}) conn.create_tags([instance.id], {"Recipe": fabconf['INSTANCE_RECIPE']}) while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name)) return instance.public_dns_name, instance.id
def terminate_instance(name): """ Terminates all servers with the given name """ print(_green("Started terminating {}...".format(name))) conn = connect_to_ec2() filters = {"tag:Name": name} for reservation in conn.get_all_instances(filters=filters): for instance in reservation.instances: if "terminated" in str(instance._state): print "instance {} is already terminated".format(instance.id) continue else: print instance._state print(instance.id, instance.tags['Name']) if raw_input("terminate? (y/n) ").lower() == "y": print(_yellow("Terminating {}".format(instance.id))) conn.terminate_instances(instance_ids=[instance.id]) os.remove( os.path.join(env.ssh_directory, ''.join([name, '.json']))) # noqa os.remove( os.path.join(env.fab_hosts_directory, ''.join([name, '.txt']))) print(_yellow("Terminated"))
def create_instances(): """ Creates EC2 Instance """ print(_green("Started...")) print(_yellow("...Creating EC2 instance(s)...")) ec2_region = config.get(Config.AWS, Config.AWS_EC2_REGION) ec2_key = config.get(Config.AWS, Config.AWS_ACCESS_KEY) ec2_secret = config.get(Config.AWS, Config.AWS_SECRET_KEY) conn = boto.ec2.connect_to_region(ec2_region, aws_access_key_id=ec2_key, aws_secret_access_key=ec2_secret) ec2_ami = config.get(Config.AWS, Config.AWS_EC2_AMI) image = conn.get_image(ec2_ami) ec2_key_pair = config.get(Config.AWS, Config.AWS_EC2_KEY_PAIR) ec2_instance_type = config.get(Config.AWS, Config.AWS_EC2_INSTANCE_TYPE) num_instances = config.get(Config.AWS, Config.AWS_NUM_INSTANCES) # must give num_instances twice because 1 min num and 1 max num reservation = image[0].run(num_instances, num_instances, key_name=ec2_key_pair, instance_type=ec2_instance_type) while check_instances_pending(reservation.instances): print(_yellow("Instances still pending")) time.sleep(10) for instance in reservation.instances: print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name))
def reload_supervisor(): """ Reloads the supervisor config files and restarts supervisord """ start = check_hosts() print(_yellow("Reloading the supervisor config files...")) # Stop old supervisor process sudo("supervisorctl stop all") sudo("killall supervisord") # Setup supervisor print(_yellow("Configuring supervisor")) run(_r("echo_supervisord_conf > /home/%(SERVER_USERNAME)s/supervisord.conf")) _put_template({"template": "%(FAB_CONFIG_PATH)s/templates/supervisord.conf", "destination": "/home/%(SERVER_USERNAME)s/my.supervisord.conf"}) run(_r("cat /home/{SERVER_USERNAME:s}/my.supervisord.conf >> /home/{SERVER_USERNAME:s}/supervisord.conf")) run(_r("rm /home/{SERVER_USERNAME:s}/my.supervisord.conf")) sudo(_r("mv /home/{SERVER_USERNAME:s}/supervisord.conf /etc/supervisord.conf")) sudo("supervisord") put(_r("{FAB_CONFIG_PATH:s}/templates/supervisord-init"), _r("/home/{SERVER_USERNAME:s}/supervisord-init")) sudo(_r("mv /home/{SERVER_USERNAME:s}/supervisord-init /etc/init.d/supervisord")) sudo("chmod +x /etc/init.d/supervisord") sudo("update-rc.d supervisord defaults") # Restart supervisor sudo("supervisorctl start all") # Print the final message and the elapsed time print(_yellow("%s in %.2fs" % ("Finished reloading supervisor", time.time() - start)))
def launch_instance(count): """ Provisions ec2 instance(s). For eg, to launch 3 instances you would run: fab launch_instance:3 """ print (_green("Started........")) print (_yellow("...............Creating EC2 instance(s)...")) conn = boto.connect_ec2(aws_access_key_id="XXXXX", aws_secret_access_key="XXXXXX") image = conn.get_all_images("ami-4b814f22") reservation = image[0].run( count, count, key_name="fabric", security_groups=["default"], instance_type="m1.small", user_data=get_script() ) instance_list = reservation.instances print instance_list for instance in instance_list: conn.create_tags([instance.id], {"Name": "jetty"}) while instance.state == u"pending": print (_yellow("Instance state: %s" % instance.state)) time.sleep(5) instance.update() print (_green("Instance state: %s" % instance.state)) print (_green("Public dns: %s" % instance.public_dns_name))
def launch_instance(): print(_green("Launching instance of %s..." % env.ec2_amis[0])) conn = boto.ec2.connect_to_region(env.ec2_region) reservation = conn.run_instances( image_id=env.ec2_amis[0], key_name=env.ec2_keypair, security_groups=env.ec2_secgroups, instance_type=env.ec2_instancetype, user_data=env.ec2_userdata) instance = reservation.instances[0] while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(15) instance.update() while not instance.public_dns_name: print(_yellow("Waiting for Public DNS")) time.sleep(15) instance.update() print(_green("Public DNS: %s" % instance.public_dns_name)) print(_green("Public IP address: %s" % instance.ip_address)) print(_green("Instance state: %s" % instance.state)) print(_green("Instance ID: %s" % instance.id)) print(_green("Waiting 60 seconds for instance to boot...")) time.sleep(60) return instance
def create_server(): """ Creates EC2 Instance """ print(_green("Started...")) print(_yellow("...Creating EC2 instance...")) conn = boto.ec2.connect_to_region('us-west-2', aws_access_key_id=ec2_key, aws_secret_access_key=ec2_secret) image = conn.get_all_images(ec2_amis) group = conn.get_all_security_groups(groupnames=['quicklaunch-2'])[0] group.authorize(ip_protocol='tcp', from_port='22', to_port='22', cidr_ip='0.0.0.0/0') group.authorize(ip_protocol='tcp', from_port='80', to_port='80', cidr_ip='0.0.0.0/0') reservation = image[0].run(1, 1, key_name=ec2_key_pair, security_groups=ec2_security, instance_type=ec2_instancetype) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name":config['INSTANCE_NAME_TAG']}) while instance.state == u'pending': print(_yellow("Instance state: %s" % instance.state)) time.sleep(10) instance.update() print(_green("Instance state: %s" % instance.state)) print(_green("Public dns: %s" % instance.public_dns_name)) return instance.public_dns_name # install the things # deploy with github
def install_apache_conf(): """ Backup old httpd.conf and install new one. """ print(_yellow('>>> starting %s()' % _fn())) require('settings', provided_by=[staging, production]) if env.settings == 'vagrant': upload_template('apache/vagrant.conf.template', '/etc/apache2/sites-available/%(project_name)s' % env, context=env, use_jinja=True, use_sudo=True) upload_template('apache/config.wsgi.template', '/etc/apache2/sites-available/%(project_name)s.wsgi' % env, context=env, use_jinja=True, use_sudo=True) sudo('a2ensite %(project_name)s' % env) else: # get port apache is listening on env.port = run('grep "Listen" %(path)s/apache2/conf/httpd.conf | cut -f2 -d " "' % env) print(_yellow('*** apache is listening on port %(port)s' % env)) upload_template('apache/webfaction.conf.template', '%(path)s/apache2/conf/httpd.conf' % env, context=env, use_jinja=True) upload_template('apache/config.wsgi.template', '%(path)s/apache2/conf/%(project_name)s.wsgi' % env, context=env, use_jinja=True)
def instance(): """ Creates an EC2 instance from an Ubuntu AMI and configures it as a Django server with nginx + gunicorn """ # Record the starting time and print a starting message start_time = time.time() print(_green("Started...")) # Use boto to create an EC2 instance env.host_string = _create_ec2_instance() print(_green("Waiting 30 seconds for server to boot...")) time.sleep(30) # Configure the instance that was just created for item in tasks.configure_instance: try: print(_yellow(item['message'])) except KeyError: pass globals()["_" + item['action']](item['params']) # Print out the final runtime and the public dns of the new instance end_time = time.time() print(_green("Runtime: %f minutes" % ((end_time - start_time) / 60))) print(_green("\nPLEASE ADD ADDRESS THIS TO YOUR ")), print(_yellow("project_conf.py")), print(_green(" FILE UNDER ")), print(_yellow("fabconf['EC2_INSTANCES'] : ")), print(_green(env.host_string))
def ami_status(ec2conn, ami_id): getami_id = ec2conn.get_all_images(image_ids = ami_id) logging.info (_yellow( 'AMI is ') + _red(getami_id[0].state) ) while getami_id[0].state != 'available' : logging.info (_yellow( 'AMI is still ') + _red(getami_id[0].state) + ', sleeping for 5 seconds..') time.sleep(5) getami_id[0].update() logging.debug(_green( 'AMI Status' ) + _red(getami_id[0].state)) return getami_id[0].state
def vagrant_command(action, vm_name): config = ConfigParser() config.read([os.path.abspath('lib/cheflab.ini')]) try: vcwd = config.get('default', 'VAGRANT_CWD') vconf = config.get('default', 'VAGRANT_VAGRANTFILE') vdot = config.get('default', 'VAGRANT_DOTFILE_PATH') except (NoSectionError, NoOptionError, ParsingError): sendError('Error parsing config file, check "cheflab.ini" under lib') raise cheflab = vagrant.Vagrant(quiet_stdout=False, quiet_stderr=False) os_env = os.environ.copy() os_env['VAGRANT_CWD'] = vcwd os_env['VAGRANT_VAGRANTFILE'] = vconf os_env['VAGRANT_DOTFILE_PATH'] = vdot cheflab.env = os_env conf = read_conf() if action == "up": run_gitmodules() cheflab.up(provision=True, vm_name=vm_name) get_keys(conf) sys.exit(0) elif action == "destroy": cheflab.destroy(vm_name=vm_name) destory_config(conf) sys.exit(0) elif action == "start": run_gitmodules() cheflab.up(vm_name=vm_name) sys.exit(0) elif action == "reload": run_gitmodules() cheflab.reload(vm_name=vm_name) sys.exit(0) elif action == "stop": cheflab.halt(vm_name=vm_name) sys.exit(0) elif action == "ssh": cheflab.ssh(vm_name=vm_name) sys.exit(0) elif action == "status": servers = cheflab.status(vm_name=vm_name) for server in servers: sendInfo("Server Name: " + _yellow(server.name) + " Server Status: " + _yellow(server.state)) sys.exit(0) elif action == "validate_vms": grabinfo = [] servers = cheflab.status(vm_name=vm_name) for server in servers: grabinfo.append(server.name) return grabinfo
def heroku_select_or_create_app(account_name): account_applications = local('heroku apps --account %s' % account_name, capture=True) app = prompt(_white("Type the name of the application you wish to use, or press " "Enter to create a new one:\n\t%s%s\nSelection: " % (_yellow('* '), _yellow('\n\t* '.join(account_applications.split('\n')))))) or None if app: if app in account_applications: return app app = prompt(_white("Type the name of the app you would like to create: ")) local('heroku apps:create %s --account %s' % (app, account_name)) return app
def get_puppet_properties(): properties = {} print(_white('Enter setup information. Defaults in %s. ' % _green('green'))) prompt( _white( 'To keep default, simply press enter when prompted. \nAll optional unless noted. Press enter to continue.' )) properties['bind'] = prompt( _white('CouchDB bind address %s ' % _green('[0.0.0.0]:'))) or '0.0.0.0' properties['database_dir'] = prompt( _white('CouchDB database dir %s ' % _green('[/usr/local/var/lib/couchdb]:'))) or None ''' admin user info ''' properties['admin_user'] = prompt( _white('CouchDB admin user %s ' % _green('[None]:'))) or None if properties['admin_user']: properties['admin_password'] = None while not properties['admin_password']: properties['admin_password'] = prompt( _white( 'CouchDb admin password %s ' % _green('[None] :'))) or None ''' masterless/slave setup ''' properties['couchdb_masterless_mode'] = bool( prompt( _yellow('Run as part of a masterless cluster %s ' % _green('[y/N]:')))) or False if not properties['couchdb_masterless_mode']: properties['slave_mode'] = bool( prompt( _yellow('Run as a slave to another master %s ' % _green('[y/N]:')))) or False else: properties['slave_mode'] = False if properties['couchdb_masterless_mode'] or properties['slave_mode']: properties['couchdb_master_hostname'] = None properties['couchdb_master_ip'] = None while not properties['couchdb_master_hostname']: properties['couchdb_master_hostname'] = prompt( _white( 'Hostname of CouchDB master server -- required: ')) or None while not properties['couchdb_master_ip']: properties['couchdb_master_ip'] = prompt( _white('IP Address of CouchDB master server -- required: ') ) or None return properties
def heroku_select_account(): # select heroku account accounts = local('heroku accounts', capture=True) or None if accounts: return prompt(_white("Type the name of the Heroku account you would like to use:\n\t%s%s\n" % (_yellow('* '), _yellow('\n\t* '.join(accounts.split('\n')))))) else: add_account = prompt(_white("No heroku toolbelt account was found. Would you like to add one? [n] ")) or None if add_account: account_name = prompt(_white("Type the name of the account you would like to add")) local('heroku accounts:add %s' % account_name) return account_name
def terminate_instance(instance_id): print(_green("Terminating instance...")) conn = boto.ec2.connect_to_region(env.ec2_region) results = conn.terminate_instances(instance_ids=[instance_id]) instance = results[0] while instance.state == u'shutting-down': print(_yellow("Instance state: %s" % instance.state)) time.sleep(15) instance.update() if instance.state == u'terminated': print(_green("Instance terminated.")) else: print(_yellow("Instance state: %s" % instance.state))
def terminate_instance(instance_id): print(_green("Terminating instance...")) conn = boto.ec2.connect_to_region(env.ec2_region) results = conn.terminate_instances(instance_ids=[instance_id]) instance = results[0] while instance.state == u"shutting-down": print(_yellow("Instance state: %s" % instance.state)) time.sleep(15) instance.update() if instance.state == u"terminated": print(_green("Instance terminated.")) else: print(_yellow("Instance state: %s" % instance.state))
def _launch_gpu(): ''' Boots up a new GPU-based instance on EC2. ''' print(_green("Started...")) print(_green("Creating EC2 instance...")) try: # Create new instance conn = boto.ec2.connect_to_region(GPU_INSTANCE_REGION) reservation = conn.run_instances( GPU_INSTANCE_AMI_ID, key_name=GPU_INSTANCE_KEY, instance_type=GPU_INSTANCE_TYPE) # Assumes we're only using one instance instance = reservation.instances[0] # Wait for instance to boot up status = instance.update() while status == 'pending': print(_yellow("Booting instance ...")) time.sleep(10) status = instance.update() # Once instances are alive, do tagging and other post-activation work if status == 'running': print(_green("Instance booted! Tagging ...")) instance.add_tag('Name', GPU_INSTANCE_NAME) # Wait until instance is accessible via SSH sshable = False while sshable == False: print(_yellow("Waiting for SSH connection (this might take a minute) ...")) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((instance.public_dns_name, 22)) sshable = True print(_green("SSH is now accessible!")) except socket.error as e: pass s.close() # Wrapup print(_green("Done!")) print(_green("ID: %s" % instance.id)) print(_green("Public DNS: %s" % instance.public_dns_name)) except: print(_red('Error creating instance.')) raise return
def render(slug): ''' Compile app template into HTML ''' if not os.path.exists('apps/%s' % slug): print _red('--- App with this slug can\'t be found(%s)' % slug) return env = Environment(loader=FileSystemLoader('templates')) template = env.get_template('apps/app_template.html') # Manifest data = '' with open("apps/%s/manifest.json" % slug, "r") as manifest: data = manifest.read().replace('\n', '') context = json.loads(data) # Description description = '' with open("apps/%s/description.md" % slug, "r") as desc_file: description = desc_file.read() # Images to /static/ if not os.path.exists('static/apps/%s' % slug): os.makedirs('static/apps/%s' % slug) if context['images'].get('logo'): shutil.copy2('apps/%s/images/%s' % (slug, context['images']['logo']), 'static/apps/%s' % (slug)) for screenshot in context['images'].get('screenshots'): shutil.copy2('apps/%s/images/screenshots/%s' % (slug, screenshot), 'static/apps/%s' % (slug)) # Description context['description'] = markdown.markdown(description) # Write output if not os.path.exists('templates/apps/%s' % slug): os.makedirs('templates/apps/%s' % slug) else: # remove old files open("templates/apps/%s/index.html" % slug, "w").close() output = open("templates/apps/%s/index.html" % slug, "w") output.write('{% extends "base.html" %}\n{% block body %}\n') output.write(template.render(context)) output.write('\n{% endblock %}') output.close() print _green('--- Done rendering. You can always re-render files with') print _yellow('--- fab render:%s' % slug)
def update_packages(): """ Updates the python packages on the server as defined in requirements/common.txt and requirements/prod.txt """ start = check_hosts() print(_yellow("Updating server packages with pip...")) # Updates the python packages _virtualenv("pip install -r %(PROJECT_PATH)s/requirements/common.txt --upgrade") _virtualenv("pip install -r %(PROJECT_PATH)s/requirements/prod.txt --upgrade") time_diff = time.time() - start print(_yellow("Finished updating python packages in %.2fs" % time_diff))
def delete_bucket(bucket_name): '''Delete a bucket''' start_time = time.time() conn = S3Connection(ec2_key, ec2_secret) delete = prompt('Are you sure you want to delete this bucket (Y/N): ') if delete.upper() == 'Y': try: bucket = conn.get_bucket(bucket_name=bucket_name) print(_green("Deleting bucket...")) conn.delete_bucket(bucket) print _yellow('Bucket "%s" successfully deleted' % bucket_name) except Exception, e: print(_red('Delete bucket error: {}'.format(str(e))))
def check_image_availability(ami_id): print(_green("Building AMI...")) conn = boto.ec2.connect_to_region(env.ec2_region) image = conn.get_image(ami_id) while image.state == u"pending": print(_yellow("AMI state: %s" % image.state)) time.sleep(15) image.update() if image.state == u"available": print(_green("AMI is ready.")) print(_green("AMI ID: %s" % image.id)) print(_green("AMI Name: %s" % image.name)) print(_green("AMI Description: %s" % image.description)) else: print(_yellow("AMI state: %s" % image.state))
def download_services(): """ Downloads the software you want on your instance """ print(_yellow("Installing services...")) for item in service_list: try: print(_yellow(item["message"])) except KeyError: pass globals()[item["action"]](item["params"]) for pkg in params: _sudo("apt-get install -qq %s" % pkg)
def check_image_availability(ami_id): print(_green("Building AMI...")) conn = boto.ec2.connect_to_region(env.ec2_region) image = conn.get_image(ami_id) while image.state == u'pending': print(_yellow("AMI state: %s" % image.state)) time.sleep(15) image.update() if image.state == u'available': print(_green("AMI is ready.")) print(_green("AMI ID: %s" % image.id)) print(_green("AMI Name: %s" % image.name)) print(_green("AMI Description: %s" % image.description)) else: print(_yellow("AMI state: %s" % image.state))
def launch_instance(): print(_green("Launching instance of %s..." % env.ec2_amis[0])) conn = boto.ec2.connect_to_region(env.ec2_region) reservation = conn.run_instances( image_id=env.ec2_amis[0], key_name=env.ec2_keypair, security_groups=env.ec2_secgroups, instance_type=env.ec2_instancetype, user_data=env.ec2_userdata, ) instance = reservation.instances[0] while instance.state == u"pending": print(_yellow("Instance state: %s" % instance.state)) time.sleep(15) instance.update() while not instance.public_dns_name: print(_yellow("Waiting for Public DNS")) time.sleep(15) instance.update() print(_green("Public DNS: %s" % instance.public_dns_name)) print(_green("Public IP address: %s" % instance.ip_address)) print(_green("Instance state: %s" % instance.state)) print(_green("Instance ID: %s" % instance.id)) print(_green("Waiting for instance to boot...")) status = _get_instance_status(instance, conn) while not ( status.system_status.status == "ok" and status.instance_status.status == "ok" and status.system_status.details[u"reachability"] == u"passed" ): print( _yellow( "System Status: {} - Instance Status: {} - Reachability: {}".format( status.system_status.status, status.instance_status.status, status.system_status.details[u"reachability"], ) ) ) time.sleep(15) status = _get_instance_status(instance, conn) return instance
def build_databag(): print(_yellow("--BUILDING DATA BAG--")) # Add ec2 host to tmp settings tmp_settings = {'EC2_HOST': env.host_string} # Add base settings to tmp settings with open(env.app_settings_base, 'r') as f: tmp_settings.update(json.load(f)) # Add deploy key to tmp settings with open(env.app_settings_deploy_key, 'r') as f: tmp_settings.update({ 'GITHUB_DEPLOY_KEY': f.read().replace('\n', '\\n') }) # Write out tmp settings tmp_settings_path = 'chef_repo/tmp_settings.json' with open(tmp_settings_path, 'w') as f: json.dump(tmp_settings, f) data_bag_key_exists = os.path.isfile('chef_repo/data_bag_key') with settings(warn_only=True): with lcd('chef_repo'): if not data_bag_key_exists: # Create data bag key if not exists local('openssl rand -base64 512 > data_bag_key') # Create data bag local('knife solo data bag create config config_1 --json-file tmp_settings.json') # Delete tmp settings file os.remove(tmp_settings_path) return True
def deploy_app(name): print(_yellow("--RUNNING CHEF--")) node = "./nodes/deploy_node.json".format(name=name) with lcd('chef_files'): try: # skip updating the Berkshelf cookbooks to save time try: os.rename("chef_files/Berksfile", "chef_files/hold_Berksfile") except OSError: pass local("knife solo cook -i {key_file} {host} {node}".format( key_file=env.aws_ssh_key_path, host=env.host_string, node=node)) restart(name) except Exception as e: print e finally: try: os.rename("chef_files/hold_Berksfile", "chef_files/Berksfile") except OSError: pass
def init_dbdump(): """ Installs Cron job to dump database daily """ if (not is_sudo()): mode_sudo() print(_yellow("...Initialising DB Dump...")) # noting changes so read? t = loader.get_template('proddump-daily-script.txt') c = Context({ "readonly_database": settings.READONLY_DATABASE_INSTANCE, "readonly_database_user": settings.READONLY_DATABASE_USER, "readonly_database_password": settings.READONLY_DATABASE_PASSWORD, "readonly_database_instance": settings.READONLY_DATABASE_INSTANCE }) file_write("/etc/cron/daily.d/proddump-daily", t.render(c), mode="00755", owner=settings.ADMIN_USER, group=settings.ADMIN_GROUP, scp=True)
def build_databag(): print(_yellow("--BUILDING DATA BAG--")) # Add ec2 host to tmp settings tmp_settings = {'EC2_HOST': env.host_string} # Add base settings to tmp settings with open(env.app_settings_base, 'r') as f: tmp_settings.update(json.load(f)) # Add deploy key to tmp settings with open(env.app_settings_deploy_key, 'r') as f: tmp_settings.update( {'GITHUB_DEPLOY_KEY': f.read().replace('\n', '\\n')}) # Write out tmp settings tmp_settings_path = 'chef_repo/tmp_settings.json' with open(tmp_settings_path, 'w') as f: json.dump(tmp_settings, f) data_bag_key_exists = os.path.isfile('chef_repo/data_bag_key') with settings(warn_only=True): with lcd('chef_repo'): if not data_bag_key_exists: # Create data bag key if not exists local('openssl rand -base64 512 > data_bag_key') # Create data bag local( 'knife solo data bag create config config_1 --json-file tmp_settings.json' ) # Delete tmp settings file os.remove(tmp_settings_path) return True
def maintenance_off(): """ Turn maintenance mode off. """ print(_yellow('>>> starting %s()' % _fn())) require('settings', provided_by=[vagrant]) run('rm -f %(repo_path)s/.upgrading' % env)
def update_packages(): """ Updates the python packages on the server as defined in requirements/common.txt and requirements/prod.txt """ start = check_hosts() print(_yellow("Updating server packages with pip...")) # Updates the python packages _virtualenv( "pip install -r %(PROJECT_PATH)s/requirements/common.txt --upgrade") _virtualenv( "pip install -r %(PROJECT_PATH)s/requirements/prod.txt --upgrade") time_diff = time.time() - start print(_yellow("Finished updating python packages in %.2fs" % time_diff))
def dummy(): """ Dummy task for testing """ print(_yellow('>>> starting %s()' % _fn())) require('settings', provided_by=[vagrant]) run('uname -a && hostname && pwd')
def download_corpora(): with cd(VIRTUAL_ENVIRONMENT): with prefix("source bin/activate"): run("pip freeze") print(_yellow("Now downloading textblob packages")) run("python -m textblob.download_corpora") print( _green("Finished Downloading and installing textblob packages") ) with cd(VIRTUAL_ENVIRONMENT): with prefix("source bin/activate"): print(_yellow("Now downloading nltk packages")) run("sudo {0}/bin/python -m nltk.downloader punkt maxent_ne_chunker maxent_treebank_pos_tagger words" .format(VIRTUAL_ENVIRONMENT)) print(_yellow("Now downloading textblob packages"))
def install_chef(): """ Install chef-solo on the server. """ print(_yellow("--INSTALLING CHEF--")) local("knife solo prepare -i {key_file} {host}".format(key_file=env.key_filename, host=env.host_string))
def virtualenv(command): """ Run command in virtualenv. """ print(_yellow('>>> starting {}'.format(_fn()))) with prefix('source {}/bin/activate'.format(env.venv_path)): run(command)
def read_dotenv(dotenv=None, set_heroku=False): """ Read a .env file into os.environ. If not given a path to a dotenv path, does filthy magic stack backtracking to find manage.py and then find the dotenv. """ if dotenv is None: frame_filename = sys._getframe().f_back.f_code.co_filename dotenv = os.path.join(os.path.dirname(frame_filename), '.env') if os.path.isdir(dotenv) and os.path.isfile(os.path.join(dotenv, '.env')): dotenv = os.path.join(dotenv, '.env') if os.path.exists(dotenv): with open(dotenv) as f: for k, v in parse_dotenv(f.read()).items(): try: os.environ.setdefault(k, v) if set_heroku and v: print(_yellow('Setting {}...'.format(k))) local("heroku config:set {0}='{1}'".format(k, v)) except KeyError: error_msg = "Set the {} environment variable".format(k) raise ImproperlyConfigured(error_msg) else: warnings.warn("Not reading {} - it doesn't exist.".format(dotenv), stacklevel=2)
def install_chef(): """ Install chef-solo on the server. """ print(_yellow("--INSTALLING CHEF--")) local("knife solo prepare -i {key_file} {host}".format( key_file=env.aws_ssh_key_path, host=env.host_string))
def update_ubuntu(): """ updates ubuntu packages """ print(_yellow("...Updating Ubuntu...")) with mode_sudo(): run('apt-get update -q -y')
def reload_supervisor(): """ Reloads the supervisor config files and restarts supervisord """ start = check_hosts() print(_yellow("Reloading the supervisor config files...")) # Stop old supervisor process sudo("supervisorctl stop all") sudo("killall supervisord") # Setup supervisor print(_yellow("Configuring supervisor")) run( _r("echo_supervisord_conf > /home/%(SERVER_USERNAME)s/supervisord.conf" )) _put_template({ "template": "%(FAB_CONFIG_PATH)s/templates/supervisord.conf", "destination": "/home/%(SERVER_USERNAME)s/my.supervisord.conf" }) run( _r("cat /home/{SERVER_USERNAME:s}/my.supervisord.conf >> /home/{SERVER_USERNAME:s}/supervisord.conf" )) run(_r("rm /home/{SERVER_USERNAME:s}/my.supervisord.conf")) sudo( _r("mv /home/{SERVER_USERNAME:s}/supervisord.conf /etc/supervisord.conf" )) sudo("supervisord") put(_r("{FAB_CONFIG_PATH:s}/templates/supervisord-init"), _r("/home/{SERVER_USERNAME:s}/supervisord-init")) sudo( _r("mv /home/{SERVER_USERNAME:s}/supervisord-init /etc/init.d/supervisord" )) sudo("chmod +x /etc/init.d/supervisord") sudo("update-rc.d supervisord defaults") # Restart supervisor sudo("supervisorctl start all") # Print the final message and the elapsed time print( _yellow("%s in %.2fs" % ("Finished reloading supervisor", time.time() - start)))
def update(): print(_green("Connecting to EC2 Instance...")) execute(update_git) execute(update_nginx_conf) execute(nginx_status) print(_yellow("...Disconnecting EC2 instance...")) disconnect_all()
def _push_heroku(): """Push the latest code to Heroku.""" print(_cyan('Maintenance mode on.')) local('heroku maintenance:on') print(_yellow('Pushing to Heroku...', bold=True)) local('git push heroku master') local('heroku maintenance:off') print(_cyan('Maintenance mode off.'))
def init_database(): """ Installs Cron job to dump database daily """ if (not is_sudo()): mode_sudo() print(_yellow("...Initialising Database Python...")) # noting changes so read? t = loader.get_template('database.py.txt') for key, value in settings.APPS.items(): app_settings = AppSettings(key, value) c = Context({ "readonly_database": settings.READONLY_DATABASE_INSTANCE, "readonly_database_user": settings.READONLY_DATABASE_USER, "readonly_database_password": settings.READONLY_DATABASE_PASSWORD, "readonly_database_instance": settings.READONLY_DATABASE_INSTANCE, "database_instance": settings.DATABASE_INSTANCE, "database_user": settings.DATABASE_USER, "database_password": settings.DATABASE_PASSWORD, "database_host": settings.DATABASE_HOST, "production": settings.PRODUCTION, "maintenance_landing_page": settings.MAINT, "instance": settings.INSTANCE, "app_name": app_settings.app_name, "admin_name": settings.ADMIN_NAME, "admin_email": settings.ADMIN_EMAIL, "snippet_email": settings.SNIPPET_EMAIL, "aws_access_key": settings.AWS_ACCESS_KEY_ID, "aws_access_secret": settings.AWS_SECRET_ACCESS_KEY, "aws_storage_bucket": settings.STORAGE_BUCKET, "django_secret": settings.DJANGO_SECRET, "piazza_endpoint": settings.PIAZZA_ENDPOINT, "piazza_secret": settings.PIAZZA_SECRET, "piazza_key": settings.PIAZZA_KEY, "smtp_user": settings.SMTP_USER, "smtp_password": settings.SMTP_PASSWORD, "yt_service_developer_key": settings.YT_SERVICE_DEVELOPER_KEY, "google_client_id": settings.GOOGLE_CLIENT_ID, "google_client_secret": settings.GOOGLE_CLIENT_SECRET, "grader_endpoint": settings.GRADER_ENDPOINT }) print settings.ADMIN_USER file_write("/home/" + settings.ADMIN_USER + "/" + app_settings.app_name + "/main/database.py", t.render(c), mode="00644", owner=settings.ADMIN_USER, group=settings.ADMIN_GROUP, scp=True) dir_ensure("/opt/assets", mode="00777", owner="root", group="root")
def health_check(): print(_green("Connecting to EC2 Instance...")) execute(mongo_status) execute(nginx_status) execute(gunicorn_status) execute(disk_usage) execute(ram_usage) print(_yellow("...Disconnecting EC2 instance...")) disconnect_all()
def gunicorn_status(): """ Check if gunicorn is running fine or not. """ print("\n\n\t\t%s" % _yellow("Checking gunicorn status")) with settings(hide("running", "stderr", "stdout")): result = run('ps aux | grep gunicorn') print(_green(result)) return