def validate_required_params(self, source, make, model): """Validate that the required parameters are provided.""" # require a source if not source: raise ParamRequired(cmd=self.owner, param="source") # require both make and model if not make: raise ParamRequired(cmd=self.owner, param="make") if not model: raise ParamRequired(cmd=self.owner, param="model")
def run(self, params, args): (host, interface, port) = self.fillParams([('host', None), ('interface', None), ('port', None)]) switches = self.getSwitchNames(args) if len(switches) == 0: raise ArgRequired(self, 'switch') if len(switches) > 1: raise ArgUnique(self, 'switch') switch = switches[0] hosts = self.getHostnames([host]) host = hosts[0] if not host: raise ParamRequired(self, ('host')) if not interface: raise ParamRequired(self, ('interface')) if not port: raise ParamRequired(self, ('port')) try: port = int(port) except: raise ParamType(self, 'port', 'integer') # # check if the host/port is defined for this switch # found = False for o in self.call('list.switch.host', [switch]): if o['host'] == host and o['port'] == port: found = True if not found: raise ArgError(self, 'host/port', '"%s/%s" not found' % (host, port)) # # see if the interface exists for this host # row = self.db.select("""net.id from networks net, nodes n where n.name='%s' and net.device='%s' and net.node = n.id""" % (host, interface)) if not row: raise CommandError( self, 'interface "%s" does not exist for host "%s"' % (interface, host)) interfaceid, = row[0] self.db.execute("""update switchports set interface=%s where port=%s and switch=(select id from nodes where name='%s') """ % (interfaceid, port, switch))
def run(self, args): params, args = args make, imp, = lowered( self.owner.fillParams( names=[ ("make", ""), ("imp", ""), ], params=params, ), ) # require a make if not make: raise ParamRequired(cmd=self.owner, param="make") # require an implementation if not imp: raise ParamRequired(cmd=self.owner, param="imp") # get rid of any duplicate names models = tuple(unique_everseen(lowered(args))) # ensure the model name doesn't already exist for the given make self.owner.ensure_unique_models(make=make, models=models) with ExitStack() as cleanup: # create the make if it doesn't already exist self.create_missing_make(make=make, cleanup=cleanup) # create the implementation if it doesn't already exist self.create_missing_imp(imp=imp, cleanup=cleanup) # get the ID of the make to associate with make_id = self.owner.get_make_id(make=make) # get the ID of the imp to associate with imp_id = self.owner.get_imp_id(imp=imp) self.owner.db.execute( """ INSERT INTO firmware_model ( name, make_id, imp_id ) VALUES (%s, %s, %s) """, [(model, make_id, imp_id) for model in models], many=True, ) # everything was successful, dismiss cleanup. cleanup.pop_all()
def run(self, params, args): hosts = self.getHostnames(args) (ip, interface, mac) = self.fillParams([('ip', None, True), ('interface', None), ('mac', None)]) if not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) if len(hosts) != 1: raise ArgUnique(self, 'host') ip = ip.upper() # null -> NULL host = hosts[0] if interface: self.db.execute(""" update networks, nodes set networks.ip=NULLIF('%s','NULL') where nodes.name='%s' and networks.node=nodes.id and networks.device like '%s' """ % (ip, host, interface)) else: self.db.execute(""" update networks, nodes set networks.ip=NULLIF('%s','NULL') where nodes.name='%s' and networks.node=nodes.id and networks.mac like '%s' """ % (ip, host, mac))
def run(self, params, args): (network, interface, mac) = self.fillParams([('network', None, True), ('interface', None), ('mac', None)]) if not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) for host in self.getHostnames(args): if interface: self.db.execute(""" update networks net, nodes n set net.subnet= (select id from subnets s where s.name='%s') where n.name='%s' and net.node=n.id and net.device like '%s' """ % (network, host, interface)) else: self.db.execute(""" update networks net, nodes n set net.subnet= (select id from subnets s where s.name='%s') where n.name='%s' and net.node=n.id and net.mac like '%s' """ % (network, host, mac))
def run(self, params, args): (channel, interface, mac) = self.fillParams([('channel', None, True), ('interface', None), ('mac', None)]) if not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) if channel.upper() == 'NULL': channel = 'NULL' for host in self.getHostnames(args): if interface: self.db.execute(""" update networks, nodes set networks.channel=NULLIF('%s','NULL') where nodes.name='%s' and networks.node=nodes.id and networks.device like '%s' """ % (channel, host, interface)) else: self.db.execute(""" update networks, nodes set networks.channel=NULLIF('%s','NULL') where nodes.name='%s' and networks.node=nodes.id and networks.mac like '%s' """ % (channel, host, mac))
def run(self, params, args): hosts = self.getHostnames(args) (name, interface, mac) = self.fillParams([ ('name', None, True), ('interface', None), ('mac', None) ]) if len(name.split('.')) > 1: raise ParamType(self, 'name', 'non-FQDN (base hostname)') if not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) if len(hosts) != 1: raise ArgUnique(self, 'host') host = hosts[0] if name.upper() == "NULL": name = host if interface: self.db.execute(""" update networks, nodes set networks.name='%s' where nodes.name='%s' and networks.node=nodes.id and networks.device like '%s' """ % (name, host, interface)) else: self.db.execute(""" update networks, nodes set networks.name='%s' where nodes.name='%s' and networks.node=nodes.id and networks.mac like '%s' """ % (name, host, mac))
def run(self, params, args): hosts = self.getHosts(args) (interface, mac, network) = self.fillParams([('interface', None, True), ('mac', None), ('network', None)]) # Gotta have one of these if not any([mac, network]): raise ParamRequired(self, ('mac', 'network')) # Make sure mac and/or network exist on our hosts self.validate(hosts, None, mac, network) for host in hosts: if network: sql = """ update networks,nodes,subnets set networks.device=%s where nodes.name=%s and subnets.name=%s and networks.node=nodes.id and networks.subnet=subnets.id """ values = [interface, host, network] else: sql = """ update networks,nodes set networks.device=%s where nodes.name=%s and networks.node=nodes.id """ values = [interface, host] if mac: sql += " and networks.mac=%s" values.append(mac) self.db.execute(sql, values)
def run(self, params, args): (vlan, interface, mac) = self.fillParams([('vlan', None, True), ('interface', None), ('mac', None)]) if not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) try: vlanid = int(vlan) except: raise ParamType(self, 'vlan', 'integer') for host in self.getHostnames(args): if interface: self.db.execute(""" update networks net, nodes n set net.vlanid = IF(%d = 0, NULL, %d) where net.device like '%s' and n.name = '%s' and net.node = n.id """ % (vlanid, vlanid, interface, host)) else: self.db.execute(""" update networks net, nodes n set net.vlanid = IF(%d = 0, NULL, %d) where net.mac like'%s' and n.name = '%s' and net.node = n.id """ % (vlanid, vlanid, mac, host))
def run(self, params, args): hosts = self.getHosts(args) (interface, mac, network) = self.fillParams([('interface', None), ('mac', None), ('network', None, True)]) # Gotta have one of these if not any([interface, mac]): raise ParamRequired(self, ('interface', 'mac')) # Make sure interface and/or mac exist on our hosts self.validate(hosts, interface, mac, None) for host in hosts: sql = """ update networks,nodes set networks.subnet=( select id from subnets where subnets.name=%s ) where nodes.name=%s and networks.node=nodes.id """ values = [network, host] if interface: sql += " and networks.device=%s" values.append(interface) if mac: sql += " and networks.mac=%s" values.append(mac) self.db.execute(sql, values)
def run(self, params, args): (scope, device, mountpoint) = self.fillParams([('scope', 'global'), ('device', None), ('mountpoint', None)]) oses = [] appliances = [] hosts = [] name = None accepted_scopes = ['global', 'os', 'appliance', 'host'] # Some checking that we got usable input if scope not in accepted_scopes: raise ParamValue(self, '%s' % params, 'one of the following: %s' % accepted_scopes) elif scope == 'global' and len(args) >= 1: raise ArgError( self, '%s' % args, 'unexpected, please provide a scope: %s' % accepted_scopes) elif scope == 'global' and (device is None and mountpoint is None): raise ParamRequired(self, 'device OR mountpoint') elif scope != 'global' and len(args) < 1: raise ArgRequired(self, '%s name' % scope) if scope == "os": oses = self.getOSNames(args) elif scope == "appliance": appliances = self.getApplianceNames(args) elif scope == "host": hosts = self.getHostnames(args) if scope != 'global': name = args[0] # Look up the id in the appropriate 'scope' table if scope == 'appliance': tableid = self.db.select('id from appliances where name=%s', [name])[0][0] elif scope == 'os': tableid = self.db.select('id from oses where name=%s', [name])[0][0] elif scope == 'host': tableid = self.db.select('id from nodes where name=%s', [name])[0][0] else: tableid = -1 query = 'delete from storage_partition where scope = %s and tableid = %s' values = [scope, tableid] if device and device != '*': query += ' and device = %s' values.append(device) if mountpoint and mountpoint != '*': query += ' and mountpoint = %s' values.append(mountpoint) self.db.execute(query, values)
def run(self, params, args): hosts = self.getHosts(args) (bridge, interface, network) = self.fillParams([ ('name', None, True), ('interface', ''), ('network', ''), ]) if not interface and not network: raise ParamRequired(self, ('interface', 'network')) for host in hosts: sql = """nt.ip, nt.name, s.name, nt.device, nt.main, nt.options from networks nt, nodes n, subnets s where nt.node=n.id and nt.subnet=s.id and n.name=%s""" values = [host] if network: sql += ' and s.name=%s' values.append(network) if interface: sql += ' and nt.device=%s' values.append(interface) rows = self.db.select(sql, values) if len(rows) == 0: raise CommandError(self, 'Could not find ' + ("interface %s configured on " % interface if interface else '') + ("network %s on " % network if network else '') + "host %s" % host) else: (ip, netname, net, dev, default_if, opts) = rows[0] # Set ip and subnet to NULL for original device self.command('set.host.interface.ip', [host, 'interface=%s' % dev, 'ip=NULL']) self.command('set.host.interface.network', [host, 'interface=%s' % dev, 'network=NULL']) # Create new bridge interface a_h_i_args = [host, "interface=%s" % bridge, 'network=%s' % net, 'name=%s' % netname] if ip: a_h_i_args.append('ip=%s' % ip) self.command('add.host.interface', a_h_i_args) self.command('set.host.interface.options', [host, 'interface=%s' % bridge, 'options=bridge']) # Set the original device to point to the bridge self.command('set.host.interface.channel', [host, 'interface=%s' % dev, 'channel=%s' % bridge]) if default_if == 1: self.command('set.host.interface.default', [host, 'interface=%s' % bridge, 'default=True'])
def run(self, params, args): apps = self.getApplianceNames(args) (address, gateway, netmask, interface) = self.fillParams([ ('address', None, True), ('gateway', None, True), ('netmask', '255.255.255.255'), ('interface', None), ]) if len(args) == 0: raise ParamRequired(self, 'appliance') # # determine if this is a subnet identifier # subnet = 0 rows = self.db.execute(""" select id from subnets where name = '%s' """ % gateway) if rows == 1: subnet, = self.db.fetchone() gateway = "''" else: subnet = 'NULL' gateway = "'%s'" % gateway # Verify the route doesn't already exist. If it does # for any of the appliances raise a CommandError. for app in apps: rows = self.db.execute("""select * from appliance_routes r, appliances a where r.appliance=a.id and r.network='%s' and a.name='%s'""" % (address, app)) if rows: raise CommandError(self, 'route exists') # # if interface is being set, check if it exists first # if not interface: interface = 'NULL' # Now that we know things will work insert the route for # all the appliances for app in apps: self.db.execute( """insert into appliance_routes values ((select id from appliances where name='%s'), '%s', '%s', %s, %s, '%s')""" % (app, address, netmask, gateway, subnet, interface))
def run(self, params, args): cartfile = self.fillParams([('file', None)]) cartsdir = '/export/stack/carts' if cartfile[0] == None: raise ParamRequired(self, 'file') else: cartfile = cartfile[0] cart = os.path.basename(cartfile).rsplit('.', 1)[0] self.addCart(cart) self.unpackCart(cart, cartfile, cartsdir)
def run(self, params, args): hosts = self.getHosts(args) (default, interface, mac, network) = self.fillParams([ ('default', 'true'), ('interface', None), ('mac', None), ('network', None) ]) default = self.str2bool(default) # Gotta have one of these if not any([interface, mac, network]): raise ParamRequired(self, ('interface', 'mac', 'network')) # Make sure interface, mac, and/or network exist on our hosts self.validate(hosts, interface, mac, network) for host in hosts: if default: # Clear the old default self.db.execute(""" update networks,nodes set networks.main=0 where nodes.name=%s and networks.node=nodes.id """, (host,)) # Set the new default value (might be 0 or 1) if network: sql = """ update networks,nodes,subnets set networks.main=%s where nodes.name=%s and subnets.name=%s and networks.node=nodes.id and networks.subnet=subnets.id """ values = [default, host, network] else: sql = """ update networks,nodes set networks.main=%s where nodes.name=%s and networks.node=nodes.id """ values = [default, host] if interface: sql += " and networks.device=%s" values.append(interface) if mac: sql += " and networks.mac=%s" values.append(mac) self.db.execute(sql, values)
def run(self, params, args): if len(args) == 0: raise ArgRequired(self, 'host') hosts = self.getHostnames(args) if not hosts: raise ArgRequired(self, 'host') (interface, mac, all_interfaces) = self.fillParams([('interface', None), ('mac', None), ('all', 'false')]) all_interfaces = self.str2bool(all_interfaces) if not all_interfaces and not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) networks = () for host in hosts: if all_interfaces: networks = flatten( self.db.select( """ id from networks where node=(select id from nodes where name=%s) """, (host, ))) elif interface: networks = flatten( self.db.select( """ id from networks where node=(select id from nodes where name=%s) and device=%s """, (host, interface))) if not networks: raise CommandError( self, 'no interface "%s" exists on %s' % (interface, host)) else: networks = flatten( self.db.select( """ id from networks where node=(select id from nodes where name=%s) and mac=%s """, (host, mac))) if not networks: raise CommandError( self, 'no mac address "%s" exists on %s' % (mac, host)) self.runPlugins(networks)
def run(self, params, args): hosts = self.getHosts(args) (vlan, interface, mac, network) = self.fillParams([('vlan', None, True), ('interface', None), ('mac', None), ('network', None)]) # Gotta have one of these if not any([interface, mac, network]): raise ParamRequired(self, ('interface', 'mac', 'network')) # Make sure interface, mac, and/or network exist on our hosts self.validate(hosts, interface, mac, network) # vlan has to be an integer try: vlan = int(vlan) except: raise ParamType(self, 'vlan', 'integer') # If vlan is 0 then it should be NULL in the db if vlan == 0: vlan = None for host in hosts: if network: sql = """ update networks,nodes,subnets set networks.vlanid=%s where nodes.name=%s and subnets.name=%s and networks.node=nodes.id and networks.subnet=subnets.id """ values = [vlan, host, network] else: sql = """ update networks,nodes set networks.vlanid=%s where nodes.name=%s and networks.node=nodes.id """ values = [vlan, host] if interface: sql += " and networks.device=%s" values.append(interface) if mac: sql += " and networks.mac=%s" values.append(mac) self.db.execute(sql, values)
def validate_name(self, name): """Validate the name is provided and is unique.""" # A name is required if not name: raise ParamRequired(cmd=self.owner, param="name") # The name must not already exist if self.owner.version_regex_exists(name=name): raise ParamError( cmd=self.owner, param="name", msg= f"A version_regex with the name {name} already exists in the database." )
def run(self, params, args): (scope, device, mountpoint) = self.fillParams([ ('scope', 'global'), ('device', None), ('mountpoint', None)]) oses = [] appliances = [] hosts = [] name = None accepted_scopes = ['global', 'os', 'appliance', 'host'] # Some checking that we got usable input.: if scope not in accepted_scopes: raise ParamValue(self, '%s' % params, 'one of the following: %s' % accepted_scopes ) elif scope == 'global' and len(args) >= 1: raise ArgError(self, '%s' % args, 'unexpected, please provide a scope: %s' % accepted_scopes) elif scope == 'global' and (device is None and mountpoint is None): raise ParamRequired(self, 'device OR mountpoint') elif scope != 'global' and len(args) < 1: raise ArgRequired(self, '%s name' % scope) if scope == "os": oses = self.getOSNames(args) elif scope == "appliance": appliances = self.getApplianceNames(args) elif scope == "host": hosts = self.getHostnames(args) if scope != 'global': name = args[0] # # look up the id in the appropriate 'scope' table # tableid = -1 tablename = {"os":"oses", "appliance":"appliances", "host":"nodes"} if scope != 'global': self.db.execute("""select id from %s where name = '%s' """ % (tablename[scope], name)) tableid, = self.db.fetchone() deletesql = """delete from storage_partition where scope = '%s' and tableid = %s """ % (scope, tableid) if device and device != '*': deletesql += """ and device = '%s'""" % device if mountpoint and mountpoint != '*': deletesql += """ and mountpoint = '%s'""" % mountpoint self.db.execute(deletesql)
def run(self, params, args): host = self.getSingleHost(args) (name, interface, mac, network) = self.fillParams([ ('name', None, True), ('interface', None), ('mac', None), ('network', None) ]) # Name can't be a FQDN (IE: have dots) if '.' in name: raise ParamType(self, 'name', 'non-FQDN (base hostname)') # Gotta have one of these if not any([interface, mac, network]): raise ParamRequired(self, ('interface', 'mac', 'network')) # Make sure interface, mac, and/or network exist on our hosts self.validate([host], interface, mac, network) # If name is set to 'NULL' it gets the host name if name.upper() == 'NULL': name = host # Make the change in the DB if network: sql = """ update networks,nodes,subnets set networks.name=%s where nodes.name=%s and subnets.name=%s and networks.node=nodes.id and networks.subnet=subnets.id """ values = [name, host, network] else: sql = """ update networks,nodes set networks.name=%s where nodes.name=%s and networks.node=nodes.id """ values = [name, host] if interface: sql += " and networks.device=%s" values.append(interface) if mac: sql += " and networks.mac=%s" values.append(mac) self.db.execute(sql, values)
def run(self, params, args): filename, processor = self.fillParams([('file', None), ('processor', 'default')]) if not filename: raise ParamRequired(self, 'file') if not os.path.exists(filename): raise CommandError(self, 'file "%s" does not exist' % filename) self.hosts = {} self.switches = self.getSwitchNames() sys.stderr.write('Loading Spreadsheet\n') self.runImplementation('load_%s' % processor, (filename, )) sys.stderr.write('Configuring Database\n') args = self.hosts self.runPlugins(args) sys.stderr.write('\tSyncing Switch\n') self.call('sync.switch') # # checkin the hosts spreadsheet # sheetsdir = '/export/stack/spreadsheets' if not os.path.exists(sheetsdir): os.makedirs(sheetsdir) RCSdir = '%s/RCS' % sheetsdir if not os.path.exists(RCSdir): os.makedirs(RCSdir) # # if the 'sheetsfile' doesn't exist or if the 'sheetsfile' and # the 'filename' are not the same file, then copy 'filename' # to 'sheetsfile'. # sheetsfile = '%s/%s' % (sheetsdir, os.path.basename(filename)) if not os.path.exists(sheetsfile) or not os.path.samefile( filename, sheetsfile): shutil.copyfile(filename, '%s' % sheetsfile) cmd = 'date | /opt/stack/bin/ci "%s"' % sheetsfile os.system(cmd) cmd = '/opt/stack/bin/co -f -l "%s"' % sheetsfile os.system(cmd)
def run(self, params, args): switches = self.getSwitchNames(args) if not switches: raise ArgRequired(self, 'switch') elif len(switches) > 1: raise ArgUnique(self, 'switch') switch = switches[0] (host, interface, port) = self.fillParams([ ('host', None), ('interface', None), ('port', None) ]) hosts = self.getHostnames([host]) if not hosts: raise ParamRequired(self, ('host')) elif len(hosts) > 1: raise ParamUnique(self, 'host') host = hosts[0] try: port = int(port) except: raise ParamType(self, 'port', 'integer') # Check if the host/port is defined for this switch for row in self.call('list.switch.host', [switch]): if row['host'] == host and row['port'] == port: break else: raise ArgError(self, 'host/port', f'"{host}/{port}" not found') # See if the interface exists for this host row = self.db.select(""" networks.id FROM networks, nodes WHERE nodes.name=%s AND networks.device=%s AND networks.node=nodes.id """, (host, interface)) if not row: raise CommandError( self, f'interface "{interface}" does not exist for host "{host}"' ) self.db.execute(""" UPDATE switchports SET interface=%s WHERE port=%s AND switch=(SELECT id FROM nodes WHERE name=%s) """, (row[0][0], port, switch))
def run(self, params, args): (interface, mac, all) = self.fillParams([('interface', None), ('mac', None), ('all', 'false')]) all = self.str2bool(all) if not all and not interface and not mac: raise ParamRequired(self, ('interface', 'mac')) networks = () for host in self.getHostnames(args): if all: self.db.execute(""" select id from networks where node=(select id from nodes where name='%s') """ % (host)) networks = self.db.fetchall() elif interface: self.runPlugins(networks) rows_affected = self.db.execute(""" select id from networks where node=(select id from nodes where name='%s') and device like '%s' """ % (host, interface)) if not rows_affected: raise CommandError( self, "No interface '%s' exists on %s." % (interface, host)) networks = self.db.fetchall() else: self.runPlugins(networks) rows_affected = self.db.execute(""" select id from networks where node=(select id from nodes where name='%s') and mac like '%s' """ % (host, mac)) if not rows_affected: raise CommandError( self, "No mac address '%s' exists on %s." % (mac, host)) networks = self.db.fetchall() self.runPlugins(networks)
def run(self, params, args): hosts = self.getHosts(args) (options, interface, mac, network) = self.fillParams([ ('options', None, True), ('interface', None), ('mac', None), ('network', None) ]) # Gotta have one of these if not any([interface, mac, network]): raise ParamRequired(self, ('interface', 'mac', 'network')) # Make sure interface, mac, and/or network exist on our hosts self.validate(hosts, interface, mac, network) # Options set to the string "NULL" is a null in the DB if options.upper() == 'NULL': options = None for host in hosts: if network: sql = """ update networks,nodes,subnets set networks.options=%s where nodes.name=%s and subnets.name=%s and networks.node=nodes.id and networks.subnet=subnets.id """ values = [options, host, network] else: sql = """ update networks,nodes set networks.options=%s where nodes.name=%s and networks.node=nodes.id """ values = [options, host] if interface: sql += " and networks.device=%s" values.append(interface) if mac: sql += " and networks.mac=%s" values.append(mac) self.db.execute(sql, values)
def run(self, params, args): cartfile = self.fillParams([('file', None)]) cartsdir = '/export/stack/carts' if not len(args): raise ArgRequired(self, 'cart') if len(args) > 1: raise ArgUnique(self, 'cart') if cartfile[0] == None: raise ParamRequired(self, 'file') else: cartfile = cartfile[0] cart = args[0] self.addCart(cart) self.unpackCart(cart, cartfile, cartsdir)
def run(self, params, args): switches = self.getSwitchNames(args) if not switches: raise ArgRequired(self, 'switch') elif len(switches) > 1: raise ArgUnique(self, 'switch') switch = switches[0] (host, interface, port) = self.fillParams([('host', None), ('interface', None), ('port', None)]) hosts = self.getHostnames([host]) if not hosts: raise ParamRequired(self, ('host')) elif len(hosts) > 1: raise ParamUnique(self, 'host') host = hosts[0] try: port = int(port) except: raise ParamType(self, 'port', 'integer') # Check if the host/interface is defined for this switch for row in self.call('list.switch.host', [switch]): if row['host'] == host and row['interface'] == interface: break else: raise ArgError(self, 'host/interface', f'"{host}/{interface}" not found') # Update the switch port self.db.execute( """ UPDATE switchports SET port=%s WHERE interface=( SELECT networks.id FROM networks, nodes WHERE nodes.name=%s AND networks.device=%s AND networks.node=nodes.id ) AND switch=(SELECT id FROM nodes WHERE name=%s) """, (port, host, interface, switch))
def run(self, params, args): apps = self.getApplianceNames(args) (address, gateway, netmask, interface) = self.fillParams([ ('address', None, True), ('gateway', None, True), ('netmask', '255.255.255.255'), ('interface', None), ]) if len(args) == 0: raise ParamRequired(self, 'appliance') # determine if this is a subnet identifier rows = self.db.select('id from subnets where name = %s', (gateway, )) if len(rows) == 1: subnet = rows[0][0] gateway = '' else: subnet = None # Verify the route doesn't already exist. If it does # for any of the appliances raise a CommandError. for app in apps: if self.db.count( """(*) from appliance_routes r, appliances a where r.appliance=a.id and r.network=%s and a.name=%s""", (address, app)) > 0: raise CommandError(self, 'route exists') # Now that we know things will work insert the route for # all the appliances for app in apps: self.db.execute( """insert into appliance_routes values ((select id from appliances where name=%s), %s, %s, %s, %s, %s)""", (app, address, netmask, gateway, subnet, interface))
def run(self, params, args): host = self.getSingleHost(args) (interface, mac, network) = self.fillParams([('interface', None), ('mac', None, True), ('network', None)]) # Gotta have one of these if not any([interface, network]): raise ParamRequired(self, ('interface', 'network')) # Make sure mac and/or network exist on our hosts self.validate([host], interface, None, network) # If mac is an empty sting or NULL, we are clearing it if not mac or mac.upper() == 'NULL': mac = None # Make the change in the DB if network: sql = """ update networks,nodes,subnets set networks.mac=%s where nodes.name=%s and subnets.name=%s and networks.node=nodes.id and networks.subnet=subnets.id """ values = [mac, host, network] else: sql = """ update networks,nodes set networks.mac=%s where nodes.name=%s and networks.node=nodes.id """ values = [mac, host] if interface: sql += " and networks.device=%s" values.append(interface) self.db.execute(sql, values)
def run(self, params, args): (default, interface, network, mac) = self.fillParams([ ('default', 'true'), ('interface', None), ('network', None), ('mac', None), ]) default = self.str2bool(default) if not interface and not network and not mac: raise ParamRequired(self, ('interface', 'network', 'mac')) for host in self.getHostnames(args): valid = False # Check validity of params. Match them against the # values in the database to check params. for dict in self.call('list.host.interface', [host]): if network and network == dict['network']: valid = True sql_set_cmd = """update networks net, nodes n, subnets s set net.main = %d where n.name = '%s' and s.name='%s' and net.node = n.id and net.subnet=s.id""" % \ (default, host, network) break if interface and interface == dict['interface']: valid = True sql_set_cmd = """update networks net, nodes n set net.main = %d where n.name = '%s' and net.node = n.id and net.device ='%s'""" % \ (default, host, interface) break if mac and mac == dict['mac']: valid = True sql_set_cmd = """update networks net, nodes n set net.main = %d where n.name = '%s' and net.node = n.id and net.mac ='%s'""" % \ (default, host, mac) break if valid: if default: sql_clear_cmd = """update networks net, nodes n set net.main = 0 where n.name = '%s' and net.node = n.id """ % (host) self.db.execute(sql_clear_cmd) self.db.execute(sql_set_cmd) else: if network: raise CommandError( self, "Network '%s' for '%s' not found" % (network, host)) elif interface: raise CommandError( self, "Interface '%s' for '%s' not found" % (interface, host)) elif mac: raise CommandError( self, "MAC Address '%s' for '%s' not found" % (mac, host))
def run(self, args): params, hosts = args make, model, versions, sort, = self.owner.fillParams(names=[ ("make", ""), ("model", ""), ("versions", ""), ("sort", "host") ], params=params) sort_map = { "host": "nodes.Name", "make": "firmware_make.name", "model": "firmware_model.name", "version": "firmware.version", } # sort must be one of the allowed values try: # also convert to the column name for use in ORDER BY sort = sort_map[sort] except KeyError: raise ParamError( cmd=self.owner, param="sort", msg=f"Sort must be one of: {list(sort_map.keys())}") # process hosts if present if hosts: # hosts must exist hosts = self.owner.getHosts(args=hosts) # process make if present if make: # ensure the make exists if not self.owner.make_exists(make=make): raise ParamError(cmd=self.owner, param="make", msg=f"The make {make} doesn't exist.") # process model if present if model: # make is now required if not make: raise ParamRequired(cmd=self.owner, param="make") # ensure the model exists if not self.owner.model_exists(make=make, model=model): raise ParamError( cmd=self.owner, param="model", msg=f"The model {model} doesn't exist for make {make}.") # Process versions if present if versions: # make and model are now required if not make: raise ParamRequired(cmd=self.owner, param="make") if not model: raise ParamRequired(cmd=self.owner, param="model") # turn a comma separated string into a list of versions and # get rid of any duplicate names versions = tuple( unique_everseen((version.strip() for version in versions.split(",") if version.strip()))) # ensure the versions exist try: self.owner.ensure_firmwares_exist(make=make, model=model, versions=versions) except CommandError as exception: raise ArgError(cmd=self.owner, arg="version", msg=exception.message()) results = self.get_firmware_mappings( hosts=hosts, make=make, model=model, versions=versions, sort=sort, ) # return the results return { "keys": ["host", "version", "make", "model"], "values": results }