Пример #1
0
    def run(self, params, args):

        (dryrun, host) = self.fillParams([('dryrun', None), ('host', None)])

        if dryrun:
            dryrun = self.str2bool(dryrun)
            if not dryrun and host:
                self.abort("If you select a host you can't disable dryrun.")
        else:
            dryrun = True

        if not host:
            host = 'localhost'

        script = []
        script.append('#!/bin/sh\n')
        script.append('yum clean all\n')

        rolls = []
        for roll in args:
            rolls.append(roll)
        xml = self.command('list.host.xml',
                           [host, 'roll=%s' % string.join(rolls, ',')])

        reader = Sax2.Reader()
        gen = getattr(rocks.gen, 'Generator_%s' % self.os)()
        gen.setArch(self.arch)
        gen.setOS(self.os)
        gen.parse(xml)

        distPath = os.path.join(
            self.command('report.distro')[:-1], 'rocks-dist')
        tree = rocks.file.Tree(distPath)
        rpm_list = {}
        len_base_path = len('/export/rocks')
        base_url = "http://" + self.db.getHostAttr('localhost',
                                                   'Kickstart_PublicHostname')
        for file in tree.getFiles(os.path.join(self.arch, 'RedHat', 'RPMS')):
            if isinstance(file, rocks.file.RPMFile):
                rpm_url = base_url + file.getFullName()[len_base_path:]
                rpm_list[file.getBaseName()] = rpm_url
                rpm_list["%s.%s" % (file.getBaseName(), \
                 file.getPackageArch())] = rpm_url

        rpms = []
        for line in gen.generate('packages'):
            if line.find('%package') == -1:
                rpms.append(line)
        for rpm in rpms:
            if rpm in rpm_list.keys():
                script.append('yum install %s\n' % rpm)
                script.append(rpm_force_template % rpm_list[rpm])

        script += gen.generate_config_script()

        if dryrun:
            self.addText(string.join(script, ''))
        else:
            os.system(string.join(script, ''))
Пример #2
0
	def run(self, params, args):

		(dryrun, host ) = self.fillParams([('dryrun', None),
						   ('host', None)])
		
		if dryrun:
			dryrun = self.str2bool(dryrun)
			if not dryrun and host:
				self.abort("If you select a host you can't disable dryrun.")
		else:
			dryrun = True

		if not host:
			host = 'localhost'

		script = []
		script.append('#!/bin/sh\n')
		script.append('yum clean all\n')
			
		rolls = []
		for roll in args:
			rolls.append(roll)
		xml = self.command('list.host.xml', [ host,
			'roll=%s' % string.join(rolls, ',') ])


		reader = Sax2.Reader()
		gen = getattr(rocks.gen,'Generator_%s' % self.os)()
		gen.setArch(self.arch)
		gen.setOS(self.os)
		gen.parse(xml)

		distPath = os.path.join(self.command('report.distro')[:-1], 'rocks-dist')
                tree = rocks.file.Tree(distPath)
		rpm_list = {}
		len_base_path = len('/export/rocks')
                base_url = "http://" + self.db.getHostAttr('localhost', 'Kickstart_PublicHostname')
		for file in tree.getFiles(os.path.join(self.arch, 'RedHat', 'RPMS')):
			if isinstance(file, rocks.file.RPMFile):
				rpm_url = base_url + file.getFullName()[len_base_path:]
				rpm_list[file.getBaseName()] = rpm_url
				rpm_list["%s.%s" % (file.getBaseName(), \
					file.getPackageArch())] = rpm_url
			
		rpms = []
		for line in gen.generate('packages'):
			if line.find('%package') == -1:
				rpms.append(line)
		for rpm in rpms:
			if rpm in rpm_list.keys():
				script.append('yum install %s\n' % rpm)
				script.append(rpm_force_template % rpm_list[rpm])

		script += gen.generate_config_script()
		
		if dryrun:
			self.addText(string.join(script, ''))
		else:
			os.system(string.join(script, ''))
Пример #3
0
	def run(self, params, args):

		(clear, showattr) = \
			self.fillParams([
				('clear', 'n'),
				('showattr', 'n')
			])

		hostname = self.newdb.getCategoryAttr('global', 'global', 'Kickstart_PrivateHostname_old')

		if not hostname or not self.isValidHostname(hostname):
			# Kickstart_PrivateHostname did not get changed
			# so we can use the original value
			hostname = self.newdb.getCategoryAttr('global', 'global', 'Kickstart_PrivateHostname')

			if not hostname:
				self.abort("unable to get the value of Kickstart_PrivateHostname")

			if not self.isValidHostname(hostname):
				self.abort("The value of your Kickstart_PrivateHostname is invalid (%s)" % hostname)


		#
		# show which attr will be modified
		#
		if self.str2bool(showattr) :
			current_attr = self.db.getHostAttrs(hostname)
			attrs = self.get_modified_attr(hostname, current_attr)
			additional_attr = get_additional_attr(attrs, current_attr)

			self.addText("User modified attributes\n")
			self.beginOutput()
			for name in attrs:
				self.addOutput("", (name, current_attr[name + "_old"], attrs[name]))
			
			if additional_attr :
				self.addOutput("", (" ", " ", " "))
				self.addOutput("", ("Additional attributes", " ", " "))
				for name in additional_attr:
					self.addOutput("", (name, current_attr[name], additional_attr[name]))
			self.endOutput(header=['host', 'attr', 'old value ->', 'new value'])
			return

		#
		# clear attributes historical values
		#
		if self.str2bool(clear) :
			current_attr = self.db.getHostAttrs(hostname, 1)
			for name in current_attr:
				if name.endswith("_old"):
					# we need to delete this one
					if current_attr[name][1] == 'H':
						command = 'remove.host.attr'
					elif current_attr[name][1] == 'G':
						command = 'remove.attr'
					elif current_attr[name][1] == 'A':
						command = 'remove.appliance.attr'
					elif current_attr[name][1] == 'O':
						command = 'remove.os.attr'
					self.command(command, [name])
			return

		#
		# really run the script
		#

		# first fix the missing attribute
		current_attr = self.db.getHostAttrs(hostname)
		attrs = self.get_modified_attr(hostname, current_attr)
		additional_attr = get_additional_attr(attrs, current_attr)
		for name in additional_attr:
			if name == 'dhcp_nextserver':
				# this is an appliance attribute it needs special care
				s = self.newdb.getSession()
				apps = s.query(sqlalchemy.distinct(Catindex.name))\
					.join(Catindex.attributes)\
					.join(Attribute.category)\
					.filter(Attribute.attr == "dhcp_nextserver", \
						Category.name == 'appliance')\
					.all()
				for app_name in apps:
					self.command('set.appliance.attr',
						[app_name[0], name, additional_attr[name]])
			else:
				self.command('set.attr', [name, additional_attr[name]])

		# apparently session and connection view two different state of the database
		# hence to see the changes above a commit on the sessions will not be enough
		# (i tried commit, flush, and close_all) the connection will see the old 
		# values in the DB
		#
		# So only closing the connection will fix this
		self.newdb.close()
		self.newdb.connect()

		rolls = []
		for roll in args:
			rolls.append(roll)
		xml = self.command('list.host.xml', [ hostname,
			'roll=%s' % string.join(rolls, ',') ])


		if self.os != 'linux':
			self.abort('it runs only on linux!!')
		gen = rocks.gen.Generator_linux()
		# set reconfigure stage
		gen.set_phases(["reconfigure"])
		gen.parse(xml)

		script = []
		script.append('#!/bin/sh\n')
		script += gen.generate_config_script()
		self.addText(string.join(script, ''))
Пример #4
0
    def run(self, params, args):

        (dryrun, host) = self.fillParams([("dryrun", None), ("host", None)])

        if dryrun:
            dryrun = self.str2bool(dryrun)
            if not dryrun and host:
                self.abort("If you select a host you can't disable dryrun.")
        else:
            dryrun = True

        if not host:
            host = "localhost"

        script = []
        script.append("#!/bin/sh\n")

        rolls = []
        for roll in args:
            rolls.append(roll)
        xml = self.command("list.host.xml", [host, "roll=%s" % string.join(rolls, ",")])

        reader = Sax2.Reader()
        gen = getattr(rocks.gen, "Generator_%s" % self.os)()
        gen.setArch(self.arch)
        gen.setOS(self.os)
        gen.parse(xml)

        distPath = os.path.join(self.command("report.distro")[:-1], "rocks-dist")
        tree = rocks.file.Tree(distPath)
        rpm_list = {}
        len_base_path = len("/export/rocks")
        base_url = "http://" + self.db.getHostAttr("localhost", "Kickstart_PublicHostname")
        for file in tree.getFiles(os.path.join(self.arch, "RedHat", "RPMS")):
            if isinstance(file, rocks.file.RPMFile):
                rpm_url = base_url + file.getFullName()[len_base_path:]
                rpm_list[file.getBaseName()] = rpm_url
                rpm_list["%s.%s" % (file.getBaseName(), file.getPackageArch())] = rpm_url

        rpms = []
        for line in gen.generate("packages"):
            if line.find("%package") == -1:
                rpms.append(line)
        for rpm in rpms:
            if rpm in rpm_list.keys():
                script.append("yum install %s\n" % rpm)
                script.append(rpm_force_template % rpm_list[rpm])

        cur_proc = False
        for line in gen.generate("post"):
            if not line.startswith("%post"):
                script.append(line)
            else:
                if cur_proc == True:
                    script.append("__POSTEOF__\n")
                    script.append("%s %s\n" % (interpreter, t_name))
                    cur_proc = False
                try:
                    i = line.split().index("--interpreter")
                except ValueError:
                    continue
                interpreter = line.split()[i + 1]
                t_name = tempfile.mktemp()
                cur_proc = True
                script.append('cat > %s << "__POSTEOF__"\n' % t_name)

        if dryrun:
            self.addText(string.join(script, ""))
        else:
            os.system(string.join(script, ""))
Пример #5
0
    def run(self, params, args):

        (clear, showattr) = \
         self.fillParams([
          ('clear', 'n'),
          ('showattr', 'n')
         ])

        hostname = self.newdb.getCategoryAttr('global', 'global',
                                              'Kickstart_PrivateHostname_old')

        if not hostname or not self.isValidHostname(hostname):
            # Kickstart_PrivateHostname did not get changed
            # so we can use the original value
            hostname = self.newdb.getCategoryAttr('global', 'global',
                                                  'Kickstart_PrivateHostname')

            if not hostname:
                self.abort(
                    "unable to get the value of Kickstart_PrivateHostname")

            if not self.isValidHostname(hostname):
                self.abort(
                    "The value of your Kickstart_PrivateHostname is invalid (%s)"
                    % hostname)

        #
        # show which attr will be modified
        #
        if self.str2bool(showattr):
            current_attr = self.db.getHostAttrs(hostname)
            attrs = self.get_modified_attr(hostname, current_attr)
            additional_attr = get_additional_attr(attrs, current_attr)

            self.addText("User modified attributes\n")
            self.beginOutput()
            for name in attrs:
                self.addOutput(
                    "", (name, current_attr[name + "_old"], attrs[name]))

            if additional_attr:
                self.addOutput("", (" ", " ", " "))
                self.addOutput("", ("Additional attributes", " ", " "))
                for name in additional_attr:
                    self.addOutput(
                        "", (name, current_attr[name], additional_attr[name]))
            self.endOutput(
                header=['host', 'attr', 'old value ->', 'new value'])
            return

        #
        # clear attributes historical values
        #
        if self.str2bool(clear):
            current_attr = self.db.getHostAttrs(hostname, 1)
            for name in current_attr:
                if name.endswith("_old"):
                    # we need to delete this one
                    if current_attr[name][1] == 'H':
                        command = 'remove.host.attr'
                    elif current_attr[name][1] == 'G':
                        command = 'remove.attr'
                    elif current_attr[name][1] == 'A':
                        command = 'remove.appliance.attr'
                    elif current_attr[name][1] == 'O':
                        command = 'remove.os.attr'
                    self.command(command, [name])
            return

        #
        # really run the script
        #

        # first fix the missing attribute
        current_attr = self.db.getHostAttrs(hostname)
        attrs = self.get_modified_attr(hostname, current_attr)
        additional_attr = get_additional_attr(attrs, current_attr)
        for name in additional_attr:
            if name == 'dhcp_nextserver':
                # this is an appliance attribute it needs special care
                s = self.newdb.getSession()
                apps = s.query(sqlalchemy.distinct(Catindex.name))\
                 .join(Catindex.attributes)\
                 .join(Attribute.category)\
                 .filter(Attribute.attr == "dhcp_nextserver", \
                  Category.name == 'appliance')\
                 .all()
                for app_name in apps:
                    self.command('set.appliance.attr',
                                 [app_name[0], name, additional_attr[name]])
            else:
                self.command('set.attr', [name, additional_attr[name]])

        # apparently session and connection view two different state of the database
        # hence to see the changes above a commit on the sessions will not be enough
        # (i tried commit, flush, and close_all) the connection will see the old
        # values in the DB
        #
        # So only closing the connection will fix this
        self.newdb.close()
        self.newdb.connect()

        rolls = []
        for roll in args:
            rolls.append(roll)
        xml = self.command(
            'list.host.xml',
            [hostname, 'roll=%s' % string.join(rolls, ',')])

        if self.os != 'linux':
            self.abort('it runs only on linux!!')
        gen = rocks.gen.Generator_linux()
        # set reconfigure stage
        gen.set_phases(["reconfigure"])
        gen.parse(xml)

        script = []
        script.append('#!/bin/sh\n')
        script += gen.generate_config_script()
        self.addText(string.join(script, ''))
Пример #6
0
    def run(self, params, args):

        (clear, showattr) = self.fillParams([("clear", "n"), ("showattr", "n")])

        hostname = "localhost"

        #
        # show which attr will be modified
        #
        if self.str2bool(showattr):
            current_attr = self.db.getHostAttrs(hostname)
            attrs = self.get_modified_attr(hostname, current_attr)
            additional_attr = get_additional_attr(attrs, current_attr)

            self.addText("User modified attributes\n")
            self.beginOutput()
            for name in attrs:
                self.addOutput("", (name, current_attr[name + "_old"], attrs[name]))

            if additional_attr:
                self.addOutput("", (" ", " ", " "))
                self.addOutput("", ("Additional attributes", " ", " "))
                for name in additional_attr:
                    self.addOutput("", (name, current_attr[name], additional_attr[name]))
            self.endOutput(header=["host", "attr", "old value ->", "new value"])
            return

            #
            # clear attributes historical values
            #
        if self.str2bool(clear):
            current_attr = self.db.getHostAttrs(hostname, 1)
            for name in current_attr:
                if name.endswith("_old"):
                    # we need to delete this one
                    if current_attr[name][1] == "H":
                        command = "remove.host.attr"
                    elif current_attr[name][1] == "G":
                        command = "remove.attr"
                    elif current_attr[name][1] == "A":
                        command = "remove.appliance.attr"
                    elif current_attr[name][1] == "O":
                        command = "remove.os.attr"
                    self.command(command, [name])
            return

            #
            # really run the script
            #

            # first fix the missing attribute
        current_attr = self.db.getHostAttrs(hostname)
        attrs = self.get_modified_attr(hostname, current_attr)
        additional_attr = get_additional_attr(attrs, current_attr)
        for name in additional_attr:
            if name == "dhcp_nextserver":
                # this is an appliance attribute it needs special care
                rows = self.db.execute(
                    '''select name
					from appliance_attributes, appliances
					where Appliance = id and
					Attr = "dhcp_nextserver"'''
                )
                for (app_name,) in self.db.fetchall():
                    self.command("set.appliance.attr", [app_name, name, additional_attr[name]])
            else:
                self.command("set.attr", [name, additional_attr[name]])

        rolls = []
        for roll in args:
            rolls.append(roll)
        xml = self.command("list.host.xml", [hostname, "roll=%s" % string.join(rolls, ",")])

        if self.os != "linux":
            self.abort("it runs only on linux!!")
        gen = rocks.gen.Generator_linux()
        # set reconfigure stage
        gen.set_phases(["reconfigure"])
        gen.parse(xml)

        script = []
        script.append("#!/bin/sh\n")
        script += gen.generate_config_script()
        self.addText(string.join(script, ""))
Пример #7
0
    def run(self, params, args):

        anacondaKickstart = '/root/anaconda-ks.cfg'
        postinstallXml = '/root/postinstall.xml'

        (args, file) = self.fillPositionalArgs(('file', ))

        if not file:
            self.abort('missing kcikstartfile')
        try:
            kickstartFile = open(file)
        except IOError:
            self.abort("Unable to open kickstart file %s." % file)

        for line in kickstartFile:
            if line.startswith("%packages"):
                break
        #now we start with the packages
        packages = []
        for line in kickstartFile:
            if len(line.strip()) == 0:
                pass
            elif line.startswith("%"):
                break
            else:
                packages.append(line.strip())

        #
        # get current installed rpms list
        #
        (installedRpms, ret) = getOutputAsList('rpm -qa', None)
        #plz no rocks-grub
        excludeRpm = ['rocks-boot-auto']
        installedRpms = installedRpms + excludeRpm
        print " - postinstall - Download RPMs..."
        cmd = ['yumdownloader', '--resolve', '--destdir', '/mnt/temp', \
                                      '--exclude=' + string.join(installedRpms, ',')] + packages
        cmd = string.join(cmd, " ")
        print " - execuing: ", cmd
        getOutputAsList(cmd, None)
        print " - postinstall - Installing downloaded RPMs..."
        getOutputAsList('rpm --force --nodeps -ih /mnt/temp/*.rpm', None)

        #
        # executing the postsection
        # first generate the xml kickstart with only the necessary postsections
        #
        # grub and grub-client  -> avoid messing ec2 grub menu
        # hpc-client            -> MaxStartups messes up the sshd.config
        # ntp-client, resolve, syslog,routes	-> private ip addresses
        excludedPackage = [
            './nodes/grub.xml', './nodes/grub-client.xml',
            './nodes/client-firewall.xml', './nodes/ntp-client.xml',
            './nodes/partitions-save.xml', './nodes/resolv.xml',
            './nodes/routes-client.xml', './nodes/syslog-client.xml',
            './nodes/hpc-client.xml'
        ]
        #get already run nodes
        cmd = "grep 'begin post section' " + anacondaKickstart + " | awk -F ':' '{print $1}' | sort -u "
        (nodesRunned, ret) = getOutputAsList(cmd, None)
        if ret != 0:
            self.abort("unable to get the list of executed nodes.")
        #get nodes to be executed
        cmd = """grep '<post' """ + postinstallXml + """ | awk -F 'file=' '{print $2}' |sort -u |awk -F '"' '{print $2}'"""
        (nodesNewKickstart, ret) = getOutputAsList(cmd, None)
        if ret != 0:
            self.abort("unable to get the list of nodes to be executed.")
        #make the diff
        nodesTobeRun = []
        for node in nodesNewKickstart:
            if node in excludedPackage:
                continue
            if node in nodesRunned:
                continue
            nodesTobeRun.append(node)
        print "List of nodes that will be executed: ", nodesTobeRun
        tree = xml.etree.ElementTree.parse(postinstallXml)
        root = tree.getroot()
        for node in root.findall("post"):
            if "file" in node.attrib and node.attrib["file"] in nodesTobeRun:
                #let's keep this node we need to execute it
                pass
            else:
                root.remove(node)
        #save this for debugging
        tree.write("/tmp/tempKickstart.xml")
        print "xml kickstart saved in /tmp/tempKickstart.xml"
        buffer = StringIO.StringIO()
        tree.write(buffer)
        buffer.seek(0)

        #
        # now that we have the xml kickstart let's generate the script
        #
        script = []
        script.append('#!/bin/sh\n')
        reader = Sax2.Reader()
        gen = getattr(rocks.gen, 'Generator_%s' % self.os)()
        gen.setOS(self.os)
        gen.parse(buffer.getvalue())
        cur_proc = False
        for line in gen.generate('post'):
            if not line.startswith('%post'):
                script.append(line)
            else:
                if cur_proc == True:
                    script.append('__POSTEOF__\n')
                    script.append('%s %s\n' % (interpreter, t_name))
                    cur_proc = False
                try:
                    i = line.split().index('--interpreter')
                except ValueError:
                    continue
                interpreter = line.split()[i + 1]
                t_name = tempfile.mktemp()
                cur_proc = True
                script.append('cat > %s << "__POSTEOF__"\n' % t_name)

        #keep this for debuging
        fd = open("/tmp/kickstart.sh", 'w')
        fd.write(string.join(script, ''))
        fd.close()
        print "shell kickstart saved in /tmp/tempKickstart.xml, executing it..."
        os.system(string.join(script, ''))