def install(self, **kwargs): """ Installs and configures supervisor on the remote machine. Supervisor is installed via easy_install, a supervisor.conf file is created with an entry to include additional conf files in the directory: cfg().supervisord_include_conf. An init.d script is written, and if the program "chkconfig" exists, supervisor is added. :return: None """ start_msg('----- Install "supervisord" via "easy_install".') result = sudo("easy_install supervisor") if result.return_code != 0: HaltError('Failed to install "supervisord".') result = run("which supervisord") if result.return_code != 0: raise HaltError('Confusion: just installed "supervisord" but its not there?') message("Install successful; setting configuration.") # create the root supervisord.conf with an [include] entry that allows loading additional # from the folder: /etc/supervisor/conf.d/*.conf # we use this location for site-specific config (e.g., a site's [program] section). # start with the default conf file; the grep strips comment lines. result = run('echo_supervisord_conf | grep "^;.*$" -v') if result.failed: raise HaltError("Unable to retrieve default supervisord configuration.") # build the new configuration by just appending the include definition, # then write it to: /etc/supervisord.conf files = path.join(cfg().supervisord_include_conf, "*.conf") new_conf = "{result}\n\n[include]\nfiles = {files}\n".format(**locals()) put_string(new_conf, "/etc/supervisord.conf", use_sudo=True) # make sure the directory exists. result = sudo("mkdir -p {0}".format(cfg().supervisord_include_conf)) if result.failed: raise HaltError('Unable to create include dir: "{0}"'.format(cfg().supervisord_include_conf)) # finally write an init-script to /etc/init.d so supervisord gets run at startup. # TODO: write system-dependent script using "uname -s": OSX=Darwin, Amazon Linux AMI=Linux, ?? put_string(_INIT_SCRIPT_LINUX, "/etc/init.d/supervisor", use_sudo=True, mode=00755) # the Amazon Linux AMI uses chkconfig; the init.d script won't do the job by itself. # set supervisord so it can be managed by chkconfig; and turn on boot startup. # ubuntu (and Debian?) use UpStart or update-rc.d, so check them out. result = run("which chkconfig") if result.succeeded: message("System has chkconfig; configuring.") result = sudo("chkconfig --add supervisor") if result.failed: raise HaltError('"chkconfig --add supervisor" failed.') result = sudo("chkconfig supervisor on") if result.failed: raise HaltError('"chkconfig supervisor on" failed.') succeed_msg('"supervisord" is installed ({0}).'.format(result)) return self
def write_config(self, name, server_names, proxy_pass, static_locations='', log_root=None, listen=80): """ Writes an Nginx server configuration file. This function writes a specific style of configuration, that seems to be somewhat common, where Nginx is used as a reverse-proxy for a locally-running (e.g., WSGI) server. :param name: identifies the server name; used to name the configuration file. :param server_names: :param proxy_pass: identifies the local proxy to which Nginx will pass requests. """ start_msg('----- Writing Nginx server configuration for "{0}":'.format(name)) # be sure the log directory exists. if log_root is None: log_root = path.join(cfg().deploy_root, name, 'logs') result = sudo('mkdir -p {0}'.format(log_root)) if result.failed: raise HaltError('Unable to create log directory: "{0}"'.format(log_root)) # generate and write the configuration file. server_config = _NGINX_SERVER_CONF.format(**locals()) dest = path.join(cfg().nginx_include_conf, '{name}.conf'.format(**locals())) message('Writing to file: "{0}"'.format(dest)) put_string(server_config, dest, use_sudo=True) succeed_msg('Wrote conf file for "{0}".'.format(name)) return self
def install(self, **kwargs): # install Nginx using the package manager. self._simple.install() start_msg('----- Configuring "Nginx":') # verify that there's an init-script. result = run('test -f /etc/init.d/nginx') if result.failed: raise HaltError('Uh oh. Package manager did not install an Nginx init-script.') # write nginx.conf file. dest = path.join(cfg().nginx_conf, 'nginx.conf') message('Writing "nginx.conf"') put_string(_NGINX_CONF, dest, use_sudo=True) # the Amazon Linux AMI uses chkconfig; the init.d script won't do the job by itself. # set Nginx so it can be managed by chkconfig; and turn on boot startup. result = run('which chkconfig') if result.succeeded: message('System has chkconfig; configuring.') result = sudo('chkconfig --add nginx') if result.failed: raise HaltError('"chkconfig --add nginx" failed.') result = sudo('chkconfig nginx on') if result.failed: raise HaltError('"chkconfig nginx on" failed.') succeed_msg('Successfully installed and configured "Nginx".') return self
def write_config(self, name, cmd, dir=None, log_root=None, env=None): """ Writes a supervisor [program] entry to a "conf" file. The conf file is named "<name>.conf", and is located in the directory identified by: cfg().supervisord_include_conf Calling this function is typically followed soon after by a call to reload_config(). :param name: specifies the program name. :param cmd: specifies the command to start the program. :param dir: specifies the directory to chdir to before executing command. default: no chdir. :param log_root: specifies the location for supervisor log file. default is 'logs' in the deployment root directory. :param env: specifies the child process environment. default: None. """ start_msg('----- Writing supervisor conf file for "{0}":'.format(name)) if dir is None: dir = "" if env is None: env = "" if not log_root: log_root = path.join(cfg().deploy_root, "logs") # first be sure the log directory exists. if not supervisor will fail to load the config. result = sudo("mkdir -p {0}".format(log_root)) if result.failed: raise HaltError('Unable to create log directory: "{0}"'.format(log_root)) # now write the entry. entry = ( "[program:{name}]\n" "command={cmd}\n" "directory={dir}\n" "user=nobody\n" "autostart=true\n" "autorestart=true\n" "stdout_logfile={log_root}/{name}.log\n" "redirect_stderr=True\n" "environment={env}\n".format(**locals()) ) dest = path.join(cfg().supervisord_include_conf, "{name}.conf".format(**locals())) message('Writing to file: "{0}"'.format(dest)) put_string(entry, dest, use_sudo=True) succeed_msg('Wrote conf file for "{0}".'.format(name)) return self