~kwmonroe/charms/trusty/xcat/deprecate

« back to all changes in this revision

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

  • Committer: Adalberto Medeiros
  • Date: 2015-03-09 15:39:26 UTC
  • mfrom: (22.2.8 gpfs-config)
  • Revision ID: adalbas@linux.vnet.ibm.com-20150309153926-3p3ugnjtw4h1xyn1
Add config-change hook; pep8 fixes and install lib updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import tempfile
8
8
import socket
9
9
from shlex import split
 
10
import subprocess
10
11
from subprocess import (
11
12
    call,
12
13
    check_call,
13
 
    check_output
 
14
    check_output,
 
15
    Popen,
 
16
    CalledProcessError
14
17
)
15
18
 
16
19
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
17
20
 
 
21
from charmhelpers import fetch
18
22
from charmhelpers.core import (
19
23
    hookenv,
20
24
)
21
25
log = hookenv.log
22
26
 
 
27
GPFS_PATH = '/Linux/x86_64'
 
28
 
 
29
 
23
30
def add_to_path(p, new):
24
31
    return p if new in p.split(':') else p + ':' + new
25
32
 
26
33
os.environ['PATH'] = add_to_path(os.environ['PATH'], '/usr/lpp/mmfs/bin')
27
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
 
28
79
## ssh key methods ##
29
80
def create_ssh_keys():
30
81
    # Generate ssh keys if needed
34
85
        # Ensure permissions are good
35
86
        os.chmod('/root/.ssh/id_rsa.pub', 0600)
36
87
        os.chmod('/root/.ssh/id_rsa', 0600)
37
 
   
 
88
 
 
89
 
38
90
def get_ssh_keys():
39
91
    # get public and private ssh keys
40
92
    with open("/root/.ssh/id_rsa.pub", "r") as idfile:
48
100
        idfile.close()
49
101
    return [privkey, pubkey]
50
102
 
 
103
 
51
104
def set_ssh_key(key, private=False):
52
105
    if key:
53
 
        if private == False:
 
106
        if private is False:
54
107
            f = open("/root/.ssh/id_rsa.pub", "w")
55
108
            f.write(key)
56
109
            f.close()
65
118
            f.close()
66
119
            os.chmod('/root/.ssh/id_rsa', 0600)
67
120
 
 
121
 
68
122
def configure_ssh():
69
123
    # Configure sshd_config file to allow root
70
124
    sshconf = open("/etc/ssh/sshd_config", 'r')
91
145
        idfile.write("StrictHostKeyChecking no\n")
92
146
        idfile.close()
93
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
 
94
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
 
95
165
 
96
166
def start():
97
167
    # Only manager can start/stop cluster
98
168
    if is_manager():
99
169
        log(check_output(split('mmstartup -a')))
100
170
 
 
171
 
101
172
def stop():
102
173
    # stop the server
103
174
    if is_manager():
104
175
        check_output(split('mmshutdown -a'))
105
176
 
 
177
 
106
178
def node_exists(nodename):
107
179
    # Check if node has already been added to cluster
108
180
    lscluster = check_output('mmlscluster')
109
181
    node = re.search('^.*\d+.*%s.*\n' % nodename, lscluster, re.M)
110
182
    return False if node is None else True
111
183
 
 
184
 
112
185
def add_node(nodename, m_designation='client', q_designation='nonquorum'):
113
186
    # add new node to the cluster
114
187
    if cluster_exists() and not node_exists(nodename):
115
 
        log(check_output(split('mmaddnode -N %s:%s-%s' % \
116
 
                    (nodename, m_designation, q_designation))))
 
188
        log(check_output(split('mmaddnode -N %s:%s-%s' %
 
189
                         (nodename, m_designation, q_designation))))
117
190
    else:
118
191
        log('Node %s could not be added. Manager not defined' % nodename)
119
 
  
 
192
 
 
193
 
120
194
def apply_license(nodename):
121
195
    check_output(split('mmchlicense server --accept -N %s' % nodename))
122
196
 
 
197
 
123
198
def set_manager_file():
124
199
    # define a file that indicates the node is a manager
125
 
    open("/var/mmfs/.manager","a").close()
 
200
    open("/var/mmfs/.manager", "a").close()
 
201
 
126
202
 
127
203
def is_manager():
128
204
    # check if the node is a manager
129
205
    if os.path.exists("/var/mmfs/.manager"):
130
206
        return True
131
207
 
 
208
 
132
209
def cluster_exists():
133
210
    # Check if cluster is already defined
134
211
    return True if call('mmlscluster') == 0 else False
135
 
       
 
212
 
 
213
 
136
214
def create_cluster(hostname):
137
215
    # create the cluster for the hostname, manager only
138
216
    if is_manager() and not cluster_exists():
139
 
        log(check_output(split('mmcrcluster -N %s:quorum-manager' % \
140
 
                    hostname + ' -r /usr/bin/ssh -R /usr/bin/scp')))
 
217
        log(check_output(split('mmcrcluster -N %s:quorum-manager' %
 
218
                         hostname + ' -r /usr/bin/ssh -R /usr/bin/scp')))
 
219
 
141
220
 
142
221
def chk_dns(hostname, ip):
143
222
    try:
145
224
    except:
146
225
        log("Hostname not resolving, adding to /etc/hosts")
147
226
        with open("/etc/hosts", "a") as hostfile:
148
 
            hostfile.write("%s %s\n" % (ip, hostname) )
 
227
            hostfile.write("%s %s\n" % (ip, hostname))
149
228
            hostfile.close()
150