aws-ec2 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +11 -0
  5. data/Gemfile +1 -1
  6. data/Gemfile.lock +18 -10
  7. data/LICENSE.txt +1 -1
  8. data/README.md +74 -7
  9. data/Rakefile +1 -1
  10. data/aws-ec2.gemspec +7 -5
  11. data/lib/aws-ec2.rb +5 -2
  12. data/lib/aws_ec2/ami.rb +1 -1
  13. data/lib/aws_ec2/base.rb +34 -1
  14. data/lib/aws_ec2/cli.rb +20 -1
  15. data/lib/aws_ec2/command.rb +34 -5
  16. data/lib/aws_ec2/completer.rb +161 -0
  17. data/lib/aws_ec2/completer/script.rb +6 -0
  18. data/lib/aws_ec2/completer/script.sh +10 -0
  19. data/lib/aws_ec2/config.rb +4 -2
  20. data/lib/aws_ec2/core.rb +5 -1
  21. data/lib/aws_ec2/create.rb +11 -8
  22. data/lib/aws_ec2/create/error_messages.rb +1 -1
  23. data/lib/aws_ec2/create/params.rb +2 -6
  24. data/lib/aws_ec2/help/completion.md +22 -0
  25. data/lib/aws_ec2/help/completion_script.md +3 -0
  26. data/lib/aws_ec2/profile.rb +26 -19
  27. data/lib/aws_ec2/script.rb +1 -0
  28. data/lib/aws_ec2/script/compile.rb +15 -6
  29. data/lib/aws_ec2/script/compress.rb +62 -0
  30. data/lib/aws_ec2/script/upload.rb +75 -9
  31. data/lib/aws_ec2/setting.rb +41 -0
  32. data/lib/aws_ec2/template.rb +13 -0
  33. data/lib/aws_ec2/template/context.rb +32 -0
  34. data/lib/aws_ec2/template/helper.rb +17 -0
  35. data/lib/aws_ec2/{template_helper → template/helper}/ami_helper.rb +8 -3
  36. data/lib/aws_ec2/template/helper/core_helper.rb +88 -0
  37. data/lib/aws_ec2/{template_helper → template/helper}/partial_helper.rb +2 -2
  38. data/lib/aws_ec2/template/helper/script_helper.rb +53 -0
  39. data/lib/aws_ec2/template/helper/ssh_key_helper.rb +21 -0
  40. data/lib/aws_ec2/version.rb +1 -1
  41. data/spec/lib/cli_spec.rb +14 -0
  42. data/spec/spec_helper.rb +16 -6
  43. metadata +54 -14
  44. data/lib/aws_ec2/template_helper.rb +0 -18
  45. data/lib/aws_ec2/template_helper/core_helper.rb +0 -98
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 195f128cedbd5bf833767064b92f8f0a81fa3cc24ae131d9a932a7b714e7e3b3
4
- data.tar.gz: 408ad9efbdbcbc7008db3267cf7ed68617bb01c437ffd8d4b410dfe43dfd0516
3
+ metadata.gz: 41c5379df446fa7ebaeb8a4e7243b70c6b8ef3c91afc6a61af1ab42181695c8a
4
+ data.tar.gz: 8d4558040a99f9496bfdf74b1d43aeb470229f370ae9b14cda174a4096e83f34
5
5
  SHA512:
6
- metadata.gz: 06af930c02065bcd7a88a3c14ba72c0716738faa663c8cd59cba9900719614d7bb328b84620d41dd4823aed56333dcccf19261c9ca11c988fdcf162991bfaebf
7
- data.tar.gz: e13dd0da82d53d71ab2fe955e08ac7f1fe622981024361f1c926475159e7c4749d28a92e9ade7aa258ba92398f605a327da26b41674f29191b6857a2cf44e83b
6
+ metadata.gz: 0ce576737f3e0dc3c225c581d1507e9a9d338e048db588f7776cd2faa45e73d580b1789e43103d5e9d61298016457c4ec011e3eddf387eb14573fea292550d78
7
+ data.tar.gz: 94969a46aeb9d0e203b141dc7114df127b1bf353405c2d0574996493baaeea4d79c2863773e895d2d47b4228968c0876e94cba8e0606d628a69d893fac045524
data/.gitignore CHANGED
@@ -3,9 +3,10 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- InstalledFiles
7
6
  _yardoc
8
7
  coverage
8
+ doc/
9
+ InstalledFiles
9
10
  lib/bundler/man
10
11
  pkg
11
12
  rdoc
data/.gitmodules ADDED
File without changes
data/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [1.0.0]
7
+ - Merge pull request #1 from tongueroo/cli-template-upgrade
8
+ - Merge pull request #2 from tongueroo/render_me_pretty
9
+ - Merge pull request #3 from tongueroo/s3-upload
10
+ - Merge pull request #4 from tongueroo/layout-support
11
+ - add --randomize option
12
+ - add extract_scripts and add_ssh_key helpers
13
+ - conventionally use name of server as profile if profile exists
14
+ - introduce settings.yml
15
+ - latest_ami: exit if image cannot be found
16
+
6
17
  ## [0.9.0]
7
18
  - much improved error messaging
8
19
  - rename docs folder
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- # Specify your gem dependencies in aws_ec2.gemspec
3
+ # Specify your gem dependencies in aws-ec2.gemspec
4
4
  gemspec
5
5
 
6
6
  gem "codeclimate-test-reporter", group: :test, require: nil
data/Gemfile.lock CHANGED
@@ -1,32 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aws-ec2 (0.8.4)
4
+ aws-ec2 (0.9.0)
5
5
  activesupport
6
6
  aws-sdk-ec2
7
7
  colorize
8
8
  dotenv
9
+ filesize
9
10
  hashie
11
+ render_me_pretty
10
12
  thor
11
13
 
12
14
  GEM
13
15
  remote: https://rubygems.org/
14
16
  specs:
15
- activesupport (5.1.4)
17
+ activesupport (5.1.5)
16
18
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
19
  i18n (~> 0.7)
18
20
  minitest (~> 5.1)
19
21
  tzinfo (~> 1.1)
20
- aws-partitions (1.57.0)
21
- aws-sdk-core (3.14.0)
22
+ aws-partitions (1.67.0)
23
+ aws-sdk-core (3.17.0)
22
24
  aws-partitions (~> 1.0)
23
25
  aws-sigv4 (~> 1.0)
24
26
  jmespath (~> 1.0)
25
- aws-sdk-ec2 (1.25.0)
27
+ aws-sdk-ec2 (1.28.0)
26
28
  aws-sdk-core (~> 3)
27
29
  aws-sigv4 (~> 1.0)
28
30
  aws-sigv4 (1.0.2)
29
- byebug (9.1.0)
31
+ byebug (10.0.0)
30
32
  codeclimate-test-reporter (1.0.8)
31
33
  simplecov (<= 0.13)
32
34
  coderay (1.1.2)
@@ -35,7 +37,8 @@ GEM
35
37
  diff-lcs (1.3)
36
38
  docile (1.1.5)
37
39
  dotenv (2.2.1)
38
- ffi (1.9.18)
40
+ ffi (1.9.23)
41
+ filesize (0.1.1)
39
42
  formatador (0.2.5)
40
43
  guard (2.14.2)
41
44
  formatador (>= 0.2.4)
@@ -56,7 +59,7 @@ GEM
56
59
  guard-compat (~> 1.1)
57
60
  rspec (>= 2.99.0, < 4.0)
58
61
  hashie (3.5.7)
59
- i18n (0.9.3)
62
+ i18n (0.9.5)
60
63
  concurrent-ruby (~> 1.0)
61
64
  jmespath (1.3.1)
62
65
  json (2.1.0)
@@ -78,6 +81,10 @@ GEM
78
81
  rb-fsevent (0.10.2)
79
82
  rb-inotify (0.9.10)
80
83
  ffi (>= 0.5.0, < 2)
84
+ render_me_pretty (0.8.0)
85
+ activesupport
86
+ colorize
87
+ tilt
81
88
  rspec (3.7.0)
82
89
  rspec-core (~> 3.7.0)
83
90
  rspec-expectations (~> 3.7.0)
@@ -90,7 +97,7 @@ GEM
90
97
  rspec-mocks (3.7.0)
91
98
  diff-lcs (>= 1.2.0, < 2.0)
92
99
  rspec-support (~> 3.7.0)
93
- rspec-support (3.7.0)
100
+ rspec-support (3.7.1)
94
101
  ruby_dep (1.5.0)
95
102
  shellany (0.0.1)
96
103
  simplecov (0.13.0)
@@ -100,7 +107,8 @@ GEM
100
107
  simplecov-html (0.10.2)
101
108
  thor (0.20.0)
102
109
  thread_safe (0.3.6)
103
- tzinfo (1.2.4)
110
+ tilt (2.0.8)
111
+ tzinfo (1.2.5)
104
112
  thread_safe (~> 0.1)
105
113
 
106
114
  PLATFORMS
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Tung Nguyen
1
+ Copyright (c) 2018 Tung Nguyen
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # AWS EC2 Tool
2
2
 
3
- Simple tool to create AWS ec2 instances consistently with pre-configured settings. The pre-configured settings are stored in the profiles folder of the current project directory.
3
+ [![CircleCI](https://circleci.com/gh/tongueroo/aws-ec2.svg?style=svg)](https://circleci.com/gh/tongueroo/aws-ec2)
4
+
5
+ Tool to create AWS ec2 instances consistently with pre-configured settings. The pre-configured settings are stored in the profiles folder of the current project directory.
4
6
  Example:
5
7
 
6
8
  * profiles/default.yml: Default settings. Used when no profile is specified.
@@ -9,6 +11,7 @@ Example:
9
11
  ## Usage
10
12
 
11
13
  ```sh
14
+ aws-ec2 create NAME --profile PROFILE
12
15
  aws-ec2 create myserver --profile myserver
13
16
  ```
14
17
 
@@ -23,17 +26,40 @@ aws-ec2 create myserver --profile myserver --noop
23
26
  cat tmp/user-data.txt # to view generated user-data script
24
27
  ```
25
28
 
29
+ ## Conventional Profile Name
30
+
31
+ If there is a profile name that matches the ec2 specified instance name, you can omit the `--profile` flag. Example
32
+
33
+ ```sh
34
+ aws-ec2 create webserver --profile webserver
35
+ aws-ec2 create webserver # same thing as --profile whatever
36
+ ```
37
+
38
+ It is useful to add a random string to the end of your server name, but not use it for the `--profile` flag. Example:
39
+
40
+ ```
41
+ aws-ec2 create myserver-abc --profile myserver
42
+ aws-ec2 create myserver-123 --profile myserver
43
+ ```
44
+
45
+ You can use the `--randomize` option to do this automatically:
46
+
47
+ ```
48
+ aws-ec2 create myserver --randomize
49
+ ```
50
+
26
51
  ## Project Structure
27
52
 
28
53
  Directory | Description
29
54
  ------------- | -------------
30
55
  app/helpers | Custom helpers methods. Define them as modules and their methods are made available whenever ERB is available: `profiles`, `app/scripts`, `app/user-data` files, etc. For example, you would define a `module FooHelper` in `app/helpers/foo_helper.rb`.
31
- app/partials | Your partials that can to be included in other scripts. This is used in conjunction with the `partial` helper method.
56
+ app/partials | Your partials that can to be included in other scripts. This is used in conjunction with the `partial` helper method. With great power comes great responsibility. It is recommended to use partials sparely to keep scripts more straightforward.
32
57
  app/scripts | Where you define common scripts that can be used to configure the server. These scripts can be automatically uploaded to an s3 bucket for later downloading in your user-data script by setting the `scripts_s3_bucket` config option.
33
58
  app/user-data | Your user-data scripts that are used to bootstrap EC2 instance.
34
59
  config/[AWS_EC2_ENV].yml | The config file where you set configs that you want available in your templating logic. Examples are: `config/development.yml` and `config/production.yml`. You access the config variables with the `<%= config["var"] %>` helper.
35
- profiles | Your profile files. These files mainly contain parameters that are passed to the aws-sd2 run_instances API method.
36
- tmp | Where the generated scripts get compiled to. You can manually invoke the complilation via `aws-ec2 compile` to inspect what is generated. This is automatically done as part of the `aws-ec2` create command.
60
+ app/user-data/layouts | user-data scripts support layouts. You user-data layouts go in here.
61
+ profiles | Your profile files. These files mainly contain parameters that are passed to the aws-sdk run_instances API method.
62
+ tmp | Where the generated scripts get compiled to. You can manually invoke the compilation via `aws-ec2 compile` to inspect what is generated. This is automatically done as part of the `aws-ec2` create command.
37
63
 
38
64
  ## Helpers
39
65
 
@@ -43,7 +69,7 @@ Helper | Description
43
69
  ------------- | -------------
44
70
  user_data | Allows you to embed a generated user_data script. More details on the user-data are provided in the user data section below.
45
71
  config | Access to the variables set in config/[AWS_EC2_ENV].yml. Examples are `config/development.yml` and `config/production.yml`.
46
- latest_ami | Returns an AMI id by searching the AMI name pattern and sorting in reverse older. Example: `latest_ami("ruby-2.5.0_*")` would return the latest ruby AMIs are named with timestamps at the end like so: `ruby-2.5.0_2018-01-30-05-36-02` and `ruby-2.5.0_2018-01-29-05-36-02`.
72
+ latest_ami | Returns an AMI id by searching the AMI name pattern and sorting in reverse order. Example: `latest_ami("ruby-2.5.0_*")` would return the latest ruby AMIs are named with timestamps at the end like so: `ruby-2.5.0_2018-01-30-05-36-02` and `ruby-2.5.0_2018-01-29-05-36-02`.
47
73
  search_ami | Returns a collection of AMI image objects based on a search pattern. The query searches on the AMI name.
48
74
 
49
75
  For a full list of all the template helpers check out: [aws_ec2/template_helper](lib/aws_ec2/template_helper).
@@ -56,14 +82,14 @@ You can provide a user-data script to customize the server upon launch. The use
56
82
 
57
83
  * app/user-data/myserver.yml
58
84
 
59
- The user-data script is generated on the machine that is running the aws-ec2 command. If this is your local macosx machine, then the context of your local macosx machine is available. To see the generated user-data script, you can run the create command in noop mode and then inspect the generated script. Example:
85
+ The user-data script is generated on the machine that is running the aws-ec2 command. If this is your local macosx machine, then the context of your local macosx machine is available. To see the generated user-data script, you can run the create command in `--noop` mode and then inspect the generated script. Example:
60
86
 
61
87
  ```sh
62
88
  aws-ec2 create myserver --noop
63
89
  cat tmp/user-data.txt
64
90
  ```
65
91
 
66
- Another way to view the generated user-data scripts is the `aws-ec2 compile` command. It generates the files in the tmp folder. Example:
92
+ Another way to view the generated user-data scripts is the `aws-ec2 compile` command. It generates the files in the `tmp` folder. Example:
67
93
 
68
94
  ```
69
95
  aws-ec2 compile # generates files in tmp folder
@@ -76,6 +102,43 @@ $ grep user_data profiles/default.yml
76
102
  user_data: "<%= user_data("bootstrap") %>"
77
103
  ```
78
104
 
105
+ ### User-Data Layouts
106
+
107
+ User-data scripts support layouts. This is useful if you have common setup and finish code with your user-data scripts. Here's an example: `app/user-data/layouts/default.sh`:
108
+
109
+ ```bash
110
+ #!/bin/bash
111
+ # do some setup
112
+ <%= yield %>
113
+ # finish work
114
+ ```
115
+
116
+ And `app/user-data/box.sh`:
117
+
118
+ ```
119
+ yum install -y vim
120
+ ```
121
+
122
+ The resulting generated user-data script will be:
123
+
124
+ ```bash
125
+ #!/bin/bash
126
+ # do some setup
127
+ yum install -y vim
128
+ # finish work
129
+ ```
130
+
131
+ You can specify the layout to use when you call the `user_data` helper method in your profile. Example: `profiles/box.yml`:
132
+
133
+ ```yaml
134
+ ---
135
+ ...
136
+ user_data: <%= user_data("box.sh", layout: "mylayout" ) %>
137
+ ...
138
+ ```
139
+
140
+ If there's a `layouts/default.sh`, then it will automatically be used without having to specify the layout option. You can disable this behavior by passing in `layout: false` or by deleting the `layouts/default.sh` file.
141
+
79
142
  ### Config
80
143
 
81
144
  You can set variables in a config file and they are available when ERB is available: profiles, user-data, scripts, etc. Example `config/development.yml`:
@@ -112,6 +175,10 @@ Option | Description
112
175
  --- | ---
113
176
  scripts_s3_bucket | Set this to the bucket name where you want the generated scripts in app/scripts and app/user-data to be uploaded to. The upload sync happens right before the internal to run_instances call that launches the instance. If you need more custom logic, you can use the `before_run_instances` hook, covered in the Hooks section.
114
177
 
178
+ ### Settings
179
+
180
+ A `config/settings.yml` file controls the internal behavior of aws-ec2. It is different from config files which are meant for user defined varibles. Settings variables are for internal use.
181
+
115
182
  ### Hooks
116
183
 
117
184
  There is only one hook: `before_run_instances`. You can configure this with `config/hooks.yml`: Example:
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- task :default => :spec
4
+ task default: :spec
5
5
 
6
6
  RSpec::Core::RakeTask.new
data/aws-ec2.gemspec CHANGED
@@ -19,17 +19,19 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency "thor"
23
- spec.add_dependency "hashie"
24
- spec.add_dependency "colorize"
25
- spec.add_dependency "dotenv"
26
22
  spec.add_dependency "activesupport"
27
23
  spec.add_dependency "aws-sdk-ec2"
24
+ spec.add_dependency "colorize"
25
+ spec.add_dependency "dotenv"
26
+ spec.add_dependency "filesize"
27
+ spec.add_dependency "hashie"
28
+ spec.add_dependency "render_me_pretty"
29
+ spec.add_dependency "thor"
28
30
 
29
31
  spec.add_development_dependency "bundler"
30
32
  spec.add_development_dependency "byebug"
31
- spec.add_development_dependency "rake"
32
33
  spec.add_development_dependency "guard"
33
34
  spec.add_development_dependency "guard-bundler"
34
35
  spec.add_development_dependency "guard-rspec"
36
+ spec.add_development_dependency "rake"
35
37
  end
data/lib/aws-ec2.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $:.unshift(File.expand_path("../", __FILE__))
2
2
  require "aws_ec2/version"
3
3
  require "colorize"
4
+ require "render_me_pretty"
4
5
 
5
6
  module AwsEc2
6
7
  autoload :Help, "aws_ec2/help"
@@ -11,13 +12,15 @@ module AwsEc2
11
12
  autoload :Base, "aws_ec2/base"
12
13
  autoload :Create, "aws_ec2/create"
13
14
  autoload :Ami, "aws_ec2/ami"
14
- autoload :TemplateHelper, "aws_ec2/template_helper"
15
+ autoload :Template, "aws_ec2/template"
15
16
  autoload :Script, "aws_ec2/script"
16
17
  autoload :Config, "aws_ec2/config"
17
18
  autoload :Core, "aws_ec2/core"
18
19
  autoload :Dotenv, "aws_ec2/dotenv"
19
20
  autoload :Hook, "aws_ec2/hook"
20
-
21
+ autoload :Completion, "aws_ec2/completion"
22
+ autoload :Completer, "aws_ec2/completer"
23
+ autoload :Setting, "aws_ec2/setting"
21
24
  extend Core
22
25
  end
23
26
 
data/lib/aws_ec2/ami.rb CHANGED
@@ -3,7 +3,7 @@ module AwsEc2
3
3
  def run
4
4
  # Delegates to the Create command.
5
5
  # So we just have to set up the option for it.
6
- @options[:ami_name] = @options[:name]
6
+ @options[:ami_name] = @name
7
7
  Create.new(@options).run
8
8
  end
9
9
  end
data/lib/aws_ec2/base.rb CHANGED
@@ -1,9 +1,42 @@
1
1
  module AwsEc2
2
2
  class Base
3
+ # constants really only used by script classes
4
+ SCRIPTS_INFO_PATH = "tmp/data/scripts_info.txt"
5
+ BUILD_ROOT = "tmp"
6
+
3
7
  def initialize(options={})
4
8
  @options = options.clone
9
+ @name = randomize(@options[:name])
5
10
  AwsEc2.validate_in_project!
6
- Profile.new(@options).check!
7
11
  end
12
+
13
+ # Appends a short random string at the end of the ec2 instance name.
14
+ # Later we will strip this same random string from the name.
15
+ # Very makes it convenient. We can just type:
16
+ #
17
+ # aws-ec2 create server --randomize
18
+ #
19
+ # instead of:
20
+ #
21
+ # aws-ec2 create server-123 --profile server
22
+ #
23
+ def randomize(name)
24
+ if @options[:randomize]
25
+ random = (0...3).map { (65 + rand(26)).chr }.join.downcase # Ex: jhx
26
+ [name, random].join('-')
27
+ else
28
+ name
29
+ end
30
+ end
31
+
32
+ # Strip the random string at end of the ec2 instance name
33
+ def derandomize(name)
34
+ if @options[:randomize]
35
+ name.sub(/-(\w{3})$/,'') # strip the random part at the end
36
+ else
37
+ name
38
+ end
39
+ end
40
+
8
41
  end
9
42
  end
data/lib/aws_ec2/cli.rb CHANGED
@@ -8,6 +8,7 @@ module AwsEc2
8
8
  option :ami_name, desc: "when specified, an ami creation script is appended to the user-data script"
9
9
  option :auto_terminate, type: :boolean, default: false, desc: "automatically terminate the instance at the end of user-data"
10
10
  option :source_ami, desc: "override the source image_id in profile"
11
+ option :randomize, type: :boolean, desc: "append random characters to end of name"
11
12
  def create(name)
12
13
  Create.new(options.merge(name: name)).run
13
14
  end
@@ -21,8 +22,9 @@ module AwsEc2
21
22
 
22
23
  desc "compile", "compiles app/scripts and app/user-data to tmp folder"
23
24
  long_desc Help.text(:compile)
25
+ option :layout, default: "default", desc: "layout for user_data helper"
24
26
  def compile
25
- Script::Compile.new(options).compile
27
+ Script::Compile.new(options).compile_all
26
28
  end
27
29
 
28
30
  desc "upload", "compiles and uploads scripts to s3"
@@ -31,5 +33,22 @@ module AwsEc2
31
33
  def upload
32
34
  Script::Upload.new(options).upload
33
35
  end
36
+
37
+ desc "completion *PARAMS", "Prints words for auto-completion."
38
+ long_desc Help.text("completion")
39
+ def completion(*params)
40
+ Completer.new(CLI, *params).run
41
+ end
42
+
43
+ desc "completion_script", "Generates a script that can be eval to setup auto-completion."
44
+ long_desc Help.text("completion_script")
45
+ def completion_script
46
+ Completer::Script.generate
47
+ end
48
+
49
+ desc "version", "prints version"
50
+ def version
51
+ puts VERSION
52
+ end
34
53
  end
35
54
  end