def refresh(expected=None): """Update local cache of IPs for OpenStack instances. This will write a `farmboy.os.yaml` file of the running instances on OpenStack tagged with `farmboy` and their associated roles. """ conn = client.Client(env.farmboy_os_username, env.farmboy_os_password, env.farmboy_os_tenant_name, env.farmboy_os_auth_url, service_type='compute') max_tries = 30 for i in range(max_tries): util.puts('[os] Fetching running and pending instances') # check for instances found_instances = [] running_instances = conn.servers.list() # TODO(termie): we _probably_ want to just terminate them all and start # new ones, is there a use case for keeping them? for instance in running_instances: if (instance.metadata.get('farmboy') and instance.status == 'ACTIVE' and getattr(instance, 'OS-EXT-STS:task_state') == None): found_instances.append(instance) if not expected: if found_instances: break util.puts('[os] nothing found, retrying in 5 seconds (%d of %d)' % (i, max_tries)) else: if found_instances and len(found_instances) == expected: break util.puts('[os] found %d, expecting %d, retrying in 5 seconds' ' (%d of %d)' % (len(found_instances), expected, i, max_tries)) time.sleep(5) if not found_instances or (expected and len(found_instances) != expected): raise Exception('Did not find enough running instances.') o = {'roledefs': {}} for inst in found_instances: role = str(inst.metadata['farmboy']) ip_address = inst.addresses['private'][0]['addr'] util.puts('[os] found: %s (%s)' % (ip_address, role)) role_l = o['roledefs'].get(role, []) role_l.append(str('%s@%s' % (env.farmboy_os_image_user, ip_address))) o['roledefs'][role] = role_l util.puts('[os] Dumping roledefs to file: %s' % DEFAULT_ROLEDEF_FILE) util.update(o, DEFAULT_ROLEDEF_FILE)
def create_user(name, password=None, user_host=None): if not password: password = util.load('farmboy_mysql_password_%s' % name) if not password: password = _generate_password() util.update({'farmboy_mysql_password_%s' % name: password}) with settings(mysql_user='******', mysql_password=env.farmboy_mysql_password): fabtools.require.mysql.user(name, password, host=user_host)
def init(): """Locally set up basic files for using Vagrant.""" vagrantfile = util.files('vagrant/Vagrantfile') # TODO(termie): local cp or shutil? local('cp %s %s' % (vagrantfile, './Vagrantfile')) #shutil.copy(vagrantfile, './Vagrantfile') util.update({'roledefs': DEFAULT_ROLEDEFS}) fabfile_context = {'roledefs': "util.load_roledefs()", 'keyfile': KEYFILE_S, 'preamble': '', } util.template_file('farmboy/fabfile.py.template', 'fabfile.py', fabfile_context)
def refresh(expected=None): """Update local cache of IPs for AWS instances. This will write a `farmboy.yaml` file of the running instances on AWS tagged with `farmboy` and their associated roles. """ conn = ec2.connect_to_region(env.farmboy_aws_region) max_tries = 15 for i in range(max_tries): util.puts('[aws] Fetching running and pending instances') running_instances = conn.get_only_instances( filters={'tag-key': 'farmboy', 'instance-state-name': 'pending', 'instance-state-name': 'running'}) if not expected: if running_instances: break util.puts('[aws] nothing found, retrying in 5 seconds (%d of %d)' % (i, max_tries)) else: if running_instances and len(running_instances) == expected: break util.puts('[aws] found %d, expecting %d, retrying in 5 seconds' ' (%d of %d)' % (len(running_instances), expected, i, max_tries)) time.sleep(5) if not running_instances or (expected and len(running_instances) != expected): raise Exception('Did not find enough running instances.') o = {'roledefs': {}} for inst in running_instances: role = str(inst.tags['farmboy']) util.puts('[aws] found: %s (%s)' % (inst.ip_address, role)) role_l = o['roledefs'].get(role, []) role_l.append(str('%s@%s' % (env.farmboy_aws_image_user, inst.ip_address))) o['roledefs'][role] = role_l util.puts('[aws] Dumping roledefs to file: %s' % DEFAULT_ROLEDEF_FILE) util.update(o)
def deploy(): """Add mysql to the db hosts.""" # If we don't have a key yet generate one and save it locally if not env.farmboy_mysql_password: new_password = _generate_password() util.update({'farmboy_mysql_password': new_password}) env.farmboy_mysql_password = new_password mysql_password = env.farmboy_mysql_password fabtools.require.mysql.server(password=mysql_password) fabtools.require.files.file( source = util.files('mysql/mysqld.cnf'), path = '/etc/mysql/conf.d/mysqld.cnf', owner = 'root', group = 'root', mode = '644', use_sudo = True) sudo('service mysql restart')
def images(filter=None): """List the images available on the server.""" conn = client.Client(env.farmboy_os_username, env.farmboy_os_password, env.farmboy_os_tenant_name, env.farmboy_os_auth_url, service_type='compute') images = conn.images.list() for i, image in enumerate(images): print '[%d] %s (%s)' % (i, image.name, image.id) print print 'Choose an image from above, or leave blank to skip.' print '(It will be written to %s)' % DEFAULT_ROLEDEF_FILE print number = raw_input('>>> ') if not number: return selected = images[int(number)] o = {'farmboy_os_image_id': str(selected.id)} util.update(o, DEFAULT_ROLEDEF_FILE)