def _upload(owner, upload_dir, revision): tmp_tar = git.create_archive(revision) try: with ctx.cd(upload_dir): with ctx.sudo(): put(tmp_tar, 'deploy.tar.gz') file.attributes('deploy.tar.gz', owner=owner) with ctx.sudo(owner): run('tar -xzf deploy.tar.gz') file.remove('deploy.tar.gz') file.write('VERSION', git.revparse(revision)) finally: local('rm -rf %s' % tmp_tar)
def site_enable(site): site_available = '/etc/nginx/sites-available/%s' % site site_enabled = '/etc/nginx/sites-enabled/%s' % site with ctx.sudo(): file.link(site_available, site_enabled) reload()
def _create_new_release_dir(owner, base_dir): date_dir = datetime.utcnow().strftime('%Y%m%d%H%M%S') release_dir = os.path.join(base_dir, date_dir) with ctx.sudo(owner): dir.create(release_dir) return release_dir
def test_sudo_restores_previous_settings(): old_user = env.sudo_user old_forced = env.sudo_forced with ctx.sudo("foo"): pass assert env.sudo_forced == old_forced assert env.sudo_user == old_user
def add_upstart(name, content): if name.endswith(".conf"): name = name[:-5] upstart_file = "/etc/init/%s.conf" % name with ctx.sudo(): with ctx.unpatched_state(): file.write(upstart_file, content)
def _layout(self): current_user = core.run("echo $USER").stdout for type, path in self.folders.iteritems(): if not dir.exists(path): if type != "current": dir.create(path, recursive=True) else: with ctx.sudo(): dir.attributes(path, owner=current_user, recursive=True)
def _store_root_cnf(password): cnf_config = """# Config generated by revolver [client] host = localhost user = root password = %s """ % password cnf_dir = "/etc/mysql/" cnf_file = cnf_dir + "root.cnf" with ctx.sudo(): file.write(cnf_file, cnf_config)
def _preseed_server(root_password): seed_config = """# Mysql preseed generated by revolver mysql-server mysql-server/root_password password %(root_password)s mysql-server mysql-server/root_password_again password %(root_password)s """ % {"root_password": root_password} seed_dir = "/var/cache/local/preseeding/" seed_file = seed_dir + "mysql-server.seed" with ctx.sudo(): dir.create(seed_dir, recursive=True) file.write(seed_file, seed_config) sudo("debconf-set-selections %s" % seed_file)
def _store_root_cnf(password): cnf_config = """# Config generated by revolver [client] host = localhost user = root password = %s """ % password cnf_dir = "/etc/mysql/" cnf_file = cnf_dir + "root.cnf" if file.exists(cnf_file): return with ctx.sudo(): file.write(cnf_file, cnf_config) file.link(cnf_file, "/root/.my.cnf")
def _preseed_server(root_password): seed_config = """# Mysql preseed generated by revolver mysql-server mysql-server/root_password password %(root_password)s mysql-server mysql-server/root_password_again password %(root_password)s mysql-server mysql-server/start_on_boot boolean true """ % { "root_password": root_password } seed_dir = "/var/cache/local/preseeding/" seed_file = seed_dir + "mysql-server.seed" if file.exists(seed_file): return with ctx.sudo(): dir.create(seed_dir, recursive=True) file.write(seed_file, seed_config) sudo("debconf-set-selections %s" % seed_file)
def install(): already_installed = package.is_installed('nginx') if server.version == '10.04': package.install_ppa('nginx/stable') package.install('nginx') if not already_installed: site_disable('default') www_dir = '/var/www' www_owner = 'www-data' if not dir.exists(www_dir): with ctx.sudo(): dir.create(www_dir) dir.attributes(www_dir, owner=www_owner, group=www_owner) restart()
def install(name=_DEFAULT_NAME, subnet=_DEFAULT_SUBNET, _update=True): packages = ["lxc", "debootstrap", "libvirt-bin"] if _update: package.install(packages) else: package.ensure(packages) networks = _list_networks() if name not in networks: _create_network(name, subnet) core.run("virsh net-start %s" % name) else: if not networks[name]: core.run("virsh net-start %s" % name) with ctx.sudo(): config = _LXC_NETWORK % dict(name=name, subnet=subnet) file.write("/etc/lxc/net-lxc.conf", config, mode="a+r")
def deploy(owner, upload_hook=None, revision='HEAD', keep_versions=10): if not user.exists(owner): log.abort('Specified owner does not exists! Deploy aborted') # Ensure some directories paths = _ensure_layout(owner) new_release_dir = _create_new_release_dir(owner, paths['releases']) paths['new_release'] = new_release_dir # Upload the new version and call the after upload hook _upload(owner, new_release_dir, revision) if upload_hook: with ctx.sudo(owner), ctx.cd(new_release_dir): upload_hook(owner, paths) # Activate the new release and _symlink_release(owner, paths['current'], new_release_dir) _clear_old_releases(paths['releases'], keep_versions) return paths
def _ensure_layout(owner): home_dir = user.home_directory(owner) repo_name = git.repository_name() join = os.path.join project_dir = join(home_dir, repo_name) paths = { 'project': join(project_dir), 'current': join(project_dir, 'current'), 'releases': join(project_dir, 'releases'), 'shared': join(project_dir, 'shared'), 'logs': join(project_dir, 'shared', 'logs'), 'temp': join(project_dir, 'shared', 'temp') } with ctx.sudo(owner): for path in paths.itervalues(): if dir.exists(path): continue dir.create(path, recursive=True) return paths
def test_sudo_default_login(): old_shell = env.shell with ctx.sudo(): assert env.shell == old_shell
def test_sudo_with_user_change_sudo_env_user(): with ctx.sudo("foo"): assert env.sudo_user == "foo"
def test_sudo_with_interactive_login(shell): shell.expects_call().with_args("foo").returns("bar") with ctx.sudo("foo", login=True): assert env.shell == "-i bar -i -c"
def wrapper(*args, **kwargs): with ctx.sudo(): func(*args, **kwargs)
def site_ensure(site, lines): with ctx.sudo(): with ctx.cd('/etc/nginx/sites-available/'): file.write(site, lines) site_enable(site)
def test_sudo_changes_env_flag(): with ctx.sudo(): assert env.sudo_forced
def test_inject_use_sudo_with_forced_sudo(): with ctx.sudo(): assert decorator.inject_use_sudo(_use_sudo_dummy)()
def test_sudo_without_user_does_not_change_sudo_env_user(): old_user = env.sudo_user with ctx.sudo(): assert env.sudo_user == old_user
def timezone(zone='UTC'): with ctx.sudo(): from_file = '/usr/share/zoneinfo/%s' % zone to_file = '/etc/localtime' file.copy(from_file, to_file)
def _symlink_release(owner, current_dir, release_dir): with ctx.sudo(owner): if dir.exists(current_dir): dir.remove(current_dir, recursive=True) file.link(release_dir, current_dir)
def ensure(lines): with ctx.sudo(): file.update('/etc/sudoers', lambda _: text.ensure_line(_, *lines))
def timezone(zone='UTC'): from_file = '/usr/share/zoneinfo/%s' % zone to_file = '/etc/localtime' with ctx.sudo(): file.copy(from_file, to_file)
def site_disable(site): with ctx.sudo(): with ctx.cd('/etc/nginx/sites-enabled'): file.remove(site) reload()
def install(): package.install('stunnel') with ctx.sudo(): file.sed('/etc/default/stunnel4', 'ENABLED=0', 'ENABLED=1')