Skip to content

Commit

Permalink
update to ruby 2.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
goya committed Jul 23, 2016
1 parent e5515ac commit 78ac056
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swo
*.swp
*.gem
.ruby-version
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# A sample Gemfile
source "https://rubygems.org"
ruby "2.0.0"
ruby '~>2.3'

gem "json"
gem "net-ssh"
gem 'aws-sdk', '< 2.0'
gem "net-ssh", "<3"
gem 'aws-sdk'
gem "i18n"
gem "rake"

Expand Down
51 changes: 30 additions & 21 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
GEM
remote: https://rubygems.org/
specs:
aws-sdk (1.8.3)
json (~> 1.4)
nokogiri (>= 1.4.4)
uuidtools (~> 2.1)
diff-lcs (1.1.3)
aws-sdk (2.4.2)
aws-sdk-resources (= 2.4.2)
aws-sdk-core (2.4.2)
jmespath (~> 1.0)
aws-sdk-resources (2.4.2)
aws-sdk-core (= 2.4.2)
diff-lcs (1.2.5)
i18n (0.7.0)
json (1.7.7)
net-ssh (2.6.5)
nokogiri (1.5.6)
rake (10.0.3)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.2)
uuidtools (2.1.3)
jmespath (1.3.1)
json (2.0.1)
net-ssh (2.9.4)
rake (11.2.2)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.1)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)

PLATFORMS
ruby

DEPENDENCIES
aws-sdk (< 2.0)
aws-sdk
i18n
json
net-ssh
net-ssh (< 3)
rake
rspec

RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.11.2
1.12.5
25 changes: 10 additions & 15 deletions devops.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@ files = `git ls-files`.split("\n")

Gem::Specification.new do |s|
s.name = 'devops'

s.version = Ops::version

s.date = Time.new

s.summary = "Ops tool for remote servers"
s.description = "Ops tool for remote servers"

s.authors = [ "Hardeep Shoker", "Ryan Willoughby" ]
s.description = "Ops tool for remote servers (EC2 etc.)"
s.authors = [ "Hardeep Shoker", "Ryan Willoughby", "Brett Rudd" ]
s.email = '[email protected]'
s.homepage = 'https://github.com/hardeep/ops'

s.files = files

s.licenses = "MIT"
s.required_ruby_version = '~> 2.3'
s.bindir = 'bin'
s.require_paths = ["lib"]
s.executables = [ 'ops' ]

s.add_dependency('bundler')
s.add_dependency('i18n')
s.add_dependency('aws-sdk', '< 2.0')
s.add_dependency('net-ssh', '< 3.0')
s.add_dependency('json')
s.add_dependency('rake')
s.add_dependency('bundler', '~>1.12')
s.add_dependency('i18n', '~>0.7')
s.add_dependency('aws-sdk', '~>2.4')
s.add_dependency('net-ssh', '<3')
s.add_dependency('json', '~>2.0')
s.add_dependency('rake', '~>11.2')
end
4 changes: 4 additions & 0 deletions lib/host/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def ssh_pem
def shell!( opts = nil )
ssh_cmd = [ "ssh" ]

if $config['SSH'] && $config['SSH']['verbose']
ssh_cmd << "-v"
end

raise( IOError, "Error: HostName invalid." ) if @host_name.nil?

ssh_cmd << [ "-l", @user ]
Expand Down
78 changes: 48 additions & 30 deletions lib/tasks/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,70 @@
## Sync EC2 Hosts
desc "hosts.sync"
task "sync" do
ec2 = AWS::EC2.new(
:access_key_id => $config[ "AWS" ][ "AccessKeyId" ],
:secret_access_key => $config[ "AWS" ][ "SecretAccessKey" ] )

opts = { }
instance_opts = { }
if $config[ "AWS" ]
opts[:access_key_id] = $config[ "AWS" ][ "AccessKeyId" ] if $config[ "AWS" ][ "AccessKeyId" ]
opts[:secret_access_key] = $config[ "AWS" ][ "SecretAccessKey" ] if $config[ "AWS" ][ "SecretAccessKey" ]
opts[:region] = $config[ "AWS" ][ "Region" ] if $config[ "AWS" ][ "Region" ]
instance_opts = $config[ "AWS" ][ "InstanceOpts" ] if $config[ "AWS" ][ "InstanceOpts" ]
end

ec2 = Aws::EC2::Client.new(opts)

hosts = {}

# used if no name is given
count = 0
host_count = {}

response = ec2.client.describe_instances
response = ec2.describe_instances(instance_opts)

response[:instance_index].each do | instance |
response[:reservations].each do |reservation|
reservation[:instances].each do |instance|

h = instance[1]
h = instance

next if h[:instance_state][:code] == 48 # skip if instance terminated
next if h.state.code != 16 # skip if instance not running

tags = {}
h[:tag_set].each { |tag|
tags[tag[:key]] = tag[:value]
}
tags = {}
h.tags.each { |tag|
tags[tag[:key]] = tag[:value]
}

name = tags["Name"]
name = tags["Name"]

if name.nil? || name.empty?
name = "noname-#{ count }"
count += 1
end
if name.nil? || name.empty?
name = h.private_dns_name
end

if hosts[ name ]
name = "#{name}.#{ count }"
count += 1
end
idx = host_count[ name ] || 1
host_count[ name ] = idx + 1
name = "#{name}.#{idx}"

ip = h[:private_dns_name] || "stopped"
tags['Name'] = name

ip = h[:private_dns_name]

puts "Discovered: #{ name } -> #{ ip }"

hosts[ name ] = {
"HostName" => ip,
"User" => tags["User"],
"IdentityFile" => h[:key_name],
"Tags" => tags,
"Type" => "EC2" }
hosts[ name ] = {
"HostName" => ip,
"User" => tags["User"],
"IdentityFile" => h[:key_name],
"Tags" => tags,
"Type" => "EC2"
}
end
end

hosts.each { |key|
if key[0] =~ /\.1$/
base_name = key[0][0..key[0].length-3]
count = hosts.count {|a| a[0] =~ /^#{base_name}.2/ }
key[0] = base_name if count == 0
end
puts "Discovered: #{ key[0] } -> #{ key[1]['HostName'] }"
}

puts "Synced #{hosts.count} instances"

tmp_dir = File.join( Ops::pwd_dir, 'tmp' )
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Ops

def self.version
"0.2.0"
"0.3.0"
end
end
11 changes: 8 additions & 3 deletions res/samples/default/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"IdentityLocations" : [ "~/.ssh" ],
"IdentityLocations" : [ "~/.ssh" ],
"AWS" : {
"AccessKeyId" : "",
"SecretAccessKey" : ""
}
"SecretAccessKey" : "",
"Region" : "",
"InstanceOpts": {
"filters": [{ "name": "tag:TagName", "values": ["TagValue"] }]
}
},
"SSH" : { "verbose" : true }
}
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'ops.rb'
require 'devops.rb'

0 comments on commit 78ac056

Please sign in to comment.