Skip to content

Commit db988e9

Browse files
committed
Initial import
0 parents  commit db988e9

13 files changed

+867
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/bin/jp.php
3+
/.idea

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Soisy
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Description
2+
Codedeployer is a small library that helps deploying any application or codebase to groups of ec2 instances through AWS Codedeploy.
3+
The deploy process needs pre-configuration on the AWS side, so ask your nearest ops.
4+
5+
The deployment works like this:
6+
- an archive is created containing the codebase and a special `appspec.yml` file needed by Codedeploy
7+
- this archive is uploaded to S3
8+
- a series of calls is made to the Codedeploy API to initiate deployments
9+
- an agent installed on all supported instances polls Codedeploy waiting for deployment instructions
10+
- when the agent receives a deployment instruction, it downloads the archive from S3 and executes the steps defined in the `appspec.yml` file
11+
12+
This process shifts the deployment from a push system like Idephix or Deployer that requires ssh access to machines in order to rsync the code, to a poll system where the instances are notified of a new deployment request by the agent and download the application archive, running all the configured scripts to complete the deployment.
13+
14+
## Installation and usage
15+
1. Run `composer require mriva/codedeployer`
16+
2. Run `bin/deploy --setup`
17+
3. Create directories named as deployment groups
18+
4. Add scripts inside deployment groups directories
19+
5. Compile `config.php`
20+
6. Run `bin/deploy`
21+
22+
**Point 1** and **2** are self explanatory.
23+
24+
**Point 3** requires you to create a directory under `deploy/hook-script` for each instance group you want to deploy to.
25+
For example, if you wish to deploy to `ec2-alfa` and `ec2-beta` instance groups, the tree will look like this:
26+
27+
```
28+
deploy/
29+
└── hook-scripts/
30+
├── ec2-alfa/
31+
└── ec2-beta/
32+
```
33+
34+
**Point 4** requires you to populate the directories from point 3.
35+
AWS Codedeploy offers various hooks during the process, for simplicity the default setup only uses `AfterInstall` but other hooks can be easily added to `appspec.yml` if needed.
36+
37+
`AfterInstall` is the first hook available and is run after the agent has downloaded and extracted the revision archive to a temporary directory;
38+
it is used to actually copy all the code to the real target directory and execute any post-install script that might be needed (ie. clearing cache, recreating snapshots, etc).
39+
40+
For more information on AWS Codedeploy hooks see: https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html#appspec-hooks-server
41+
42+
Now add scripts in those directories named after these hooks, so assuming you go for the easy version with just `AfterInstall`, your tree should look like this:
43+
44+
```
45+
deploy/
46+
└── hook-scripts/
47+
├── ec2-alfa/
48+
│  └── AfterInstall.sh
49+
└── ec2-beta/
50+
└── AfterInstall.sh
51+
52+
```
53+
54+
these script will be run by the main `hook-wrapper.sh` which is defined as the main hook in `appspec.yml`
55+
56+
There are two example scripts ready to be used with minimal configuration for simple copy and rotating releases deploy in the `config/hook-scripts/example-scripts/` directory.
57+
58+
**Point 5** only requires you to compile a few application related options, the file should be self explanatory as well.
59+
60+
## TODO
61+
- refactor config.php with some other form of configuration
62+
- write Codedeploy new activation (app + groups) through CLI skipping web console
63+
- sanity checks

bin/deploy

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Codedeployer\Deploy;
5+
use Composer\Autoload\ClassLoader;
6+
7+
$file = __DIR__ . '/../../../autoload.php';
8+
9+
if (file_exists($file)) {
10+
define('CODEDEPLOYER_COMPOSER_INSTALL', $file);
11+
}
12+
13+
if (!defined('CODEDEPLOYER_COMPOSER_INSTALL')) {
14+
fwrite(STDERR, 'You need to set up the project dependencies using Composer');
15+
16+
die(1);
17+
}
18+
19+
require CODEDEPLOYER_COMPOSER_INSTALL;
20+
21+
$reflection = new \ReflectionClass(ClassLoader::class);
22+
$rootDir = dirname($reflection->getFileName(), 3);
23+
24+
if (isset($argv[1]) && $argv[1] == '--setup') {
25+
$packageDir = dirname(__DIR__, 1);
26+
27+
$deployDir = $rootDir . '/deploy/';
28+
29+
if (!is_dir($deployDir)) {
30+
mkdir($deployDir);
31+
}
32+
33+
exec("cp -a {$packageDir}/config/* {$deployDir}");
34+
35+
echo "Config files successfully installed\n";
36+
exit(0);
37+
}
38+
39+
$deployer = new Deploy();
40+
$deployer->run($rootDir);

composer.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "soisy/codedeployer",
3+
"description": "Deploy tool interacting with AWS Codedeploy",
4+
"type": "library",
5+
"keywords": ["soisy", "aws", "codedeploy", "deploy"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Matteo Riva",
10+
"email": "[email protected]",
11+
"role": "Developer"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0",
16+
"aws/aws-sdk-php": "^3.100"
17+
},
18+
"bin": [
19+
"bin/deploy"
20+
],
21+
"config": {
22+
"bin-dir": "bin"
23+
},
24+
"autoload": {
25+
"psr-0": {
26+
"Codedeployer": "src/"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)