~hopem/charms/trusty/ceph-mon/lp1523871

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Corey Bryant
  • Date: 2016-01-14 14:46:26 UTC
  • mfrom: (125.2.6 ceph)
  • Revision ID: corey.bryant@canonical.com-20160114144626-bgo0ptiwgts4uqjc
[chris.macnaughton, r=cholcombe] Add support for Ceph Infernalis release

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
        stopped = service_stop(service_name)
73
73
    upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
74
74
    sysv_file = os.path.join(initd_dir, service_name)
75
 
    if os.path.exists(upstart_file):
 
75
    if init_is_systemd():
 
76
        service('disable', service_name)
 
77
    elif os.path.exists(upstart_file):
76
78
        override_path = os.path.join(
77
79
            init_dir, '{}.override'.format(service_name))
78
80
        with open(override_path, 'w') as fh:
80
82
    elif os.path.exists(sysv_file):
81
83
        subprocess.check_call(["update-rc.d", service_name, "disable"])
82
84
    else:
83
 
        # XXX: Support SystemD too
84
85
        raise ValueError(
85
 
            "Unable to detect {0} as either Upstart {1} or SysV {2}".format(
 
86
            "Unable to detect {0} as SystemD, Upstart {1} or"
 
87
            " SysV {2}".format(
86
88
                service_name, upstart_file, sysv_file))
87
89
    return stopped
88
90
 
94
96
    Reenable starting again at boot. Start the service"""
95
97
    upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
96
98
    sysv_file = os.path.join(initd_dir, service_name)
97
 
    if os.path.exists(upstart_file):
 
99
    if init_is_systemd():
 
100
        service('enable', service_name)
 
101
    elif os.path.exists(upstart_file):
98
102
        override_path = os.path.join(
99
103
            init_dir, '{}.override'.format(service_name))
100
104
        if os.path.exists(override_path):
102
106
    elif os.path.exists(sysv_file):
103
107
        subprocess.check_call(["update-rc.d", service_name, "enable"])
104
108
    else:
105
 
        # XXX: Support SystemD too
106
109
        raise ValueError(
107
 
            "Unable to detect {0} as either Upstart {1} or SysV {2}".format(
 
110
            "Unable to detect {0} as SystemD, Upstart {1} or"
 
111
            " SysV {2}".format(
108
112
                service_name, upstart_file, sysv_file))
109
113
 
110
114
    started = service_running(service_name)
115
119
 
116
120
def service(action, service_name):
117
121
    """Control a system service"""
118
 
    cmd = ['service', service_name, action]
 
122
    if init_is_systemd():
 
123
        cmd = ['systemctl', action, service_name]
 
124
    else:
 
125
        cmd = ['service', service_name, action]
119
126
    return subprocess.call(cmd) == 0
120
127
 
121
128
 
122
 
def service_running(service):
 
129
def service_running(service_name):
123
130
    """Determine whether a system service is running"""
124
 
    try:
125
 
        output = subprocess.check_output(
126
 
            ['service', service, 'status'],
127
 
            stderr=subprocess.STDOUT).decode('UTF-8')
128
 
    except subprocess.CalledProcessError:
129
 
        return False
 
131
    if init_is_systemd():
 
132
        return service('is-active', service_name)
130
133
    else:
131
 
        if ("start/running" in output or "is running" in output):
132
 
            return True
133
 
        else:
 
134
        try:
 
135
            output = subprocess.check_output(
 
136
                ['service', service_name, 'status'],
 
137
                stderr=subprocess.STDOUT).decode('UTF-8')
 
138
        except subprocess.CalledProcessError:
134
139
            return False
 
140
        else:
 
141
            if ("start/running" in output or "is running" in output):
 
142
                return True
 
143
            else:
 
144
                return False
135
145
 
136
146
 
137
147
def service_available(service_name):
146
156
        return True
147
157
 
148
158
 
 
159
SYSTEMD_SYSTEM = '/run/systemd/system'
 
160
 
 
161
 
 
162
def init_is_systemd():
 
163
    return os.path.isdir(SYSTEMD_SYSTEM)
 
164
 
 
165
 
149
166
def adduser(username, password=None, shell='/bin/bash', system_user=False,
150
167
            primary_group=None, secondary_groups=None):
151
168
    """