def handle_instance_create(self, payload): context = MonikerContext.get_admin_context() # Fetch the FixedIP Domain fixed_ip_domain = self.central_service.get_domain(context, self.fixed_ip_domain) # For each fixed ip, create an associated record. for fixed_ip in payload['fixed_ips']: record_name = '%(instance_id)s.%(tenant_id)s.%(domain)s' % dict( instance_id=payload['instance_id'], tenant_id=payload['tenant_id'], domain=fixed_ip_domain['name']) record_values = { 'type': 'A' if fixed_ip['version'] == 4 else 'AAAA', 'name': record_name, 'data': fixed_ip['address'], 'managed_resource': True, 'managed_resource_type': u'instance', 'managed_resource_id': payload['instance_id'], } self.central_service.create_record(context, self.fixed_ip_domain, record_values)
def _delete(self, managed=True, resource_id=None, resource_type='instance', criterion={}): """ Handle a generic delete of a fixed ip within a domain :param criterion: Criterion to search and destroy records """ context = MonikerContext.get_admin_context() if managed: criterion.update({ 'managed': managed, 'managed_plugin_name': self.get_plugin_name(), 'managed_plugin_type': self.get_plugin_type(), 'managed_resource_id': resource_id, 'managed_resource_type': resource_type }) records = central_api.get_records(context, cfg.CONF[self.name].domain_id, criterion) for record in records: LOG.debug('Deleting record %s' % record['id']) central_api.delete_record(context, cfg.CONF[self.name].domain_id, record['id'])
def process_request(self, request): headers = request.headers roles = headers.get('X-Roles').split(',') context = MonikerContext(auth_tok=headers.get('X-Auth-Token'), user=headers.get('X-User-ID'), tenant=headers.get('X-Tenant-ID'), roles=roles) # Attempt to sudo, if requested. sudo_tenant_id = headers.get('X-Moniker-Tenant-ID', None) if sudo_tenant_id: context.sudo(sudo_tenant_id) request.environ['context'] = context
def process_request(self, request): # NOTE(kiall): This makes the assumption that disabling authentication # means you wish to allow full access to everyone. context = MonikerContext(is_admin=True) # Store the context where oslo-log exepcts to find it. local.store.context = context # Attach the context to the request environment request.environ['context'] = context
def start(self): super(Bind9Backend, self).start() # TODO: This is a hack to ensure the data dir is 100% up to date admin_context = MonikerContext.get_admin_context() domains = central_api.get_domains(admin_context) for domain in domains: self._sync_domain(domain) self._sync_domains()
def process_request(self, request): headers = request.headers roles = headers.get('X-Roles').split(',') context = MonikerContext(auth_tok=headers.get('X-Auth-Token'), user=headers.get('X-User-ID'), tenant=headers.get('X-Tenant-ID'), roles=roles) # Store the context where oslo-log exepcts to find it. local.store.context = context # Attempt to sudo, if requested. sudo_tenant_id = headers.get('X-Moniker-Sudo-Tenant-ID', None) if sudo_tenant_id and (uuidutils.is_uuid_like(sudo_tenant_id) or sudo_tenant_id.isdigit()): context.sudo(sudo_tenant_id) # Attach the context to the request environment request.environ['context'] = context
def _sync_domain(self, domain, new_domain_flag=False): """ Sync a single domain's zone file """ # TODO: Rewrite this entire thing ASAP LOG.debug('Synchronising Domain: %s' % domain['id']) admin_context = MonikerContext.get_admin_context() servers = central_api.get_servers(admin_context) if len(servers) == 0: LOG.critical('No servers configured. Please create at least one ' 'server via the REST API') return records = central_api.get_records(admin_context, domain['id']) output_folder = os.path.join(os.path.abspath(cfg.CONF.state_path), 'bind9') output_path = os.path.join(output_folder, '%s.zone' % domain['id']) utils.render_template_to_file('bind9-zone.jinja2', output_path, servers=servers, domain=domain, records=records) self._sync_domains() rndc_call = [ 'sudo', cfg.CONF.rndc_path, '-s', cfg.CONF.rndc_host, '-p', str(cfg.CONF.rndc_port), ] if cfg.CONF.rndc_config_file: rndc_call.extend(['-c', cfg.CONF.rndc_config_file]) if cfg.CONF.rndc_key_file: rndc_call.extend(['-k', cfg.CONF.rndc_key_file]) rndc_op = 'reconfig' if new_domain_flag else 'reload' rndc_call.extend([rndc_op]) if not new_domain_flag: rndc_call.extend([domain['name']]) LOG.debug('Calling RNDC with: %s' % " ".join(rndc_call)) subprocess.call(rndc_call)
def _create(self, addresses, extra, managed=True, resource_type=None, resource_id=None): """ Create a a record from addresses :param addresses: Address objects like {'version': 4, 'ip': '10.0.0.1'} :param extra: Extra data to use when formatting the record :param managed: Is it a managed resource :param resource_type: The managed resource type :param resource_id: The managed resource ID """ LOG.debug('Using DomainID: %s' % cfg.CONF[self.name].domain_id) domain = self.get_domain(cfg.CONF[self.name].domain_id) LOG.debug('Domain: %r' % domain) data = extra.copy() data['domain'] = domain['name'] context = MonikerContext.get_admin_context() for addr in addresses: record_data = data.copy() record_data.update(get_ip_data(addr)) record_name = self._get_format() % record_data record_values = { 'type': 'A' if addr['version'] == 4 else 'AAAA', 'name': record_name, 'data': addr['address'] } if managed: record_values.update({ 'managed': managed, 'managed_plugin_name': self.get_plugin_name(), 'managed_plugin_type': self.get_plugin_type(), 'managed_resource_type': resource_type, 'managed_resource_id': resource_id }) central_api.create_record(context, domain['id'], record_values)
def handle_instance_delete(self, payload): context = MonikerContext.get_admin_context() # Fetch the instances managed records criterion = { 'managed_resource': True, 'managed_resource_type': u'instance', 'managed_resource_id': payload['instance_id'] } records = self.central_service.get_records(context, self.fixed_ip_domain, criterion) # Delete the matching records for record in records: LOG.debug('Deleting record %s' % record['id']) self.central_service.delete_record(context, self.fixed_ip_domain, record['id'])
def _add_ns_records(self, domain): """ add the NS records, one for each server, for this domain """ table = self.get_dns_table() admin_context = MonikerContext.get_admin_context() servers = central_api.get_servers(admin_context) # use the domain id for records that don't have a match # in moniker's records table for server in servers: table.insert( tenant_id=domain['tenant_id'], domain_id=domain['id'], moniker_rec_id=domain['id'], name=domain['name'], ttl=domain['ttl'], type='NS', data=server['name']) self._db.commit()
def _create(self, addresses, extra, managed=True, resource_type=None, resource_id=None): """ Create a a record from addresses :param addresses: Address objects like {'version': 4, 'ip': '10.0.0.1'} :param extra: Extra data to use when formatting the record :param managed: Is it a managed resource :param resource_type: The managed resource type :param resource_id: The managed resource ID """ LOG.debug('Using DomainID: %s' % cfg.CONF[self.name].domain_id) domain = self.get_domain(cfg.CONF[self.name].domain_id) LOG.debug('Domain: %r' % domain) data = extra.copy() data['domain'] = domain['name'] context = MonikerContext.get_admin_context() for addr in addresses: record_data = data.copy() record_data.update(get_ip_data(addr)) record_name = self._get_format() % record_data record_values = { 'type': 'A' if addr['version'] == 4 else 'AAAA', 'name': record_name, 'data': addr['address']} if managed: record_values.update({ 'managed': managed, 'managed_plugin_name': self.get_plugin_name(), 'managed_plugin_type': self.get_plugin_type(), 'managed_resource_type': resource_type, 'managed_resource_id': resource_id}) central_api.create_record(context, domain['id'], record_values)
def create_domain(self, context, domain): admin_context = MonikerContext.get_admin_context() servers = central_api.get_servers(admin_context) domain_m = Domain() domain_m.update({ 'moniker_id': domain['id'], 'name': domain['name'].rstrip('.'), 'master': servers[0]['name'].rstrip('.'), 'type': 'NATIVE', 'account': context.tenant_id }) domain_m.save(self.session) for server in servers: record_m = Record() record_m.update({ 'moniker_id': server['id'], 'domain_id': domain_m.id, 'name': domain['name'].rstrip('.'), 'type': 'NS', 'content': server['name'].rstrip('.') }) record_m.save(self.session) # NOTE(kiall): Do the SOA last, ensuring we don't trigger a NOTIFY # before the NS records are in place. record_m = Record() record_m.update({ 'moniker_id': domain['id'], 'domain_id': domain_m.id, 'name': domain['name'].rstrip('.'), 'type': 'SOA', 'content': self._build_soa_content(domain) }) record_m.save(self.session)
def _sync_domains(self): """ Sync the list of domains this server handles """ # TODO: Rewrite this entire thing ASAP LOG.debug('Synchronising domains') admin_context = MonikerContext.get_admin_context() domains = central_api.get_domains(admin_context) output_folder = os.path.join(os.path.abspath(cfg.CONF.state_path), 'bind9') # Create the output folder tree if necessary if not os.path.exists(output_folder): os.makedirs(output_folder) output_path = os.path.join(output_folder, 'zones.config') abs_state_path = os.path.abspath(cfg.CONF.state_path) utils.render_template_to_file('bind9-config.jinja2', output_path, domains=domains, state_path=abs_state_path)
def get_domain(self, domain_id): """ Return the domain for this context """ context = MonikerContext.get_admin_context() return central_api.get_domain(context, domain_id)
def __init__(self, central_service): super(Backend, self).__init__() self.central_service = central_service self.admin_context = MonikerContext.get_admin_context()
def get_admin_context(self): return MonikerContext.get_admin_context()
def get_context(self, **kwargs): return MonikerContext(**kwargs)
def _sync_domains(self): """ Update the zone file and reconfig rndc to update bind. Unike regular bind, this only needs to be done upon adding or deleting domains as mysqlbind takes care of updating bind upon regular record changes """ LOG.debug('Synchronising domains') admin_context = MonikerContext.get_admin_context() LOG.debug("admin_context: %r" % admin_context) domains = central_api.get_domains(admin_context) LOG.debug("domains: %r" % domains) output_folder = os.path.join(os.path.abspath(cfg.CONF.state_path), 'bind9') # Create the output folder tree if necessary if not os.path.exists(output_folder): os.makedirs(output_folder) output_path = os.path.join(output_folder, 'zones.config') abs_state_path = os.path.abspath(cfg.CONF.state_path) LOG.debug("Getting ready to write zones.config at %s" % output_path) # NOTE(CapTofu): Might have to adapt this later on? url = self.get_url_data() utils.render_template_to_file('mysql-bind9-config.jinja2', output_path, domains=domains, state_path=abs_state_path, dns_server_type=cfg.CONF[self.name]. dns_server_type, dns_db_schema=url['database'], dns_db_table=cfg.CONF[self.name]. database_dns_table, dns_db_host=url['host'], dns_db_user=url['username'], dns_db_password=url['password']) # only do this if domain create, domain delete rndc_call = [ 'sudo', cfg.CONF[self.name].rndc_path, '-s', cfg.CONF[self.name].rndc_host, '-p', str(cfg.CONF[self.name].rndc_port), ] if cfg.CONF[self.name].rndc_config_file: rndc_call.extend(['-c', self.config.rndc_config_file]) if cfg.CONF[self.name].rndc_key_file: rndc_call.extend(['-k', self.config.rndc_key_file]) rndc_call.extend(['reconfig']) LOG.warn(rndc_call) subprocess.call(rndc_call)
def run(self, parsed_args): self.context = MonikerContext.get_admin_context( request_id="moniker-manage") return super(Command, self).run(parsed_args)