solutious-rudy 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/CHANGES.txt +19 -1
  2. data/README.rdoc +66 -35
  3. data/Rakefile +1 -0
  4. data/Rudyfile +97 -6
  5. data/bin/ird +7 -4
  6. data/bin/rudy +120 -212
  7. data/bin/rudy-ec2 +240 -0
  8. data/bin/rudy-s3 +76 -0
  9. data/bin/rudy-sdb +67 -0
  10. data/lib/annoy.rb +61 -18
  11. data/lib/console.rb +10 -5
  12. data/lib/rudy/aws/ec2/address.rb +76 -40
  13. data/lib/rudy/aws/ec2/group.rb +138 -78
  14. data/lib/rudy/aws/ec2/image.rb +134 -12
  15. data/lib/rudy/aws/ec2/instance.rb +116 -89
  16. data/lib/rudy/aws/ec2/keypair.rb +26 -14
  17. data/lib/rudy/aws/ec2/snapshot.rb +23 -12
  18. data/lib/rudy/aws/ec2/volume.rb +198 -202
  19. data/lib/rudy/aws/ec2/zone.rb +77 -0
  20. data/lib/rudy/aws/ec2.rb +56 -25
  21. data/lib/rudy/aws/s3.rb +54 -0
  22. data/lib/rudy/aws/sdb/error.rb +46 -0
  23. data/lib/rudy/aws/sdb.rb +298 -0
  24. data/lib/rudy/aws.rb +29 -57
  25. data/lib/rudy/{metadata/backup.rb → backup.rb} +8 -8
  26. data/lib/rudy/cli/aws/ec2/addresses.rb +105 -0
  27. data/lib/rudy/cli/aws/ec2/candy.rb +208 -0
  28. data/lib/rudy/cli/aws/ec2/groups.rb +121 -0
  29. data/lib/rudy/cli/aws/ec2/images.rb +196 -0
  30. data/lib/rudy/cli/aws/ec2/instances.rb +194 -0
  31. data/lib/rudy/cli/aws/ec2/keypairs.rb +53 -0
  32. data/lib/rudy/cli/aws/ec2/snapshots.rb +49 -0
  33. data/lib/rudy/cli/aws/ec2/volumes.rb +104 -0
  34. data/lib/rudy/cli/aws/ec2/zones.rb +22 -0
  35. data/lib/rudy/cli/aws/s3/buckets.rb +50 -0
  36. data/lib/rudy/cli/aws/s3/store.rb +22 -0
  37. data/lib/rudy/cli/aws/sdb/domains.rb +41 -0
  38. data/lib/rudy/cli/{deploy.rb → candy.rb} +2 -6
  39. data/lib/rudy/cli/config.rb +25 -20
  40. data/lib/rudy/cli/disks.rb +18 -108
  41. data/lib/rudy/cli/machines.rb +94 -0
  42. data/lib/rudy/cli/routines.rb +47 -70
  43. data/lib/rudy/cli.rb +104 -89
  44. data/lib/rudy/config/objects.rb +5 -43
  45. data/lib/rudy/config.rb +8 -24
  46. data/lib/rudy/disks.rb +248 -0
  47. data/lib/rudy/global.rb +121 -0
  48. data/lib/rudy/huxtable.rb +232 -147
  49. data/lib/rudy/machines.rb +245 -0
  50. data/lib/rudy/metadata.rb +136 -0
  51. data/lib/rudy/routines/helpers/diskhelper.rb +101 -0
  52. data/lib/rudy/routines/helpers/scripthelper.rb +91 -0
  53. data/lib/rudy/routines/release.rb +27 -8
  54. data/lib/rudy/routines/shutdown.rb +47 -32
  55. data/lib/rudy/routines/startup.rb +47 -37
  56. data/lib/rudy/routines.rb +30 -37
  57. data/lib/rudy/scm/svn.rb +1 -1
  58. data/lib/rudy/utils.rb +262 -4
  59. data/lib/rudy.rb +76 -248
  60. data/lib/storable.rb +19 -16
  61. data/lib/sysinfo.rb +1 -1
  62. data/rudy.gemspec +88 -68
  63. data/support/rudy-ec2-startup +5 -5
  64. data/test/05_config/00_setup_test.rb +3 -7
  65. data/test/20_sdb/00_setup_test.rb +2 -17
  66. data/test/20_sdb/10_domains_test.rb +18 -16
  67. data/test/25_ec2/00_setup_test.rb +5 -10
  68. data/test/25_ec2/10_keypairs_test.rb +13 -5
  69. data/test/25_ec2/20_groups_test.rb +48 -56
  70. data/test/25_ec2/30_addresses_test.rb +13 -10
  71. data/test/25_ec2/40_volumes_test.rb +11 -8
  72. data/test/25_ec2/50_snapshots_test.rb +17 -12
  73. data/test/26_ec2_instances/00_setup_test.rb +3 -8
  74. data/test/26_ec2_instances/10_instances_test.rb +21 -19
  75. data/test/30_sdb_metadata/00_setup_test.rb +2 -9
  76. data/test/30_sdb_metadata/10_disks_test.rb +47 -37
  77. data/test/30_sdb_metadata/20_backups_test.rb +9 -9
  78. data/test/helper.rb +5 -3
  79. data/vendor/highline-1.5.1/CHANGELOG +222 -0
  80. data/vendor/highline-1.5.1/INSTALL +35 -0
  81. data/vendor/highline-1.5.1/LICENSE +7 -0
  82. data/vendor/highline-1.5.1/README +63 -0
  83. data/vendor/highline-1.5.1/Rakefile +82 -0
  84. data/vendor/highline-1.5.1/TODO +6 -0
  85. data/vendor/highline-1.5.1/examples/ansi_colors.rb +38 -0
  86. data/vendor/highline-1.5.1/examples/asking_for_arrays.rb +18 -0
  87. data/vendor/highline-1.5.1/examples/basic_usage.rb +75 -0
  88. data/vendor/highline-1.5.1/examples/color_scheme.rb +32 -0
  89. data/vendor/highline-1.5.1/examples/limit.rb +12 -0
  90. data/vendor/highline-1.5.1/examples/menus.rb +65 -0
  91. data/vendor/highline-1.5.1/examples/overwrite.rb +19 -0
  92. data/vendor/highline-1.5.1/examples/page_and_wrap.rb +322 -0
  93. data/vendor/highline-1.5.1/examples/password.rb +7 -0
  94. data/vendor/highline-1.5.1/examples/trapping_eof.rb +22 -0
  95. data/vendor/highline-1.5.1/examples/using_readline.rb +17 -0
  96. data/vendor/highline-1.5.1/lib/highline/color_scheme.rb +120 -0
  97. data/vendor/highline-1.5.1/lib/highline/compatibility.rb +17 -0
  98. data/vendor/highline-1.5.1/lib/highline/import.rb +43 -0
  99. data/vendor/highline-1.5.1/lib/highline/menu.rb +395 -0
  100. data/vendor/highline-1.5.1/lib/highline/question.rb +463 -0
  101. data/vendor/highline-1.5.1/lib/highline/system_extensions.rb +193 -0
  102. data/vendor/highline-1.5.1/lib/highline.rb +758 -0
  103. data/vendor/highline-1.5.1/setup.rb +1360 -0
  104. data/vendor/highline-1.5.1/test/tc_color_scheme.rb +56 -0
  105. data/vendor/highline-1.5.1/test/tc_highline.rb +823 -0
  106. data/vendor/highline-1.5.1/test/tc_import.rb +54 -0
  107. data/vendor/highline-1.5.1/test/tc_menu.rb +429 -0
  108. data/vendor/highline-1.5.1/test/ts_all.rb +15 -0
  109. metadata +81 -69
  110. data/lib/aws_sdb/error.rb +0 -42
  111. data/lib/aws_sdb/service.rb +0 -215
  112. data/lib/aws_sdb.rb +0 -3
  113. data/lib/rudy/aws/simpledb.rb +0 -71
  114. data/lib/rudy/cli/addresses.rb +0 -85
  115. data/lib/rudy/cli/backups.rb +0 -175
  116. data/lib/rudy/cli/domains.rb +0 -17
  117. data/lib/rudy/cli/groups.rb +0 -77
  118. data/lib/rudy/cli/images.rb +0 -111
  119. data/lib/rudy/cli/instances.rb +0 -142
  120. data/lib/rudy/cli/keypairs.rb +0 -47
  121. data/lib/rudy/cli/manager.rb +0 -51
  122. data/lib/rudy/cli/release.rb +0 -174
  123. data/lib/rudy/cli/volumes.rb +0 -121
  124. data/lib/rudy/command/addresses.rb +0 -69
  125. data/lib/rudy/command/backups.rb +0 -65
  126. data/lib/rudy/command/disks-old.rb +0 -322
  127. data/lib/rudy/command/disks.rb +0 -9
  128. data/lib/rudy/command/domains.rb +0 -34
  129. data/lib/rudy/command/groups.rb +0 -118
  130. data/lib/rudy/command/instances.rb +0 -278
  131. data/lib/rudy/command/keypairs.rb +0 -149
  132. data/lib/rudy/command/manager.rb +0 -65
  133. data/lib/rudy/command/volumes.rb +0 -127
  134. data/lib/rudy/metadata/disk.rb +0 -149
  135. data/lib/rudy/metadata/machine.rb +0 -34
  136. data/lib/rudy/routines/disk_handler.rb +0 -190
  137. data/lib/rudy/routines/script_runner.rb +0 -65
  138. data/test/50_commands/00_setup_test.rb +0 -11
  139. data/test/50_commands/10_keypairs_test.rb +0 -79
  140. data/test/50_commands/20_groups_test.rb +0 -77
  141. data/test/50_commands/40_volumes_test.rb +0 -55
  142. data/test/50_commands/50_instances_test.rb +0 -110
  143. data/tryouts/console_tryout.rb +0 -91
  144. data/tryouts/disks.rb +0 -55
  145. data/tryouts/nested_methods.rb +0 -36
  146. data/tryouts/session_tryout.rb +0 -46
@@ -1,48 +1,58 @@
1
1
 
2
2
 
3
- module Rudy
4
- module Routines
5
- class Startup < Rudy::Routines::Base
3
+ module Rudy; module Routines;
4
+
5
+ class Startup < Rudy::Routines::Base
6
+
7
+ def execute
8
+ # There's no keypair check here because Rudy::Machines will attempt
9
+ # to create one.
10
+ rmach = Rudy::Machines.new
11
+ routine = fetch_routine_config(:startup)
12
+ rbox_local = Rye::Box.new('localhost')
13
+ sconf = fetch_script_config
6
14
 
15
+ # Runs "before_local" scripts of routines config.
16
+ # NOTE: Does not run "before" scripts b/c there are no remote machines
17
+ puts task_separator("BEFORE SCRIPTS (local)")
18
+ Rudy::Routines::ScriptHelper.before_local(routine, sconf, rbox_local)
7
19
 
8
- def startup(opts={})
9
-
10
- routine = fetch_routine(:startup)
11
- rdisks = Rudy::Disks.new(:config => @config, :global => @global)
20
+ rmach.create do |machine|
21
+ #rmach.list do |machine|
22
+ puts machine_separator(machine.liner_note)
23
+ print "Waiting for instance..."
24
+ isup = Rudy::Utils.waiter(3, 120, STDOUT, "it's up!", 2) {
25
+ inst = machine.get_instance
26
+ inst && inst.running?
27
+ }
28
+ machine.update # Add instance info to machine and save it
29
+ print "Waiting for SSH daemon..."
30
+ isup = Rudy::Utils.waiter(2, 60, STDOUT, "it's up!", 3) {
31
+ Rudy::Utils.service_available?(machine.dns_public, 22)
32
+ }
12
33
 
34
+ opts = { :keys => root_keypairpath, :user => 'root', :debug => nil }
35
+ rbox = Rye::Box.new(machine.dns_public, opts)
13
36
 
14
- rmach = Rudy::Instances.new(:config => @config, :global => @global)
37
+ puts task_separator("DISK ROUTINES")
38
+ # Runs "disk" portion of routines config
39
+ Rudy::Routines::DiskHelper.execute(routine, machine, rbox)
15
40
 
16
- # TODO: .list for debugging, .create for actual use
17
- instances = rmach.list(opts) do |instance| # Rudy::AWS::EC2::Instance objects
18
- puts '-'*60
19
- puts "Instance: #{instance.awsid.bright} (AMI: #{instance.ami})"
20
- puts instance.to_s
41
+ puts task_separator("AFTER SCRIPTS")
42
+ # Runs "after" scripts of routines config
43
+ Rudy::Routines::ScriptHelper.after(routine, sconf, machine, rbox)
21
44
 
22
- @logger.puts("Running DISK routines")
23
- routine.disks.each_pair do |action,disks|
24
-
25
- unless rdisks.respond_to?(action)
26
- @logger.puts("Skipping unknown action: #{action}").color(:blue)
27
- next
28
- end
29
-
30
- disks.each_pair do |path,props|
31
- props[:path] = path
32
- puts path
33
- begin
34
- rdisks.send(action, instance, props)
35
- rescue => ex
36
- @logger.puts "Continuing..."
37
- end
38
- end
39
- end
40
- end
41
-
42
- instances
45
+ puts task_separator("INFO")
46
+ puts "Filesystem on #{machine.name}:"
47
+ puts " " << rbox.df(:h).join("#{$/} ")
43
48
  end
44
-
45
-
49
+
50
+ puts task_separator("AFTER SCRIPTS (local)")
51
+ # Runs "after_local" scripts of routines config
52
+ Rudy::Routines::ScriptHelper.after_local(routine, sconf, rbox_local)
53
+
46
54
  end
55
+
47
56
  end
48
- end
57
+
58
+ end; end
data/lib/rudy/routines.rb CHANGED
@@ -5,50 +5,43 @@ module Rudy
5
5
  class Base
6
6
  include Rudy::Huxtable
7
7
 
8
- # Examples:
9
- #
10
- # def before
11
- # end
12
- # def before_local
13
- # end
14
- # def svn
15
- # end
16
- #
8
+ def initialize
9
+ a, s, r = @@global.accesskey, @@global.secretkey, @@global.region
10
+ @sdb = Rudy::AWS::SDB.new(a, s, r)
11
+ @rinst = Rudy::AWS::EC2::Instances.new(a, s, r)
12
+ @rgrp = Rudy::AWS::EC2::Groups.new(a, s, r)
13
+ @rkey = Rudy::AWS::EC2::KeyPairs.new(a, s, r)
14
+ @rvol = Rudy::AWS::EC2::Volumes.new(a, s, r)
15
+ @rsnap = Rudy::AWS::EC2::Snapshots.new(a, s, r)
16
+ init
17
+ end
17
18
 
19
+ def init
20
+ end
18
21
 
19
- #
20
- #
22
+
21
23
  def execute
22
- raise "Override this method"
24
+ raise "Override execute method"
23
25
  end
24
26
 
27
+ def task_separator(title)
28
+ dashes = 52 - title.size #
29
+ dashes = 0 if dashes < 1
30
+ ("%s--- %s %s" % [$/, title, '-'*dashes]).bright
31
+ end
25
32
 
26
-
27
- # We grab the appropriate routines config and check the paths
28
- # against those defined for the matching machine group.
29
- # Disks that appear in a routine but not the machine will be
30
- # removed and a warning printed. Otherwise, the routines config
31
- # is merged on top of the machine config and that's what we return.
32
- def fetch_routine(action)
33
- raise "No configuration" unless @config
34
- raise "No globals" unless @global
35
-
36
- disk_definitions = @config.machines.find_deferred(@global.environment, @global.role, :disks)
37
- routine = @config.routines.find(@global.environment, @global.role, action)
38
- routine.disks.each_pair do |raction,disks|
39
- disks.each_pair do |path, props|
40
- routine.disks[raction][path] = disk_definitions[path].merge(props) if disk_definitions.has_key?(path)
41
- unless disk_definitions.has_key?(path)
42
- @logger.puts "#{path} is not defined. Check your #{action} routines config.".color(:red)
43
- routine.disks[raction].delete(path)
44
- end
45
- end
46
- end
47
- routine
33
+ def machine_separator(title)
34
+ dashes = 52 - title.size #
35
+ dashes = 0 if dashes < 1
36
+ ("%s--- %s %s" % [$/, title.bright, '-'*dashes]).bright.color(:blue)
48
37
  end
49
38
 
50
-
51
-
52
39
  end
40
+
53
41
  end
54
- end
42
+ end
43
+
44
+ Rudy::Utils.require_glob(RUDY_LIB, 'rudy', 'routines', 'helpers', '*.rb')
45
+ Rudy::Utils.require_glob(RUDY_LIB, 'rudy', 'routines', '*.rb')
46
+
47
+
data/lib/rudy/scm/svn.rb CHANGED
@@ -36,7 +36,7 @@ module Rudy
36
36
  rev = "01"
37
37
  criteria = ['rel', now.year, mon, day, rev]
38
38
  criteria.insert(-2, username) if username
39
- tag = criteria.join(RUDY_DELIM)
39
+ tag = criteria.join(Rudy::DELIM)
40
40
  # Keep incrementing the revision number until we find the next one.
41
41
  tag.succ! while (valid_uri?("#{@base_uri}/#{tag}"))
42
42
  tag
data/lib/rudy/utils.rb CHANGED
@@ -15,9 +15,14 @@ module Rudy
15
15
  # Return the external IP address (the one seen by the internet)
16
16
  def external_ip_address
17
17
  ip = nil
18
- %w{solutious.com/ip myip.dk/ whatismyip.com }.each do |sponge| # w/ backup
19
- ip = (open("http://#{sponge}") { |f| /([0-9]{1,3}\.){3}[0-9]{1,3}/.match(f.read) }).to_s rescue nil
20
- break if !ip.nil?
18
+ begin
19
+ %w{solutious.com/ip/ myip.dk/ whatismyip.com }.each do |sponge| # w/ backup
20
+ ipstr = Net::HTTP.get(URI.parse("http://#{sponge}")) || ''
21
+ ip = /([0-9]{1,3}\.){3}[0-9]{1,3}/.match(ipstr).to_s
22
+ break if ip && !ip.empty?
23
+ end
24
+ rescue SocketError, Errno::ETIMEDOUT
25
+ STDERR.puts "Connection Error. Check your internets!"
21
26
  end
22
27
  ip += "/32" if ip
23
28
  ip
@@ -48,6 +53,156 @@ module Rudy
48
53
  end
49
54
 
50
55
 
56
+
57
+
58
+ # Determine if we're running directly on EC2 or
59
+ # "some other machine". We do this by checking if
60
+ # the file /etc/ec2/instance-id exists. This
61
+ # file is written by /etc/init.d/rudy-ec2-startup.
62
+ # NOTE: Is there a way to know definitively that this is EC2?
63
+ # We could make a request to the metadata IP addresses.
64
+ def Rudy.in_situ?
65
+ File.exists?('/etc/ec2/instance-id')
66
+ end
67
+
68
+
69
+ # Wait for something to happen.
70
+ # * +duration+ seconds to wait between tries (default: 2).
71
+ # * +max+ maximum time to wait (default: 120). Throws an exception when exceeded.
72
+ # * +logger+ IO object to print +dot+ to.
73
+ # * +msg+ the message to print on success
74
+ # * +bells+ number of terminal bells to ring
75
+ # Set to nil or false to keep the waiter silent.
76
+ # The block must return false while waiting. Once it returns true
77
+ # the waiter will return true too.
78
+ def waiter(duration=2, max=120, logger=STDOUT, msg=nil, bells=0, &check)
79
+ # TODO: Move to Drydock. [ed-why?]
80
+ raise "The waiter needs a block!" unless check
81
+ duration = 1 if duration < 1
82
+ max = duration*2 if max < duration
83
+ dot = '.'
84
+ begin
85
+ Timeout::timeout(max) do
86
+ while !check.call
87
+ sleep duration
88
+ logger.print dot if logger.respond_to?(:print)
89
+ logger.flush if logger.respond_to?(:flush)
90
+ end
91
+ end
92
+ rescue Timeout::Error => ex
93
+ retry if Annoy.pose_question(" Keep waiting?\a ", /yes|y|ya|sure|you bet!/i, logger)
94
+ return false
95
+ end
96
+ logger.puts msg if msg
97
+ Rudy::Utils.bell(bells, logger)
98
+ true
99
+ end
100
+
101
+ # Make a terminal bell chime
102
+ def bell(chimes=1, logger=nil)
103
+ chimes ||= 0
104
+ return unless logger
105
+ chimed = chimes.to_i
106
+ logger.print "\a"*chimes if chimes > 0 && logger
107
+ true # be like Rudy.bug()
108
+ end
109
+
110
+ # Have you seen that episode of The Cosby Show where Dizzy Gillespie... ah nevermind.
111
+ def bug(bugid, logger=STDERR)
112
+ logger.puts "You have found a bug! If you want, you can email".color(:red)
113
+ logger.puts 'rudy@solutious.com'.color(:red).bright << " about it. It's bug ##{bugid}.".color(:red)
114
+ logger.puts "Continuing...".color(:red)
115
+ true # so we can string it together like: bug('1') && next if ...
116
+ end
117
+
118
+ # Is the given string +str+ an ID of type +identifier+?
119
+ # * +identifier+ is expected to be a key from ID_MAP
120
+ # * +str+ is a string you're investigating
121
+ def is_id?(identifier, str)
122
+ return false unless identifier && str && known_type?(identifier)
123
+ identifier &&= identifier.to_sym
124
+ str &&= str.to_s.strip
125
+ str.split('-').first == Rudy::ID_MAP[identifier].to_s
126
+ end
127
+
128
+ # Returns the object type associated to +str+ or nil if unknown.
129
+ # * +str+ is a string you're investigating
130
+ def id_type(str)
131
+ return false unless str
132
+ str &&= str.to_s.strip
133
+ (Rudy::ID_MAP.detect { |n,v| v == str.split('-').first } || []).first
134
+ end
135
+
136
+ # Is the given +key+ a known type of object?
137
+ def known_type?(key)
138
+ return false unless key
139
+ key &&= key.to_s.to_sym
140
+ Rudy::ID_MAP.has_key?(key)
141
+ end
142
+
143
+ # Returns the string identifier associated to this +key+
144
+ def identifier(key)
145
+ key &&= key.to_sym
146
+ return unless Rudy::ID_MAP.has_key?(key)
147
+ Rudy::ID_MAP[key]
148
+ end
149
+
150
+ # Return a string ID without the identifier. i.e. key-stage-app-root => stage-app-root
151
+ def noid(str)
152
+ el = str.split('-')
153
+ el.shift
154
+ el.join('-')
155
+ end
156
+
157
+
158
+ # +msg+ The message to return as a banner
159
+ # +size+ One of: :normal (default), :huge
160
+ # +colour+ a valid
161
+ # Returns a string with styling applying
162
+ def banner(msg, size = :normal, colour = :black)
163
+ return unless msg
164
+ banners = {
165
+ :huge => Rudy::Utils.without_indent(%Q(
166
+ =======================================================
167
+ =======================================================
168
+ !!!!!!!!! %s !!!!!!!!!
169
+ =======================================================
170
+ =======================================================)),
171
+ :normal => %Q(============ %s ============)
172
+ }
173
+ size = :normal unless banners.has_key?(size)
174
+ colour = :black unless Console.valid_colour?(colour)
175
+ size, colour = size.to_sym, colour.to_sym
176
+ sprintf(banners[size], msg).colour(colour).bgcolour(:white).bright
177
+ end
178
+
179
+
180
+ # <tt>require</tt> a glob of files.
181
+ # * +path+ is a list of path elements which is sent to File.join
182
+ # and then to Dir.glob. The list of files found are sent to require.
183
+ # Nothing is returned but LoadError exceptions are caught. The message
184
+ # is printed to STDERR and the program exits with 7.
185
+ def require_glob(*path)
186
+ begin
187
+ # TODO: Use autoload
188
+ Dir.glob(File.join(*path.flatten)).each do |path|
189
+ require path
190
+ end
191
+ rescue LoadError => ex
192
+ puts "Error: #{ex.message}"
193
+ exit 7
194
+ end
195
+ end
196
+
197
+ # Checks whether something is listening to a socket.
198
+ # * +host+ A hostname
199
+ # * +port+ The port to check
200
+ # * +wait+ The number of seconds to wait for before timing out.
201
+ #
202
+ # Returns true if +host+ allows a socket connection on +port+.
203
+ # Returns false if one of the following exceptions is raised:
204
+ # Errno::EAFNOSUPPORT, Errno::ECONNREFUSED, SocketError, Timeout::Error
205
+ #
51
206
  def service_available?(host, port, wait=3)
52
207
  begin
53
208
  status = Timeout::timeout(wait) do
@@ -84,7 +239,7 @@ module Rudy
84
239
  end
85
240
 
86
241
  # A basic file writer
87
- def write_to_file(filename, content, mode, chmod=nil)
242
+ def write_to_file(filename, content, mode, chmod=600)
88
243
  mode = (mode == :append) ? 'a' : 'w'
89
244
  f = File.open(filename,mode)
90
245
  f.puts content
@@ -114,6 +269,109 @@ module Rudy
114
269
  str.gsub(/^[[:blank:]]{#{indent}}/, '')
115
270
  end
116
271
 
272
+
273
+
274
+
275
+ ######### Everything below here is TO BE REMOVED.
276
+
277
+ def sh(command, chdir=false, verbose=false)
278
+ prevdir = Dir.pwd
279
+ Dir.chdir chdir if chdir
280
+ puts command if verbose
281
+ system(command)
282
+ Dir.chdir prevdir if chdir
283
+ end
284
+
285
+
286
+ def ssh_command(host, keypair, user, command=false, printonly=false, verbose=false)
287
+ #puts "CONNECTING TO #{host}..."
288
+ cmd = "ssh -i #{keypair} #{user}@#{host} "
289
+ cmd += " '#{command}'" if command
290
+ puts cmd if verbose
291
+ return cmd if printonly
292
+ # backticks returns STDOUT
293
+ # exec replaces current process (it's just like running ssh)
294
+ # -- UPDATE -- Some problem with exec. "Operation not supported"
295
+ # using system (http://www.mail-archive.com/mongrel-users@rubyforge.org/msg02018.html)
296
+ (command) ? `#{cmd}` : Kernel.system(cmd)
297
+ end
298
+
299
+
300
+ # TODO: This is old and insecure.
301
+ def scp_command(host, keypair, user, paths, to_path, to_local=false, verbose=false, printonly=false)
302
+
303
+ paths = [paths] unless paths.is_a?(Array)
304
+ from_paths = ""
305
+ if to_local
306
+ paths.each do |path|
307
+ from_paths << "#{user}@#{host}:#{path} "
308
+ end
309
+ #puts "Copying FROM remote TO this machine", $/
310
+
311
+ else
312
+ to_path = "#{user}@#{host}:#{to_path}"
313
+ from_paths = paths.join(' ')
314
+ #puts "Copying FROM this machine TO remote", $/
315
+ end
117
316
 
317
+
318
+ cmd = "scp -r "
319
+ cmd << "-i #{keypair}" if keypair
320
+ cmd << " #{from_paths} #{to_path}"
321
+
322
+ puts cmd if verbose
323
+ printonly ? (puts cmd) : system(cmd)
324
+ end
325
+
326
+ end
327
+ end
328
+
329
+ # = RSSReader
330
+ #
331
+ # A rudimentary way to read an RSS feed as a hash.
332
+ # Adapted from: http://snippets.dzone.com/posts/show/68
333
+ #
334
+ module Rudy::Utils::RSSReader
335
+ extend self
336
+ require 'net/http'
337
+ require 'rexml/document'
338
+
339
+ # Returns a feed as a hash.
340
+ # * +uri+ to RSS feed
341
+ def run(uri)
342
+ begin
343
+ xmlstr = Net::HTTP.get(URI.parse(uri))
344
+ rescue SocketError, Errno::ETIMEDOUT
345
+ STDERR.puts "Connection Error. Check your internets!"
346
+ end
347
+
348
+ xml = REXML::Document.new xmlstr
349
+
350
+ data = { :items => [] }
351
+ xml.elements.each '//channel' do |item|
352
+ item.elements.each do |e|
353
+ n = e.name.downcase.gsub(/^dc:(\w)/,"\1").to_sym
354
+ next if n == :item
355
+ data[n] = e.text
356
+ end
357
+ end
358
+
359
+ #data = {
360
+ # :title => xml.root.elements['channel/title'].text,
361
+ # :link => xml.root.elements['channel/link'].text,
362
+ # :updated => xml.root.elements['channel/lastBuildDate'].text,
363
+ # :uri => uri,
364
+ # :items => []
365
+ #}
366
+ #data[:updated] &&= DateTime.parse(data[:updated])
367
+
368
+ xml.elements.each '//item' do |item|
369
+ new_items = {} and item.elements.each do |e|
370
+ n = e.name.downcase.gsub(/^dc:(\w)/,"\1").to_sym
371
+ new_items[n] = e.text
372
+ end
373
+ data[:items] << new_items
374
+ end
375
+ data
118
376
  end
119
377
  end