-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.py
202 lines (170 loc) · 6.47 KB
/
operations.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'''
'''
import os
import logging
import time
from getpass import getpass
from io import StringIO
#from fabric.api import sudo, get, put, run
#from fabric.context_managers import env
def expandNodeID(nodeId):
'''Add urn:node: to start of nodeID if not present.
'''
if nodeId.startswith('urn:node:'):
return nodeId
return "urn:node:" + nodeId
def escapeSolrQueryTerm(term):
'''
+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
'''
reserved = ['+', '-', '&', '|', '!', '(', ')', '{', '}',
'[', ']', '^', '"', '~', '*', '?', ':',
]
term = term.replace('\\', '\\\\')
for c in reserved:
term = term.replace(c, "\%s" % c)
return term
def tmpFileName(tmp_dir="/tmp", prefix="", ext="txt"):
'''
Returns a temporary file name with a high likelihood of uniqueness.
:param tmp_dir: Folder to contain the file
:param prefix: Prefix for file name
:param ext: Extension for file name (not including the .)
:return: File name and path relative to tmp_dir.
'''
ts = int(time.time()*100000)
return os.path.join(tmp_dir, "{0}_{1}.{2}".format(prefix, ts, ext))
#=======================
#== Fabric Operations ==
def listCNClientCertificates():
'''List the contents of /etc/dataone/client/private
'''
cmd = "ls -la /etc/dataone/client/private"
sudo(cmd)
def getCNClientCertificate(cert_file_name):
'''Retrieve the client certificate + key combination from the host.
The file is retrieved and placed in ~/.dataone/certificates/<host name>
Attributes:
cert_file_name (str): Name of the certificate file located under
/etc/dataone/client/private to be retrieved.
'''
host_name = env.host_string
target_folder = os.path.join(os.path.expanduser('~'), ".dataone/certificates/{0}".format(host_name))
if not os.path.exists(target_folder):
os.makedirs(target_folder, 0o700)
target_file = os.path.join(target_folder, cert_file_name)
if os.path.exists(target_file):
print(("The certificate file already exists: {0}. Rename or remove to download a new copy.".format(target_file)))
return
source_file = os.path.join("/etc/dataone/client/private/", cert_file_name)
get(source_file, target_file, use_sudo=True)
def setNodeSynchronize(node_id, state, ldap_pass=None):
'''Adjust the LDAP entry for the specified node to indicate if node should be synchronized
Attributes:
node_id (str): The node entry to adjust
state (str): Either "up" or "down"
'''
node_id = expandNodeID(node_id)
state = state.lower()
if state not in ("up", "down"):
raise ValueError("State must be 'up' or 'down'")
if ldap_pass is None:
ldap_pass = getpass("Enter LDAP password: ")
ldiff = StringIO()
ldiff.write("dn: cn={0},dc=dataone,dc=org\n".format(node_id))
ldiff.write("changetype: modify\n")
ldiff.write("replace: d1NodeState\n")
ldiff.write("d1NodeState: {0}\n".format(state));
remote_file = "nodeupdate.ldiff"
put(ldiff, remote_file)
cmd = "cat " + remote_file + "; "
cmd += "ldapmodify -H ldap://localhost -D cn=admin,dc=dataone,dc=org "
cmd += " -w '{0}' ".format(ldap_pass)
cmd += " -f {0}".format(remote_file)
run(cmd)
def setNodeState(node_id, state, ldap_pass=None):
'''Adjust the LDAP entry for the specified node to indicate if node is online.
Attributes:
node_id (str): The node entry to adjust
state (str): Either "up" or "down"
'''
node_id = expandNodeID(node_id)
state = state.lower()
if state not in ("up", "down"):
raise ValueError("State must be 'up' or 'down'")
if ldap_pass is None:
ldap_pass = getpass("Enter LDAP password: ")
ldiff = StringIO()
ldiff.write("dn: cn={0},dc=dataone,dc=org\n".format(node_id))
ldiff.write("changetype: modify\n")
ldiff.write("replace: d1NodeState\n")
ldiff.write("d1NodeState: {0}\n".format(state));
remote_file = "nodeupdate.ldiff"
put(ldiff, remote_file)
cmd = "cat " + remote_file + "; "
cmd += "ldapmodify -H ldap://localhost -D cn=admin,dc=dataone,dc=org "
cmd += " -w '{0}' ".format(ldap_pass)
cmd += " -f {0}".format(remote_file)
run(cmd)
def approveNode(node_id, ldap_pass=None, approval=False):
'''Adjust the LDAP entry for a node, indicating if approved or not.
Attributes:
node_id (str): The node entry to adjust
approval (bool): True or False
'''
node_id = expandNodeID(node_id)
state = str(approval).upper()
if state not in ("TRUE", "FALSE"):
raise ValueError("State must be 'TRUE' or 'FALSE'")
if ldap_pass is None:
ldap_pass = getpass("Enter LDAP password: ")
ldiff = StringIO()
ldiff.write("dn: cn={0},dc=dataone,dc=org\n".format(node_id))
ldiff.write("changetype: modify\n")
ldiff.write("replace: d1NodeApproved\n")
ldiff.write("d1NodeApproved: {0}\n".format(state));
remote_file = "nodeupdate.ldiff"
put(ldiff, remote_file)
cmd = "cat " + remote_file + "; "
cmd += "ldapmodify -H ldap://localhost -D cn=admin,dc=dataone,dc=org "
cmd += " -w '{0}' ".format(ldap_pass)
cmd += " -f {0}".format(remote_file)
run(cmd)
def resetNodeHarvestDate(node_id, ldap_pass=None, harvest_timestamp="1900-01-01T00:00:00Z"):
'''Reset the last harvest date for a MN.
'''
node_id = expandNodeID(node_id)
if ldap_pass is None:
ldap_pass = getpass("Enter LDAP password: ")
ldiff = StringIO()
ldiff.write("dn: cn={0},dc=dataone,dc=org\n".format(node_id))
ldiff.write("changetype: modify\n")
ldiff.write("replace: d1NodeLastHarvested\n")
ldiff.write("d1NodeLastHarvested: {0}\n".format(harvest_timestamp));
remote_file = "nodeupdate.ldiff"
put(ldiff, remote_file)
cmd = "cat " + remote_file + "; "
cmd += "ldapmodify -H ldap://localhost -D cn=admin,dc=dataone,dc=org "
cmd += " -w '{0}' ".format(ldap_pass)
cmd += " -f {0}".format(remote_file)
run(cmd)
def resetNodeLogAggregationDate(node_id, ldap_pass=None, harvest_timestamp="1900-01-01T00:00:00Z"):
'''Reset the last log aggregation harvest date for a MN.
'''
node_id = expandNodeID(node_id)
if ldap_pass is None:
ldap_pass = getpass("Enter LDAP password: ")
ldiff = StringIO()
ldiff.write("dn: cn={0},dc=dataone,dc=org\n".format(node_id))
ldiff.write("changetype: modify\n")
ldiff.write("replace: d1NodeLogLastAggregated\n")
ldiff.write("d1NodeLogLastAggregated: {0}\n".format(harvest_timestamp));
remote_file = "nodeupdate.ldiff"
put(ldiff, remote_file)
cmd = "cat " + remote_file + "; "
cmd += "ldapmodify -H ldap://localhost -D cn=admin,dc=dataone,dc=org "
cmd += " -w '{0}' ".format(ldap_pass)
cmd += " -f {0}".format(remote_file)
run(cmd)
#========================
#== CRUD on MN custom properties