Class: DockerFish

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

Overview

Bri’s TODO list

TODO: Add Export image
TODO: Fix multiple container start
TODO: Add Pull of image from Repository
TODO: Download progress bar for images
TODO: Refactor code duplication / case statement
TODO: Websocket attach with Eventmachine / Readline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(splash) ⇒ DockerFish

Returns a new instance of DockerFish.



39
40
41
42
43
# File 'lib/dockerfish.rb', line 39

def initialize(splash)
  @baseurl = "http://localhost:2375"
  @containerjson = "container.json"
  if splash == true; banner; end
end

Instance Attribute Details

#baseurlObject

Returns the value of attribute baseurl.



36
37
38
# File 'lib/dockerfish.rb', line 36

def baseurl
  @baseurl
end

#hostname=(value) ⇒ Object (writeonly)

Sets the attribute hostname

Parameters:

  • value

    the value to set the attribute hostname to.



37
38
39
# File 'lib/dockerfish.rb', line 37

def hostname=(value)
  @hostname = value
end

#image=(value) ⇒ Object (writeonly)

Sets the attribute image

Parameters:

  • value

    the value to set the attribute image to.



37
38
39
# File 'lib/dockerfish.rb', line 37

def image=(value)
  @image = value
end

Instance Method Details

#apicall(action) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/dockerfish.rb', line 132

def apicall(action)
  
  case action
  when action = "images"
    response = apiget("#{@url}")
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
    puts "\e[1;30mList of Docker Images\e[0m\ "
    puts "\e[1;30m=====================\e[0m\ "
    puts ""
    0.upto(j.length - 1) {|n|
      j[n].each {|s|
        case s[0]
        when "Created"
          ftime = Time.at(s[1]).to_s
          ctime = ftime[0..18]
          created = "#{s[0]}: #{ctime} "
        when "Id"
          parentid = "#{s[0]}: #{s[1]} "
          imageid = parentid[0..21]
        when "RepoTags"
          repotags = "#{s[0]}: #{s[1]} ".gsub("RepoTags", "Image")
        when "Size"
          num = s[1].to_i.to_filesize
          size = "#{s[0]}: #{num} "
        when "VirtualSize"
          num = s[1].to_i.to_filesize
          virtualsize = "#{s[0]}: #{num} "
        end
        print "#{created} #{imageid} #{repotags} #{size} #{virtualsize}"
      }
      print "\n"
    }
  when action = "containers"
    response = apiget("#{@url}")
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
    puts "\e[1;30mList of Docker Containers\e[0m\ "
    puts "\e[1;30m=========================\e[0m\ "
    puts ""
    #puts j.length
    0.upto(j.length - 1) {|n|
      j[n].each {|s|
       case s[0]
       when "Command"
         cmd = "#{s[0]}: #{s[1]}".strip
       when "Id"
         id = "#{s[0]}: #{s[1]} "
         cointainerid = id[0..15].gsub("Id", "ContainerId").strip
       when "Image"
         image = "#{s[0]}: #{s[1]}"
       when "Names"
         names = "#{s[0]}: #{s[1]}".strip
       when "Status"
         status = "#{s[0]}: #{s[1]}"
       when "Created"
         ftime = Time.at(s[1]).to_s
         ctime = ftime[0..18]
         created = "#{s[0]}: #{ctime} ".strip
       end
       print "#{names} #{cointainerid} #{image} #{cmd} #{status}".gsub("   Command:", "Command:")
       #pp s
      }
      print "\n"
    }
    #pp j
  when action = "start"
    response = apipost("#{@url}")
    if response.code == "204"
      puts "\e[1;30mStart Successfull\e[0m\ "
    elsif response.code == "304"
      puts "\e[1;30mContainer already started\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "pause"
    response = apipost("#{@url}")
    if response.code == "204"
      puts "\e[1;30mPaused Successfull\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "unpause"
    response = apipost("#{@url}")
    if response.code == "204"
      puts "\e[1;30mResumed Successfull\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "rename"
    response = apipost("#{@url}")
    if response.code == "204"
      puts "\e[1;30mRenamed Successfully\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "409"
      puts "\e[1;30mName conflict with another container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "stop"
    response = apipost("#{@url}")
    if response.code == "204"
      puts "\e[1;30mStopped Successfully\e[0m\ "
    elsif response.code == "304"
      puts "\e[1;30mContainer already stopped\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "inspect"
    response = apiget("#{@url}")
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
    pp j
  when action = "remove"
    response = apidelete("#{@url}")
    if response.code == "204"
      puts "\e[1;30mRemoved Successfully\e[0m\ "
    elsif response.code == "400"
      puts "\e[1;30mBad parameter\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "imageremove"
    response = apidelete("#{@url}")
    if response.code == "200"
      puts "\e[1;30mRemoved Successfully\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such image\e[0m\ "
    elsif response.code == "409"
      puts "\e[1;30mConflict removing image\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "imagecommit"
    response = apipost("#{@url}")
    if response.code == "201"
      puts "\e[1;30mImage Commit Successfull\e[0m\ "
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "create"
    body = String.new
    File.open("#{Dir.home}/.dockerfish/#{@containerjson}", 'r') {|n|
      n.each_line {|l|
        body << l
      }
    }
    body.gsub!("##hostname##", @hostname)
    body.gsub!("##image##", @image)
    puts body
    if !defined? @name; @name = @hostname; end
    response = apipost("#{@url}?name=#{@name}", body)
    if response.code == "201"
      puts "\e[1;30mContainer Creation Successfull\e[0m\ "
      j = JSON.parse(response.body)
      n = 0
      0.upto(j.length - 1) {|n|
        j[n].each {|s|
          puts "#{s[0]}: #{s[1]}"
        }
        print "\n"
      }
    elsif response.code == "404"
      puts "\e[1;30mNo such container\e[0m\ "
    elsif response.code == "406"
      puts "\e[1;30mImposible to attach\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "imagehistory"
    response = apiget("#{@url}")
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
    puts "\e[1;30mImage history\e[0m\ "
    puts "\e[1;30m=================\e[0m\ "
    puts ""
    #puts j.length
    0.upto(j.length - 1) {|n|
      j[n].each {|s|
       case s[0]
       when "Id"
         id = "#{s[0]}: #{s[1]} "
         cointainerid = id[0..15].strip
       when "Created"
         ftime = Time.at(s[1]).to_s
         ctime = ftime[0..18]
         created = "#{s[0]}: #{ctime} ".strip
       when "CreatedBy"
         createdby = "#{s[0]}: #{s[1]}"
       end
       print "#{id} #{created} #{createdby}"
       #pp s
      }
      print "\n"
    }
    if response.code == "404"
      puts "\e[1;30mNo such image\e[0m\ "
    elsif response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "containerprocs"
    response = apiget("#{@url}")
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
    pp j
  when action = "search"
    response = apiget("#{@url}")
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
    puts "\e[1;30mSearch Results\e[0m\ "
    puts "\e[1;30m==============\e[0m\ "
    puts ""
    #puts j.length
    0.upto(j.length - 1) {|n|
      j[n].each {|s|
       case s[0]
       when "description"
         description = "#{s[0]}: #{s[1]} "
       when "is_offical"
         offical = "#{s[0]}: #{s[1]} "
       when "is_automated"
         automated = "#{s[0]}: #{s[1]}"
       when "name"
         name = "Image: \e[1;36m\ [\"#{s[1]}\"] \e[0m\ "
       when "star_count"
         starcount = "Star rating: #{s[1]}"
       end
       print "#{name} #{starcount}".lstrip
       #pp s
      }
      print "\n"
    }
    if response.code == "500"
      puts "\e[1;30mServer error\e[0m\ "
    end
  when action = "menu"
    while buf = Readline.readline("\e[1;32m\DockerFish>\e[0m\ ", true)
      begin
        puts "\n"
        puts "\e[1;38m|Our items in the boat yard|\e[0m\ \n"
        puts "\e[1;38m\\==========================/\e[0m\ "
        puts "\n"
        puts "\e[1;36m1)\e[0m\ List Images"
        puts "\e[1;36m2)\e[0m\ List Containers"
        puts "\e[1;36m3)\e[0m\ Start/Restart Container"
        puts "\e[1;36m4)\e[0m\ Stop Container"
        puts "\e[1;36m5)\e[0m\ Rename Container"
        puts "\e[1;36m6)\e[0m\ Create Container from Image"
        puts "\e[1;36m7)\e[0m\ Remove Container"
        puts "\e[1;36m8)\e[0m\ Inspect Container"
        puts "\e[1;36m9)\e[0m\ View Image History"
        puts "\e[1;36m10)\e[0m\ Search for Images"
        puts "\e[1;36m11)\e[0m\ List Container processes"
        puts "\e[1;36m12)\e[0m\ Remove Image"
        puts "\e[1;36m13)\e[0m\ Pause container"
        puts "\e[1;36m14)\e[0m\ Resume container"
        puts "\e[1;36m15)\e[0m\ Create a new image from a container's changes"
        puts ""
        puts "m: This menu"
        puts "b: Bookmarks"
        puts "q: Exit Dockerfish\n"
        puts "\n"
        case buf
        when "1"
          chooser("/images/json?all=0")
          #puts "#{@url}"
          apicall("images")
        when "2"
          chooser("/containers/json?all=1")
          #puts "#{@url}"
          apicall("containers")
        when "3"
          while buf2 = Readline.readline("\e[1;33m\Enter Container to Start>\e[0m\ ", true)
            puts "\e[1;30mStarting Container #{buf2}\e[0m\ "
            chooser("/containers/#{buf2}/restart")
            #puts "#{@url}"
            apicall("start")
            break
          end
        when "4"
          while buf2 = Readline.readline("\e[1;33m\Enter Container to Stop>\e[0m\ ", true)
            puts "\e[1;30mStopping Container #{buf2}\e[0m\ "
            chooser("/containers/#{buf2}/stop")
            #puts "#{@url}"
            apicall("stop")
            break
          end
        when "5"
          while buf2 = Readline.readline("\e[1;33m\Enter Container To Rename>\e[0m\ ", true)
            while buf3 = Readline.readline("\e[1;33m\Enter new name>\e[0m\ ", true)
              chooser("/containers/#{buf2}/rename?name=#{buf3}")
              break
            end
            #puts "#{@url}"
            apicall("rename")
            break
          end
        when "6"
          while buf2 = Readline.readline("\e[1;33m\Enter Image to use>\e[0m\ ", true)
            @image = buf2
            while buf3 = Readline.readline("\e[1;33m\Enter Container name(s)>\e[0m\ ", true)
              chooser("/containers/create")
              @hostname = buf3
              apicall("create")
              break
            end
            break
          end
        when "7"
          while buf2 = Readline.readline("\e[1;33m\Enter Container to Remove>\e[0m\ ", true)
            chooser("/containers/#{buf2}")
            #puts "#{@url}"
            apicall("remove")
            break                
          end
        when "8"
          while buf2 = Readline.readline("\e[1;33m\Enter Container to Inspect>\e[0m\ ", true)
            chooser("/containers/#{buf2}/json")
            #puts "#{@url}"
            apicall("inspect")
            break
          end
        when "9"
          while buf2 = Readline.readline("\e[1;33m\View history of image>\e[0m\ ", true)
            chooser("/images/#{buf2}/history")
            #puts "#{@url}"
            apicall("imagehistory")
            break
          end
        when "10"
          while buf2 = Readline.readline("\e[1;33m\Search for images>\e[0m\ ", true)
            chooser("/images/search?term=#{buf2}")
            #puts "#{@url}"
            apicall("search")
            break
          end
        when "11"
          while buf2 = Readline.readline("\e[1;33m\Enter Container to query>\e[0m\ ", true)
            chooser("/containers/#{buf2}/top")
            #puts "#{@url}"
            apicall("containerprocs")
            break
          end
        when "12"
          while buf2 = Readline.readline("\e[1;33m\Enter Image to remove>\e[0m\ ", true)
            chooser("/images/#{buf2}")
            #puts "#{@url}"
            apicall("imageremove")
            break
          end
        when "13"
          while buf2 = Readline.readline("\e[1;33m\Pause Container name / id>\e[0m\ ", true)
            chooser("/containers/#{buf2}/pause")
            #puts "#{@url}"
            apicall("pause")
            break
          end
        when "14"
          while buf2 = Readline.readline("\e[1;33m\Resume Container name / id>\e[0m\ ", true)
            chooser("/containers/#{buf2}/unpause")
            #puts "#{@url}"
            apicall("pause")
            break
          end
        when "15"
          while buf2 = Readline.readline("\e[1;33m\Enter Container to Image>\e[0m\ ", true)
            while buf3 = Readline.readline("\e[1;33m\Enter your commit comment important>\e[0m\ ", true)
              commentenc = URI.encode(buf3)
              break
            end
            while buf4 = Readline.readline("\e[1;33m\Enter your Repository name>\e[0m\ ", true)
              chooser("/commit?container=#{buf2}&comment=#{commentenc}&repo=#{buf4}")
              break
            end
            #puts "#{@url}"
            apicall("imagecommit")
            break
          end
        when "m"
          apicall("menu")
        #when "b"
        #  ret = bookmarks("#{BOOKMARKSFILE}")
        #  @baseurl = ret
        #  instance_variable_set("@url", ret)
        when "q"
          exit
        when "f"
          banner
        end
      rescue NoMethodError
      end
    end
  end
end

#apidelete(url) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/dockerfish.rb', line 105

def apidelete(url)
  uri = URI.parse("#{url}")
  http = Net::HTTP.new(uri.host, uri.port)
  begin
    response = http.request(Net::HTTP::Delete.new(uri.request_uri))
  rescue
    puts "Error posting data"
  end
end

#apiget(url) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/dockerfish.rb', line 80

def apiget(url)
  uri = URI.parse("#{url}")
  #puts "Request URI: #{url}"
  http = Net::HTTP.new(uri.host, uri.port)
  begin
    response = http.request(Net::HTTP::Get.new(uri.request_uri))  
  rescue
    puts "Error retrieving data"
  end
end

#apipost(url, body = "") ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dockerfish.rb', line 91

def apipost(url, body="")
  uri = URI.parse("#{url}")
  #puts "Request URI: #{url}"
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_content_type("application/json")
  begin
    request.body = body unless body.empty?
    response = http.request(request)
  rescue
    puts "Error posting data"
  end
end


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dockerfish.rb', line 45

def banner
  puts "Welcome to DockerFish Version #{VERSION}"
  puts "---------------------------------"
  puts "Interactive CLI Docker Interface"
  print "\[\e[1;31m\]               |
               |
              ,|.
             ,\|/.
           ,' .V. `.
          / .     . \
         /_`       '_\
        ,' .:     ;, `.
        |@)|  . .  |(@|
   ,-._ `._';  .  :`_,' _,-.
  '--  `-\ /,-===-.\ /-'  --`
 (----  _|  ||___||  |_  ----)
  `._,-'  \  `-.-'  /  `-._,'
           `-.___,-' ap\[\e[0m\]\n"
  puts ""
  puts "TIPS: Please note ContainerId's / Names are interchangeable"
  puts "You can also use short ContainerId's as long as there is a unique match"
  puts "Start with Docker API Enabled: /usr/bin/docker --api-enable-cors=true -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock -d"
  puts "Add a symlink ln -s /home/brian/Projects/DockerFish/dockerfish.rb /usr/bin/dockerfish\n\n"
  puts "Container Config file: #{Dir.home}/.dockerfish/#{@containerjson}\n\n"
  if File.exists?("#{Dir.home}/.dockerfish/#{@containerjson}") == false
    puts "\e[1;31m\WARNING: \e[0m\ \e[1;32m\ To Create Containers \e[1;33m\ mkdir #{Dir.home}/.dockerfish\e[0m\ \e[1;32m\ and copy the provided container.json into it !!!\e[0m\ \n\n"
  end
  puts "--help # For more information"
  puts "\n"
  puts "Enjoy!"
  puts "\n"
  puts "Coded by Brian Hood"
  version
end

#chooser(opts) ⇒ Object



128
129
130
# File 'lib/dockerfish.rb', line 128

def chooser(opts)
  @url = "#{@baseurl}#{opts}"
end

#versionObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dockerfish.rb', line 115

def version
  @url = "#{@baseurl}/version"
  uri = URI.parse("#{@url}")
  begin
    response = apiget("#{@url}")
    puts "Server Info:\n\n"
    j = JSON.parse(response.body)
    pp j
  rescue JSON::ParserError, NoMethodError
    puts "Could not read JSON data"
  end
end