def install_ntpmon(): """ Install package dependencies, source files, and startup configuration. """ install_dir = layer.options.get('ntpmon', 'install-dir') service_name = layer.options.get('ntpmon', 'service-name') using_systemd = host.init_is_systemd() if install_dir: log('installing ntpmon') host.mkdir(os.path.dirname(install_dir)) host.rsync('src/', '{}/'.format(install_dir)) if service_name: if using_systemd: systemd_config = '/etc/systemd/system/' + service_name + '.service' log('installing systemd service: {}'.format(service_name)) with open(systemd_config, 'w') as conffile: conffile.write(templating.render('src/' + service_name + '.systemd', layer.options.get('ntpmon'))) subprocess.call(['systemd', 'daemon-reload']) else: upstart_config = '/etc/init/' + service_name + '.conf' log('installing upstart service: {}'.format(service_name)) with open(upstart_config, 'w') as conffile: conffile.write(templating.render('src/' + service_name + '.upstart', layer.options.get('ntpmon'))) set_flag('ntpmon.installed') clear_flag('ntpmon.configured')
def install_ntpmon(): """ Install package dependencies, source files, and startup configuration. """ install_dir = layer.options.get('ntpmon', 'install-dir') service_name = layer.options.get('ntpmon', 'service-name') using_systemd = host.init_is_systemd() if install_dir: log('installing ntpmon') host.mkdir(os.path.dirname(install_dir)) host.rsync('src/', '{}/'.format(install_dir)) if service_name: if using_systemd: systemd_config = '/etc/systemd/system/' + service_name + '.service' log('installing systemd service: {}'.format(service_name)) with open(systemd_config, 'w') as conffile: conffile.write( templating.render('src/' + service_name + '.systemd', layer.options.get('ntpmon'))) subprocess.call(['systemd', 'daemon-reload']) else: upstart_config = '/etc/init/' + service_name + '.conf' log('installing upstart service: {}'.format(service_name)) with open(upstart_config, 'w') as conffile: conffile.write( templating.render('src/' + service_name + '.upstart', layer.options.get('ntpmon'))) set_flag('ntpmon.installed') clear_flag('ntpmon.configured')
def test_render_loop_template(self): name = "loop" self._write_template_to_file(name, LOOP_TEMPLATE) expected = "12345" result = render(name, {"somevar": ["1", "2", "3", "4", "5"]}, template_dir=self.templates_dir) self.assertEqual(expected, result)
def test_render_simple_template(self): name = "simple" self._write_template_to_file(name, SIMPLE_TEMPLATE) expected = "hello" result = render(name, {"somevar": expected}, template_dir=self.templates_dir) self.assertEqual(expected, result)
def write_config(): use_iburst = hookenv.config('use_iburst') source = hookenv.config('source') remote_sources = get_sources(source, iburst=use_iburst) for relid in hookenv.relation_ids('master'): for unit in hookenv.related_units(relid=relid): u_addr = hookenv.relation_get(attribute='private-address', unit=unit, rid=relid) remote_sources.append({'name': u_addr, 'iburst': 'iburst'}) peers = hookenv.config('peers') remote_peers = get_sources(peers, iburst=use_iburst) auto_peers = hookenv.config('auto_peers') if hookenv.relation_ids('ntp-peers') and auto_peers: remote_peers = get_sources(get_peer_nodes(), iburst=use_iburst, source_list=remote_peers) if len(remote_sources) == 0 and len(remote_peers) == 0: # we have no peers/servers; restore default ntp.conf provided by OS shutil.copy(NTP_CONF_ORIG, NTP_CONF) else: # otherwise, write our own configuration with open(NTP_CONF, "w") as ntpconf: ntpconf.write( render(os.path.basename(NTP_CONF), { 'servers': remote_sources, 'peers': remote_peers, })) if hookenv.relation_ids('nrpe-external-master'): update_nrpe_config()
def install_maintenance_crontab(): # Every unit should run repair once per week (at least once per # GCGraceSeconds, which defaults to 10 days but can be changed per # keyspace). # Distribute the repair time evenly over the week. unit_num = int(hookenv.local_unit().split('/')[-1]) dow, hour, minute = helpers.week_spread(unit_num) contents = jinja.render('cassandra_maintenance_cron.tmpl', vars()) cron_path = "/etc/cron.d/cassandra-maintenance" host.write_file(cron_path, contents.encode('US-ASCII'))
def install_maintenance_crontab(): # Every unit should run repair once per week (at least once per # GCGraceSeconds, which defaults to 10 days but can be changed per # keyspace). # Distribute the repair time evenly over the week. unit_num = int(hookenv.local_unit().split('/')[-1]) dow, hour, minute = helpers.week_spread(unit_num) contents = jinja.render('cassandra_maintenance_cron.tmpl', vars()) cron_path = "/etc/cron.d/cassandra-maintenance" host.write_file(cron_path, contents.encode('US-ASCII'))
def test_custom_delimiters(self): name = "custom_delimiters" template_var = "foo" jinja_env_args = { "variable_start_string": "<<", "variable_end_string": ">>" } expected = "{{ not_var }} %s" % template_var self._write_template_to_file(name, CUSTOM_DELIM_TEMPLATE) result = render(name, {'template_var': template_var}, template_dir=self.templates_dir, jinja_env_args=jinja_env_args) self.assertEqual(expected, result)
def render_grafana_dashboard(datasource): """Load grafana dashboard json model and insert prometheus datasource. :param datasource: name of the 'prometheus' application that will be used as datasource in grafana dashboard :return: Grafana dashboard json model as a dict. """ datasource = "{} - Juju generated source".format(datasource) jinja_args = {"variable_start_string": "<<", "variable_end_string": ">>"} return json.loads( render( GRAFANA_DASHBOARD_FILE, {"datasource": datasource}, jinja_env_args=jinja_args, ))
def set_config(config): with open(config_file(), "w") as conffile: conffile.write(templating.render(_config_file_template(), config))