ec2launcher 1.0.32 → 1.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.0.33
2
+
3
+ * Refactored config, application and environment loading code to allow easier reuse from other projects.
4
+
1
5
  ## 1.0.32
2
6
 
3
7
  * Copied Alestic's runurl script to avoid problems with LaunchPad is down. Switched to using ec2launcher's copy of runurl.
data/lib/ec2launcher.rb CHANGED
@@ -19,6 +19,8 @@ require 'ec2launcher/instance_paths_config'
19
19
  require 'ec2launcher/block_device_builder'
20
20
  require 'ec2launcher/hostname_generator'
21
21
 
22
+ require 'ec2launcher/config_wrapper'
23
+
22
24
  include Log4r
23
25
 
24
26
  module EC2Launcher
@@ -57,54 +59,11 @@ module EC2Launcher
57
59
  end
58
60
 
59
61
  # Load configuration data
60
- @config = load_config_file
61
-
62
- environments_directories = process_directory_list(@config.environments, "environments", "Environments", false)
63
- applications_directories = process_directory_list(@config.applications, "applications", "Applications", true)
64
-
65
- # Load other environments
66
- @environments = { }
67
- environments_directories.each do |env_dir|
68
- Dir.entries(env_dir).each do |env_name|
69
- filename = File.join(env_dir, env_name)
70
- next if File.directory?(filename)
62
+ config_wrapper = ConfigWrapper.new(@options.directory)
71
63
 
72
- new_env = load_environment_file(filename)
73
- validate_environment(filename, new_env)
74
-
75
- @environments[new_env.name] = new_env
76
- new_env.aliases.each {|env_alias| @environments[env_alias] = new_env }
77
- end
78
- end
79
-
80
- # Load applications
81
- @applications = {}
82
- applications_directories.each do |app_dir|
83
- Dir.entries(app_dir).each do |application_name|
84
- filename = File.join(app_dir, application_name)
85
- next if File.directory?(filename)
86
-
87
- apps = EC2Launcher::DSL::ApplicationDSL.execute(File.read(filename)).applications
88
- apps.each do |new_application|
89
- @applications[new_application.name] = new_application
90
- validate_application(filename, new_application)
91
- end
92
- end
93
- end
94
-
95
- # Process inheritance rules for environments
96
- @environments.values.each do |env|
97
- new_env = process_environment_inheritance(env)
98
- @environments[new_env.name] = new_env
99
- end
100
-
101
- # Process inheritance rules for applications
102
- @applications.values.each do |app|
103
- next if app.inherit.nil?
104
-
105
- new_app = process_application_inheritance(app)
106
- @applications[new_app.name] = new_app
107
- end
64
+ @config = config_wrapper.config
65
+ @environments = config_wrapper.environments
66
+ @applications = config_wrapper.applications
108
67
 
109
68
  if @options.list
110
69
  puts ""
@@ -452,23 +411,6 @@ module EC2Launcher
452
411
  end
453
412
  end
454
413
 
455
- # Given a list of possible directories, build a list of directories that actually exist.
456
- #
457
- # @param [Array<String>] directories list of possible directories
458
- # @return [Array<String>] directories that exist or an empty array if none of the directories exist.
459
- #
460
- def build_list_of_valid_directories(directories)
461
- dirs = []
462
- unless directories.nil?
463
- if directories.kind_of? Array
464
- directories.each {|d| dirs << d if File.directory?(d) }
465
- else
466
- dirs << directories if File.directory?(directories)
467
- end
468
- end
469
- dirs
470
- end
471
-
472
414
  # Searches for the most recent AMI matching the criteria.
473
415
  #
474
416
  # @param [String] arch system archicture, `i386` or `x86_64`
@@ -640,103 +582,6 @@ module EC2Launcher
640
582
  new_instance
641
583
  end
642
584
 
643
- # Read in the configuration file stored in the workspace directory.
644
- # By default this will be './config.rb'.
645
- #
646
- # @return [EC2Launcher::Config] the parsed configuration object
647
- def load_config_file()
648
- # Load configuration file
649
- config_filename = File.join(@options.directory, "config.rb")
650
- abort("Unable to find 'config.rb' in '#{@options.directory}'") unless File.exists?(config_filename)
651
- EC2Launcher::DSL::ConfigDSL.execute(File.read(config_filename)).config
652
- end
653
-
654
- # Load and parse an environment file
655
- #
656
- # @param [String] name full pathname of the environment file to load
657
- # @param [EC2Launcher::Environment, nil] default_environment the default environment,
658
- # which will be used as the base for the new environment. Optional.
659
- # @param [Boolean] fail_on_missing print an error and exit if the file does not exist.
660
- #
661
- # @return [EC2Launcher::Environment] the new environment loaded from the specified file.
662
- #
663
- def load_environment_file(name, fail_on_missing = false)
664
- unless File.exists?(name)
665
- abort("Unable to read environment: #{name}") if fail_on_missing
666
- return nil
667
- end
668
-
669
- load_env = EC2Launcher::DSL::Environment.new
670
- load_env.load(File.read(name))
671
- load_env
672
- end
673
-
674
- # Attempts to build a list of valid directories.
675
- #
676
- # @param [Array<String>, nil] target_directories list of possible directories
677
- # @param [String] default_directory directory to use if the target_directories list is empty or nil
678
- # @param [String] name name of the type of directory. Used only for error messages.
679
- # @param [Boolean] fail_on_error exit with an error if the list of valid directories is empty
680
- #
681
- # @return [Array<String] list of directories that exist
682
- #
683
- def process_directory_list(target_directories, default_directory, name, fail_on_error = false)
684
- dirs = []
685
- if target_directories.nil?
686
- dirs << File.join(@options.directory, default_directory)
687
- else
688
- target_directories.each {|d| dirs << File.join(@options.directory, d) }
689
- end
690
- valid_directories = build_list_of_valid_directories(dirs)
691
-
692
- if valid_directories.empty?
693
- temp_dirs = dirs.each {|d| "'#{d}'"}.join(", ")
694
- if fail_on_error
695
- abort("ERROR - #{name} directories not found: #{temp_dirs}")
696
- else
697
- @log.warn "WARNING - #{name} directories not found: #{temp_dirs}"
698
- end
699
- end
700
-
701
- valid_directories
702
- end
703
-
704
- def process_application_inheritance(app)
705
- return app if app.inherit.nil?
706
-
707
- # Find base application
708
- base_app = @applications[app.inherit]
709
- abort("Invalid inheritance '#{app.inherit}' in #{app.name}") if base_app.nil?
710
-
711
- new_app = nil
712
- if base_app.inherit.nil?
713
- # Clone base application
714
- new_app = Marshal::load(Marshal.dump(base_app))
715
- else
716
- new_app = process_application_inheritance(base_app)
717
- end
718
- new_app.merge(app)
719
- new_app
720
- end
721
-
722
- def process_environment_inheritance(env)
723
- return env if env.inherit.nil?
724
-
725
- # Find base environment
726
- base_env = @environments[env.inherit]
727
- abort("Invalid inheritance '#{env.inherit}' in #{env.name}") if base_env.nil?
728
-
729
- new_env = nil
730
- if base_env.inherit.nil?
731
- # Clone base environment
732
- new_env = Marshal::load(Marshal.dump(base_env))
733
- else
734
- new_env = process_environment_inheritance(base_env)
735
- end
736
- new_env.merge(env)
737
- new_env
738
- end
739
-
740
585
  # Given a string containing a command to run, replaces any inline variables.
741
586
  # Supported variables include:
742
587
  # * @APPLICATION@ - name of the application
@@ -764,32 +609,6 @@ module EC2Launcher
764
609
  cmd
765
610
  end
766
611
 
767
- # Validates all settings in an application file
768
- #
769
- # @param [String] filename name of the application file
770
- # @param [EC2Launcher::DSL::Application] application application object to validate
771
- #
772
- def validate_application(filename, application)
773
- unless application.availability_zone.nil? || AVAILABILITY_ZONES.include?(application.availability_zone)
774
- abort("Invalid availability zone '#{application.availability_zone}' in application '#{application.name}' (#{filename})")
775
- end
776
-
777
- unless application.instance_type.nil? || INSTANCE_TYPES.include?(application.instance_type)
778
- abort("Invalid instance type '#{application.instance_type}' in application '#{application.name}' (#{filename})")
779
- end
780
- end
781
-
782
- # Validates all settings in an environment file
783
- #
784
- # @param [String] filename name of the environment file
785
- # @param [EC2Launcher::DSL::Environment] environment environment object to validate
786
- #
787
- def validate_environment(filename, environment)
788
- unless environment.availability_zone.nil? || AVAILABILITY_ZONES.include?(environment.availability_zone)
789
- abort("Invalid availability zone '#{environment.availability_zone}' in environment '#{environment.name}' (#{filename})")
790
- end
791
- end
792
-
793
612
  # Builds the launch scripts that should run on the new instance.
794
613
  #
795
614
  # @param [String] fqdn Fully qualified hostname
@@ -0,0 +1,81 @@
1
+ #
2
+ # Copyright (c) 2012 Sean Laurent
3
+ #
4
+ require 'rubygems'
5
+ require 'log4r'
6
+
7
+ require 'ec2launcher/directory_processing'
8
+ require 'ec2launcher/dsl/application'
9
+ require 'ec2launcher/dsl/environment'
10
+
11
+ include Log4r
12
+
13
+ module EC2Launcher
14
+ class ApplicationProcessor
15
+ attr_accessor :applications
16
+
17
+ include DirectoryProcessing
18
+
19
+ def initialize(base_directory, applications_directories)
20
+ app_dirs = process_directory_list(base_directory, applications_directories, "applications", "Applications", true)
21
+
22
+ # Load applications
23
+ @applications = {}
24
+ app_dirs.each do |app_dir|
25
+ Dir.entries(app_dir).each do |application_name|
26
+ filename = File.join(app_dir, application_name)
27
+ next if File.directory?(filename)
28
+
29
+ apps = EC2Launcher::DSL::ApplicationDSL.execute(File.read(filename)).applications
30
+ apps.each do |new_application|
31
+ @applications[new_application.name] = new_application
32
+ validate_application(filename, new_application)
33
+ end
34
+ end
35
+ end
36
+
37
+ # Process inheritance rules for applications
38
+ @applications.values.each do |app|
39
+ next if app.inherit.nil?
40
+
41
+ new_app = process_application_inheritance(app)
42
+ @applications[new_app.name] = new_app
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def process_application_inheritance(app)
49
+ return app if app.inherit.nil?
50
+
51
+ # Find base application
52
+ base_app = @applications[app.inherit]
53
+ abort("Invalid inheritance '#{app.inherit}' in #{app.name}") if base_app.nil?
54
+
55
+ new_app = nil
56
+ if base_app.inherit.nil?
57
+ # Clone base application
58
+ new_app = Marshal::load(Marshal.dump(base_app))
59
+ else
60
+ new_app = process_application_inheritance(base_app)
61
+ end
62
+ new_app.merge(app)
63
+ new_app
64
+ end
65
+
66
+ # Validates all settings in an application file
67
+ #
68
+ # @param [String] filename name of the application file
69
+ # @param [EC2Launcher::DSL::Application] application application object to validate
70
+ #
71
+ def validate_application(filename, application)
72
+ unless application.availability_zone.nil? || AVAILABILITY_ZONES.include?(application.availability_zone)
73
+ abort("Invalid availability zone '#{application.availability_zone}' in application '#{application.name}' (#{filename})")
74
+ end
75
+
76
+ unless application.instance_type.nil? || INSTANCE_TYPES.include?(application.instance_type)
77
+ abort("Invalid instance type '#{application.instance_type}' in application '#{application.name}' (#{filename})")
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,63 @@
1
+ #
2
+ # Copyright (c) 2012 Sean Laurent
3
+ #
4
+ require 'rubygems'
5
+ require 'log4r'
6
+
7
+ include Log4r
8
+
9
+ module EC2Launcher
10
+ module DirectoryProcessing
11
+ # Attempts to build a list of valid directories.
12
+ #
13
+ # @param [Array<String>, nil] target_directories list of possible directories
14
+ # @param [String] default_directory directory to use if the target_directories list is empty or nil
15
+ # @param [String] name name of the type of directory. Used only for error messages.
16
+ # @param [Boolean] fail_on_error exit with an error if the list of valid directories is empty
17
+ #
18
+ # @return [Array<String] list of directories that exist
19
+ #
20
+ def process_directory_list(base_directory, target_directories, default_directory, name, fail_on_error = false)
21
+ log = Logger['ec2launcher']
22
+ dirs = []
23
+ if target_directories.nil?
24
+ dirs << File.join(base_directory, default_directory)
25
+ else
26
+ target_directories.each do |d|
27
+ dirs << File.join(base_directory, d)
28
+ end
29
+ end
30
+ valid_directories = build_list_of_valid_directories(dirs)
31
+
32
+ if valid_directories.empty?
33
+ temp_dirs = dirs.each {|d| "'#{d}'"}.join(", ")
34
+ if fail_on_error
35
+ abort("ERROR - #{name} directories not found: #{temp_dirs}")
36
+ else
37
+ log.warn "WARNING - #{name} directories not found: #{temp_dirs}"
38
+ end
39
+ end
40
+
41
+ valid_directories
42
+ end
43
+
44
+ private
45
+
46
+ # Given a list of possible directories, build a list of directories that actually exist.
47
+ #
48
+ # @param [Array<String>] directories list of possible directories
49
+ # @return [Array<String>] directories that exist or an empty array if none of the directories exist.
50
+ #
51
+ def build_list_of_valid_directories(directories)
52
+ dirs = []
53
+ unless directories.nil?
54
+ if directories.kind_of? Array
55
+ directories.each {|d| dirs << d if File.directory?(d) }
56
+ else
57
+ dirs << directories if File.directory?(directories)
58
+ end
59
+ end
60
+ dirs
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,93 @@
1
+ #
2
+ # Copyright (c) 2012 Sean Laurent
3
+ #
4
+ require 'rubygems'
5
+ require 'log4r'
6
+
7
+ require 'ec2launcher/directory_processing'
8
+
9
+ include Log4r
10
+
11
+ module EC2Launcher
12
+ class EnvironmentProcessor
13
+ attr_accessor :environments
14
+
15
+ include DirectoryProcessing
16
+
17
+ def initialize(base_directory, environments_directories)
18
+ env_dirs = process_directory_list(base_directory, environments_directories, "environments", "Environments", false)
19
+
20
+ # Load other environments
21
+ @environments = { }
22
+ env_dirs.each do |env_dir|
23
+ Dir.entries(env_dir).each do |env_name|
24
+ filename = File.join(env_dir, env_name)
25
+ next if File.directory?(filename)
26
+
27
+ new_env = load_environment_file(filename)
28
+ validate_environment(filename, new_env)
29
+
30
+ @environments[new_env.name] = new_env
31
+ new_env.aliases.each {|env_alias| @environments[env_alias] = new_env }
32
+ end
33
+ end
34
+
35
+ # Process inheritance rules for environments
36
+ @environments.values.each do |env|
37
+ new_env = process_environment_inheritance(env)
38
+ @environments[new_env.name] = new_env
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ # Load and parse an environment file
45
+ #
46
+ # @param [String] name full pathname of the environment file to load
47
+ # @param [EC2Launcher::Environment, nil] default_environment the default environment,
48
+ # which will be used as the base for the new environment. Optional.
49
+ # @param [Boolean] fail_on_missing print an error and exit if the file does not exist.
50
+ #
51
+ # @return [EC2Launcher::Environment] the new environment loaded from the specified file.
52
+ #
53
+ def load_environment_file(name, fail_on_missing = false)
54
+ unless File.exists?(name)
55
+ abort("Unable to read environment: #{name}") if fail_on_missing
56
+ return nil
57
+ end
58
+
59
+ load_env = EC2Launcher::DSL::Environment.new
60
+ load_env.load(File.read(name))
61
+ load_env
62
+ end
63
+
64
+ def process_environment_inheritance(env)
65
+ return env if env.inherit.nil?
66
+
67
+ # Find base environment
68
+ base_env = @environments[env.inherit]
69
+ abort("Invalid inheritance '#{env.inherit}' in #{env.name}") if base_env.nil?
70
+
71
+ new_env = nil
72
+ if base_env.inherit.nil?
73
+ # Clone base environment
74
+ new_env = Marshal::load(Marshal.dump(base_env))
75
+ else
76
+ new_env = process_environment_inheritance(base_env)
77
+ end
78
+ new_env.merge(env)
79
+ new_env
80
+ end
81
+
82
+ # Validates all settings in an environment file
83
+ #
84
+ # @param [String] filename name of the environment file
85
+ # @param [EC2Launcher::DSL::Environment] environment environment object to validate
86
+ #
87
+ def validate_environment(filename, environment)
88
+ unless environment.availability_zone.nil? || AVAILABILITY_ZONES.include?(environment.availability_zone)
89
+ abort("Invalid availability zone '#{environment.availability_zone}' in environment '#{environment.name}' (#{filename})")
90
+ end
91
+ end
92
+ end
93
+ end
@@ -2,5 +2,5 @@
2
2
  # Copyright (c) 2012 Sean Laurent
3
3
  #
4
4
  module EC2Launcher
5
- VERSION = "1.0.32"
5
+ VERSION = "1.0.33"
6
6
  end
@@ -377,7 +377,16 @@ def run_chef_client(chef_path)
377
377
  end
378
378
 
379
379
  result = run_chef_client(chef_path)
380
- run_chef_client(chef_path) unless result == 0
380
+ unless result == 0
381
+ puts "***** ERROR running chef-client. Relaunching chef-client in 30 seconds."
382
+ sleep(30)
383
+ result = run_chef_client(chef_path)
384
+ end
385
+ unless result == 0
386
+ puts "***** ERROR running chef-client. Relaunching chef-client in 30 seconds."
387
+ sleep(30)
388
+ result = run_chef_client(chef_path)
389
+ end
381
390
 
382
391
  ##############################
383
392
  # EMAIL NOTIFICATION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2launcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.32
4
+ version: 1.0.33
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-20 00:00:00.000000000 Z
12
+ date: 2012-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -60,15 +60,18 @@ files:
60
60
  - bin/ec2launcher
61
61
  - ec2launcher.gemspec
62
62
  - lib/ec2launcher.rb
63
+ - lib/ec2launcher/application_processor.rb
63
64
  - lib/ec2launcher/backoff_runner.rb
64
65
  - lib/ec2launcher/block_device_builder.rb
65
66
  - lib/ec2launcher/defaults.rb
67
+ - lib/ec2launcher/directory_processing.rb
66
68
  - lib/ec2launcher/dsl/application.rb
67
69
  - lib/ec2launcher/dsl/block_device.rb
68
70
  - lib/ec2launcher/dsl/config.rb
69
71
  - lib/ec2launcher/dsl/email_notification.rb
70
72
  - lib/ec2launcher/dsl/environment.rb
71
73
  - lib/ec2launcher/dsl/helper.rb
74
+ - lib/ec2launcher/environment_processor.rb
72
75
  - lib/ec2launcher/hostname_generator.rb
73
76
  - lib/ec2launcher/init_options.rb
74
77
  - lib/ec2launcher/instance_paths_config.rb