Skip to content

Commit 2f824a9

Browse files
sudhamshkGopi A
authored andcommitted
Initial commit
1 parent b2b5e94 commit 2f824a9

18 files changed

+434
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*@.*
2+
.project

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,110 @@
1+
2+
█████╗ ██████╗ ███████╗██████╗ ████████╗
3+
██╔══██╗██╔══██╗██╔════╝██╔══██╗╚══██╔══╝
4+
███████║██║ ██║█████╗ ██████╔╝ ██║
5+
██╔══██║██║ ██║██╔══╝ ██╔═══╝ ██║
6+
██║ ██║██████╔╝███████╗██║ ██║
7+
╚═╝ ╚═╝╚═════╝ ╚══════╝╚═╝ ╚═╝
8+
19
Adept
210
-----
311

4-
###Using Adept
12+
_Adept_ is a *very* simple way to run commands & deploy apps on multiple servers. It runs on `Shell` (Linux, Mac, etc), over SSH and without any agents. Adept was created by non-DevOps folks at @UnwiredLabs to quickly manage ~ 25 servers without learning `ansible` or `chef`.
13+
14+
###How it works
15+
16+
Adept uses `SSH` to login to one or more servers and execute a series of `sh/bash` commands. This can be done sequentially or parallely. Commands are executed from the entry script - `deploy.sh` and the structure of a typical command is as follows:
17+
18+
```
19+
$ ./deploy.sh <Host-group file / SSH identity> <Task file> [Parallel or Sequential; default sequential]
20+
```
21+
22+
Host group & Task files are stored in the `./hosts` and `./tasks` folders respectively. Hosts are read from the local SSH config file, usually located at `~/.ssh/config`. These hosts need to be configured with SSH keys.
23+
24+
###Things you could do on multiple systems with Adept
25+
* Install a new stack
26+
* Add / remove users
27+
* Copy config files to multiple servers while also executing commands (using sFTP)
28+
* Deploy a script (git pulls, etc)
29+
* Deploy a new system by installing default tools
30+
* Just about anything else you do in Shell / Bash, but want replicated :-)
31+
32+
###Examples
33+
*These examples are included in `/tasks` folder of the project*
34+
35+
####Show current system date on all servers parallely:
36+
```
37+
$ ./deploy.sh all date 1
38+
```
39+
40+
####Show available disk space on all servers sequentially:
41+
```
42+
$ ./deploy.sh all disk 0
43+
```
44+
45+
####Deploy LAMP-server on a group of hosts:
46+
```
47+
$ ./deploy.sh webservers deploy-lamp
48+
```
49+
Adept can also use `sFTP` to copy files, such as config files, to servers. In-order to deploy the configuration files, we recommend you place them under `./configs` folder
50+
51+
####Deploy a PHPMyAdmin instance on a single host:
52+
```
53+
$ ./deploy.sh host1 deploy-pma
54+
```
55+
56+
###Adding servers / host-groups
57+
58+
####Create a host-group
59+
* Create a new `<host-group-name>.txt` file in `./hosts` folder
60+
* Enter host names (as configured in your local `~/.ssh/config` file) separated by `|`
61+
* The last host name should be followed by `|` for the file to work
62+
63+
####For a single server
64+
* You don't have to create a `host` file. Directly enter the SSH identity name in place of the hostgroup.
65+
66+
###Writing Adept scripts
67+
68+
####Create a task
69+
* Create a new `<task-name>.sh` file in `./tasks` folder
70+
* A simple task file looks like this:
71+
````
72+
ssh -t -t "$line" \
73+
'hostname &&
74+
exit;'
75+
````
76+
* This task can execute both parallely or in sequentially
77+
* If it's a parallel task, you should use the following style of code to ensure output for each host comes together:
78+
````
79+
out=$(printf "\n*SSH to $line*\n\n"
80+
ssh -t -t "$line" \
81+
'hostname;
82+
echo -ne "Git pull \t";
83+
cd /var/www/ &&
84+
git pull -q && echo "Done";
85+
exit;
86+
')
87+
88+
printf "%s\n\n" "$out"
89+
````
90+
* To prevent parallel execution, use this snippet inside your task file:
91+
````
92+
if [ "$typeExec" = "1" ]; then
93+
echo "Cannot run parallely, please set parallel to 0"
94+
exit
95+
fi
96+
````
97+
* _Error handling_: Use `&&` at the end of a line to exit if there's an error and `;` to continue even if there are errors.
98+
99+
####Create a local-only task
100+
To create a task or host-group that's only accessible on your local machine and not visible to GIT, add `@` to the task or host group and save it in the /local/ sub-directory.
101+
102+
103+
####Road-map for new features
104+
* Conditions before deploying, like running the tests locally before deploying a project
105+
* Show only error output instead of everything, so it's easier to manage deployments for more servers
106+
* Auto-predict list of hosts & tasks
107+
* Stay simple :-)
108+
* Any other ideas, folks?
5109

110+
Logo credit: `http://patorjk.com/software/taag/`

configs/my.cnf

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#
2+
# The MySQL database server configuration file.
3+
#
4+
# You can copy this to one of:
5+
# - "/etc/mysql/my.cnf" to set global options,
6+
# - "~/.my.cnf" to set user-specific options.
7+
#
8+
# One can use all long options that the program supports.
9+
# Run program with --help to get a list of available options and with
10+
# --print-defaults to see which it would actually understand and use.
11+
#
12+
# For explanations see
13+
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
14+
15+
# This will be passed to all mysql clients
16+
# It has been reported that passwords should be enclosed with ticks/quotes
17+
# escpecially if they contain "#" chars...
18+
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
19+
[client]
20+
port = 3306
21+
socket = /var/run/mysqld/mysqld.sock
22+
23+
# Here is entries for some specific programs
24+
# The following values assume you have at least 32M ram
25+
26+
# This was formally known as [safe_mysqld]. Both versions are currently parsed.
27+
[mysqld_safe]
28+
socket = /var/run/mysqld/mysqld.sock
29+
nice = 0
30+
31+
[mysqld]
32+
#
33+
# * Basic Settings
34+
#
35+
user = mysql
36+
pid-file = /var/run/mysqld/mysqld.pid
37+
socket = /var/run/mysqld/mysqld.sock
38+
port = 3306
39+
basedir = /usr
40+
datadir = /var/lib/mysql
41+
tmpdir = /tmp
42+
lc-messages-dir = /usr/share/mysql
43+
skip-external-locking
44+
#
45+
# Instead of skip-networking the default is now to listen only on
46+
# localhost which is more compatible and is not less secure.
47+
bind-address = 127.0.0.1
48+
#
49+
# * Fine Tuning
50+
#
51+
key_buffer = 16M
52+
max_allowed_packet = 16M
53+
thread_stack = 192K
54+
thread_cache_size = 8
55+
# This replaces the startup script and checks MyISAM tables if needed
56+
# the first time they are touched
57+
myisam-recover = BACKUP
58+
#max_connections = 100
59+
#table_cache = 64
60+
#thread_concurrency = 10
61+
#
62+
# * Query Cache Configuration
63+
#
64+
query_cache_limit = 1M
65+
query_cache_size = 16M
66+
#
67+
# * Logging and Replication
68+
#
69+
# Both location gets rotated by the cronjob.
70+
# Be aware that this log type is a performance killer.
71+
# As of 5.1 you can enable the log at runtime!
72+
#general_log_file = /var/log/mysql/mysql.log
73+
#general_log = 1
74+
#
75+
# Error log - should be very few entries.
76+
#
77+
log_error = /var/log/mysql/error.log
78+
#
79+
# Here you can see queries with especially long duration
80+
#log_slow_queries = /var/log/mysql/mysql-slow.log
81+
#long_query_time = 2
82+
#log-queries-not-using-indexes
83+
#
84+
# The following can be used as easy to replay backup logs or for replication.
85+
# note: if you are setting up a replication slave, see README.Debian about
86+
# other settings you may need to change.
87+
#server-id = 1
88+
#log_bin = /var/log/mysql/mysql-bin.log
89+
expire_logs_days = 10
90+
max_binlog_size = 100M
91+
#binlog_do_db = include_database_name
92+
#binlog_ignore_db = include_database_name
93+
#
94+
# * InnoDB
95+
#
96+
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
97+
# Read the manual for more InnoDB related options. There are many!
98+
#
99+
innodb_file_per_table
100+
innodb_flush_log_at_trx_commit=2
101+
#innodb_log_files_in_group = 2
102+
innodb_log_file_size = 256M
103+
innodb_file_per_table = 1
104+
innodb_file_format=BARRACUDA
105+
106+
# * Security Features
107+
#
108+
# Read the manual, too, if you want chroot!
109+
# chroot = /var/lib/mysql/
110+
#
111+
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
112+
#
113+
# ssl-ca=/etc/mysql/cacert.pem
114+
# ssl-cert=/etc/mysql/server-cert.pem
115+
# ssl-key=/etc/mysql/server-key.pem
116+
117+
118+
119+
[mysqldump]
120+
quick
121+
quote-names
122+
max_allowed_packet = 16M
123+
124+
[mysql]
125+
#no-auto-rehash # faster start of mysql but no tab completition
126+
127+
[isamchk]
128+
key_buffer = 16M
129+
130+
#
131+
# * IMPORTANT: Additional settings that can override those from this file!
132+
# The files must end with '.cnf', otherwise they'll be ignored.
133+
#
134+
!includedir /etc/mysql/conf.d/

deploy.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
echo "
3+
█████╗ ██████╗ ███████╗██████╗ ████████╗
4+
██╔══██╗██╔══██╗██╔════╝██╔══██╗╚══██╔══╝
5+
███████║██║ ██║█████╗ ██████╔╝ ██║
6+
██╔══██║██║ ██║██╔══╝ ██╔═══╝ ██║
7+
██║ ██║██████╔╝███████╗██║ ██║
8+
╚═╝ ╚═╝╚═════╝ ╚══════╝╚═╝ ╚═╝
9+
"
10+
echo "Deployment tool to run tasks on multiple SSH servers"
11+
echo "Created by Unwired Labs (https://unwiredlabs.com)"
12+
13+
#Parse commandline
14+
if (( ! "$#" > "1" )); then
15+
echo "Usage: <hosts-file> <task> <parallel>"
16+
echo "<hosts-file> mention hostname (without .txt extension) file stored in ./hosts e.g.
17+
app - for all app servers. You can also use a direct shortcut as mentioned in ~/.ssh/config"
18+
echo "<task> mention type of task . e.g.
19+
deploy - deploys
20+
init-repo - creates git repo
21+
update-config - updates config files, and restarts procs"
22+
echo "<parallel> 1 for executing parallely, 0 for sequential"
23+
exit
24+
fi
25+
26+
typeDeploy="./tasks/$2.sh"
27+
typeDeploy2="./tasks/local/$2@.sh"
28+
typeHost="./hosts/$1.txt"
29+
typeHost2="./hosts/local/$1@.txt"
30+
typeExec="$3"
31+
32+
#Switching to current directory
33+
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
34+
cd $dir
35+
36+
#Make sure the script exists
37+
if [ ! -f $typeDeploy ]; then
38+
if [ ! -f $typeDeploy2 ]; then
39+
echo "Deploy script $typeDeploy or $typeDeploy2 not found!"
40+
exit;
41+
fi
42+
typeDeploy=$typeDeploy2
43+
fi
44+
45+
#Make sure the script exists
46+
if [ ! -f $typeHost ]; then
47+
if [ ! -f $typeHost2 ]; then
48+
typeHost="$1|"
49+
echo "Host file $typeHost not found, using $typeHost as host"
50+
else
51+
typeHost=$typeHost2
52+
fi
53+
fi
54+
55+
if [ "$typeExec" = "" ] || [ "$typeExec" = "1" ]; then
56+
typeExec=1
57+
echo "***Executing parallely"
58+
else
59+
echo "***Executing sequentially"
60+
fi
61+
62+
#Read the commands
63+
cmd="`cat $typeDeploy`"
64+
65+
COUNTER=0
66+
while read -d "|" line
67+
do
68+
COUNTER=$((COUNTER + 1))
69+
if [ "$typeExec" == "1" ]; then
70+
source $typeDeploy < /dev/null &
71+
else
72+
source $typeDeploy < /dev/tty
73+
fi
74+
75+
done < "$typeHost"
76+
wait
77+
78+
exit
79+
80+
#Print output in order
81+
for i in "${outputArr[@]}"
82+
do
83+
cat $i
84+
done
85+
86+
printf "\n\nAll done!\n"

hosts/all.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
host1|host2|host3|
2+
#you HAVE to end the last SSH host with the `|` delimiter

hosts/local/.gitignore

Whitespace-only changes.

hosts/webservers.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
host1|host2|
2+
#you HAVE to end the last SSH host with the `|` delimiter
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
out=$(printf "\n\n\n SSH to *$line*\n\n\n"
2+
ssh -t -t "$line" \
3+
'hostname &&
4+
echo -ne "Installing unattended upgrades \t" &&
5+
sudo apt-get install -qq unattended-upgrades && echo "Done" &&
6+
echo -ne "Initating the unattended upgrades \t" &&
7+
echo -e "APT::Periodic::Update-Package-Lists \"1\";\nAPT::Periodic::Unattended-Upgrade \"1\";\n" > /etc/apt/apt.conf.d/20auto-upgrades && echo "Done" &&
8+
/etc/init.d/unattended-upgrades restart;
9+
exit;')
10+
printf "%s\n\n" "$out"

tasks/date.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
out=$(printf "\n\n\n SSH to *$line*\n\n\n"
2+
ssh -t -t "$line" \
3+
'hostname &&
4+
date &&
5+
exit')
6+
printf "%s\n\n" "$out"

tasks/delete-user.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#delete "test" user
2+
ssh -t -t "$line" \
3+
'hostname;
4+
echo -ne "Delete test user \t" &&
5+
sudo deluser --remove-home test && echo "Done";
6+
exit;'

0 commit comments

Comments
 (0)