~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

Viewing changes to hooks/pg_dir_utils.py

  • Committer: bbaqar at plumgrid
  • Date: 2015-05-19 21:05:20 UTC
  • Revision ID: bbaqar@plumgrid.com-20150519210520-8nymqswwgdu7qygg
PLUMgrid director initial charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
 
2
from copy import deepcopy
 
3
from charmhelpers.core.hookenv import log
 
4
from charmhelpers.contrib.openstack import templating
 
5
from collections import OrderedDict
 
6
from charmhelpers.contrib.openstack.utils import (
 
7
    os_release,
 
8
)
 
9
import pg_dir_context
 
10
import subprocess
 
11
import time
 
12
 
 
13
#Dont need these right now
 
14
NOVA_CONF_DIR = "/etc/nova"
 
15
NEUTRON_CONF_DIR = "/etc/neutron"
 
16
NEUTRON_CONF = '%s/neutron.conf' % NEUTRON_CONF_DIR
 
17
NEUTRON_DEFAULT = '/etc/default/neutron-server'
 
18
 
 
19
#Puppet Files
 
20
P_PGKA_CONF = '/opt/pg/etc/puppet/modules/sal/templates/keepalived.conf.erb'
 
21
P_PG_CONF = '/opt/pg/etc/puppet/modules/plumgrid/templates/plumgrid.conf.erb'
 
22
P_PGDEF_CONF = '/opt/pg/etc/puppet/modules/sal/templates/default.conf.erb'
 
23
 
 
24
#Plumgrid Files
 
25
PGKA_CONF = '/var/lib/libvirt/filesystems/plumgrid/etc/keepalived/keepalived.conf'
 
26
PG_CONF = '/var/lib/libvirt/filesystems/plumgrid/opt/pg/etc/plumgrid.conf'
 
27
PGDEF_CONF = '/var/lib/libvirt/filesystems/plumgrid/opt/pg/sal/nginx/conf.d/default.conf'
 
28
PGHN_CONF = '/var/lib/libvirt/filesystems/plumgrid-data/conf/etc/hostname'
 
29
PGHS_CONF = '/var/lib/libvirt/filesystems/plumgrid-data/conf/etc/hosts'
 
30
PGIFCS_CONF = '/var/lib/libvirt/filesystems/plumgrid-data/conf/pg/ifcs.conf'
 
31
IFCTL_CONF = '/var/run/plumgrid/lxc/ifc_list_gateway'
 
32
IFCTL_P_CONF = '/var/lib/libvirt/filesystems/plumgrid/var/run/plumgrid/lxc/ifc_list_gateway'
 
33
 
 
34
BASE_RESOURCE_MAP = OrderedDict([
 
35
    (PGKA_CONF, {
 
36
        'services': ['plumgrid'],
 
37
        'contexts': [pg_dir_context.PGDirContext()],
 
38
    }),
 
39
    (PG_CONF, {
 
40
        'services': ['plumgrid'],
 
41
        'contexts': [pg_dir_context.PGDirContext()],
 
42
    }),
 
43
    (PGDEF_CONF, {
 
44
        'services': ['plumgrid'],
 
45
        'contexts': [pg_dir_context.PGDirContext()],
 
46
    }),
 
47
    (PGHN_CONF, {
 
48
        'services': ['plumgrid'],
 
49
        'contexts': [pg_dir_context.PGDirContext()],
 
50
    }),
 
51
    (PGHS_CONF, {
 
52
        'services': ['plumgrid'],
 
53
        'contexts': [pg_dir_context.PGDirContext()],
 
54
    }),
 
55
    (PGIFCS_CONF, {
 
56
        'services': [],
 
57
        'contexts': [pg_dir_context.PGDirContext()],
 
58
    }),
 
59
])
 
60
TEMPLATES = 'templates/'
 
61
 
 
62
 
 
63
def determine_packages():
 
64
    return neutron_plugin_attribute('plumgrid', 'packages', 'neutron')
 
65
 
 
66
 
 
67
def register_configs(release=None):
 
68
    release = release or os_release('neutron-common', base='icehouse')
 
69
    configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
 
70
                                          openstack_release=release)
 
71
    for cfg, rscs in resource_map().iteritems():
 
72
        configs.register(cfg, rscs['contexts'])
 
73
    return configs
 
74
 
 
75
 
 
76
def resource_map():
 
77
    '''
 
78
    Dynamically generate a map of resources that will be managed for a single
 
79
    hook execution.
 
80
    '''
 
81
    resource_map = deepcopy(BASE_RESOURCE_MAP)
 
82
    return resource_map
 
83
 
 
84
 
 
85
def restart_map():
 
86
    '''
 
87
    Constructs a restart map based on charm config settings and relation
 
88
    state.
 
89
    '''
 
90
    return {k: v['services'] for k, v in resource_map().iteritems()}
 
91
 
 
92
 
 
93
def ensure_files():
 
94
    _exec_cmd(cmd=['cp', '--remove-destination', '-f', P_PGKA_CONF, PGKA_CONF])
 
95
    _exec_cmd(cmd=['cp', '--remove-destination', '-f', P_PG_CONF, PG_CONF])
 
96
    _exec_cmd(cmd=['cp', '--remove-destination', '-f', P_PGDEF_CONF, PGDEF_CONF])
 
97
    _exec_cmd(cmd=['touch', PGIFCS_CONF])
 
98
    _exec_cmd(cmd=['mkdir', '/etc/nova'])
 
99
    _exec_cmd(cmd=['touch', 'neutron_plugin.conf'])
 
100
 
 
101
 
 
102
def restart_pg():
 
103
    _exec_cmd(cmd=['virsh', '-c', 'lxc:', 'destroy', 'plumgrid'], error_msg='ERROR Destroying PLUMgrid')
 
104
    _exec_cmd(cmd=['rm', IFCTL_CONF, IFCTL_P_CONF], error_msg='ERROR Removing ifc_ctl_gateway file')
 
105
    _exec_cmd(cmd=['iptables', '-F'])
 
106
    _exec_cmd(cmd=['virsh', '-c', 'lxc:', 'start', 'plumgrid'], error_msg='ERROR Starting PLUMgrid')
 
107
    time.sleep(5)
 
108
    _exec_cmd(cmd=['service', 'plumgrid', 'start'], error_msg='ERROR starting PLUMgrid service')
 
109
    time.sleep(5)
 
110
 
 
111
 
 
112
def stop_pg():
 
113
    _exec_cmd(cmd=['virsh', '-c', 'lxc:', 'destroy', 'plumgrid'], error_msg='ERROR Destroying PLUMgrid')
 
114
    time.sleep(2)
 
115
    _exec_cmd(cmd=['rm', IFCTL_CONF, IFCTL_P_CONF], error_msg='ERROR Removing ifc_ctl_gateway file')
 
116
 
 
117
 
 
118
def _exec_cmd(cmd=None, error_msg='Command exited with ERRORs'):
 
119
    if cmd is None:
 
120
        log("NO command")
 
121
    else:
 
122
        try:
 
123
            subprocess.check_call(cmd)
 
124
        except subprocess.CalledProcessError, e:
 
125
            log(error_msg)