~charmers/charms/trusty/ibm-mq/trunk

« back to all changes in this revision

Viewing changes to charms/trusty/gpfs/hooks/gpfshooklib.py

  • Committer: Thierry Fauck
  • Date: 2015-03-11 09:38:50 UTC
  • mfrom: (23.1.4 devel)
  • Revision ID: thierry@linux.vnet.ibm.com-20150311093850-e9adl4bz3nj312qt
updated on 03/11

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import sys
6
6
import shutil
7
7
import tempfile
 
8
import socket
8
9
from shlex import split
 
10
import subprocess
9
11
from subprocess import (
10
12
    call,
11
13
    check_call,
12
 
    check_output
 
14
    check_output,
 
15
    Popen,
 
16
    CalledProcessError
13
17
)
14
18
 
15
19
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
16
20
 
 
21
from charmhelpers import fetch
17
22
from charmhelpers.core import (
18
23
    hookenv,
19
24
)
20
25
log = hookenv.log
21
26
 
 
27
GPFS_PATH = '/Linux/x86_64'
 
28
 
 
29
 
22
30
def add_to_path(p, new):
23
31
    return p if new in p.split(':') else p + ':' + new
24
32
 
25
33
os.environ['PATH'] = add_to_path(os.environ['PATH'], '/usr/lpp/mmfs/bin')
26
34
 
 
35
 
 
36
## installation methods ##
 
37
def _get_source(url, source_dir=""):
 
38
    return "deb %s%s ./" % (url, source_dir)
 
39
 
 
40
 
 
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
 
44
    if not gpfs_url:
 
45
        log("No GPFS source available. Use juju set gpfs gpfs_url=<url> "
 
46
            "to install packages. See README for more info.", log.ERROR)
 
47
        sys.exit(0)
 
48
 
 
49
 
 
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)
 
54
 
 
55
    # Add gpfs source and update apt
 
56
    fetch.add_source(gpfs_source)
 
57
    try:
 
58
        fetch.apt_update(fatal=True)
 
59
        # Install gpfs packages
 
60
        fetch.apt_install("gpfs*", options=["--allow-unauthenticated"],
 
61
                          fatal=True)
 
62
    except CalledProcessError:
 
63
        #TODO: verify source was configured correctly
 
64
        raise
 
65
 
 
66
 
 
67
def gpfs_install_updates(url, version, update=True):
 
68
    # install update packages (patches), if given
 
69
    if update:
 
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"],
 
76
                          fatal=True)
 
77
 
 
78
 
27
79
## ssh key methods ##
28
80
def create_ssh_keys():
29
81
    # Generate ssh keys if needed
33
85
        # Ensure permissions are good
34
86
        os.chmod('/root/.ssh/id_rsa.pub', 0600)
35
87
        os.chmod('/root/.ssh/id_rsa', 0600)
36
 
   
 
88
 
 
89
 
37
90
def get_ssh_keys():
38
91
    # get public and private ssh keys
39
92
    with open("/root/.ssh/id_rsa.pub", "r") as idfile:
47
100
        idfile.close()
48
101
    return [privkey, pubkey]
49
102
 
 
103
 
50
104
def set_ssh_key(key, private=False):
51
105
    if key:
52
 
        if private == False:
 
106
        if private is False:
53
107
            f = open("/root/.ssh/id_rsa.pub", "w")
54
108
            f.write(key)
55
109
            f.close()
64
118
            f.close()
65
119
            os.chmod('/root/.ssh/id_rsa', 0600)
66
120
 
 
121
 
67
122
def configure_ssh():
68
123
    # Configure sshd_config file to allow root
69
124
    sshconf = open("/etc/ssh/sshd_config", 'r')
90
145
        idfile.write("StrictHostKeyChecking no\n")
91
146
        idfile.close()
92
147
 
 
148
 
 
149
## Other utility methods##
 
150
def validate_quorum(quorum):
 
151
    valid_values = ['', 'quorum', 'non-quorum']
 
152
    return quorum if quorum in valid_values else ''
 
153
 
 
154
 
93
155
## cluster management methods ##
 
156
def build_modules():
 
157
# build binary gpfs modules after installation
 
158
    try:
 
159
        check_call(["mmbuildgpl"])
 
160
    except CalledProcessError:
 
161
        log('mmbuildgpl was not executed', level=hookenv.WARNING)
 
162
    except OSError:
 
163
        log('mmbuildgpl not found/installed')
 
164
 
94
165
 
95
166
def start():
96
167
    # Only manager can start/stop cluster
97
168
    if is_manager():
98
169
        log(check_output(split('mmstartup -a')))
99
170
 
 
171
 
100
172
def stop():
101
173
    # stop the server
102
174
    if is_manager():
103
175
        check_output(split('mmshutdown -a'))
104
176
 
 
177
 
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
110
183
 
 
184
 
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))))
116
190
    else:
117
191
        log('Node %s could not be added. Manager not defined' % nodename)
118
 
  
 
192
 
 
193
 
119
194
def apply_license(nodename):
120
195
    check_output(split('mmchlicense server --accept -N %s' % nodename))
121
196
 
 
197
 
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()
 
201
 
125
202
 
126
203
def is_manager():
127
204
    # check if the node is a manager
128
205
    if os.path.exists("/var/mmfs/.manager"):
129
206
        return True
130
207
 
 
208
 
131
209
def cluster_exists():
132
210
    # Check if cluster is already defined
133
211
    return True if call('mmlscluster') == 0 else False
134
 
       
 
212
 
 
213
 
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')))
 
219
 
 
220
 
 
221
def chk_dns(hostname, ip):
 
222
    try:
 
223
        socket.gethostbyname(hostname)
 
224
    except:
 
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))
 
228
            hostfile.close()