API documentation¶
The following documentation is based on the source code of version 0.9.1 of the debuntu-tools package.
Available modules
debuntu_tools¶
The top level debuntu_tools module.
-
debuntu_tools.__version__= '0.9.1'¶ The global version number of the debuntu-tools package (a string).
-
debuntu_tools.start_interactive_shell(context)¶ Start an interactive shell in the given execution context.
Parameters: context – An execution context created by executor.contexts.Swallows return code 130 which can be caused by the operator typing Control-C followed by Control-D.
debuntu_tools.kernel_manager¶
Usage: debuntu-kernel-manager [OPTIONS] – [APT_OPTIONS]
Detect and remove old Linux kernel header, image and modules packages that can be safely removed to conserve disk space and speed up apt-get runs that install or remove kernels.
By default old packages are detected and reported on the command line but no
changes are made. To actually remove old packages you need to use the -c,
--clean or --remove option. Using the following command you can perform
a dry run that shows you what will happen without actually doing it:
$ debuntu-kernel-manager --remove -- --dry-run
The debuntu-kernel-manager program is currently in alpha status, which means a first attempt at a usable program has been published but there are no guarantees about what it actually does. You have been warned :-).
Supported options:
| Option | Description |
|---|---|
-c, --clean, --remove |
Remove Linux kernel header and/or image packages that are deemed to be safe to remove. The use of this option requires sudo access on the system in order to run the ‘apt-get remove’ command. |
-f, --force |
When more than one Linux kernel meta package is installed the -c, --clean
and --remove options will refuse to run apt-get and exit with an error
instead. Use the -f or --force option to override this sanity check. |
-p, --preserve-count=NUMBER |
Preserve the NUMBER newest versions of the kernel packages (defaults to 2). |
-r, --remote-host=ALIAS |
Detect and remove old Linux kernel header and image packages on a remote
host over SSH. The ALIAS argument gives the SSH alias that should be used
to connect to the remote host. |
-v, --verbose |
Increase verbosity (can be repeated). |
-q, --quiet |
Decrease verbosity (can be repeated). |
-h, --help |
Show this message and exit. |
-
debuntu_tools.kernel_manager.REBOOT_REQUIRED_FILE= '/var/run/reboot-required'¶ The absolute pathname of the file that exists when a system reboot is required (a string).
-
debuntu_tools.kernel_manager.REBOOT_REQUIRED_PACKAGES_FILE= '/var/run/reboot-required.pkgs'¶ The absolute pathname of a file with details about why a system reboot is required (a string).
-
debuntu_tools.kernel_manager.main()¶ Command line interface for
debuntu-kernel-manager.
-
class
debuntu_tools.kernel_manager.KernelPackageManager(**kw)¶ Python API for automated Linux kernel image package cleanup on Debian based systems.
Here’s an overview of the
KernelPackageManagerclass:When you initialize a
KernelPackageManagerobject you are required to provide values for theapt_options,context,forceandpreserve_countproperties. You can set the values of theapt_options,context,forceandpreserve_countproperties by passing keyword arguments to the class initializer.-
apt_options¶ A list of strings with command line options to pass to
apt-get.Note
The
apt_optionsproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named apt_options (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
context¶ An execution context created by
executor.contexts.Note
The
contextproperty is acustom_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named context (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
force¶ Whether to continue with removal despite warnings (a boolean, defaults to
False).Note
The
forceproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named force (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
preserve_count¶ The number of kernel packages to preserve (an integer, defaults to 2).
Note
The
preserve_countproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named preserve_count (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
dry_run¶ Trueifcleanup_commandperforms a dry run,Falseotherwise.
-
installed_packages¶ A dictionary that maps package names (strings) to
MaybeKernelPackageobjects.The value of this property is generated by parsing the output of the
dpkg --listcommand.Note
The
installed_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
installed_header_packages¶ A list of
MaybeKernelPackageobjects for the installed Linux kernel header packages.Note
The
installed_header_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
installed_kernel_packages¶ A list of
MaybeKernelPackageobjects for the installed kernel images.Note
The
installed_kernel_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
installed_modules_packages¶ A list of
MaybeKernelPackageobjects for the installed kernel modules packages.Note
The
installed_modules_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
installed_header_meta_packages¶ A list of
MaybeKernelPackageobjects with installed meta packages for kernel headers.Note
The
installed_header_meta_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
installed_image_meta_packages¶ A list of
MaybeKernelPackageobjects with installed meta packages for kernel images.Note
The
installed_image_meta_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
installed_package_groups¶ A list of sets with
MaybeKernelPackageobjects for installed header and kernel packages.Note
The
installed_package_groupsproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
active_kernel_release¶ The output of
uname --kernel-release(a string).Note
The
active_kernel_releaseproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
active_kernel_package¶ The package name for the running kernel (a string).
Note
The
active_kernel_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
reboot_required¶ TrueifREBOOT_REQUIRED_FILEexists,Falseotherwise.Note
The
reboot_requiredproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
removable_package_groups¶ A list of sets with
MaybeKernelPackageobjects considered to be removable.Candidates for removal are selected from
installed_package_groups, ignoringactive_kernel_packageand the newestpreserve_countkernel images (minus one whenactive_kernel_packagewas ignored).Note
The
removable_package_groupsproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
removable_header_packages¶ A list of
MaybeKernelPackageobjects for header packages considered to be removable.Note
The
removable_header_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
removable_kernel_packages¶ A list of
MaybeKernelPackageobjects for kernel packages considered to be removable.Note
The
removable_kernel_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
removable_modules_packages¶ A list of
MaybeKernelPackageobjects for modules packages considered to be removable.Note
The
removable_modules_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
removable_packages¶ A list of
MaybeKernelPackageobjects for kernel related packages considered to be removable.Note
The
removable_packagesproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
running_newest_kernel¶ Trueif the newest kernel is currently active,Falseotherwise.Note
The
running_newest_kernelproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
cleanup_command¶ A list of strings with the
apt-getcommand to remove old packages.Note
The
cleanup_commandproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
render_summary()¶ Render a summary of installed and removable kernel packages on the terminal.
-
cleanup_packages(**options)¶ Run
apt-getto cleanup removable kernel related packages.Parameters: options – Any keyword arguments are passed on to the execute()method of thecontextobject.Returns: Trueif a system reboot is required (to switch to the newest installed kernel image or because security updates have been installed),Falseotherwise.Raises: CleanupErrorwhen multiple Linux kernel meta packages are installed andforceisFalse.
-
-
class
debuntu_tools.kernel_manager.MaybeKernelPackage(**kw)¶ Dumb container for entries parsed from
dpkg --listoutput.Here’s an overview of the
MaybeKernelPackageclass:Superclass: PropertyManagerProperties: is_header_meta_package,is_header_package,is_image_meta_package,is_installed,is_kernel_package,is_modules_package,is_supported_package,kernel_type,name,status,tokenized_name,versionandversion_in_nameWhen you initialize a
MaybeKernelPackageobject you are required to provide values for thename,statusandversionproperties. You can set the values of thename,statusandversionproperties by passing keyword arguments to the class initializer.-
name¶ The name of the package (a string).
Note
The
nameproperty is akey_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named name (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). Once this property has been assigned a value you are not allowed to assign a new value to the property.
-
version¶ The version of the package (a
Versionobject).Note
The
versionproperty is akey_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named version (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). Once this property has been assigned a value you are not allowed to assign a new value to the property.
-
status¶ The status of the package (a string of two characters, refer to the
dpkgman pages for details).Note
The
statusproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named status (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
tokenized_name¶ The tokenized name of the package (a list of strings).
The value of this property is computed by calling
tokenize_package_name()onname.Note
The
tokenized_nameproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
is_header_meta_package¶ Trueif the package is a Linux kernel header meta package,Falseotherwise.Note
The
is_header_meta_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
is_image_meta_package¶ Trueif the package is a Linux kernel image meta package,Falseotherwise.Note
The
is_image_meta_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
is_header_package¶ Trueif the package is a specific version of the Linux kernel headers,Falseotherwise.Note
The
is_header_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
is_kernel_package¶ Trueif the package is a specific version of the Linux kernel image,Falseotherwise.Note
The
is_kernel_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
is_modules_package¶ Trueif the package contains Linux kernel modules,Falseotherwise.Note
The
is_modules_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
is_supported_package¶ Trueif the package concerns a Linux kernel image or modules or headers,Falseotherwise.Note
The
is_supported_packageproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
version_in_name¶ The version encoded in the name of the package (a string or
None).Note
The
version_in_nameproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
kernel_type¶ The kernel type encoded in the name of the package (a string or
None).Note
The
kernel_typeproperty is acached_property. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
-
debuntu_tools.kernel_manager.tokenize_package_name(package_name)¶ Split a (kernel) package name into tokens.
Parameters: package_name – The name of a package (a string). Returns: A list of strings. The
tokenize_package_name()function uses a regular expression to tokenize package names by splitting on dashes while ignoring dashes with a digit on both sides (i.e. dashes that are part of a version number). For example:>>> from debuntu_tools.kernel_manager import tokenize_package_name >>> tokenize_package_name('linux-image-4.4.0-72-generic') ['linux', 'image', '4.4.0-72', 'generic']
-
debuntu_tools.kernel_manager.is_kernel_version(token)¶ Check whether a token in a package name looks like a kernel version.
Parameters: token – The token to check (a string). Returns: Trueif the token looks like a kernel version,Falseotherwise.The
is_kernel_version()function uses a regular expression to check whether the given string looks like a kernel version of the form4.4.0-72. The string is assumed to be a package name token returned bytokenize_package_name().
-
exception
debuntu_tools.kernel_manager.CleanupError¶ Custom exception to detect known problems.
Raised by
KernelPackageManagerwhen multiple Linux kernel meta packages are installed butforceisFalse.
debuntu_tools.nodejs_installer¶
Usage: debuntu-nodejs-installer [OPTIONS]
Install an up to date Node.js binary distribution on a Debian or Ubuntu system by configuring and using the NodeSource binary package repositories.
Due to the time it takes for new software releases to find their way into the Debian and Ubuntu ecosystems versus the speed with which the Node.js community is currently moving, the system packages that provide Node.js are hopelessly out of date. Fortunately the folks at NodeSource maintain Debian and Ubuntu package repositories that provide up to date Node.js binary distributions.
NodeSource makes installation scripts available and the suggested way to run these is to download and pipe them straight to a shell. That kind of rubs me the wrong way :-) but I’ve nevertheless had to set up NodeSource installations a dozen times now. One thing led to another and now there is this program.
Supported options:
| Option | Description |
|---|---|
-i, --install |
Configure the system to use one of the NodeSource binary package repositories and install the ‘nodejs’ package from the repository. |
-V, --version=NODEJS_VERSION |
Set the version of Node.js to be installed. You can find a list of available versions on the following web page: https://github.com/nodesource/distributions/ Default: node_10.x (active LTS) |
-s, --sources-file=FILENAME |
Set the pathname of the ‘package resource list’ that will be added to the system during configuration of the NodeSource binary package repository. Default: /etc/apt/sources.list.d/nodesource.list |
-r, --remote-host=ALIAS |
Perform the requested action(s) on a remote host over SSH. The ALIAS
argument gives the SSH alias that should be used to connect to the remote
host. |
-v, --verbose |
Increase verbosity (can be repeated). |
-q, --quiet |
Decrease verbosity (can be repeated). |
-h, --help |
Show this message and exit. |
-
debuntu_tools.nodejs_installer.main()¶ Command line interface for
debuntu-nodejs-installer.
-
class
debuntu_tools.nodejs_installer.NodeInstaller(**kw)¶ Python API for the Node.js installer.
The logic in this installer is based on the manual installation instructions and setup scripts provided by NodeSource and my experience with system administration of Debian and Ubuntu systems.
The main method of this class is
install()and the behavior of this method can be configured by setting thecontext,nodejs_versionandsources_fileproperties by passing keyword arguments to the class initializer.Here’s an overview of the
NodeInstallerclass:Superclass: PropertyManagerPublic methods: install(),install_https_transport(),install_package(),install_signing_key(),install_sources_file(),update_package_lists()andvalidate_system()Properties: context,nodejs_versionandsources_fileWhen you initialize a
NodeInstallerobject you are required to provide a value for thecontextproperty. You can set the values of thecontext,nodejs_versionandsources_fileproperties by passing keyword arguments to the class initializer.-
context¶ An execution context created using
executor.contexts.Note
The
contextproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named context (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
nodejs_version¶ The Node.js version to install (a string, defaults to
node_10.x).Note
The
nodejs_versionproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
sources_file¶ The absolute pathname of the ‘package resource list’ used to enable the NodeSource repositories (a string).
Defaults to
/etc/apt/sources.list.d/nodesource.list.Note
The
sources_fileproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
install()¶ Enable the NodeSource repository and install the
nodejspackage.Raises: UnsupportedSystemErrorwhenvalidate_system()fails.
-
validate_system()¶ Make sure the system is running a supported version of Debian or Ubuntu.
Raises: UnsupportedSystemErrorwhen validation fails.
-
install_signing_key(key_url='https://deb.nodesource.com/gpgkey/nodesource.gpg.key')¶ Install the signing key for the NodeSource repositories.
-
install_https_transport()¶ Install the
apt-transport-httpssystem package.
-
install_sources_file()¶ Install a ‘package resource list’ that points
aptto the NodeSource repository.
-
update_package_lists()¶ Run
apt-get update(with compensation if things break).
-
install_package()¶ Install the Node.js package on a Debian or Ubuntu system.
-
-
exception
debuntu_tools.nodejs_installer.UnsupportedSystemError¶ Raised when an unsupported operating system is detected.
debuntu_tools.remote_reboot¶
Usage: reboot-remote-system [OPTIONS] [SSH_ALIAS]
Reboot a remote system and wait for the system to come back online. If the SSH alias matches a section in the ‘unlock-remote-system’ configuration, the root disk encryption of the remote system will be unlocked after it is rebooted.
Supported options:
| Option | Description |
|---|---|
-s, --shell |
Start an interactive shell on the remote system after it has finished booting. |
-v, --verbose |
Increase logging verbosity (can be repeated). |
-q, --quiet |
Decrease logging verbosity (can be repeated). |
-h, --help |
Show this message and exit. |
-
debuntu_tools.remote_reboot.main()¶ Command line interface for
reboot-remote-system.
-
debuntu_tools.remote_reboot.reboot_remote_system(context=None, name=None)¶ Reboot a remote Linux system (unattended).
Parameters: - context – A
RemoteContextobject (orNone). - name – The name of the
unlock-remote-systemconfiguration section for the remote host (a string) orNone.
Raises: ValueErrorwhen the remote system appears to be using root disk encryption but there’s nounlock-remote-systemconfiguration section available. The reasoning behind this is to err on the side of caution when we suspect we won’t be able to get the remote system back online.This function reboots a remote Linux system, waits for the system to go down and then waits for it to come back up.
If the
ssh_aliasof the context matches a section in the unlock-remote-system configuration, the root disk encryption of the remote system will be unlocked after it is rebooted.- context – A
-
debuntu_tools.remote_reboot.get_uptime(context, multiplexed=False)¶ Get the uptime of a remote Linux system by reading
/proc/uptime.Parameters: context – An execution context created by executor.contexts.Returns: The uptime of the remote system (as a float).This function is used by
reboot_remote_system()to wait until a remote Linux system has successfully rebooted.The uptime check uses
ssh -S none $REMOTE_HOST cat /proc/uptimewhere-S noneopts out of OpenSSH connection multiplexing, because in my experience the master connection for multiplexed OpenSSH connections handles remote reboots rather ungracefully (it can take a minute or two before the client finally considers the master connection dead and successfully establishes a fresh connection to the rebooted remote host).Any output on the standard error stream is silenced because 99% of the time this output will consist of SSH client connection errors due to the “retry until success” approach taken by
reboot_remote_system().
-
debuntu_tools.remote_reboot.get_post_context(name)¶ Get an execution context for the post-boot environment of a remote host.
Parameters: name – The configuration section name or SSH alias of the remote host (a string). Returns: A RemoteContextobject.
-
debuntu_tools.remote_reboot.is_encrypted(context)¶ Detect whether a remote system is using root disk encryption.
Parameters: context – A RemoteContextobject.Returns: Trueif root disk encryption is being used,Falseotherwise.
debuntu_tools.remote_unlock¶
Usage: unlock-remote-system [OPTIONS] PRE_BOOT [POST_BOOT]
Boot a remote Linux system that’s waiting for the root disk encryption password to be entered into an interactive prompt by connecting to the remote system over the network using SSH and entering the password non-interactively. The remote Linux system needs to be configured in such a way that the pre-boot environment enables a static IP address and starts an SSH server like Dropbear.
The PRE_BOOT argument defines how to connect to the pre-boot environment:
- Its value is assumed to be a host name, IP address or SSH alias.
- It can optionally start with a username followed by an ‘@’ sign.
- It can optionally end with a ‘:’ followed by a port number.
The default username is ‘root’ and the default port number 22. The optional POST_BOOT argument defines how to connect to the post-boot environment, this is useful when the pre and post-boot environments run SSH servers on different port numbers.
If the PRE_BOOT argument matches the name of a user defined configuration section the options in that section define how unlock-remote-system operates.
Supported options:
| Option | Description |
|---|---|
-i, --identity-file=KEY_FILE |
Use the private key stored in KEY_FILE for SSH connections to the pre-boot
environment. The post-boot environment is expected to use your default
private key or have a suitable configuration in ~/.ssh/config. |
-k, --known-hosts=HOSTS_FILE |
Use HOSTS_FILE as the “known hosts file” for SSH connections to the
pre-boot environment. When this option is not given host key verification
will be disabled to avoid conflicts between the host keys of the different
SSH servers running in the pre-boot and post-boot environments. |
-p, --password=NAME |
Get the password for the root disk encryption of the remote system from
the local password store in ~/.password-store using the ‘pass’ program.
The NAME argument gives the full name of the password. |
-r, --remote-host=SSH_ALIAS |
Connect to the remote system through an SSH proxy. |
-s, --shell |
Start an interactive shell on the remote system after it has finished booting. |
-w, --watch |
Start monitoring the remote system and automatically unlock the root disk encryption when the remote system is rebooted. The monitoring continues indefinitely. |
-a, --all |
Enable monitoring of all configured systems when combined with --watch. |
-v, --verbose |
Increase logging verbosity (can be repeated). |
-q, --quiet |
Decrease logging verbosity (can be repeated). |
-h, --help |
Show this message and exit. |
-
debuntu_tools.remote_unlock.EXPRESSION_PATTERN= <_sre.SRE_Pattern object>¶ A compiled regular expression pattern to parse connection profile expressions.
-
debuntu_tools.remote_unlock.HOST_KEYS_FILE= '~/.config/unlock-remote-system.d/known-hosts.ini'¶ The configuration file that’s used to store SSH host keys (a string).
-
debuntu_tools.remote_unlock.main()¶ Command line interface for
unlock-remote-system.
-
debuntu_tools.remote_unlock.find_local_username()¶ Find the username of the current user on the local system.
-
debuntu_tools.remote_unlock.get_password_from_store(name, store=None)¶ Get the disk encryption password from the ‘pass’ program.
-
debuntu_tools.remote_unlock.prompt_for_password(hostname)¶ Prompt the operator to interactively enter the disk encryption password.
-
class
debuntu_tools.remote_unlock.EncryptedSystem(**kw)¶ Python API for the unlock-remote-system program.
This class implements a Python API for remote unlocking of Linux systems with root disk encryption over SSH. The internals of this class differentiate between the pre-boot and post-boot environments:
- The
pre_bootandpost_bootproperties areConnectionProfileobjects that define how to connect to the SSH server in the pre-boot environment (usually this is Dropbear running in the initial ram disk) and the SSH server in the post-boot environment (usually this is OpenSSH). - The
pre_contextandpost_contextproperties areRemoteContextobjects that enable command execution in the pre-boot and post-boot environments. The values of these properties are created based on the corresponding connection profiles. - In addition to the command execution contexts for the pre-boot and
post-boot environments there is the
contextproperty which provides a command execution context for commands outside of the remote system. This defaults to the local system on which unlock-remote-system is running but will be a remote system whenssh_proxyis set.
Here’s an overview of the
EncryptedSystemclass:When you initialize a
EncryptedSystemobject you are required to provide values for thepost_bootandpre_bootproperties. You can set the values of theboot_timeout,config_loader,config_section,connect_timeout,control_directory,cryptroot_config,cryptroot_program,interactive,key_script,known_hosts_file,named_pipe,password,post_boot,pre_boot,retry_interval,scan_timeout,ssh_proxyandwatch_intervalproperties by passing keyword arguments to the class initializer.-
boot_timeout¶ The number of seconds to wait for the system to boot (a number, defaults to 5 minutes).
Note
The
boot_timeoutproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
config¶ A dictionary with configuration options.
See also
configandconfig_section.Note
The
configproperty is alazy_property. This property’s value is computed once (the first time it is accessed) and the result is cached.
-
config_loader¶ A
ConfigLoaderobject.See also
configandconfig_section.Note
The
config_loaderproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
config_section¶ The configuration section to use (a string or
None).When this option is set
configcan be loaded andstore_host_keys()can persist SSH host keys.See also
configandconfig_loader.Note
The
config_sectionproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
connect_timeout¶ How long to wait for the system to become reachable (an integer, defaults to 2 minutes).
Note
The
connect_timeoutproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
context¶ The command execution context from which the remote system is being controlled.
The computed value of this property is a command execution context created by
executor.contexts. Whenssh_proxyis set this will be aRemoteContextobject, otherwise it will be aLocalContextobject.Note
The
contextproperty is alazy_property. This property’s value is computed once (the first time it is accessed) and the result is cached.
-
control_directory¶ A temporary directory for SSH control sockets (a string).
Note
The
control_directoryproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
cryptroot_config¶ The pathname of the ‘cryptroot’ configuration file (a string).
The value of this property sets the pathname of the ‘cryptroot’ configuration file in the pre-boot environment (the initial ram disk). It defaults to ‘/conf/conf.d/cryptroot’.
Note
The
cryptroot_configproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
cryptroot_program¶ The pathname of the ‘cryptroot’ program (a string).
The value of this property sets the pathname of the ‘cryptroot’ program in the pre-boot environment (the initial ram disk). It defaults to ‘/scripts/local-top/cryptroot’.
Note
The
cryptroot_programproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
have_cryptroot_config¶ Trueifcryptroot_configexists,Falseotherwise.The existence of the
/conf/conf.d/cryptrootconfiguration file is taken as confirmation that the remote system is currently in its pre-boot environment (initial ram disk).
-
have_cryptroot_program¶ Trueifcryptroot_programexists,Falseotherwise.
-
have_named_pipe¶ Trueifnamed_pipeexists,Falseotherwise.The named pipe configured by the
named_pipeproperty provides a simple and robust way to inject the root disk encryption pass phrase into the boot sequence. When the named pipe is available it will be used as the preferred method.In my experience this works on Ubuntu 16.04 but it doesn’t work on Ubuntu 14.04 (because the named pipe doesn’t exist).
-
interactive¶ Trueto allow user interaction,Falseotherwise.The value of
interactivedefaults to the return value ofconnected_to_terminal()when givensys.stdin.Note
The
interactiveproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
key_script¶ The pathname of the generated key script (defaults to
'/tmp/keyscript.sh').Note
The
key_scriptproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
known_hosts_file¶ The filename of the “known hosts file” to use (a string or
None).Note
The
known_hosts_fileproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
named_pipe¶ The pathname of the named pipe used by cryptsetup (a string).
The value of this property sets the pathname of the named pipe used by cryptsetup in the pre-boot environment (the initial ram disk). It defaults to ‘/lib/cryptsetup/passfifo’.
Note
The
named_pipeproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
password¶ The password that unlocks the root filesystem of the remote host (a string or
None).If the configuration section contains the option password-name then
get_password_from_store()will be used to get the password by executing thepassprogram.The optional configuration option password-store can be used to set the
$PASSWORD_STORE_DIRenvironment variable.Note
The
passwordproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
post_boot¶ A connection profile for the post-boot environment (a
ConnectionProfileobject).Note
The
post_bootproperty is acustom_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named post_boot (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached.
-
post_context¶ A command execution context for the post-boot environment.
The computed value of this property is a command execution context created by
executor.contexts, more specifically it’s aRemoteContextobject.Note
The
post_contextproperty is alazy_property. This property’s value is computed once (the first time it is accessed) and the result is cached.
-
pre_boot¶ A connection profile for the pre-boot environment (a
ConnectionProfileobject).Note
The
pre_bootproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named pre_boot (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
pre_context¶ The command execution context inside the pre-boot environment.
The computed value of this property is a command execution context created by
executor.contexts, more specifically it’s aRemoteContextobject.Note
The
pre_contextproperty is alazy_property. This property’s value is computed once (the first time it is accessed) and the result is cached.
-
retry_interval¶ The time between connection attempts (an integer, defaults to 1 second).
Note
The
retry_intervalproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
scan_timeout¶ The timeout for ssh-keyscan (an integer, defaults to 5 seconds).
Note
The
scan_timeoutproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
ssh_proxy¶ The SSH alias of a proxy to the remote system (a string or
None).Note
The
ssh_proxyproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
watch_interval¶ The time between pre-boot environment checks (an integer, defaults to 60 seconds).
Note
The
watch_intervalproperty is acustom_property. You can change the value of this property using normal attribute assignment syntax. This property’s value is computed once (the first time it is accessed) and the result is cached. To clear the cached value you can usedelordelattr().
-
__enter__()¶ Prepare
control_directory.
-
__exit__(exc_type=None, exc_value=None, traceback=None)¶ Cleanup
control_directory.
-
check_ssh_connection()¶ Verify SSH connectivity to the pre-boot environment.
-
create_key_script()¶ Create a key script in the pre-boot environment (initial ram disk).
This method creates a minimal key script (a shell script containing a single
echocommand) in the pre-boot environment and modifies thecryptroot_configfile so that the key script is used to unlock the root disk.
-
find_process_id(program, **options)¶ Determine the process id of a program or script in the pre-boot environment.
Parameters: program – The name of a program or script (a string). Returns: A process id (an integer) or None.
-
get_known_host_keys(option_name)¶ Get the configured SSH host keys.
Parameters: option_name – The name of the configuration option that holds known host keys (a string). Returns: A setof strings.
-
kill_emergency_shell()¶ Kill the emergency shell process to resume the boot process.
-
kill_interactive_prompt()¶ Kill the process responsible for the interactive prompt in the pre-boot environment.
-
offer_password()¶ Make the root disk encryption pass phrase available to the remote host.
Raises: UnsupportedSystemErrorwhenhave_named_pipe,have_cryptroot_configandhave_cryptroot_programare allFalse.If
have_named_pipeisTrueandpasswordis set, the internal methodwrite_to_named_pipe()is used to writepasswordto the named pipe. This is the preferred way to make the password available to the remote host because it’s simple and robust.When the named pipe isn’t available but
have_cryptroot_configisTruethe following internal methods are used instead:kill_interactive_prompt()create_key_script()(this is only called whenpasswordis set)run_cryptroot_program()kill_emergency_shell()
This is more convoluted than the named pipe but it works :-).
-
parse_host_keys(cmd)¶ Find the SSH host keys in the output of ssh-keyscan.
Parameters: cmd – A RemoteCommandobject.Returns: A setof strings.
-
parse_server_header(cmd)¶ Find the SSH server header in the output of ssh-keyscan.
Parameters: cmd – A RemoteCommandobject.Returns: The SSH server header (a string).
-
scan_ssh_server(profile)¶ Get the SSH server header and host keys.
Parameters: profile – A ConnectionProfileobject.Returns: A ServerDetailsobject.The
scan_ssh_server()method runs ssh-keyscan incontextand parses its output to get the SSH server header and host keys.
-
run_cryptroot_program(interactive=True)¶ Unlock the root disk encryption by running
cryptroot_program.
-
store_host_keys(pre_server, post_server)¶ Store the SSH host keys in the configuration.
Parameters: - pre_server – The
ServerDetailsobject fromwait_for_pre_boot(). - post_server – The
ServerDetailsobject fromwait_for_post_boot().
- pre_server – The
-
test_ssh_connection(profile, context)¶ Verify SSH connectivity to the pre-boot environment.
-
unlock_system(server=None)¶ Validate the pre-boot environment and unlock the root filesystem encryption.
Parameters: server – A ServerDetailsobject orNone.When the server argument isn’t given
wait_for_pre_boot()is used to wait for the pre-boot environment to become available.
-
wait_for_post_boot(pre_server)¶ Wait for the post-boot environment to come online.
Parameters: pre_server – A ServerDetailsobject created bywait_for_pre_boot().
-
wait_for_pre_boot()¶ Wait for the pre-boot environment to become available.
Returns: A
ServerDetailsobject.Raises: The following exceptions can be raised:
SystemUnreachableErrorwhenconnect_timeoutseconds have passed and we still haven’t managed to query the SSH server in the pre-boot environment.UnlockAbortedErrorwhen the post-boot environment is detected and the operator aborts the unlock sequence.
-
watch_system()¶ Start monitoring the remote system for reboots.
When the remote system is rebooted, the root disk encryption will be unlocked automatically. The monitoring continues indefinitely.
-
write_file(filename, contents)¶ Write a file in the initial ram disk of the pre-boot environment.
Parameters: - filename – The pathname of the file to write (a string).
- contents – The contents to write to the file (a string).
This method writes a file in the initial ram disk by running a remote ‘sh’ shell that redirects its standard input to the given filename.
-
write_to_named_pipe()¶ Write
passwordto the named pipe configured bynamed_pipe.
- The
-
class
debuntu_tools.remote_unlock.ConnectionProfile(**kw)¶ SSH connection profiles.
Here’s an overview of the
ConnectionProfileclass:Superclass: PropertyManagerProperties: expression,hostname,identity_file,port_numberandusernameWhen you initialize a
ConnectionProfileobject you are required to provide a value for thehostnameproperty. You can set the values of theexpression,hostname,identity_file,port_numberandusernameproperties by passing keyword arguments to the class initializer.-
expression¶ The SSH connection profile encoded as a string.
Note
The
expressionproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
hostname¶ The host name, IP address or SSH alias of the remote system (a string).
Note
The
hostnameproperty is arequired_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named hostname (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). You can change the value of this property using normal attribute assignment syntax.
-
port_number¶ The port number for SSH connections (an integer, defaults to 22).
Note
The
port_numberproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
username¶ The username used to log in to the remote system (a string, defaults to ‘root’).
Note
The
usernameproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
identity_file¶ The pathname of the identity file used to connect to the remote system (a string or
None).Note
The
identity_fileproperty is amutable_property. You can change the value of this property using normal attribute assignment syntax. To reset it to its default (computed) value you can usedelordelattr().
-
-
class
debuntu_tools.remote_unlock.ServerDetails(**kw)¶ Properties that can be used to uniquely identify SSH servers.
Here’s an overview of the
ServerDetailsclass:Superclass: PropertyManagerPublic methods: match_header()Properties: headerandhost_keysWhen you initialize a
ServerDetailsobject you are required to provide values for theheaderandhost_keysproperties. You can set the values of theheaderandhost_keysproperties by passing keyword arguments to the class initializer.-
header¶ The SSH server header (a string).
This is the first line of output emitted by the SSH server when a client opens a TCP connection.
Note
The
headerproperty is akey_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named header (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). Once this property has been assigned a value you are not allowed to assign a new value to the property.
-
host_keys¶ The SSH server host keys (a
frozensetof strings).Note
The
host_keysproperty is akey_property. You are required to provide a value for this property by calling the constructor of the class that defines the property with a keyword argument named host_keys (unless a custom constructor is defined, in this case please refer to the documentation of that constructor). Once this property has been assigned a value you are not allowed to assign a new value to the property.
-
-
exception
debuntu_tools.remote_unlock.EncryptedSystemError¶ Base class for custom exceptions raised by
debuntu_tools.remote_unlock.
-
exception
debuntu_tools.remote_unlock.UnlockAbortedError¶ Raised when the operator changes their minds.
-
exception
debuntu_tools.remote_unlock.SystemUnreachableError¶ Raised when connecting to the SSH server in the pre-boot environment fails.
-
exception
debuntu_tools.remote_unlock.BootTimeoutError¶ Raised when the remote system doesn’t boot properly after unlocking.
-
exception
debuntu_tools.remote_unlock.UnsupportedSystemError¶ Raised when the configuration file ‘/conf/conf.d/cryptroot’ is not found.
debuntu_tools.upgrade_system¶
Usage: upgrade-remote-system [OPTIONS] [SSH_ALIAS]
Upgrade the system packages on a remote Debian or Ubuntu system, reboot the system when this is required due to security updates or because the system isn’t yet running the newest kernel, remove old Linux kernel and header packages and optionally remove ‘auto-removable’ system packages.
If the given SSH alias matches a section in the ‘unlock-remote-system’ configuration, the root disk encryption of the remote system will be automatically unlocked when the system is rebooted.
Supported options:
| Option | Description |
|---|---|
-s, --shell |
Start an interactive shell on the remote system afterwards. |
-v, --verbose |
Increase logging verbosity (can be repeated). |
-q, --quiet |
Decrease logging verbosity (can be repeated). |
-h, --help |
Show this message and exit. |
-
debuntu_tools.upgrade_system.main()¶ Command line interface for
upgrade-remote-system.
-
debuntu_tools.upgrade_system.upgrade_remote_system(context, force_reboot=False)¶ Perform standard system maintenance tasks on a remote Debian or Ubuntu system.
Parameters: context – An execution context created by executor.contexts.This function performs the following system maintenance tasks:
- The
apt-get updatecommand is run (using the Python API of the apt-mirror-updater program). - The
apt-get dist-upgradecommand is run [1]. - The
apt-get cleancommand is run. - If the file
/var/run/reboot-requiredexists (indicating that a reboot is required due to security updates) the remote system is rebooted using the Python API of thereboot-remote-systemprogram, to enable automatic unlocking of remote root disk encryption. - Old kernel packages are removed (using the Python API of the
debuntu-kernel-managerprogram). If more than one meta package is installed a warning message is logged but no exception is raised. - The
apt-get autoremove --purgecommand is run to optionally [1] remove any ‘auto-removable’ system packages.
[1] (1, 2) Because the apt-getoption--yesis not used, the operator will be asked to confirm using an interactive confirmation prompt.- The