Class: DockerManager

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

Instance Method Summary collapse

Instance Method Details

#get_environmentsObject



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
# File 'lib/docker_manager.rb', line 5

def get_environments
    command = "docker ps --all"
    stdout, stderr, status = Open3.capture3(command)
    
    match_envs = stdout.scan(/wpd_web_(.+_\d+)/)
    match_web = stdout.scan(/.*0\.0\.0\.0:(\d+)->80\/tcp.*wpd_web_(.+_\d+)/)
    match_db = stdout.scan(/.*0\.0\.0\.0:(\d+)->3306\/tcp.*wpd_db_(.+_\d+)/)
    
    match = Array.new
    match_envs.each do |env|

        web_port_match = match_web.select { |item| item[1] == env[0] }
        db_port_match = match_db.select { |item| item[1] == env[0] }

        web_port = "<stopped>"
        if web_port_match.count != 0
            web_port = web_port_match[0][0]
        end

        db_port = "<stopped>"
        if db_port_match.count != 0
            db_port = db_port_match[0][0]
        end
        match.push({"env" => env[0], "web_port" => web_port, "db_port" => db_port})
    end
    
    return match
end

#get_new_wpd_idObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/docker_manager.rb', line 34

def get_new_wpd_id
    command = "docker ps --all"
    stdout, stderr, status = Open3.capture3(command)
    
    match = stdout.scan(/wpd_.+_(\d+)/)
    current_ids = match.uniq
    
    if current_ids.count > 89
        puts "No more available ids"
        exit
    end
    
    new_id = nil
    while new_id == nil
        rand_id = rand(10..99)
        if !match.include? rand_id
            new_id = rand_id
        end
    end
    
    return new_id
end

#remove(environment) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/docker_manager.rb', line 83

def remove environment
    
    self.stop environment
    
    web = "wpd_web_" + environment
    db = "wpd_db_" + environment
    
    puts "Remove: " + web
    command="docker rm " + web
    system(command)
    
    puts "Remove: " + db
    command="docker rm " + db
    system(command)
end

#start(environment) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/docker_manager.rb', line 57

def start environment
    web = "wpd_web_" + environment
    db = "wpd_db_" + environment
    
    puts "Start: " + db
    command="docker start " + db
    system(command)
    
    puts "Start: " + web
    command="docker start " + web
    system(command)        
end

#stop(environment) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/docker_manager.rb', line 70

def stop environment
    web = "wpd_web_" + environment
    db = "wpd_db_" + environment
    
    puts "Stop: " + web
    command="docker stop " + web
    system(command)
    
    puts "Stop: " + db
    command="docker stop " + db
    system(command)
end