Ejemplo n.º 1
0
def config_linux(p_comp, p_systemsvc, p_S, p_K, p_svc_user, p_start, p_start_log,
                   p_stop, p_reload, p_status="", is_pg=True, p_env=None):

  apg_home = os.getenv("APG_HOME")

  if util.is_systemd():
    sys_svc_file = os.path.join(util.get_systemd_dir(), p_systemsvc + ".service")
  else:
    sys_svc_file = os.path.join("/etc/init.d", p_systemsvc)

  print(p_comp + " config autostart " + sys_svc_file)

  if not util.is_systemd():
    svcfile = os.path.join(apg_home, p_comp, p_systemsvc)
    write_sysv_svcfile(svcfile, p_systemsvc, p_S, p_K, p_svc_user,
                         p_start, p_start_log, p_stop, p_reload, p_status)
    rc = util.run_sudo("ln -sf " + svcfile + " " + sys_svc_file)

    link_cmd = "ln -sf ../init.d/" + p_systemsvc + "  "
    util.run_sudo(link_cmd + "/etc/rc0.d/K" + p_K + p_systemsvc)
    util.run_sudo(link_cmd + "/etc/rc1.d/K" + p_K + p_systemsvc)
    util.run_sudo(link_cmd + "/etc/rc2.d/S" + p_S + p_systemsvc)
    util.run_sudo(link_cmd + "/etc/rc3.d/S" + p_S + p_systemsvc)
    util.run_sudo(link_cmd + "/etc/rc4.d/S" + p_S + p_systemsvc)
    util.run_sudo(link_cmd + "/etc/rc5.d/S" + p_S + p_systemsvc)
    rc = util.run_sudo(link_cmd + "/etc/rc6.d/K" + p_K + p_systemsvc)
    return(rc)

  ## systemD ################################
  unit_file = tempfile.mktemp(".service")
  fh = open(unit_file, "w")
  fh.write("[Unit]\n")
  fh.write("Description=PostgreSQL (" + p_comp + ")\n")
  if is_pg:
    fh.write("After=syslog.target\n")
  fh.write("After=network.target\n")
  fh.write("\n")
  fh.write("[Service]\n")
  if p_env:
    fh.write("Environment=" + p_env + "\n")
  if is_pg:
    fh.write("Type=forking\n")
  fh.write("User="******"\n")
  if is_pg:
    fh.write("\n")
    fh.write("OOMScoreAdjust=-1000\n")
  fh.write("ExecStart="  + p_start  + "\n")
  if p_stop!="":
    fh.write("ExecStop="   + p_stop   + "\n")
  fh.write("ExecReload=" + p_reload + "\n")
  if is_pg:
    fh.write("TimeoutSec=300\n")
  fh.write("\n")
  fh.write("[Install]\n")
  fh.write("WantedBy=multi-user.target\n")
  fh.close()

  util.run_sudo("mv " + unit_file + " " + sys_svc_file)

  return(util.run_sudo("systemctl enable " + p_systemsvc))
Ejemplo n.º 2
0
def remove_linux(p_systemsvc, p_S, p_K):
  if util.is_systemd():
    util.run_sudo("systemctl disable " + p_systemsvc)
    util.run_sudo("rm -f " + os.path.join(util.get_systemd_dir(), p_systemsvc + ".service"))
    return(0)

  ## sysV #####################################
  cmd = "rm -f "
  util.run_sudo(cmd + "/etc/rc0.d/K" + p_K + p_systemsvc)
  util.run_sudo(cmd + "/etc/rc1.d/K" + p_K + p_systemsvc)
  util.run_sudo(cmd + "/etc/rc2.d/S" + p_S + p_systemsvc)
  util.run_sudo(cmd + "/etc/rc3.d/S" + p_S + p_systemsvc)
  util.run_sudo(cmd + "/etc/rc4.d/S" + p_S + p_systemsvc)
  util.run_sudo(cmd + "/etc/rc5.d/S" + p_S + p_systemsvc)
  util.run_sudo(cmd + "/etc/rc6.d/K" + p_K + p_systemsvc)

  rc = util.run_sudo("rm -f /etc/init.d/" + p_systemsvc)
  return(rc)
Ejemplo n.º 3
0
def get_pgdg_base(p_ver, p_isJSON):
    basedir = None
    ver = p_ver
    svcname = None
    datadir = None
    port = None
    logdir = None
    to_devnull = " >/dev/null 2>&1"

    ####### (1) PGHOME ##############################
    if util.get_os() in YUM_LIST:
        basedir = "/usr/pgsql-" + ver
    else:
        basedir = "/usr/lib/postgresql/" + ver

    pgbin = basedir + "/bin/postgres"
    if not os.path.isfile(pgbin):
        print("  PGHOME could not be located at " + pgbin)
        return ("1111111")

    ###### (2) Service Control File #################
    if util.get_os() in APT_LIST:
        svcname = "postgresql"
    else:
        svcname = "postgresql-" + ver

    if util.is_systemd():
        sysdir = util.get_systemd_dir()
        svcfile = sysdir + "/" + svcname + ".service"
    else:
        sysdir = "/etc/init.d"
        svcfile = sysdir + "/" + svcname

    if not os.path.isfile(svcfile):
        print("ERROR: ServiceFile not found (" + svcfile + ")")
        return ("2222222")

    ###### (3) DATADIR ###############################
    if util.get_os() in YUM_LIST:
        datadir = "/var/lib/pgsql/" + ver + "/data"
    else:
        datadir = "/var/lib/postgresql/" + ver + "/main"

    cmd = "sudo ls " + datadir
    rc = os.system(cmd + to_devnull)
    if rc != 0:
        print("ERROR: DataDir not found (" + datadir + ")")
        return ("3333333")

    ##### LOOK FOR PORT ####################
    pidfile = datadir + "/postmaster.pid"
    cmd = "sudo ls " + pidfile
    rc = os.system(cmd + to_devnull)
    if rc == 0:
        cmd = "sudo cat " + pidfile + " | sed -n '4p'"
        port = util.getoutput(cmd)
    else:
        port = "1"

    ##### LOOK FOR LOGDIR ##################
    if util.get_os() in APT_LIST:
        logdir = "/var/log/postgresql"
    else:
        logdir = datadir + "/pg_log"
    cmd = "sudo ls " + logdir
    rc = os.system(cmd + to_devnull)
    if rc != 0:
        logdir = ""

    return ([basedir, ver, svcname, svcfile, datadir, port, logdir])
Ejemplo n.º 4
0
def reload_linux(p_systemsvc):
    if util.is_systemd():
        return (util.run_sudo("systemctl reload " + p_systemsvc))
    else:
        return (util.run_sudo("service " + p_systemsvc + " reload"))
Ejemplo n.º 5
0
def stop_linux(p_systemsvc):
    if util.is_systemd():
        return (util.run_sudo("systemctl stop  " + p_systemsvc))
    else:
        return (util.run_sudo("service " + p_systemsvc + " stop"))