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

dev 0.4 #4

Merged
merged 2 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rebi (0.3.2)
rebi (0.4.0)
activesupport (~> 5.0)
aws-sdk-ec2 (~> 1.0)
aws-sdk-elasticbeanstalk (~> 1.0)
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ $ gem install rebi

## Usage

### Init

```bash
$ bundle exec rebi init staging web
# use profile option if you're using aws profile
```

### Yaml config
Default config file is `.rebi.yml` use `-c` to switch
```yaml
Expand Down Expand Up @@ -77,9 +84,11 @@ $ bundle exec rebi --help

### ERB in ebextensions config
Use `rebi.env` to get environment variables config in .ebextensions
config file name must end with .config.erb

```yaml
# Ex
# .ebextensions/00-envrionments.config
# .ebextensions/00-envrionments.config.erb
option_settings:
- option_name: KEY
value: <%= rebi.env[KEY] %>
Expand Down
14 changes: 13 additions & 1 deletion bin/rebi
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,22 @@ 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.option '-s','--select', 'Select instance ids'
c.action do |args, options|
stage, env_name = args
raise Rebi::Error.new("Stage cannot be nil") if stage.blank?
Rebi.app.ssh_interaction stage, env_name, options.__hash__
end
end

command :init do |c|
c.syntax = "rebi init stage_name env_name"
c.description = "Init"
c.action do |args, options|
stage, env = args
raise Rebi::Error.new("Stage cannot be nil") if stage.blank?
raise Rebi::Error.new("Env cannot be blank") if env.blank?

Rebi.init stage, env
end
end
11 changes: 9 additions & 2 deletions lib/rebi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
require 'rebi/config_environment'
require 'rebi/error'
require 'rebi/ec2'
require 'rebi/eb'
require 'rebi/init_service'
require 'rebi/version'

# Dotenv.load
Expand All @@ -43,11 +45,11 @@ def root
end

def eb c=nil
@@eb = Aws::ElasticBeanstalk::Client.new
@@eb = Rebi::EB.new
end

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

def iam
Expand All @@ -71,4 +73,9 @@ def reload!
config.reload!
end

def init stage_name, env_name
init = Rebi::InitService.new(stage_name, env_name)
init.execute
end

end
1 change: 1 addition & 0 deletions lib/rebi/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def self.create_application app_name, description=nil
end

def self.get_or_create_application app_name
raise "App name cannot be nil" if app_name.blank?
begin
return get_application app_name
rescue Error::ApplicationNotFound
Expand Down
27 changes: 22 additions & 5 deletions lib/rebi/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Rebi
class Config
include Singleton

attr_accessor :data
def initialize
reload!
end
Expand Down Expand Up @@ -41,12 +42,16 @@ def app_name
data[:app_name]
end

def app_name=name
data[:app_name] = name
end

def app_description
data[:app_description] || "Created via rebi"
end

def stage stage
data[:stages][stage] || raise(Rebi::Error::ConfigNotFound.new("Stage: #{stage}"))
data[:stages] && data[:stages][stage] || raise(Rebi::Error::ConfigNotFound.new("Stage: #{stage}"))
end

def timeout
Expand Down Expand Up @@ -74,14 +79,26 @@ def stages
data[:stages].keys
end

private
def data
@data ||= YAML::load(ERB.new(IO.read(config_file)).result).with_indifferent_access
raise Rebi::Error::ConfigInvalid.new("app_name cannot be nil") if @data[:app_name].blank?
raise Rebi::Error::ConfigInvalid.new("stages cannot be nil") if @data[:stages].blank?
return @data unless @data.nil?
begin
@data = YAML::load(ERB.new(IO.read(config_file)).result).with_indifferent_access
rescue Errno::ENOENT
@data = {}.with_indifferent_access
end
return @data
end

def push_to_file
File.open(config_file, "wb") do |f|
f.write JSON.parse(data.to_json).to_yaml
end

Rebi.log "Saved config to #{config_file}"
Rebi.log "For more configs, please refer sample or github"
end

private
def set_aws_config
conf = {}

Expand Down
25 changes: 23 additions & 2 deletions lib/rebi/config_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ class ConfigEnvironment

DEFAULT_IAM_INSTANCE_PROFILE = "aws-elasticbeanstalk-ec2-role"


DEFAULT_CONFIG = {
tier: "web",
instance_type: "t2.micro",
instance_num: {
min: 1,
max: 1,
}
}

def initialize stage, env_name, env_conf={}
@raw_conf = env_conf.with_indifferent_access
@stage = stage.to_sym
Expand All @@ -52,6 +62,11 @@ def name
@name ||= raw_conf[:name] || "#{env_name}-#{stage}"
end


def name=n
raw_conf[:name] ||= @name = n
end

def app_name
Rebi.config.app_name
end
Expand All @@ -71,7 +86,7 @@ def tier
elsif cfg.present? && cfg[:EnvironmentTier].present? && cfg[:EnvironmentTier][:Name].present?
cfg[:EnvironmentTier][:Name] == "Worker" ? :worker : :web
else
:web
DEFAULT_CONFIG[:tier].to_sym
end

@tier = if t == :web
Expand Down Expand Up @@ -128,6 +143,7 @@ def options
end

def cfg_file
return nil
@cfg_file ||= raw_conf[:cfg_file]
return @cfg_file if @cfg_file.blank?
return @cfg_file if Pathname.new(@cfg_file).absolute?
Expand All @@ -153,7 +169,7 @@ def cfg
end

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

def platform_arn
Expand All @@ -170,6 +186,11 @@ def ebextensions
end
end

def ebignore
return @ebignore ||= raw_conf[:ebignore] || ".ebignore"
end


def raw_environment_variables
raw_conf[:environment_variables] || {}
end
Expand Down
55 changes: 55 additions & 0 deletions lib/rebi/eb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Rebi::EB

attr_reader :client
def initialize client=Aws::ElasticBeanstalk::Client.new
@client = client
end

def applications
client.describe_applications.applications.map(&:application_name)
end

def solution_stacks
@solution_stacks = client.list_available_solution_stacks.solution_stacks.map do |s|
stacks_from_string s
end
end

def platforms
@platforms ||= solution_stacks.map do |st|
st["platform"]
end.uniq
end

def versions_by_platform platform
raise "Invalid platform" unless platforms.include?(platform)

solution_stacks.select do |st|
st["platform"] == platform
end.map do |st|
st["version"]
end.uniq
end

def get_solution_stack platform, version
solution_stacks.find do |st|
st["platform"] == platform && st["version"] == version
end["solution_stack"]
end

def method_missing(m, *args, &block)
client.send(m, *args, &block)
end

private

def stacks_from_string s
res = {}.with_indifferent_access
res[:platform] = s.match('.+running\s([^0-9]+).*')&.captures&.first.try(:strip)
res[:version] = s.match('.+running\s(.*)')&.captures&.first.try(:strip)
res[:server] = s.match('(.*)\srunning\s.*')&.captures&.first.try(:strip)
res[:stack_version] = s.match('.+v([0-9.]+)\srunning\s.*')&.captures&.first.try(:strip)
res[:solution_stack] = s
res
end
end
2 changes: 1 addition & 1 deletion lib/rebi/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class EC2

attr_reader :client

def initialize client
def initialize client=Aws::EC2::Client.new
@client = client
end

Expand Down
15 changes: 12 additions & 3 deletions lib/rebi/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def instance_ids
def create_app_version opts={}
return if opts[:settings_only]
start = Time.now.utc
source_bundle = Rebi::ZipHelper.new.gen(self.config, opts)
source_bundle = Rebi::ZipHelper.new(self.config, opts).gen
version_label = source_bundle[:label]
key = "#{app_name}/#{version_label}.zip"
log("Uploading source bundle: #{version_label}.zip")
Expand Down Expand Up @@ -303,8 +303,17 @@ def ssh instance_id

Rebi.ec2.authorize_ssh instance_id do
user = "ec2-user"
key_file = "~/.ssh/#{instance.key_name}.pem"
raise Rebi::Error::KeyFileNotFound unless File.exists? File.expand_path(key_file)
key_files = [
"~/.ssh/#{instance.key_name}.pem",
"~/.ssh/#{instance.key_name}",
]

key_file = key_files.find do |f|
File.exists? File.expand_path(f)
end

raise Rebi::Error::KeyFileNotFound unless key_file.present?

cmd = "ssh -i #{key_file} #{user}@#{instance.public_ip_address}"
log cmd

Expand Down
Loading