summaryrefslogtreecommitdiffstats
path: root/lib/cac.sh
blob: 7ed06012a4e0e9e333544db9759ee98aa2666021 (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
. ./lib/url.sh

cac_listservers() {(
  listservers=$(_cac_get_api_v1 listservers)
  status=$(echo "$listservers" | jq -r .status)
  if [ "$status" = ok ]; then
    echo "$listservers"
  else
    echo "$0: bad listservers status: $status" >&2
    exit 1
  fi
)}

cac_listtasks() {
  _cac_get_api_v1 listtasks
}

cac_listtemplates() {
  _cac_get_api_v1 listtemplates
}

cac_console() {
  _cac_post_api_v1 console sid="$1"
}

cac_powerop() {
  _cac_post_api_v1 powerop sid="$1" action="$2"
}

cac_renameserver() {
  _cac_post_api_v1 renameserver sid="$1" name="$2"
}

cac_rnds() {
  _cac_post_api_v1 rdns sid="$1" hostname="$2"
}

cac_runmode() {
  _cac_post_api_v1 rdns sid="$1" mode="$2"
}

# default os=26 is CentOS-7-64bit
cac_cloudpro_build() {
  _cac_post_api_v1 cloudpro/build cpu="$1" ram="$2" storage="$3" os="${4-26}"
}

cac_cloudpro_delete() {
  _cac_post_api_v1 cloudpro/delete sid="$1"
}

cac_cloudpro_resources() {
  _cac_get_api_v1 cloudpro/resources
}

_cac_get_api_v1() {
  _cac_curl_api_v1 -G "$@"
}

_cac_post_api_v1() {
  _cac_curl_api_v1 -XPOST "$@"
}

_cac_curl_api_v1() {
  _cac_exec curl -fsS "$1" "https://panel.cloudatcost.com/api/v1/$2.php" $(
    shift 2
    set -- "$@" login="$cac_login" key="$cac_key"
    for arg; do
      echo -d $(printf '%s' "$arg" | url_encode)
    done
  )
}

_cac_exec() {
  if test -z "${cac_via-}"; then
    (exec "$@")
  else
    ssh -q "$cac_via" -t "$@"
  fi
}