Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from khiemns54/feature/ssh
Browse files Browse the repository at this point in the history
Feature/ssh
  • Loading branch information
khiemns54 authored Nov 10, 2017
2 parents 44b9c35 + a0a004e commit 41135a4
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 52 deletions.
27 changes: 15 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
PATH
remote: .
specs:
rebi (0.1.8)
rebi (0.2.0)
activesupport (~> 5.0)
aws-sdk (~> 2.10)
colorize (~> 0.8)
commander (~> 4.4)
dotenv (~> 2.1)
rubyzip (~> 1.2)
subprocess (~> 1.3)

GEM
remote: https://rubygems.org/
specs:
activesupport (5.0.4)
activesupport (5.1.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
aws-sdk (2.10.9)
aws-sdk-resources (= 2.10.9)
aws-sdk-core (2.10.9)
aws-sdk (2.10.82)
aws-sdk-resources (= 2.10.82)
aws-sdk-core (2.10.82)
aws-sigv4 (~> 1.0)
jmespath (~> 1.0)
aws-sdk-resources (2.10.9)
aws-sdk-core (= 2.10.9)
aws-sigv4 (1.0.0)
aws-sdk-resources (2.10.82)
aws-sdk-core (= 2.10.82)
aws-sigv4 (1.0.2)
colorize (0.8.1)
commander (4.4.3)
highline (~> 1.7.2)
concurrent-ruby (1.0.5)
dotenv (2.1.2)
dotenv (2.2.1)
highline (1.7.8)
i18n (0.8.5)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
jmespath (1.3.1)
minitest (5.10.2)
minitest (5.10.3)
rubyzip (1.2.1)
subprocess (1.3.2)
thread_safe (0.3.6)
tzinfo (1.2.3)
tzinfo (1.2.4)
thread_safe (~> 0.1)

PLATFORMS
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Deployment tool for Elasticbeanstalk
- Multiple deployment
- Deploy source code along with updating beanstalk options
- Simple config
- Simple ssh

## Installation
Or install it yourself as:
Expand Down Expand Up @@ -62,6 +63,12 @@ $ bundle exec rebi deploy development web
$ bundle exec rebi deploy development
```

### Ssh
```bash
$ bundle exec rebi ssh development web
```


### Get envronment variables and status
```bash
# Running envronment variables
Expand All @@ -78,13 +85,22 @@ $ bundle exec rebi --help
```

### ERB in ebextensions config
Use `rebi_env` to get environment variables config in .ebextensions
Use `rebi.env` to get environment variables config in .ebextensions
```yaml
# Ex
# .ebextensions/00-envrionments.config
option_settings:
- option_name: KEY
value: <%= rebi.env[KEY] %>
```
Use `rebi.opts` or `rebi.options` to get options config in .ebextensions
```yaml
# Ex
# .ebextensions/00-envrionments.config
option_settings:
- option_name: KEY
value: <%= rebi_env[KEY] %>
value: <%= rebi.options.key %>
```

## Contributing
Expand Down
24 changes: 22 additions & 2 deletions bin/rebi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ command :deploy do |c|
c.description = 'Deploy single or multiple ElasticBeanstalk environments'
c.example 'Deploy only web environment in development', 'rebi deploy development web'
c.example 'Deploy all environments in development', 'rebi deploy development'
c.option '--source-only', 'Deploy source only'
c.option '--include-settings', 'Deploy source and settings'
c.option '--settings-only', 'Deploy option_settings and environment variables only'
c.action do |args, options|
stage, env_name = args
Expand Down Expand Up @@ -70,7 +70,7 @@ command :sample do |c|
end

command :terminate do |c|
c.syntax = 'rebi terminate stage env_name '
c.syntax = 'rebi terminate stage env_name'
c.description = 'Terminate environment'
c.action do |args, options|
stage, env_name = args
Expand All @@ -82,3 +82,23 @@ command :terminate do |c|
end
end
end

command :list do |c|
c.syntax = 'rebi list'
c.description = "List all environments"
c.action do |args, options|
Rebi.app.print_list
end
end

command :ssh do |c|
c.syntax = 'rebi ssh stage env_name'
c.description = "Ssh into instance"
c.option '-s','--select', 'Get environment variables from config'
c.action do |args, options|
stage, env_name = args
raise Rebi::Error.new("Stage cannot be nil") if stage.blank?
raise Rebi::Error.new("Env name cannot be nil") if env_name.blank?
Rebi.app.ssh_interaction stage, env_name, options.__hash__
end
end
10 changes: 10 additions & 0 deletions lib/rebi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'ostruct'
require 'thread'
require 'thwait'
require 'subprocess'

require 'rebi/erb_helper'
require 'rebi/zip_helper'
Expand All @@ -20,6 +21,7 @@
require 'rebi/config'
require 'rebi/config_environment'
require 'rebi/error'
require 'rebi/ec2'
require 'rebi/version'

Dotenv.load
Expand All @@ -38,6 +40,14 @@ def client c=nil
@@client = c || Aws::ElasticBeanstalk::Client.new
end

def ec2
@@ec2_client = Rebi::EC2.new
end

def iam
@@iam_client = Aws::IAM::Client.new
end

def app
return Rebi::Application.get_or_create_application(config.app_name)
end
Expand Down
65 changes: 61 additions & 4 deletions lib/rebi/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def environments

def deploy stage_name, env_name=nil, opts={}
return deploy_stage(stage_name, opts) if env_name.blank?
env = Rebi::Environment.new stage_name, env_name, client
env = get_environment stage_name, env_name
app_version = create_app_version env
begin
req_id = env.deploy app_version, opts
Expand Down Expand Up @@ -58,7 +58,7 @@ def print_environment_variables stage_name, env_name, from_config=false
return
end

env = Rebi::Environment.new stage_name, env_name, client
env = get_environment stage_name, env_name
env_vars = from_config ? env.config.environment_variables : env.environment_variables

Rebi.log("#{from_config ? "Config" : "Current"} environment variables", env.name)
Expand All @@ -77,7 +77,7 @@ def print_environment_status stage_name, env_name
return
end

env = Rebi::Environment.new stage_name, env_name, client
env = get_environment stage_name, env_name
env.check_created!
Rebi.log("--------- CURRENT STATUS -------------", env.name)
Rebi.log("id: #{env.id}", env.name)
Expand All @@ -87,7 +87,7 @@ def print_environment_status stage_name, env_name
end

def terminate! stage_name, env_name
env = Rebi::Environment.new stage_name, env_name, client
env = get_environment stage_name, env_name
begin
req_id = env.terminate!
ThreadsWait.all_waits(env.watch_request req_id) if req_id
Expand Down Expand Up @@ -123,6 +123,63 @@ def create_app_version env
end


def print_list
others = []
configed = Hash.new {|h, k| h[k] = {} }
environments.each do |e|
if env_conf = Rebi.config.env_by_name(e.environment_name)
configed[env_conf.stage.to_s].merge! env_conf.env_name.to_s => env_conf.name
else
others << e.environment_name
end
end

configed.each do |stg, envs|
Rebi.log "-------------"
Rebi.log "#{stg.camelize}:"
envs.each do |k, v|
Rebi.log "\t#{k.camelize}: #{v}"
end
end

if others.present?
Rebi.log "-------------"
Rebi.log "Others:"
others.each do |e|
Rebi.log "\t- #{e}"
end
end
end

def ssh_interaction stage_name, env_name, opts={}
env = get_environment stage_name, env_name
instance_ids = env.instance_ids
return if instance_ids.empty?

instance_ids.each.with_index do |i,idx|
Rebi.log "#{idx+1}) #{i}"
end

instance_id = instance_ids.first

if instance_ids.count != 1 && opts[:select]


idx = 0
while idx < 1 || idx > instance_ids.count
idx = ask_for_integer "Select an instance to ssh into:"
end
instance_id = instance_ids[idx - 1]
end

Rebi.log "Preparing to ssh into [#{instance_id}]"
env.ssh instance_id
end

def get_environment stage_name, env_name
Rebi::Environment.new stage_name, env_name, client
end

def self.client
Rebi.client || Aws::ElasticBeanstalk::Client.new
end
Expand Down
15 changes: 15 additions & 0 deletions lib/rebi/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def environment stg_name, env_name
return Rebi::ConfigEnvironment.new(stg_name, env_name, stg[env_name] || {})
end

def env_by_name name
data[:stages].each do |stg_name, stg_conf|
stg = stage stg_name
stg_conf.keys.each do |env_name|
env_conf = Rebi::ConfigEnvironment.new(stg_name, env_name, stg[env_name] || {})
return env_conf if env_conf.name == name
end
end
return nil
end

def stages
data[:stages].keys
end

private
def data
@data ||= YAML::load(ERB.new(IO.read(config_file)).result).with_indifferent_access
Expand Down
39 changes: 26 additions & 13 deletions lib/rebi/config_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class ConfigEnvironment
:instance_type,
:instance_num,
:key_name,
:service_role,
:instance_profile,
:ebextensions,
:solution_stack_name,
:cfg_file,
:env_file,
:environment_variables,
:option_settings,
:raw_conf
:raw_conf,
:options

NAMESPACE ={
app_env: "aws:elasticbeanstalk:application:environment",
Expand All @@ -39,6 +40,8 @@ class ConfigEnvironment
eb_env: [:ServiceRole],
}

DEFAULT_IAM_INSTANCE_PROFILE = "aws-elasticbeanstalk-ec2-role"

def initialize stage, env_name, env_conf={}
@raw_conf = env_conf.with_indifferent_access
@stage = stage.to_sym
Expand Down Expand Up @@ -107,8 +110,17 @@ def key_name
get_opt(ns[:autoscaling_launch], :EC2KeyName)
end

def service_role
get_opt(ns[:eb_env], :ServiceRole)
def instance_profile
get_opt(ns[:autoscaling_launch], :IamInstanceProfile)
end

def default_instance_profile?
self.instance_profile == DEFAULT_IAM_INSTANCE_PROFILE
end

def options
opts = (raw_conf[:options] || {}).with_indifferent_access
JSON.parse(opts.to_json, object_class: OpenStruct)
end

def cfg_file
Expand All @@ -133,7 +145,7 @@ def cfg
end

def solution_stack_name
@solution_stack_name ||= raw_conf[:solution_stack_name] || "64bit Amazon Linux 2017.03 v2.4.1 running Ruby 2.3 (Puma)"
@solution_stack_name ||= raw_conf[:solution_stack_name] || "64bit Amazon Linux 2017.09 v2.6.0 running Ruby 2.4 (Puma)"
end

def platform_arn
Expand Down Expand Up @@ -201,7 +213,7 @@ def option_settings
opt = set_opt_keyname opt
opt = set_opt_instance_type opt
opt = set_opt_instance_num opt
opt = set_opt_service_role opt
opt = set_opt_instance_profile opt

return @opt = opt
end
Expand Down Expand Up @@ -237,17 +249,18 @@ def set_opt_keyname opt
return opt
end

def set_opt_service_role opt
s_role = if raw_conf.key?(:service_role)
raw_conf[:service_role]
elsif role = get_raw_opt(ns[:eb_env], :ServiceRole)
def set_opt_instance_profile opt
s_role = if raw_conf.key?(:instance_profile)
raw_conf[:instance_profile]
elsif role = get_raw_opt(ns[:autoscaling_launch], :IamInstanceProfile)
role
else
'aws-elasticbeanstalk-service-role'
DEFAULT_IAM_INSTANCE_PROFILE
end

if s_role.present?
opt[ns[:eb_env]].merge!({
ServiceRole: s_role,
opt[ns[:autoscaling_launch]].merge!({
IamInstanceProfile: s_role,
}.with_indifferent_access)
end
return opt
Expand Down
Loading

0 comments on commit 41135a4

Please sign in to comment.