Exemple #1
0
def create(mac=None, template=None, run_id=None):
    '''
    Create a config for a specific MAC from a template
    run_id is optional

    CLI Examples::

        salt '*' tftp.create template='test.template' mac='11:22:33:44:55:66'
        salt '*' tftp.create template='test.template' mac='11:22:33:44:55:66'\\
        run_id=2590
    '''
    if mac is None or template is None:
        return "mac and template must be specified"
    conf = _spoke_config(_salt_config('config'))
    tftproot = conf.get("TFTP", "tftp_root")
    tftp = SpokeTFTP(tftproot)
    if run_id is None:
        try:
            result = tftp.create(mac, template)
        except error.SpokeError as e:
            result = common.handle_error(e)
    else:
        try:
            result = tftp.create(mac, template, run_id)
        except error.SpokeError as e:
            result = common.handle_error(e)
    return result
Exemple #2
0
 def test_get_missing_mac(self):
     """Search for missing mac config; return empty list."""
     mac = '00:00:00:00:00:02'
     tftp = SpokeTFTP(self.tftp_root)
     result = tftp.search(mac)['data']
     expected_result = []
     self.assertEqual(result, expected_result)
Exemple #3
0
 def test_get_missing_mac(self):
     """Search for missing mac config; return empty list."""
     mac = '00:00:00:00:00:02'
     tftp = SpokeTFTP(self.tftp_root)
     result = tftp.search(mac)['data']
     expected_result = []
     self.assertEqual(result, expected_result)
Exemple #4
0
 def test_get_tftp_mac(self):
     """Search for a tftp mac config; return list."""
     # mac should be returned as lower case, with s/:/- and with prefix
     mac = self.tftp_mac_prefix + '-' + '00-0c-29-57-3b-31'
     tftp = SpokeTFTP(self.tftp_root)
     result = tftp.search(self.mac)['data']
     expected_result = [[mac]]
     self.assertEqual(result, expected_result) 
Exemple #5
0
 def test_get_tftp_mac(self):
     """Search for a tftp mac config; return list."""
     # mac should be returned as lower case, with s/:/- and with prefix
     mac = self.tftp_mac_prefix + '-' + '00-0c-29-57-3b-31'
     tftp = SpokeTFTP(self.tftp_root)
     result = tftp.search(self.mac)['data']
     expected_result = [[mac]]
     self.assertEqual(result, expected_result)
Exemple #6
0
 def test_create_association_runid(self):
     """Create mac config with run_id; return results object."""
     mac = '00:00:00:00:00:02'
     mac_file = '01-00-00-00-00-00-02'
     tftp = SpokeTFTP(self.tftp_root)
     expected_result = [[mac_file]]
     result = tftp.create(mac, self.template, self.run_id)['data']
     tftp.delete(mac)
     self.assertEqual(result, expected_result)
Exemple #7
0
 def test_create_association_runid(self):
     """Create mac config with run_id; return results object."""
     mac = '00:00:00:00:00:02'
     mac_file = '01-00-00-00-00-00-02'
     tftp = SpokeTFTP(self.tftp_root)
     expected_result = [[mac_file]]
     result = tftp.create(mac, self.template, self.run_id)['data']
     tftp.delete(mac)
     self.assertEqual(result, expected_result)
Exemple #8
0
 def tearDown(self):
     """Delete test tftp file structure."""
     tftp = SpokeTFTP(self.tftp_root)
     tftp.delete(self.mac)
     if os.path.exists(self.tftp_dir + self.template):
         os.remove(self.tftp_dir + self.template)
     
     for d in self.tftp_dir, self.tftp_root:
         if (os.path.exists(d)):
             try:
                 os.removedirs(d)
             except Exception as e:
                 raise e
Exemple #9
0
    def tearDown(self):
        """Delete test tftp file structure."""
        tftp = SpokeTFTP(self.tftp_root)
        tftp.delete(self.mac)
        if os.path.exists(self.tftp_dir + self.template):
            os.remove(self.tftp_dir + self.template)

        for d in self.tftp_dir, self.tftp_root:
            if (os.path.exists(d)):
                try:
                    os.removedirs(d)
                except Exception as e:
                    raise e
Exemple #10
0
 def setUp(self):
     """Create test tftp file structure."""
     for d in self.tftp_root, self.tftp_dir:
         if not (os.path.exists(d)):
             try:
                 os.makedirs(d)
             except Exception as e:
                 raise e
     if not os.path.exists(self.tftp_dir + self.template):
         f = open(self.tftp_dir + self.template, 'w')
         f.write("default local\nkernel linux\nappend somearg=2")
         f.close
     tftp = SpokeTFTP(self.tftp_root)
     tftp.create(self.mac, self.template)
Exemple #11
0
 def setUp(self):
     """Create test tftp file structure."""
     for d in self.tftp_root, self.tftp_dir:
         if not (os.path.exists(d)):
             try:
                 os.makedirs(d)
             except Exception as e:
                 raise e
     if not os.path.exists(self.tftp_dir + self.template):
         f = open(self.tftp_dir + self.template, 'w')
         f.write("default local\nkernel linux\nappend somearg=2")
         f.close
     tftp = SpokeTFTP(self.tftp_root)
     tftp.create(self.mac, self.template)
Exemple #12
0
 def test_create_association_with_template_noext(self):
     """Create mac config using missing template; raise NotFound."""
     mac = '00:00:00:00:00:03'
     tftp = SpokeTFTP(self.tftp_root)
     open(self.tftp_dir + 'template.conf', 'w').close
     self.assertRaises(error.InputError, tftp.create, mac, 'template.conf')
     os.remove(self.tftp_dir + 'template.conf')
Exemple #13
0
def search(mac=None, template=None):
    '''
    Return dict with list of templates and configs

    CLI Examples::

        salt '*' tftp.search
        salt '*' tftp.search mac='11:22:33:44:55:66'
        salt '*' tftp.search template='test.template'
    '''
    try:
        conf = _spoke_config(_salt_config('config'))
        tftproot = conf.get("TFTP", "tftp_root")
        tftp = SpokeTFTP(tftproot)
        result = tftp.search(mac, template)
    except error.SpokeError as e:
        result = common.handle_error(e)
    return result
Exemple #14
0
 def test_template_no_kernelargs(self):
     """Create mac config with bad template file contents; raises InputError."""
     tftp = SpokeTFTP(self.tftp_root)
     temp = open(self.tftp_dir + 'badtemplate.template', 'w')
     temp.write("default local\nkernel linux\n")
     temp.close
     self.assertRaises(error.InputError, tftp.create, self.mac,
                       'badtemplate.template', self.run_id)
     os.remove(self.tftp_dir + 'badtemplate.template')
Exemple #15
0
def delete(mac=None):
    '''
    Delete a config for a specific MAC

    CLI Examples::

        salt '*' tftp.delete mac='11:22:33:44:55:66'
    '''
    if mac is None:
        return "mac must be specified"
    try:
        conf = _spoke_config(_salt_config('config'))
        tftproot = conf.get("TFTP", "tftp_root")
        tftp = SpokeTFTP(tftproot)
        result = tftp.delete(mac)
    except error.SpokeError as e:
        result = common.handle_error(e)
    return result
Exemple #16
0
 def test_get_all_macs_templates(self):
     """Search for all macs and templates; return list."""
     # macs should be returned as lower case, with s/:/- and with prefix
     raw_mac1 = '00-0c-29-57-3b-31'
     raw_mac3 = '00-00-00-00-00-03'
     raw_mac4 = '00-00-00-00-00-04'
     template = self.template
     template2 = 'newtemplate.template'
     open(self.tftp_dir + template2, 'w').close()
     tftp = SpokeTFTP(self.tftp_root)
     tftp.create(raw_mac3, template2)
     tftp.create(raw_mac4, template2)
     result = tftp.search()['data']
     expected_result = [{
         'templates': [template2, template],
         'configs': [raw_mac3, raw_mac4, raw_mac1]
     }]
     tftp.delete(raw_mac3)
     tftp.delete(raw_mac4)
     os.remove(self.tftp_dir + template2)
     self.assertEqual(result, expected_result)
Exemple #17
0
 def test_get_all_macs_templates(self):
     """Search for all macs and templates; return list."""
     # macs should be returned as lower case, with s/:/- and with prefix
     raw_mac1 = '00-0c-29-57-3b-31'
     raw_mac3 = '00-00-00-00-00-03'
     raw_mac4 = '00-00-00-00-00-04'
     template = self.template
     template2 = 'newtemplate.template'
     open(self.tftp_dir + template2, 'w').close()
     tftp = SpokeTFTP(self.tftp_root)
     tftp.create(raw_mac3, template2)
     tftp.create(raw_mac4, template2)
     result = tftp.search()['data']
     expected_result = [{'templates' : [template2, template], 'configs' : [raw_mac3, raw_mac4, raw_mac1]}]
     tftp.delete(raw_mac3)
     tftp.delete(raw_mac4)
     os.remove(self.tftp_dir + template2)
     self.assertEqual(result, expected_result)
Exemple #18
0
import spoke.lib.error as error
import spoke.lib.config as config
import spoke.lib.logger as logger
import spoke.lib.mc_helper as mc

from spoke.lib.tftp import SpokeTFTP
        
config_file = '/usr/local/pkg/spoke/etc/spoke.conf'
config = config.setup(config_file)
    
if __name__ == '__main__':
    log = logger.setup('main', verbose=False, quiet=True)
    mc = mc.MCollectiveAction()
    request = mc.request()
    try:
        tftp = SpokeTFTP()
    except Exception as e:
        mc.fail(e)

    if request['action'] == 'search':
        try:
            mac = request['data']['mac']
        except KeyError:
            mac = None
        try:
            target = request['data']['target']
        except KeyError:
            target = None
        try:
            mc.data = tftp.search(mac=mac, target=target)
        except error.SpokeError as e:
Exemple #19
0
 def test_delete_mac(self):
     """Delete a config; return True."""
     tftp = SpokeTFTP(self.tftp_root)
     self.assertTrue(tftp.delete(self.mac))
     tftp.create(self.mac, self.template)
Exemple #20
0
 def test_delete_missing_mac(self):
     """Delete a missing config; raise NotFound."""
     mac = '00:00:00:00:00:02'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.NotFound, tftp.delete, mac)
Exemple #21
0
 def test_delete_mac(self):
     """Delete a config; return True."""
     tftp = SpokeTFTP(self.tftp_root)
     self.assertTrue(tftp.delete(self.mac))
     tftp.create(self.mac, self.template)
Exemple #22
0
 def test_create_invalid_template(self):
     """Create a config with an invalid template; raise InputError."""
     template = 'du$$_lv'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.InputError, tftp.search, template)
Exemple #23
0
 def test_create_invalid_mac(self):
     """Create config with an invalid mac; raise InputError."""
     mac = '00:00:00:00:00'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.InputError, tftp.search, mac)
Exemple #24
0
 def test_get_missing_template(self):
     """Search for missing template; raise NotFound."""
     template = 'missing'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.NotFound, tftp.search, template=template)
Exemple #25
0
 def test_create_association_with_missing_template(self):
     """Create mac config using missing template; raise NotFound."""
     mac = '00:00:00:00:00:03'
     template = 'missing'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.NotFound, tftp.create, mac, template)
Exemple #26
0
 def test_get_invalid_template(self):
     """Search for an invalid template; raise InputError."""
     template = 'du$$_lv'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.InputError, tftp.search, template)
Exemple #27
0
 def test_get_invalid_mac(self):
     """Search for an invalid mac; raise InputError."""
     mac = '00:00:00:00:00'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.InputError, tftp.search, mac)
Exemple #28
0
 def test_invalid_runid(self):
     """Create mac config with unsafe run_id; raises InputError."""
     run_id = 'alk;sdj'
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.InputError, tftp.create, self.mac,
                       self.template, run_id)
Exemple #29
0
 def test_create_existing_association(self):
     """Create existing mac config; raise AlreadyExists."""
     tftp = SpokeTFTP(self.tftp_root)
     self.assertRaises(error.AlreadyExists, tftp.create, self.mac,
                       self.template)
Exemple #30
0
import spoke.lib.error as error
import spoke.lib.config as config
import spoke.lib.common as common

version = common.version
config_file = '/usr/local/pkg/spoke/etc/spoke.conf'

try:
    conf = config.setup(config_file)
except error.ConfigError, e:
    raise

try:
    tftproot = conf.get("TFTP", "tftp_root")
    from spoke.lib.tftp import SpokeTFTP
    tftp = SpokeTFTP(tftproot)
except error.SpokeError, e:
    raise


def search(mac=None, template=None):
    '''
    Return dict with list of templates and configs

    CLI Examples::

        salt '*' tftp.search
        salt '*' tftp.search mac='11:22:33:44:55:66'
        salt '*' tftp.search template='test.template'
    '''
    try: