Class: Shaddox::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/shaddox/target.rb

Direct Known Subclasses

Localhost, Server

Instance Method Summary collapse

Instance Method Details

#deploy(shadow_script, opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shaddox/target.rb', line 7

def deploy(shadow_script, opts)
	tmpdir = opts[:tmpdir] || '/tmp/shaddox'
	shadow_script_path = "#{tmpdir}/shadow_script.rb"
	# Everything inside this block is handled by the target's actor (typically an SSH session)
	new_actor do

		rm_tmpdir = lambda { 
			unless !exec("test -e #{tmpdir} >/dev/null")
				info "Removing #{tmpdir}", 1
				exec("rm -r #{tmpdir}")
			end
		}

		rm_tmpdir.call() if opts[:force]

		# Try to create tmpdir:
		info "Creating #{tmpdir}", 1
		unlocked = exec "mkdir #{tmpdir}"

		# Abort if the tmpdir already exists
		raise TargetError, "A shadow script is already running on this machine. Try again later." unless unlocked

		begin
			# Initial provisioning to ensure that we have everyting we need to execute a shadow script:
			ruby_installed = exec 'type ruby >/dev/null'
			raise TargetError, "Ruby is required to use shaddox. Please install it manually." unless ruby_installed
			gem_installed = exec 'type gem >/dev/null'
			raise TargetError, "Gem is required to use shaddox. Please install it manually." unless gem_installed
			shaddox_installed = lambda { exec 'gem list shaddox -i >/dev/null' }
			if shaddox_installed.call()
				info "Updating shaddox...", 1
				updated = exec "gem update shaddox", :verbose => false
				updated = exec "sudo gem update shaddox" unless updated
				warn "Shaddox could not be automatically updated. Please update it manually with 'gem update shaddox'.", 1 unless updated
			else
				info "Installing shaddox...", 1
				installed = exec "gem install shaddox", :verbose => false
				installed = exec "sudo gem install shaddox" unless installed
				warn "Shaddox could not be automatically installed. Please update it manually with 'gem update shaddox'.", 1 unless installed
			end
			unless shaddox_installed.call()
				raise TargetError, "Shaddox was not found on this target."
			end

			# Push the shadow script to tmpdir:
			info "Writing shadow script", 1
			write_file(shadow_script.script, shadow_script_path)

			# Execute the shadow script:
			info "Executing shadow script", 1
			raise TargetError, "Shadow script was not executed successfully." unless exec "ruby #{shadow_script_path}"

			rm_tmpdir.call() unless opts[:keep_tmp_dir]
		rescue Exception => e
			# Make sure the tmpdir is removed even if the provisioning fails:
			rm_tmpdir.call() unless opts[:keep_tmp_dir]
			raise e
		end
	end
end

#new_actorObject



4
5
6
# File 'lib/shaddox/target.rb', line 4

def new_actor
	raise "new_actor method must be implemented by Target subclass"
end