-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcloud.js
156 lines (144 loc) · 4.57 KB
/
gcloud.js
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
'use strict';
const cp=require('child_process');
const fs=require('fs');
var inst_data;
var proj_data;
var gcloud_bin;
var zones;
module.exports={
tech:"gcloud",
detect:function(){
//give a chance for wrapping:
if (fs.existsSync('/usr/local/bin/gcloud')) {
gcloud_bin='/usr/local/bin/gcloud';
return true;
}
//the "proper" location
if (fs.existsSync('/usr/lib/google-cloud-sdk/bin/gcloud')) {
gcloud_bin='/usr/lib/google-cloud-sdk/bin/gcloud';
return true;
}
//the new "proper" location
if (fs.existsSync('/snap/bin/gcloud')) {
gcloud_bin='/snap/bin/gcloud';
return true;
}
//just to make sure..
if (fs.existsSync('/usr/bin/gcloud')) {
gcloud_bin='/usr/bin/gcloud';
return true;
}
if (fs.existsSync('/bin/gcloud')) {
gcloud_bin='/bin/gcloud';
return true;
}
return false;
},
get_inst_data:function(info){
if (inst_data) return inst_data;
if (!info.localips || !(info.localips instanceof Array)) return undefined;
let inst=cp.execSync(gcloud_bin+" --format=json compute instances list").toString();
inst=JSON.parse(inst);
for (let i of inst) {
if (i.status!='RUNNING') continue;
for (let ni of i.networkInterfaces){
for (let lip of info.localips) if (lip==ni.networkIP) {
inst_data=i;
inst_data.networkInterface=ni;
return inst_data;
}
}
}
return undefined;
},
get_extip:function(info){
if (this.get_inst_data(info)===undefined) return '';
for (let ac of inst_data.networkInterface.accessConfigs) {
if (ac.natIP) return ac.natIP;
}
return '';
},
instance_nm:function(info){
if (this.get_inst_data(info)===undefined) return '';
return inst_data.name;
},
get_metadata:function(info){
let proj=cp.execSync(gcloud_bin+" --format=json compute project-info describe").toString();
proj=JSON.parse(proj);
let md={};
if (proj && proj.commonInstanceMetadata && proj.commonInstanceMetadata.items) for (let i of proj.commonInstanceMetadata.items) {
if(i.key=='sshKeys') continue;
md[i.key]=i.value;
}
if (this.get_inst_data(info)===undefined) return md;
if (inst_data && inst_data.metadata && inst_data.metadata.items) for (let i of inst_data.metadata.items) {
md[i.key]=i.value;
}
return md;
},
get_zones_data() {
if (zones) return zones;
zones=JSON.parse(cp.execSync(gcloud_bin+" --format=json dns managed-zones list").toString());
},
get_zonename(domain) {
if (domain[domain.length-1]!='.') domain=domain+'.';
this.get_zones_data();
let bestzone=undefined;
let bestzdnsname=undefined;
for (let zone of zones) {
if (zone.dnsName==domain) return zone.name;
if (domain.endsWith('.'+zone.dnsName)) {
if (bestzone===undefined) {
bestzone=zone.name;
bestzdnsname=zone.dnsName;
} else {
if (zone.dnsName>bestzdnsname){
bestzone=zone.name;
bestzdnsname=zone.dnsName;
}
}
}
}
return bestzone;
},
update_dns:function(info,name,ip,ttl) {
let parts=name.split('.');
if (parts.length<2) return false;
let domain=name.substr((parts[0].length+1)-name.length);
let zone=this.get_zonename(domain);
if (!zone) return false;
ttl=parseInt(ttl,10);
if (!ttl) ttl=300;
if (name[name.length-1]!='.') name=name+'.';
var dns_records=JSON.parse(cp.execSync(gcloud_bin+" --format=json dns record-sets list --filter='type=\"A\" AND kind=\"dns#resourceRecordSet\"' '--zone="+zone+"'").toString());
for (let r of dns_records) if (r.name==name) { //name found
var cur_extips='';
for (let rip of r.rrdatas) {
cur_extips=(cur_extips==''?'':cur_extips+' ')+"'"+rip+"'";
if (ip==rip && ttl==r.ttl) { //no update needed!
console.log("update_dns no update needed!");
return true;
}
}
//no ip match must update:
let script=
gcloud_bin+" dns record-sets transaction start '-z="+zone+"' ; "+
gcloud_bin+" dns record-sets transaction remove '-z="+zone+"' '--name="+name+"' --type=A --ttl="+r.ttl+" "+cur_extips+" ; "+
gcloud_bin+" dns record-sets transaction add '-z="+zone+"' '--name="+name+"' --type=A --ttl="+ttl+" '"+ip+"' ; "+
gcloud_bin+" dns record-sets transaction execute '-z="+zone+"' "
;
//console.log("update_dns update script:"+script);
cp.execSync(script);
return true;
}
//pure create
let script=
gcloud_bin+" dns record-sets transaction start '-z="+zone+"' ; "+
gcloud_bin+" dns record-sets transaction add '-z="+zone+"' '--name="+name+"' --type=A --ttl="+ttl+" '"+ip+"' ; "+
gcloud_bin+" dns record-sets transaction execute '-z="+zone+"' "
;
//console.log("update_dns create script:"+script);
cp.execSync(script);
return true;
}
}