Class: Arachni::UI::Web::Addons::AutoDeploy::Manager

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/arachni/ui/web/addons/autodeploy/lib/manager.rb

Overview

@author: Tasos “Zapotek” Laskos

<[email protected]>
<[email protected]>

@version: 0.1.2

Defined Under Namespace

Classes: Deployment

Constant Summary collapse

ARCHIVE_PATH =
'https://github.com/downloads/Zapotek/arachni/'
ARCHIVE_NAME =

ARCHIVE_PATH = ‘localhost/~zapotek/’

'arachni-v' + Arachni::VERSION + '-autodeploy'
ARCHIVE_EXT =
'.tar.gz'
NODE_SSL_KEY =
"node_ssl_key.pem"
NODE_SSL_CERT =
"node_ssl_cert.pem"
SSL_KEY =
"ssl_key.pem"
SSL_CERT =
"ssl_cert.pem"
SSL_CA =
"ssl_ca.pem"
EXEC =
'arachni_rpcd'

Instance Method Summary collapse

Methods included from Utilities

#escape, #escape_hash, #parse_datetime, #port_to_url, #remove_proto, #unescape, #unescape_hash

Constructor Details

#initialize(opts, settings) ⇒ Manager

Returns a new instance of Manager.



76
77
78
79
80
81
82
83
84
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 76

def initialize( opts, settings )
    @opts     = opts
    @settings = settings

    DataMapper::setup( :default, "sqlite3://#{@settings.db}/default.db" )
    DataMapper.finalize

    Deployment.auto_upgrade!
end

Instance Method Details

#alive?(deployment, &block) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
279
280
281
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 276

def alive?( deployment, &block )
    @settings.dispatchers.alive?( get_rpc_url( deployment ) ){
        |alive|
        block.call( alive )
    }
end

#delete(id, password) ⇒ Object



287
288
289
290
291
292
293
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 287

def delete( id, password )
    deployment = get( id )
    ret = uninstall( deployment, password )
    return ret if ret[:code]
    deployment.destroy
    return ret
end

#finalize_setup(channel) ⇒ Object



154
155
156
157
158
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 154

def finalize_setup( channel )
    @@setup[channel][:deployment].ssl = @settings.conf['ssl']['server']['enable']
    @@setup[channel][:deployment].save
    return @@setup[channel][:deployment]
end

#get(id) ⇒ Object



283
284
285
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 283

def get( id )
    Deployment.get( id )
end

#get_rpc_url(deployment) ⇒ Object



306
307
308
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 306

def get_rpc_url( deployment )
    deployment.host + ':' + deployment.dispatcher_port.to_s
end

#get_url(deployment) ⇒ Object



310
311
312
313
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 310

def get_url( deployment )
    deployment.user + '@' + deployment.host + ':' + deployment.port.to_s +
        '$' + deployment.dispatcher_port.to_s
end

#listObject



257
258
259
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 257

def list
    Deployment.all.reverse
end

#list_with_liveness(&block) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 261

def list_with_liveness( &block )
    ::EM.synchrony do
        deployments = ::EM::Synchrony::Iterator.new( list ).map {
            |deployment, iter|
            alive?( deployment ){
                |alive|
                deployment.alive = alive
                iter.return( deployment )
            }
        }

        block.call( deployments )
    end
end

#output(channel) ⇒ Object



150
151
152
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 150

def output( channel )
    return @@setup[channel]
end

#run(deployment, password) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 181

def run( deployment, password )
   begin
       session = ssh( deployment, password )
   rescue Exception => e
       return {
           :output => e.class.name + ': ' + e.to_s + "\n" + e.backtrace.join( "\n" ),
           :status => 'failed',
           :code   => 1
       }
   end

    cmd  = 'nohup ./' + ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s + '/' + ARCHIVE_NAME + '/' + EXEC
    cmd += " --port='#{deployment.dispatcher_port}'"

    if deployment.nickname && !deployment.nickname.empty?
        cmd += " --nickname='#{deployment.nickname}'"
    end

    if deployment.pipe_id && !deployment.pipe_id.empty?
        cmd += " --pipe-id='#{deployment.pipe_id}'"
    end

    if deployment.weight && !deployment.weight.empty?
        cmd += " --weight='#{deployment.weight}'"
    end

    if deployment.neighbour && !deployment.neighbour.empty?
        cmd += " --neighbour='#{deployment.neighbour}'"
    end

    if deployment.address && !deployment.address.empty?
        cmd += " --address='#{deployment.address}'"
    end

    if deployment.pool_size > 0
        cmd += " --pool-size='#{deployment.pool_size}'"
    end

    if deployment.ssl
        cmd += " --node-ssl-pkey='/#{NODE_SSL_KEY}'"
        cmd += " --node-ssl-cert='/#{NODE_SSL_CERT}'"
        cmd += " --ssl-pkey='/#{SSL_KEY}'"
        cmd += " --ssl-cert='/#{SSL_CERT}'"
        cmd += " --ssl-ca='/#{SSL_CA}'"
    end

    # puts cmd

    cmd += ' > ' + EXEC + '-startup.log 2>&1 &'

    session.exec!( cmd )

   sleep( 3 )
   { :code   => 0 }
end

#setup(deployment, password) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 86

def setup( deployment, password )

    @@setup ||= {}
    url = get_url( deployment )
    @@setup[url] ||= {}

    Thread.new {
        @@setup[url][:deployment] ||= deployment
        @@setup[url][:status] = 'working'

        begin
            session = ssh( deployment, password )
        rescue Exception => e
            @@setup[url][:status] = 'failed'
            @@setup[url][:output] = e.class.name + ': ' + e.to_s + "\n" + e.backtrace.join( "\n" )
            @@setup[url][:code]   = 1
            return
        end


        wget = 'wget --output-document=' + ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s +
            ARCHIVE_EXT + ' ' + ARCHIVE_PATH + ARCHIVE_NAME + ARCHIVE_EXT
        ret = ssh_exec!( deployment, session, wget )

        if ret[:code] != 0
            @@setup[url][:status] = 'failed'
            return
        end

        mkdir = 'mkdir ' + ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s
        ret = ssh_exec!( deployment, session,  mkdir )

        if ret[:code] != 0
            @@setup[url][:status] = 'failed'
            return
        end


        tar = 'tar xvf ' + ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s + ARCHIVE_EXT +
            ' -C ' + ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s
        ret = ssh_exec!( deployment, session,  tar )

        if ret[:code] != 0
            @@setup[url][:status] = 'failed'
            return
        end

        chmod = 'chmod +x ' + ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s + '/' +
            ARCHIVE_NAME + '/' + EXEC
        ret = ssh_exec!( deployment, session, chmod )

        upload_ssl_pems!( deployment, session )

        if ret[:code] != 0
            @@setup[url][:status] = 'failed'
            return
        end

        @@setup[url][:status] = 'finished'
    }

    return get_url( deployment )
end

#shutdown(deployment, password, &block) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 237

def shutdown( deployment, password, &block )
   url =  "#{deployment.host}:#{deployment.dispatcher_port}"
   @settings.dispatchers.connect( url ).proc_info {
       |proc|
       begin
           session = ssh( deployment, password )
       rescue Exception => e
           block.call( {
               :output => e.class.name + ': ' + e.to_s + "\n" + e.backtrace.join( "\n" ),
               :status => 'failed',
               :code   => 1
           })
           next
       end

        block.call ssh_exec!( deployment, session, 'kill -9 -' + proc['pgrp'] )
    }
end

#ssh(deployment, password) ⇒ Object



295
296
297
298
299
300
301
302
303
304
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 295

def ssh( deployment, password )
    @@ssh ||= {}
    @@ssh[get_url( deployment ) + '$' + Digest::MD5.hexdigest( password ) ] ||=
        Net::SSH.start( deployment.host, deployment.user,
            {
              :port     => deployment.port,
              :password => password
            }
        )
end

#ssh_exec!(deployment, ssh, command) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 335

def ssh_exec!( deployment, ssh, command )

    stdout_data = ""
    stderr_data = ""

    exit_code   = nil
    exit_signal = nil

    @@setup ||= {}

    url = get_url( deployment )

    @@setup[url] ||= {}
    @@setup[url][:code]   = 0
    @@setup[url][:output] ||= ''
    @@setup[url][:output] += "\n" + command + "\n"

    ssh.open_channel do |channel|
        channel.exec(command) do |ch, success|
            unless success
                abort "FAILED: couldn't execute command (ssh.channel.exec)"
            end

            channel.on_data {
                |ch, data|
                stdout_data += data
                @@setup[url][:output] += data
            }

            channel.on_extended_data {
                |ch, type, data|
                stderr_data += data
                @@setup[url][:output] += data
            }

            channel.on_request( "exit-status" ) {
                |ch, data|
                exit_code = data.read_long
                @@setup[url][:code] = data.read_long
            }

            channel.on_request( "exit-signal" ) {
                |ch, data|
                exit_signal = data.read_long
            }

        end
    end

    ssh.loop
    return {
        :stdout => stdout_data,
        :stderr => stderr_data,
        :code   => exit_code,
        :signal => exit_signal
    }
end

#uninstall(deployment, password) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 160

def uninstall( deployment, password )

    begin
        session = ssh( deployment, password )
    rescue Exception => e
        return {
            :output => e.class.name + ': ' + e.to_s + "\n" + e.backtrace.join( "\n" ),
            :status => 'failed',
            :code   => 1
         }
     end

    out = "\n" + rm = "rm -rf #{ARCHIVE_NAME}-#{deployment.dispatcher_port}*"
    ret = ssh_exec!( deployment, session, rm )
    out += "\n" + ret[:stdout] + "\n" + ret[:stderr]

    return { :output => out, :code => ret[:code], :status => 'failed', } if ret[:code] != 0

    return { :output => out }
end

#upload_ssl_pems!(deployment, ssh) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/arachni/ui/web/addons/autodeploy/lib/manager.rb', line 315

def upload_ssl_pems!( deployment, ssh )

    if !@settings.conf['ssl']['client']['enable'] ||
        !@settings.conf['ssl']['server']['enable']
        return
    end

    dir = ARCHIVE_NAME + '-' + deployment.dispatcher_port.to_s + '/' +
        ARCHIVE_NAME + '/cde-root/'

    tx = []
    tx << ssh.scp.upload( @settings.conf['ssl']['client']['key'], dir + NODE_SSL_KEY )
    tx << ssh.scp.upload( @settings.conf['ssl']['client']['cert'], dir + NODE_SSL_CERT )
    tx << ssh.scp.upload( @settings.conf['ssl']['server']['key'], dir + SSL_KEY )
    tx << ssh.scp.upload( @settings.conf['ssl']['server']['cert'], dir + SSL_CERT )
    tx << ssh.scp.upload( @settings.conf['ssl']['server']['ca'], dir + SSL_CA )

    tx.each { |d| d.wait }
end