summaryrefslogtreecommitdiffstats
path: root/vcp
blob: 182e4154ffe5671eb38a704abf0f7a2c96bd699a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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