Skip to content

Commit f989975

Browse files
committed
Configurations Files
Server and Client have different configuration files Configuration file generator and reader script Updated scripts to use configuration files Updated some script options Moved install from lib to lib/GUFI Updated RPMs Added configuration script to both Server and Client pre-install calls pip Added *.pyc to .gitignore
1 parent 74e9582 commit f989975

8 files changed

+481
-233
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.a
22
*.o
3+
*.pyc
34
db.db
4-
build
5+
build

scripts/CMakeLists.txt

+29-8
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,38 @@
7575

7676
cmake_minimum_required(VERSION 3.0.0)
7777

78-
# list of scripts that act as tools
79-
set(TOOLS gufi_find gufi_stats gufi_ls query_builder.py)
80-
78+
# list this first so that make install will overwrite these files
8179
if (PARAMIKO)
8280
# add the paramiko source for installing
8381
# keep the trailing / to rename the directory
84-
install(DIRECTORY ${DEP_BUILD_PREFIX}/paramiko-master/ DESTINATION paramiko COMPONENT Client)
82+
install(DIRECTORY ${DEP_BUILD_PREFIX}/paramiko-master/ DESTINATION lib/GUFI/paramiko COMPONENT Client)
83+
84+
# copy the configuration handling script into both the Server and Client RPMs
85+
configure_file(gufi_config gufi_config @ONLY)
86+
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/gufi_config DESTINATION bin COMPONENT Server)
87+
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/gufi_config DESTINATION bin COMPONENT Client)
88+
89+
# common.py is also needed by the client
90+
configure_file(gufi_common.py common.py @ONLY)
91+
install(FILES gufi_common.py DESTINATION lib/GUFI COMPONENT Client)
8592

86-
# copy gufi_client into the build directory for easy access
87-
configure_file(gufi_client gufi_client @ONLY)
93+
# create individual client scripts
94+
set(TOOL find)
95+
configure_file(gufi_client gufi_client_find @ONLY)
96+
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/gufi_client_find RENAME gufi_find DESTINATION bin COMPONENT Client)
8897

89-
# install gufi_client separately
90-
install(PROGRAMS gufi_client DESTINATION bin COMPONENT Client)
98+
set(TOOL ls)
99+
configure_file(gufi_client gufi_client_ls @ONLY)
100+
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/gufi_client_ls RENAME gufi_ls DESTINATION bin COMPONENT Client)
101+
102+
set(TOOL stats)
103+
configure_file(gufi_client gufi_client_stats @ONLY)
104+
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/gufi_client_stats RENAME gufi_stats DESTINATION bin COMPONENT Client)
91105
endif()
92106

107+
# list of scripts that act as tools on the server
108+
set(TOOLS gufi_find gufi_stats gufi_ls)
109+
93110
# copy and install scripts that act like binaries
94111
foreach(TOOL ${TOOLS})
95112
# copy the tools into the build directory for easy access
@@ -98,3 +115,7 @@ foreach(TOOL ${TOOLS})
98115
# install these tools into ${CMAKE_INSTALL_PREFIX}/bin
99116
install(PROGRAMS ${TOOL} DESTINATION bin COMPONENT Server)
100117
endforeach()
118+
119+
# common.py is not an executable
120+
configure_file(gufi_common.py common.py @ONLY)
121+
install(FILES gufi_common.py DESTINATION lib/GUFI COMPONENT Server)

scripts/gufi_client

+65-55
Original file line numberDiff line numberDiff line change
@@ -74,82 +74,87 @@
7474

7575

7676

77-
78-
import argparse
7977
import getpass
78+
import imp
8079
import os
81-
import pwd
8280
import sys
8381

84-
# get location of this file
85-
# assumes both client and server were installed to directories with the same name on different machines
86-
INSTALL_PREFIX = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
82+
# location of this file
83+
PATH = os.path.realpath(__file__)
84+
DIR = os.path.dirname(PATH)
85+
86+
gufi_config = imp.load_source('gufi_config', os.path.join(DIR, 'gufi_config'))
8787

88-
# command line argument aliases of available tools
89-
# assumes that all of the tools are installed under the same directory
90-
# a prefix will be prepended onto the selected tool
91-
TOOLS = {
92-
'find' : 'gufi_find',
93-
'ls' : 'gufi_ls',
94-
'stats' : 'gufi_stats',
95-
}
88+
class Args:
89+
def __init__(self):
90+
self.config = gufi_config.DEFAULT_CONFIG_PATH
91+
self.args = []
9692

97-
def parse_args(args = None):
93+
def parse_args(cmd_args = sys.argv):
9894
'''
99-
Create an argument parser and return the parsed arguments
95+
Manually parse arguments
10096
'''
10197

102-
def get_username(username):
103-
return pwd.getpwnam(username).pw_name
104-
105-
def get_port(port):
106-
if type(port) != int:
107-
port = int(port)
108-
109-
if (0 < port) and (port < 65536):
110-
return port
111-
112-
raise argparse.ArgumentTypeError("Invalid Port: {}".format(port))
113-
114-
parser = argparse.ArgumentParser(description='gufi_client')
115-
parser.add_argument('--version', '-v', action='version', version=os.path.basename(os.path.realpath(__file__)) + ' @GUFI_VERSION@')
116-
parser.add_argument('--username', dest='username', type=get_username, default=getpass.getuser(), help='Username (not UID)')
117-
parser.add_argument('-p', '--port', dest='port', type=get_port, default=22, help='SSH port')
118-
parser.add_argument('--known_hosts', dest='known_hosts', type=str, default=['/etc/ssh/ssh_known_hosts'], action='append', help='File containing hosts known to the system')
119-
parser.add_argument('--hostkey', dest='hostkey', type=str, default='/etc/ssh/ssh_host_rsa_key.pub', help='Public key of host')
120-
parser.add_argument('--paramiko', dest='paramiko', type=str, default=os.path.join(INSTALL_PREFIX, 'paramiko'), help='Location of paramiko on client machine')
121-
parser.add_argument('--gufi_bin', dest='gufi_bin', type=str, default=os.path.join(INSTALL_PREFIX, 'bin'), help='GUFI binaries directory on server')
122-
parser.add_argument('hostname', type=str, help='GUFI server hostname')
123-
parser.add_argument('gufi_tree', type=str, help='GUFI tree path')
124-
parser.add_argument('tool', type=str, choices=TOOLS.keys(), help='GUFI tool to use')
125-
parser.add_argument('args', nargs='*', default=[], help='Arguments to pass to the GUFI tool')
126-
127-
if args is None:
128-
args = sys.argv[1:]
129-
130-
return parser.parse_args(args)
131-
132-
def contact_server(args):
98+
if len(cmd_args) == 0:
99+
sys.stderr.write('Bad command line arguments\n')
100+
sys.exit(1)
101+
102+
tool = os.path.basename(cmd_args[0])
103+
104+
args = Args()
105+
106+
i = 1
107+
while i < len(cmd_args):
108+
if (cmd_args[i] == '--help') or (cmd_args[i] == '-h'):
109+
print 'usage: ' + tool + ' [-h] [--version] [--config CONFIG] [args [args ...]]'
110+
print
111+
print tool
112+
print
113+
print 'positional arguments:'
114+
print ' args arguments to ' + tool + ' to pass to the server'
115+
print
116+
print 'optional arguments:'
117+
print ' -h, --help show this help message and exit'
118+
print ' --version show program\'s version number and exit'
119+
print ' --config CONFIG Configuration file name'
120+
sys.exit(0)
121+
elif cmd_args[i] == '--version':
122+
print tool + ' @GUFI_VERSION@'
123+
sys.exit(0)
124+
elif cmd_args[i] == '--config':
125+
i += 1
126+
args.config = cmd_args[i]
127+
elif cmd_args[i] == '--':
128+
i += 1
129+
break
130+
else:
131+
args.args += [cmd_args[i]]
132+
i += 1
133+
134+
args.args += cmd_args[i:]
135+
return args
136+
137+
def talk_to_server(args):
133138
'''
134139
Given a set of arguments, attempt to connect to a remote host and run the command
135140
'''
136141
try:
137142
ssh = paramiko.client.SSHClient()
138143

139144
# load known server host keys
140-
for known_hosts in args.known_hosts:
141-
ssh.load_system_host_keys(os.path.expanduser(known_hosts))
145+
ssh.load_system_host_keys(os.path.expanduser(args.known_hosts))
142146

143147
# connect to the server
144148
ssh.connect(hostname=args.hostname,
145149
port=args.port,
146-
username=args.username,
150+
username=getpass.getuser(),
147151
hostkey=paramiko.opensshkey.load_pubkey_from_file(args.hostkey))
148152

149153
# run the command
150-
cmd = os.path.join(args.gufi_bin, TOOLS[args.tool]) + ' ' + ' '.join(['"' + arg + '"' for arg in args.args]) + ' --bfq ' + os.path.join(args.gufi_bin, 'bfq')
154+
# assumes that gufi_@TOOL@ can be found in PATH
155+
cmd = os.path.join('gufi_@TOOL@ ' + ' '.join(['"' + arg + '"' for arg in args.args]))
151156

152-
_, stdout, stderr = ssh.exec_command(cmd + ' ' + args.gufi_tree)
157+
_, stdout, stderr = ssh.exec_command(cmd)
153158

154159
try:
155160
sys.stdout.write(stdout.read())
@@ -160,10 +165,15 @@ def contact_server(args):
160165
ssh.close()
161166

162167
if __name__ == '__main__':
163-
args = parse_args()
168+
args = parse_args(sys.argv)
169+
config = gufi_config.read_filename(args.config)
170+
args.hostname = config['Server']
171+
args.port = config['Port']
172+
args.known_hosts = config['KnownHosts']
173+
args.hostkey = config['HostKey']
164174

165175
# import paramiko from provided path
166-
sys.path.append(args.paramiko)
176+
sys.path.append(config['Paramiko'])
167177
import paramiko
168178

169-
contact_server(args)
179+
talk_to_server(args)

scripts/query_builder.py scripts/gufi_common.py

+7
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ def get_size(value):
113113
raise argparse.ArgumentTypeError("%s is not a valid size" % value)
114114
return value
115115

116+
def get_port(port):
117+
'''Make sure the value is an integer between 0 and 65536'''
118+
p = int(port)
119+
if (p < 0) or (65535 < p):
120+
raise argparse.ArgumentTypeError("Bad port: %d" % p)
121+
return p
122+
116123
def get_uid(uid_str):
117124
'''
118125
Attempts to convert a string into an integer

0 commit comments

Comments
 (0)