-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
70 lines (63 loc) · 2.38 KB
/
base.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
#!/usr/bin/env python
import ConfigParser
import os
import shlex
import socket
import subprocess
import sys
from optparse import OptionParser
def check_dirs(p):
if not os.path.exists(os.path.dirname(p)):
os.makedirs(os.path.dirname(p))
def update_openssl_config(template_file, output_name, index=None, crlnumber=None):
if not os.path.exists(template_file):
print "Unable to find template file for openssl configuration: %s" % (template_file)
return False
check_dirs(output_name)
template = open(template_file, "r").read()
template = template.replace("REPLACE_CRL_DATABASE_FILE", index)
template = template.replace("REPLACE_CRL_NUMBER_FILE", crlnumber)
out_file = open(output_name, "w").write(template)
return True
def run_command(cmd, verbose=True):
if verbose:
print "Running: %s" % (cmd)
if isinstance(cmd, str):
cmd = shlex.split(cmd.encode('ascii', 'ignore'))
handle = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_msg, err_msg = handle.communicate(None)
if handle.returncode != 0:
print "Error running: %s" % (cmd)
print "stdout:\n%s" % (out_msg)
print "stderr:\n%s" % (err_msg)
return False
return True, out_msg, err_msg
def get_config(filename='config_pulp_certs.cfg', section='certs'):
config = ConfigParser.ConfigParser()
config.read(filename)
cfg = {}
for item,value in config.items(section):
cfg[item] = value
return cfg
def get_parser(config=None, parser=None, description=None, limit_options=None):
if not config:
config = get_config()
if not parser:
if not description:
description="Helper utility to create certs for repository authentication"
parser = OptionParser(description=description)
keys = config.keys()
keys.sort() # want --help to list options in order
for item in keys:
if limit_options:
if item not in limit_options:
continue
value = config[item]
parser.add_option('--%s' % (item), action='store', help="Default value: %s" % value, default=value)
return parser
def add_hostname_option(parser, hostname=None):
if not hostname:
hostname = socket.gethostname()
parser.add_option('--hostname', action='store',
help="Default value: %s" % (hostname), default=hostname)
return parser