Example #1
0
 def set(self, value):
     logger.info("set env: %s=%s", self.__name, value)
     if sys.platform == 'win32':
         ret = run(
             "wmic ENVIRONMENT where \"name='%s'\" get UserName,VariableValue"
             % self.__name)
         if "No Instance(s) Available" in ret[1]:
             runex(
                 'wmic ENVIRONMENT create Name="%s",UserName="******",VariableValue="%s"'
                 % (self.__name, value))
         else:
             runex(
                 "wmic ENVIRONMENT where \"name='%s' and username='******'\" set VariableValue=\"%s\""
                 % (self.__name, value))
         runex("set %s=%s" % (self.__name, value))
     elif sys.platform.startswith('linux'):
         runex("export %s=%s" % (self.__name, value))
         txtedit = TextEditor("/etc/profile")
         txtedit.set_param(self.__name, value)
         txtedit.delete("export %s" % self.__name)
         txtedit.insert("export %s" % self.__name)
         txtedit.save()
     else:
         pass
     os.environ[self.__name] = value
Example #2
0
 def set(self, value):
     logger.info("set env: %s=%s", self.__name, value)
     if sys.platform == 'win32':
         ret = run("wmic ENVIRONMENT where \"name='%s'\" get UserName,VariableValue" % self.__name)
         if "No Instance(s) Available" in ret[1]:
             runex('wmic ENVIRONMENT create Name="%s",UserName="******",VariableValue="%s"' % (self.__name, value))
         else:
             runex("wmic ENVIRONMENT where \"name='%s' and username='******'\" set VariableValue=\"%s\"" % (self.__name, value))
         runex("set %s=%s" % (self.__name, value) )
     elif sys.platform.startswith('linux'):
         runex("export %s=%s" % (self.__name, value) )
         txtedit = TextEditor("/etc/profile")
         txtedit.set_param(self.__name, value)
         txtedit.delete("export %s" % self.__name)
         txtedit.insert("export %s" % self.__name)
         txtedit.save()
     else:
         pass
     os.environ[self.__name]=value
Example #3
0
def mount(source, target, fstype, fstab=False):
    logger.debug("mount: source=%s, target=%s, fstype=%s, fstab=%s", source, target, fstype, fstab)

    (retcode, mount_info) = oscommon.run("mount")
    if retcode != 0:
        raise Exception("can't run 'mount' command")
    if source in mount_info:
        logger.debug("%s is already mounted" % source)
    else:
        oscommon.runex("mount -t %s %s %s" % (fstype, source, target))
    
    if fstab:
        txt = TextEditor("/etc/fstab")
        txt.delete(target)
        txt.insert("%s %s    %s    defaults    0 0" % (source, target, fstype))
        txt.save()
Example #4
0
    def unset(self):
        logger.info("unset env: %s", self.__name)
        if self.__name in os.environ:
            del os.environ[self.__name]

        if sys.platform == 'win32':
            ret = run(
                "wmic ENVIRONMENT where \"name='%s'\" get UserName,VariableValue"
                % self.__name)
            if "No Instance(s) Available" not in ret[1]:
                runex("wmic ENVIRONMENT where \"name='%s'\" delete" %
                      self.__name)
        elif sys.platform.startswith('linux'):
            runex("unset -v %s" % self.__name)
            txtedit = TextEditor("/etc/profile")
            txtedit.delete(r'^%s=' % self.__name)
            txtedit.delete(r'^export %s' % self.__name)
            txtedit.save()
        else:
            pass
Example #5
0
    def unset(self):
        logger.info("unset env: %s", self.__name)
        if self.__name in os.environ:
            del os.environ[self.__name]

        if sys.platform == 'win32':
            ret = run("wmic ENVIRONMENT where \"name='%s'\" get UserName,VariableValue" % self.__name)
            if "No Instance(s) Available" not in ret[1]:
                runex("wmic ENVIRONMENT where \"name='%s'\" delete" % self.__name)
        elif sys.platform.startswith('linux'):
            runex("unset -v %s" % self.__name )
            txtedit = TextEditor("/etc/profile")
            txtedit.delete(r'^%s=' % self.__name)
            txtedit.delete(r'^export %s' % self.__name)
            txtedit.save()
        else:
            pass
Example #6
0
def umount(target, fstab=True):
    logger.debug("umount: target=%s, fstab=%s", target, fstab)

    (retcode, mount_info) = oscommon.run("mount")
    if retcode != 0:
        raise Exception("can't run 'mount' command")
    if target in mount_info:
        oscommon.runex("umount %s" % target)
    else:
        logger.debug("target %s is not mounted", target)
    
    if fstab:
        txt = TextEditor("/etc/fstab")
        txt.delete(target)
        txt.save()
Example #7
0
 def delete(name, filename=None):
     filename = filename or Hosts.locate()
     txt = TextEditor(filename)
     txt.delete(name)
     txt.save()
Example #8
0
 def set(name, addr, filename=None):
     filename = filename or Hosts.locate()
     txt = TextEditor(filename)
     txt.set_param(name, addr, "%(value)s  %(name)s")
     txt.save()
Example #9
0
 def delete(name, filename=None):
     filename = filename or Hosts.locate()
     txt = TextEditor(filename)
     txt.delete(name)
     txt.save()
Example #10
0
 def set(name, addr, filename=None):
     filename = filename or Hosts.locate()
     txt = TextEditor(filename)
     txt.set_param(name, addr, "%(value)s  %(name)s")
     txt.save()
Example #11
0
def disable_selinux():
    txt = TextEditor("/etc/sysconfig/selinux")
    txt.set_param("SELINUX", "disabled")
    txt.save()
    oscommon.run("setenforce 0")
Example #12
0
def sethostname(name):
    oscommon.runex("hostname %s" % name)
    txt = TextEditor("/etc/sysconfig/network")
    txt.set_param("HOSTNAME", name)
    txt.save()
Example #13
0
 def __init__(self, cls):
     if not issubclass(cls, Daemon):
         raise TypeError
     self.svc_name = cls._svc_name_
     self.svc_loct = "/etc/init.d/%s" % self.svc_name
     self._txt = TextEditor()
Example #14
0
class DaemonService(object):
    _svc_tmpl_ = """#!/bin/sh
#
# chkconfig: 2345 86 14
#
# description: Daemon Service to support deployment

if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

RETVAL=0

prog="service_name"
args=""
binary=/xormedia/build/xdeploy/xagent/dist/DeployAgent
pidfile=/var/run/${prog}.pid

start() {
    echo -n $"Starting ${prog} services: "
    if [ -f $pidfile ] ; then
        rm -f $pidfile
    fi
    daemon --pidfile=$pidfile $binary $args
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/${prog} || RETVAL=1
    return $RETVAL
}

stop() {
    echo -n $"Shutting down ${prog} services: "
    killproc -p $pidfile $binary
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/${prog}
    return $RETVAL
}


case "$1" in
    start)
        ps_log=`mktemp /var/tmp/ps.log.XXXXXX`
        ps aux --cols 1024 >"ps_log"
        if grep $binary "ps_log" >/dev/null 2>/dev/null ; then
            echo "${prog} service is up and running, no need start this service again"
        else
            start
        fi
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status ${prog}
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac
exit $RETVAL
"""

    def __init__(self, cls):
        if not issubclass(cls, Daemon):
            raise TypeError
        self.svc_name = cls._svc_name_
        self.svc_loct = "/etc/init.d/%s" % self.svc_name
        self._txt = TextEditor()

    def create(self, **kwargs):
        if not os.path.exists(self.svc_loct):
            self._txt.load(text=self._svc_tmpl_)
            self._txt.set_param("prog", self.svc_name)
            self._txt.save(self.svc_loct)
            fs.chmod(self.svc_loct, 0o755)
        self.config(**kwargs)

        os.system("chkconfig --add %s" % self.svc_name)

    def delete(self):
        os.system("chkconfig --del %s" % self.svc_name)
        if os.path.exists(self.svc_loct):
            fs.remove(self.svc_loct)

    def config(self, svc_desp=None, exe_name=None, exe_args=None, startup=None):
        exe_name = exe_name or os.path.realpath(sys.argv[0])

        self._txt.load(self.svc_loct)
        self._txt.set_param("binary", '%s' % exe_name)
        if exe_args is not None:
            self._txt.set_param("args", '"%s"' % exe_args)
        if exe_args is not None:
            self._txt.set_param("description", svc_desp, '%(name)s: %(value)s')
        self._txt.save()

        if startup is not None:
            if startup:
                os.system("chkconfig %s on" % self.svc_name)
            else:
                os.system("chkconfig %s off" % self.svc_name)