Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_file_upload_op(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
with patch('pyinfra.modules.files.path.isfile', lambda *args, **kwargs: True):
# Test normal
add_op(
state, files.put,
{'First op name'},
'files/file.txt',
'/home/vagrant/file.txt',
)
# And with sudo
add_op(
state, files.put,
'files/file.txt',
'/home/vagrant/file.txt',
sudo=True,
sudo_user='pyinfra',
)
# And with su
add_op(
Upstart jobs define runlevels in their config files - as such there is no way to
edit/list these without fiddling with the config. So pyinfra simply manages the
existence of a ``/etc/init/.override`` file, and sets its content to
"manual" to disable automatic start of services.
'''
yield _handle_service_control(
name, host.fact.upstart_status,
'initctl {1} {0}',
running, restarted, reloaded, command,
)
# Upstart jobs are setup w/runlevels etc in their config files, so here we just check
# there's no override file.
if enabled is True:
yield files.file(
state, host,
'/etc/init/{0}.override'.format(name),
present=False,
)
# Set the override file to "manual" to disable automatic start
elif enabled is False:
yield 'echo "manual" > /etc/init/{0}.override'.format(name)
+ restarted: whether the service should be restarted
+ reloaded: whether the service should be reloaded
+ command: custom command to pass like: ``/etc/rc.d/ ``
+ enabled: whether this service should be enabled/disabled on boot
'''
yield _handle_service_control(
name, host.fact.rcd_status,
'/etc/rc.d/{0} {1}',
running, restarted, reloaded, command,
status_argument='check',
)
# BSD init is simple, just add/remove _enabled="YES"
if isinstance(enabled, bool):
yield files.line(
state, host,
'/etc/rc.conf.local',
'^{0}_enable='.format(name),
replace='{0}_enable="YES"'.format(name),
present=enabled,
)
from pyinfra import host
from pyinfra.modules import files, server
# Executing as the SSH user (vagrant):
#
# Generate files from local jinja2 templates
files.template(
{'Generate/upload templates/template.txt.j2'},
'templates/template.txt.j2',
'/home/vagrant/template.txt',
)
server.shell(
{'Execute some shell commands'},
[
'echo "Shell command"',
'echo "My hostname is {{ host.fact.hostname }}"',
],
)
# and scripts
server.script(
{'Run the files/test.sh script'},
'files/test.sh',
def script(state, host, filename, chdir=None):
'''
Upload and execute a local script on the remote host.
+ filename: local script filename to upload & execute
+ chdir: directory to cd into before executing the script
'''
temp_file = state.get_temp_filename(filename)
yield files.put(state, host, filename, temp_file)
yield chmod(temp_file, '+x')
if chdir:
yield 'cd {0} && {1}'.format(chdir, temp_file)
else:
yield temp_file
if filename:
filename = '/etc/apt/sources.list.d/{0}.list'.format(filename)
else:
filename = '/etc/apt/sources.list'
# Work out if the repo exists already
apt_sources = host.fact.apt_sources
is_present = False
repo = parse_apt_repo(name)
if repo and repo in apt_sources:
is_present = True
# Doesn't exist and we want it
if not is_present and present:
yield files.line(
state, host, filename, name,
)
# Exists and we don't want it
if is_present and not present:
yield files.line(
state, host, filename, name,
present=False,
)
'[{0}]'.format(name),
'name={0}'.format(description),
'baseurl={0}'.format(baseurl),
'enabled={0}'.format(1 if enabled else 0),
'gpgcheck={0}'.format(1 if gpgcheck else 0),
]
if gpgkey:
repo_lines.append('gpgkey={0}'.format(gpgkey))
repo_lines.append('')
repo = '\n'.join(repo_lines)
repo = StringIO(repo)
# Ensure this is the file on the server
yield files.put(state, host, repo, filename)