IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 13KB

4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. # Focker
  2. ## Introduction
  3. Focker is a FreeBSD image orchestration tool in the vein of Docker.
  4. ## Table of Contents
  5. <!-- TOC depthFrom:2 depthTo:4 withLinks:1 updateOnSave:1 orderedList:0 -->
  6. - [Introduction](#introduction)
  7. - [Table of Contents](#table-of-contents)
  8. - [Installation](#installation)
  9. - [Installing the Python package from PyPi](#installing-the-python-package-from-pypi)
  10. - [Installing the Python package from GitHub](#installing-the-python-package-from-github)
  11. - [Setting up ZFS](#setting-up-zfs)
  12. - [Preparing base image](#preparing-base-image)
  13. - [Usage](#usage)
  14. - [`focker` command syntax](#focker-command-syntax)
  15. - [focker image|img|im|i](#focker-imageimgimi)
  16. - [focker jail|j](#focker-jailj)
  17. - [focker volume](#focker-volume)
  18. - [focker compose](#focker-compose)
  19. - [`Fockerfile` syntax](#fockerfile-syntax)
  20. - [`focker-compose.yml` syntax](#focker-composeyml-syntax)
  21. - [Images](#images)
  22. - [Jails](#jails)
  23. - [Volumes](#volumes)
  24. - [Commands](#commands)
  25. <!-- /TOC -->
  26. ## Installation
  27. In order to use Focker you need a ZFS pool available in your FreeBSD installation.
  28. ### Installing the Python package from PyPi
  29. Run:
  30. ```bash
  31. pip install focker
  32. ```
  33. ### Installing the Python package from GitHub
  34. Run:
  35. ```bash
  36. git clone https://github.com/sadaszewski/focker.git
  37. cd focker/
  38. python setup.py install
  39. ```
  40. or (if you want an uninstaller):
  41. ```bash
  42. git clone https://github.com/sadaszewski/focker.git
  43. cd focker/
  44. python setup.py sdist
  45. pip install dist/focker-0.9.tgz
  46. ```
  47. ### Setting up ZFS
  48. Upon first execution of the `focker` command, Focker will automatically create the necessary directories and ZFS datasets. You just need to exclude the unlikely case that you are already using `/focker` in your filesystem hierarchy. The layout after initialization will look the following:
  49. ```
  50. /focker
  51. /focker/images
  52. /focker/jails
  53. /focker/volumes
  54. ```
  55. `images`, `jails`, and `volumes` have corresponding ZFS datasets with `canmount=off` so that they serve as mountpoint anchors for child entries.
  56. ### Preparing base image
  57. To bootstrap the images system you need to install FreeBSD in jail mode to a ZFS dataset placed in `/focker/images` and provide two user-defined properties - `focker:sha256` and `focker:tags`. One way to achieve this would be the following (using Bash shell):
  58. ```bash
  59. TAGS="freebsd-latest freebsd-$(freebsd-version | cut -d'-' -f1)"
  60. VERSION="FreeBSD $(freebsd-version)"
  61. SHA256=$(echo -n ${VERSION} | sha256)
  62. NAME=${SHA256:0:7}
  63. zfs create -o focker:sha256=${SHA256} -o focker:tags="${TAGS}" zroot/focker/images/${NAME}
  64. bsdinstall jail /focker/images/${NAME}
  65. zfs set readonly=on zroot/focker/images/${NAME}
  66. zfs snapshot zroot/focker/images/${NAME}@1
  67. ```
  68. ## Usage
  69. At this point, Focker is ready to use.
  70. ### `focker` command syntax
  71. The `focker` command is the single entrypoint to all of the Focker's functionality. The overview of its syntax is presented below as a tree where the `focker` command is the root, the first level of descendants represents the choice of Level 1 mode (`image`, `jail`, `volume` or `compose`), the second level - the Level 2 mode (dependent on L1 mode) and the final third level lists required and optional arguments specific to the given combination of L1/L2 modes.
  72. ```
  73. focker
  74. |- image|img|im|i
  75. | |- build|b
  76. | | |- FOCKER_DIR
  77. | | `- --tags|-t TAG [...TAG]
  78. | |- tag|t
  79. | | |- REFERENCE
  80. | | `- TAG [...TAG]
  81. | |- untag|u
  82. | | `- TAG [...TAG]
  83. | |- list|ls|l
  84. | | `- --full-sha256|-f
  85. | |- prune|p
  86. | `- remove|r
  87. | |- REFERENCE
  88. | `- --remove-dependents|-R
  89. |- jail|j
  90. | |- create|c
  91. | | |- IMAGE
  92. | | |- --command|-c COMMAND (default: /bin/sh)
  93. | | |- --env|-e VAR1:VALUE1 [...VARN:VALUEN]
  94. | | |- --mounts|-m FROM1:ON1 [...FROMN:ONN]
  95. | | `- --hostname|-n HOSTNAME
  96. | |- start|s
  97. | | `- REFERENCE
  98. | |- stop|S
  99. | | `- REFERENCE
  100. | |- remove|r
  101. | | `- REFERENCE
  102. | |- exec|e
  103. | | |- REFERENCE
  104. | | `- [...COMMAND]
  105. | |- oneshot|o
  106. | | `- IMAGE
  107. | | `- --env|-e VAR1:VALUE1 [...VARN:VALUEN]
  108. | | `- --mounts|-m FROM1:ON1 [...FROMN:ONN]
  109. | | `- [...COMMAND]
  110. | |- list|ls|l
  111. | | `- --full-sha256|-f
  112. | |- tag|t
  113. | | |- REFERENCE
  114. | | `- TAG [...TAG]
  115. | |- untag|u
  116. | | `- TAG [...TAG]
  117. | `- prune|p
  118. | `- --force|-f
  119. |- volume
  120. | |- create
  121. | | `- --tags|-t TAG [...TAG]
  122. | |- prune
  123. | |- list
  124. | | `- --full-sha256|-f
  125. | |- tag
  126. | | |- REFERENCE
  127. | | `- TAG [...TAG]
  128. | `- untag
  129. | `- TAG [...TAG]
  130. `- compose
  131. |- build
  132. | `- FILENAME
  133. `- run
  134. |- FILENAME
  135. `- COMMAND
  136. ```
  137. Individual combinations are briefly described below:
  138. #### focker image|img|im|i
  139. The `focker image` mode groups commands related to Focker images.
  140. ##### build|b FOCKER_DIR [--tags TAG [...TAG]]
  141. Build a Focker image according to the specification in a Fockerfile present in the specified FOCKER_DIR. Fockerfile syntax is very straightforward and explained below.
  142. ##### tag|t REFERENCE TAG [...TAG]
  143. Applies one or more tags to the given image. REFERENCE can be the SHA256 of an image or one of its existing tags. It can be just a few first characters as long as they are unambiguous.
  144. ##### untag|u TAG [...TAG]
  145. Removes one or more image tags.
  146. ##### list|ls|l [--full-sha256|-f]
  147. Lists existing Focker images, optionally with full SHA256 checksums (instead of the default 7 first characters).
  148. ##### prune|p
  149. Greedily removes existing Focker images without tags and without dependents.
  150. ##### remove|r REFERENCE
  151. Removes the specified image.
  152. #### focker jail|j
  153. The `focker jail` mode groups commands related to Focker-managed jails.
  154. ##### create|c IMAGE [--command|-c COMMAND] [--env|-e VAR1:VALUE1 [...VARN:VALUEN]] [--mounts|-m FROM1:ON1 [...FROMN:ONN]] [--hostname|-n HOSTNAME]
  155. Creates a new Focker-managed jail. A jail consists of a clone of the given `IMAGE` and an entry in `/etc/jail.conf`. The configuration entry uses `exec.prestart` and `exec.start` to specify how the runtime environment (mounts and environmental variables) should be set up. It also calls `COMMAND` as last in `exec.start`. If not specified `COMMAND` defaults to `/bin/sh`. The hostname can be specified using the `HOSTNAME` parameter. Mounts and environment variables are provided as tuples separated by a colon (:). The environmental variable specification consists of variable name followed by variable value. The mount specification consists of the "from path", followed by the "on path". "From path" can be a local system path or a volume name.
  156. ##### start|s REFERENCE
  157. Starts the given jail specified by `REFERENCE`. `REFERENCE` can be the SHA256 of an existing jail or one of its existing tags. It can be just a few first characters as long as they are unambiguous. This command is equivalent of calling `jail -c`.
  158. ##### stop|S REFERENCE
  159. Stops the given jail specified by `REFERENCE`. This command is equivalent to calling `jail -r`.
  160. ##### remove|r REFERENCE
  161. Removes the given jail specified by `REFERENCE`. The jail is stopped if running, any filesystems mounted under its root directory are unmounted, its ZFS dataset and entry in `/etc/jail.conf` are removed.
  162. ##### exec|e REFERENCE [...COMMAND]
  163. Executes given `COMMAND` (or `/bin/sh` if not specified) in the given running jail specified by `REFERENCE`. This command is the equivalent of calling `jexec`.
  164. ##### oneshot|o IMAGE [--env|-e VAR1:VALUE1 [...VARN:VALUEN]] [--mounts|-m FROM1:ON1 [...FROMN:ONN]] [...COMMAND]
  165. Create a new one-time Focker-managed jail. The syntax and logic is identical to `focker jail create`, the difference being that the hostname cannot be specified and that the jail will be automatically removed when the `COMMAND` exits.
  166. Example: `focker jail oneshot freebsd-latest -e FOO:bar -- ls -al`
  167. ##### list|ls|l
  168. Lists Focker-managed jails. For running jails their JIDs will be displayed.
  169. ##### tag REFERENCE TAG [...TAG]
  170. Applies one or more tags to the given jail. REFERENCE can be the SHA256 of a jail or one of its existing tags. It can be just a few first characters as long as they are unambiguous.
  171. ##### untag TAG [...TAG]
  172. Removes one or more jail tags.
  173. ##### prune
  174. Removes existing Focker jails without tags.
  175. #### focker volume
  176. The `focker volume` mode groups commands related to Focker volumes.
  177. ##### create [--tags|-t TAG [...TAG]]
  178. Create a new Focker volume optionally tagged with the given `TAG`s.
  179. ##### prune
  180. Removes existing Focker volumes without tags.
  181. ##### list [--full-sha256|-f]
  182. Lists existing Focker volumes. Full SHA256 is displayed if the `-f` switch is used, otherwise only the first 7 characters will be shown.
  183. ##### tag REFERENCE TAG [...TAG]
  184. Applies one or more tags to the given volume. REFERENCE can be the SHA256 of a volume or one of its existing tags. It can be just a few first characters as long as they are unambiguous.
  185. ##### untag TAG [...TAG]
  186. Removes one or more volume tags.
  187. #### focker compose
  188. The `focker compose` mode groups commands related to Focker composition files - `focker-compose.yml`.
  189. ##### build FILENAME
  190. Builds images, volumes and jails according to the specification provided in the file pointed to by `FILENAME`.
  191. ##### run FILENAME COMMAND
  192. Runs one of the commands (specified by `COMMAND`) from the composition file pointed to by `FILENAME`.
  193. ### `Fockerfile` syntax
  194. A sample `Fockerfile` is pasted below.
  195. ```yaml
  196. base: freebsd-latest
  197. steps:
  198. - copy:
  199. - [ '/tmp/x', '/etc/x' ]
  200. - [ 'files/y', '/etc/y' ]
  201. - copy: [ files/z, /etc/z ]
  202. - run: |
  203. pkg install -y python3
  204. - run:
  205. - pkg install -y py37-pip
  206. - pkg install -y py37-yaml
  207. - pkg install -y py37-certbot
  208. - run: |
  209. mkdir -p /persist/etc/ssh && \
  210. sed -i '' -e 's/\/etc\/ssh\/ssh_host_/\/persist\/etc\/ssh\/ssh_host_/g' /etc/rc.d/sshd && \
  211. sed -i '' -e 's/\/etc\/ssh\/ssh_host_/\/persist\/etc\/ssh\/ssh_host_/g' /etc/ssh/sshd_config && \
  212. sed -i '' -e 's/#HostKey/HostKey/g' /etc/ssh/sshd_config
  213. ```
  214. `Fockerfile` currently supports only two entries - `base` and `steps`. `base` specifies the parent image on top of which the operations described by `steps` are executed. Each entry in `steps` results in creation of a new image. Focker determines a checksum for each step and if the corresponding image already exists the step is skipped and work continues on top of the existing image. This is a powerful paradigm for image building experimentation where we can split the task into multiple steps and resume work from the last successful step in case of problems. It is a big time saver. `steps` is a list that can contain `copy` and `run` entries. The `copy` entry specifies a single one or a list of copy operations from local files to the image in form of the `[FROM, TO]` tuples. The `run` entry specifies a chain of commands to be executed within the image. It can be a list of string or a single string.
  215. ### `focker-compose.yml` syntax
  216. A sample composition file illustrating all of the principles is pasted below.
  217. ```yaml
  218. images:
  219. wordpress-5: /path/to/wordpress_5_focker_dir
  220. jails:
  221. wordpress:
  222. image: wordpress-5
  223. env:
  224. SITE_NAME: Test site
  225. mounts:
  226. wp-volume2: /mnt/volume2
  227. wp-volume1: /mnt/volume1
  228. ip4.addr: 127.0.1.1
  229. interface: lo1
  230. volumes:
  231. wp-volume1: {}
  232. wp-volume2: {}
  233. wp-backup: {}
  234. commands:
  235. backup:
  236. jail: wordpress
  237. command: |
  238. mysqldump >/mnt/volume2/backup.sql
  239. mounts:
  240. wp-backup: /mnt/backup
  241. restore:
  242. jail: wordpress
  243. command: |
  244. mysql </mnt/volume2/backup.sql
  245. mounts:
  246. wp-backup: /mnt/backup
  247. ```
  248. #### Images
  249. The `images` entry in Focker composition file specifies a dictionary from image tags to Focker directories (directories containing the `Fockerfile` and any supplementary files needed to build an image). Upon running `focker compose build` Focker will run `focker image build` for all of the specified directories and tag the results with the corresponding tags. This process can be repeated without significant performance penalty since the images will not need to be rebuilt unless their `Fockerfile`s or contexts change.
  250. #### Jails
  251. The `jails` entry in the Focker composition file specifies a dictionary from jail tags to jail specifications. A jail specification is a dictionary that can contain the following fields: `image`, `env`, `mounts`, `exec.start`, `exec.stop`, `hostname`, `ip4.addr`, `interface`. `image`, `env` and `mounts` have the same semantics as in the `focker jail create` command. The syntax for `env` and `mounts` is in the form of dictionaries. `exec.start`, `exec.stop`, `hostname`, `ip4.addr` and `interface` have the same semantics as the corresponding entries in `/etc/jail.conf`. The jails will be recreated each time `focker compose build` is executed. Hence, any persistent data should be stored in volumes.
  252. #### Volumes
  253. #### Commands