]>
Commit | Line | Data |
---|---|---|
dd17ac50 | 1 | #!/usr/bin/env sh |
38f23343 | 2 | |
59182dbc PG |
3 | #Author: Philipp Grosswiler <philipp.grosswiler@swiss-design.net> |
4 | ||
9a473640 | 5 | LINODE_API_URL="https://api.linode.com/?api_key=$LINODE_API_KEY&api_action=" |
38f23343 PG |
6 | |
7 | ######## Public functions ##################### | |
8 | ||
9 | #Usage: dns_linode_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
10 | dns_linode_add() { | |
11 | fulldomain="${1}" | |
12 | txtvalue="${2}" | |
13 | ||
59182dbc PG |
14 | if ! _Linode_API; then |
15 | return 1 | |
16 | fi | |
17 | ||
38f23343 PG |
18 | _info "Using Linode" |
19 | _debug "Calling: dns_linode_add() '${fulldomain}' '${txtvalue}'" | |
20 | ||
59182dbc PG |
21 | _debug "First detect the root zone" |
22 | if ! _get_root "$fulldomain"; then | |
23 | _err "Domain does not exist." | |
24 | return 1 | |
25 | fi | |
26 | _debug _domain_id "$_domain_id" | |
27 | _debug _sub_domain "$_sub_domain" | |
28 | _debug _domain "$_domain" | |
29 | ||
9a473640 | 30 | _parameters="&DomainID=$_domain_id&Type=TXT&Name=$_sub_domain&Target=$txtvalue" |
59182dbc | 31 | |
9a473640 AS |
32 | if _rest GET "domain.resource.create" "$_parameters" && [ -n "$response" ]; then |
33 | _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1) | |
59182dbc PG |
34 | _debug _resource_id "$_resource_id" |
35 | ||
36 | if [ -z "$_resource_id" ]; then | |
37 | _err "Error adding the domain resource." | |
38 | return 1 | |
39 | fi | |
38f23343 | 40 | |
59182dbc PG |
41 | _info "Domain resource successfully added." |
42 | return 0 | |
43 | fi | |
44 | ||
45 | return 1 | |
38f23343 PG |
46 | } |
47 | ||
48 | #Usage: dns_linode_rm _acme-challenge.www.domain.com | |
49 | dns_linode_rm() { | |
50 | fulldomain="${1}" | |
51 | ||
59182dbc PG |
52 | if ! _Linode_API; then |
53 | return 1 | |
54 | fi | |
55 | ||
38f23343 PG |
56 | _info "Using Linode" |
57 | _debug "Calling: dns_linode_rm() '${fulldomain}'" | |
58 | ||
59182dbc PG |
59 | _debug "First detect the root zone" |
60 | if ! _get_root "$fulldomain"; then | |
61 | _err "Domain does not exist." | |
62 | return 1 | |
63 | fi | |
64 | _debug _domain_id "$_domain_id" | |
65 | _debug _sub_domain "$_sub_domain" | |
66 | _debug _domain "$_domain" | |
67 | ||
9a473640 AS |
68 | _parameters="&DomainID=$_domain_id" |
69 | ||
70 | if _rest GET "domain.resource.list" "$_parameters" && [ -n "$response" ]; then | |
c145f246 | 71 | response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")" |
59182dbc | 72 | |
9a473640 | 73 | resource="$(echo "$response" | _egrep_o "{.*\"NAME\":\s*\"$_sub_domain\".*}")" |
59182dbc | 74 | if [ "$resource" ]; then |
9a473640 | 75 | _resource_id=$(printf "%s\n" "$resource" | _egrep_o "\"RESOURCEID\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ ) |
59182dbc PG |
76 | if [ "$_resource_id" ]; then |
77 | _debug _resource_id "$_resource_id" | |
38f23343 | 78 | |
9a473640 AS |
79 | _parameters="&DomainID=$_domain_id&ResourceID=$_resource_id" |
80 | ||
81 | if _rest GET "domain.resource.delete" "$_parameters" && [ -n "$response" ]; then | |
82 | _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1) | |
83 | _debug _resource_id "$_resource_id" | |
59182dbc | 84 | |
9a473640 AS |
85 | if [ -z "$_resource_id" ]; then |
86 | _err "Error deleting the domain resource." | |
59182dbc PG |
87 | return 1 |
88 | fi | |
89 | ||
90 | _info "Domain resource successfully deleted." | |
91 | return 0 | |
92 | fi | |
93 | fi | |
94 | ||
95 | return 1 | |
96 | fi | |
97 | ||
98 | return 0 | |
99 | fi | |
100 | ||
101 | return 1 | |
38f23343 PG |
102 | } |
103 | ||
104 | #################### Private functions below ################################## | |
105 | ||
59182dbc PG |
106 | _Linode_API() { |
107 | if [ -z "$LINODE_API_KEY" ]; then | |
108 | LINODE_API_KEY="" | |
109 | ||
110 | _err "You didn't specify the Linode API key yet." | |
111 | _err "Please create your key and try again." | |
112 | ||
38f23343 PG |
113 | return 1 |
114 | fi | |
59182dbc PG |
115 | |
116 | _saveaccountconf LINODE_API_KEY "$LINODE_API_KEY" | |
38f23343 PG |
117 | } |
118 | ||
59182dbc PG |
119 | #################### Private functions below ################################## |
120 | #_acme-challenge.www.domain.com | |
121 | #returns | |
122 | # _sub_domain=_acme-challenge.www | |
123 | # _domain=domain.com | |
124 | # _domain_id=12345 | |
125 | _get_root() { | |
126 | domain=$1 | |
127 | i=2 | |
128 | p=1 | |
129 | ||
9a473640 | 130 | if _rest GET "domain.list"; then |
c145f246 | 131 | response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")" |
59182dbc PG |
132 | while true; do |
133 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
134 | _debug h "$h" | |
135 | if [ -z "$h" ]; then | |
136 | #not valid | |
137 | return 1 | |
138 | fi | |
38f23343 | 139 | |
9a473640 | 140 | hostedzone="$(echo "$response" | _egrep_o "{.*\"DOMAIN\":\s*\"$h\".*}")" |
59182dbc | 141 | if [ "$hostedzone" ]; then |
9a473640 | 142 | _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"DOMAINID\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ ) |
59182dbc PG |
143 | if [ "$_domain_id" ]; then |
144 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
145 | _domain=$h | |
146 | return 0 | |
147 | fi | |
148 | return 1 | |
149 | fi | |
150 | p=$i | |
151 | i=$(_math "$i" + 1) | |
152 | done | |
38f23343 | 153 | fi |
59182dbc | 154 | return 1 |
38f23343 PG |
155 | } |
156 | ||
59182dbc PG |
157 | #method method action data |
158 | _rest() { | |
159 | mtd="$1" | |
160 | ep="$2" | |
161 | data="$3" | |
162 | ||
163 | _debug mtd "$mtd" | |
164 | _debug ep "$ep" | |
165 | ||
166 | export _H1="Accept: application/json" | |
167 | export _H2="Content-Type: application/json" | |
168 | ||
169 | if [ "$mtd" != "GET" ]; then | |
170 | # both POST and DELETE. | |
171 | _debug data "$data" | |
172 | response="$(_post "$data" "$LINODE_API_URL$ep" "" "$mtd")" | |
173 | else | |
174 | response="$(_get "$LINODE_API_URL$ep$data")" | |
175 | fi | |
176 | ||
177 | if [ "$?" != "0" ]; then | |
178 | _err "error $ep" | |
179 | return 1 | |
180 | fi | |
181 | _debug2 response "$response" | |
182 | return 0 | |
c070407a | 183 | } |