2018-11-06 13:01:05 +00:00
# | nix-amer |
2018-11-06 10:17:33 +00:00
2018-11-11 00:08:33 +00:00
[![Go version ](https://img.shields.io/badge/go_version-1.11-blue.svg )](https://golang.org/doc/go1.11)
2018-11-13 20:50:10 +00:00
[![License: MIT ](https://img.shields.io/github/license/xdrm-brackets/nix-amer.svg )](https://opensource.org/licenses/MIT)
2018-11-18 21:49:11 +00:00
[![Go Report Card ](https://goreportcard.com/badge/git.xdrm.io/go/nix-amer )](https://goreportcard.com/report/git.xdrm.io/go/nix-amer)
2018-11-13 20:50:10 +00:00
[![Coverage Status ](https://img.shields.io/coveralls/github/xdrm-brackets/nix-amer/master.svg )](https://coveralls.io/github/xdrm-brackets/nix-amer?branch=master)
2018-11-18 21:49:11 +00:00
[![Build Status ](https://drone.xdrm.io/api/badges/xdrm-brackets/ci-test/status.svg )](https://drone.xdrm.io/xdrm-brackets/ci-test)
[![Go doc ](https://godoc.org/git.xdrm.io/go/nix-amer?status.svg )](https://godoc.org/git.xdrm.io/go/nix-amer)
2018-11-11 00:08:33 +00:00
2018-11-06 13:01:05 +00:00
```yaml
name: nix-amer
version: 0.1.0
description: Configuration automation tool
author: xdrm-brackets
```
2018-11-06 10:19:09 +00:00
2018-11-06 13:01:05 +00:00
>Need to automate the setup of your linux server or desktop ? This tool is made for you.
2018-11-13 12:50:33 +00:00
<!-- toc -->
2018-11-13 13:06:33 +00:00
* [I. How to use ](#i-how-to-use )
+ [1) Requirements ](#1-requirements )
+ [2) Installation ](#2-installation )
+ [3) Usage ](#3-usage )
* [1. Create build file ](#1-create-build-file )
* [2. Run on the target ](#2-run-on-the-target )
* [II. Commands ](#ii-commands )
2018-11-18 21:49:11 +00:00
+ [1) Sections ](#1-sections )
+ [2) Comments ](#2-comments )
+ [3) Install/remove Packages ](#3-installremove-packages )
+ [4) Setup configuration ](#4-setup-configuration )
+ [5) Service management ](#5-service-management )
+ [6) Custom scripts ](#6-custom-scripts )
+ [7) Copy files ](#7-copy-files )
+ [8) Aliases ](#8-aliases )
2018-11-13 13:06:33 +00:00
* [III. Path Expressions ](#iii-path-expressions )
+ [1) Syntax ](#1-syntax )
+ [2) File Formats ](#2-file-formats )
2018-11-13 12:50:33 +00:00
- [Example ](#example )
<!-- tocstop -->
2018-11-06 13:01:05 +00:00
----
2018-11-13 13:06:33 +00:00
### I. How to use
2018-11-06 16:17:39 +00:00
2018-11-13 13:06:33 +00:00
#### 1) Requirements
In order to install the `nix-amer` executable, you must have :
- any recent linux system (_has not been tested over other OS_)
- `go` installed (_has not been tested under version **1.11**_)
#### 2) Installation
Simply launch the following command in any terminal
```bash
2018-11-18 21:49:11 +00:00
$ go get -u git.xdrm.io/go/nix-amer
2018-11-13 13:06:33 +00:00
```
2018-11-18 21:49:11 +00:00
> For those who don't know, it will load the project sources into `$GOPATH/src/git.xdrm.io/go/nix-amer` and compile into the executable at `$GOPATH/bin/nix-amer`.
2018-11-13 13:06:33 +00:00
#### 3) Usage
###### 1. Create build file
2018-11-18 21:49:11 +00:00
The first step is to write your build file according to the installation you want. While writing it you can check the syntax and validate instructions by using the `-dry-run` command-line argument as follows :
2018-11-13 13:06:33 +00:00
```bash
$ nix-amer -p apt-get -dry-run < path / to / build / file >
```
> The `-p` argument (package manager) is mandatory but it will have no effect in `-dry-run` mode. You can use for instance `apt-get` as a default.
###### 2. Run on the target
2018-11-18 21:49:11 +00:00
Once your build file is correct and fulfills your needs, you can log in to the target machine, install nix-amer and run it with your build file. The rich and colorful command-line output will give you a good feedback to rapidly fix problems.
2018-11-13 13:06:33 +00:00
------
### II. Commands
2018-11-06 16:17:39 +00:00
2018-11-18 21:49:11 +00:00
Your whole setup remains in only one file. Each line contains one instruction, the list of instructions is listed below.
2018-11-06 13:01:05 +00:00
2018-11-18 21:49:11 +00:00
#### 1) Sections
2018-11-12 21:54:37 +00:00
2018-11-18 21:49:11 +00:00
Each instruction is enclosed in a section (_cf. ini file format_), a section definition stands on a line where the name of the section is surrounded by `[` and `]` . Each section is executed in parallel ; the special section named `pre` is executed before every other.
2018-11-12 21:54:37 +00:00
2018-11-18 21:49:11 +00:00
#### 2) Comments
Each line beginning with one of the following characters : `#` or `;` is considered a comment and is not interpreted.
#### 3) Install/remove Packages
2018-11-06 13:01:05 +00:00
These instructions allow you to interact with the package system available on your system.
```
2018-11-12 21:54:37 +00:00
install < packages >
2018-11-06 13:01:05 +00:00
```
Install the listed packages. If more than one, use spaces to separate package names.
2018-11-12 21:54:37 +00:00
2018-11-06 13:01:05 +00:00
```
2018-11-12 21:54:37 +00:00
delete < packages >
2018-11-06 13:01:05 +00:00
```
Remove the listed packages. If more than one, use spaces to separate package names.
2018-11-18 21:49:11 +00:00
#### 4) Setup configuration
2018-11-06 13:01:05 +00:00
2018-11-12 21:54:37 +00:00
This instruction allow you to set fields of configuration files without the need of an editor and in a developer-readable manner.
2018-11-06 13:01:05 +00:00
```
2018-11-12 21:54:37 +00:00
set < expr > < content >
2018-11-06 13:01:05 +00:00
```
2018-11-06 16:17:39 +00:00
Update a configuration file where \<expr\> is a dot-separated human-readable [path expression ](#ii-path-expressions ) and \<content\> is a string or a file. If a string is given, it will < u > override/add the field</ u > ; if a file is given the configuration file will be < u > replaced</ u > by it.
2018-11-06 13:01:05 +00:00
2018-11-12 21:54:37 +00:00
2018-11-18 21:49:11 +00:00
#### 5) Service management
2018-11-12 21:54:37 +00:00
These instructions allow you to interact with the service system (_cf. [systemd ](https://github.com/systemd/systemd )_).
2018-11-06 13:01:05 +00:00
```
2018-11-12 21:54:37 +00:00
service [enable|start|stop|restart|reload] < services >
2018-11-06 13:01:05 +00:00
```
2018-11-12 21:54:37 +00:00
Perform the action on services. If more than one, use spaces to separate service names.
2018-11-06 13:01:05 +00:00
2018-11-06 16:17:39 +00:00
2018-11-18 21:49:11 +00:00
#### 6) Custom scripts
2018-11-06 13:01:05 +00:00
2018-11-13 13:23:36 +00:00
This instruction allows you to use custom scripts for complex operations.
2018-11-06 13:01:05 +00:00
```
2018-11-13 13:06:33 +00:00
run < script >
2018-11-06 13:01:05 +00:00
```
2018-11-18 21:49:11 +00:00
Execute the executable located at the path \<script\>. If script is an [alias ](#8-aliases ) it will resolve to its path
2018-11-12 23:41:42 +00:00
2018-11-18 21:49:11 +00:00
#### 7) Copy files
2018-11-06 13:01:05 +00:00
2018-11-13 13:23:36 +00:00
This instruction allows you to copy files.
2018-11-12 21:54:37 +00:00
2018-11-13 13:23:36 +00:00
```
copy < src > < dst >
```
Try to copy the file \<src\> to the path \<dst\>.
2018-11-18 21:49:11 +00:00
#### 8) Aliases
2018-11-06 16:17:39 +00:00
2018-11-13 13:06:33 +00:00
The file format allows you to create aliases to file paths for more readability in the [path expression ](#ii-path-expressions ) or with the [`run` command ](#5-custom-scripts ).
2018-11-06 16:17:39 +00:00
```
2018-11-13 13:06:33 +00:00
alias name /path/to.file
2018-11-06 16:17:39 +00:00
```
2018-11-13 13:06:33 +00:00
Create the alias `name` which resolves to the path `/path/to.file` .
> Alias value either for the `set` or `run` command have priority to real file paths.
2018-11-06 16:17:39 +00:00
2018-11-06 13:01:05 +00:00
----
2018-11-06 16:17:39 +00:00
2018-11-13 13:06:33 +00:00
### III. Path Expressions
2018-11-06 13:01:05 +00:00
2018-11-13 13:06:33 +00:00
#### 1) Syntax
2018-11-12 21:54:37 +00:00
The syntax is pretty fast-forward, it uses 2 levels (file, fields) to find your configuration line : `location_or_alias@fields` .
2018-11-06 13:01:05 +00:00
| Field | Description | Example |
2018-11-06 16:17:39 +00:00
| --------- | :----------------------------------- | -------------------------- |
2018-11-18 21:49:11 +00:00
| `location_or_alias` | Path to the configuration file to edit. The file will be created if not found. If the path is an [alias ](#8-aliases ) created before in the file, it will resolve to the alias value as a filename. | `/etc/nginx/nginx.conf` , `some-alias` |
2018-11-12 21:54:37 +00:00
| `fields` | Dot-separated chain of strings that match a configuration field. If the field does not point to a raw field but an existing field container, the \<value\> will replace the group with a text value. | `AllowGroups` , `http.gzip` |
2018-11-07 11:41:34 +00:00
2018-11-13 13:06:33 +00:00
> The `fields` is processed only for known file formats listed in this [section](#2-file-formats).
2018-11-06 16:17:39 +00:00
2018-11-13 13:06:33 +00:00
#### 2) File Formats
2018-11-07 10:56:53 +00:00
Configuration files can be written according to some standards or application-specific syntax. This tool uses standard and third-party to parse the following formats :
- [xml ](https://fr.wikipedia.org/wiki/Extensible_Markup_Language ) and [json ](https://json.org/ ) are read/written using the go standard library (_cf. [json ](https://golang.org/pkg/encoding/json/ ), [xml ](https://golang.org/pkg/encoding/xml/ )_).
2018-11-12 21:54:37 +00:00
> xml is not yet supported ; complex structure the library outputs (attributes vs. content) to understand better
2018-11-11 18:10:11 +00:00
- [yaml ](https://en.wikipedia.org/wiki/YAML ) with [go-yaml/yaml ](https://github.com/go-yaml/yaml ).
2018-11-12 21:54:37 +00:00
- [ini ](https://en.wikipedia.org/wiki/INI_file ) with [go-ini/ini ](https://github.com/go-ini/ini ).
2018-11-18 21:49:11 +00:00
- [nginx configurations ](https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/ ) with [my own library ](https://godoc.org/git.xdrm.io/go/nix-amer/internal/cnf/parser/nginx ).
- [bash sourced configurations]() with [my own library ](https://godoc.org/git.xdrm.io/go/nix-amer/internal/cnf/parser/bash ) (_e.g. ~/.bashrc_).
- _and more to come..._
2018-11-07 10:56:53 +00:00
2018-11-06 16:17:39 +00:00
----
## Example
2018-11-12 21:54:37 +00:00
Launch the following command to install your server :
```bash
$ nix-amer -p apt-get myserver.build
```
> Replace `apt-get` by your package manager; `nix-amer -help` for available options.
2018-11-06 16:17:39 +00:00
_myserver.build_
```
2018-11-14 10:34:27 +00:00
# [pre] is executed before launching everything else
[pre]
install nginx ssh sslh
2018-11-12 21:54:37 +00:00
alias nginx /etc/nginx/nginx.conf
2018-11-14 10:34:27 +00:00
alias sshd /etc/ssh/sshd_config
2018-11-12 21:54:37 +00:00
alias sslh /etc/default/sslh
2018-11-06 16:17:39 +00:00
2018-11-07 11:41:34 +00:00
[nginx]
2018-11-12 21:54:37 +00:00
set nginx@http.gzip off
service enable nginx
service start nginx
2018-11-06 16:17:39 +00:00
2018-11-07 11:41:34 +00:00
[sshd]
2018-11-12 21:54:37 +00:00
set sshd@PermitRootLogin no
set sshd@PermitEmptyPasswords no
service enable sshd
service enable ssh
2018-11-06 16:17:39 +00:00
2018-11-07 11:41:34 +00:00
[sslh]
2018-11-12 21:54:37 +00:00
set sslh@RUN yes
set sslh@DEAMON_OPTS "--user sslh --listen 0.0.0.0:443 --ssh 127.0.0.1:22 --ssl 127.0.0.1:44300 --pidfile /var/run/sslh/sslh.pid"
service enable sslh
service start sslh
2018-11-06 16:17:39 +00:00
```