aws-ec2 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dea551aeb6e2d51a45460e30e408f4a82a32f61356f2e1391bcc837081272d28
4
- data.tar.gz: f2dbabe12ceb094fba4fa6accb920e5a4bd38436ba5095dc1e26a1b53acb573c
3
+ metadata.gz: b4c5bf33bed4b038a859606fca1faf595e8fca5455f3ae4e3c07f3c2329d7c3b
4
+ data.tar.gz: f57b5c4305db3141a8f8dd1c540669ed2d666403df7d05af6a3ddb36f1d79280
5
5
  SHA512:
6
- metadata.gz: 158ca61a47b19c5ed7ebc3a81331170df07cd7c55dac58df4610948a14c2e7ead29f7f132c1654479b7b0559da220952681f950cfe0977ef1a9c7401584205f2
7
- data.tar.gz: 0e1d65584446dc61b176a75f0d16bb6f113b8574e103980e592875c500d4d002f59e2faf82e99def3eae12d62af28b8d34e1e0662729655c02c0933bfcef2174
6
+ metadata.gz: 0ab78f966ab1614ebe690b16f04d9e053803df832103542686d7697cc60af3e81ea341af447bb42c9fc181b599ac2cfdf268f665138441662a72d683e2da74bb
7
+ data.tar.gz: 787504de7fb53ae1c7656a420cbede3ae3c44844d21315b488ebd9fe6e005b06256800a6fd03d219ecd28f7e4c50192b9faad59e5ae9be1c6bf99a06d347058f
@@ -3,8 +3,13 @@
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
+ ## [0.3.0]
7
+ - Do not merge profile to default profile. This was pretty confusing usage.
8
+ - Add --ami option which result in automatically creating an ami at the end of
9
+ the user-data script.
10
+
6
11
  ## [0.2.0]
7
- - add config files support. example: config/development.yml
12
+ - Add config files support. example: config/development.yml.
8
13
 
9
14
  ## [0.1.0]
10
- - initial release
15
+ - Initial release.
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
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 directory.
4
4
  Example:
5
5
 
6
- * profiles/default.yml: default settings. Takes the lowest precedence.
7
- * profiles/myserver.yml: myserver settings get combined with the default settings
6
+ * profiles/default.yml: default settings. If there is no other matching profile.
7
+ * profiles/myserver.yml: myserver settings.
8
8
 
9
9
  ## Usage
10
10
 
@@ -14,6 +14,13 @@ aws-ec2 create myserver --profile myserver
14
14
 
15
15
  In a nutshell, the profile parameters are passed to the ruby aws-sdk [AWS::EC2::Client#run_instances](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Client.html#run_instances-instance_method) method. So you can specify any parameter you wish that is available there. To check out what a profile looks like look at the [example default](example/profiles/default.yml)
16
16
 
17
+ You can use ERB in the profile files. Some useful helper methods in the profile files are:
18
+
19
+ * user_data: a helper method that allows you to embed a generated user_data script. More details on the user_data helper are provided below.
20
+ * config:
21
+
22
+ The template helpers defined in [template_helper.rb](lib/aws_ec2/template_helper.rb).
23
+
17
24
  ### Convention
18
25
 
19
26
  By convention, the profile name matches the first parameter after the create command. So the command above could be shortened to:
@@ -34,6 +41,10 @@ The user-data script is generated on the machine that is running the aws-ec2 com
34
41
 
35
42
  To use the user-data script when creating an EC2 instance, you can use the helper method in the profile.
36
43
 
44
+ ### Config
45
+
46
+ You can set a config file and define variables in there that are available to in your profiles and user_data scripts.
47
+
37
48
  ## Noop mode
38
49
 
39
50
  You can do a test run with the `--noop` flag. This will print out what settings will be used to launch the instance.
@@ -49,20 +60,6 @@ Spot instance support natively supported by the AWS run_instances command. Simp
49
60
  * [https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateSpotMarketOptionsRequest.html](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateSpotMarketOptionsRequest.html)
50
61
  * [https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Types/SpotMarketOptions.html](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Types/SpotMarketOptions.html)
51
62
 
52
- ## Spot Fleet Support
53
-
54
- Additionally, spot fleet is supported. Launching a fleet request is slighlyt more complicated but is useful if you are okay with multiple types of instances. The spot instance profile files are stored in the profiles/spot folder. Example:
55
-
56
- * profiles/spot/default.yml: default settings. Takes the lowest precedence.
57
- * profiles/spot/myspot.yml: myspot settings get combined with the default settings
58
-
59
- Note the parameters structure of a spot fleet request is different from the parameter structure to run a single instance with the create command above. The profile parameters are passed to the ruby aws-sdk [AWS::EC2::Client#request_spot_fleet](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Client.html#request_spot_fleet-instance_method) method. So you can specify any parameter you wish that is available there.
60
-
61
- ```sh
62
- ec2 spot myspot --profile myspot
63
- ec2 spot myspot # same as above by convention
64
- ```
65
-
66
63
  ## More Help
67
64
 
68
65
  ```sh
@@ -12,6 +12,7 @@ module AwsEc2
12
12
  autoload :Spot, "aws_ec2/spot"
13
13
  autoload :TemplateHelper, "aws_ec2/template_helper"
14
14
  autoload :UserData, "aws_ec2/user_data"
15
+ autoload :Ami, "aws_ec2/ami"
15
16
  autoload :Config, "aws_ec2/config"
16
17
  autoload :Core, "aws_ec2/core"
17
18
 
@@ -0,0 +1,15 @@
1
+ module AwsEc2
2
+ class Ami
3
+ def initialize(ami_name)
4
+ @ami_name = ami_name
5
+ end
6
+
7
+ def user_data_snippet
8
+ region = `aws configure get region`.strip rescue 'us-east-1'
9
+ # the shebang line is here in case there's currently an
10
+ # empty user-data script. If there is not, then it wont hurt.
11
+ template = IO.read(File.expand_path("../scripts/ami_creation.sh", __FILE__))
12
+ ERB.new(template, nil, "-").result(binding)
13
+ end
14
+ end
15
+ end
@@ -5,6 +5,7 @@ module AwsEc2
5
5
  class_option :profile, desc: "profile name to use"
6
6
 
7
7
  desc "create NAME", "create ec2 instance"
8
+ option :ami, desc: "ami name, if specified an ami will be created at the end of user data"
8
9
  long_desc Help.text(:create)
9
10
  def create(name)
10
11
  Create.new(options.merge(name: name)).run
@@ -17,6 +18,7 @@ module AwsEc2
17
18
  end
18
19
 
19
20
  desc "userdata NAME", "displays generated userdata script"
21
+ option :ami, desc: "ami name, if specified an ami will be created at the end of user data"
20
22
  long_desc Help.text(:user_data)
21
23
  def userdata(name)
22
24
  UserData.new(options.merge(name: name)).run
@@ -1,3 +1,11 @@
1
1
  Examples:
2
2
 
3
3
  $ aws-ec2 create my-instance
4
+
5
+ If you want to create an ami at the end of of a successful user-data script run you can use the `--ami` option. Example:
6
+
7
+ $ aws-ec2 create my-instance --ami myname
8
+
9
+ To see the snippet of code that gets added to the user-data script you can use the aws-ec2 userdata command.
10
+
11
+ $ aws-ec2 userdata myscript --ami myname
@@ -3,3 +3,11 @@ Displays the generated user data script. Useful for debugging since ERB can be r
3
3
  Given a user data script in profiles/user-data/myscript.sh, run:
4
4
 
5
5
  $ aws-ec2 userdata myscript
6
+
7
+ You can have an ami creation snippet of code added to the end of the user data script with the `--ami` option.
8
+
9
+ $ aws-ec2 userdata myscript --ami myname
10
+
11
+ If you want to include a timestamp in the name you can use this:
12
+
13
+ $ aws-ec2 userdata myscript --ami '`date "+myname_%Y-%m-%d-%H-%M"`'
@@ -0,0 +1,27 @@
1
+ #!/bin/bash -exu
2
+
3
+ # Configure aws cli in case it is not yet configured
4
+ mkdir -p /home/ec2-user/.aws
5
+ if [ ! -f /home/ec2-user/.aws/config ]; then
6
+ cat >/home/ec2-user/.aws/config <<EOL
7
+ [default]
8
+ region = <%= region %>
9
+ output = json
10
+ EOL
11
+ fi
12
+
13
+ # The aws ec2 create-image command below reboots the instance.
14
+ # So before rebooting the instance, schedule a job to terminate the instance
15
+ # in 20 mins after the machine has rebooted
16
+ cat >~/terminate-myself.sh <<EOL
17
+ #!/bin/bash -exu
18
+ INSTANCE_ID=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
19
+ aws ec2 terminate-instances --instance-ids \$INSTANCE_ID
20
+ EOL
21
+ at now + 1 minutes -f ~/terminate-myself.sh
22
+
23
+ # Create AMI Bundle
24
+ AMI_NAME="<%= @ami_name %>"
25
+ INSTANCE_ID=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
26
+ REGION=$(aws configure get region)
27
+ aws ec2 create-image --name $AMI_NAME --instance-id $INSTANCE_ID --region $REGION
@@ -3,7 +3,7 @@ require "erb"
3
3
 
4
4
  module AwsEc2
5
5
  module TemplateHelper
6
- def user_data(name)
6
+ def user_data(name, base64=true)
7
7
  # allow user to specify the path also
8
8
  if File.exist?(name)
9
9
  name = File.basename(name) # normalize name, change path to name
@@ -11,8 +11,36 @@ module AwsEc2
11
11
  name = File.basename(name, '.sh')
12
12
  path = "#{root}/profiles/user-data/#{name}.sh"
13
13
  result = erb_result(path)
14
+ result = append_ami_creation(result)
14
15
 
15
- Base64.encode64(result).strip
16
+ base64 ? Base64.encode64(result).strip : result
17
+ end
18
+
19
+ # provides access to config/* settings as variables
20
+ # AWS_EC2_ENV=development => config/development.yml
21
+ # AWS_EC2_ENV=production => config/production.yml
22
+ def config
23
+ AwsEc2.config
24
+ end
25
+
26
+ # pretty timestamp that is useful for ami ids.
27
+ # the timestamp is generated once and cached.
28
+ def timestamp
29
+ @timestamp ||= Time.now.strftime("%Y-%m-%d-%H-%M-%S")
30
+ end
31
+
32
+ private
33
+ def append_ami_creation(user_data)
34
+ ami = @options[:ami]
35
+
36
+ if ami
37
+ # assuming that the user-data script is a bash script here for simplicity
38
+ # TODO: add support for other types of scripts
39
+ # might be able to do this by wrapping all scripts in cloud-init
40
+ ami_creation_snippet = AwsEc2::Ami.new(ami).user_data_snippet
41
+ user_data += ami_creation_snippet
42
+ end
43
+ user_data
16
44
  end
17
45
 
18
46
  def erb_result(path)
@@ -11,13 +11,7 @@ module AwsEc2
11
11
  end
12
12
 
13
13
  def run
14
- if File.exist?(@options[:name])
15
- filename = File.basename(@options[:name], '.sh')
16
- end
17
-
18
- filename ||= @options[:name]
19
- path = "profiles/user-data/#{filename}.sh"
20
- puts erb_result(path)
14
+ puts user_data(@options[:name], false)
21
15
  end
22
16
  end
23
17
  end
@@ -24,9 +24,9 @@ module AwsEc2
24
24
 
25
25
  params_exit_check!(profile_file, default_file)
26
26
 
27
- defaults = load_profile(default_file)
28
- params = load_profile(profile_file)
29
- params = defaults.deep_merge(params)
27
+ params = File.exist?(profile_file) ?
28
+ load_profile(profile_file) :
29
+ load_profile(default_file)
30
30
  @profile_params = params
31
31
  end
32
32
 
@@ -44,6 +44,7 @@ module AwsEc2
44
44
  puts "Using profile: #{file}"
45
45
  data = YAML.load(erb_result(file))
46
46
  data ? data : {} # in case the file is empty
47
+ data.has_key?("run_instances") ? data["run_instances"] : data
47
48
  end
48
49
 
49
50
  def profile_name
@@ -1,3 +1,3 @@
1
1
  module AwsEc2
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-ec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-22 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -162,6 +162,7 @@ files:
162
162
  - example/profiles/user-data/dev.sh
163
163
  - exe/aws-ec2
164
164
  - lib/aws-ec2.rb
165
+ - lib/aws_ec2/ami.rb
165
166
  - lib/aws_ec2/aws_services.rb
166
167
  - lib/aws_ec2/cli.rb
167
168
  - lib/aws_ec2/command.rb
@@ -172,6 +173,7 @@ files:
172
173
  - lib/aws_ec2/help/create.md
173
174
  - lib/aws_ec2/help/spot.md
174
175
  - lib/aws_ec2/help/user_data.md
176
+ - lib/aws_ec2/scripts/ami_creation.sh
175
177
  - lib/aws_ec2/spot.rb
176
178
  - lib/aws_ec2/template_helper.rb
177
179
  - lib/aws_ec2/user_data.rb