Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tags and boot script #35

Merged
merged 2 commits into from
Feb 1, 2021
Merged

add tags and boot script #35

merged 2 commits into from
Feb 1, 2021

Conversation

brycahta
Copy link
Contributor

Issue #, if available:

  • N/A-- adding more functionality

Description of changes:

  • users can now provide their own user data/boot script and tags:
    • --boot-script or -b
    • --tags
  • added validations + unit tests
  • don't worry-- simple-ec2 tags are still added to instances/volumes
  • If users designate an auto-termination-time AND boot-script, the shutdown command will be prepended to the user's script

Testing:

  • make unit-test
  • ./build/simple-ec2 launch -i
Add filepath to instance boot script? 
format: absolute file path
[Press enter to choose default: no]
Your Choice >> /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot

Add tags to instances and persisted volumes? 
format: tag1:val1,tag2:val2
[Press enter to choose default: no]
Your Choice >> testedBy:Mee,reviewedBy:pdk

Please confirm if you would like to launch instance with following options: 

+--------------------------------------+----------------------------------------------------------------------------------------+
| Region                               | us-east-1                                                                              |
+--------------------------------------+----------------------------------------------------------------------------------------+
| VPC                                  | vpc-d295a6a8                                                                           |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Subnet                               | subnet-0f0fd0423d9a6183e                                                               |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Instance Type                        | t1.micro                                                                               |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Image                                | ami-047a51fa27710816e                                                                  |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Security Group                       | sg-819314ab                                                                            |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Keep EBS Volume(s) After Termination | false                                                                                  |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Auto Termination Timer in Minutes    | None                                                                                   |
+--------------------------------------+----------------------------------------------------------------------------------------+
| EBS Volumes                          | /dev/xvda(gp2): 8 GiB                                                                  |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Boot Script Filepath                 | /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Tag Specification                    | testedBy:Mee                                                                           |
|                                      | reviewedBy:pdk                                                                         |
+--------------------------------------+----------------------------------------------------------------------------------------+

  • >cat test-boot:
#!/bin/bash 
    echo "sudo poweroff" | at now + 5 minutes
  • ✅ user script took effect & instance shutdown as expected

  • ./build/simple-ec2 launch -b /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot-2 --tags createdBy:Bryan,reviewedBy:PdK --region us-east-1 -a 3

Please confirm if you would like to launch instance with following options: 

+--------------------------------------+------------------------------------------------------------------------------------------+
| Region                               | us-east-1                                                                                |
+--------------------------------------+------------------------------------------------------------------------------------------+
| VPC                                  | vpc-d295a6a8                                                                             |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Subnet                               | subnet-0f0fd0423d9a6183e                                                                 |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Instance Type                        | t1.micro                                                                                 |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Image                                | ami-047a51fa27710816e                                                                    |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Security Group                       | sg-819314ab                                                                              |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Keep EBS Volume(s) After Termination | false                                                                                    |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Auto Termination Timer in Minutes    | 3                                                                                        |
+--------------------------------------+------------------------------------------------------------------------------------------+
| EBS Volumes                          | /dev/xvda(gp2): 8 GiB                                                                    |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Boot Script Filepath                 | /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot-2 |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Tag Specification                    | createdBy:Bryan                                                                          |
|                                      | reviewedBy:PdK                                                                           |
+--------------------------------------+------------------------------------------------------------------------------------------+
  • >cat test-boot-2:
echo "yada yada"
echo "still testing ish"
  • ✅ user script took effect & auto-termination-timer was applied & instance shutdown as expected

  • ./build/simple-ec2 launch -h

Launch an Amazon EC2 instance with the default configurations. All configurations can be overridden by configurations provided by configuration files or user input.

Usage:
  simple-ec2 launch [flags]

Flags:
  -a, --auto-termination-timer int       The auto-termination timer for the instance in minutes
  -b, --boot-script string               The absolute filepath to a bash script passed to the instance and executed after the instance starts (user data)
  -h, --help                             help for launch
  -p, --iam-instance-profile string      The profile containing an IAM role to attach to the instance
  -m, --image-id string                  The image id of the AMI used to launch the instance
  -t, --instance-type string             The instance type of the instance
  -i, --interactive                      Interactive mode
  -k, --keep-ebs                         Keep EBS volumes after instance termination
  -l, --launch-template-id string        The launch template id with which the instance will be launched
  -v, --launch-template-version string   The launch template version with which the instance will be launched
  -r, --region string                    The region where the instance will be launched
  -c, --save-config                      Save config as a JSON config file
  -g, --security-group-ids strings       The security groups with which the instance will be launched
  -s, --subnet-id string                 The subnet id in which the instance will be launched
      --tags strings                     The tags applied to instances and volumes at launch

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@brycahta brycahta requested a review from pdk27 January 27, 2021 21:28
cmd/launch.go Outdated Show resolved Hide resolved
cmd/launch.go Outdated Show resolved Hide resolved
cmd/launch.go Show resolved Hide resolved
cmd/launch.go Outdated Show resolved Hide resolved
pkg/ec2helper/ec2helper.go Outdated Show resolved Hide resolved
pkg/ec2helper/ec2helper.go Outdated Show resolved Hide resolved
pkg/ec2helper/ec2helper.go Outdated Show resolved Hide resolved
pkg/ec2helper/ec2helper.go Outdated Show resolved Hide resolved
pkg/ec2helper/ec2helper.go Show resolved Hide resolved
pkg/config/config.go Show resolved Hide resolved
@brycahta
Copy link
Contributor Author

  • Using | as the delimiter for defining tags in interactive mode since it is not a legal tag char
  • Created a constructor for SimpleInfo now that there's a map attr

Testing

Interactive mode CX

Add tags to instances and persisted volumes? 
format: tag1|val1,tag2|val2
[Press enter to choose default: no]
Your Choice >> revdBy|PdK,createdBy:|BRYAN

Please confirm if you would like to launch instance with following options: 

+--------------------------------------+----------------------------------------------------------------------------------------+
| Region                               | us-east-1                                                                              |
+--------------------------------------+----------------------------------------------------------------------------------------+
| VPC                                  | vpc-d295a6a8                                                                           |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Subnet                               | subnet-0f0fd0423d9a6183e                                                               |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Instance Type                        | t1.micro                                                                               |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Image                                | ami-047a51fa27710816e                                                                  |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Security Group                       | sg-819314ab                                                                            |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Keep EBS Volume(s) After Termination | false                                                                                  |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Auto Termination Timer in Minutes    | None                                                                                   |
+--------------------------------------+----------------------------------------------------------------------------------------+
| EBS Volumes                          | /dev/xvda(gp2): 8 GiB                                                                  |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Boot Script Filepath                 | /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot |
+--------------------------------------+----------------------------------------------------------------------------------------+
| Tag Specification                    | revdBy | PdK                                                                           |
|                                      | createdBy: | BRYAN                                                                     |
+--------------------------------------+----------------------------------------------------------------------------------------+

  • non-Interactive mode
./build/simple-ec2 launch -b /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot-2 --tags createdBy=Bryan,reviewedBy=PdK --region us-east-1 -a 3

Please confirm if you would like to launch instance with following options: 

+--------------------------------------+------------------------------------------------------------------------------------------+
| Region                               | us-east-1                                                                                |
+--------------------------------------+------------------------------------------------------------------------------------------+
| VPC                                  | vpc-d295a6a8                                                                             |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Subnet                               | subnet-0f0fd0423d9a6183e                                                                 |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Instance Type                        | t1.micro                                                                                 |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Image                                | ami-047a51fa27710816e                                                                    |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Security Group                       | sg-819314ab                                                                              |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Keep EBS Volume(s) After Termination | false                                                                                    |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Auto Termination Timer in Minutes    | 3                                                                                        |
+--------------------------------------+------------------------------------------------------------------------------------------+
| EBS Volumes                          | /dev/xvda(gp2): 8 GiB                                                                    |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Boot Script Filepath                 | /Users/crtbry/workplace/go/src/simpleEc2_brycahta_fork_ws/aws-simple-ec2-cli/test-boot-2 |
+--------------------------------------+------------------------------------------------------------------------------------------+
| Tag Specification                    | createdBy | Bryan                                                                        |
|                                      | reviewedBy | PdK                                                                         |
+--------------------------------------+------------------------------------------------------------------------------------------+

Copy link
Contributor

@pdk27 pdk27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the changes.

Left a few minor comments.

pkg/ec2helper/ec2helper.go Outdated Show resolved Hide resolved
pkg/question/question.go Outdated Show resolved Hide resolved
cmd/launch.go Show resolved Hide resolved
data = append(data, []string{cli.ResourceUserTags, strings.Join(simpleConfig.UserTags, "\n")})
var tags []string
for k := range simpleConfig.UserTags {
tags = append(tags, fmt.Sprintf("%s | %s", k, simpleConfig.UserTags[k]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format below tag1|val1 without spaces. May be get rid of extra spaces for clarity?

Or how about adding a header to the tags slice here like tags

var tags = []string{"KEY | VALUE"}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Removing the spaces from tag1|val1
  • Your suggestion is definitely clearer, but the way table is implemented and formatted now it'd be difficult to get such a view today
  • In the mean time, I'll format it in the final confirmation to be cleaner and revisit when/if we refactor the table logic:
+--------------------------------------+--------------------------+
| Tag Specification(key|value)         | taggd|Bryan              |
|                                      | revd:|pdk                |
+--------------------------------------+--------------------------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants