def do_refresh(self): LOG.info('* Refresh mode *') mode = self.options.refresh with SSHInterface(**self.emparams) as emsshifc: bpmgmt = self.bigipparams.address if mode == 1: rows = SQL.query("SELECT * FROM device WHERE mgmt_address='%s' AND last_refresh IS NULL;" % bpmgmt, ifc=emsshifc) elif mode == 2: rows = SQL.query("SELECT * FROM device WHERE mgmt_address='%s' AND (last_refresh IS NULL OR refresh_failed_at IS NOT NULL);" % bpmgmt, ifc=emsshifc) else: rows = SQL.query("SELECT * FROM device WHERE mgmt_address='%s'" % bpmgmt, ifc=emsshifc) LOG.info("Found %d devices.", len(rows)) with EMInterface(**self.emicparams) as emicifc: emapi = emicifc.api for row in rows: if not int(row.is_local_em): LOG.info('Refresh %s', row.access_address) try: emapi.device.refresh_device(deviceIds=[row.uid]) except IControlTransportError, e: LOG.error(e)
def do_delete(self): LOG.info('* Delete mode *') with SSHInterface(**self.emparams) as emsshifc: bpmgmt = self.bigipparams.address devices = SQL.query("SELECT uid,access_address FROM device WHERE mgmt_address='%s';" % bpmgmt, ifc=emsshifc) clones = devices[1:] uids = [x.uid for x in clones] with EMInterface(**self.emicparams) as emicifc: emapi = emicifc.api LOG.info('Deleting devices...') try: emapi.device.delete_device(deviceIds=uids) #LOG.info('Enabling auto-refresh on EM...') #EMAPI.device.set_config(auto_refresh=True, ifc=emicifc) except ParsingError, e: LOG.warning(e)
def do_inject(self): LOG.info('* Cloning mode *') bulk_sql = [] ip_offset = self.options.ip_offset with EMInterface(**self.emicparams) as emicifc: LOG.info('Disable auto-refresh on EM...') EMAPI.device.set_config(auto_refresh=False, ifc=emicifc) with SSHInterface(**self.emparams) as emsshifc: version = SCMD.ssh.get_version(ifc=emsshifc) has_groups = version < 'em 2.3.0' rows = SQL.query('SELECT MAX(device.uid) AS device, MAX(device_slot.uid) AS slot FROM device, device_slot;', ifc=emsshifc) max_device_uid = int(rows[0].device) max_slot_uid = int(rows[0].slot) bpmgmt = self.bigipparams.address template = self.do_get_template(bpmgmt, emsshifc) assert template.access_address != template.mgmt_address, \ "Template device must be discovered by its self IP." device_uid = int(template.uid) LOG.info('Template: %s', template.host_name) start_ip = IPAddress(START_IP) LOG.info('Inserting device rows...') for i in range(1, self.options.clones + 1, 1): template.uid = max_device_uid + UIDOFFSET + i template.access_address = str(start_ip + ip_offset + i) template.system_id = None template.last_refresh = None query = "INSERT INTO `device` %s" % self.do_prep_insert(template) bulk_sql.append(query) if has_groups: bulk_sql.append("INSERT INTO device_2_device_group VALUES (NULL,%d,1)" % template.uid) while bulk_sql: SQL.query(";".join(bulk_sql[:MAXSQL]), ifc=emsshifc) bulk_sql[:MAXSQL] = [] # Prepare device slot rows = SQL.query("SELECT * FROM device_slot WHERE device_id=%d;" % device_uid, ifc=emsshifc) last_device_slot_uid = max_slot_uid + UIDOFFSET LOG.info('Inserting device_slot rows...') for row in rows: last_device_uid = max_device_uid + UIDOFFSET for i in range(1, self.options.clones + 1, 1): last_device_slot_uid += 1 last_device_uid += 1 row.uid = last_device_slot_uid row.device_id = last_device_uid query = "INSERT INTO `device_slot` %s" % self.do_prep_insert(row) bulk_sql.append(query) while bulk_sql: SQL.query(";".join(bulk_sql[:MAXSQL]), ifc=emsshifc) bulk_sql[:MAXSQL] = [] LOG.info('Creating SelfIPs on %s...', bpmgmt) self_ips = [str(start_ip + ip_offset + x) for x in range(1, self.options.clones + 1, 1)] vlan_names = ['internal'] * self.options.clones netmasks = ['255.255.0.0'] * self.options.clones unit_ids = [0] * self.options.clones floating_states = ['STATE_DISABLED'] * self.options.clones with IcontrolInterface(**self.bigipparams) as bigipicifc: ic = bigipicifc.api ic.Networking.SelfIP.create(self_ips=self_ips, vlan_names=vlan_names, netmasks=netmasks, unit_ids=unit_ids, floating_states=floating_states) access_lists = [dict(self_ip=x, mode='ALLOW_MODE_ALL', protocol_ports=[]) for x in self_ips] ic.Networking.SelfIPPortLockdown.add_allow_access_list(access_lists=access_lists)
def do_get_template(self, mgmtip, ifc): # Select template row rows = SQL.query("SELECT * FROM device WHERE mgmt_address='%s';" % mgmtip, ifc=ifc) if not rows: raise ClonerFailed('Device with mgmtip %s not found.' % mgmtip) return rows[0]