Class: DockerTask::Task

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/docker_task/task.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :namespace => :docker
}
DOCKER_CMD =
'docker'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { }) {|_self| ... } ⇒ Task

Returns a new instance of Task.

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
# File 'lib/docker_task/task.rb', line 15

def initialize(options = { })
  @options = DEFAULT_OPTIONS.merge(options)
  yield(self) if block_given?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/docker_task/task.rb', line 13

def options
  @options
end

Instance Method Details

#container_nameObject



25
26
27
# File 'lib/docker_task/task.rb', line 25

def container_name
  @options[:container_name]
end

#define!Object



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
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
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
# File 'lib/docker_task/task.rb', line 33

def define!
  namespace ns do
    desc 'Perform whole cycle of destroy (if any), build, and run'
    task :reset do
      invoke_task('destroy')
      invoke_task('build')
      invoke_task('run')
    end

    desc 'Rebuild the docker image'
    task :rebuild do
      invoke_task('destroy')
      invoke_task('build')
    end

    desc 'Build a new docker image based on the Dockerfile'
    task :build do
      docker_do 'build -t %s .' % image_name
    end

    desc 'Show help, and how to use this docker tool'
    task :help do
      puts help_text
    end

    desc 'Run the latest docker image'
    task :run do
      run_opts = [ ]

      if ENV['INTERACTIVE'] == '1'
        run_opts << '--rm -t -i'
      else
        run_opts = @options[:run].call(self, run_opts)

        run_opts << '-d'
        run_opts << '--name=%s' % container_name
      end

      run_opts << '%s:%s' % [ @options[:image_name], @options.fetch(:image_tag, 'latest') ]

      if ENV['INTERACTIVE'] == '1'
        docker_do 'run %s %s' % [ run_opts.join(' '), '/bin/bash -l' ], :ignore_fail => true
      else
        docker_do 'run %s' % run_opts.join(' ')
      end
    end

    desc 'Run docker in interactive mode (with bash shell)'
    task :runi do
      ENV['INTERACTIVE'] = '1'
      invoke_task('run')
    end

    task :bash do
      if @options.include?(:bash)
        exec_cmd = @options[:bash]
      else
        exec_cmd = 'bash -l'
      end

      docker_do('exec -it %s %s' % [ container_name, exec_cmd ], :ignore_fail => true)
    end

    desc 'Run docker container'
    task :start do
      docker_do 'start %s' % container_name
    end

    desc 'Stop docker container'
    task :stop do
      docker_do 'stop %s; true' % container_name
    end

    desc 'Restart docker container'
    task :restart do
      invoke_task('stop')
      invoke_task('start')
    end

    desc 'Delete container, and create a new one'
    task :reload do
      docker_do 'kill %s; true' % container_name
      docker_do 'rm %s; true' % container_name

      invoke_task('run')
    end

    desc 'Push latest built image to repo'
    task :push do
      if @options[:push_repo]
        should_create_tag = false

        if !ENV['PUSH_MIRROR'].nil? && !ENV['PUSH_MIRROR'].empty?
          mk = ENV['PUSH_MIRROR']
          pm = @options[:push_mirrors]

          if !pm.nil? && !pm.empty?
            push_repo = pm[mk.to_sym]
          else
            push_repo = nil
          end

          if push_repo.nil? || push_repo.empty?
            fail "Mirror %s not found" % mk
          end
        else
          push_repo = '%s/%s' % [ @options[:registry], @options[:push_repo] ]
        end

        docker_do 'tag %s %s' % [ @options[:image_name], push_repo ]
        docker_do 'push %s' % push_repo
      else
        puts 'Please specify a push_repo for this docker context'
      end
    end

    desc 'Pull from registry based on push_repo options'
    task :pull do
      if @options[:push_repo]
        pull_repo = '%s/%s' % [ @options[:registry], @options[:push_repo] ]

        docker_do 'pull %s' % pull_repo
        docker_do 'tag %s %s' % [ pull_repo, @options[:image_name] ]
      else
        puts 'Please specify a push_repo for this docker context'
      end
    end

    desc 'Re-tag a local copy from latest remote (will not pull)'
    task :retag do
      if @options[:push_repo]
        pull_repo = '%s/%s' % [ @options[:registry], @options[:push_repo] ]
        docker_do 'tag %s %s' % [ pull_repo, @options[:image_name] ]
      else
        puts 'Please specify a push_repo for this docker context'
      end
    end

    desc 'Destroy container and delete image'
    task :destroy do
      invoke_task('remove')
      docker_do 'rmi %s' % image_name, :ignore_fail => true
    end

    desc 'Stop and remove container'
    task 'remove' do
      docker_do 'kill %s' % container_name, :ignore_fail => true
      docker_do 'rm %s' % container_name, :ignore_fail => true
    end
  end
end

#docker_do(cmd, opts = { }) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/docker_task/task.rb', line 185

def docker_do(cmd, opts = { })
  if opts[:ignore_fail]
    cmd += '; true'
  end

  sh '%s %s' % [ DOCKER_CMD, cmd ]
end

#help_textObject



197
198
199
200
201
# File 'lib/docker_task/task.rb', line 197

def help_text
  <<-HELP
This is a set of Rake tasks that you can include in your own Rakefile, to aid in managing docker images and containers.
HELP
end

#image_nameObject



29
30
31
# File 'lib/docker_task/task.rb', line 29

def image_name
  @options[:image_name]
end

#invoke_task(tname) ⇒ Object



193
194
195
# File 'lib/docker_task/task.rb', line 193

def invoke_task(tname)
  Rake::Task['%s:%s' % [ ns, tname ]].invoke
end

#task_namespaceObject Also known as: ns



20
21
22
# File 'lib/docker_task/task.rb', line 20

def task_namespace
  @options[:namespace]
end