8
9
from shlex import split
9
11
from subprocess import (
15
19
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
21
from charmhelpers import fetch
17
22
from charmhelpers.core import (
27
GPFS_PATH = '/Linux/x86_64'
22
30
def add_to_path(p, new):
23
31
return p if new in p.split(':') else p + ':' + new
25
33
os.environ['PATH'] = add_to_path(os.environ['PATH'], '/usr/lpp/mmfs/bin')
36
## installation methods ##
37
def _get_source(url, source_dir=""):
38
return "deb %s%s ./" % (url, source_dir)
41
def check_empty_source(gpfs_url):
42
# Check if there is a repository to install from
43
# TODO: check if packages are already installed
45
log("No GPFS source available. Use juju set gpfs gpfs_url=<url> "
46
"to install packages. See README for more info.", log.ERROR)
50
def gpfs_install(url, version):
51
# install main gpfs packages
52
gpfs_source = _get_source(url, source_dir=GPFS_PATH)
53
log('Gpfs source' + gpfs_source)
55
# Add gpfs source and update apt
56
fetch.add_source(gpfs_source)
58
fetch.apt_update(fatal=True)
59
# Install gpfs packages
60
fetch.apt_install("gpfs*", options=["--allow-unauthenticated"],
62
except CalledProcessError:
63
#TODO: verify source was configured correctly
67
def gpfs_install_updates(url, version, update=True):
68
# install update packages (patches), if given
70
log('Applying patches')
71
source_dir = '/gpfs' + version + '_patches' + GPFS_PATH
72
patch_source = _get_source(url, source_dir=source_dir)
73
fetch.add_source(patch_source)
74
fetch.apt_update(fatal=True)
75
fetch.apt_install("gpfs*", options=["--allow-unauthenticated"],
27
79
## ssh key methods ##
28
80
def create_ssh_keys():
29
81
# Generate ssh keys if needed
90
145
idfile.write("StrictHostKeyChecking no\n")
149
## Other utility methods##
150
def validate_quorum(quorum):
151
valid_values = ['', 'quorum', 'non-quorum']
152
return quorum if quorum in valid_values else ''
93
155
## cluster management methods ##
157
# build binary gpfs modules after installation
159
check_call(["mmbuildgpl"])
160
except CalledProcessError:
161
log('mmbuildgpl was not executed', level=hookenv.WARNING)
163
log('mmbuildgpl not found/installed')
96
167
# Only manager can start/stop cluster
98
169
log(check_output(split('mmstartup -a')))
101
173
# stop the server
103
175
check_output(split('mmshutdown -a'))
105
178
def node_exists(nodename):
106
179
# Check if node has already been added to cluster
107
180
lscluster = check_output('mmlscluster')
108
181
node = re.search('^.*\d+.*%s.*\n' % nodename, lscluster, re.M)
109
182
return False if node is None else True
111
185
def add_node(nodename, m_designation='client', q_designation='nonquorum'):
112
186
# add new node to the cluster
113
187
if cluster_exists() and not node_exists(nodename):
114
log(check_output(split('mmaddnode -N %s:%s-%s' % \
115
(nodename, m_designation, q_designation))))
188
log(check_output(split('mmaddnode -N %s:%s-%s' %
189
(nodename, m_designation, q_designation))))
117
191
log('Node %s could not be added. Manager not defined' % nodename)
119
194
def apply_license(nodename):
120
195
check_output(split('mmchlicense server --accept -N %s' % nodename))
122
198
def set_manager_file():
123
199
# define a file that indicates the node is a manager
124
open("/var/mmfs/.manager","a").close()
200
open("/var/mmfs/.manager", "a").close()
126
203
def is_manager():
127
204
# check if the node is a manager
128
205
if os.path.exists("/var/mmfs/.manager"):
131
209
def cluster_exists():
132
210
# Check if cluster is already defined
133
211
return True if call('mmlscluster') == 0 else False
135
214
def create_cluster(hostname):
136
215
# create the cluster for the hostname, manager only
137
if not is_manager() and not cluster_exists():
138
log(check_output(split('mmcrcluster -N %s:quorum-manager' % \
139
hostname + ' -r /usr/bin/ssh -R /usr/bin/scp')))
216
if is_manager() and not cluster_exists():
217
log(check_output(split('mmcrcluster -N %s:quorum-manager' %
218
hostname + ' -r /usr/bin/ssh -R /usr/bin/scp')))
221
def chk_dns(hostname, ip):
223
socket.gethostbyname(hostname)
225
log("Hostname not resolving, adding to /etc/hosts")
226
with open("/etc/hosts", "a") as hostfile:
227
hostfile.write("%s %s\n" % (ip, hostname))