Skip to content

Commit

Permalink
Sync CodeDeploy host agent to Github
Browse files Browse the repository at this point in the history
  • Loading branch information
feverLu committed Jul 7, 2015
1 parent de28905 commit 748de99
Show file tree
Hide file tree
Showing 114 changed files with 6,628 additions and 6,397 deletions.
7 changes: 3 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
#disable_system_gems

source 'http://rubygems.org'

# our dependencies
gem 'jamespath'
gem 'json'
gem 'json_pure'
gem 'gli'
gem 'aws-sdk-core'
gem 'codedeploy-commands'
gem 'rubyzip'
gem 'httpclient'
gem 'rake'
gem 'archive-tar-minitar'
gem 'logging'

group :development do
# this doesn't need to be a global or even a standard dependency
Expand Down
162 changes: 125 additions & 37 deletions bin/install
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/usr/bin/ruby2.0
#!/usr/bin/ruby

##################################################################
# This part of the code might be running on Ruby versions other
# than 2.0. Testing on multiple Ruby versions is required for
# changes to this part of the code.
##################################################################

class Proxy
instance_methods.each do |m|
Expand All @@ -13,7 +19,7 @@ class Proxy

def method_missing(name, *args, &block)
@targets.map do |target|
target.public_send(name, *args, &block)
target.__send__(name, *args, &block)
end
end
end
Expand All @@ -36,19 +42,17 @@ end
@log.level = Logger::INFO

begin
# use the gems bundled with the agent if present
Gem.use_paths(nil, Gem.path << "/opt/codedeploy-agent/vendor")

require 'fileutils'
require 'openssl'
require 'open-uri'
require 'uri'
require 'json'
require 'getoptlong'

def usage
print <<EOF
install <package-type>
install [--sanity-check] <package-type>
--sanity-check [optional]
package-type: 'rpm', 'deb', or 'auto'
Installs fetches the latest package version of the specified type and
Expand All @@ -64,9 +68,105 @@ are detected the automatic detection will abort
When using the automatic setup, if the system has apt-get but not gdebi,
the gdebi will be installed using apt-get first.
If --sanity-check is specified, the install script will wait for 3 minutes post installation
to check for a running agent.
This install script needs Ruby version 2.0.x installed as a prerequisite.
If you do not have Ruby version 2.0.x installed, please install it first.
EOF
end

# check ruby version, only version 2.0.x works
def check_ruby_version_and_symlink
@log.info("Starting Ruby version check.")
actual_ruby_version = RUBY_VERSION.split('.').map{|s|s.to_i}
left_bound = '2.0.0'.split('.').map{|s|s.to_i}
right_bound = '2.1.0'.split('.').map{|s|s.to_i}
if !(File.exist?('/usr/bin/ruby2.0'))
if (File.symlink?('/usr/bin/ruby2.0'))
@log.error("The symlink /usr/bin/ruby2.0 already exists, but it's linked to a non-existent directory or executable file.")
exit(1)

# The spaceship operator is a rarely used Ruby feature - particularly how it interacts with arrays.
# Not all languages that have it handle that case the same way.
elsif ((actual_ruby_version <=> left_bound) < 0 || (actual_ruby_version <=> right_bound) >= 0)
@log.error("Current running Ruby version for "+ENV['USER']+" is "+RUBY_VERSION+", but Ruby version 2.0.x needs to be installed.")
@log.error('If you have Ruby version 2.0.x installed for other users, please create a symlink to /usr/bin/ruby2.0.')
@log.error("Otherwise please install Ruby 2.0.x for "+ENV['USER']+" user.")
exit(1)
else
ruby_interpreter_path = File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["RUBY_INSTALL_NAME"] + RbConfig::CONFIG["EXEEXT"])
File.symlink(ruby_interpreter_path, '/usr/bin/ruby2.0')
end
end
end

def parse_args()
if (ARGV.length > 4)
usage
@log.error('Too many arguments.')
exit(1)
elsif (ARGV.length < 1)
usage
@log.error('Expected package type as argument.')
exit(1)
end

@sanity_check = false
@reexeced = false

@args = Array.new(ARGV)
opts = GetoptLong.new(['--sanity-check', GetoptLong::NO_ARGUMENT], ['--help', GetoptLong::NO_ARGUMENT], ['--re-execed', GetoptLong::NO_ARGUMENT])
opts.each do |opt, args|
case opt
when '--sanity-check'
@sanity_check = true
when '--help'
usage
when '--re-execed'
@reexeced = true
end
end
if (ARGV.length < 1)
usage
@log.error('Expected package type as argument.')
exit(1)
end
@type = ARGV.shift.downcase;
end

def force_ruby20()
# change interpreter when symlink /usr/bin/ruby2.0 exists, but running with lower ruby version
actual_ruby_version = RUBY_VERSION.split('.').map{|s|s.to_i}
left_bound = '2.0.0'.split('.').map{|s|s.to_i}
right_bound = '2.1.0'.split('.').map{|s|s.to_i}
if (actual_ruby_version <=> left_bound) < 0
if(!@reexeced)
@log.info("The current Ruby version is not 2.0.x! Restarting the installer with /usr/bin/ruby2.0")
exec('/usr/bin/ruby2.0', __FILE__, '--re-execed' , *@args)
else
@log.error('The Ruby version in /usr/bin/ruby2.0 is '+RUBY_VERSION+', but this must be Ruby version 2.0.x. Installation cannot continue.')
@log.error('If you have Ruby version 2.0.x installed, please correct the symlink. Otherwise, please install Ruby 2.0')
exit(1)
end
elsif (actual_ruby_version <=> right_bound) >= 0
@log.warn('The Ruby version in /usr/bin/ruby2.0 is '+RUBY_VERSION+', but this must be Ruby version 2.0.x. Attempting to install anyway.')
@log.warn('If you have Ruby version 2.0.x installed, please correct the symlink. Otherwise, please install Ruby 2.0')
end
end

if (Process.uid != 0)
@log.error('Must run as root to install packages')
exit(1)
end

parse_args()

########## Force running as Ruby 2.0 or fail here ##########
check_ruby_version_and_symlink()
force_ruby20()

def run_command(*args)
exit_ok = system(*args)
$stdout.flush
Expand All @@ -77,18 +177,19 @@ EOF

def get_ec2_metadata_region
begin
uri = URI.parse('http://169.254.169.254/latest/meta-data/placement/availability-zone')
az = uri.read(:read_timeout => 120)
az.strip
rescue OpenURI::HTTPError => e
uri = URI.parse('http://169.254.169.254/latest/meta-data/placement/availability-zone')
az = uri.read(:read_timeout => 120)
az.strip
rescue
@log.warn("Could not get region from EC2 metadata service at '#{uri.to_s}'")
return nil
end

if (az !~ /[a-z]{2}-[a-z]+-\d+[a-z]/)
@log.warn("Invalid availability zone name: '#{az}'.")
nil
return nil
else
az.chop
return az.chop
end
end

Expand Down Expand Up @@ -137,6 +238,8 @@ EOF
uri = get_s3_uri(region, bucket, key)

begin
require 'json'

version_string = uri.read(:ssl_verify_mode => OpenSSL::SSL::VERIFY_PEER, :redirect => true, :read_timeout => 120)
JSON.parse(version_string)
rescue OpenURI::HTTPError => e
Expand Down Expand Up @@ -166,34 +269,19 @@ EOF
end

def do_sanity_check(cmd)
@log.info("Waiting for a while before I check for a running agent")
sleep(3 * 60)
res = run_command(cmd, 'codedeploy-agent', 'status')
if (res.nil? || res == false)
@log.info("No codedeploy agent seems to be running. Starting the agent.")
run_command(cmd, 'codedeploy-agent', 'start-no-update')
if @sanity_check
@log.info("Waiting for 3 minutes before I check for a running agent")
sleep(3 * 60)
res = run_command(cmd, 'codedeploy-agent', 'status')
if (res.nil? || res == false)
@log.info("No codedeploy agent seems to be running. Starting the agent.")
run_command(cmd, 'codedeploy-agent', 'start-no-update')
end
end
end

if (Process.uid != 0)
@log.error('Must run as root to install packages')
exit(1)
end

@log.info("Starting update check.")

if (ARGV.length > 1)
usage
@log.error('Too many arguments.')
exit(1)
elsif (ARGV.length < 1)
usage
@log.error('Expected package type as argument.')
exit(1)
end

@type = ARGV[0].downcase;

if (@type == 'auto')
@log.info('Attempting to automatically detect supported package manager type for system...')

Expand Down Expand Up @@ -243,7 +331,7 @@ EOF
when 'deb'
#use -n for non-interactive mode
#use -o to not overwrite config files unless they have not been changed
install_cmd = ['/usr/bin/gdebi', '-n', '-o', 'Dpkg::Options::="--force-confdef"', '-o', 'Dpg::Options::="--force-conffold"']
install_cmd = ['/usr/bin/gdebi', '-n', '-o', 'Dpkg::Options::=--force-confdef', '-o', 'Dpg::Options::=--force-confold']
install_from_s3(region, bucket, version_file_key, @type, install_cmd)
do_sanity_check('/usr/sbin/service')
when 'zypper'
Expand Down
30 changes: 30 additions & 0 deletions certs/host-agent-deployment-signer-ca-chain.pem
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,33 @@ CSqGSIb3DQEBBQUAA4GBABByUqkFFBkyCEHwxWsKzH4PIRnN5GfcX6kb5sroc50i
2JhucwNhkcV8sEVAbkSdjbCxlnRhLQ2pRdKkkirWmnWXbj9T/UWZYB2oK0z5XqcJ
2HUw19JlYD1n1khVdWk/kfVIC0dpImmClr7JyDiGSnoscxlIaU5rfGW/D/xwzoiQ
-----END CERTI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Iu
Y29tL3BjYTMtZzUuY3JsMA4GA1UdDwEB/wQEAwIBBjAvBggrBgEFBQcBAQQjMCEw
HwYIKwYBBQUHMAGGE2h0dHA6Ly9zMi5zeW1jYi5jb20wawYDVR0gBGQwYjBgBgpg
hkgBhvhFAQc2MFIwJgYIKwYBBQUHAgEWGmh0dHA6Ly93d3cuc3ltYXV0aC5jb20v
Y3BzMCgGCCsGAQUFBwICMBwaGmh0dHA6Ly93d3cuc3ltYXV0aC5jb20vcnBhMCkG
A1UdEQQiMCCkHjAcMRowGAYDVQQDExFTeW1hbnRlY1BLSS0xLTUzNDAdBgNVHQ4E
FgQUX2DPYZBV34RDFIpgKrL1evRDGO8wHwYDVR0jBBgwFoAUf9Nlp8Ld7LvwMAnz
Qzn6Aq8zMTMwDQYJKoZIhvcNAQELBQADggEBAF6UVkndji1l9cE2UbYD49qecxny
H1mrWH5sJgUs+oHXXCMXIiw3k/eG7IXmsKP9H+IyqEVv4dn7ua/ScKAyQmW/hP4W
Ko8/xabWo5N9Q+l0IZE1KPRj6S7t9/Vcf0uatSDpCr3gRRAMFJSaXaXjS5HoJJtG
QGX0InLNmfiIEfXzf+YzguaoxX7+0AjiJVgIcWjmzaLmFN5OUiQt/eV5E1PnXi8t
TRttQBVSK/eHiXgSgW7ZTaoteNTCLD0IX4eRnh8OsN4wUmSGiaqdZpwOdgyA8nTY
Kvi4Os7X1g8RvmurFPW9QaAiY4nxug9vKWNmLT+sjHLF+8fk1A/yO0+MKcc=
-----END CERTIFICATE-----
1 change: 0 additions & 1 deletion codedeploy_agent-1.1.0.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ Gem::Specification.new do |spec|
spec.add_dependency('rake', '~> 0.9')
spec.add_dependency('logging', '~>1.8')
spec.add_dependency('aws-sdk-core', '~>2.0')
spec.add_dependency('httpclient', '~>2.3')
end
1 change: 0 additions & 1 deletion conf/codedeployagent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
:program_name: codedeploy-agent
:root_dir: '/opt/codedeploy-agent/deployment-root'
:verbose: false
:wait_after_error: 1
:wait_between_runs: 1
21 changes: 1 addition & 20 deletions lib/instance_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
require 'core_ext'

require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

if defined?(Bundler)
Bundler.require(:default)
end

require 'process_manager'

Expand All @@ -20,20 +13,8 @@
require 'instance_agent/log'
require 'instance_agent/platform'
require 'instance_agent/platform/linux_util'
require 'instance_agent/agent/plugin'
require 'instance_agent/agent/base'
require 'instance_agent/codedeploy_plugin/command_poller'
require 'instance_agent/codedeploy_plugin/command_executor'
require 'instance_agent/codedeploy_plugin/deployment_specification'
require 'instance_agent/codedeploy_plugin/application_specification/application_specification'
require 'instance_agent/codedeploy_plugin/application_specification/file_info'
require 'instance_agent/codedeploy_plugin/application_specification/script_info'
require 'instance_agent/codedeploy_plugin/application_specification/linux_permission_info'
require 'instance_agent/codedeploy_plugin/application_specification/mode_info'
require 'instance_agent/codedeploy_plugin/application_specification/acl_info'
require 'instance_agent/codedeploy_plugin/application_specification/ace_info'
require 'instance_agent/codedeploy_plugin/application_specification/context_info'
require 'instance_agent/codedeploy_plugin/application_specification/range_info'
require 'instance_agent/codedeploy_plugin/install_instruction'
require 'instance_agent/runner/master'
require 'instance_agent/runner/child'
end
Expand Down
Loading

0 comments on commit 748de99

Please sign in to comment.