-
Notifications
You must be signed in to change notification settings - Fork 0
/
dk.rb
88 lines (65 loc) · 2.01 KB
/
dk.rb
1
2
3
4
5
6
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def CreateMachine(name)
token_file = File.join(File.dirname(__FILE__), "do.token")
f = File.open(token_file, "rb")
token = f.read
f.close
token = token.strip!
system "docker-machine create --driver digitalocean --digitalocean-image ubuntu-14-04-x64 --digitalocean-access-token #{token} #{name}"
puts 'Machine created'
end
def SaveImageToMachine(machine, image)
puts `docker save -o tmp.tar #{image}`
UploadImageToMachine(machine)
puts 'Machine saved to the remote server'
puts `docker-machine ssh #{machine} docker load -i tmp.tar`
system "rm tmp.tar"
puts 'Machine loaded to the remote docker server'
end
def UploadImageToMachine(machine)
system "docker-machine scp tmp.tar #{machine}:/root"
end
def RunImageInMachine(machine, image)
system "docker-machine ssh #{machine} docker run -p 8080:8080 -t #{image}"
puts "#{image} up and running in #{machine}"
end
def ListRemoteMachines()
system "docker-machine ls"
end
def RemoveRemoteMachine(machine)
system "docker-machine rm #{machine}"
end
def PrintHelp()
puts "Commands available"
puts "create [name of droplet] -- Creates a new droplet"
puts "save [name of machine] [name of image] -- Save a local image to a droplet"
puts "run [name of machine] [name of image] -- Runs an image in a droplet"
puts "remove [name of machine] -- Remvoe a remote machine"
puts "list -- Lists all remote machines"
end
PrintHelp()
prompt = "> "
print prompt
while (input = gets.chomp)
break if input == "exit"
if input.include? "create"
CreateMachine(input.split[1])
end
if input.include? "save"
SaveImageToMachine(input.split[1], input.split[2])
end
if input.include? "run"
RunImageInMachine(input.split[1], input.split[2])
end
if input == "list"
ListRemoteMachines()
end
if input.include? "remove"
RemoveRemoteMachine(input.split[1])
end
if input == "help"
PrintHelp()
puts " "
end
system(input)
print prompt
end