mirror of
https://github.com/ivuorinen/sysvinit-service-generator.git
synced 2026-02-21 16:54:28 +00:00
Initial commit
This commit is contained in:
187
src/App.vue
Normal file
187
src/App.vue
Normal file
@@ -0,0 +1,187 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from 'vue'
|
||||
|
||||
let service = defineModel('service', { default: 'my-service' })
|
||||
let description = defineModel('description', { default: 'This command does something' })
|
||||
let username = defineModel('username', { default: 'root' })
|
||||
let command = defineModel('command', { default: '/usr/local/bin/command' })
|
||||
let servicePath = ref('/etc/init.d/' + service.value )
|
||||
let logRotatePath = ref('/etc/logrotate.d/' + service.value)
|
||||
|
||||
let shellCommands = ref(`sudo chmod +x ${servicePath.value} && sudo update-rc.d ${service.value} defaults`)
|
||||
|
||||
let serviceTemplateString = `#!/usr/bin/env sh
|
||||
### BEGIN INIT INFO
|
||||
# Provides: <NAME>
|
||||
# Required-Start: $local_fs $network $named $time $syslog
|
||||
# Required-Stop: $local_fs $network $named $time $syslog
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop: 0 1 6
|
||||
# Description: <DESCRIPTION>
|
||||
### END INIT INFO
|
||||
|
||||
SCRIPT="<COMMAND>"
|
||||
RUNAS=<USERNAME>
|
||||
|
||||
PIDFILE=/var/run/<NAME>.pid
|
||||
LOGFILE=/var/log/<NAME>.log
|
||||
|
||||
start() {
|
||||
if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
|
||||
echo "Service already running" >&2
|
||||
return 1
|
||||
fi
|
||||
echo 'Starting service...' >&2
|
||||
local CMD="$SCRIPT &> \\"$LOGFILE\\" & echo \\$!"
|
||||
su -c "$CMD" $RUNAS > "$PIDFILE"
|
||||
# Try with this command instead if above does not work
|
||||
# su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
|
||||
|
||||
sleep 2
|
||||
PID=$(cat $PIDFILE)
|
||||
if pgrep -u $RUNAS -f $NAME > /dev/null
|
||||
then
|
||||
echo "$NAME is now running, the PID is $PID"
|
||||
else
|
||||
echo ''
|
||||
echo "Error! Could not start $NAME!"
|
||||
fi
|
||||
}
|
||||
|
||||
stop() {
|
||||
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
|
||||
echo 'Service not running' >&2
|
||||
return 1
|
||||
fi
|
||||
echo 'Stopping service...' >&2
|
||||
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
|
||||
echo 'Service stopped' >&2
|
||||
}
|
||||
|
||||
uninstall() {
|
||||
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
|
||||
local SURE
|
||||
read SURE
|
||||
if [ "$SURE" = "yes" ]; then
|
||||
stop
|
||||
rm -f "$PIDFILE"
|
||||
echo "Notice: log file was not removed: $LOGFILE" >&2
|
||||
update-rc.d -f $NAME remove
|
||||
rm -fv "$0"
|
||||
else
|
||||
echo "Abort!"
|
||||
fi
|
||||
}
|
||||
|
||||
status() {
|
||||
printf "%-50s" "Checking <NAME>..."
|
||||
if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
|
||||
PID=$(cat $PIDFILE)
|
||||
if [ -z "$(ps axf | grep \${PID} | grep -v grep)" ]; then
|
||||
printf "%s\\n" "The process appears to be dead but pidfile still exists"
|
||||
else
|
||||
echo "Running, the PID is $PID"
|
||||
fi
|
||||
else
|
||||
printf "%s\\n" "Service not running"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
start
|
||||
;;
|
||||
stop)
|
||||
stop
|
||||
;;
|
||||
status)
|
||||
status
|
||||
;;
|
||||
uninstall)
|
||||
uninstall
|
||||
;;
|
||||
restart)
|
||||
stop
|
||||
start
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|status|restart|uninstall}"
|
||||
esac
|
||||
`
|
||||
|
||||
let logRotateString = `/var/log/<NAME>.log {
|
||||
rotate 4
|
||||
weekly
|
||||
missingok
|
||||
notifempty
|
||||
compress
|
||||
delaycompress
|
||||
}`
|
||||
|
||||
let serviceTemplate = serviceTemplateString
|
||||
.replace(/<NAME>/g, service.value)
|
||||
.replace(/<DESCRIPTION>/g, description.value)
|
||||
.replace(/<USERNAME>/g, username.value)
|
||||
.replace(/<COMMAND>/g, command.value)
|
||||
|
||||
let logRotate = logRotateString.replace(/<NAME>/g, service.value)
|
||||
|
||||
watch([service, description, username, command], ([newService, newDescription, newUsername, newCommand]) => {
|
||||
serviceTemplate = serviceTemplateString
|
||||
.replace(/<NAME>/g, newService)
|
||||
.replace(/<DESCRIPTION>/g, newDescription)
|
||||
.replace(/<USERNAME>/g, newUsername)
|
||||
.replace(/<COMMAND>/g, newCommand)
|
||||
servicePath.value = '/etc/init.d/' + newService
|
||||
logRotate = logRotateString.replace(/<NAME>/g, newService)
|
||||
logRotatePath.value = '/etc/logrotate.d/' + newService
|
||||
shellCommands.value = `sudo chmod +x ${servicePath.value} && sudo update-rc.d ${newService} defaults`
|
||||
})
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<header>
|
||||
<h1 class="green">sysvinit service generator</h1>
|
||||
|
||||
<label>
|
||||
Service name:
|
||||
<input type="text" minlength="1" v-model="service" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Description:
|
||||
<input type="text" minlength="1" v-model="description" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Run as user:
|
||||
<input type="text" minlength="1" v-model="username" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Command:
|
||||
<input type="text" minlength="1" v-model="command" />
|
||||
</label>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<h3>Generated service script:</h3>
|
||||
<div>Path: <code>{{ servicePath }}</code></div>
|
||||
<textarea style="height: 400px" v-text="serviceTemplate"></textarea>
|
||||
<details>
|
||||
<summary>Logrotate</summary>
|
||||
<div>Path: <code>{{ logRotatePath }}</code></div>
|
||||
<textarea class="just-right noresize" v-text="logRotate"></textarea>
|
||||
</details>
|
||||
<details>
|
||||
<summary>Shell commands to run</summary>
|
||||
<textarea class="just-right noresize" v-text="shellCommands"></textarea>
|
||||
</details>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>Created by <a href="https://github.com/ivuorinen">@ivuorinen</a> from Tampere, Finland</p>
|
||||
</footer>
|
||||
</template>
|
||||
82
src/assets/base.css
Normal file
82
src/assets/base.css
Normal file
@@ -0,0 +1,82 @@
|
||||
/* color palette from <https://github.com/vuejs/theme> */
|
||||
:root {
|
||||
--vt-c-white: #ffffff;
|
||||
--vt-c-white-soft: #f8f8f8;
|
||||
--vt-c-white-mute: #f2f2f2;
|
||||
|
||||
--vt-c-black: #181818;
|
||||
--vt-c-black-soft: #222222;
|
||||
--vt-c-black-mute: #282828;
|
||||
|
||||
--vt-c-indigo: #2c3e50;
|
||||
|
||||
--vt-c-divider-light-1: rgba(60, 60, 60, 0.29);
|
||||
--vt-c-divider-light-2: rgba(60, 60, 60, 0.12);
|
||||
--vt-c-divider-dark-1: rgba(84, 84, 84, 0.65);
|
||||
--vt-c-divider-dark-2: rgba(84, 84, 84, 0.48);
|
||||
|
||||
--vt-c-text-light-1: var(--vt-c-indigo);
|
||||
--vt-c-text-light-2: rgba(60, 60, 60, 0.66);
|
||||
--vt-c-text-dark-1: var(--vt-c-white);
|
||||
--vt-c-text-dark-2: rgba(235, 235, 235, 0.64);
|
||||
|
||||
/* semantic color variables for this project */
|
||||
--color-background: var(--vt-c-white);
|
||||
--color-background-soft: var(--vt-c-white-soft);
|
||||
--color-background-mute: var(--vt-c-white-mute);
|
||||
|
||||
--color-border: var(--vt-c-divider-light-2);
|
||||
--color-border-hover: var(--vt-c-divider-light-1);
|
||||
|
||||
--color-heading: var(--vt-c-text-light-1);
|
||||
--color-text: var(--vt-c-text-light-1);
|
||||
|
||||
--section-gap: 160px;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--color-background: var(--vt-c-black);
|
||||
--color-background-soft: var(--vt-c-black-soft);
|
||||
--color-background-mute: var(--vt-c-black-mute);
|
||||
|
||||
--color-border: var(--vt-c-divider-dark-2);
|
||||
--color-border-hover: var(--vt-c-divider-dark-1);
|
||||
|
||||
--color-heading: var(--vt-c-text-dark-1);
|
||||
--color-text: var(--vt-c-text-dark-2);
|
||||
}
|
||||
}
|
||||
|
||||
*,
|
||||
*::before,
|
||||
*::after {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
body {
|
||||
min-height: 100vh;
|
||||
color: var(--color-text);
|
||||
background: var(--color-background);
|
||||
transition: color 0.5s,
|
||||
background-color 0.5s;
|
||||
line-height: 1.6;
|
||||
font-family: Inter,
|
||||
-apple-system,
|
||||
BlinkMacSystemFont,
|
||||
'Segoe UI',
|
||||
Roboto,
|
||||
Oxygen,
|
||||
Ubuntu,
|
||||
Cantarell,
|
||||
'Fira Sans',
|
||||
'Droid Sans',
|
||||
'Helvetica Neue',
|
||||
sans-serif;
|
||||
font-size: 15px;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
102
src/assets/main.css
Normal file
102
src/assets/main.css
Normal file
@@ -0,0 +1,102 @@
|
||||
@import './base.css';
|
||||
|
||||
#app {
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
a,
|
||||
.green {
|
||||
text-decoration: none;
|
||||
color: hsla(160, 100%, 37%, 1);
|
||||
transition: 0.4s;
|
||||
padding: 3px;
|
||||
}
|
||||
|
||||
header {
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 1rem;
|
||||
|
||||
input {
|
||||
display: block;
|
||||
margin-top: 1rem;
|
||||
padding: 0.4rem;
|
||||
font-size: 1rem;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 4px;
|
||||
}
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-weight: 500;
|
||||
font-size: 2.6rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.2rem;
|
||||
}
|
||||
|
||||
|
||||
@media (hover: hover) {
|
||||
a:hover {
|
||||
background-color: hsla(160, 100%, 37%, 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 1024px) {
|
||||
body {
|
||||
display: flex;
|
||||
place-items: center;
|
||||
}
|
||||
|
||||
#app {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 2fr;
|
||||
padding: 0 2rem;
|
||||
}
|
||||
|
||||
footer {
|
||||
border-top: 1px solid var(--color-border);
|
||||
padding-top: 1rem;
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
}
|
||||
|
||||
textarea {
|
||||
width: 100%;
|
||||
min-width: 100%;
|
||||
max-width: fit-content;
|
||||
padding: 1rem;
|
||||
margin: 1rem 0;
|
||||
border: 1px solid hsla(160, 100%, 37%, 1);
|
||||
border-radius: 5px;
|
||||
background: #222222;
|
||||
color: #f8f8f8;
|
||||
font-family: monospace;
|
||||
|
||||
&.just-right {
|
||||
height: fit-content;
|
||||
min-height: fit-content;
|
||||
max-height: fit-content;
|
||||
}
|
||||
|
||||
&.noresize {
|
||||
resize: none;
|
||||
}
|
||||
}
|
||||
|
||||
summary {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
footer {
|
||||
margin-top: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
6
src/main.ts
Normal file
6
src/main.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import './assets/main.css'
|
||||
|
||||
import { createApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
|
||||
createApp(App).mount('#app')
|
||||
Reference in New Issue
Block a user