def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) self.chroot = luna.OsImage( name=self._play_context.remote_addr).get('path') self.osimage = self._play_context.remote_addr if os.geteuid() != 0: raise AnsibleError("chroot connection requires running as root") # we're running as root on the local system so do some # trivial checks for ensuring 'host' is actually a chroot'able dir if not os.path.isdir(self.chroot): raise AnsibleError("%s is not a directory" % self.chroot) chrootsh = os.path.join(self.chroot, 'bin/sh') # Want to check for a usable bourne shell inside the chroot. # is_executable() == True is sufficient. For symlinks it # gets really complicated really fast. So we punt on finding that # out. As long as it's a symlink we assume that it will work if not (is_executable(chrootsh) or (os.path.lexists(chrootsh) and os.path.islink(chrootsh))): raise AnsibleError( "%s does not look like a chrootable dir (/bin/sh missing)" % self.chroot) self.chroot_cmd = distutils.spawn.find_executable('lchroot') if not self.chroot_cmd: raise AnsibleError("chroot command not found in PATH")
def test_create_osimage_with_defaults(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True, ) doc = self.db['osimage'].find_one({'_id': osimage._id}) expected = { 'path': self.path, 'kernmodules': 'ipmi_devintf,ipmi_si,ipmi_msghandler', 'dracutmodules': 'luna,-i18n,-plymouth', 'kernver': '3.10-999-el0.x86_64' } for attr in expected: self.assertEqual(doc[attr], expected[attr])
def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser(), ) self.osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True, )
def luna_inventory(self): osimage_suffix = ".osimages.luna" group_suffix = ".groups.luna" inventory = {} inventory['_meta'] = { 'hostvars': {}} osimages = {'hosts':[],'vars': {'ansible_connection': 'lchroot' }} for osimage in luna.list('osimage'): #osimages['hosts'].append(luna.OsImage(osimage).get('path')) osimages['hosts'].append(osimage + osimage_suffix) osimage_path = luna.OsImage(osimage).get('path') inventory['_meta']['hostvars'][osimage + osimage_suffix]= { 'ansible_host': osimage, } inventory['osimages.luna'] = osimages nodes = {} inventory['groups.luna'] = {'hosts': []} for g in luna.list('group'): group = luna.Group(g) hosts = [] nodes = group.list_nodes() for node_name in nodes: node = luna.Node(node_name) hosts.append(node_name) inventory['_meta']['hostvars'][node.show()['name']]={ "bmc_ip":node.get_ip('BMC',version=4)} inventory[g + group_suffix] = {'hosts': hosts} inventory['groups.luna']['hosts'].extend(hosts) return inventory
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group(name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.new_group1 = luna.Group(name='new1', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.node = luna.Node(group=self.group.name, mongo_db=self.db, create=True) self.network11 = luna.Network(name="net11", mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16) self.network61 = luna.Network(name="net61", mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, version=6)
def luna_osimage_absent(data): name = data['name'] try: osimage = luna.OsImage(name) except RuntimeError: return False, False, name return not osimage.delete(), True, name
def luna_osimage_present(data): changed = False ret = True try: osimage = luna.OsImage(name=data['name']) except RuntimeError: try: osimage = luna.OsImage(name=data['name'], create=True, path=data['path'], kernver=data['kernver'], kernopts=data['kernopts'], comment=data['comment']) changed = True except RuntimeError: return True, False, 'Unable to create image' for key in [ 'path', 'kernver', 'kernopts', 'dracutmodules', 'kernmodules', 'grab_exclude_list', 'grab_filesystems' ]: if data[key] and data[key] != osimage.get(key): ret &= osimage.set(key, data[key]) changed = True if data['comment'] != osimage.get('comment'): ret &= osimage.set('comment', data['comment']) changed = True if data['pack']: changed = True if data['copy_boot']: ret &= osimage.copy_boot() else: ret &= osimage.pack_boot() ret &= osimage.create_tarball() ret &= osimage.create_torrent() return not ret, changed, osimage.get('name')
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set('path', self.path) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group_new = luna.Group( name='testgroup_new', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, )
def test_create_osimage_with_same_path(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages args = { 'name': 'testosimage', 'path': self.path, 'mongo_db': self.db, 'create': True, } luna.OsImage(**args) args['name'] = 'testosimage2' self.assertRaises(RuntimeError, luna.OsImage, **args)
def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) osimage_name = str(self._play_context.remote_addr) self.osimage = luna.OsImage(name=osimage_name) self.path = self.osimage.get('path') self.lock = self.path + "/tmp/lchroot.lock" if os.geteuid() != 0: raise AnsibleError("lchroot connection requires running as root") # we're running as root on the local system so do some # trivial checks for ensuring 'host' is actually a chroot'able dir if not os.path.isdir(self.path): raise AnsibleError("%s is not a directory" % self.path) chrootsh = os.path.join(self.path, 'bin/sh') # Want to check for a usable bourne shell inside the chroot. # is_executable() == True is sufficient. For symlinks it # gets really complicated really fast. So we punt on finding that # out. As long as it's a symlink we assume that it will work if not (is_executable(chrootsh) or (os.path.lexists(chrootsh) and os.path.islink(chrootsh))): raise AnsibleError( "%s does not look like a chrootable dir (/bin/sh missing)" % self.path) self.chroot_cmd = distutils.spawn.find_executable('chroot') if not self.chroot_cmd: raise AnsibleError("chroot command not found in PATH") if os.path.isfile(self.lock): raise AnsibleError("%s exists. Unable to proceed." % self.lock) with open(self.lock, 'a') as f: f.write("PID %d of the ansible\n" % os.getpid()) self.prepare_mounts(self.path)
def test_osimage( self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages start_dict = self.db['group'].find_one({'_id': self.group._id}) osimage_name = 'osimage2' osimage = luna.OsImage(name=osimage_name, path='', mongo_db=self.db, create=True) self.group.osimage(osimage.name) self.assertEqual(self.group.show()['osimage'], '[' + osimage.name + ']') self.group.osimage(self.osimage.name) self.assertEqual(self.group.show()['osimage'], '[' + self.osimage.name + ']') end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict)
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_addmacro.return_value = True mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set('path', self.path) self.cluster.set('frontend_address', '127.0.0.1') self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.net1 = luna.Network( 'testnet1', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.net2 = luna.Network( 'testnet2', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16, ) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group_new = luna.Group( name='testgroup_new', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.boot_expected_dict = { 'domain': '', 'initrd_file': '', 'mac': '', 'kernel_file': '', 'localboot': 0, 'name': 'node001', 'service': 0, 'net': {}, 'hostname': 'node001', 'kern_opts': '', 'bootproto': 'dhcp' } self.install_expected_dict = { 'torrent_if': '', 'setupbmc': True, 'partscript': 'mount -t tmpfs tmpfs /sysroot', 'name': 'node001', 'tarball': '', 'bmcsetup': {}, 'interfaces': { 'BOOTIF': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } }, 'eth0': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } } }, 'prescript': '', 'domain': '', 'hostname': 'node001', 'postscript': ('cat << EOF >> /sysroot/etc/fstab\n' + 'tmpfs / tmpfs defaults 0 0\n' + 'EOF'), 'kernopts': '', 'kernver': '3.10-999-el0.x86_64', 'torrent': '', 'mac': '', }
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path #osimage_path = self.sandbox.create_osimage() self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set("frontend_address", "127.0.0.1") self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.bmcsetup = luna.BMCSetup(name='bmcsetup', mongo_db=self.db, create=True) self.net1 = luna.Network(name='cluster', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net2 = luna.Network(name='external', NETWORK='10.12.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net3 = luna.Network(name='ib', NETWORK='10.13.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net6 = luna.Network(name='net6', NETWORK='fe80::', PREFIX=64, mongo_db=self.db, create=True, version=6) self.prescript = 'pre' self.postscript = 'post' self.partscript = 'part' self.nics = {'eth0': 'PARM=1', 'eth1': 'PARM=2', 'ib0': 'PARM=3'} self.group = luna.Group(name='compute', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True)
PREFIX=24, NETMASK='255.255.255.0') ifcfg2 = luna.IfCfg(name='internal2', create=True, NETWORK='192.168.2.0', PREFIX=24, NETMASK='255.255.255.0') ifcfg3 = luna.IfCfg(name='internal3', create=True, NETWORK='192.168.3.0', PREFIX=24, NETMASK='255.255.255.0') ipmi = luna.IfCfg(name='ipmi', create=True, NETWORK='10.10.0.0', PREFIX=16) bmc = luna.BMCSetup(name='base', create=True) osimage = luna.OsImage(create=True, name='compute1', path='/os/compute-image', kernver='3.10.0-327.3.1.el7.x86_64') osimage = luna.OsImage(create=True, name='compute2', path='/os/compute-image-2', kernver='3.10.0-327.3.1.el7.x86_64') group = luna.Group(create=True, name='compute-group', bmcsetup='base', bmcnetwork='ipmi', osimage='compute1', interfaces={ 'eth0': 'internal1', 'eth1': 'internal2' })
import luna #opt = luna.Options(create=True) #opt = luna.Options() #osimage = luna.OsImage(create=True, name='compute', path='/os/compute-image/', kernver='3.10.0-327.3.1.el7.x86_64') osimage = luna.OsImage(name='compute') """ try: osimage = luna.OsImage(create=True, name='compute1', path='/os/compute-image', kernver='3.10.0-327.3.1.el7.x86_64') except: pass try: osimage = luna.OsImage(create=True, name='compute2', path='/os/compute-image///', kernver='3.10.0-327.3.1.el7.x86_64') except: pass try: osimage = luna.OsImage(create=True, name='compute3', path='../os/compute-image///', kernver='3.10.0-327.3.1.el7.x86_64') except: pass print opt print osimage print '===========================' print opt.nice_json print '===========================' print osimage.nice_json print '===========================' print osimage.delete() print opt.delete() """ print dir(osimage) #osimage.path = '/os/jkl' #osimage.kernopts = 'kjhkjhkjhkjhkjhkjhk'
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set("frontend_address", "127.0.0.1") self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.bmcsetup = luna.BMCSetup(name='bmcsetup', mongo_db=self.db, create=True) self.net1 = luna.Network(name='cluster', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net2 = luna.Network(name='external', NETWORK='10.12.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net3 = luna.Network(name='ib', NETWORK='10.13.0.0', PREFIX=16, mongo_db=self.db, create=True) self.prescript = 'pre' self.postscript = 'post' self.partscript = 'part' self.nics = {'BOOTIF': 'PARM=1', 'eth1': 'PARM=2', 'ib0': 'PARM=3'} self.group = luna.Group(name='compute', osimage=self.osimage.name, mongo_db=self.db, interfaces=['BOOTIF'], create=True) # mocking osimage boot stuff with mock.patch('os.path'), \ mock.patch('shutil.copy'), \ mock.patch('os.remove'), \ mock.patch('os.chown'), \ mock.patch('os.chmod'), \ mock.patch('subprocess.Popen') as mock_subprocess_popen, \ mock.patch('os.open'), \ mock.patch('os.chroot'), \ mock.patch('os.fchdir'), \ mock.patch('os.close'), \ mock.patch('os.chdir'), \ mock.patch('shutil.move'), \ mock.patch('libtorrent.add_files'), \ mock.patch('libtorrent.set_piece_hashes'): (mock_subprocess_popen.return_value.stderr.readline.return_value ) = '' self.osimage.copy_boot() self.osimage.create_tarball() self.osimage.create_torrent() group_json = self.db['group'].find_one({'_id': self.group._id}) osimage_json = self.db['osimage'].find_one({'_id': self.osimage._id}) self.install_expected_dict = { 'torrent_if': '', 'partscript': group_json['partscript'], 'tarball': osimage_json['tarball'] + '.tgz', 'bmcsetup': {}, 'interfaces': { 'BOOTIF': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } } }, 'prescript': '', 'domain': '', 'postscript': group_json['postscript'], 'kernopts': '', 'kernver': '3.10-999-el0.x86_64', 'torrent': osimage_json['torrent'] + '.torrent' }
import luna osimage = luna.OsImage('compute1') #osimage.create_tarball() osimage.create_torrent()
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group(name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.net11 = luna.Network(name='net11', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net11.set('ns_hostname', 'master') self.net61 = luna.Network(name='net61', NETWORK='fe90::', PREFIX=64, mongo_db=self.db, create=True) self.net61.set('ns_hostname', 'master') self.group.set_net_to_if('eth0', self.net11.name) self.group.set_net_to_if('eth0', self.net61.name) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.node = luna.Node(group=self.group.name, mongo_db=self.db, create=True) self.switch = luna.Switch(name='sw01', network=self.net11.name, mongo_db=self.db, create=True) self.switch.set('ip', '10.11.1.1') self.otherdev = luna.OtherDev(name='pdu01', network=self.net11.name, ip="10.11.2.1", mongo_db=self.db, create=True) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db)
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group1 = luna.Group(name='testgroup1', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.group2 = luna.Group(name='testgroup2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['BOOTIF'], create=True) self.net11 = luna.Network(name='net11', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.group1.set_net_to_if('eth0', self.net11.name) self.group2.set_net_to_if('BOOTIF', self.net11.name) self.node1 = luna.Node(group=self.group1.name, mongo_db=self.db, create=True) self.node2 = luna.Node(group=self.group2.name, mongo_db=self.db, create=True) self.node1.set_mac('00:11:22:33:44:55') self.node2.set_mac('01:11:22:33:44:55') self.group1 = luna.Group(name=self.group1.name, mongo_db=self.db) self.group2 = luna.Group(name=self.group2.name, mongo_db=self.db) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.node1 = luna.Node(name=self.node1.name, mongo_db=self.db) self.node2 = luna.Node(name=self.node2.name, mongo_db=self.db)
def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set('path', self.path) self.cluster.set('frontend_address', '127.0.0.1') self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.net = luna.Network( 'testnet', mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, ) self.net.set('ns_hostname', 'master') self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group.set_net_to_if('eth0', self.net.name) self.nodes = [] for i in range(10): self.nodes.append( luna.Node( group=self.group, create=True, mongo_db=self.db, )) self.net = luna.Network(name=self.net.name, mongo_db=self.db)