Newer
Older
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
#!/usr/bin/python
'''
Copyright 2012 ETH Zuerich, CISD
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
# @author Manuel Kohler
import subprocess
import shlex
import getpass
import xmlrpclib
import ConfigParser
from datetime import datetime
SERVERS = {
"openbis@openbis-phosphonetx.ethz.ch" : "phosphonetx",
"openbis@obis.ethz.ch" : "yeastx",
"openbis@lmc-openbis.ethz.ch" : "lmc",
"openbis@infectx01.ethz.ch" : "infectx",
"sbsuser@openbis-dsu.ethz.ch" : "dsu",
"openbis@bs-plasmids.ethz.ch" : "csb",
"openbis@openbis-scu.ethz.ch" : "scu",
"openbis@basysbio.ethz.ch" : "basybio",
"openbis@openbis-cina.ethz.ch" : "cina",
"openbis@limb.ethz.ch" : "limb",
"openbis@newchipdb.ethz.ch" : "newchipdb",
"openbis@basynthec.ethz.ch" : "basynthec",
"openbis@sprint-openbis.ethz.ch" : "sprint",
"openbis@cisd-openbis.ethz.ch" : "demo"
}
PSQL_PROPERTIES = ["archive_mode",
"bgwriter_delay",
"bgwriter_lru_maxpages",
"bgwriter_lru_multiplier",
"checkpoint_segments",
"checkpoint_timeout",
"effective_cache_size",
"effective_io_concurrency",
"maintenance_work_mem",
"max_connections",
"server_version",
"shared_buffers",
"temp_buffers",
"wal_segment_size",
]
SSH_BIN = "/usr/bin/ssh"
SSH_PARAMETERS = "-oConnectTimeout=5"
PSQL_BIN = "/usr/bin/psql"
PSQL_PARAMETERS = " -Upostgres -c \\\"show all;\\\""
SINGLE_SPACE = " "
DOUBLE_QUOTE = "\""
NEWLINE = "\n"
PIPE = "|"
SERVER = 'https://wiki-bsse.ethz.ch:8443/rpc/xmlrpc'
CONFIGFILE = "wikiPostgres.conf"
ConfluenceToken = None
ConfluenceServer = xmlrpclib.ServerProxy(SERVER)
def parseConfigurationFile():
'''
Python 2.7 Style
reads out the settings defined in the config file
'''
config = ConfigParser.ConfigParser()
config.read(CONFIGFILE)
return config
def getTimestamp():
return datetime.now().strftime("%d %b %Y %H:%M:%S")
def logIntoConfluence(config):
'''
Logs user into Confluence and returns a session token
'''
user = config.get("DEFAULT", "username")
password = config.get("DEFAULT", "password")
ConfluenceToken = ConfluenceServer.confluence1.login(user, password)
return ConfluenceToken
def logOutConluence(token):
'''
Logs out from confluence and invalidates the session token
'''
ConfluenceServer.confluence1.logout(token)
def writeToConfluence (serverMap, token, config):
'''
Builds the content of the page and writes it into confluence
'''
wikiMarkup = ""
spacekey = config.get("DEFAULT", "spacekey")
pagetitle = config.get("DEFAULT", "pagetitle")
page = ConfluenceServer.confluence1.getPage(token, spacekey, pagetitle)
if page is None:
exit("Could not find page " + spacekey + ":" + pagetitle)
wikiMarkup = "h1. Server List" + NEWLINE
wikiMarkup += "{table-plus:|autoNumber=true}" + NEWLINE
wikiMarkup += PIPE + SINGLE_SPACE + PIPE
for server in (serverMap.iterkeys()):
wikiMarkup += PIPE + PIPE + SERVERS[server]
wikiMarkup += NEWLINE
try:
for property in PSQL_PROPERTIES:
wikiMarkup += PIPE + property + PIPE
for server in (serverMap.iterkeys()):
wikiMarkup += serverMap[server][property] + PIPE
wikiMarkup += NEWLINE
except:
print("Not run correctly! Check ssh logins.")
wikiMarkup += NEWLINE + "{table-plus}"
wikiMarkup += NEWLINE + "Last update:" + SINGLE_SPACE + getTimestamp()
page["content"] = wikiMarkup
ConfluenceServer.confluence1.storePage(token, page)
def callCommandLine(args):
'''
Calls a command line program and returns the standard out
'''
SplitArgs = shlex.split(args)
p = subprocess.Popen(SplitArgs, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
out, err = p.communicate()
return out
def dictOutput (commandLineOutput):
'''
Parses the psql output and puts it into a dictionary which is the return value
'''
# skip the header
settingsList = commandLineOutput.split("\n")[2:]
settingsDict = {}
for item in settingsList:
try:
splittedItem = item.split("|")[:-1]
key, value = splittedItem[0].strip() , splittedItem[1].strip()
settingsDict[key] = value
except IndexError:
pass
except AttributeError:
settingsDict[key] = [value]
return settingsDict
def main():
serverMap = {}
config = parseConfigurationFile()
confluenceToken = logIntoConfluence(config)
for (server, alias) in SERVERS.items():
args = SSH_BIN + SINGLE_SPACE + SSH_PARAMETERS + SINGLE_SPACE + server + SINGLE_SPACE + \
DOUBLE_QUOTE + PSQL_BIN + PSQL_PARAMETERS + DOUBLE_QUOTE
commandLineOutput = callCommandLine(args)
sDict = dictOutput (commandLineOutput)
serverMap [server] = sDict
writeToConfluence(serverMap, confluenceToken, config)
logOutConluence(confluenceToken)
if __name__ == "__main__":
main()