summaryrefslogtreecommitdiffstats
path: root/vcp
diff options
context:
space:
mode:
Diffstat (limited to 'vcp')
-rwxr-xr-xvcp173
1 files changed, 173 insertions, 0 deletions
diff --git a/vcp b/vcp
new file mode 100755
index 0000000..182e415
--- /dev/null
+++ b/vcp
@@ -0,0 +1,173 @@
+#! /bin/sh
+#
+# usage: vcp COMMAND [COMMAND_ARGS...]
+#
+# To get a list of all available commands and their type, run vcp helpCommands
+#
+
+# helpCommands : TODO
+_netcup_vcp_cmd_helpCommands() {
+ sed -n 's/^# \([^ ]\+ : .*\)/\1/p' "$0" | sort
+}
+
+
+# login : TODO
+_netcup_vcp_cmd_login() {
+ _netcup_vcp_post Login \
+ -L \
+ -d username="$netcup_vcp_username" \
+ -d password="$netcup_vcp_password" \
+ -d doLogin=Login
+}
+
+# kvmInformation : VServerID -> TODO
+_netcup_vcp_cmd_kvmInformation() {
+ _netcup_vcp_post VServersKVM \
+ -q page=vServerKVMGeneral \
+ -q section=kvmInformation \
+ -q selectedVServerId="$1"
+}
+
+# attachCdrom : VServerID, ImageName -> TODO
+_netcup_vcp_cmd_attachCdrom() {
+ _netcup_vcp_post VServersKVM \
+ -q page=vServerKVMCdrom \
+ -q section=vServerKVMCdromFTP \
+ -q selectedVServerId="$1" \
+ -d attachCdrom='attach CD' \
+ -d vcpLoginPassword="$netcup_vcp_password" \
+ -d imageName="$2"
+}
+
+# detachCdrom : VServerID -> TODO
+_netcup_vcp_cmd_detachCdrom() {
+ _netcup_vcp_post VServersKVM \
+ -q page=vServerKVMCdrom \
+ -q section=vServerKVMCdromFTP \
+ -q selectedVServerId="$1" \
+ -d detachCdrom=
+}
+
+# getLogEntries : VServerID -> TODO
+_netcup_vcp_cmd_getLogEntries() {
+ _netcup_vcp_post VServers \
+ -q page=page_log \
+ -q selectedVServerId="$1" \
+ -d getLogEntries=true \
+ -d paging_maxResults=10 \
+ -d paging_pageSelected=1
+}
+
+# controlAction : VServerID, ControlAction -> TODO
+# ControlAction ∈ { STOP, REBOOT, RESET, POWEROFF, SUSPEND, SAVE, START }
+_netcup_vcp_cmd_controlAction() {
+ _netcup_vcp_post VServersKVM \
+ -q page=vServerKVMControl \
+ -q selectedVServerId="$1" \
+ -q action="$2"
+}
+
+# setBootOrder : VServerID, CSV (ListOf BootDevice) -> TODO
+# BootDevice ∈ { cdrom, hd, network }
+_netcup_vcp_cmd_setBootOrder() {
+ _netcup_vcp_post VServersKVM \
+ -q page=vServerKVMGeneral \
+ -q section=bootOrder \
+ -q selectedVServerId="$1" \
+ -d bootOrder="$2"
+}
+
+_netcup_vcp_cmd_ipv4support() {
+ _netcup_vcp_post VServersKVM \
+ -q page=vServerKVMGeneral \
+ -q section=ipv4support \
+ -q selectedVServerId="$1"
+}
+
+# listVServerIds : TODO
+_netcup_vcp_cmd_listVServerIds() {
+ _netcup_vcp_parse_vserver_ids
+}
+
+_netcup_vcp_parse_site_key() {
+ sed -n 's/.*site_key\s*=\s*"\([0-9A-Za-z]\+\)";.*/\1/p;T;q' "$_netcup_vcp_content_cache"
+}
+
+_netcup_vcp_parse_vserver_ids() {
+ sed -n 's/.*\<selectedVServerId=\([0-9]\+\).*/\1/p' "$_netcup_vcp_content_cache" \
+ | sort -u
+}
+
+_netcup_vcp_content_cache=${netcup_vcp_content_cache-$HOME/tmp/netcup_vcp_content_cache}
+_netcup_vcp_cookie_jar=${netcup_vcp_cookie_jar-$HOME/tmp/netcup_vcp_cookie_jar}
+_netcup_vcp_secrets_file=${netcup_vcp_secrets-$HOME/.secrets/netcup_vcp}
+
+_netcup_vcp_loadSecrets() {
+ if ! test -r "$_netcup_vcp_secrets_file"; then
+ echo "unable to load secrets from '$_netcup_vcp_secrets_file'" >&2
+ return 1
+ else
+ . "$_netcup_vcp_secrets_file"
+ fi
+}
+
+_netcup_vcp_post() {(
+ endpoint=
+ query=site_key=$(_netcup_vcp_parse_site_key)
+ data=
+ flags=
+
+ while test $# -gt 0; do
+ case $1 in
+ -L)
+ flags=${flags:+$flags }$1
+ shift
+ ;;
+ -q)
+ query=${query:+$query&}$2
+ shift 2
+ ;;
+ -d)
+ data=${data:+$data&}$2
+ shift 2
+ ;;
+ -*)
+ printf 'unknown option %q\n' "$1" >&2
+ exit 1
+ ;;
+ *)
+ if test -n "$endpoint"; then
+ printf 'unexpected argument %q\n' "$1" >&2
+ exit 2
+ fi
+ endpoint=/$1
+ shift 1
+ ;;
+ esac
+ done
+
+ url=https://www.vservercontrolpanel.de/$endpoint?$query
+
+ echo "$data" |
+ exec curl \
+ -d @- \
+ -b "$_netcup_vcp_cookie_jar" \
+ -c "$_netcup_vcp_cookie_jar" \
+ -o "$_netcup_vcp_content_cache" \
+ -sS \
+ $flags \
+ "$url"
+)}
+
+_netcup_vcp_main() {(
+ set -efu
+ _netcup_vcp_loadSecrets
+ _netcup_vcp_cmd_"$@"
+)}
+
+
+case ${netcup_vpc_run-true} in
+ true)
+ _netcup_vcp_main "$@"
+ ;;
+esac