def Host(name, alias = None, address = None, use = "generic-host", groups = [], action = "create", **kwargs): env = Environment.get_instance() kwargs['name'] = name kwargs['alias'] = alias or name kwargs['address'] = address or name kwargs['use'] = use kwargs['services'] = {} kwargs['groups'] = groups if action == "delete": host = env.config.nagios3.hosts.pop(name, None) if host: for g in host.get('groups', []): env.config.nagios3.hostgroups[g]['members'].remove(name) else: env.config.nagios3.hosts[name] = kwargs for g in groups: env.config.nagios3.hostgroups[g]['members'].append(name)
def Service(service_description, host_name=None, hostgroup_name=None, check_command=None, use="generic-service", notification_interval=0, action="create"): env = Environment.get_instance() values = dict( host_name=host_name, hostgroup_name=hostgroup_name, service_description=service_description, check_command=check_command, use=use, notification_interval=notification_interval, ) if host_name: env.config.nagios3.hosts[host_name]["services"][ service_description] = values return File("/etc/nagios3/conf.d/service_%s.cfg" % service_description.lower(), content=Template("nagios3/service.cfg.j2", values), action=action, notifies=[("restart", env.resources["Service"]["nagios3"])])
def setup(name, **kwargs): env = Environment.get_instance() config = env.config.mongodb.copy() config.update(kwargs) config['configpath'] = "/etc/mongodb/%s.conf" % name if 'dbpath' not in kwargs: config['dbpath'] = os.path.join(config.dbpath, name) if 'logfilename' not in kwargs: config['logfilename'] = "%s.log" % name Directory("/etc/mongodb", owner="root", group="root", mode=0755) Directory(config.dbpath, owner="mongodb", group="mongodb", mode=0755, recursive=True) File(config.configpath, owner="root", group="root", mode=0644, content=Template("mongodb/mongodb.conf.j2", variables=dict(mongodb=config))) # notifies = [("restart", env.resources["MonitService"]["mongodb-%s" % name])]) controller = kwargs.get("controller") if controller == "monit": env.include_recipe("monit") env.cookbooks.monit.rc( "mongodb-%s" % name, Template("mongodb/monit.conf.j2", variables=dict(name=name, mongodb=config))) env.cookbooks.monit.MonitService( "mongodb-%s" % name, subscribes=[("restart", env.resources["File"][config.configpath])]) elif controller == "supervisord": env.include_recipe("supervisor") env.cookbooks.supervisor.configuration( "mongodb-%s" % name, Template("mongodb/supervisord.conf.j2", variables=dict(name=name, mongodb=config))) env.cookbooks.supervisor.SupervisorService( "mongodb-%s" % name, subscribes=[("restart", env.resources["File"][config.configpath])]) else: Service("mongodb-%s" % name, subscribes=[("restart", env.resources["File"][config.configpath])]) File("/etc/init/mongodb-%s.conf" % name, owner="root", group="root", mode=0644, content=Template("mongodb/upstart.conf.j2", variables=dict(mongodb=config)), notifies=[ ("reload", env.resources["Service"]["mongodb-%s" % name], True), ])
def setup_ebs_volume(name=None, availability_zone=None, volume_id=None, device=None, linux_device=None, snapshot_id=None, size=None, fstype=None, mount_point=None, fsoptions=None): env = Environment.get_instance() if linux_device is None: linux_device = device env.cookbooks.aws.EBSVolume(name or volume_id, volume_id = volume_id, availability_zone = availability_zone or env.config.aws.availability_zone, device = device, linux_device = linux_device, snapshot_id = snapshot_id, size = size, action = "attach" if volume_id else ["create", "attach"]) if fstype: if fstype == "xfs": Package("xfsprogs") Execute("mkfs.%(fstype)s -f %(device)s" % dict(fstype=fstype, device=linux_device), not_if = """if [ "`file -s %(device)s`" = "%(device)s: data" ]; then exit 1; fi""" % dict(device=linux_device)) if mount_point: Mount(mount_point, device = linux_device, fstype = fstype, options = fsoptions if fsoptions is not None else ["noatime"], action = ["mount", "enable"])
def Host(name, alias=None, address=None, use="generic-host", groups=[], action="create", **kwargs): env = Environment.get_instance() kwargs['name'] = name kwargs['alias'] = alias or name kwargs['address'] = address or name kwargs['use'] = use kwargs['services'] = {} kwargs['groups'] = groups if action == "delete": host = env.config.nagios3.hosts.pop(name, None) if host: for g in host.get('groups', []): env.config.nagios3.hostgroups[g]['members'].remove(name) else: env.config.nagios3.hosts[name] = kwargs for g in groups: env.config.nagios3.hostgroups[g]['members'].append(name)
def rc(name, content): env = Environment.get_instance() return File("monitrc-%s" % name, content = content, owner = "root", group = "root", mode = 0644, path = "%s/monit.d/%s" % (env.config.monit.config_path, name), notifies = [("restart", env.resources["Service"]["monit"])])
def ssh_path_for_user(user): env = Environment.get_instance() if env.system.os == "linux": if user == "root": return "/root/.ssh/" return "/home/%s/.ssh/" % user elif env.system.platform == "mac_os_x": return "/Users/%s/.ssh/" % user raise Fail("Unable to determine ssh path for user %s on os %s platform %s" % (user, env.system.os, env.system.platform))
def configuration(name, content): env = Environment.get_instance() return File("supervisor-%s" % name, content = content, owner = "root", group = "root", mode = 0644, path = os.path.join(env.config.supervisor.custom_config_path, name) + ".conf", notifies = [("reload", env.resources["Service"]["supervisor"])])
def Limit(domain, type, item, value, action="include"): env = Environment().get_instance() for i, l in enumerate(env.config.limits): if l['domain'] == domain and l['type'] == type and l['item'] == item: del env.config.limits[i] break if action == "include": env.config.limits.append(dict(domain=domain, type=type, item=item, value=value))
def ssh_path_for_user(user): env = Environment.get_instance() if env.system.os == "linux": if user == "root": return "/root/.ssh/" return "/home/%s/.ssh/" % user elif env.system.platform == "mac_os_x": return "/Users/%s/.ssh/" % user raise Fail( "Unable to determine ssh path for user %s on os %s platform %s" % (user, env.system.os, env.system.platform))
def setup(name, **kwargs): env = Environment.get_instance() config = env.config.mongodb.copy() config.update(kwargs) config["configpath"] = "/etc/mongodb/%s.conf" % name if "dbpath" not in kwargs: config["dbpath"] = os.path.join(config.dbpath, name) if "logfilename" not in kwargs: config["logfilename"] = "%s.log" % name Directory("/etc/mongodb", owner="root", group="root", mode=0755) Directory(config.dbpath, owner="mongodb", group="mongodb", mode=0755, recursive=True) File( config.configpath, owner="root", group="root", mode=0644, content=Template("mongodb/mongodb.conf.j2", variables=dict(mongodb=config)), ) # notifies = [("restart", env.resources["MonitService"]["mongodb-%s" % name])]) controller = kwargs.get("controller") if controller == "monit": env.include_recipe("monit") env.cookbooks.monit.rc( "mongodb-%s" % name, Template("mongodb/monit.conf.j2", variables=dict(name=name, mongodb=config)) ) env.cookbooks.monit.MonitService( "mongodb-%s" % name, subscribes=[("restart", env.resources["File"][config.configpath])] ) elif controller == "supervisord": env.include_recipe("supervisor") env.cookbooks.supervisor.configuration( "mongodb-%s" % name, Template("mongodb/supervisord.conf.j2", variables=dict(name=name, mongodb=config)) ) env.cookbooks.supervisor.SupervisorService( "mongodb-%s" % name, subscribes=[("restart", env.resources["File"][config.configpath])] ) else: Service("mongodb-%s" % name, subscribes=[("restart", env.resources["File"][config.configpath])]) File( "/etc/init/mongodb-%s.conf" % name, owner="root", group="root", mode=0644, content=Template("mongodb/upstart.conf.j2", variables=dict(mongodb=config)), notifies=[("reload", env.resources["Service"]["mongodb-%s" % name], True)], )
def site(name, enable=True): env = Environment.get_instance() if enable: Execute("a2ensite %s" % name, command = "/usr/sbin/a2ensite %s" % name, notifies = [("restart", env.resources["Service"]["apache2"])], not_if = lambda:os.path.exists("%s/sites-enabled/%s" % (env.config.apache.dir, name)), only_if = lambda:os.path.exists("%s/sites-available/%s" % (env.config.apache.dir, name))) else: Execute("a2dissite %s" % name, command = "/usr/sbin/a2dissite %s" % name, notifies = [("restart", env.resources["Service"]["apache2"])], only_if = lambda:os.path.exists("%s/sites-enabled/%s" % (env.config.apache.dir, name)))
def module(name, enable=True, conf=False): env = Environment.get_instance() if conf: env.cookbooks.apache2.config(name) if enable: Execute("a2enmod %s" % name, command = "/usr/sbin/a2enmod %s" % name, notifies = [("restart", env.resources["Service"]["apache2"])], not_if = lambda:os.path.exists("%s/mods-enabled/%s.load" % (env.config.apache.dir, name))) else: Execute("a2dismod %s" % name, command = "/usr/sbin/a2dismod %s" % name, notifies = [("restart", env.resources["Service"]["apache2"])], only_if = lambda:os.path.exists("%s/mods-enabled/%s.load" % (env.config.apache.dir, name)))
def site(name, enable=True): env = Environment.get_instance() if enable: cmd = 'nxensite' else: cmd = 'nxdissite' def _not_if(): e = exists("%s/sites-enabled/%s" % (env.config.nginx.dir, name)) return e if enable else not e Execute("%s %s" % (cmd, name), command = "/usr/sbin/%s %s" % (cmd, name), notifies = [("reload", env.resources["Service"]["nginx"])], not_if = _not_if)
def site(name, enable=True): env = Environment.get_instance() if enable: Execute("a2ensite %s" % name, command="/usr/sbin/a2ensite %s" % name, notifies=[("restart", env.resources["Service"]["apache2"])], not_if=lambda: os.path.exists("%s/sites-enabled/%s" % (env.config.apache.dir, name)), only_if=lambda: os.path.exists("%s/sites-available/%s" % (env.config.apache.dir, name))) else: Execute("a2dissite %s" % name, command="/usr/sbin/a2dissite %s" % name, notifies=[("restart", env.resources["Service"]["apache2"])], only_if=lambda: os.path.exists("%s/sites-enabled/%s" % (env.config.apache.dir, name)))
def module(name, enable=True, conf=False): env = Environment.get_instance() if conf: env.cookbooks.apache2.config(name) if enable: Execute("a2enmod %s" % name, command="/usr/sbin/a2enmod %s" % name, notifies=[("restart", env.resources["Service"]["apache2"])], not_if=lambda: os.path.exists("%s/mods-enabled/%s.load" % (env.config.apache.dir, name))) else: Execute("a2dismod %s" % name, command="/usr/sbin/a2dismod %s" % name, notifies=[("restart", env.resources["Service"]["apache2"])], only_if=lambda: os.path.exists("%s/mods-enabled/%s.load" % (env.config.apache.dir, name)))
def setup_ebs_volume(name=None, availability_zone=None, volume_id=None, device=None, linux_device=None, snapshot_id=None, size=None, fstype=None, mount_point=None, fsoptions=None): env = Environment.get_instance() if linux_device is None: linux_device = device env.cookbooks.aws.EBSVolume( name or volume_id, volume_id=volume_id, availability_zone=availability_zone or env.config.aws.availability_zone, device=device, linux_device=linux_device, snapshot_id=snapshot_id, size=size, action="attach" if volume_id else ["create", "attach"]) if fstype: if fstype == "xfs": Package("xfsprogs") Execute( "mkfs.%(fstype)s -f %(device)s" % dict(fstype=fstype, device=linux_device), not_if= """if [ "`file -s %(device)s`" = "%(device)s: data" ]; then exit 1; fi""" % dict(device=linux_device)) if mount_point: Mount(mount_point, device=linux_device, fstype=fstype, options=fsoptions if fsoptions is not None else ["noatime"], action=["mount", "enable"])
def Service(service_description, host_name=None, hostgroup_name=None, check_command=None, use="generic-service", notification_interval=0, action="create"): env = Environment.get_instance() values = dict( host_name = host_name, hostgroup_name = hostgroup_name, service_description = service_description, check_command = check_command, use = use, notification_interval = notification_interval, ) if host_name: env.config.nagios3.hosts[host_name]["services"][service_description] = values return File("/etc/nagios3/conf.d/service_%s.cfg" % service_description.lower(), content = Template("nagios3/service.cfg.j2", values), action = action, notifies = [("restart", env.resources["Service"]["nagios3"])])
def Contact(name, alias, service_notification_commands="notify-service-by-email", host_notification_commands="notify-host-by-email", service_notification_period="24x7", host_notification_period="24x7", service_notification_options="w,u,c,r", host_notification_options="d,r", email=None, groups=[], **kwargs): env = Environment.get_instance() for k in ('service_notification_commands', 'host_notification_commands', 'service_notification_period', 'host_notification_period', 'service_notification_options', 'host_notification_options'): kwargs[k] = locals()[k] env.config.nagios3.contacts[name] = kwargs for g in groups: env.config.nagios3.contactgroups[g].append(name)
def Contact(name, alias, service_notification_commands = "notify-service-by-email", host_notification_commands = "notify-host-by-email", service_notification_period = "24x7", host_notification_period = "24x7", service_notification_options = "w,u,c,r", host_notification_options = "d,r", email = None, groups = [], **kwargs): env = Environment.get_instance() for k in ('service_notification_commands', 'host_notification_commands', 'service_notification_period', 'host_notification_period', 'service_notification_options', 'host_notification_options'): kwargs[k] = locals()[k] env.config.nagios3.contacts[name] = kwargs for g in groups: env.config.nagios3.contactgroups[g].append(name)
def config(name): env = Environment.get_instance() File("%s/mods-available/%s.conf" % (env.config.apache.dir, name), content = Template('apache2/mods/%s.conf.j2' % name), notifies = [("restart", env.resources["Service"]["apache2"])])