This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for shell provisioning * Added support for scripts in the shell provisioner * Removed unnecessary check in homedir_expanded_path method * Refactor the shell provisioner schema * Used shlex to split raw commands comming from the shell provisioner * Added a note related to the use of the shell provisioner
- Loading branch information
Showing
13 changed files
with
255 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ __pycache__ | |
.coverage.* | ||
.env/ | ||
env/ | ||
*.retry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
##### | ||
Shell | ||
##### | ||
|
||
The shell provisioner allows you to execute commands on the guest side or the host side in order to | ||
provision your containers. | ||
|
||
Usage | ||
----- | ||
|
||
Just append a ``shell`` provisioning operation to your LXDock file as follows: | ||
|
||
.. code-block:: yaml | ||
name: myproject | ||
image: ubuntu/xenial | ||
provisioning: | ||
- type: shell | ||
inline: echo "Hello, World!" | ||
.. note:: | ||
|
||
Keep in mind that the shell provisioner will use the LXD's ``exec`` method in order to run your | ||
commands on containers (the same method used by the ``lxc exec`` command). This means that common | ||
shell patterns (like file redirects, ``|``, ``>``, ``<``, ...) won't work because the ``exec`` | ||
method doesn't use a shell (so the kernel will not be able to understand these shell patterns). | ||
The only way to overcome this is to put things like ``sh -c 'ls -l > /tmp/test'`` in your | ||
``inline`` options. | ||
|
||
Required options | ||
---------------- | ||
|
||
inline | ||
====== | ||
|
||
The ``inline`` option allows you to specify a shell command that should be executed on the guest | ||
side or on the host. Note that the ``inline`` option and the ``script`` option are mutually | ||
exclusive. | ||
|
||
.. code-block:: yaml | ||
[...] | ||
provisioning: | ||
- type: shell | ||
inline: echo "Hello, World!" | ||
script | ||
====== | ||
|
||
The ``script`` option lets you define the path to an existing script that should be executed on the | ||
guest side or on the host. Note that the ``script`` option and the ``inline`` option are mutually | ||
exclusive. | ||
|
||
.. code-block:: yaml | ||
[...] | ||
provisioning: | ||
- type: shell | ||
script: path/to/my/script.sh | ||
Optional options | ||
---------------- | ||
|
||
side | ||
==== | ||
|
||
Use the ``side`` option if you want to define that the shell commands/scripts should be executed on | ||
the host side. The default value for this option is ``guest``. Here is an example: | ||
|
||
.. code-block:: yaml | ||
[...] | ||
provisioning: | ||
- type: shell | ||
side: host | ||
inline: echo "Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
|
||
from .ansible import * # noqa | ||
from .base import * # noqa | ||
from .shell import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import shlex | ||
|
||
from voluptuous import Any, Exclusive, IsFile | ||
|
||
from .base import Provisioner | ||
|
||
|
||
class ShellProvisioner(Provisioner): | ||
""" Allows to perform provisioning shell operations on the host/guest sides. """ | ||
|
||
name = 'shell' | ||
schema = { | ||
Exclusive('inline', 'shelltype'): str, | ||
Exclusive('script', 'shelltype'): IsFile(), | ||
'side': Any('guest', 'host'), | ||
} | ||
|
||
def provision(self): | ||
""" Executes the shell commands in the guest container or in the host. """ | ||
if 'script' in self.options and self._is_for_guest: | ||
# First case: we have to run the script inside the container. So the first step is | ||
# to copy the content of the script to a temporary file in the container, ensure | ||
# that the script is executable and then run the script. | ||
guest_scriptpath = os.path.join('/tmp/', os.path.basename(self.options['script'])) | ||
with open(self.homedir_expanded_path(self.options['script'])) as fd: | ||
self.guest.lxd_container.files.put(guest_scriptpath, fd.read()) | ||
self.guest.run(['chmod', '+x', guest_scriptpath]) | ||
self.guest.run([guest_scriptpath, ]) | ||
elif 'script' in self.options and self._is_for_host: | ||
# Second case: the script is executed on the host side. | ||
self.host.run([self.homedir_expanded_path(self.options['script']), ]) | ||
elif 'inline' in self.options: | ||
# Final case: we run a command directly inside the container or outside. | ||
host_or_guest = getattr(self, self._side) | ||
host_or_guest.run(shlex.split(self.options['inline'])) | ||
|
||
################################## | ||
# PRIVATE METHODS AND PROPERTIES # | ||
################################## | ||
|
||
@property | ||
def _is_for_guest(self): | ||
""" Returns True if this provisioner should run on the guest side. """ | ||
return self._side == 'guest' | ||
|
||
@property | ||
def _is_for_host(self): | ||
""" Returns True if this provisioner should run on the host side. """ | ||
return self._side == 'host' | ||
|
||
@property | ||
def _side(self): | ||
return self.options.get('side', 'guest') |
Oops, something went wrong.